Code

Hmm...
[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" => _("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" => _("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       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
327       /* Optionally execute a command after we're done */
328       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
330       /* Delete references to object groups */
331       $ldap->cd ($this->config->current['BASE']);
332       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
333       while ($ldap->fetch()){
334         $og= new ogroup($this->config, $ldap->getDN());
335         unset($og->member[$this->dn]);
336         $og->save ();
337       }
338     }
340     if(isset($_POST["inheritAll"])){
341       $this->set_everything_to_inherited();
342     }
343   }
346   /* Save data to object */
347   function save_object()
348   {
350     /* Create a base backup and reset the
351        base directly after calling plugin::save_object();
352        Base will be set seperatly a few lines below */
353     $base_tmp = $this->base;
354     plugin::save_object();
355     $this->base = $base_tmp;
357     /* Save base, since this is no LDAP attribute */
358     $tmp = $this->get_allowed_bases();
359     if(isset($_POST['base'])){
360       if(isset($tmp[$_POST['base']])){
361         $this->base= $_POST['base'];
362       }
363     }
365     $this->netConfigDNS->save_object();
367     /* Set inherit mode */
368     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
369       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
370         $this->inheritTimeServer = true;
371       }else{
372         $this->inheritTimeServer = false;
373       }
374     }
375     
376     if(isset($_POST["inheritAll"])){
377       $this->set_everything_to_inherited();
378     }
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.",".get_ou('workstationou').$this->base;
395     if ($this->cn == ""){
396       $message[]= _("The required field 'Workstation name' is not set.");
397     }
399     /* Check if given name is a valid host/dns name */
400     if(!tests::is_dns_name($this->cn)){
401       $message[] = _("Please specify a valid name for this object.");
402     }
404     if ($this->orig_dn != $this->dn){
405       $ldap= $this->config->get_ldap_link();
406       $ldap->cd ($this->base);
408       if($this->cn == "wdefault"){
409         $ldap->cat($this->dn);
410       }else{
411         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
412       }
413       if ($ldap->count() != 0){
414         while ($attrs= $ldap->fetch()){
415           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
416             continue;
417           } else {
418             if ($attrs['dn'] != $this->orig_dn){
419               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
420               break;
421             }
422           }
423         }
424       }
425     }
427     /* Check for valid ntpServer selection */
428     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
429       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
430     }
432     return ($message);
433   }
436   /* Save to LDAP */
437   function save()
438   {
439     /* Detect mode changes */
440     $activate= (isset($this->saved_attributes['gotoMode']) &&
441         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
442         $this->gotoMode == "active" &&
443         tests::is_ip($this->netConfigDNS->ipHostNumber));
445     plugin::save();
447     /* Strip out 'default' values */
448     foreach (array("gotoSyslogServer") as $val){
450       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
451         $this->attrs[$val]= array();
452       }
453     }
455     /* Add missing arrays */
456     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
457       if (isset ($this->$val) && count ($this->$val) != 0){
458         $this->attrs["$val"]= $this->$val;
459       }
460     }
462     /* Remove all empty values */
463     if ($this->orig_dn == 'new'){
464       $attrs= array();
465       foreach ($this->attrs as $key => $val){
466         if (is_array($val) && count($val) == 0){
467           continue;
468         }
469         $attrs[$key]= $val;
470       }
471       $this->attrs= $attrs;
472     }
474     /* Update ntp server settings */
475     if($this->inheritTimeServer){
476       if($this->is_new){
477         if(isset($this->attrs['gotoNtpServer'])){
478           unset($this->attrs['gotoNtpServer']);
479         }
480       }else{
481         $this->attrs['gotoNtpServer'] = array();
482       }
483     }else{
484       /* Set ntpServers */
485       $this->attrs['gotoNtpServer'] = array();
486       foreach($this->gotoNtpServer as $server){
487         $this->attrs['gotoNtpServer'][] = $server;
488       }
489     }
491     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
492       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
493     }
495     /* Write back to ldap */
496     $ldap= $this->config->get_ldap_link();
497     if ($this->orig_dn == 'new'){
498       $ldap->cd($this->config->current['BASE']);
499       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
500       $ldap->cd($this->dn);
501       $ldap->add($this->attrs);
502       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
503       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
504       $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
505     } else {
506       if ($this->orig_dn != $this->dn){
507         $this->move($this->orig_dn, $this->dn);
508       }
509       $ldap->cd($this->dn);
510       $this->cleanup();
511       $ldap->modify ($this->attrs); 
512       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
514       $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
515     }
517     /* cn=default and macAddress=- indicates that this is a template */
518     if($this->cn == "wdefault"){
519       $this->netConfigDNS->macAddress = "-";
520     }
522     $this->netConfigDNS->cn = $this->cn;
523     $this->netConfigDNS->save();
524     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
526     if ($activate && class_available("DaemonEvent")){
528       /* Send installation activation
529        */
530       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
531       $o_queue = new gosaSupportDaemon();
532       if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
533         $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
534         $tmp = new $evt['CLASS_NAME']($this->config);
535         $tmp->set_type(TRIGGERED_EVENT);
536         $tmp->add_targets(array($this->netConfigDNS->macAddress));
537         if(!$o_queue->append($tmp)){
538           msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
539                 $o_queue->get_error()),ERROR_DIALOG);
540         }
541       }
542     }
543   }
546   /* Display generic part for server copy & paste */
547   function getCopyDialog()
548   {
549     $vars = array("cn");
550     $smarty = get_smarty();
551     $smarty->assign("cn" ,$this->cn);
552     $smarty->assign("object","workstation");
553     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
554     $ret = array();
555     $ret['string'] = $str;
556     $ret['status'] = "";
557     return($ret);
558   }
561   function saveCopyDialog()
562   {
563     if(isset($_POST['cn'])){
564       $this->cn = $_POST['cn'];
565     }
566   }
569   function PrepareForCopyPaste($source)
570   {
571     plugin::PrepareForCopyPaste($source);
572     if(isset($source['macAddress'][0])){
573       $this->netConfigDNS->macAddress = $source['macAddress'][0];
574     }
575     if(isset($source['ipHostNumber'][0])){
576       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
577     }
579     /* Create used ntp server array */
580     $this->gotoNtpServer= array();
581     if(isset($source['gotoNtpServer'])){
582       $this->inheritTimeServer = false;
583       unset($source['gotoNtpServer']['count']);
584       foreach($source['gotoNtpServer'] as $server){
585         $this->gotoNtpServer[$server] = $server;
586       }
587     }
589     /* Set inherit checkbox state */
590     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
591       $this->inheritTimeServer = true;
592       $this->gotoNtpServer=array();
593     }
594   }
597   /* Return plugin informations for acl handling 
598       #FIXME FAIscript seams to ununsed within this class... */ 
599   static function plInfo()
600   {
601     return (array(  
602           "plShortName"   => _("Generic"),
603           "plDescription" => _("Workstation generic"),
604           "plSelfModify"  => FALSE,
605           "plDepends"     => array(),
606           "plPriority"    => 0,
607           "plSection"     => array("administration"),
608           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
609                                                           "objectClass"  => "gotoWorkstation")),
610           "plProvidedAcls"=> array(
611             "cn"                  => _("Workstation name"),
612             "description"         => _("Description") ,
613             "l"                   => _("Location") ,
614             "base"                => _("Base") ,
615             "gotoMode"            => _("Goto mode"), 
616             "gotoSyslogServer"    => _("Syslog server"), 
617             "gotoNtpServer"       => _("Ntp server"), 
618             "gotoRootPasswd"      => _("Root password"),
619             "FAIstate"            => _("Action flag"))
620           ));
621   }
623   function set_everything_to_inherited()
624   {
625     $this->gotoSyslogServer  = "default";
626     $this->inheritTimeServer = TRUE;
628     /* Set workstation service attributes to inherited */
629     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
630       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
631             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
632         $this->parent->by_object['workservice']->$name = "default"; 
633       }
634     }
636     /* Set workstation startup attributes to inherited */
637     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
638       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
639       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
640       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
641     }
642   }
644   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
645 ?>