Code

Template update .
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationGeneric.inc
1 <?php
3 class workgeneric extends plugin
4 {
5   /* Generic terminal attributes */
6   var $gotoMode= "locked";
7   var $gotoSyslogServer= "";
8   var $gotoSyslogServers= array();
9   var $gotoNtpServer= array();
10   var $gotoNtpServers= array();
11   var $gotoSndModule= "";
12   var $gotoFloppyEnable= "";
13   var $gotoCdromEnable= "";
14   var $description= "";
15   var $ghCpuType= "-";
16   var $ghMemSize= "-";
17   var $ghUsbSupport= "-";
18   var $ghNetNic= array();
19   var $ghIdeDev= array();
20   var $ghScsiDev= array();
21   var $ghGfxAdapter= "-";
22   var $ghSoundAdapter= "-";
23   var $gotoLastUser= "-";
24   var $FAIscript= "";
25   var $view_logged = FALSE;
27   /* Needed values and lists */
28   var $base= "";
29   var $cn= "";
30   var $l= "";
31   var $orig_dn= "";
33   /* Plugin side filled */
34   var $modes= array();
36   var $netConfigDNS;
38   var $inheritTimeServer = true;
40   /* attribute list for save action */
41   var $ignore_account= TRUE;
42   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
43       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44       "ghCpuType", "ghMemSize", "ghUsbSupport", "description",
45       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
46   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
48   var $validActions   = array("reboot" => "", "localboot" => "", "halt" => "", "update" => "", "reinstall" => "",
49                             "rescan" => "", "wake" => "", "memcheck" => "", "sysinfo" => "");
50   
51   var $fai_activated = FALSE;
53   var $member_of_ogroup = FALSE;
55   function workgeneric (&$config, $dn= NULL, $parent= NULL)
56   {
57     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
58     if(!empty($tmp)){
59       $this->fai_activated = TRUE;
60     }
62     plugin::plugin ($config, $dn, $parent);
64     if(!isset($this->parent->by_object['ogroup'])){
65       $ldap = $this->config->get_ldap_link();
66       $ldap->cd ($this->config->current['BASE']);
67       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
68       $this->member_of_ogroup = $ldap->count() >= 1;
69     }
71     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
73     /* Read arrays */
74     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
75       if (!isset($this->attrs[$val])){
76         continue;
77       }
78       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
79         array_push($this->$val, $this->attrs[$val][$i]);
80       }
81     }
83     /* Create used ntp server array */
84     $this->gotoNtpServer= array();
85     if(isset($this->attrs['gotoNtpServer'])){
86       $this->inheritTimeServer = false;
87       unset($this->attrs['gotoNtpServer']['count']);
88       foreach($this->attrs['gotoNtpServer'] as $server){
89         $this->gotoNtpServer[$server] = $server;
90       }
91     }
93     /* Set inherit checkbox state */
94     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
95       $this->inheritTimeServer = true;
96       $this->gotoNtpServer=array();
97     }
99     /* You can't inherit the NTP service, if we are not member in an object group */
100     if(!$this->member_of_ogroup){
101       $this->inheritTimeServer = FALSE;
102     }
104     /* Create available ntp options */
105     $tmp = $this->config->data['SERVERS']['NTP'];
106     $this->gotoNtpServers = array();
107     foreach($tmp as $key => $server){
108       if($server == "default") continue;
109       $this->gotoNtpServers[$server] = $server;
110     }
112     $this->modes["active"]= _("Activated");
113     $this->modes["locked"]= _("Locked");
115     /* Set base */
116     if ($this->dn == "new"){
117       $ui= get_userinfo();
118       $this->base= dn2base($ui->dn);
119     } else {
120       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
121     }
123     /* Create an array of all Syslog servers */
124     $tmp = $this->config->data['SERVERS']['SYSLOG'];
125     foreach($tmp as $server){
126       $visible = $server;
127       if($server == "default" && $this->member_of_ogroup) {
128         $visible = "["._("inherited")."]";
129       }
130       $this->gotoSyslogServers[$server] = $visible;
131     }
133     /* Save 'dn' for later referal */
134     $this->orig_dn= $this->dn;
135   }
138   function set_acl_base($base)
139   {
140     plugin::set_acl_base($base);
141     $this->netConfigDNS->set_acl_base($base);
142   }
144   function set_acl_category($cat)
145   {
146     plugin::set_acl_category($cat);
147     $this->netConfigDNS->set_acl_category($cat);
148   }
150   function execute()
151   {
152     /* Call parent execute */
153     plugin::execute();
155     if($this->is_account && !$this->view_logged){
156       $this->view_logged = TRUE;
157       new log("view","workstation/".get_class($this),$this->dn);
158     }
160     /* Do we need to flip is_account state? */
161     if(isset($_POST['modify_state'])){
162       if($this->is_account && $this->acl_is_removeable()){
163         $this->is_account= FALSE;
164       }elseif(!$this->is_account && $this->acl_is_createable()){
165         $this->is_account= TRUE;
166       }
167     }
169     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->validActions[$_POST['saction']]) ){
170       $action= $_POST['saction'];
172       /* Check if we have an DaemonEvent for this action */ 
173       if(class_available("DaemonEvent")){
174         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
175         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
176           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
177           $tmp = new $evt['CLASS_NAME']($this->config);
178           $tmp->add_targets(array($this->netConfigDNS->macAddress));
179           $tmp->set_type(TRIGGERED_EVENT);
180           $o_queue = new gosaSupportDaemon();
181           if(!$o_queue->append($tmp)){
182             msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
183                   $o_queue->get_error()),ERROR_DIALOG);
184           }
185         }
186       } else {
187         msg_dialog::display(_("Event error"),
188                     sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
189       }
192     }
194     /* Do we represent a valid terminal? */
195     if (!$this->is_account && $this->parent === NULL){
196       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
197         _("This 'dn' has no workstation features.")."</b>";
198       return($display);
199     }
201     /* Base select dialog */
202     $once = true;
203     foreach($_POST as $name => $value){
204       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
205         $once = false;
206         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
207         $this->dialog->setCurrentBase($this->base);
208       }
209     }
211     /* Dialog handling */
212     if(is_object($this->dialog)){
213       /* Must be called before save_object */
214       $this->dialog->save_object();
216       if($this->dialog->isClosed()){
217         $this->dialog = false;
218       }elseif($this->dialog->isSelected()){
220         /* A new base was selected, check if it is a valid one */
221         $tmp = $this->get_allowed_bases();
222         if(isset($tmp[$this->dialog->isSelected()])){
223           $this->base = $this->dialog->isSelected();
224         }
226         $this->dialog= false;
227       }else{
228         return($this->dialog->execute());
229       }
230     }
232     /* Add new ntp Server to our list */ 
233     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
234       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
235     }
237     /* Delete selected NtpServer for list of used servers  */
238     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
239       foreach($_POST['gotoNtpServerSelected'] as $name){
240         unset($this->gotoNtpServer[$name]);
241       }
242     }
244     /* Fill templating stuff */
245     $smarty= get_smarty();
247     /* Set acls */
248     $tmp = $this->plInfo();
249     foreach($tmp['plProvidedAcls'] as $name => $translation){
250       $smarty->assign($name."ACL",$this->getacl($name));
251     }
253     $smarty->assign("cn", $this->cn);
254     $smarty->assign("description", $this->description);
255     $smarty->assign("l", $this->l);
256     $smarty->assign("bases", $this->get_allowed_bases());
257     $smarty->assign("staticAddress", "");
259     $tmp = array();
260     foreach($this->gotoNtpServers as $server){
261       if(!in_array($server,$this->gotoNtpServer)){
262         $tmp[$server] = $server;
263       }
264     }
265     $smarty->assign("gotoNtpServers",$tmp);
266         
267     /* Check if workstation is online */
268     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
269       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
270                                        "update" => _("System update"),
271                                        "reinstall" => _("Reinstall"),
272                                        "rescan" => _("Rescan hardware"),
273                                        "memcheck" => _("Memory test"),
274                                        "localboot" => _("Force localboot"),
275                                        "sysinfo"  => _("System analysis")));
276     } else {
277       $smarty->assign("actions", array("wake" => _("Wake up"),
278                                        "reinstall" => _("Reinstall"),
279                                        "update" => _("System update"),
280                                        "memcheck" => _("Memory test"),
281                                        "localboot" => _("Force localboot"),
282                                        "sysinfo"  => _("System analysis")));
283     }
284     /* Arrays */
285     $smarty->assign("modes", $this->modes);
286     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
287     $smarty->assign("syslogservers", $this->gotoSyslogServers);
288     $smarty->assign("fai_activated",$this->fai_activated);
290     $ntpser = array();
291     foreach($this->gotoNtpServers as $server){
292       if(!in_array($server,$this->gotoNtpServer)){
293         $ntpser[$server] = $server;
294       }
295     }
296     $smarty->assign("gotoNtpServers", $ntpser);
298     /* Variables */
299     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
300       $smarty->assign($val."_select", $this->$val);
301     }
303     /* tell smarty the inherit checkbox state */
304     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
305     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
307     $str = $this->netConfigDNS->execute();
308     if(is_object($this->netConfigDNS->dialog)){
309       return($str);
310     }
311     $smarty->assign("netconfig", $str);
313     /* Show main page */
314     return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
315   }
317   function remove_from_parent()
318   {
319     if($this->acl_is_removeable()){
321       $this->netConfigDNS->remove_from_parent();
322       $ldap= $this->config->get_ldap_link();
323       $ldap->rmdir($this->dn);
324       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
325       if (!$ldap->success()){
326         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
327       }
329       /* Optionally execute a command after we're done */
330       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
332       /* Delete references to object groups */
333       $ldap->cd ($this->config->current['BASE']);
334       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
335       while ($ldap->fetch()){
336         $og= new ogroup($this->config, $ldap->getDN());
337         unset($og->member[$this->dn]);
338         $og->save ();
339       }
340     }
342     /* Clean queue form entries with this mac 
343      */
344     if(class_available("gosaSupportDaemon") && tests::is_mac($this->netConfigDNS->orig_macAddress)){
345       $q = new gosaSupportDaemon();
346       $q->clean_queue_from_mac($this->netConfigDNS->orig_macAddress);
347     }
349     if(isset($_POST["inheritAll"])){
350       $this->set_everything_to_inherited();
351     }
352   }
355   /* Save data to object */
356   function save_object()
357   {
359     /* Create a base backup and reset the
360        base directly after calling plugin::save_object();
361        Base will be set seperatly a few lines below */
362     $base_tmp = $this->base;
363     plugin::save_object();
364     $this->base = $base_tmp;
366     /* Save base, since this is no LDAP attribute */
367     $tmp = $this->get_allowed_bases();
368     if(isset($_POST['base'])){
369       if(isset($tmp[$_POST['base']])){
370         $this->base= $_POST['base'];
371       }
372     }
374     $this->netConfigDNS->save_object();
376     /* Set inherit mode */
377     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
378       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
379         $this->inheritTimeServer = true;
380       }else{
381         $this->inheritTimeServer = false;
382       }
383     }
384     
385     if(isset($_POST["inheritAll"])){
386       $this->set_everything_to_inherited();
387     }
388   }
391   /* Check supplied data */
392   function check()
393   {
394     /* Call common method to give check the hook */
395     $message= plugin::check();
396   
397     /* Skip IP & Mac checks if this is a template */
398     if($this->cn != "wdefault"){
399       $message= array_merge($message, $this->netConfigDNS->check());
400     }
402     $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
404     if ($this->cn == ""){
405       $message[]= _("The required field 'Workstation name' is not set.");
406     }
408     /* Check if given name is a valid host/dns name */
409     if(!tests::is_dns_name($this->cn)){
410       $message[] = _("Please specify a valid name for this object.");
411     }
413     if ($this->orig_dn != $this->dn){
414       $ldap= $this->config->get_ldap_link();
415       $ldap->cd ($this->base);
417       if($this->cn == "wdefault"){
418         $ldap->cat($this->dn);
419       }else{
420         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
421       }
422       if ($ldap->count() != 0){
423         while ($attrs= $ldap->fetch()){
424           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
425             continue;
426           } else {
427             if ($attrs['dn'] != $this->orig_dn){
428               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
429               break;
430             }
431           }
432         }
433       }
434     }
436     /* Check for valid ntpServer selection */
437     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
438       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
439     }
441     return ($message);
442   }
445   /* Save to LDAP */
446   function save()
447   {
448     /* Detect mode changes */
449     $activate= (isset($this->saved_attributes['gotoMode']) &&
450         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
451         $this->gotoMode == "active" &&
452         tests::is_ip($this->netConfigDNS->ipHostNumber));
453     plugin::save();
455     /* Strip out 'default' values */
456     foreach (array("gotoSyslogServer") as $val){
458       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
459         $this->attrs[$val]= array();
460       }
461     }
463     /* Add missing arrays */
464     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
465       if (isset ($this->$val) && count ($this->$val) != 0){
466         $this->attrs["$val"]= $this->$val;
467       }
468     }
470     /* Remove all empty values */
471     if ($this->orig_dn == 'new'){
472       $attrs= array();
473       foreach ($this->attrs as $key => $val){
474         if (is_array($val) && count($val) == 0){
475           continue;
476         }
477         $attrs[$key]= $val;
478       }
479       $this->attrs= $attrs;
480     }
482     /* Update ntp server settings */
483     if($this->inheritTimeServer){
484       if($this->is_new){
485         if(isset($this->attrs['gotoNtpServer'])){
486           unset($this->attrs['gotoNtpServer']);
487         }
488       }else{
489         $this->attrs['gotoNtpServer'] = array();
490       }
491     }else{
492       /* Set ntpServers */
493       $this->attrs['gotoNtpServer'] = array();
494       foreach($this->gotoNtpServer as $server){
495         $this->attrs['gotoNtpServer'][] = $server;
496       }
497     }
499     /* Write back to ldap */
500     $ldap= $this->config->get_ldap_link();
501     if ($this->orig_dn == 'new'){
502       $ldap->cd($this->config->current['BASE']);
503       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
504       $ldap->cd($this->dn);
505       $ldap->add($this->attrs);
506       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
507       if (!$ldap->success()){
508         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
509       }
510       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
511     } else {
512       if ($this->orig_dn != $this->dn){
513         $this->move($this->orig_dn, $this->dn);
514       }
515       $ldap->cd($this->dn);
516       $this->cleanup();
517       $ldap->modify ($this->attrs); 
518       if (!$ldap->success()){
519         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
520       }
521       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
523       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
524     }
526     /* cn=default and macAddress=- indicates that this is a template */
527     if($this->cn == "wdefault"){
528       $this->netConfigDNS->macAddress = "-";
529     }
531     $this->netConfigDNS->cn = $this->cn;
532     $this->netConfigDNS->save();
534     if ($activate && class_available("DaemonEvent")){
536       /* Send installation activation
537        */
538       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
539       $o_queue = new gosaSupportDaemon();
540       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
541         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
542         $tmp = new $evt['CLASS_NAME']($this->config);
543         $tmp->set_type(TRIGGERED_EVENT);
544         $tmp->add_targets(array($this->netConfigDNS->macAddress));
545         if(!$o_queue->append($tmp)){
546           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
547                 $o_queue->get_error()),ERROR_DIALOG);
548         }
549       }
550     }
551   }
554   /* Display generic part for server copy & paste */
555   function getCopyDialog()
556   {
557     $vars = array("cn");
558     $smarty = get_smarty();
559     $smarty->assign("cn" ,$this->cn);
560     $smarty->assign("object","workstation");
561     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
562     $ret = array();
563     $ret['string'] = $str;
564     $ret['status'] = "";
565     return($ret);
566   }
569   function saveCopyDialog()
570   {
571     if(isset($_POST['cn'])){
572       $this->cn = $_POST['cn'];
573     }
574   }
577   function PrepareForCopyPaste($source)
578   {
579     plugin::PrepareForCopyPaste($source);
580     if(isset($source['macAddress'][0])){
581       $this->netConfigDNS->macAddress = $source['macAddress'][0];
582     }
583     if(isset($source['ipHostNumber'][0])){
584       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
585     }
587     /* Create used ntp server array */
588     $this->gotoNtpServer= array();
589     if(isset($source['gotoNtpServer'])){
590       $this->inheritTimeServer = false;
591       unset($source['gotoNtpServer']['count']);
592       foreach($source['gotoNtpServer'] as $server){
593         $this->gotoNtpServer[$server] = $server;
594       }
595     }
597     /* Set inherit checkbox state */
598     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
599       $this->inheritTimeServer = true;
600       $this->gotoNtpServer=array();
601     }
602   }
605   /* Return plugin informations for acl handling 
606       #FIXME FAIscript seams to ununsed within this class... */ 
607   static function plInfo()
608   {
609     return (array(  
610           "plShortName"   => _("Generic"),
611           "plDescription" => _("Workstation generic"),
612           "plSelfModify"  => FALSE,
613           "plDepends"     => array(),
614           "plPriority"    => 0,
615           "plSection"     => array("administration"),
616           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
617                                                           "objectClass"  => "gotoWorkstation")),
618           "plProvidedAcls"=> array(
619             "cn"                  => _("Workstation name"),
620             "description"         => _("Description") ,
621             "l"                   => _("Location") ,
622             "base"                => _("Base") ,
623             "gotoMode"            => _("Goto mode"), 
624             "gotoSyslogServer"    => _("Syslog server"), 
625             "gotoNtpServer"       => _("Ntp server"), 
626             "gotoRootPasswd"      => _("Root password"),
627             "FAIstate"            => _("Action flag"))
628           ));
629   }
631   function set_everything_to_inherited()
632   {
633     $this->gotoSyslogServer  = "default";
634     $this->inheritTimeServer = TRUE;
636     /* Set workstation service attributes to inherited */
637     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
638       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
639             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
640         $this->parent->by_object['workservice']->$name = "default"; 
641       }
642     }
644     /* Set workstation startup attributes to inherited */
645     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
646       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
647       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
648       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
649     }
650   }
652   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
653 ?>