Code

Fixed typo
[gosa.git] / gosa-plugins / goto / admin / systems / goto / 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                             "wake"            => "",
62                             "memcheck"        => "memcheck",
63                             "sysinfo"         => "sysinfo");
65   
66   var $fai_activated = FALSE;
68   var $member_of_ogroup = FALSE;
70   function workgeneric (&$config, $dn= NULL, $parent= NULL)
71   {
72     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
73     if(!empty($tmp)){
74       $this->fai_activated = TRUE;
75     }
77     plugin::plugin ($config, $dn, $parent);
79     if(!isset($this->parent->by_object['ogroup'])){
80       $ldap = $this->config->get_ldap_link();
81       $ldap->cd ($this->config->current['BASE']);
82       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
83       $this->member_of_ogroup = $ldap->count() >= 1;
84     }
86     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
88     /* Read arrays */
89     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
90       if (!isset($this->attrs[$val])){
91         continue;
92       }
93       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
94         array_push($this->$val, $this->attrs[$val][$i]);
95       }
96     }
98     /* Create used ntp server array */
99     $this->gotoNtpServer= array();
100     if(isset($this->attrs['gotoNtpServer'])){
101       $this->inheritTimeServer = false;
102       unset($this->attrs['gotoNtpServer']['count']);
103       foreach($this->attrs['gotoNtpServer'] as $server){
104         $this->gotoNtpServer[$server] = $server;
105       }
106     }
108     /* Set inherit checkbox state */
109     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
110       $this->inheritTimeServer = true;
111       $this->gotoNtpServer=array();
112     }
114     /* You can't inherit the NTP service, if we are not member in an object group */
115     if(!$this->member_of_ogroup){
116       $this->inheritTimeServer = FALSE;
117     }
119     /* Create available ntp options */
120     $tmp = $this->config->data['SERVERS']['NTP'];
121     $this->gotoNtpServers = array();
122     foreach($tmp as $key => $server){
123       if($server == "default") continue;
124       $this->gotoNtpServers[$server] = $server;
125     }
127     $this->modes["active"]= _("Activated");
128     $this->modes["locked"]= _("Locked");
130     /* Set base */
131     if ($this->dn == "new"){
132       $ui= get_userinfo();
133       $this->base= dn2base($ui->dn);
134     } else {
135       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
136     }
138     /* Create an array of all Syslog servers */
139     $tmp = $this->config->data['SERVERS']['SYSLOG'];
140     foreach($tmp as $server){
141       $visible = $server;
142       if($server == "default" && $this->member_of_ogroup) {
143         $visible = "["._("inherited")."]";
144       }
145       $this->gotoSyslogServers[$server] = $visible;
146     }
148     /* Save 'dn' for later referal */
149     $this->orig_dn= $this->dn;
150   }
153   function set_acl_base($base)
154   {
155     plugin::set_acl_base($base);
156     $this->netConfigDNS->set_acl_base($base);
157   }
159   function set_acl_category($cat)
160   {
161     plugin::set_acl_category($cat);
162     $this->netConfigDNS->set_acl_category($cat);
163   }
165   function execute()
166   {
167     /* Call parent execute */
168     plugin::execute();
170     if($this->is_account && !$this->view_logged){
171       $this->view_logged = TRUE;
172       new log("view","workstation/".get_class($this),$this->dn);
173     }
175     /* Do we need to flip is_account state? */
176     if(isset($_POST['modify_state'])){
177       if($this->is_account && $this->acl_is_removeable()){
178         $this->is_account= FALSE;
179       }elseif(!$this->is_account && $this->acl_is_createable()){
180         $this->is_account= TRUE;
181       }
182     }
184     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
185         $method= "gosa";
186         $action= $_POST['saction'];
187         if ($action == "reinstall" || $action == "update"){
188                 $method= "job";
189         }
190         gosaSupportDaemon::send("${method}_trigger_action_".$action, $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
192         if ($action != "wake") {
194           /* Set FAIstate */
195           if($this->fai_activated && $this->dn != "new"){
196             $ldap = $this->config->get_ldap_link();
197             $ldap->cd($this->config->current['BASE']);
198             $ldap->cat($this->dn,array("objectClass"));
199             $res = $ldap->fetch();
201             $attrs = array();
202             $attrs['FAIstate'] = $this->FAIstate;
203             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
204               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
205             }
207             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
208               $attrs['objectClass'][] = $res['objectClass'][$i];
209             }
211             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
212               $attrs['objectClass'][] = "FAIobject";
213             }
215             if($attrs['FAIstate'] == ""){
216               #FIXME we should check if FAIobject is not used anymore
217               $attrs['FAIstate'] = array();
218             }
220             $ldap->cd($this->dn);
221             $ldap->modify($attrs);
222             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
224           }
225           $this->didAction= TRUE;
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, dirname(__FILE__))));
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=".LDAP::prepare4filter($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.",".get_ou('workstationou').$this->base;
434     if ($this->cn == ""){
435       $message[]= _("The required field 'Workstation name' is not set.");
436     }
438     /* Check if given name is a valid host/dns name */
439     if(!tests::is_dns_name($this->cn)){
440       $message[] = _("Please specify a valid name for this object.");
441     }
443     if ($this->orig_dn != $this->dn){
444       $ldap= $this->config->get_ldap_link();
445       $ldap->cd ($this->base);
447       if($this->cn == "wdefault"){
448         $ldap->cat($this->dn);
449       }else{
450         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
451       }
452       if ($ldap->count() != 0){
453         while ($attrs= $ldap->fetch()){
454           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
455             continue;
456           } else {
457             if ($attrs['dn'] != $this->orig_dn){
458               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
459               break;
460             }
461           }
462         }
463       }
464     }
466     /* Check for valid ntpServer selection */
467     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
468       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
469     }
471     return ($message);
472   }
475   /* Save to LDAP */
476   function save()
477   {
478     /* Detect mode changes */
479     $activate= (isset($this->saved_attributes['gotoMode']) &&
480         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
481         $this->gotoMode == "active" &&
482         tests::is_ip($this->netConfigDNS->ipHostNumber));
484     plugin::save();
486     /* Strip out 'default' values */
487     foreach (array("gotoSyslogServer") as $val){
489       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
490         $this->attrs[$val]= array();
491       }
492     }
494     /* Add missing arrays */
495     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
496       if (isset ($this->$val) && count ($this->$val) != 0){
497         $this->attrs["$val"]= $this->$val;
498       }
499     }
501     /* Remove all empty values */
502     if ($this->orig_dn == 'new'){
503       $attrs= array();
504       foreach ($this->attrs as $key => $val){
505         if (is_array($val) && count($val) == 0){
506           continue;
507         }
508         $attrs[$key]= $val;
509       }
510       $this->attrs= $attrs;
511     }
513     /* Update ntp server settings */
514     if($this->inheritTimeServer){
515       if($this->is_new){
516         if(isset($this->attrs['gotoNtpServer'])){
517           unset($this->attrs['gotoNtpServer']);
518         }
519       }else{
520         $this->attrs['gotoNtpServer'] = array();
521       }
522     }else{
523       /* Set ntpServers */
524       $this->attrs['gotoNtpServer'] = array();
525       foreach($this->gotoNtpServer as $server){
526         $this->attrs['gotoNtpServer'][] = $server;
527       }
528     }
530     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
531       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
532     }
534     /* Write back to ldap */
535     $ldap= $this->config->get_ldap_link();
536     if ($this->orig_dn == 'new'){
537       $ldap->cd($this->config->current['BASE']);
538       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
539       $ldap->cd($this->dn);
540       $ldap->add($this->attrs);
541       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
542       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
543       if(!$this->didAction){
544         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
545       }
546     } else {
547       if ($this->orig_dn != $this->dn){
548         $this->move($this->orig_dn, $this->dn);
549       }
550       $ldap->cd($this->dn);
551       $this->cleanup();
552       $ldap->modify ($this->attrs); 
553       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
555       if(!$this->didAction){
556         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
557       }
558     }
560     /* cn=default and macAddress=- indicates that this is a template */
561     if($this->cn == "wdefault"){
562       $this->netConfigDNS->macAddress = "-";
563     }
565     $this->netConfigDNS->cn = $this->cn;
566     $this->netConfigDNS->save();
567     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
569     if ($activate){
570       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->macAddress);
571     }
572   }
575   /* Display generic part for server copy & paste */
576   function getCopyDialog()
577   {
578     $vars = array("cn");
579     $smarty = get_smarty();
580     $smarty->assign("cn" ,$this->cn);
581     $smarty->assign("object","workstation");
582     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
583     $ret = array();
584     $ret['string'] = $str;
585     $ret['status'] = "";
586     return($ret);
587   }
590   function saveCopyDialog()
591   {
592     if(isset($_POST['cn'])){
593       $this->cn = $_POST['cn'];
594     }
595   }
598   function PrepareForCopyPaste($source)
599   {
600     plugin::PrepareForCopyPaste($source);
601     if(isset($source['macAddress'][0])){
602       $this->netConfigDNS->macAddress = $source['macAddress'][0];
603     }
604     if(isset($source['ipHostNumber'][0])){
605       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
606     }
608     /* Create used ntp server array */
609     $this->gotoNtpServer= array();
610     if(isset($source['gotoNtpServer'])){
611       $this->inheritTimeServer = false;
612       unset($source['gotoNtpServer']['count']);
613       foreach($source['gotoNtpServer'] as $server){
614         $this->gotoNtpServer[$server] = $server;
615       }
616     }
618     /* Set inherit checkbox state */
619     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
620       $this->inheritTimeServer = true;
621       $this->gotoNtpServer=array();
622     }
623   }
626   /* Return plugin informations for acl handling 
627       #FIXME FAIscript seams to ununsed within this class... */ 
628   static function plInfo()
629   {
630     return (array(  
631           "plShortName"   => _("Generic"),
632           "plDescription" => _("Workstation generic"),
633           "plSelfModify"  => FALSE,
634           "plDepends"     => array(),
635           "plPriority"    => 0,
636           "plSection"     => array("administration"),
637           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
638                                                           "objectClass"  => "gotoWorkstation")),
639           "plProvidedAcls"=> array(
640             "cn"                  => _("Workstation name"),
641             "l"                   => _("Location") ,
642             "base"                => _("Base") ,
643             "gotoMode"            => _("Goto mode"), 
644             "gotoSyslogServer"    => _("Syslog server"), 
645             "gotoNtpServer"       => _("Ntp server"), 
646             "gotoRootPasswd"      => _("Root password"),
647             "FAIstate"            => _("Action flag"))
648           ));
649   }
651   function set_everything_to_inherited()
652   {
653     $this->gotoSyslogServer  = "default";
654     $this->inheritTimeServer = TRUE;
656     /* Set workstation service attributes to inherited */
657     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
658       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
659             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
660         $this->parent->by_object['workservice']->$name = "default"; 
661       }
662     }
664     /* Set workstation startup attributes to inherited */
665     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
666       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
667       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
668       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
669     }
670   }
672   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
673 ?>