Code

Added support for general check hooks
[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= "";
14   var $gotoSndModule= "";
15   var $gotoFloppyEnable= "";
16   var $gotoCdromEnable= "";
17   var $ghCpuType= "-";
18   var $ghMemSize= "-";
19   var $ghUsbSupport= "-";
20   var $ghNetNic= array();
21   var $ghIdeDev= array();
22   var $ghScsiDev= array();
23   var $ghGfxAdapter= "-";
24   var $ghSoundAdapter= "-";
25   var $gotoLastUser= "-";
26   var $FAIscript="";
27   var $didAction= FALSE;
29   /* Needed values and lists */
30   var $base= "";
31   var $cn= "";
32   var $l= "";
33   var $orig_dn= "";
35   /* Plugin side filled */
36   var $modes= array();
38   var $netConfigDNS;
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   function workgeneric ($config, $dn= NULL)
49   {
50     plugin::plugin ($config, $dn);
51     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
52     /* Load available modes */
53     $ldap= $this->config->get_ldap_link();
54     $ldap->cd ($this->config->current['BASE']);
55     $ldap->search ("(objectClass=gotoInstallProfile)",array("cn"));
56     while ($attrs= $ldap->fetch()){
57       $this->modes[]= $attrs["cn"][0];
58     }
60     /* Read arrays */
61     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
62       if (!isset($this->attrs[$val])){
63         continue;
64       }
65       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
66         array_push($this->$val, $this->attrs[$val][$i]);
67       }
68     }
70     $this->modes["active"]= _("Activated");
71     $this->modes["locked"]= _("Locked");
72     $this->modes["memcheck"]= _("Memory test");
73     $this->modes["sysinfo"]= _("System analysis");
75     /* Set base */
76     if ($this->dn == "new"){
77       $ui= get_userinfo();
78       $this->base= dn2base($ui->dn);
79     } else {
80       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
81     }
83     /* Save 'dn' for later referal */
84     $this->orig_dn= $this->dn;
85   }
87   function execute()
88   {
89     /* Call parent execute */
90     plugin::execute();
92     /* Do we need to flip is_account state? */
93     if (isset($_POST['modify_state'])){
94       $this->is_account= !$this->is_account;
95     }
97     if (isset($_POST['action'])){
98       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
99       if ($cmd == ""){
100         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
101       } else {
102         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
103         if ($retval != 0){
104           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
105         } else {
106           $this->didAction= TRUE;
107         }
108       }
109     }
111     /* Do we represent a valid terminal? */
112     if (!$this->is_account && $this->parent == NULL){
113       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
114         _("This 'dn' has no workstation features.")."</b>";
115       return($display);
116     }
118     /* Base select dialog */
119     $once = true;
120     foreach($_POST as $name => $value){
121       if(preg_match("/^chooseBase/",$name) && $once){
122         $once = false;
123         $this->dialog = new baseSelectDialog($this->config);
124         $this->dialog->setCurrentBase($this->base);
125       }
126     }
128     /* Dialog handling */
129     if(is_object($this->dialog)){
130       /* Must be called before save_object */
131       $this->dialog->save_object();
133       if($this->dialog->isClosed()){
134         $this->dialog = false;
135       }elseif($this->dialog->isSelected()){
136         $this->base = $this->dialog->isSelected();
137         $this->dialog= false;
138       }else{
139         return($this->dialog->execute());
140       }
141     }
143     /* Fill templating stuff */
144     $smarty= get_smarty();
145     $smarty->assign("cn", $this->cn);
146     $smarty->assign("l", $this->l);
147     $smarty->assign("bases", $this->config->idepartments);
148     $smarty->assign("staticAddress", "");
149         
150     /* Check if workstation is online */
151     $query= "fping -q -r 1 -t 500 ".$this->cn;
152     exec ($query, $dummy, $retval);
154     /* Offline */
155     if ($retval == 0){
156       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
157                                        "instant_update" => _("Instant update"),
158                                        "update" => _("Scheduled update"),
159                                        "reinstall" => _("Reinstall"),
160                                        "rescan" => _("Rescan hardware")));
161     } else {
162       $smarty->assign("actions", array("wake" => _("Wake up"),
163                                        "reinstall" => _("Reinstall"),
164                                        "update" => _("Scheduled update")));
165     }
166     /* Arrays */
167     $smarty->assign("modes", $this->modes);
168     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
169     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
170     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
172     /* Variables */
173     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
174       $smarty->assign($val."_select", $this->$val);
175       $smarty->assign($val."ACL", chkacl($this->acl, $val));
176     }
177     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
179     /* Show main page */
180     $smarty->assign("netconfig", $this->netConfigDNS->execute());
181     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
182   }
184   function remove_from_parent()
185   {
186     $this->netConfigDNS->remove_from_parent();
187     $ldap= $this->config->get_ldap_link();
188     $ldap->rmdir($this->dn);
189     show_ldap_error($ldap->get_error());
191     /* Optionally execute a command after we're done */
192     $this->handle_post_events("remove");
194     /* Delete references to object groups */
195     $ldap->cd ($this->config->current['BASE']);
196     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
197     while ($ldap->fetch()){
198       $og= new ogroup($this->config, $ldap->getDN());
199       unset($og->member[$this->dn]);
200       $og->save ();
201     }
202   }
205   /* Save data to object */
206   function save_object()
207   {
208     plugin::save_object();
210     /* Save base, since this is no LDAP attribute */
211     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
212       $this->base= $_POST['base'];
213     }
214     $this->netConfigDNS->save_object();
215   }
218   /* Check supplied data */
219   function check()
220   {
221     /* Call common method to give check the hook */
222     $message= plugin::check();
223     $message= array_merge($message, $this->netConfigDNS->check());
225     $ui= get_userinfo();
226     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
227     $acl= get_permissions ($this->dn, $ui->subtreeACL);
228     $acl= get_module_permission($acl, "group", $this->dn);
229     if (chkacl($acl, "create") != ""){
230       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
231     }
233     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
234       $message[]= _("The required field 'Workstation name' is not set.");
235     }
237     if ($this->orig_dn != $this->dn){
238       $ldap= $this->config->get_ldap_link();
239       $ldap->cd ($this->base);
240       $ldap->search ("(cn=".$this->cn.")", array("cn"));
241       if ($ldap->count() != 0){
242         while ($attrs= $ldap->fetch()){
243           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
244             continue;
245           } else {
246             if ($attrs['dn'] != $this->orig_dn){
247               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
248               break;
249             }
250           }
251         }
252       }
253     }
255     return ($message);
256   }
259   /* Save to LDAP */
260   function save()
261   {
262     plugin::save();
264     /* Strip out 'default' values */
265     foreach (array("gotoSyslogServer") as $val){
267       if ($this->attrs[$val] == "default"){
268         $this->attrs[$val]= array();
269       }
270     }
272     /* Add missing arrays */
273     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
274       if (isset ($this->$val) && count ($this->$val) != 0){
275         $this->attrs["$val"]= $this->$val;
276       }
277     }
279     /* Remove all empty values */
280     if ($this->orig_dn == 'new'){
281       $attrs= array();
282       foreach ($this->attrs as $key => $val){
283         if (is_array($val) && count($val) == 0){
284           continue;
285         }
286         $attrs[$key]= $val;
287       }
288       $this->attrs= $attrs;
289     }
291     /* Write back to ldap */
292     $ldap= $this->config->get_ldap_link();
293     if ($this->orig_dn == 'new'){
294       $ldap->cd($this->config->current['BASE']);
295       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
296       $ldap->cd($this->dn);
297       $ldap->add($this->attrs);
298       if(!$this->didAction){
299         $this->handle_post_events("add");
300       }
301     } else {
302       if ($this->orig_dn != $this->dn){
303         $this->move($this->orig_dn, $this->dn);
304       }
305       $ldap->cd($this->dn);
306       $this->cleanup();
307 $ldap->modify ($this->attrs); 
309       if(!$this->didAction){
310         $this->handle_post_events("modify");
311       }
312     }
313     $this->netConfigDNS->cn = $this->cn;
314     $this->netConfigDNS->save($this->dn);
315     show_ldap_error($ldap->get_error());
316   }
320 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
321 ?>