Code

Updated terminal & workstation service .
[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 $description= "";
20   var $ghCpuType= "-";
21   var $ghMemSize= "-";
22   var $ghUsbSupport= "-";
23   var $ghNetNic= array();
24   var $ghIdeDev= array();
25   var $ghScsiDev= array();
26   var $ghGfxAdapter= "-";
27   var $ghSoundAdapter= "-";
28   var $gotoLastUser= "-";
29   var $FAIscript= "";
30   var $didAction= FALSE;
31   var $FAIstate= "";
32   var $view_logged = FALSE;
34   /* Needed values and lists */
35   var $base= "";
36   var $cn= "";
37   var $l= "";
38   var $orig_dn= "";
40   /* Plugin side filled */
41   var $modes= array();
43   var $netConfigDNS;
45   var $inheritTimeServer = true;
47   /* attribute list for save action */
48   var $ignore_account= TRUE;
49   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
50       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
51       "ghCpuType", "ghMemSize", "ghUsbSupport", "description",
52       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
53   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
55   var $mapActions   = array("reboot"          => "",
56                             "localboot"       => "localboot",
57                             "halt"            => "",
58                             "instant_update"  => "softupdate",
59                             "update"          => "scheduledupdate",
60                             "reinstall"       => "install",
61                             "rescan"          => "",
62                             "wake"            => "",
63                             "memcheck"        => "memcheck",
64                             "sysinfo"         => "sysinfo");
66   
67   var $fai_activated = FALSE;
69   var $member_of_ogroup = FALSE;
71   function workgeneric (&$config, $dn= NULL, $parent= NULL)
72   {
73     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
74     if(!empty($tmp)){
75       $this->fai_activated = TRUE;
76     }
78     plugin::plugin ($config, $dn, $parent);
80     if(!isset($this->parent->by_object['ogroup'])){
81       $ldap = $this->config->get_ldap_link();
82       $ldap->cd ($this->config->current['BASE']);
83       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
84       $this->member_of_ogroup = $ldap->count() >= 1;
85     }
87     $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
89     /* Read arrays */
90     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
91       if (!isset($this->attrs[$val])){
92         continue;
93       }
94       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
95         array_push($this->$val, $this->attrs[$val][$i]);
96       }
97     }
99     /* Create used ntp server array */
100     $this->gotoNtpServer= array();
101     if(isset($this->attrs['gotoNtpServer'])){
102       $this->inheritTimeServer = false;
103       unset($this->attrs['gotoNtpServer']['count']);
104       foreach($this->attrs['gotoNtpServer'] as $server){
105         $this->gotoNtpServer[$server] = $server;
106       }
107     }
109     /* Set inherit checkbox state */
110     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
111       $this->inheritTimeServer = true;
112       $this->gotoNtpServer=array();
113     }
115     /* You can't inherit the NTP service, if we are not member in an object group */
116     if(!$this->member_of_ogroup){
117       $this->inheritTimeServer = FALSE;
118     }
120     /* Create available ntp options */
121     $tmp = $this->config->data['SERVERS']['NTP'];
122     $this->gotoNtpServers = array();
123     foreach($tmp as $key => $server){
124       if($server == "default") continue;
125       $this->gotoNtpServers[$server] = $server;
126     }
128     $this->modes["active"]= _("Activated");
129     $this->modes["locked"]= _("Locked");
131     /* Set base */
132     if ($this->dn == "new"){
133       $ui= get_userinfo();
134       $this->base= dn2base($ui->dn);
135     } else {
136       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
137     }
139     /* Create an array of all Syslog servers */
140     $tmp = $this->config->data['SERVERS']['SYSLOG'];
141     foreach($tmp as $server){
142       $visible = $server;
143       if($server == "default" && $this->member_of_ogroup) {
144         $visible = "["._("inherited")."]";
145       }
146       $this->gotoSyslogServers[$server] = $visible;
147     }
149     /* Save 'dn' for later referal */
150     $this->orig_dn= $this->dn;
151   }
154   function set_acl_base($base)
155   {
156     plugin::set_acl_base($base);
157     $this->netConfigDNS->set_acl_base($base);
158   }
160   function set_acl_category($cat)
161   {
162     plugin::set_acl_category($cat);
163     $this->netConfigDNS->set_acl_category($cat);
164   }
166   function execute()
167   {
168     /* Call parent execute */
169     plugin::execute();
171     if($this->is_account && !$this->view_logged){
172       $this->view_logged = TRUE;
173       new log("view","workstation/".get_class($this),$this->dn);
174     }
176     /* Do we need to flip is_account state? */
177     if(isset($_POST['modify_state'])){
178       if($this->is_account && $this->acl_is_removeable()){
179         $this->is_account= FALSE;
180       }elseif(!$this->is_account && $this->acl_is_createable()){
181         $this->is_account= TRUE;
182       }
183     }
185     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
186         $method= "gosa";
187         $action= $_POST['saction'];
188         if ($action == "reinstall" || $action == "update" || $action == "instant_update"){
189                 $method= "job";
190         }
191         gosaSupportDaemon::send("${method}_trigger_action_".$action, $this->netConfigDNS->macAddress, array("macAddress" => $this->netConfigDNS->macAddress));
193         if ($action != "wake") {
195           /* Set FAIstate */
196           if($this->fai_activated && $this->dn != "new"){
197             $ldap = $this->config->get_ldap_link();
198             $ldap->cd($this->config->current['BASE']);
199             $ldap->cat($this->dn,array("objectClass"));
200             $res = $ldap->fetch();
202             $attrs = array();
203             $attrs['FAIstate'] = $this->FAIstate;
204             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
205               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
206             }
208             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
209               $attrs['objectClass'][] = $res['objectClass'][$i];
210             }
212             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
213               $attrs['objectClass'][] = "FAIobject";
214             }
216             if($attrs['FAIstate'] == ""){
217               #FIXME we should check if FAIobject is not used anymore
218               $attrs['FAIstate'] = array();
219             }
221             $ldap->cd($this->dn);
222             $ldap->modify($attrs);
223             show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
225           }
226           $this->didAction= TRUE;
227         }
228     }
230     /* Do we represent a valid terminal? */
231     if (!$this->is_account && $this->parent === NULL){
232       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
233         _("This 'dn' has no workstation features.")."</b>";
234       return($display);
235     }
237     /* Base select dialog */
238     $once = true;
239     foreach($_POST as $name => $value){
240       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
241         $once = false;
242         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
243         $this->dialog->setCurrentBase($this->base);
244       }
245     }
247     /* Dialog handling */
248     if(is_object($this->dialog)){
249       /* Must be called before save_object */
250       $this->dialog->save_object();
252       if($this->dialog->isClosed()){
253         $this->dialog = false;
254       }elseif($this->dialog->isSelected()){
256         /* A new base was selected, check if it is a valid one */
257         $tmp = $this->get_allowed_bases();
258         if(isset($tmp[$this->dialog->isSelected()])){
259           $this->base = $this->dialog->isSelected();
260         }
262         $this->dialog= false;
263       }else{
264         return($this->dialog->execute());
265       }
266     }
268     /* Add new ntp Server to our list */ 
269     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
270       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
271     }
273     /* Delete selected NtpServer for list of used servers  */
274     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
275       foreach($_POST['gotoNtpServerSelected'] as $name){
276         unset($this->gotoNtpServer[$name]);
277       }
278     }
280     /* Fill templating stuff */
281     $smarty= get_smarty();
283     /* Set acls */
284     $tmp = $this->plInfo();
285     foreach($tmp['plProvidedAcls'] as $name => $translation){
286       $smarty->assign($name."ACL",$this->getacl($name));
287     }
289     $smarty->assign("cn", $this->cn);
290     $smarty->assign("description", $this->description);
291     $smarty->assign("l", $this->l);
292     $smarty->assign("bases", $this->get_allowed_bases());
293     $smarty->assign("staticAddress", "");
295     $tmp = array();
296     foreach($this->gotoNtpServers as $server){
297       if(!in_array($server,$this->gotoNtpServer)){
298         $tmp[$server] = $server;
299       }
300     }
301     $smarty->assign("gotoNtpServers",$tmp);
302         
303     /* Check if workstation is online */
304     if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
305       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
306                                        "instant_update" => _("Instant update"),
307                                        "update" => _("Scheduled update"),
308                                        "reinstall" => _("Reinstall"),
309                                        "rescan" => _("Rescan hardware"),
310                                        "memcheck" => _("Memory test"),
311                                        "localboot" => _("Force localboot"),
312                                        "sysinfo"  => _("System analysis")));
313     } else {
314       $smarty->assign("actions", array("wake" => _("Wake up"),
315                                        "reinstall" => _("Reinstall"),
316                                        "update" => _("Scheduled update"),
317                                        "memcheck" => _("Memory test"),
318                                        "localboot" => _("Force localboot"),
319                                        "sysinfo"  => _("System analysis")));
320     }
321     /* Arrays */
322     $smarty->assign("modes", $this->modes);
323     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
324     $smarty->assign("syslogservers", $this->gotoSyslogServers);
325     $smarty->assign("fai_activated",$this->fai_activated);
327     $ntpser = array();
328     foreach($this->gotoNtpServers as $server){
329       if(!in_array($server,$this->gotoNtpServer)){
330         $ntpser[$server] = $server;
331       }
332     }
333     $smarty->assign("gotoNtpServers", $ntpser);
335     /* Variables */
336     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
337       $smarty->assign($val."_select", $this->$val);
338     }
340     /* tell smarty the inherit checkbox state */
341     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
342     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
344     $str = $this->netConfigDNS->execute();
345     if(is_object($this->netConfigDNS->dialog)){
346       return($str);
347     }
348     $smarty->assign("netconfig", $str);
350     /* Show main page */
351     return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
352   }
354   function remove_from_parent()
355   {
356     if($this->acl_is_removeable()){
358       $this->netConfigDNS->remove_from_parent();
359       $ldap= $this->config->get_ldap_link();
360       $ldap->rmdir($this->dn);
361       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
362       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
364       /* Optionally execute a command after we're done */
365       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
367       /* Delete references to object groups */
368       $ldap->cd ($this->config->current['BASE']);
369       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
370       while ($ldap->fetch()){
371         $og= new ogroup($this->config, $ldap->getDN());
372         unset($og->member[$this->dn]);
373         $og->save ();
374       }
375     }
377     if(isset($_POST["inheritAll"])){
378       $this->set_everything_to_inherited();
379     }
380   }
383   /* Save data to object */
384   function save_object()
385   {
387     /* Create a base backup and reset the
388        base directly after calling plugin::save_object();
389        Base will be set seperatly a few lines below */
390     $base_tmp = $this->base;
391     plugin::save_object();
392     $this->base = $base_tmp;
394     /* Save base, since this is no LDAP attribute */
395     $tmp = $this->get_allowed_bases();
396     if(isset($_POST['base'])){
397       if(isset($tmp[$_POST['base']])){
398         $this->base= $_POST['base'];
399       }
400     }
402     $this->netConfigDNS->save_object();
404     /* Set inherit mode */
405     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
406       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
407         $this->inheritTimeServer = true;
408       }else{
409         $this->inheritTimeServer = false;
410       }
411     }
412     
413     if(isset($_POST["inheritAll"])){
414       $this->set_everything_to_inherited();
415     }
416   }
419   /* Check supplied data */
420   function check()
421   {
422     /* Call common method to give check the hook */
423     $message= plugin::check();
424   
425     /* Skip IP & Mac checks if this is a template */
426     if($this->cn != "wdefault"){
427       $message= array_merge($message, $this->netConfigDNS->check());
428     }
430     $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
432     if ($this->cn == ""){
433       $message[]= _("The required field 'Workstation name' is not set.");
434     }
436     /* Check if given name is a valid host/dns name */
437     if(!tests::is_dns_name($this->cn)){
438       $message[] = _("Please specify a valid name for this object.");
439     }
441     if ($this->orig_dn != $this->dn){
442       $ldap= $this->config->get_ldap_link();
443       $ldap->cd ($this->base);
445       if($this->cn == "wdefault"){
446         $ldap->cat($this->dn);
447       }else{
448         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
449       }
450       if ($ldap->count() != 0){
451         while ($attrs= $ldap->fetch()){
452           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
453             continue;
454           } else {
455             if ($attrs['dn'] != $this->orig_dn){
456               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
457               break;
458             }
459           }
460         }
461       }
462     }
464     /* Check for valid ntpServer selection */
465     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
466       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
467     }
469     return ($message);
470   }
473   /* Save to LDAP */
474   function save()
475   {
476     /* Detect mode changes */
477     $activate= (isset($this->saved_attributes['gotoMode']) &&
478         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
479         $this->gotoMode == "active" &&
480         tests::is_ip($this->netConfigDNS->ipHostNumber));
482     plugin::save();
484     /* Strip out 'default' values */
485     foreach (array("gotoSyslogServer") as $val){
487       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
488         $this->attrs[$val]= array();
489       }
490     }
492     /* Add missing arrays */
493     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
494       if (isset ($this->$val) && count ($this->$val) != 0){
495         $this->attrs["$val"]= $this->$val;
496       }
497     }
499     /* Remove all empty values */
500     if ($this->orig_dn == 'new'){
501       $attrs= array();
502       foreach ($this->attrs as $key => $val){
503         if (is_array($val) && count($val) == 0){
504           continue;
505         }
506         $attrs[$key]= $val;
507       }
508       $this->attrs= $attrs;
509     }
511     /* Update ntp server settings */
512     if($this->inheritTimeServer){
513       if($this->is_new){
514         if(isset($this->attrs['gotoNtpServer'])){
515           unset($this->attrs['gotoNtpServer']);
516         }
517       }else{
518         $this->attrs['gotoNtpServer'] = array();
519       }
520     }else{
521       /* Set ntpServers */
522       $this->attrs['gotoNtpServer'] = array();
523       foreach($this->gotoNtpServer as $server){
524         $this->attrs['gotoNtpServer'][] = $server;
525       }
526     }
528     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
529       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
530     }
532     /* Write back to ldap */
533     $ldap= $this->config->get_ldap_link();
534     if ($this->orig_dn == 'new'){
535       $ldap->cd($this->config->current['BASE']);
536       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
537       $ldap->cd($this->dn);
538       $ldap->add($this->attrs);
539       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
540       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
541       if(!$this->didAction){
542         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
543       }
544     } else {
545       if ($this->orig_dn != $this->dn){
546         $this->move($this->orig_dn, $this->dn);
547       }
548       $ldap->cd($this->dn);
549       $this->cleanup();
550       $ldap->modify ($this->attrs); 
551       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
553       if(!$this->didAction){
554         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
555       }
556     }
558     /* cn=default and macAddress=- indicates that this is a template */
559     if($this->cn == "wdefault"){
560       $this->netConfigDNS->macAddress = "-";
561     }
563     $this->netConfigDNS->cn = $this->cn;
564     $this->netConfigDNS->save();
565     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
567     if ($activate){
568       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->macAddress);
569     }
570   }
573   /* Display generic part for server copy & paste */
574   function getCopyDialog()
575   {
576     $vars = array("cn");
577     $smarty = get_smarty();
578     $smarty->assign("cn" ,$this->cn);
579     $smarty->assign("object","workstation");
580     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
581     $ret = array();
582     $ret['string'] = $str;
583     $ret['status'] = "";
584     return($ret);
585   }
588   function saveCopyDialog()
589   {
590     if(isset($_POST['cn'])){
591       $this->cn = $_POST['cn'];
592     }
593   }
596   function PrepareForCopyPaste($source)
597   {
598     plugin::PrepareForCopyPaste($source);
599     if(isset($source['macAddress'][0])){
600       $this->netConfigDNS->macAddress = $source['macAddress'][0];
601     }
602     if(isset($source['ipHostNumber'][0])){
603       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
604     }
606     /* Create used ntp server array */
607     $this->gotoNtpServer= array();
608     if(isset($source['gotoNtpServer'])){
609       $this->inheritTimeServer = false;
610       unset($source['gotoNtpServer']['count']);
611       foreach($source['gotoNtpServer'] as $server){
612         $this->gotoNtpServer[$server] = $server;
613       }
614     }
616     /* Set inherit checkbox state */
617     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
618       $this->inheritTimeServer = true;
619       $this->gotoNtpServer=array();
620     }
621   }
624   /* Return plugin informations for acl handling 
625       #FIXME FAIscript seams to ununsed within this class... */ 
626   static function plInfo()
627   {
628     return (array(  
629           "plShortName"   => _("Generic"),
630           "plDescription" => _("Workstation generic"),
631           "plSelfModify"  => FALSE,
632           "plDepends"     => array(),
633           "plPriority"    => 0,
634           "plSection"     => array("administration"),
635           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
636                                                           "objectClass"  => "gotoWorkstation")),
637           "plProvidedAcls"=> array(
638             "cn"                  => _("Workstation name"),
639             "description"         => _("Description") ,
640             "l"                   => _("Location") ,
641             "base"                => _("Base") ,
642             "gotoMode"            => _("Goto mode"), 
643             "gotoSyslogServer"    => _("Syslog server"), 
644             "gotoNtpServer"       => _("Ntp server"), 
645             "gotoRootPasswd"      => _("Root password"),
646             "FAIstate"            => _("Action flag"))
647           ));
648   }
650   function set_everything_to_inherited()
651   {
652     $this->gotoSyslogServer  = "default";
653     $this->inheritTimeServer = TRUE;
655     /* Set workstation service attributes to inherited */
656     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
657       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
658             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
659         $this->parent->by_object['workservice']->$name = "default"; 
660       }
661     }
663     /* Set workstation startup attributes to inherited */
664     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
665       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
666       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
667       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
668     }
669   }
671   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
672 ?>