Code

Added speed optimizations
[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   }
127   function execute()
128   {
129     /* Call parent execute */
130     plugin::execute();
132     /* Do we need to flip is_account state? */
133     if(isset($_POST['modify_state'])){
134       if($this->is_account && $this->acl_is_removeable()){
135         $this->is_account= FALSE;
136       }elseif(!$this->is_account && $this->acl_is_createable()){
137         $this->is_account= TRUE;
138       }
139     }
141     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
142       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
143       if ($cmd == ""){
144         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
145       } else {
146         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
147         if ($retval != 0){
148           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
149         } else {
151           /* Set FAIstate */
152           $ldap = $this->config->get_ldap_link();
153           $ldap->cd($this->config->current['BASE']);
154           $ldap->cat($this->dn,array("objectClass"));
155           $res = $ldap->fetch();
157           $attrs = array();
158           $attrs['FAIstate'] = "";
159           if(isset($this->mapActions[$_POST['saction']])){
160             $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
161           }
163           for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
164             $attrs['objectClass'][] = $res['objectClass'][$i];
165           }
167           if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
168             $attrs['objectClass'][] = "FAIobject";
169           }
171           if($attrs['FAIstate'] == ""){
172             #FIXME we should check if FAIobject is used anymore
173             $attrs['FAIstate'] = array();
174           }
176           $ldap->cd($this->dn);
177           $ldap->modify($attrs);
178           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
179  
180           $this->didAction= TRUE;
181         }
182       }
183     }
185     /* Do we represent a valid terminal? */
186     if (!$this->is_account && $this->parent == NULL){
187       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
188         _("This 'dn' has no workstation features.")."</b>";
189       return($display);
190     }
192     /* Base select dialog */
193     $once = true;
194     foreach($_POST as $name => $value){
195       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
196         $once = false;
197         $this->dialog = new baseSelectDialog($this->config,$this);
198         $this->dialog->setCurrentBase($this->base);
199       }
200     }
202     /* Dialog handling */
203     if(is_object($this->dialog)){
204       /* Must be called before save_object */
205       $this->dialog->save_object();
207       if($this->dialog->isClosed()){
208         $this->dialog = false;
209       }elseif($this->dialog->isSelected()){
210         $this->base = $this->dialog->isSelected();
211         $this->dialog= false;
212       }else{
213         return($this->dialog->execute());
214       }
215     }
217     /* Add new ntp Server to our list */ 
218     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
219       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
220     }
222     /* Delete selected NtpServer for list of used servers  */
223     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
224       foreach($_POST['gotoNtpServerSelected'] as $name){
225         unset($this->gotoNtpServer[$name]);
226       }
227     }
229     /* Fill templating stuff */
230     $smarty= get_smarty();
232     /* Create base acls */
233     $baseACL = $this->getacl("base");
234     if(!$this->acl_is_moveable()) {
235       $baseACL = preg_replace("/w/","",$baseACL);
236     }
237     $smarty->assign("baseACL",          $baseACL);
239     /* Set acls */
240     $tmp = $this->plInfo();
241     foreach($tmp['plProvidedAcls'] as $name => $translation){
242       $smarty->assign($name."ACL",$this->getacl($name));
243     }
245     $smarty->assign("cn", $this->cn);
246     $smarty->assign("l", $this->l);
247     $smarty->assign("bases", $this->config->idepartments);
248     $smarty->assign("staticAddress", "");
250     $tmp = array();
251     foreach($this->gotoNtpServers as $server){
252       if(!in_array($server,$this->gotoNtpServer)){
253         $tmp[$server] = $server;
254       }
255     }
256     $smarty->assign("gotoNtpServers",$tmp);
257         
258     /* Check if workstation is online */
259     $query= "fping -q -r 1 -t 500 ".$this->cn;
260     exec ($query, $dummy, $retval);
262     /* Offline */
263     if ($retval == 0){
264       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
265                                        "instant_update" => _("Instant update"),
266                                        "update" => _("Scheduled update"),
267                                        "reinstall" => _("Reinstall"),
268                                        "rescan" => _("Rescan hardware"),
269                                        "memcheck" => _("Memory test"),
270                                        "sysinfo"  => _("System analysis")));
271     } else {
272       $smarty->assign("actions", array("wake" => _("Wake up"),
273                                        "reinstall" => _("Reinstall"),
274                                        "update" => _("Scheduled update"),
275                                        "memcheck" => _("Memory test"),
276                                        "sysinfo"  => _("System analysis")));
277     }
278     /* Arrays */
279     $smarty->assign("modes", $this->modes);
280     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
281     $smarty->assign("syslogservers", $this->gotoSyslogServers);
283     $ntpser = array();
284     foreach($this->gotoNtpServers as $server){
285       if(!in_array($server,$this->gotoNtpServer)){
286         $ntpser[$server] = $server;
287       }
288     }
289     $smarty->assign("gotoNtpServers", $ntpser);
291     /* Variables */
292     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
293       $smarty->assign($val."_select", $this->$val);
294     }
296     /* tell smarty the inherit checkbox state */
297     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
299     /* Show main page */
300     $smarty->assign("netconfig", $this->netConfigDNS->execute());
301     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
302   }
304   function remove_from_parent()
305   {
306     $this->netConfigDNS->remove_from_parent();
307     $ldap= $this->config->get_ldap_link();
308     $ldap->rmdir($this->dn);
309     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
311     /* Optionally execute a command after we're done */
312     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
314     /* Delete references to object groups */
315     $ldap->cd ($this->config->current['BASE']);
316     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
317     while ($ldap->fetch()){
318       $og= new ogroup($this->config, $ldap->getDN());
319       unset($og->member[$this->dn]);
320       $og->save ();
321     }
322   }
325   /* Save data to object */
326   function save_object()
327   {
328     plugin::save_object();
330     /* Save base, since this is no LDAP attribute */
331     if((isset($_POST['base'])) && ($this->acl_is_moveable())){
332       $this->set_acl_base('dummy,'.$_POST['base']);
333       if($this->acl_is_moveable()){
335         if(isset($this->config->idepartments[$_POST['base']])){
336           $this->base = $_POST['base'];
337           if ($_POST['base'] != $this->base){
338             $this->is_modified= TRUE;
339           }
340         }
341       }else{
343         print_red(sprintf(_("You are not allowed to move this object to '%s'."),LDAP::fix($_POST['base'])));
344         $this->set_acl_base('dummy,'.$this->base);
345       }
346     }
347     $this->netConfigDNS->save_object();
349     /* Set inherit mode */
350     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
351       if(isset($_POST["inheritTimeServer"])){
352         $this->inheritTimeServer = true;
353       }else{
354         $this->inheritTimeServer = false;
355       }
356     }
358   }
361   /* Check supplied data */
362   function check()
363   {
364     /* Call common method to give check the hook */
365     $message= plugin::check();
366     $message= array_merge($message, $this->netConfigDNS->check());
368     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
369     if ($this->orig_dn == "new" && !$this->acl_is_createable()){
370       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
371     }
374     if ($this->cn == ""){
375       $message[]= _("The required field 'Workstation name' is not set.");
376     }
378     if ($this->orig_dn != $this->dn){
379       $ldap= $this->config->get_ldap_link();
380       $ldap->cd ($this->base);
381       $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
382       if ($ldap->count() != 0){
383         while ($attrs= $ldap->fetch()){
384           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
385             continue;
386           } else {
387             if ($attrs['dn'] != $this->orig_dn){
388               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
389               break;
390             }
391           }
392         }
393       }
394     }
395     
396     /* Check for valid ntpServer selection */
397     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
398       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
399     }
401     return ($message);
402   }
405   /* Save to LDAP */
406   function save()
407   {
408     plugin::save();
410     /* Strip out 'default' values */
411     foreach (array("gotoSyslogServer") as $val){
413       if ($this->attrs[$val] == "default"){
414         $this->attrs[$val]= array();
415       }
416     }
418     /* Add missing arrays */
419     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
420       if (isset ($this->$val) && count ($this->$val) != 0){
421         $this->attrs["$val"]= $this->$val;
422       }
423     }
425     /* Remove all empty values */
426     if ($this->orig_dn == 'new'){
427       $attrs= array();
428       foreach ($this->attrs as $key => $val){
429         if (is_array($val) && count($val) == 0){
430           continue;
431         }
432         $attrs[$key]= $val;
433       }
434       $this->attrs= $attrs;
435     }
437     /* Update ntp server settings */
438     if($this->inheritTimeServer){
439       if($this->is_new){
440         if(isset($this->attrs['gotoNtpServer'])){
441           unset($this->attrs['gotoNtpServer']);
442         }
443       }else{
444         $this->attrs['gotoNtpServer'] = array();
445       }
446     }else{
447       /* Set ntpServers */
448       $this->attrs['gotoNtpServer'] = array();
449       foreach($this->gotoNtpServer as $server){
450         $this->attrs['gotoNtpServer'][] = $server;
451       }
452     }
454     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
455       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
456     }
458     /* Write back to ldap */
459     $ldap= $this->config->get_ldap_link();
460     if ($this->orig_dn == 'new'){
461       $ldap->cd($this->config->current['BASE']);
462       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
463       $ldap->cd($this->dn);
464       $ldap->add($this->attrs);
465       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
466       if(!$this->didAction){
467         $this->handle_post_events("add");
468       }
469     } else {
470       if ($this->orig_dn != $this->dn){
471         $this->move($this->orig_dn, $this->dn);
472       }
473       $ldap->cd($this->dn);
474       $this->cleanup();
475       $ldap->modify ($this->attrs); 
477       if(!$this->didAction){
478         $this->handle_post_events("modify");
479       }
480     }
481     $this->netConfigDNS->cn = $this->cn;
482     $this->netConfigDNS->save($this->dn);
483     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
484   }
487   /* Return plugin informations for acl handling 
488       #FIXME FAIscript seams to ununsed within this class... */ 
489   function plInfo()
490   {
491     return (array(  
492           "plShortName"   => _("Generic"),
493           "plDescription" => _("Workstation generic"),
494           "plSelfModify"  => FALSE,
495           "plDepends"     => array(),
496           "plPriority"    => 0,
497           "plSection"     => array("administration"),
498           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
499                                                           "objectClass"  => "gotoWorkstation")),
500           "plProvidedAcls"=> array(
501             "cn"                  => _("Workstation name"),
502             "l"                   => _("Location") ,
503             "base"                => _("Base") ,
504             "gotoMode"            => _("Goto mode"), 
505             "gotoSyslogServer"    => _("Syslog server"), 
506             "gotoNtpServer"       => _("Ntp server"), 
507             "FAIstate"            => _("Action flag"))
508           ));
509   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>