Code

Udpated gotomasses
[gosa.git] / plugins / admin / systems / class_workstationGeneric.inc
1 <?php
3 class workgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage workstation base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $gotoMode= "locked";
12   var $gotoSyslogServer= "";
13   var $gotoSyslogServers= array();
14   var $gotoNtpServer= array();
15   var $gotoNtpServers= array();
16   var $gotoSndModule= "";
17   var $gotoFloppyEnable= "";
18   var $gotoCdromEnable= "";
19   var $ghCpuType= "-";
20   var $ghMemSize= "-";
21   var $ghUsbSupport= "-";
22   var $ghNetNic= array();
23   var $ghIdeDev= array();
24   var $ghScsiDev= array();
25   var $ghGfxAdapter= "-";
26   var $ghSoundAdapter= "-";
27   var $gotoLastUser= "-";
28   var $FAIscript= "";
29   var $didAction= FALSE;
30   var $FAIstate= "";
31   var $view_logged = FALSE;
33   /* Needed values and lists */
34   var $base= "";
35   var $cn= "";
36   var $l= "";
37   var $orig_dn= "";
39   /* Plugin side filled */
40   var $modes= array();
42   var $netConfigDNS;
44   var $inheritTimeServer = true;
46   /* attribute list for save action */
47   var $ignore_account= TRUE;
48   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
49       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
50       "ghCpuType", "ghMemSize", "ghUsbSupport",
51       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
52   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
54   var $mapActions   = array("reboot"          => "",
55                             "localboot"       => "localboot",
56                             "halt"            => "",
57                             "instant_update"  => "softupdate",
58                             "update"          => "scheduledupdate",
59                             "reinstall"       => "install",
60                             "rescan"          => "",
61                             "memcheck"        => "memcheck",
62                             "sysinfo"         => "sysinfo");
64   
65   var $fai_activated = FALSE;
67   var $member_of_ogroup = FALSE;
69   function workgeneric (&$config, $dn= NULL, $parent= NULL)
70   {
71     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
72     if(!empty($tmp)){
73       $this->fai_activated = TRUE;
74     }
76     plugin::plugin ($config, $dn, $parent);
78     if(!isset($this->parent->by_object['ogroup'])){
79       $ldap = $this->config->get_ldap_link();
80       $ldap->cd ($this->config->current['BASE']);
81       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))",array("cn"));
82       $this->member_of_ogroup = $ldap->count() >= 1;
83     }
85     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
87     /* Read arrays */
88     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
89       if (!isset($this->attrs[$val])){
90         continue;
91       }
92       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
93         array_push($this->$val, $this->attrs[$val][$i]);
94       }
95     }
97     /* Create used ntp server array */
98     $this->gotoNtpServer= array();
99     if(isset($this->attrs['gotoNtpServer'])){
100       $this->inheritTimeServer = false;
101       unset($this->attrs['gotoNtpServer']['count']);
102       foreach($this->attrs['gotoNtpServer'] as $server){
103         $this->gotoNtpServer[$server] = $server;
104       }
105     }
107     /* Set inherit checkbox state */
108     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
109       $this->inheritTimeServer = true;
110       $this->gotoNtpServer=array();
111     }
113     /* You can't inherit the NTP service, if we are not member in an object group */
114     if(!$this->member_of_ogroup){
115       $this->inheritTimeServer = FALSE;
116     }
118     /* Create available ntp options */
119     $tmp = $this->config->data['SERVERS']['NTP'];
120     $this->gotoNtpServers = array();
121     foreach($tmp as $key => $server){
122       if($server == "default") continue;
123       $this->gotoNtpServers[$server] = $server;
124     }
126     $this->modes["active"]= _("Activated");
127     $this->modes["locked"]= _("Locked");
129     /* Set base */
130     if ($this->dn == "new"){
131       $ui= get_userinfo();
132       $this->base= dn2base($ui->dn);
133     } else {
134       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
135     }
137     /* Create an array of all Syslog servers */
138     $tmp = $this->config->data['SERVERS']['SYSLOG'];
139     foreach($tmp as $server){
140       $visible = $server;
141       if($server == "default" && $this->member_of_ogroup) {
142         $visible = "["._("inherited")."]";
143       }
144       $this->gotoSyslogServers[$server] = $visible;
145     }
147     /* Save 'dn' for later referal */
148     $this->orig_dn= $this->dn;
149   }
152   function set_acl_base($base)
153   {
154     plugin::set_acl_base($base);
155     $this->netConfigDNS->set_acl_base($base);
156   }
158   function set_acl_category($cat)
159   {
160     plugin::set_acl_category($cat);
161     $this->netConfigDNS->set_acl_category($cat);
162   }
164   function execute()
165   {
166     /* Call parent execute */
167     plugin::execute();
169     if($this->is_account && !$this->view_logged){
170       $this->view_logged = TRUE;
171       new log("view","workstation/".get_class($this),$this->dn);
172     }
174     /* Do we need to flip is_account state? */
175     if(isset($_POST['modify_state'])){
176       if($this->is_account && $this->acl_is_removeable()){
177         $this->is_account= FALSE;
178       }elseif(!$this->is_account && $this->acl_is_createable()){
179         $this->is_account= TRUE;
180       }
181     }
183     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
184       $cmd= $this->config->search("workgeneric", "ACTIONCMD",array('tabs'));
185       if ($cmd == ""){
186         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
187       } else {
188         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
189         if ($retval != 0){
190           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
191         } elseif ($_POST['saction'] != "wake") {
193           /* Set FAIstate */
194           if($this->fai_activated && $this->dn != "new"){
195             $ldap = $this->config->get_ldap_link();
196             $ldap->cd($this->config->current['BASE']);
197             $ldap->cat($this->dn,array("objectClass"));
198             $res = $ldap->fetch();
200             $attrs = array();
201             $attrs['FAIstate'] = $this->FAIstate;
202             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
203               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
204             }
206             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
207               $attrs['objectClass'][] = $res['objectClass'][$i];
208             }
210             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
211               $attrs['objectClass'][] = "FAIobject";
212             }
214             if($attrs['FAIstate'] == ""){
215 #FIXME we should check if FAIobject is not used anymore
216               $attrs['FAIstate'] = array();
217             }
219             $ldap->cd($this->dn);
220             $ldap->modify($attrs);
221             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
223           }
224           $this->didAction= TRUE;
225         }
226       }
227     }
229     /* Do we represent a valid terminal? */
230     if (!$this->is_account && $this->parent === NULL){
231       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
232         _("This 'dn' has no workstation features.")."</b>";
233       return($display);
234     }
236     /* Base select dialog */
237     $once = true;
238     foreach($_POST as $name => $value){
239       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
240         $once = false;
241         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
242         $this->dialog->setCurrentBase($this->base);
243       }
244     }
246     /* Dialog handling */
247     if(is_object($this->dialog)){
248       /* Must be called before save_object */
249       $this->dialog->save_object();
251       if($this->dialog->isClosed()){
252         $this->dialog = false;
253       }elseif($this->dialog->isSelected()){
255         /* A new base was selected, check if it is a valid one */
256         $tmp = $this->get_allowed_bases();
257         if(isset($tmp[$this->dialog->isSelected()])){
258           $this->base = $this->dialog->isSelected();
259         }
261         $this->dialog= false;
262       }else{
263         return($this->dialog->execute());
264       }
265     }
267     /* Add new ntp Server to our list */ 
268     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
269       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
270     }
272     /* Delete selected NtpServer for list of used servers  */
273     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
274       foreach($_POST['gotoNtpServerSelected'] as $name){
275         unset($this->gotoNtpServer[$name]);
276       }
277     }
279     /* Fill templating stuff */
280     $smarty= get_smarty();
282     /* Set acls */
283     $tmp = $this->plInfo();
284     foreach($tmp['plProvidedAcls'] as $name => $translation){
285       $smarty->assign($name."ACL",$this->getacl($name));
286     }
288     $smarty->assign("cn", $this->cn);
289     $smarty->assign("l", $this->l);
290     $smarty->assign("bases", $this->get_allowed_bases());
291     $smarty->assign("staticAddress", "");
293     $tmp = array();
294     foreach($this->gotoNtpServers as $server){
295       if(!in_array($server,$this->gotoNtpServer)){
296         $tmp[$server] = $server;
297       }
298     }
299     $smarty->assign("gotoNtpServers",$tmp);
300         
301     /* Check if workstation is online */
302     $query= "fping -q -r 1 -t 500 ".$this->cn;
303     exec ($query, $dummy, $retval);
305     /* Offline */
306     if ($retval == 0){
307       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
308                                        "instant_update" => _("Instant update"),
309                                        "update" => _("Scheduled update"),
310                                        "reinstall" => _("Reinstall"),
311                                        "rescan" => _("Rescan hardware"),
312                                        "memcheck" => _("Memory test"),
313                                        "localboot" => _("Force localboot"),
314                                        "sysinfo"  => _("System analysis")));
315     } else {
316       $smarty->assign("actions", array("wake" => _("Wake up"),
317                                        "reinstall" => _("Reinstall"),
318                                        "update" => _("Scheduled update"),
319                                        "memcheck" => _("Memory test"),
320                                        "localboot" => _("Force localboot"),
321                                        "sysinfo"  => _("System analysis")));
322     }
323     /* Arrays */
324     $smarty->assign("modes", $this->modes);
325     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
326     $smarty->assign("syslogservers", $this->gotoSyslogServers);
327     $smarty->assign("fai_activated",$this->fai_activated);
329     $ntpser = array();
330     foreach($this->gotoNtpServers as $server){
331       if(!in_array($server,$this->gotoNtpServer)){
332         $ntpser[$server] = $server;
333       }
334     }
335     $smarty->assign("gotoNtpServers", $ntpser);
337     /* Variables */
338     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
339       $smarty->assign($val."_select", $this->$val);
340     }
342     /* tell smarty the inherit checkbox state */
343     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
344     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
346     $str = $this->netConfigDNS->execute();
347     if(is_object($this->netConfigDNS->dialog)){
348       return($str);
349     }
350     $smarty->assign("netconfig", $str);
352     /* Show main page */
353     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
354   }
356   function remove_from_parent()
357   {
358     if($this->acl_is_removeable()){
360       $this->netConfigDNS->remove_from_parent();
361       $ldap= $this->config->get_ldap_link();
362       $ldap->rmdir($this->dn);
363       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
364       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
366       /* Optionally execute a command after we're done */
367       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
369       /* Delete references to object groups */
370       $ldap->cd ($this->config->current['BASE']);
371       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
372       while ($ldap->fetch()){
373         $og= new ogroup($this->config, $ldap->getDN());
374         unset($og->member[$this->dn]);
375         $og->save ();
376       }
377     }
379     if(isset($_POST["inheritAll"])){
380       $this->set_everything_to_inherited();
381     }
382   }
385   /* Save data to object */
386   function save_object()
387   {
389     /* Create a base backup and reset the
390        base directly after calling plugin::save_object();
391        Base will be set seperatly a few lines below */
392     $base_tmp = $this->base;
393     plugin::save_object();
394     $this->base = $base_tmp;
396     /* Save base, since this is no LDAP attribute */
397     $tmp = $this->get_allowed_bases();
398     if(isset($_POST['base'])){
399       if(isset($tmp[$_POST['base']])){
400         $this->base= $_POST['base'];
401       }
402     }
404     $this->netConfigDNS->save_object();
406     /* Set inherit mode */
407     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
408       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
409         $this->inheritTimeServer = true;
410       }else{
411         $this->inheritTimeServer = false;
412       }
413     }
414     
415     if(isset($_POST["inheritAll"])){
416       $this->set_everything_to_inherited();
417     }
418   }
421   /* Check supplied data */
422   function check()
423   {
424     /* Call common method to give check the hook */
425     $message= plugin::check();
426   
427     /* Skip IP & Mac checks if this is a template */
428     if($this->cn != "wdefault"){
429       $message= array_merge($message, $this->netConfigDNS->check());
430     }
432     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
434     if ($this->cn == ""){
435       $message[]= _("The required field 'Workstation name' is not set.");
436     }
438     if ($this->orig_dn != $this->dn){
439       $ldap= $this->config->get_ldap_link();
440       $ldap->cd ($this->base);
442       if($this->cn == "wdefault"){
443         $ldap->cat($this->dn);
444       }else{
445         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
446       }
447       if ($ldap->count() != 0){
448         while ($attrs= $ldap->fetch()){
449           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
450             continue;
451           } else {
452             if ($attrs['dn'] != $this->orig_dn){
453               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
454               break;
455             }
456           }
457         }
458       }
459     }
461     /* Check for valid ntpServer selection */
462     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
463       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
464     }
466     return ($message);
467   }
470   /* Save to LDAP */
471   function save()
472   {
473     plugin::save();
475     /* Strip out 'default' values */
476     foreach (array("gotoSyslogServer") as $val){
478       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
479         $this->attrs[$val]= array();
480       }
481     }
483     /* Add missing arrays */
484     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
485       if (isset ($this->$val) && count ($this->$val) != 0){
486         $this->attrs["$val"]= $this->$val;
487       }
488     }
490     /* Remove all empty values */
491     if ($this->orig_dn == 'new'){
492       $attrs= array();
493       foreach ($this->attrs as $key => $val){
494         if (is_array($val) && count($val) == 0){
495           continue;
496         }
497         $attrs[$key]= $val;
498       }
499       $this->attrs= $attrs;
500     }
502     /* Update ntp server settings */
503     if($this->inheritTimeServer){
504       if($this->is_new){
505         if(isset($this->attrs['gotoNtpServer'])){
506           unset($this->attrs['gotoNtpServer']);
507         }
508       }else{
509         $this->attrs['gotoNtpServer'] = array();
510       }
511     }else{
512       /* Set ntpServers */
513       $this->attrs['gotoNtpServer'] = array();
514       foreach($this->gotoNtpServer as $server){
515         $this->attrs['gotoNtpServer'][] = $server;
516       }
517     }
519     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
520       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
521     }
523     /* Write back to ldap */
524     $ldap= $this->config->get_ldap_link();
525     if ($this->orig_dn == 'new'){
526       $ldap->cd($this->config->current['BASE']);
527       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
528       $ldap->cd($this->dn);
529       $ldap->add($this->attrs);
530       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
531       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
532       if(!$this->didAction){
533         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
534       }
535     } else {
536       if ($this->orig_dn != $this->dn){
537         $this->move($this->orig_dn, $this->dn);
538       }
539       $ldap->cd($this->dn);
540       $this->cleanup();
541       $ldap->modify ($this->attrs); 
542       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
544       if(!$this->didAction){
545         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
546       }
547     }
549     /* cn=default and macAddress=- indicates that this is a template */
550     if($this->cn == "wdefault"){
551       $this->netConfigDNS->macAddress = "-";
552     }
554     $this->netConfigDNS->cn = $this->cn;
555     $this->netConfigDNS->save();
556     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
557   }
560   /* Display generic part for server copy & paste */
561   function getCopyDialog()
562   {
563     $vars = array("cn");
564     $smarty = get_smarty();
565     $smarty->assign("cn" ,$this->cn);
566     $smarty->assign("object","workstation");
567     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
568     $ret = array();
569     $ret['string'] = $str;
570     $ret['status'] = "";
571     return($ret);
572   }
575   function saveCopyDialog()
576   {
577     if(isset($_POST['cn'])){
578       $this->cn = $_POST['cn'];
579     }
580   }
583   function PrepareForCopyPaste($source)
584   {
585     plugin::PrepareForCopyPaste($source);
586     if(isset($source['macAddress'][0])){
587       $this->netConfigDNS->macAddress = $source['macAddress'][0];
588     }
589     if(isset($source['ipHostNumber'][0])){
590       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
591     }
593     /* Create used ntp server array */
594     $this->gotoNtpServer= array();
595     if(isset($source['gotoNtpServer'])){
596       $this->inheritTimeServer = false;
597       unset($source['gotoNtpServer']['count']);
598       foreach($source['gotoNtpServer'] as $server){
599         $this->gotoNtpServer[$server] = $server;
600       }
601     }
603     /* Set inherit checkbox state */
604     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
605       $this->inheritTimeServer = true;
606       $this->gotoNtpServer=array();
607     }
608   }
611   /* Return plugin informations for acl handling 
612       #FIXME FAIscript seams to ununsed within this class... */ 
613   function plInfo()
614   {
615     return (array(  
616           "plShortName"   => _("Generic"),
617           "plDescription" => _("Workstation generic"),
618           "plSelfModify"  => FALSE,
619           "plDepends"     => array(),
620           "plPriority"    => 0,
621           "plSection"     => array("administration"),
622           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
623                                                           "objectClass"  => "gotoWorkstation")),
624           "plProvidedAcls"=> array(
625             "cn"                  => _("Workstation name"),
626             "l"                   => _("Location") ,
627             "base"                => _("Base") ,
628             "gotoMode"            => _("Goto mode"), 
629             "gotoSyslogServer"    => _("Syslog server"), 
630             "gotoNtpServer"       => _("Ntp server"), 
631             "gotoRootPasswd"      => _("Root password"),
632             "FAIstate"            => _("Action flag"))
633           ));
634   }
636   function set_everything_to_inherited()
637   {
638     $this->gotoSyslogServer  = "default";
639     $this->inheritTimeServer = TRUE;
641     /* Set workstation service attributes to inherited */
642     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
643       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
644             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
645         $this->parent->by_object['workservice']->$name = "default"; 
646       }
647     }
649     /* Set workstation startup attributes to inherited */
650     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
651       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
652       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
653       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
654     }
655   }
657   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
658 ?>