Code

a6984a55897608e31398e03f31cdb92e56a259a9
[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 = search_config($config->data,"faiManagement","CLASS");
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->dn,$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     /* Create available ntp options */
114     $tmp = $this->config->data['SERVERS']['NTP'];
115     $this->gotoNtpServers = array();
116     foreach($tmp as $key => $server){
117       if($server == "default") continue;
118       $this->gotoNtpServers[$server] = $server;
119     }
121     $this->modes["active"]= _("Activated");
122     $this->modes["locked"]= _("Locked");
124     /* Set base */
125     if ($this->dn == "new"){
126       $ui= get_userinfo();
127       $this->base= dn2base($ui->dn);
128     } else {
129       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
130     }
132     /* Create an array of all Syslog servers */
133     $tmp = $this->config->data['SERVERS']['SYSLOG'];
134     foreach($tmp as $server){
135       $visible = $server;
136       if($server == "default") {
137         $visible = "["._("inherited")."]";
138       }
139       $this->gotoSyslogServers[$server] = $visible;
140     }
142     /* Save 'dn' for later referal */
143     $this->orig_dn= $this->dn;
144   }
147   function set_acl_base($base)
148   {
149     plugin::set_acl_base($base);
150     $this->netConfigDNS->set_acl_base($base);
151   }
153   function set_acl_category($cat)
154   {
155     plugin::set_acl_category($cat);
156     $this->netConfigDNS->set_acl_category($cat);
157   }
159   function execute()
160   {
161     /* Call parent execute */
162     plugin::execute();
164     if($this->is_account && !$this->view_logged){
165       $this->view_logged = TRUE;
166       new log("view","workstation/".get_class($this),$this->dn);
167     }
169     /* Do we need to flip is_account state? */
170     if(isset($_POST['modify_state'])){
171       if($this->is_account && $this->acl_is_removeable()){
172         $this->is_account= FALSE;
173       }elseif(!$this->is_account && $this->acl_is_createable()){
174         $this->is_account= TRUE;
175       }
176     }
178     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
179       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
180       if ($cmd == ""){
181         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
182       } else {
183         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
184         if ($retval != 0){
185           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
186         } elseif ($_POST['saction'] != "wake") {
188           /* Set FAIstate */
189           if($this->fai_activated && $this->dn != "new"){
190             $ldap = $this->config->get_ldap_link();
191             $ldap->cd($this->config->current['BASE']);
192             $ldap->cat($this->dn,array("objectClass"));
193             $res = $ldap->fetch();
195             $attrs = array();
196             $attrs['FAIstate'] = $this->FAIstate;
197             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
198               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
199             }
201             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
202               $attrs['objectClass'][] = $res['objectClass'][$i];
203             }
205             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
206               $attrs['objectClass'][] = "FAIobject";
207             }
209             if($attrs['FAIstate'] == ""){
210 #FIXME we should check if FAIobject is not used anymore
211               $attrs['FAIstate'] = array();
212             }
214             $ldap->cd($this->dn);
215             $ldap->modify($attrs);
216             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
218           }
219           $this->didAction= TRUE;
220         }
221       }
222     }
224     /* Do we represent a valid terminal? */
225     if (!$this->is_account && $this->parent == NULL){
226       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
227         _("This 'dn' has no workstation features.")."</b>";
228       return($display);
229     }
231     /* Base select dialog */
232     $once = true;
233     foreach($_POST as $name => $value){
234       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
235         $once = false;
236         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
237         $this->dialog->setCurrentBase($this->base);
238       }
239     }
241     /* Dialog handling */
242     if(is_object($this->dialog)){
243       /* Must be called before save_object */
244       $this->dialog->save_object();
246       if($this->dialog->isClosed()){
247         $this->dialog = false;
248       }elseif($this->dialog->isSelected()){
250         /* A new base was selected, check if it is a valid one */
251         $tmp = $this->get_allowed_bases();
252         if(isset($tmp[$this->dialog->isSelected()])){
253           $this->base = $this->dialog->isSelected();
254         }
256         $this->dialog= false;
257       }else{
258         return($this->dialog->execute());
259       }
260     }
262     /* Add new ntp Server to our list */ 
263     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
264       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
265     }
267     /* Delete selected NtpServer for list of used servers  */
268     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
269       foreach($_POST['gotoNtpServerSelected'] as $name){
270         unset($this->gotoNtpServer[$name]);
271       }
272     }
274     /* Fill templating stuff */
275     $smarty= get_smarty();
277     /* Set acls */
278     $tmp = $this->plInfo();
279     foreach($tmp['plProvidedAcls'] as $name => $translation){
280       $smarty->assign($name."ACL",$this->getacl($name));
281     }
283     $smarty->assign("cn", $this->cn);
284     $smarty->assign("l", $this->l);
285     $smarty->assign("bases", $this->get_allowed_bases());
286     $smarty->assign("staticAddress", "");
288     $tmp = array();
289     foreach($this->gotoNtpServers as $server){
290       if(!in_array($server,$this->gotoNtpServer)){
291         $tmp[$server] = $server;
292       }
293     }
294     $smarty->assign("gotoNtpServers",$tmp);
295         
296     /* Check if workstation is online */
297     $query= "fping -q -r 1 -t 500 ".$this->cn;
298     exec ($query, $dummy, $retval);
300     /* Offline */
301     if ($retval == 0){
302       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
303                                        "instant_update" => _("Instant update"),
304                                        "update" => _("Scheduled update"),
305                                        "reinstall" => _("Reinstall"),
306                                        "rescan" => _("Rescan hardware"),
307                                        "memcheck" => _("Memory test"),
308                                        "localboot" => _("Force localboot"),
309                                        "sysinfo"  => _("System analysis")));
310     } else {
311       $smarty->assign("actions", array("wake" => _("Wake up"),
312                                        "reinstall" => _("Reinstall"),
313                                        "update" => _("Scheduled update"),
314                                        "memcheck" => _("Memory test"),
315                                        "localboot" => _("Force localboot"),
316                                        "sysinfo"  => _("System analysis")));
317     }
318     /* Arrays */
319     $smarty->assign("modes", $this->modes);
320     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
321     $smarty->assign("syslogservers", $this->gotoSyslogServers);
322     $smarty->assign("fai_activated",$this->fai_activated);
324     $ntpser = array();
325     foreach($this->gotoNtpServers as $server){
326       if(!in_array($server,$this->gotoNtpServer)){
327         $ntpser[$server] = $server;
328       }
329     }
330     $smarty->assign("gotoNtpServers", $ntpser);
332     /* Variables */
333     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
334       $smarty->assign($val."_select", $this->$val);
335     }
337     /* tell smarty the inherit checkbox state */
338     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
339     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
341     /* Show main page */
342     $smarty->assign("netconfig", $this->netConfigDNS->execute());
343     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
344   }
346   function remove_from_parent()
347   {
348     if($this->acl_is_removeable()){
350       $this->netConfigDNS->remove_from_parent();
351       $ldap= $this->config->get_ldap_link();
352       $ldap->rmdir($this->dn);
353       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
354       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
356       /* Optionally execute a command after we're done */
357       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
359       /* Delete references to object groups */
360       $ldap->cd ($this->config->current['BASE']);
361       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
362       while ($ldap->fetch()){
363         $og= new ogroup($this->config, $ldap->getDN());
364         unset($og->member[$this->dn]);
365         $og->save ();
366       }
367     }
369     if(isset($_POST["inheritAll"])){
370       $this->set_everything_to_inherited();
371     }
372   }
375   /* Save data to object */
376   function save_object()
377   {
379     /* Create a base backup and reset the
380        base directly after calling plugin::save_object();
381        Base will be set seperatly a few lines below */
382     $base_tmp = $this->base;
383     plugin::save_object();
384     $this->base = $base_tmp;
386     /* Save base, since this is no LDAP attribute */
387     $tmp = $this->get_allowed_bases();
388     if(isset($_POST['base'])){
389       if(isset($tmp[$_POST['base']])){
390         $this->base= $_POST['base'];
391       }
392     }
394     $this->netConfigDNS->save_object();
396     /* Set inherit mode */
397     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
398       if(isset($_POST["inheritTimeServer"])){
399         $this->inheritTimeServer = true;
400       }else{
401         $this->inheritTimeServer = false;
402       }
403     }
404     
405     if(isset($_POST["inheritAll"])){
406       $this->set_everything_to_inherited();
407     }
408   }
411   /* Check supplied data */
412   function check()
413   {
414     /* Call common method to give check the hook */
415     $message= plugin::check();
416   
417     /* Skip IP & Mac checks if this is a template */
418     if($this->cn != "wdefault"){
419       $message= array_merge($message, $this->netConfigDNS->check());
420     }
422     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
424     if ($this->cn == ""){
425       $message[]= _("The required field 'Workstation name' is not set.");
426     }
428     if ($this->orig_dn != $this->dn){
429       $ldap= $this->config->get_ldap_link();
430       $ldap->cd ($this->base);
432       if($this->cn == "wdefault"){
433         $ldap->cat($this->dn);
434       }else{
435         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
436       }
437       if ($ldap->count() != 0){
438         while ($attrs= $ldap->fetch()){
439           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
440             continue;
441           } else {
442             if ($attrs['dn'] != $this->orig_dn){
443               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
444               break;
445             }
446           }
447         }
448       }
449     }
451     /* Check for valid ntpServer selection */
452     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
453       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
454     }
456     return ($message);
457   }
460   /* Save to LDAP */
461   function save()
462   {
463     plugin::save();
465     /* Strip out 'default' values */
466     foreach (array("gotoSyslogServer") as $val){
468       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
469         $this->attrs[$val]= array();
470       }
471     }
473     /* Add missing arrays */
474     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
475       if (isset ($this->$val) && count ($this->$val) != 0){
476         $this->attrs["$val"]= $this->$val;
477       }
478     }
480     /* Remove all empty values */
481     if ($this->orig_dn == 'new'){
482       $attrs= array();
483       foreach ($this->attrs as $key => $val){
484         if (is_array($val) && count($val) == 0){
485           continue;
486         }
487         $attrs[$key]= $val;
488       }
489       $this->attrs= $attrs;
490     }
492     /* Update ntp server settings */
493     if($this->inheritTimeServer){
494       if($this->is_new){
495         if(isset($this->attrs['gotoNtpServer'])){
496           unset($this->attrs['gotoNtpServer']);
497         }
498       }else{
499         $this->attrs['gotoNtpServer'] = array();
500       }
501     }else{
502       /* Set ntpServers */
503       $this->attrs['gotoNtpServer'] = array();
504       foreach($this->gotoNtpServer as $server){
505         $this->attrs['gotoNtpServer'][] = $server;
506       }
507     }
509     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
510       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
511     }
513     /* Write back to ldap */
514     $ldap= $this->config->get_ldap_link();
515     if ($this->orig_dn == 'new'){
516       $ldap->cd($this->config->current['BASE']);
517       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
518       $ldap->cd($this->dn);
519       $ldap->add($this->attrs);
520       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
521       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
522       if(!$this->didAction){
523         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
524       }
525     } else {
526       if ($this->orig_dn != $this->dn){
527         $this->move($this->orig_dn, $this->dn);
528       }
529       $ldap->cd($this->dn);
530       $this->cleanup();
531       $ldap->modify ($this->attrs); 
532       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
534       if(!$this->didAction){
535         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
536       }
537     }
539     /* cn=default and macAddress=- indicates that this is a template */
540     if($this->cn == "wdefault"){
541       $this->netConfigDNS->macAddress = "-";
542     }
544     $this->netConfigDNS->cn = $this->cn;
545     $this->netConfigDNS->save($this->dn);
546     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
547   }
550   /* Display generic part for server copy & paste */
551   function getCopyDialog()
552   {
553     $vars = array("cn");
554     $smarty = get_smarty();
555     $smarty->assign("cn" ,$this->cn);
556     $smarty->assign("object","workstation");
557     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
558     $ret = array();
559     $ret['string'] = $str;
560     $ret['status'] = "";
561     return($ret);
562   }
565   function saveCopyDialog()
566   {
567     if(isset($_POST['cn'])){
568       $this->cn = $_POST['cn'];
569     }
570   }
573   function PrepareForCopyPaste($source)
574   {
575     plugin::PrepareForCopyPaste($source);
576     if(isset($source['macAddress'][0])){
577       $this->netConfigDNS->macAddress = $source['macAddress'][0];
578     }
579     if(isset($source['ipHostNumber'][0])){
580       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
581     }
583     /* Create used ntp server array */
584     $this->gotoNtpServer= array();
585     if(isset($source['gotoNtpServer'])){
586       $this->inheritTimeServer = false;
587       unset($source['gotoNtpServer']['count']);
588       foreach($source['gotoNtpServer'] as $server){
589         $this->gotoNtpServer[$server] = $server;
590       }
591     }
593     /* Set inherit checkbox state */
594     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
595       $this->inheritTimeServer = true;
596       $this->gotoNtpServer=array();
597     }
598   }
601   /* Return plugin informations for acl handling 
602       #FIXME FAIscript seams to ununsed within this class... */ 
603   function plInfo()
604   {
605     return (array(  
606           "plShortName"   => _("Generic"),
607           "plDescription" => _("Workstation generic"),
608           "plSelfModify"  => FALSE,
609           "plDepends"     => array(),
610           "plPriority"    => 0,
611           "plSection"     => array("administration"),
612           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
613                                                           "objectClass"  => "gotoWorkstation")),
614           "plProvidedAcls"=> array(
615             "cn"                  => _("Workstation name"),
616             "l"                   => _("Location") ,
617             "base"                => _("Base") ,
618             "gotoMode"            => _("Goto mode"), 
619             "gotoSyslogServer"    => _("Syslog server"), 
620             "gotoNtpServer"       => _("Ntp server"), 
621             "gotoRootPasswd"      => _("Root password"),
622             "FAIstate"            => _("Action flag"))
623           ));
624   }
626   function set_everything_to_inherited()
627   {
628     $this->gotoSyslogServer  = "default";
629     $this->inheritTimeServer = TRUE;
631     /* Set workstation service attributes to inherited */
632     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
633       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
634             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
635         $this->parent->by_object['workservice']->$name = "default"; 
636       }
637     }
639     /* Set workstation startup attributes to inherited */
640     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
641       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
642       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
643       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
644     }
645   }
647   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
648 ?>