Code

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