Code

ef68b3993add8d2a3e020635384a8a83ab3a8187
[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= "";
32   /* Needed values and lists */
33   var $base= "";
34   var $cn= "";
35   var $l= "";
36   var $orig_dn= "";
38   /* Plugin side filled */
39   var $modes= array();
41   var $netConfigDNS;
43   var $inheritTimeServer = true;
45   /* attribute list for save action */
46   var $ignore_account= TRUE;
47   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
48       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
49       "ghCpuType", "ghMemSize", "ghUsbSupport",
50       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
51   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
53   var $mapActions   = array("reboot"          => "localboot",
54                             "instant_update"  => "softupdate",
55                             "update"          => "sceduledupdate",
56                             "reinstall"       => "install",
57                             "rescan"          => "",
58                             "memcheck"        => "memcheck",
59                             "sysinfo"         => "sysinfo");
61   
62   var $fai_activated = FALSE;
64   function workgeneric ($config, $dn= NULL, $parent= NULL)
65   {
66     $tmp = search_config($config->data,"faiManagement","CLASS");
67     if(!empty($tmp)){
68       $this->fai_activated = TRUE;
69     }
71     plugin::plugin ($config, $dn, $parent);
72     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
74     /* Read arrays */
75     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
76       if (!isset($this->attrs[$val])){
77         continue;
78       }
79       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
80         array_push($this->$val, $this->attrs[$val][$i]);
81       }
82     }
84     /* Create used ntp server array */
85     $this->gotoNtpServer= array();
86     if(isset($this->attrs['gotoNtpServer'])){
87       $this->inheritTimeServer = false;
88       unset($this->attrs['gotoNtpServer']['count']);
89       foreach($this->attrs['gotoNtpServer'] as $server){
90         $this->gotoNtpServer[$server] = $server;
91       }
92     }
94     /* Set inherit checkbox state */
95     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
96       $this->inheritTimeServer = true;
97       $this->gotoNtpServer=array();
98     }
100     /* Create available ntp options */
101     $tmp = $this->config->data['SERVERS']['NTP'];
102     $this->gotoNtpServers = array();
103     foreach($tmp as $key => $server){
104       if($server == "default") continue;
105       $this->gotoNtpServers[$server] = $server;
106     }
108     $this->modes["active"]= _("Activated");
109     $this->modes["locked"]= _("Locked");
111     /* Set base */
112     if ($this->dn == "new"){
113       $ui= get_userinfo();
114       $this->base= dn2base($ui->dn);
115     } else {
116       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
117     }
119     /* Create an array of all Syslog servers */
120     $tmp = $this->config->data['SERVERS']['SYSLOG'];
121     foreach($tmp as $server){
122       $visible = $server;
123       if($server == "default") {
124         $visible = "["._("inherited")."]";
125       }
126       $this->gotoSyslogServers[$server] = $visible;
127     }
129     /* Save 'dn' for later referal */
130     $this->orig_dn= $this->dn;
131   }
134   function set_acl_base($base)
135   {
136     plugin::set_acl_base($base);
137     $this->netConfigDNS->set_acl_base($base);
138   }
140   function set_acl_category($cat)
141   {
142     plugin::set_acl_category($cat);
143     $this->netConfigDNS->set_acl_category($cat);
144   }
146   function execute()
147   {
148     /* Call parent execute */
149     plugin::execute();
151     /* Do we need to flip is_account state? */
152     if(isset($_POST['modify_state'])){
153       if($this->is_account && $this->acl_is_removeable()){
154         $this->is_account= FALSE;
155       }elseif(!$this->is_account && $this->acl_is_createable()){
156         $this->is_account= TRUE;
157       }
158     }
160     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
161       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
162       if ($cmd == ""){
163         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
164       } else {
165         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
166         if ($retval != 0){
167           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
168         } elseif ($_POST['saction'] != "wake") {
170           /* Set FAIstate */
171           if($this->fai_activated){
172             $ldap = $this->config->get_ldap_link();
173             $ldap->cd($this->config->current['BASE']);
174             $ldap->cat($this->dn,array("objectClass"));
175             $res = $ldap->fetch();
177             $attrs = array();
178             $attrs['FAIstate'] = "";
179             if(isset($this->mapActions[$_POST['saction']])){
180               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
181             }
183             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
184               $attrs['objectClass'][] = $res['objectClass'][$i];
185             }
187             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
188               $attrs['objectClass'][] = "FAIobject";
189             }
191             if($attrs['FAIstate'] == ""){
192 #FIXME we should check if FAIobject is not used anymore
193               $attrs['FAIstate'] = array();
194             }
196             $ldap->cd($this->dn);
197             $ldap->modify($attrs);
198             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
200           }
201           $this->didAction= TRUE;
202         }
203       }
204     }
206     /* Do we represent a valid terminal? */
207     if (!$this->is_account && $this->parent == NULL){
208       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
209         _("This 'dn' has no workstation features.")."</b>";
210       return($display);
211     }
213     /* Base select dialog */
214     $once = true;
215     foreach($_POST as $name => $value){
216       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
217         $once = false;
218         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
219         $this->dialog->setCurrentBase($this->base);
220       }
221     }
223     /* Dialog handling */
224     if(is_object($this->dialog)){
225       /* Must be called before save_object */
226       $this->dialog->save_object();
228       if($this->dialog->isClosed()){
229         $this->dialog = false;
230       }elseif($this->dialog->isSelected()){
232         /* A new base was selected, check if it is a valid one */
233         $tmp = $this->get_allowed_bases();
234         if(isset($tmp[$this->dialog->isSelected()])){
235           $this->base = $this->dialog->isSelected();
236         }
238         $this->dialog= false;
239       }else{
240         return($this->dialog->execute());
241       }
242     }
244     /* Add new ntp Server to our list */ 
245     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
246       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
247     }
249     /* Delete selected NtpServer for list of used servers  */
250     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
251       foreach($_POST['gotoNtpServerSelected'] as $name){
252         unset($this->gotoNtpServer[$name]);
253       }
254     }
256     /* Fill templating stuff */
257     $smarty= get_smarty();
259     /* Set acls */
260     $tmp = $this->plInfo();
261     foreach($tmp['plProvidedAcls'] as $name => $translation){
262       $smarty->assign($name."ACL",$this->getacl($name));
263     }
265     $smarty->assign("cn", $this->cn);
266     $smarty->assign("l", $this->l);
267     $smarty->assign("bases", $this->get_allowed_bases());
268     $smarty->assign("staticAddress", "");
270     $tmp = array();
271     foreach($this->gotoNtpServers as $server){
272       if(!in_array($server,$this->gotoNtpServer)){
273         $tmp[$server] = $server;
274       }
275     }
276     $smarty->assign("gotoNtpServers",$tmp);
277         
278     /* Check if workstation is online */
279     $query= "fping -q -r 1 -t 500 ".$this->cn;
280     exec ($query, $dummy, $retval);
282     /* Offline */
283     if ($retval == 0){
284       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
285                                        "instant_update" => _("Instant update"),
286                                        "update" => _("Scheduled update"),
287                                        "reinstall" => _("Reinstall"),
288                                        "rescan" => _("Rescan hardware"),
289                                        "memcheck" => _("Memory test"),
290                                        "sysinfo"  => _("System analysis")));
291     } else {
292       $smarty->assign("actions", array("wake" => _("Wake up"),
293                                        "reinstall" => _("Reinstall"),
294                                        "update" => _("Scheduled update"),
295                                        "memcheck" => _("Memory test"),
296                                        "sysinfo"  => _("System analysis")));
297     }
298     /* Arrays */
299     $smarty->assign("modes", $this->modes);
300     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
301     $smarty->assign("syslogservers", $this->gotoSyslogServers);
302     $smarty->assign("fai_activated",$this->fai_activated);
304     $ntpser = array();
305     foreach($this->gotoNtpServers as $server){
306       if(!in_array($server,$this->gotoNtpServer)){
307         $ntpser[$server] = $server;
308       }
309     }
310     $smarty->assign("gotoNtpServers", $ntpser);
312     /* Variables */
313     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
314       $smarty->assign($val."_select", $this->$val);
315     }
317     /* tell smarty the inherit checkbox state */
318     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
320     /* Show main page */
321     $smarty->assign("netconfig", $this->netConfigDNS->execute());
322     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
323   }
325   function remove_from_parent()
326   {
327     if($this->acl_is_removeable()){
329       $this->netConfigDNS->remove_from_parent();
330       $ldap= $this->config->get_ldap_link();
331       $ldap->rmdir($this->dn);
332       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
334       /* Optionally execute a command after we're done */
335       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
337       /* Delete references to object groups */
338       $ldap->cd ($this->config->current['BASE']);
339       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
340       while ($ldap->fetch()){
341         $og= new ogroup($this->config, $ldap->getDN());
342         unset($og->member[$this->dn]);
343         $og->save ();
344       }
345     }
346   }
349   /* Save data to object */
350   function save_object()
351   {
353     /* Create a base backup and reset the
354        base directly after calling plugin::save_object();
355        Base will be set seperatly a few lines below */
356     $base_tmp = $this->base;
357     plugin::save_object();
358     $this->base = $base_tmp;
360     /* Save base, since this is no LDAP attribute */
361     $tmp = $this->get_allowed_bases();
362     if(isset($_POST['base'])){
363       if(isset($tmp[$_POST['base']])){
364         $this->base= $_POST['base'];
365       }
366     }
368     $this->netConfigDNS->save_object();
370     /* Set inherit mode */
371     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
372       if(isset($_POST["inheritTimeServer"])){
373         $this->inheritTimeServer = true;
374       }else{
375         $this->inheritTimeServer = false;
376       }
377     }
379   }
382   /* Check supplied data */
383   function check()
384   {
385     /* Call common method to give check the hook */
386     $message= plugin::check();
387   
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.",ou=workstations,ou=systems,".$this->base;
395     if ($this->cn == ""){
396       $message[]= _("The required field 'Workstation name' is not set.");
397     }
399     if ($this->orig_dn != $this->dn){
400       $ldap= $this->config->get_ldap_link();
401       $ldap->cd ($this->base);
403       if($this->cn == "wdefault"){
404         $ldap->cat($this->dn);
405       }else{
406         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
407       }
408       if ($ldap->count() != 0){
409         while ($attrs= $ldap->fetch()){
410           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
411             continue;
412           } else {
413             if ($attrs['dn'] != $this->orig_dn){
414               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
415               break;
416             }
417           }
418         }
419       }
420     }
422     /* Check for valid ntpServer selection */
423     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
424       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
425     }
427     return ($message);
428   }
431   /* Save to LDAP */
432   function save()
433   {
434     plugin::save();
436     /* Strip out 'default' values */
437     foreach (array("gotoSyslogServer") as $val){
439       if ($this->attrs[$val] == "default"){
440         $this->attrs[$val]= array();
441       }
442     }
444     /* Add missing arrays */
445     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
446       if (isset ($this->$val) && count ($this->$val) != 0){
447         $this->attrs["$val"]= $this->$val;
448       }
449     }
451     /* Remove all empty values */
452     if ($this->orig_dn == 'new'){
453       $attrs= array();
454       foreach ($this->attrs as $key => $val){
455         if (is_array($val) && count($val) == 0){
456           continue;
457         }
458         $attrs[$key]= $val;
459       }
460       $this->attrs= $attrs;
461     }
463     /* Update ntp server settings */
464     if($this->inheritTimeServer){
465       if($this->is_new){
466         if(isset($this->attrs['gotoNtpServer'])){
467           unset($this->attrs['gotoNtpServer']);
468         }
469       }else{
470         $this->attrs['gotoNtpServer'] = array();
471       }
472     }else{
473       /* Set ntpServers */
474       $this->attrs['gotoNtpServer'] = array();
475       foreach($this->gotoNtpServer as $server){
476         $this->attrs['gotoNtpServer'][] = $server;
477       }
478     }
480     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
481       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
482     }
484     /* Write back to ldap */
485     $ldap= $this->config->get_ldap_link();
486     if ($this->orig_dn == 'new'){
487       $ldap->cd($this->config->current['BASE']);
488       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
489       $ldap->cd($this->dn);
490       $ldap->add($this->attrs);
491       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
492       if(!$this->didAction){
493         $this->handle_post_events("add");
494       }
495     } else {
496       if ($this->orig_dn != $this->dn){
497         $this->move($this->orig_dn, $this->dn);
498       }
499       $ldap->cd($this->dn);
500       $this->cleanup();
501       $ldap->modify ($this->attrs); 
503       if(!$this->didAction){
504         $this->handle_post_events("modify");
505       }
506     }
508     print_a($this->attrs);
510     /* cn=default and macAddress=- indicates that this is a template */
511     if($this->cn == "wdefault"){
512       $this->netConfigDNS->macAddress = "-";
513     }
515     $this->netConfigDNS->cn = $this->cn;
516     $this->netConfigDNS->save($this->dn);
517     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
518   }
521   /* Return plugin informations for acl handling 
522       #FIXME FAIscript seams to ununsed within this class... */ 
523   function plInfo()
524   {
525     return (array(  
526           "plShortName"   => _("Generic"),
527           "plDescription" => _("Workstation generic"),
528           "plSelfModify"  => FALSE,
529           "plDepends"     => array(),
530           "plPriority"    => 0,
531           "plSection"     => array("administration"),
532           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
533                                                           "objectClass"  => "gotoWorkstation")),
534           "plProvidedAcls"=> array(
535             "cn"                  => _("Workstation name"),
536             "l"                   => _("Location") ,
537             "base"                => _("Base") ,
538             "gotoMode"            => _("Goto mode"), 
539             "gotoSyslogServer"    => _("Syslog server"), 
540             "gotoNtpServer"       => _("Ntp server"), 
541             "gotoRootPasswd"      => _("Root password"),
542             "FAIstate"            => _("Action flag"))
543           ));
544   }
548 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
549 ?>