Code

Updated filtering again
[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     }else{
70       $this->objectclasses= array("top", "gotoWorkstation", "GOhard");
71     }
73     plugin::plugin ($config, $dn, $parent);
75     if(!isset($this->parent->by_object['ogroup'])){
76       $ldap = $this->config->get_ldap_link();
77       $ldap->cd ($this->config->current['BASE']);
78       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
79       $this->member_of_ogroup = $ldap->count() >= 1;
80     }
82     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
83     $this->netConfigDNS->acl = $this->acl;
85     /* Read arrays */
86     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
87       if (!isset($this->attrs[$val])){
88         continue;
89       }
90       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
91         array_push($this->$val, $this->attrs[$val][$i]);
92       }
93     }
95     /* Create used ntp server array */
96     $this->gotoNtpServer= array();
97     if(isset($this->attrs['gotoNtpServer'])){
98       $this->inheritTimeServer = false;
99       unset($this->attrs['gotoNtpServer']['count']);
100       foreach($this->attrs['gotoNtpServer'] as $server){
101         $this->gotoNtpServer[$server] = $server;
102       }
103     }
105     /* Set inherit checkbox state */
106     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
107       $this->inheritTimeServer = true;
108       $this->gotoNtpServer=array();
109     }
111     /* You can't inherit the NTP service, if we are not member in an object group */
112     if(!$this->member_of_ogroup){
113       $this->inheritTimeServer = FALSE;
114     }
116     /* Create available ntp options */
117     $tmp = $this->config->data['SERVERS']['NTP'];
118     $this->gotoNtpServers = array();
119     foreach($tmp as $key => $server){
120       if($server == "default") continue;
121       $this->gotoNtpServers[$server] = $server;
122     }
124     $this->modes["active"]= _("Activated");
125     $this->modes["locked"]= _("Locked");
127     /* Set base */
128     if ($this->dn == "new"){
129       $ui= get_userinfo();
130       $this->base= dn2base($ui->dn);
131     } else {
132       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
133     }
135     /* Create an array of all Syslog servers */
136     $tmp = $this->config->data['SERVERS']['SYSLOG'];
137     foreach($tmp as $server){
138       $visible = $server;
139       if($server == "default" && $this->member_of_ogroup) {
140         $visible = "["._("inherited")."]";
141       }
142       $this->gotoSyslogServers[$server] = $visible;
143     }
145     /* Save 'dn' for later referal */
146     $this->orig_dn= $this->dn;
147   }
149   function execute()
150   {
151     /* Call parent execute */
152     plugin::execute();
153     $this->netConfigDNS->acl = $this->acl;
155     /* Do we need to flip is_account state? */
156     if (isset($_POST['modify_state'])){
157       $this->is_account= !$this->is_account;
158     }
160     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
161       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
162       if ($cmd == ""){
163         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
164       } else {
165         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
166         if ($retval != 0){
167           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
168         } elseif ($_POST['saction'] != "wake") {
170           if($this->fai_activated && $this->dn != "new"){
171             /* Set FAIstate */
172             $ldap = $this->config->get_ldap_link();
173             $ldap->cd($this->config->current['BASE']);
174             $ldap->cat($this->dn,array("objectClass"));
175             $res = $ldap->fetch();
177             $attrs = array();
178             $attrs['FAIstate'] = $this->FAIstate;
179             if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
180               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
181             }
183             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
184               $attrs['objectClass'][] = $res['objectClass'][$i];
185             }
187             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
188               $attrs['objectClass'][] = "FAIobject";
189             }
191             if($attrs['FAIstate'] == ""){
192               #FIXME we should check if FAIobject is used anymore
193               $attrs['FAIstate'] = array();
194             }
196             $ldap->cd($this->dn);
197             $ldap->modify($attrs);
198             show_ldap_error($ldap->get_error());
200             $this->didAction= TRUE; 
201           }
202         }
203       }
204     }
206     /* Do we represent a valid terminal? */
207     if (!$this->is_account && $this->parent == NULL){
208       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
209         _("This 'dn' has no workstation features.")."</b>";
210       return($display);
211     }
213     /* Base select dialog */
214     $once = true;
215     foreach($_POST as $name => $value){
216       if(preg_match("/^chooseBase/",$name) && $once){
217         $once = false;
218         $this->dialog = new baseSelectDialog($this->config);
219         $this->dialog->setCurrentBase($this->base);
220       }
221     }
223     /* Dialog handling */
224     if(is_object($this->dialog)){
225       /* Must be called before save_object */
226       $this->dialog->save_object();
228       if($this->dialog->isClosed()){
229         $this->dialog = false;
230       }elseif($this->dialog->isSelected()){
231         $this->base = $this->dialog->isSelected();
232         $this->dialog= false;
233       }else{
234         return($this->dialog->execute());
235       }
236     }
238     /* Add new ntp Server to our list */ 
239     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
240       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
241     }
243     /* Delete selected NtpServer for list of used servers  */
244     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
245       foreach($_POST['gotoNtpServerSelected'] as $name){
246         unset($this->gotoNtpServer[$name]);
247       }
248     }
250     /* Fill templating stuff */
251     $smarty= get_smarty();
252     $smarty->assign("cn", $this->cn);
253     $smarty->assign("l", $this->l);
254     $smarty->assign("bases", $this->config->idepartments);
255     $smarty->assign("staticAddress", "");
256     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
257         
258     /* Check if workstation is online */
259     $query= "fping -q -r 1 -t 500 ".$this->cn;
260     exec ($query, $dummy, $retval);
262     foreach($this->attributes as $attrs ){
263       $smarty->assign($attrs."ACL",chkacl($this->acl,$attrs));
264     }
266     /* Offline */
267     if ($retval == 0){
268       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
269                                        "instant_update" => _("Instant update"),
270                                        "update" => _("Scheduled 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" => _("Scheduled 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("ntpservers", $this->config->data['SERVERS']['NTP']);
290     /* Variables */
291     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
292       $smarty->assign($val."_select", $this->$val);
293       $smarty->assign($val."ACL", chkacl($this->acl, $val));
294     }
295     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
297     /* tell smarty the inherit checkbox state */
298     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
300     /* Show main page */
301     $this->netConfigDNS->cn= $this->cn;
302     $smarty->assign("netconfig", $this->netConfigDNS->execute());
303     $smarty->assign("fai_activated",$this->fai_activated);
305     /* Display sub dialog from network settings */
306     if($this->netConfigDNS->dialog){
307       $this->dialog = TRUE;
308       return($this->netConfigDNS->execute());
309     }else{ 
310       $this->dialog = FALSE;
311     }
313     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
314   
315     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
316   }
319   function remove_from_parent()
320   {
321     $this->netConfigDNS->acl = $this->acl;
322     $this->netConfigDNS->remove_from_parent();
323     $ldap= $this->config->get_ldap_link();
324     $ldap->rmdir($this->dn);
325     show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
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   }
341   /* Save data to object */
342   function save_object()
343   {
344     plugin::save_object();
346     /* Save base, since this is no LDAP attribute */
347     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
348       $this->base= $_POST['base'];
349     }
350     $this->netConfigDNS->save_object();
352     /* Set inherit mode */
353     if(isset($_POST['workgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
354       if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
355         $this->inheritTimeServer = true;
356       }else{
357         $this->inheritTimeServer = false;
358       }
359     }
361     if(isset($_POST["inheritAll"])){
362       $this->set_everything_to_inherited();
363     }
364   }
367   /* Check supplied data */
368   function check()
369   {
370     /* Call common method to give check the hook */
371     $message= plugin::check();
373     if($this->cn != "wdefault"){
374       $message= array_merge($message, $this->netConfigDNS->check());
375     }
377     $ui= get_userinfo();
378     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
379     $acl= get_permissions ($this->dn, $ui->subtreeACL);
380     $acl= get_module_permission($acl, "group", $this->dn);
381     if (chkacl($acl, "create") != ""){
382       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
383     }
385     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
386       $message[]= _("The required field 'Workstation name' is not set.");
387     }
389     /* Check if given name is a valid host/dns name */
390     if(!is_dns_name($this->cn) && chkacl ($this->acl, "cn") == ""){
391       $message[] = _("Please specify a valid name for this object.");
392     }
394     if ($this->orig_dn != $this->dn){
395       $ldap= $this->config->get_ldap_link();
396       $ldap->cd ($this->base);
397   
398       if($this->cn == "wdefault"){
399         $ldap->cat($this->dn);
400       }else{
401         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
402       }
403       if ($ldap->count() != 0){
404         while ($attrs= $ldap->fetch()){
405           if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,ou=incoming,/", $ldap->getDN())){
406             continue;
407           } else {
408             if ($attrs['dn'] != $this->orig_dn){
409               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
410               break;
411             }
412           }
413         }
414       }
415     }
418     /* Check for valid ntpServer selection */
419     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
420       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
421     }
423     return ($message);
424   }
427   /* Save to LDAP */
428   function save()
429   {
430     $this->netConfigDNS->acl = $this->acl;
431     plugin::save();
433     /* Strip out 'default' values */
434     foreach (array("gotoSyslogServer") as $val){
435       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
436         $this->attrs[$val]= array();
437       }
438     }
440     /* Remove FAIstate */
441     if (isset($this->attrs['FAIstate'])){
442       unset ($this->attrs['FAIstate']);
443     }
445     /* Add missing arrays */
446     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
447       if (isset ($this->$val) && count ($this->$val) != 0){
448         $this->attrs["$val"]= $this->$val;
449       }
450     }
452     /* Remove all empty values */
453     if ($this->orig_dn == 'new'){
454       $attrs= array();
455       foreach ($this->attrs as $key => $val){
456         if (is_array($val) && count($val) == 0){
457           continue;
458         }
459         $attrs[$key]= $val;
460       }
461       $this->attrs= $attrs;
462     }
464     /* Update ntp server settings */
465     if($this->inheritTimeServer){
466       if($this->new){
467         if(isset($this->attrs['gotoNtpServer'])){
468           unset($this->attrs['gotoNtpServer']);
469         }
470       }else{
471         $this->attrs['gotoNtpServer'] = array();
472       }
473     }else{
474       /* Set ntpServers */
475       $this->attrs['gotoNtpServer'] = array();
476       foreach($this->gotoNtpServer as $server){
477         $this->attrs['gotoNtpServer'][] = $server;
478       }
479     }
481     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
482       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
483     }
485     /* Write back to ldap */
486     $ldap= $this->config->get_ldap_link();
487     if ($this->orig_dn == 'new'){
488       $ldap->cd($this->config->current['BASE']);
489       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
490       $ldap->cd($this->dn);
491       $ldap->add($this->attrs);
492       show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
493       if(!$this->didAction){
494         $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
495       }
496     } else {
497       if ($this->orig_dn != $this->dn){
498         $this->move($this->orig_dn, $this->dn);
499       }
500       $ldap->cd($this->dn);
501       $this->cleanup();
502       $ldap->modify ($this->attrs); 
504       if(!$this->didAction){
505         $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
506       }
507     }
509     /* Set macAddress to '-' this indicates that this is a Workstation Template  */
510     if($this->cn == "wdefault"){
511       $this->netConfigDNS->macAddress = "-";
512     }
514     $this->netConfigDNS->cn = $this->cn;
515     $this->netConfigDNS->save($this->dn);
516     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
517   }
519   function set_everything_to_inherited()
520   {
521     $this->gotoSyslogServer  = "default";
522     $this->inheritTimeServer = TRUE;
524     /* Set workstation service attributes to inherited */
525     if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
526       foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
527             "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
528         $this->parent->by_object['workservice']->$name = "default"; 
529       }
530     }
532     /* Set workstation startup attributes to inherited */
533     if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
534       $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
535       $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
536       $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
537     }
538   }
540   // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
541 ?>