1 <?php
3 class workgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $gotoMode= "locked";
7 var $gotoSyslogServer= "";
8 var $gotoSyslogServers= array();
9 var $gotoNtpServer= array();
10 var $gotoNtpServers= array();
11 var $gotoSndModule= "";
12 var $gotoFloppyEnable= "";
13 var $gotoCdromEnable= "";
14 var $description= "";
15 var $ghCpuType= "-";
16 var $ghMemSize= "-";
17 var $ghUsbSupport= "-";
18 var $ghNetNic= array();
19 var $ghIdeDev= array();
20 var $ghScsiDev= array();
21 var $ghGfxAdapter= "-";
22 var $ghSoundAdapter= "-";
23 var $gotoLastUser= "-";
24 var $FAIscript= "";
25 var $view_logged = FALSE;
27 /* Needed values and lists */
28 var $base= "";
29 var $cn= "";
30 var $l= "";
31 var $orig_dn= "";
33 /* Plugin side filled */
34 var $modes= array();
36 var $netConfigDNS;
38 var $inheritTimeServer = true;
40 /* attribute list for save action */
41 var $ignore_account= TRUE;
42 var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
43 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44 "ghCpuType", "ghMemSize", "ghUsbSupport", "description",
45 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
46 var $objectclasses= array("top", "gotoWorkstation", "GOhard");
48 var $validActions = array("reboot" => "", "localboot" => "", "halt" => "", "update" => "", "reinstall" => "",
49 "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
51 var $fai_activated = FALSE;
53 var $member_of_ogroup = FALSE;
55 function workgeneric (&$config, $dn= NULL, $parent= NULL)
56 {
57 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
58 if(!empty($tmp)){
59 $this->fai_activated = TRUE;
60 }
62 plugin::plugin ($config, $dn, $parent);
64 if(!isset($this->parent->by_object['ogroup'])){
65 $ldap = $this->config->get_ldap_link();
66 $ldap->cd ($this->config->current['BASE']);
67 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
68 $this->member_of_ogroup = $ldap->count() >= 1;
69 }
71 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
73 /* Read arrays */
74 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
75 if (!isset($this->attrs[$val])){
76 continue;
77 }
78 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
79 array_push($this->$val, $this->attrs[$val][$i]);
80 }
81 }
83 /* Create used ntp server array */
84 $this->gotoNtpServer= array();
85 if(isset($this->attrs['gotoNtpServer'])){
86 $this->inheritTimeServer = false;
87 unset($this->attrs['gotoNtpServer']['count']);
88 foreach($this->attrs['gotoNtpServer'] as $server){
89 $this->gotoNtpServer[$server] = $server;
90 }
91 }
93 /* Set inherit checkbox state */
94 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
95 $this->inheritTimeServer = true;
96 $this->gotoNtpServer=array();
97 }
99 /* You can't inherit the NTP service, if we are not member in an object group */
100 if(!$this->member_of_ogroup){
101 $this->inheritTimeServer = FALSE;
102 }
104 /* Create available ntp options */
105 $tmp = $this->config->data['SERVERS']['NTP'];
106 $this->gotoNtpServers = array();
107 foreach($tmp as $key => $server){
108 if($server == "default") continue;
109 $this->gotoNtpServers[$server] = $server;
110 }
112 $this->modes["active"]= _("Activated");
113 $this->modes["locked"]= _("Locked");
115 /* Set base */
116 if ($this->dn == "new"){
117 $ui= get_userinfo();
118 $this->base= dn2base($ui->dn);
119 } else {
120 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
121 }
123 /* Create an array of all Syslog servers */
124 $tmp = $this->config->data['SERVERS']['SYSLOG'];
125 foreach($tmp as $server){
126 $visible = $server;
127 if($server == "default" && $this->member_of_ogroup) {
128 $visible = "["._("inherited")."]";
129 }
130 $this->gotoSyslogServers[$server] = $visible;
131 }
133 /* Save 'dn' for later referal */
134 $this->orig_dn= $this->dn;
135 }
138 function set_acl_base($base)
139 {
140 plugin::set_acl_base($base);
141 $this->netConfigDNS->set_acl_base($base);
142 }
144 function set_acl_category($cat)
145 {
146 plugin::set_acl_category($cat);
147 $this->netConfigDNS->set_acl_category($cat);
148 }
150 function execute()
151 {
152 /* Call parent execute */
153 plugin::execute();
155 if($this->is_account && !$this->view_logged){
156 $this->view_logged = TRUE;
157 new log("view","workstation/".get_class($this),$this->dn);
158 }
160 /* Do we need to flip is_account state? */
161 if(isset($_POST['modify_state'])){
162 if($this->is_account && $this->acl_is_removeable()){
163 $this->is_account= FALSE;
164 }elseif(!$this->is_account && $this->acl_is_createable()){
165 $this->is_account= TRUE;
166 }
167 }
169 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->validActions[$_POST['saction']]) ){
170 $action= $_POST['saction'];
172 /* Check if we have an DaemonEvent for this action */
173 if(class_available("DaemonEvent")){
174 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
175 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
176 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
177 $tmp = new $evt['CLASS_NAME']($this->config);
178 $tmp->add_targets(array($this->netConfigDNS->macAddress));
179 $tmp->set_type(TRIGGERED_EVENT);
180 $o_queue = new gosaSupportDaemon();
181 if(!$o_queue->append($tmp)){
182 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
183 $o_queue->get_error()),ERROR_DIALOG);
184 }
185 }
186 } else {
187 msg_dialog::display(_("Event error"),
188 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
189 }
192 }
194 /* Do we represent a valid terminal? */
195 if (!$this->is_account && $this->parent === NULL){
196 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
197 _("This 'dn' has no workstation features.")."</b>";
198 return($display);
199 }
201 /* Base select dialog */
202 $once = true;
203 foreach($_POST as $name => $value){
204 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
205 $once = false;
206 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
207 $this->dialog->setCurrentBase($this->base);
208 }
209 }
211 /* Dialog handling */
212 if(is_object($this->dialog)){
213 /* Must be called before save_object */
214 $this->dialog->save_object();
216 if($this->dialog->isClosed()){
217 $this->dialog = false;
218 }elseif($this->dialog->isSelected()){
220 /* A new base was selected, check if it is a valid one */
221 $tmp = $this->get_allowed_bases();
222 if(isset($tmp[$this->dialog->isSelected()])){
223 $this->base = $this->dialog->isSelected();
224 }
226 $this->dialog= false;
227 }else{
228 return($this->dialog->execute());
229 }
230 }
232 /* Add new ntp Server to our list */
233 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
234 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
235 }
237 /* Delete selected NtpServer for list of used servers */
238 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
239 foreach($_POST['gotoNtpServerSelected'] as $name){
240 unset($this->gotoNtpServer[$name]);
241 }
242 }
244 /* Fill templating stuff */
245 $smarty= get_smarty();
247 /* Set acls */
248 $tmp = $this->plInfo();
249 foreach($tmp['plProvidedAcls'] as $name => $translation){
250 $smarty->assign($name."ACL",$this->getacl($name));
251 }
253 $smarty->assign("cn", $this->cn);
254 $smarty->assign("description", $this->description);
255 $smarty->assign("l", $this->l);
256 $smarty->assign("bases", $this->get_allowed_bases());
257 $smarty->assign("staticAddress", "");
259 $tmp = array();
260 foreach($this->gotoNtpServers as $server){
261 if(!in_array($server,$this->gotoNtpServer)){
262 $tmp[$server] = $server;
263 }
264 }
265 $smarty->assign("gotoNtpServers",$tmp);
267 /* Check if workstation is online */
268 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
269 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
270 "update" => _("System update"),
271 "reinstall" => _("Reinstall"),
272 "rescan" => _("Rescan hardware"),
273 "memcheck" => _("Memory test"),
274 "localboot" => _("Force localboot"),
275 "sysinfo" => _("System analysis")));
276 } else {
277 $smarty->assign("actions", array("wake" => _("Wake up"),
278 "reinstall" => _("Reinstall"),
279 "update" => _("System update"),
280 "memcheck" => _("Memory test"),
281 "localboot" => _("Force localboot"),
282 "sysinfo" => _("System analysis")));
283 }
284 /* Arrays */
285 $smarty->assign("modes", $this->modes);
286 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
287 $smarty->assign("syslogservers", $this->gotoSyslogServers);
288 $smarty->assign("fai_activated",$this->fai_activated);
290 $ntpser = array();
291 foreach($this->gotoNtpServers as $server){
292 if(!in_array($server,$this->gotoNtpServer)){
293 $ntpser[$server] = $server;
294 }
295 }
296 $smarty->assign("gotoNtpServers", $ntpser);
298 /* Variables */
299 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
300 $smarty->assign($val."_select", $this->$val);
301 }
303 /* tell smarty the inherit checkbox state */
304 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
305 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
307 $str = $this->netConfigDNS->execute();
308 if(is_object($this->netConfigDNS->dialog)){
309 return($str);
310 }
311 $smarty->assign("netconfig", $str);
313 /* Show main page */
314 return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
315 }
317 function remove_from_parent()
318 {
319 if($this->acl_is_removeable()){
321 $this->netConfigDNS->remove_from_parent();
322 $ldap= $this->config->get_ldap_link();
323 $ldap->rmdir($this->dn);
324 new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
325 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
327 /* Optionally execute a command after we're done */
328 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
330 /* Delete references to object groups */
331 $ldap->cd ($this->config->current['BASE']);
332 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
333 while ($ldap->fetch()){
334 $og= new ogroup($this->config, $ldap->getDN());
335 unset($og->member[$this->dn]);
336 $og->save ();
337 }
338 }
340 if(isset($_POST["inheritAll"])){
341 $this->set_everything_to_inherited();
342 }
343 }
346 /* Save data to object */
347 function save_object()
348 {
350 /* Create a base backup and reset the
351 base directly after calling plugin::save_object();
352 Base will be set seperatly a few lines below */
353 $base_tmp = $this->base;
354 plugin::save_object();
355 $this->base = $base_tmp;
357 /* Save base, since this is no LDAP attribute */
358 $tmp = $this->get_allowed_bases();
359 if(isset($_POST['base'])){
360 if(isset($tmp[$_POST['base']])){
361 $this->base= $_POST['base'];
362 }
363 }
365 $this->netConfigDNS->save_object();
367 /* Set inherit mode */
368 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
369 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
370 $this->inheritTimeServer = true;
371 }else{
372 $this->inheritTimeServer = false;
373 }
374 }
376 if(isset($_POST["inheritAll"])){
377 $this->set_everything_to_inherited();
378 }
379 }
382 /* Check supplied data */
383 function check()
384 {
385 /* Call common method to give check the hook */
386 $message= plugin::check();
388 /* Skip IP & Mac checks if this is a template */
389 if($this->cn != "wdefault"){
390 $message= array_merge($message, $this->netConfigDNS->check());
391 }
393 $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
395 if ($this->cn == ""){
396 $message[]= _("The required field 'Workstation name' is not set.");
397 }
399 /* Check if given name is a valid host/dns name */
400 if(!tests::is_dns_name($this->cn)){
401 $message[] = _("Please specify a valid name for this object.");
402 }
404 if ($this->orig_dn != $this->dn){
405 $ldap= $this->config->get_ldap_link();
406 $ldap->cd ($this->base);
408 if($this->cn == "wdefault"){
409 $ldap->cat($this->dn);
410 }else{
411 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
412 }
413 if ($ldap->count() != 0){
414 while ($attrs= $ldap->fetch()){
415 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
416 continue;
417 } else {
418 if ($attrs['dn'] != $this->orig_dn){
419 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
420 break;
421 }
422 }
423 }
424 }
425 }
427 /* Check for valid ntpServer selection */
428 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
429 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
430 }
432 return ($message);
433 }
436 /* Save to LDAP */
437 function save()
438 {
439 /* Detect mode changes */
440 $activate= (isset($this->saved_attributes['gotoMode']) &&
441 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
442 $this->gotoMode == "active" &&
443 tests::is_ip($this->netConfigDNS->ipHostNumber));
444 plugin::save();
446 /* Strip out 'default' values */
447 foreach (array("gotoSyslogServer") as $val){
449 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
450 $this->attrs[$val]= array();
451 }
452 }
454 /* Add missing arrays */
455 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
456 if (isset ($this->$val) && count ($this->$val) != 0){
457 $this->attrs["$val"]= $this->$val;
458 }
459 }
461 /* Remove all empty values */
462 if ($this->orig_dn == 'new'){
463 $attrs= array();
464 foreach ($this->attrs as $key => $val){
465 if (is_array($val) && count($val) == 0){
466 continue;
467 }
468 $attrs[$key]= $val;
469 }
470 $this->attrs= $attrs;
471 }
473 /* Update ntp server settings */
474 if($this->inheritTimeServer){
475 if($this->is_new){
476 if(isset($this->attrs['gotoNtpServer'])){
477 unset($this->attrs['gotoNtpServer']);
478 }
479 }else{
480 $this->attrs['gotoNtpServer'] = array();
481 }
482 }else{
483 /* Set ntpServers */
484 $this->attrs['gotoNtpServer'] = array();
485 foreach($this->gotoNtpServer as $server){
486 $this->attrs['gotoNtpServer'][] = $server;
487 }
488 }
490 /* Write back to ldap */
491 $ldap= $this->config->get_ldap_link();
492 if ($this->orig_dn == 'new'){
493 $ldap->cd($this->config->current['BASE']);
494 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
495 $ldap->cd($this->dn);
496 $ldap->add($this->attrs);
497 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
498 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
499 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
500 } else {
501 if ($this->orig_dn != $this->dn){
502 $this->move($this->orig_dn, $this->dn);
503 }
504 $ldap->cd($this->dn);
505 $this->cleanup();
506 $ldap->modify ($this->attrs);
507 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
509 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
510 }
512 /* cn=default and macAddress=- indicates that this is a template */
513 if($this->cn == "wdefault"){
514 $this->netConfigDNS->macAddress = "-";
515 }
517 $this->netConfigDNS->cn = $this->cn;
518 $this->netConfigDNS->save();
519 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
521 if ($activate && class_available("DaemonEvent")){
523 /* Send installation activation
524 */
525 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
526 $o_queue = new gosaSupportDaemon();
527 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
528 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
529 $tmp = new $evt['CLASS_NAME']($this->config);
530 $tmp->set_type(TRIGGERED_EVENT);
531 $tmp->add_targets(array($this->netConfigDNS->macAddress));
532 if(!$o_queue->append($tmp)){
533 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
534 $o_queue->get_error()),ERROR_DIALOG);
535 }
536 }
537 }
538 }
541 /* Display generic part for server copy & paste */
542 function getCopyDialog()
543 {
544 $vars = array("cn");
545 $smarty = get_smarty();
546 $smarty->assign("cn" ,$this->cn);
547 $smarty->assign("object","workstation");
548 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
549 $ret = array();
550 $ret['string'] = $str;
551 $ret['status'] = "";
552 return($ret);
553 }
556 function saveCopyDialog()
557 {
558 if(isset($_POST['cn'])){
559 $this->cn = $_POST['cn'];
560 }
561 }
564 function PrepareForCopyPaste($source)
565 {
566 plugin::PrepareForCopyPaste($source);
567 if(isset($source['macAddress'][0])){
568 $this->netConfigDNS->macAddress = $source['macAddress'][0];
569 }
570 if(isset($source['ipHostNumber'][0])){
571 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
572 }
574 /* Create used ntp server array */
575 $this->gotoNtpServer= array();
576 if(isset($source['gotoNtpServer'])){
577 $this->inheritTimeServer = false;
578 unset($source['gotoNtpServer']['count']);
579 foreach($source['gotoNtpServer'] as $server){
580 $this->gotoNtpServer[$server] = $server;
581 }
582 }
584 /* Set inherit checkbox state */
585 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
586 $this->inheritTimeServer = true;
587 $this->gotoNtpServer=array();
588 }
589 }
592 /* Return plugin informations for acl handling
593 #FIXME FAIscript seams to ununsed within this class... */
594 static function plInfo()
595 {
596 return (array(
597 "plShortName" => _("Generic"),
598 "plDescription" => _("Workstation generic"),
599 "plSelfModify" => FALSE,
600 "plDepends" => array(),
601 "plPriority" => 0,
602 "plSection" => array("administration"),
603 "plCategory" => array("workstation" => array("description" => _("Workstation"),
604 "objectClass" => "gotoWorkstation")),
605 "plProvidedAcls"=> array(
606 "cn" => _("Workstation name"),
607 "description" => _("Description") ,
608 "l" => _("Location") ,
609 "base" => _("Base") ,
610 "gotoMode" => _("Goto mode"),
611 "gotoSyslogServer" => _("Syslog server"),
612 "gotoNtpServer" => _("Ntp server"),
613 "gotoRootPasswd" => _("Root password"),
614 "FAIstate" => _("Action flag"))
615 ));
616 }
618 function set_everything_to_inherited()
619 {
620 $this->gotoSyslogServer = "default";
621 $this->inheritTimeServer = TRUE;
623 /* Set workstation service attributes to inherited */
624 if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
625 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
626 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
627 $this->parent->by_object['workservice']->$name = "default";
628 }
629 }
631 /* Set workstation startup attributes to inherited */
632 if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
633 $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
634 $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
635 $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
636 }
637 }
638 }
639 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
640 ?>