Code

02374a3db10e5e1b3e2fda3cbfb5849035d6af66
[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);
53     /* Read arrays */
54     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
55       if (!isset($this->attrs[$val])){
56         continue;
57       }
58       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
59         array_push($this->$val, $this->attrs[$val][$i]);
60       }
61     }
63     $this->modes["active"]= _("Activated");
64     $this->modes["locked"]= _("Locked");
65     $this->modes["memcheck"]= _("Memory test");
66     $this->modes["sysinfo"]= _("System analysis");
68     /* Set base */
69     if ($this->dn == "new"){
70       $ui= get_userinfo();
71       $this->base= dn2base($ui->dn);
72     } else {
73       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
74     }
76     /* Workaround for not single-value attribute gotoNtpServer */
77     if (isset($this->attrs['gotoNtpServer'][0])){
78       $this->gotoNtpServer= $this->attrs['gotoNtpServer'][0];
79     }
81     /* Save 'dn' for later referal */
82     $this->orig_dn= $this->dn;
83   }
85   function execute()
86   {
87     /* Call parent execute */
88     plugin::execute();
90     /* Do we need to flip is_account state? */
91     if (isset($_POST['modify_state'])){
92       $this->is_account= !$this->is_account;
93     }
95     if (isset($_POST['action'])){
96       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
97       if ($cmd == ""){
98         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
99       } else {
100         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
101         if ($retval != 0){
102           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
103         } else {
104           $this->didAction= TRUE;
105         }
106       }
107     }
109     /* Do we represent a valid terminal? */
110     if (!$this->is_account && $this->parent == NULL){
111       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
112         _("This 'dn' has no workstation features.")."</b>";
113       return($display);
114     }
116     /* Base select dialog */
117     $once = true;
118     foreach($_POST as $name => $value){
119       if(preg_match("/^chooseBase/",$name) && $once){
120         $once = false;
121         $this->dialog = new baseSelectDialog($this->config);
122         $this->dialog->setCurrentBase($this->base);
123       }
124     }
126     /* Dialog handling */
127     if(is_object($this->dialog)){
128       /* Must be called before save_object */
129       $this->dialog->save_object();
131       if($this->dialog->isClosed()){
132         $this->dialog = false;
133       }elseif($this->dialog->isSelected()){
134         $this->base = $this->dialog->isSelected();
135         $this->dialog= false;
136       }else{
137         return($this->dialog->execute());
138       }
139     }
141     /* Fill templating stuff */
142     $smarty= get_smarty();
143     $smarty->assign("cn", $this->cn);
144     $smarty->assign("l", $this->l);
145     $smarty->assign("bases", $this->config->idepartments);
146     $smarty->assign("staticAddress", "");
147         
148     /* Check if workstation is online */
149     $query= "fping -q -r 1 -t 500 ".$this->cn;
150     exec ($query, $dummy, $retval);
152     /* Offline */
153     if ($retval == 0){
154       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
155                                        "instant_update" => _("Instant update"),
156                                        "update" => _("Scheduled update"),
157                                        "reinstall" => _("Reinstall"),
158                                        "rescan" => _("Rescan hardware")));
159     } else {
160       $smarty->assign("actions", array("wake" => _("Wake up"),
161                                        "reinstall" => _("Reinstall"),
162                                        "update" => _("Scheduled update")));
163     }
164     /* Arrays */
165     $smarty->assign("modes", $this->modes);
166     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
167     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
168     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
170     /* Variables */
171     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
172       $smarty->assign($val."_select", $this->$val);
173       $smarty->assign($val."ACL", chkacl($this->acl, $val));
174     }
175     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
177     /* Show main page */
178     $smarty->assign("netconfig", $this->netConfigDNS->execute());
179     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
180   }
182   function remove_from_parent()
183   {
184     $this->netConfigDNS->remove_from_parent();
185     $ldap= $this->config->get_ldap_link();
186     $ldap->rmdir($this->dn);
187     show_ldap_error($ldap->get_error());
189     /* Optionally execute a command after we're done */
190     $this->handle_post_events("remove", array("macAddress", $this->netConfigDNS->macAddress));
192     /* Delete references to object groups */
193     $ldap->cd ($this->config->current['BASE']);
194     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
195     while ($ldap->fetch()){
196       $og= new ogroup($this->config, $ldap->getDN());
197       unset($og->member[$this->dn]);
198       $og->save ();
199     }
200   }
203   /* Save data to object */
204   function save_object()
205   {
206     plugin::save_object();
208     /* Save base, since this is no LDAP attribute */
209     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
210       $this->base= $_POST['base'];
211     }
212     $this->netConfigDNS->save_object();
213   }
216   /* Check supplied data */
217   function check()
218   {
219     /* Call common method to give check the hook */
220     $message= plugin::check();
221     $message= array_merge($message, $this->netConfigDNS->check());
223     $ui= get_userinfo();
224     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
225     $acl= get_permissions ($this->dn, $ui->subtreeACL);
226     $acl= get_module_permission($acl, "group", $this->dn);
227     if (chkacl($acl, "create") != ""){
228       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
229     }
231     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
232       $message[]= _("The required field 'Workstation name' is not set.");
233     }
235     if ($this->orig_dn != $this->dn){
236       $ldap= $this->config->get_ldap_link();
237       $ldap->cd ($this->base);
238       $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
239       if ($ldap->count() != 0){
240         while ($attrs= $ldap->fetch()){
241           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
242             continue;
243           } else {
244             if ($attrs['dn'] != $this->orig_dn){
245               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
246               break;
247             }
248           }
249         }
250       }
251     }
253     return ($message);
254   }
257   /* Save to LDAP */
258   function save()
259   {
260     plugin::save();
262     /* Strip out 'default' values */
263     foreach (array("gotoSyslogServer") as $val){
265       if ($this->attrs[$val] == "default"){
266         $this->attrs[$val]= array();
267       }
268     }
270     /* Add missing arrays */
271     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
272       if (isset ($this->$val) && count ($this->$val) != 0){
273         $this->attrs["$val"]= $this->$val;
274       }
275     }
277     /* Remove all empty values */
278     if ($this->orig_dn == 'new'){
279       $attrs= array();
280       foreach ($this->attrs as $key => $val){
281         if (is_array($val) && count($val) == 0){
282           continue;
283         }
284         $attrs[$key]= $val;
285       }
286       $this->attrs= $attrs;
287     }
289     /* Write back to ldap */
290     $ldap= $this->config->get_ldap_link();
291     if ($this->orig_dn == 'new'){
292       $ldap->cd($this->config->current['BASE']);
293       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
294       $ldap->cd($this->dn);
295       $ldap->add($this->attrs);
296       if(!$this->didAction){
297         $this->handle_post_events("add");
298       }
299     } else {
300       if ($this->orig_dn != $this->dn){
301         $this->move($this->orig_dn, $this->dn);
302       }
303       $ldap->cd($this->dn);
304       $this->cleanup();
305 $ldap->modify ($this->attrs); 
307       if(!$this->didAction){
308         $this->handle_post_events("modify");
309       }
310     }
311     $this->netConfigDNS->cn = $this->cn;
312     $this->netConfigDNS->save($this->dn);
313     show_ldap_error($ldap->get_error());
314   }
318 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
319 ?>