Code

Fixed typo which avoids removal
[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 $gotoNtpServer= array();
14   var $gotoNtpServers= array();
15   var $gotoSndModule= "";
16   var $gotoFloppyEnable= "";
17   var $gotoCdromEnable= "";
18   var $ghCpuType= "-";
19   var $ghMemSize= "-";
20   var $ghUsbSupport= "-";
21   var $ghNetNic= array();
22   var $ghIdeDev= array();
23   var $ghScsiDev= array();
24   var $ghGfxAdapter= "-";
25   var $ghSoundAdapter= "-";
26   var $gotoLastUser= "-";
27   var $FAIscript="";
28   var $didAction= FALSE;
30   /* Needed values and lists */
31   var $base= "";
32   var $cn= "";
33   var $l= "";
34   var $orig_dn= "";
36   /* Plugin side filled */
37   var $modes= array();
39   var $netConfigDNS;
41   var $inheritTimeServer = true;
43   /* attribute list for save action */
44   var $ignore_account= TRUE;
45   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
46       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
47       "ghCpuType", "ghMemSize", "ghUsbSupport",
48       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
49   var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
51   function workgeneric ($config, $dn= NULL)
52   {
53     plugin::plugin ($config, $dn);
54     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
56     /* Read arrays */
57     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
58       if (!isset($this->attrs[$val])){
59         continue;
60       }
61       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
62         array_push($this->$val, $this->attrs[$val][$i]);
63       }
64     }
66     /* Create used ntp server array */
67     $this->gotoNtpServer= array();
68     if(isset($this->attrs['gotoNtpServer'])){
69       $this->inheritTimeServer = false;
70       unset($this->attrs['gotoNtpServer']['count']);
71       foreach($this->attrs['gotoNtpServer'] as $server){
72         $this->gotoNtpServer[$server] = $server;
73       }
74     }
76     /* Set inherit checkbox state */
77     if(in_array("default",$this->gotoNtpServer)){
78       $this->inheritTimeServer = true;
79       $this->gotoNtpServer=array();
80     }
82     /* Create available ntp options */
83     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
84     foreach($this->gotoNtpServers as $key => $server){
85       if($server == "default"){
86         unset($this->gotoNtpServers[$key]);
87       }
88     }
90     $this->modes["active"]= _("Activated");
91     $this->modes["locked"]= _("Locked");
92     $this->modes["memcheck"]= _("Memory test");
93     $this->modes["sysinfo"]= _("System analysis");
95     /* Set base */
96     if ($this->dn == "new"){
97       $ui= get_userinfo();
98       $this->base= dn2base($ui->dn);
99     } else {
100       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
101     }
103     /* Save 'dn' for later referal */
104     $this->orig_dn= $this->dn;
105   }
107   function execute()
108   {
109     /* Call parent execute */
110     plugin::execute();
112     /* Do we need to flip is_account state? */
113     if (isset($_POST['modify_state'])){
114       $this->is_account= !$this->is_account;
115     }
117     if (isset($_POST['action'])){
118       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
119       if ($cmd == ""){
120         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
121       } else {
122         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
123         if ($retval != 0){
124           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
125         } else {
126           $this->didAction= TRUE;
127         }
128       }
129     }
131     /* Do we represent a valid terminal? */
132     if (!$this->is_account && $this->parent == NULL){
133       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
134         _("This 'dn' has no workstation features.")."</b>";
135       return($display);
136     }
138     /* Base select dialog */
139     $once = true;
140     foreach($_POST as $name => $value){
141       if(preg_match("/^chooseBase/",$name) && $once){
142         $once = false;
143         $this->dialog = new baseSelectDialog($this->config);
144         $this->dialog->setCurrentBase($this->base);
145       }
146     }
148     /* Dialog handling */
149     if(is_object($this->dialog)){
150       /* Must be called before save_object */
151       $this->dialog->save_object();
153       if($this->dialog->isClosed()){
154         $this->dialog = false;
155       }elseif($this->dialog->isSelected()){
156         $this->base = $this->dialog->isSelected();
157         $this->dialog= false;
158       }else{
159         return($this->dialog->execute());
160       }
161     }
163     /* Add new ntp Server to our list */ 
164     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
165       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
166     }
168     /* Delete selected NtpServer for list of used servers  */
169     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
170       foreach($_POST['gotoNtpServerSelected'] as $name){
171         unset($this->gotoNtpServer[$name]);
172       }
173     }
175     /* Fill templating stuff */
176     $smarty= get_smarty();
177     $smarty->assign("cn", $this->cn);
178     $smarty->assign("l", $this->l);
179     $smarty->assign("bases", $this->config->idepartments);
180     $smarty->assign("staticAddress", "");
181     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
182         
183     /* Check if workstation is online */
184     $query= "fping -q -r 1 -t 500 ".$this->cn;
185     exec ($query, $dummy, $retval);
187     /* Offline */
188     if ($retval == 0){
189       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
190                                        "instant_update" => _("Instant update"),
191                                        "update" => _("Scheduled update"),
192                                        "reinstall" => _("Reinstall"),
193                                        "rescan" => _("Rescan hardware")));
194     } else {
195       $smarty->assign("actions", array("wake" => _("Wake up"),
196                                        "reinstall" => _("Reinstall"),
197                                        "update" => _("Scheduled update")));
198     }
199     /* Arrays */
200     $smarty->assign("modes", $this->modes);
201     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
202     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
203     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
205     /* Variables */
206     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
207       $smarty->assign($val."_select", $this->$val);
208       $smarty->assign($val."ACL", chkacl($this->acl, $val));
209     }
210     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
212     /* tell smarty the inherit checkbox state */
213     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
215     /* Show main page */
216     $smarty->assign("netconfig", $this->netConfigDNS->execute());
217     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
218   }
220   function remove_from_parent()
221   {
222     $this->netConfigDNS->remove_from_parent();
223     $ldap= $this->config->get_ldap_link();
224     $ldap->rmdir($this->dn);
225     show_ldap_error($ldap->get_error());
227     /* Optionally execute a command after we're done */
228     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
230     /* Delete references to object groups */
231     $ldap->cd ($this->config->current['BASE']);
232     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
233     while ($ldap->fetch()){
234       $og= new ogroup($this->config, $ldap->getDN());
235       unset($og->member[$this->dn]);
236       $og->save ();
237     }
238   }
241   /* Save data to object */
242   function save_object()
243   {
244     plugin::save_object();
246     /* Save base, since this is no LDAP attribute */
247     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
248       $this->base= $_POST['base'];
249     }
250     $this->netConfigDNS->save_object();
252     /* Set inherit mode */
253     if(isset($_POST['workgeneric_posted'])){
254       if(isset($_POST["inheritTimeServer"])){
255         $this->inheritTimeServer = true;
256       }else{
257         $this->inheritTimeServer = false;
258       }
259     }
261   }
264   /* Check supplied data */
265   function check()
266   {
267     /* Call common method to give check the hook */
268     $message= plugin::check();
269     $message= array_merge($message, $this->netConfigDNS->check());
271     $ui= get_userinfo();
272     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
273     $acl= get_permissions ($this->dn, $ui->subtreeACL);
274     $acl= get_module_permission($acl, "group", $this->dn);
275     if (chkacl($acl, "create") != ""){
276       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
277     }
279     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
280       $message[]= _("The required field 'Workstation name' is not set.");
281     }
283     if ($this->orig_dn != $this->dn){
284       $ldap= $this->config->get_ldap_link();
285       $ldap->cd ($this->base);
286       $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
287       if ($ldap->count() != 0){
288         while ($attrs= $ldap->fetch()){
289           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
290             continue;
291           } else {
292             if ($attrs['dn'] != $this->orig_dn){
293               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
294               break;
295             }
296           }
297         }
298       }
299     }
300     
301     /* Check for valid ntpServer selection */
302     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
303       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
304     }
306     return ($message);
307   }
310   /* Save to LDAP */
311   function save()
312   {
313     plugin::save();
315     /* Strip out 'default' values */
316     foreach (array("gotoSyslogServer") as $val){
318       if ($this->attrs[$val] == "default"){
319         $this->attrs[$val]= array();
320       }
321     }
323     /* Add missing arrays */
324     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
325       if (isset ($this->$val) && count ($this->$val) != 0){
326         $this->attrs["$val"]= $this->$val;
327       }
328     }
330     /* Remove all empty values */
331     if ($this->orig_dn == 'new'){
332       $attrs= array();
333       foreach ($this->attrs as $key => $val){
334         if (is_array($val) && count($val) == 0){
335           continue;
336         }
337         $attrs[$key]= $val;
338       }
339       $this->attrs= $attrs;
340     }
342     /* Update ntp server settings */
343     if($this->inheritTimeServer){
344       $this->attrs['gotoNtpServer'] = "default";
345     }else{
346       /* Set ntpServers */
347       $this->attrs['gotoNtpServer'] = array();
348       foreach($this->gotoNtpServer as $server){
349         $this->attrs['gotoNtpServer'][] = $server;
350       }
351     }
353     /* Write back to ldap */
354     $ldap= $this->config->get_ldap_link();
355     if ($this->orig_dn == 'new'){
356       $ldap->cd($this->config->current['BASE']);
357       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
358       $ldap->cd($this->dn);
359       $ldap->add($this->attrs);
360       if(!$this->didAction){
361         $this->handle_post_events("add");
362       }
363     } else {
364       if ($this->orig_dn != $this->dn){
365         $this->move($this->orig_dn, $this->dn);
366       }
367       $ldap->cd($this->dn);
368       $this->cleanup();
369 $ldap->modify ($this->attrs); 
371       if(!$this->didAction){
372         $this->handle_post_events("modify");
373       }
374     }
375     $this->netConfigDNS->cn = $this->cn;
376     $this->netConfigDNS->save($this->dn);
377     show_ldap_error($ldap->get_error());
378   }
382 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
383 ?>