Code

Skip executing FAI repository if FAI is not active.
[gosa.git] / plugins / admin / systems / class_workstationGeneric.inc
1 <?php
3 class workgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage workstation base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $gotoMode= "locked";
12   var $gotoSyslogServer= "";
13   var $gotoSyslogServers= array();
14   var $gotoNtpServer= array();
15   var $gotoNtpServers= array();
16   var $gotoSndModule= "";
17   var $gotoFloppyEnable= "";
18   var $gotoCdromEnable= "";
19   var $ghCpuType= "-";
20   var $ghMemSize= "-";
21   var $ghUsbSupport= "-";
22   var $ghNetNic= array();
23   var $ghIdeDev= array();
24   var $ghScsiDev= array();
25   var $ghGfxAdapter= "-";
26   var $ghSoundAdapter= "-";
27   var $gotoLastUser= "-";
28   var $FAIscript= "";
29   var $didAction= FALSE;
30   var $FAIstate= "";
32   /* Needed values and lists */
33   var $base= "";
34   var $cn= "";
35   var $l= "";
36   var $orig_dn= "";
38   /* Plugin side filled */
39   var $modes= array();
41   var $netConfigDNS;
43   var $inheritTimeServer = true;
45   /* attribute list for save action */
46   var $ignore_account= TRUE;
47   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
48       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
49       "ghCpuType", "ghMemSize", "ghUsbSupport",
50       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
51   var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
53   var $mapActions   = array("reboot"          => "localboot",
54                             "instant_update"  => "softupdate",
55                             "update"          => "sceduledupdate",
56                             "reinstall"       => "install",
57                             "rescan"          => "",
58                             "memcheck"        => "memcheck",
59                             "sysinfo"         => "sysinfo");
63   function workgeneric ($config, $dn= NULL, $parent= NULL)
64   {
65     plugin::plugin ($config, $dn, $parent);
66     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
68     /* Read arrays */
69     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
70       if (!isset($this->attrs[$val])){
71         continue;
72       }
73       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
74         array_push($this->$val, $this->attrs[$val][$i]);
75       }
76     }
78     /* Create used ntp server array */
79     $this->gotoNtpServer= array();
80     if(isset($this->attrs['gotoNtpServer'])){
81       $this->inheritTimeServer = false;
82       unset($this->attrs['gotoNtpServer']['count']);
83       foreach($this->attrs['gotoNtpServer'] as $server){
84         $this->gotoNtpServer[$server] = $server;
85       }
86     }
88     /* Set inherit checkbox state */
89     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
90       $this->inheritTimeServer = true;
91       $this->gotoNtpServer=array();
92     }
94     /* Create available ntp options */
95     $tmp = $this->config->data['SERVERS']['NTP'];
96     $this->gotoNtpServers = array();
97     foreach($tmp as $key => $server){
98       if($server == "default") continue;
99       $this->gotoNtpServers[$server] = $server;
100     }
102     $this->modes["active"]= _("Activated");
103     $this->modes["locked"]= _("Locked");
105     /* Set base */
106     if ($this->dn == "new"){
107       $ui= get_userinfo();
108       $this->base= dn2base($ui->dn);
109     } else {
110       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
111     }
113     /* Create an array of all Syslog servers */
114     $tmp = $this->config->data['SERVERS']['SYSLOG'];
115     foreach($tmp as $server){
116       $visible = $server;
117       if($server == "default") {
118         $visible = "["._("inherited")."]";
119       }
120       $this->gotoSyslogServers[$server] = $visible;
121     }
123     /* Save 'dn' for later referal */
124     $this->orig_dn= $this->dn;
125   }
127   function execute()
128   {
129     /* Call parent execute */
130     plugin::execute();
132     /* Do we need to flip is_account state? */
133     if (isset($_POST['modify_state'])){
134       $this->is_account= !$this->is_account;
135     }
137     if (isset($_POST['action'])){
138       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
139       if ($cmd == ""){
140         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
141       } else {
142         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
143         if ($retval != 0){
144           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
145         } elseif ($_POST['saction'] != "wake") {
147           /* Set FAIstate */
148           $ldap = $this->config->get_ldap_link();
149           $ldap->cd($this->config->current['BASE']);
150           $ldap->cat($this->dn,array("objectClass"));
151           $res = $ldap->fetch();
153           $attrs = array();
154           $attrs['FAIstate'] = "";
155           if(isset($this->mapActions[$_POST['saction']])){
156             $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
157           }
159           for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
160             $attrs['objectClass'][] = $res['objectClass'][$i];
161           }
163           if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
164             $attrs['objectClass'][] = "FAIobject";
165           }
167           if($attrs['FAIstate'] == ""){
168             #FIXME we should check if FAIobject is used anymore
169             $attrs['FAIstate'] = array();
170           }
172           $ldap->cd($this->dn);
173           $ldap->modify($attrs);
174           show_ldap_error($ldap->get_error());
175   
176           $this->didAction= TRUE;
177         }
178       }
179     }
181     /* Do we represent a valid terminal? */
182     if (!$this->is_account && $this->parent == NULL){
183       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
184         _("This 'dn' has no workstation features.")."</b>";
185       return($display);
186     }
188     /* Base select dialog */
189     $once = true;
190     foreach($_POST as $name => $value){
191       if(preg_match("/^chooseBase/",$name) && $once){
192         $once = false;
193         $this->dialog = new baseSelectDialog($this->config);
194         $this->dialog->setCurrentBase($this->base);
195       }
196     }
198     /* Dialog handling */
199     if(is_object($this->dialog)){
200       /* Must be called before save_object */
201       $this->dialog->save_object();
203       if($this->dialog->isClosed()){
204         $this->dialog = false;
205       }elseif($this->dialog->isSelected()){
206         $this->base = $this->dialog->isSelected();
207         $this->dialog= false;
208       }else{
209         return($this->dialog->execute());
210       }
211     }
213     /* Add new ntp Server to our list */ 
214     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
215       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
216     }
218     /* Delete selected NtpServer for list of used servers  */
219     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
220       foreach($_POST['gotoNtpServerSelected'] as $name){
221         unset($this->gotoNtpServer[$name]);
222       }
223     }
225     /* Fill templating stuff */
226     $smarty= get_smarty();
227     $smarty->assign("cn", $this->cn);
228     $smarty->assign("l", $this->l);
229     $smarty->assign("bases", $this->config->idepartments);
230     $smarty->assign("staticAddress", "");
231     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
232         
233     /* Check if workstation is online */
234     $query= "fping -q -r 1 -t 500 ".$this->cn;
235     exec ($query, $dummy, $retval);
237     /* Offline */
238     if ($retval == 0){
239       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
240                                        "instant_update" => _("Instant update"),
241                                        "update" => _("Scheduled update"),
242                                        "reinstall" => _("Reinstall"),
243                                        "rescan" => _("Rescan hardware"),
244                                        "memcheck" => _("Memory test"),
245                                        "sysinfo"  => _("System analysis")));
246     } else {
247       $smarty->assign("actions", array("wake" => _("Wake up"),
248                                        "reinstall" => _("Reinstall"),
249                                        "update" => _("Scheduled update"),
250                                        "memcheck" => _("Memory test"),
251                                        "sysinfo"  => _("System analysis")));
252     }
253     /* Arrays */
254     $smarty->assign("modes", $this->modes);
255     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
256     $smarty->assign("syslogservers", $this->gotoSyslogServers);
257     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
259     /* Variables */
260     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
261       $smarty->assign($val."_select", $this->$val);
262       $smarty->assign($val."ACL", chkacl($this->acl, $val));
263     }
264     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
266     /* tell smarty the inherit checkbox state */
267     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
269     /* Show main page */
270     $smarty->assign("netconfig", $this->netConfigDNS->execute());
271     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
272   }
274   function remove_from_parent()
275   {
276     $this->netConfigDNS->remove_from_parent();
277     $ldap= $this->config->get_ldap_link();
278     $ldap->rmdir($this->dn);
279     show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
281     /* Optionally execute a command after we're done */
282     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
284     /* Delete references to object groups */
285     $ldap->cd ($this->config->current['BASE']);
286     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
287     while ($ldap->fetch()){
288       $og= new ogroup($this->config, $ldap->getDN());
289       unset($og->member[$this->dn]);
290       $og->save ();
291     }
292   }
295   /* Save data to object */
296   function save_object()
297   {
298     plugin::save_object();
300     /* Save base, since this is no LDAP attribute */
301     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
302       $this->base= $_POST['base'];
303     }
304     $this->netConfigDNS->save_object();
306     /* Set inherit mode */
307     if(isset($_POST['workgeneric_posted'])){
308       if(isset($_POST["inheritTimeServer"])){
309         $this->inheritTimeServer = true;
310       }else{
311         $this->inheritTimeServer = false;
312       }
313     }
315   }
318   /* Check supplied data */
319   function check()
320   {
321     /* Call common method to give check the hook */
322     $message= plugin::check();
324     if($this->cn != "wdefault"){
325       $message= array_merge($message, $this->netConfigDNS->check());
326     }
328     $ui= get_userinfo();
329     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
330     $acl= get_permissions ($this->dn, $ui->subtreeACL);
331     $acl= get_module_permission($acl, "group", $this->dn);
332     if (chkacl($acl, "create") != ""){
333       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
334     }
336     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
337       $message[]= _("The required field 'Workstation name' is not set.");
338     }
340     if ($this->orig_dn != $this->dn){
341       $ldap= $this->config->get_ldap_link();
342       $ldap->cd ($this->base);
343   
344       if($this->cn == "wdefault"){
345         $ldap->cat($this->dn);
346       }else{
347         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
348       }
349       if ($ldap->count() != 0){
350         while ($attrs= $ldap->fetch()){
351           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
352             continue;
353           } else {
354             if ($attrs['dn'] != $this->orig_dn){
355               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
356               break;
357             }
358           }
359         }
360       }
361     }
364     /* Check for valid ntpServer selection */
365     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
366       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
367     }
369     return ($message);
370   }
373   /* Save to LDAP */
374   function save()
375   {
376     plugin::save();
378     /* Strip out 'default' values */
379     foreach (array("gotoSyslogServer") as $val){
380       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
381         $this->attrs[$val]= array();
382       }
383     }
385     /* Add missing arrays */
386     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
387       if (isset ($this->$val) && count ($this->$val) != 0){
388         $this->attrs["$val"]= $this->$val;
389       }
390     }
392     /* Remove all empty values */
393     if ($this->orig_dn == 'new'){
394       $attrs= array();
395       foreach ($this->attrs as $key => $val){
396         if (is_array($val) && count($val) == 0){
397           continue;
398         }
399         $attrs[$key]= $val;
400       }
401       $this->attrs= $attrs;
402     }
404     /* Update ntp server settings */
405     if($this->inheritTimeServer){
406       if($this->new){
407         if(isset($this->attrs['gotoNtpServer'])){
408           unset($this->attrs['gotoNtpServer']);
409         }
410       }else{
411         $this->attrs['gotoNtpServer'] = array();
412       }
413     }else{
414       /* Set ntpServers */
415       $this->attrs['gotoNtpServer'] = array();
416       foreach($this->gotoNtpServer as $server){
417         $this->attrs['gotoNtpServer'][] = $server;
418       }
419     }
421     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
422       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
423     }
425     /* Write back to ldap */
426     $ldap= $this->config->get_ldap_link();
427     if ($this->orig_dn == 'new'){
428       $ldap->cd($this->config->current['BASE']);
429       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
430       $ldap->cd($this->dn);
431       $ldap->add($this->attrs);
432       show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
433       if(!$this->didAction){
434         $this->handle_post_events("add");
435       }
436     } else {
437       if ($this->orig_dn != $this->dn){
438         $this->move($this->orig_dn, $this->dn);
439       }
440       $ldap->cd($this->dn);
441       $this->cleanup();
442       $ldap->modify ($this->attrs); 
444       if(!$this->didAction){
445         $this->handle_post_events("modify");
446       }
447     }
449     /* Set macAddress to '-' this indicates that this is a Workstation Template  */
450     if($this->cn == "wdefault"){
451       $this->netConfigDNS->macAddress = "-";
452     }
454     $this->netConfigDNS->cn = $this->cn;
455     $this->netConfigDNS->save($this->dn);
456     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
457   }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>