Code

Added RDN support for users
[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",
45       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
46   var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
48   var $mapActions   = array("reboot"          => "localboot",
49                             "instant_update"  => "softupdate",
50                             "update"          => "scheduledupdate",
51                             "reinstall"       => "install",
52                             "rescan"          => "",
53                             "memcheck"        => "memcheck",
54                             "sysinfo"         => "sysinfo");
57   var $fai_activated = FALSE;
59   function workgeneric ($config, $dn= NULL, $parent= NULL)
60   {
61     /* Check if FAI is activated */
62     $tmp = search_config($config->data,"faiManagement","CLASS");
63     if(!empty($tmp)){
64       $this->fai_activated = TRUE;
65     }
67     plugin::plugin ($config, $dn, $parent);
68     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
69     $this->netConfigDNS->acl = $this->acl;
71     /* Read arrays */
72     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
73       if (!isset($this->attrs[$val])){
74         continue;
75       }
76       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
77         array_push($this->$val, $this->attrs[$val][$i]);
78       }
79     }
81     /* Create used ntp server array */
82     $this->gotoNtpServer= array();
83     if(isset($this->attrs['gotoNtpServer'])){
84       $this->inheritTimeServer = false;
85       unset($this->attrs['gotoNtpServer']['count']);
86       foreach($this->attrs['gotoNtpServer'] as $server){
87         $this->gotoNtpServer[$server] = $server;
88       }
89     }
91     /* Set inherit checkbox state */
92     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
93       $this->inheritTimeServer = true;
94       $this->gotoNtpServer=array();
95     }
97     /* Create available ntp options */
98     $tmp = $this->config->data['SERVERS']['NTP'];
99     $this->gotoNtpServers = array();
100     foreach($tmp as $key => $server){
101       if($server == "default") continue;
102       $this->gotoNtpServers[$server] = $server;
103     }
105     $this->modes["active"]= _("Activated");
106     $this->modes["locked"]= _("Locked");
108     /* Set base */
109     if ($this->dn == "new"){
110       $ui= get_userinfo();
111       $this->base= dn2base($ui->dn);
112     } else {
113       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
114     }
116     /* Create an array of all Syslog servers */
117     $tmp = $this->config->data['SERVERS']['SYSLOG'];
118     foreach($tmp as $server){
119       $visible = $server;
120       if($server == "default") {
121         $visible = "["._("inherited")."]";
122       }
123       $this->gotoSyslogServers[$server] = $visible;
124     }
126     /* Save 'dn' for later referal */
127     $this->orig_dn= $this->dn;
128   }
130   function execute()
131   {
132     /* Call parent execute */
133     plugin::execute();
134     $this->netConfigDNS->acl = $this->acl;
136     /* Do we need to flip is_account state? */
137     if (isset($_POST['modify_state'])){
138       $this->is_account= !$this->is_account;
139     }
141     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
142       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
143       if ($cmd == ""){
144         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
145       } else {
146         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
147         if ($retval != 0){
148           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
149         } elseif ($_POST['saction'] != "wake") {
151           if($this->fai_activated && $this->dn != "new"){
152             /* Set FAIstate */
153             $ldap = $this->config->get_ldap_link();
154             $ldap->cd($this->config->current['BASE']);
155             $ldap->cat($this->dn,array("objectClass"));
156             $res = $ldap->fetch();
158             $attrs = array();
159             $attrs['FAIstate'] = "";
160             if(isset($this->mapActions[$_POST['saction']])){
161               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
162             }
164             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
165               $attrs['objectClass'][] = $res['objectClass'][$i];
166             }
168             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
169               $attrs['objectClass'][] = "FAIobject";
170             }
172             if($attrs['FAIstate'] == ""){
173 #FIXME we should check if FAIobject is used anymore
174               $attrs['FAIstate'] = array();
175             }
177             $ldap->cd($this->dn);
178             $ldap->modify($attrs);
179             show_ldap_error($ldap->get_error());
181             $this->didAction= TRUE; 
182           }
183         }
184       }
185     }
187     /* Do we represent a valid terminal? */
188     if (!$this->is_account && $this->parent == NULL){
189       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
190         _("This 'dn' has no workstation features.")."</b>";
191       return($display);
192     }
194     /* Base select dialog */
195     $once = true;
196     foreach($_POST as $name => $value){
197       if(preg_match("/^chooseBase/",$name) && $once){
198         $once = false;
199         $this->dialog = new baseSelectDialog($this->config);
200         $this->dialog->setCurrentBase($this->base);
201       }
202     }
204     /* Dialog handling */
205     if(is_object($this->dialog)){
206       /* Must be called before save_object */
207       $this->dialog->save_object();
209       if($this->dialog->isClosed()){
210         $this->dialog = false;
211       }elseif($this->dialog->isSelected()){
212         $this->base = $this->dialog->isSelected();
213         $this->dialog= false;
214       }else{
215         return($this->dialog->execute());
216       }
217     }
219     /* Add new ntp Server to our list */ 
220     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
221       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
222     }
224     /* Delete selected NtpServer for list of used servers  */
225     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
226       foreach($_POST['gotoNtpServerSelected'] as $name){
227         unset($this->gotoNtpServer[$name]);
228       }
229     }
231     /* Fill templating stuff */
232     $smarty= get_smarty();
233     $smarty->assign("cn", $this->cn);
234     $smarty->assign("l", $this->l);
235     $smarty->assign("bases", $this->config->idepartments);
236     $smarty->assign("staticAddress", "");
237     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
238         
239     /* Check if workstation is online */
240     $query= "fping -q -r 1 -t 500 ".$this->cn;
241     exec ($query, $dummy, $retval);
243     foreach($this->attributes as $attrs ){
244       $smarty->assign($attrs."ACL",chkacl($this->acl,$attrs));
245     }
247     /* Offline */
248     if ($retval == 0){
249       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
250                                        "instant_update" => _("Instant update"),
251                                        "update" => _("Scheduled update"),
252                                        "reinstall" => _("Reinstall"),
253                                        "rescan" => _("Rescan hardware"),
254                                        "memcheck" => _("Memory test"),
255                                        "sysinfo"  => _("System analysis")));
256     } else {
257       $smarty->assign("actions", array("wake" => _("Wake up"),
258                                        "reinstall" => _("Reinstall"),
259                                        "update" => _("Scheduled update"),
260                                        "memcheck" => _("Memory test"),
261                                        "sysinfo"  => _("System analysis")));
262     }
263     /* Arrays */
264     $smarty->assign("modes", $this->modes);
265     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
266     $smarty->assign("syslogservers", $this->gotoSyslogServers);
267     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
269     /* Variables */
270     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
271       $smarty->assign($val."_select", $this->$val);
272       $smarty->assign($val."ACL", chkacl($this->acl, $val));
273     }
274     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
276     /* tell smarty the inherit checkbox state */
277     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
279     /* Show main page */
280     $this->netConfigDNS->cn= $this->cn;
281     $smarty->assign("netconfig", $this->netConfigDNS->execute());
282     $smarty->assign("fai_activated",$this->fai_activated);
283     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
284   }
286   function remove_from_parent()
287   {
288     $this->netConfigDNS->acl = $this->acl;
289     $this->netConfigDNS->remove_from_parent();
290     $ldap= $this->config->get_ldap_link();
291     $ldap->rmdir($this->dn);
292     show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
294     /* Optionally execute a command after we're done */
295     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
297     /* Delete references to object groups */
298     $ldap->cd ($this->config->current['BASE']);
299     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
300     while ($ldap->fetch()){
301       $og= new ogroup($this->config, $ldap->getDN());
302       unset($og->member[$this->dn]);
303       $og->save ();
304     }
305   }
308   /* Save data to object */
309   function save_object()
310   {
311     plugin::save_object();
313     /* Save base, since this is no LDAP attribute */
314     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
315       $this->base= $_POST['base'];
316     }
317     $this->netConfigDNS->save_object();
319     /* Set inherit mode */
320     if(isset($_POST['workgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
321       if(isset($_POST["inheritTimeServer"])){
322         $this->inheritTimeServer = true;
323       }else{
324         $this->inheritTimeServer = false;
325       }
326     }
327   }
330   /* Check supplied data */
331   function check()
332   {
333     /* Call common method to give check the hook */
334     $message= plugin::check();
336     if($this->cn != "wdefault"){
337       $message= array_merge($message, $this->netConfigDNS->check());
338     }
340     $ui= get_userinfo();
341     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
342     $acl= get_permissions ($this->dn, $ui->subtreeACL);
343     $acl= get_module_permission($acl, "group", $this->dn);
344     if (chkacl($acl, "create") != ""){
345       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
346     }
348     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
349       $message[]= _("The required field 'Workstation name' is not set.");
350     }
352     if ($this->orig_dn != $this->dn){
353       $ldap= $this->config->get_ldap_link();
354       $ldap->cd ($this->base);
355   
356       if($this->cn == "wdefault"){
357         $ldap->cat($this->dn);
358       }else{
359         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
360       }
361       if ($ldap->count() != 0){
362         while ($attrs= $ldap->fetch()){
363           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
364             continue;
365           } else {
366             if ($attrs['dn'] != $this->orig_dn){
367               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
368               break;
369             }
370           }
371         }
372       }
373     }
376     /* Check for valid ntpServer selection */
377     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
378       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
379     }
381     return ($message);
382   }
385   /* Save to LDAP */
386   function save()
387   {
388     $this->netConfigDNS->acl = $this->acl;
389     plugin::save();
391     /* Strip out 'default' values */
392     foreach (array("gotoSyslogServer") as $val){
393       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
394         $this->attrs[$val]= array();
395       }
396     }
398     /* Add missing arrays */
399     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
400       if (isset ($this->$val) && count ($this->$val) != 0){
401         $this->attrs["$val"]= $this->$val;
402       }
403     }
405     /* Remove all empty values */
406     if ($this->orig_dn == 'new'){
407       $attrs= array();
408       foreach ($this->attrs as $key => $val){
409         if (is_array($val) && count($val) == 0){
410           continue;
411         }
412         $attrs[$key]= $val;
413       }
414       $this->attrs= $attrs;
415     }
417     /* Update ntp server settings */
418     if($this->inheritTimeServer){
419       if($this->new){
420         if(isset($this->attrs['gotoNtpServer'])){
421           unset($this->attrs['gotoNtpServer']);
422         }
423       }else{
424         $this->attrs['gotoNtpServer'] = array();
425       }
426     }else{
427       /* Set ntpServers */
428       $this->attrs['gotoNtpServer'] = array();
429       foreach($this->gotoNtpServer as $server){
430         $this->attrs['gotoNtpServer'][] = $server;
431       }
432     }
434     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
435       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
436     }
438     /* Write back to ldap */
439     $ldap= $this->config->get_ldap_link();
440     if ($this->orig_dn == 'new'){
441       $ldap->cd($this->config->current['BASE']);
442       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
443       $ldap->cd($this->dn);
444       $ldap->add($this->attrs);
445       show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
446       if(!$this->didAction){
447         $this->handle_post_events("add");
448       }
449     } else {
450       if ($this->orig_dn != $this->dn){
451         $this->move($this->orig_dn, $this->dn);
452       }
453       $ldap->cd($this->dn);
454       $this->cleanup();
455       $ldap->modify ($this->attrs); 
457       if(!$this->didAction){
458         $this->handle_post_events("modify");
459       }
460     }
462     /* Set macAddress to '-' this indicates that this is a Workstation Template  */
463     if($this->cn == "wdefault"){
464       $this->netConfigDNS->macAddress = "-";
465     }
467     $this->netConfigDNS->cn = $this->cn;
468     $this->netConfigDNS->save($this->dn);
469     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
470   }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>