Code

Cutted objects will be displayed in light grey in the management lists now.
[gosa.git] / plugins / admin / systems / 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 $ghCpuType= "-";
15   var $ghMemSize= "-";
16   var $ghUsbSupport= "-";
17   var $ghNetNic= array();
18   var $ghIdeDev= array();
19   var $ghScsiDev= array();
20   var $ghGfxAdapter= "-";
21   var $ghSoundAdapter= "-";
22   var $gotoLastUser= "-";
23   var $FAIscript= "";
24   var $didAction= FALSE;
25   var $FAIstate= "";
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", "FAIstate",
45       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
46   var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
48   var $mapActions   = array("reboot"          => "",
49                             "localboot"       => "localboot",
50                             "halt"            => "",
51                             "instant_update"  => "softupdate",
52                             "update"          => "scheduledupdate",
53                             "reinstall"       => "install",
54                             "rescan"          => "",
55                             "memcheck"        => "memcheck",
56                             "sysinfo"         => "sysinfo");
59   var $fai_activated = FALSE;
61   var $member_of_ogroup = FALSE;
63   function workgeneric ($config, $dn= NULL, $parent= NULL)
64   {
65     /* Check if FAI is activated */
66     $tmp = search_config($config->data,"faiManagement","CLASS");
67     if(!empty($tmp)){
68       $this->fai_activated = TRUE;
69     }
71     plugin::plugin ($config, $dn, $parent);
73     if(!isset($this->parent->by_object['ogroup'])){
74       $ldap = $this->config->get_ldap_link();
75       $ldap->cd ($this->config->current['BASE']);
76       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))",array("cn"));
77       $this->member_of_ogroup = $ldap->count() >= 1;
78     }
80     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
81     $this->netConfigDNS->acl = $this->acl;
83     /* Read arrays */
84     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
85       if (!isset($this->attrs[$val])){
86         continue;
87       }
88       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
89         array_push($this->$val, $this->attrs[$val][$i]);
90       }
91     }
93     /* Create used ntp server array */
94     $this->gotoNtpServer= array();
95     if(isset($this->attrs['gotoNtpServer'])){
96       $this->inheritTimeServer = false;
97       unset($this->attrs['gotoNtpServer']['count']);
98       foreach($this->attrs['gotoNtpServer'] as $server){
99         $this->gotoNtpServer[$server] = $server;
100       }
101     }
103     /* Set inherit checkbox state */
104     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
105       $this->inheritTimeServer = true;
106       $this->gotoNtpServer=array();
107     }
109     /* You can't inherit the NTP service, if we are not member in an object group */
110     if(!$this->member_of_ogroup){
111       $this->inheritTimeServer = FALSE;
112     }
114     /* Create available ntp options */
115     $tmp = $this->config->data['SERVERS']['NTP'];
116     $this->gotoNtpServers = array();
117     foreach($tmp as $key => $server){
118       if($server == "default") continue;
119       $this->gotoNtpServers[$server] = $server;
120     }
122     $this->modes["active"]= _("Activated");
123     $this->modes["locked"]= _("Locked");
125     /* Set base */
126     if ($this->dn == "new"){
127       $ui= get_userinfo();
128       $this->base= dn2base($ui->dn);
129     } else {
130       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
131     }
133     /* Create an array of all Syslog servers */
134     $tmp = $this->config->data['SERVERS']['SYSLOG'];
135     foreach($tmp as $server){
136       $visible = $server;
137       if($server == "default" && $this->member_of_ogroup) {
138         $visible = "["._("inherited")."]";
139       }
140       $this->gotoSyslogServers[$server] = $visible;
141     }
143     /* Save 'dn' for later referal */
144     $this->orig_dn= $this->dn;
145   }
147   function execute()
148   {
149     /* Call parent execute */
150     plugin::execute();
151     $this->netConfigDNS->acl = $this->acl;
153     /* Do we need to flip is_account state? */
154     if (isset($_POST['modify_state'])){
155       $this->is_account= !$this->is_account;
156     }
158     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
159       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
160       if ($cmd == ""){
161         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
162       } else {
163         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
164         if ($retval != 0){
165           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
166         } elseif ($_POST['saction'] != "wake") {
168           if($this->fai_activated && $this->dn != "new"){
169             /* Set FAIstate */
170             $ldap = $this->config->get_ldap_link();
171             $ldap->cd($this->config->current['BASE']);
172             $ldap->cat($this->dn,array("objectClass"));
173             $res = $ldap->fetch();
175             $attrs = array();
176             $attrs['FAIstate'] = $this->FAIstate;
177             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
178               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
179             }
181             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
182               $attrs['objectClass'][] = $res['objectClass'][$i];
183             }
185             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
186               $attrs['objectClass'][] = "FAIobject";
187             }
189             if($attrs['FAIstate'] == ""){
190               #FIXME we should check if FAIobject is used anymore
191               $attrs['FAIstate'] = array();
192             }
194             $ldap->cd($this->dn);
195             $ldap->modify($attrs);
196             show_ldap_error($ldap->get_error());
198             $this->didAction= TRUE; 
199           }
200         }
201       }
202     }
204     /* Do we represent a valid terminal? */
205     if (!$this->is_account && $this->parent == NULL){
206       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
207         _("This 'dn' has no workstation features.")."</b>";
208       return($display);
209     }
211     /* Base select dialog */
212     $once = true;
213     foreach($_POST as $name => $value){
214       if(preg_match("/^chooseBase/",$name) && $once){
215         $once = false;
216         $this->dialog = new baseSelectDialog($this->config);
217         $this->dialog->setCurrentBase($this->base);
218       }
219     }
221     /* Dialog handling */
222     if(is_object($this->dialog)){
223       /* Must be called before save_object */
224       $this->dialog->save_object();
226       if($this->dialog->isClosed()){
227         $this->dialog = false;
228       }elseif($this->dialog->isSelected()){
229         $this->base = $this->dialog->isSelected();
230         $this->dialog= false;
231       }else{
232         return($this->dialog->execute());
233       }
234     }
236     /* Add new ntp Server to our list */ 
237     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
238       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
239     }
241     /* Delete selected NtpServer for list of used servers  */
242     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
243       foreach($_POST['gotoNtpServerSelected'] as $name){
244         unset($this->gotoNtpServer[$name]);
245       }
246     }
248     /* Fill templating stuff */
249     $smarty= get_smarty();
250     $smarty->assign("cn", $this->cn);
251     $smarty->assign("l", $this->l);
252     $smarty->assign("bases", $this->config->idepartments);
253     $smarty->assign("staticAddress", "");
254     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
255         
256     /* Check if workstation is online */
257     $query= "fping -q -r 1 -t 500 ".$this->cn;
258     exec ($query, $dummy, $retval);
260     foreach($this->attributes as $attrs ){
261       $smarty->assign($attrs."ACL",chkacl($this->acl,$attrs));
262     }
264     /* Offline */
265     if ($retval == 0){
266       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
267                                        "instant_update" => _("Instant update"),
268                                        "update" => _("Scheduled update"),
269                                        "reinstall" => _("Reinstall"),
270                                        "rescan" => _("Rescan hardware"),
271                                        "memcheck" => _("Memory test"),
272                                        "localboot" => _("Force localboot"),
273                                        "sysinfo"  => _("System analysis")));
274     } else {
275       $smarty->assign("actions", array("wake" => _("Wake up"),
276                                        "reinstall" => _("Reinstall"),
277                                        "update" => _("Scheduled update"),
278                                        "memcheck" => _("Memory test"),
279                                        "localboot" => _("Force localboot"),
280                                        "sysinfo"  => _("System analysis")));
281     }
282     /* Arrays */
283     $smarty->assign("modes", $this->modes);
284     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
285     $smarty->assign("syslogservers", $this->gotoSyslogServers);
286     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
288     /* Variables */
289     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
290       $smarty->assign($val."_select", $this->$val);
291       $smarty->assign($val."ACL", chkacl($this->acl, $val));
292     }
293     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
295     /* tell smarty the inherit checkbox state */
296     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
298     /* Show main page */
299     $this->netConfigDNS->cn= $this->cn;
300     $smarty->assign("netconfig", $this->netConfigDNS->execute());
301     $smarty->assign("fai_activated",$this->fai_activated);
303     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
304   
305     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
306   }
309   function remove_from_parent()
310   {
311     $this->netConfigDNS->acl = $this->acl;
312     $this->netConfigDNS->remove_from_parent();
313     $ldap= $this->config->get_ldap_link();
314     $ldap->rmdir($this->dn);
315     show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
317     /* Optionally execute a command after we're done */
318     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
320     /* Delete references to object groups */
321     $ldap->cd ($this->config->current['BASE']);
322     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
323     while ($ldap->fetch()){
324       $og= new ogroup($this->config, $ldap->getDN());
325       unset($og->member[$this->dn]);
326       $og->save ();
327     }
328   }
331   /* Save data to object */
332   function save_object()
333   {
334     plugin::save_object();
336     /* Save base, since this is no LDAP attribute */
337     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
338       $this->base= $_POST['base'];
339     }
340     $this->netConfigDNS->save_object();
342     /* Set inherit mode */
343     if(isset($_POST['workgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
344       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
345         $this->inheritTimeServer = true;
346       }else{
347         $this->inheritTimeServer = false;
348       }
349     }
351     if(isset($_POST["inheritAll"])){
352       $this->set_everything_to_inherited();
353     }
354   }
357   /* Check supplied data */
358   function check()
359   {
360     /* Call common method to give check the hook */
361     $message= plugin::check();
363     if($this->cn != "wdefault"){
364       $message= array_merge($message, $this->netConfigDNS->check());
365     }
367     $ui= get_userinfo();
368     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
369     $acl= get_permissions ($this->dn, $ui->subtreeACL);
370     $acl= get_module_permission($acl, "group", $this->dn);
371     if (chkacl($acl, "create") != ""){
372       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
373     }
375     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
376       $message[]= _("The required field 'Workstation name' is not set.");
377     }
379     if ($this->orig_dn != $this->dn){
380       $ldap= $this->config->get_ldap_link();
381       $ldap->cd ($this->base);
382   
383       if($this->cn == "wdefault"){
384         $ldap->cat($this->dn);
385       }else{
386         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
387       }
388       if ($ldap->count() != 0){
389         while ($attrs= $ldap->fetch()){
390           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
391             continue;
392           } else {
393             if ($attrs['dn'] != $this->orig_dn){
394               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
395               break;
396             }
397           }
398         }
399       }
400     }
403     /* Check for valid ntpServer selection */
404     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
405       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
406     }
408     return ($message);
409   }
412   /* Save to LDAP */
413   function save()
414   {
415     $this->netConfigDNS->acl = $this->acl;
416     plugin::save();
418     /* Strip out 'default' values */
419     foreach (array("gotoSyslogServer") as $val){
420       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
421         $this->attrs[$val]= array();
422       }
423     }
425     /* Remove FAIstate */
426     if (isset($this->attrs['FAIstate'])){
427       unset ($this->attrs['FAIstate']);
428     }
430     /* Add missing arrays */
431     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
432       if (isset ($this->$val) && count ($this->$val) != 0){
433         $this->attrs["$val"]= $this->$val;
434       }
435     }
437     /* Remove all empty values */
438     if ($this->orig_dn == 'new'){
439       $attrs= array();
440       foreach ($this->attrs as $key => $val){
441         if (is_array($val) && count($val) == 0){
442           continue;
443         }
444         $attrs[$key]= $val;
445       }
446       $this->attrs= $attrs;
447     }
449     /* Update ntp server settings */
450     if($this->inheritTimeServer){
451       if($this->new){
452         if(isset($this->attrs['gotoNtpServer'])){
453           unset($this->attrs['gotoNtpServer']);
454         }
455       }else{
456         $this->attrs['gotoNtpServer'] = array();
457       }
458     }else{
459       /* Set ntpServers */
460       $this->attrs['gotoNtpServer'] = array();
461       foreach($this->gotoNtpServer as $server){
462         $this->attrs['gotoNtpServer'][] = $server;
463       }
464     }
466     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
467       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
468     }
470     /* Write back to ldap */
471     $ldap= $this->config->get_ldap_link();
472     if ($this->orig_dn == 'new'){
473       $ldap->cd($this->config->current['BASE']);
474       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
475       $ldap->cd($this->dn);
476       $ldap->add($this->attrs);
477       show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
478       if(!$this->didAction){
479         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
480       }
481     } else {
482       if ($this->orig_dn != $this->dn){
483         $this->move($this->orig_dn, $this->dn);
484       }
485       $ldap->cd($this->dn);
486       $this->cleanup();
487       $ldap->modify ($this->attrs); 
489       if(!$this->didAction){
490         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
491       }
492     }
494     /* Set macAddress to '-' this indicates that this is a Workstation Template  */
495     if($this->cn == "wdefault"){
496       $this->netConfigDNS->macAddress = "-";
497     }
499     $this->netConfigDNS->cn = $this->cn;
500     $this->netConfigDNS->save($this->dn);
501     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
502   }
504   function set_everything_to_inherited()
505   {
506     $this->gotoSyslogServer  = "default";
507     $this->inheritTimeServer = TRUE;
509     /* Set workstation service attributes to inherited */
510     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
511       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
512             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
513         $this->parent->by_object['workservice']->$name = "default"; 
514       }
515     }
517     /* Set workstation startup attributes to inherited */
518     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
519       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
520       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
521       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
522     }
523   }
525   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>