Code

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