Code

Added description for terminals
[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     $query= "fping -q -r 1 -t 500 ".$this->cn;
305     exec ($query, $dummy, $retval);
307     /* Offline */
308     if ($retval == 0){
309       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
310                                        "instant_update" => _("Instant update"),
311                                        "update" => _("Scheduled update"),
312                                        "reinstall" => _("Reinstall"),
313                                        "rescan" => _("Rescan hardware"),
314                                        "memcheck" => _("Memory test"),
315                                        "localboot" => _("Force localboot"),
316                                        "sysinfo"  => _("System analysis")));
317     } else {
318       $smarty->assign("actions", array("wake" => _("Wake up"),
319                                        "reinstall" => _("Reinstall"),
320                                        "update" => _("Scheduled update"),
321                                        "memcheck" => _("Memory test"),
322                                        "localboot" => _("Force localboot"),
323                                        "sysinfo"  => _("System analysis")));
324     }
325     /* Arrays */
326     $smarty->assign("modes", $this->modes);
327     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
328     $smarty->assign("syslogservers", $this->gotoSyslogServers);
329     $smarty->assign("fai_activated",$this->fai_activated);
331     $ntpser = array();
332     foreach($this->gotoNtpServers as $server){
333       if(!in_array($server,$this->gotoNtpServer)){
334         $ntpser[$server] = $server;
335       }
336     }
337     $smarty->assign("gotoNtpServers", $ntpser);
339     /* Variables */
340     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
341       $smarty->assign($val."_select", $this->$val);
342     }
344     /* tell smarty the inherit checkbox state */
345     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
346     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
348     $str = $this->netConfigDNS->execute();
349     if(is_object($this->netConfigDNS->dialog)){
350       return($str);
351     }
352     $smarty->assign("netconfig", $str);
354     /* Show main page */
355     return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
356   }
358   function remove_from_parent()
359   {
360     if($this->acl_is_removeable()){
362       $this->netConfigDNS->remove_from_parent();
363       $ldap= $this->config->get_ldap_link();
364       $ldap->rmdir($this->dn);
365       new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
366       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
368       /* Optionally execute a command after we're done */
369       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
371       /* Delete references to object groups */
372       $ldap->cd ($this->config->current['BASE']);
373       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
374       while ($ldap->fetch()){
375         $og= new ogroup($this->config, $ldap->getDN());
376         unset($og->member[$this->dn]);
377         $og->save ();
378       }
379     }
381     if(isset($_POST["inheritAll"])){
382       $this->set_everything_to_inherited();
383     }
384   }
387   /* Save data to object */
388   function save_object()
389   {
391     /* Create a base backup and reset the
392        base directly after calling plugin::save_object();
393        Base will be set seperatly a few lines below */
394     $base_tmp = $this->base;
395     plugin::save_object();
396     $this->base = $base_tmp;
398     /* Save base, since this is no LDAP attribute */
399     $tmp = $this->get_allowed_bases();
400     if(isset($_POST['base'])){
401       if(isset($tmp[$_POST['base']])){
402         $this->base= $_POST['base'];
403       }
404     }
406     $this->netConfigDNS->save_object();
408     /* Set inherit mode */
409     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
410       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
411         $this->inheritTimeServer = true;
412       }else{
413         $this->inheritTimeServer = false;
414       }
415     }
416     
417     if(isset($_POST["inheritAll"])){
418       $this->set_everything_to_inherited();
419     }
420   }
423   /* Check supplied data */
424   function check()
425   {
426     /* Call common method to give check the hook */
427     $message= plugin::check();
428   
429     /* Skip IP & Mac checks if this is a template */
430     if($this->cn != "wdefault"){
431       $message= array_merge($message, $this->netConfigDNS->check());
432     }
434     $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
436     if ($this->cn == ""){
437       $message[]= _("The required field 'Workstation name' is not set.");
438     }
440     /* Check if given name is a valid host/dns name */
441     if(!tests::is_dns_name($this->cn)){
442       $message[] = _("Please specify a valid name for this object.");
443     }
445     if ($this->orig_dn != $this->dn){
446       $ldap= $this->config->get_ldap_link();
447       $ldap->cd ($this->base);
449       if($this->cn == "wdefault"){
450         $ldap->cat($this->dn);
451       }else{
452         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
453       }
454       if ($ldap->count() != 0){
455         while ($attrs= $ldap->fetch()){
456           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
457             continue;
458           } else {
459             if ($attrs['dn'] != $this->orig_dn){
460               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
461               break;
462             }
463           }
464         }
465       }
466     }
468     /* Check for valid ntpServer selection */
469     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
470       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
471     }
473     return ($message);
474   }
477   /* Save to LDAP */
478   function save()
479   {
480     /* Detect mode changes */
481     $activate= (isset($this->saved_attributes['gotoMode']) &&
482         $this->gotoMode != $this->saved_attributes['gotoMode'] &&
483         $this->gotoMode == "active" &&
484         tests::is_ip($this->netConfigDNS->ipHostNumber));
486     plugin::save();
488     /* Strip out 'default' values */
489     foreach (array("gotoSyslogServer") as $val){
491       if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
492         $this->attrs[$val]= array();
493       }
494     }
496     /* Add missing arrays */
497     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
498       if (isset ($this->$val) && count ($this->$val) != 0){
499         $this->attrs["$val"]= $this->$val;
500       }
501     }
503     /* Remove all empty values */
504     if ($this->orig_dn == 'new'){
505       $attrs= array();
506       foreach ($this->attrs as $key => $val){
507         if (is_array($val) && count($val) == 0){
508           continue;
509         }
510         $attrs[$key]= $val;
511       }
512       $this->attrs= $attrs;
513     }
515     /* Update ntp server settings */
516     if($this->inheritTimeServer){
517       if($this->is_new){
518         if(isset($this->attrs['gotoNtpServer'])){
519           unset($this->attrs['gotoNtpServer']);
520         }
521       }else{
522         $this->attrs['gotoNtpServer'] = array();
523       }
524     }else{
525       /* Set ntpServers */
526       $this->attrs['gotoNtpServer'] = array();
527       foreach($this->gotoNtpServer as $server){
528         $this->attrs['gotoNtpServer'][] = $server;
529       }
530     }
532     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
533       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
534     }
536     /* Write back to ldap */
537     $ldap= $this->config->get_ldap_link();
538     if ($this->orig_dn == 'new'){
539       $ldap->cd($this->config->current['BASE']);
540       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
541       $ldap->cd($this->dn);
542       $ldap->add($this->attrs);
543       new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
544       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
545       if(!$this->didAction){
546         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
547       }
548     } else {
549       if ($this->orig_dn != $this->dn){
550         $this->move($this->orig_dn, $this->dn);
551       }
552       $ldap->cd($this->dn);
553       $this->cleanup();
554       $ldap->modify ($this->attrs); 
555       new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
557       if(!$this->didAction){
558         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
559       }
560     }
562     /* cn=default and macAddress=- indicates that this is a template */
563     if($this->cn == "wdefault"){
564       $this->netConfigDNS->macAddress = "-";
565     }
567     $this->netConfigDNS->cn = $this->cn;
568     $this->netConfigDNS->save();
569     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
571     if ($activate){
572       gosaSupportDaemon::send("gosa_set_activated_for_installation", $this->netConfigDNS->macAddress);
573     }
574   }
577   /* Display generic part for server copy & paste */
578   function getCopyDialog()
579   {
580     $vars = array("cn");
581     $smarty = get_smarty();
582     $smarty->assign("cn" ,$this->cn);
583     $smarty->assign("object","workstation");
584     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
585     $ret = array();
586     $ret['string'] = $str;
587     $ret['status'] = "";
588     return($ret);
589   }
592   function saveCopyDialog()
593   {
594     if(isset($_POST['cn'])){
595       $this->cn = $_POST['cn'];
596     }
597   }
600   function PrepareForCopyPaste($source)
601   {
602     plugin::PrepareForCopyPaste($source);
603     if(isset($source['macAddress'][0])){
604       $this->netConfigDNS->macAddress = $source['macAddress'][0];
605     }
606     if(isset($source['ipHostNumber'][0])){
607       $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
608     }
610     /* Create used ntp server array */
611     $this->gotoNtpServer= array();
612     if(isset($source['gotoNtpServer'])){
613       $this->inheritTimeServer = false;
614       unset($source['gotoNtpServer']['count']);
615       foreach($source['gotoNtpServer'] as $server){
616         $this->gotoNtpServer[$server] = $server;
617       }
618     }
620     /* Set inherit checkbox state */
621     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
622       $this->inheritTimeServer = true;
623       $this->gotoNtpServer=array();
624     }
625   }
628   /* Return plugin informations for acl handling 
629       #FIXME FAIscript seams to ununsed within this class... */ 
630   static function plInfo()
631   {
632     return (array(  
633           "plShortName"   => _("Generic"),
634           "plDescription" => _("Workstation generic"),
635           "plSelfModify"  => FALSE,
636           "plDepends"     => array(),
637           "plPriority"    => 0,
638           "plSection"     => array("administration"),
639           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
640                                                           "objectClass"  => "gotoWorkstation")),
641           "plProvidedAcls"=> array(
642             "cn"                  => _("Workstation name"),
643             "description"         => _("Description") ,
644             "l"                   => _("Location") ,
645             "base"                => _("Base") ,
646             "gotoMode"            => _("Goto mode"), 
647             "gotoSyslogServer"    => _("Syslog server"), 
648             "gotoNtpServer"       => _("Ntp server"), 
649             "gotoRootPasswd"      => _("Root password"),
650             "FAIstate"            => _("Action flag"))
651           ));
652   }
654   function set_everything_to_inherited()
655   {
656     $this->gotoSyslogServer  = "default";
657     $this->inheritTimeServer = TRUE;
659     /* Set workstation service attributes to inherited */
660     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
661       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
662             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
663         $this->parent->by_object['workservice']->$name = "default"; 
664       }
665     }
667     /* Set workstation startup attributes to inherited */
668     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
669       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
670       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
671       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
672     }
673   }
675   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
676 ?>