Code

Fixed typo
[gosa.git] / plugins / admin / ogroups / class_termgroup.inc
1 <?php
3 class termgroup extends plugin
4 {
5   /* attribute list for save action */
6   var $attributes= array("gotoMode","gotoSyslogServer", "gotoNtpServer");
7   var $objectclasses= array("gotoWorkstationTemplate");
9   var $members= array();
11   var $gotoMode           = "locked";
12   var $gotoSyslogServer   = "";
13   var $gotoNtpServer      = array();
14   var $gotoNtpServers     = array();
15   var $modes              = array();
16   var $inheritTimeServer  = true;
17   var $acl                ;
18   var $is_account                 = true; 
19   var $orig_dn                = "";
20   var $didAction                  = FALSE;
22   var $mapActions   = array("reboot"          => "localboot",
23                             "instant_update"  => "softupdate",
24                             "update"          => "scheduledupdate",
25                             "reinstall"       => "install",
26                             "rescan"          => "",
27                             "memcheck"        => "memcheck",
28                             "sysinfo"         => "sysinfo");
30   var $CopyPasteVars = array("gotoNtpServers","modes","inheritTimeServer");
32   function termgroup ($config, $dn= NULL, $parent= NULL)
33   {
34     /***************
35       Some  initialisations
36      ***************/
38     plugin::plugin($config, $dn, $parent);
40     $ldap= $config->get_ldap_link();
42     $this->is_account = true;
43     $ui               = get_userinfo();
44     $acl              = get_permissions ($this->dn, $ui->subtreeACL);
45     $this->acl        = get_module_permission($acl, "group", $this->dn);
47     $this->modes["active"]= _("Activated");
48     $this->modes["locked"]= _("Locked");
49     $this->modes["memcheck"]= _("Memory test");
50     $this->modes["sysinfo"]= _("System analysis");
52     $this->orig_dn =    $this->dn;
54     /*************** 
55       Get mac addresses from member objects  
56      ***************/
58     /* We're only interested in the terminal members here, evaluate
59        these... */
60     if(isset($this->attrs['member'])){
61       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
62         $member= $this->attrs['member'][$i];
63         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
64         if (preg_match("/success/i", $ldap->error)){
65           $attrs = $ldap->fetch();
66           if (in_array("gotoTerminal", $attrs['objectClass']) ||
67               in_array("gotoWorkstation", $attrs['objectClass'])){
68             if (isset($attrs['macAddress'])){
69               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
70             } else {
71               $this->members[$attrs['cn'][0]]= "";
72             }
73           }
74         }
75       }
76     }
78     /*************** 
79       Perpare NTP settings 
80      ***************/
82     /* Create used ntp server array */
83     $this->gotoNtpServer= array();
84     if(isset($this->attrs['gotoNtpServer'])){
85       $this->inheritTimeServer = false;
86       unset($this->attrs['gotoNtpServer']['count']);
87       foreach($this->attrs['gotoNtpServer'] as $server){
88         $this->gotoNtpServer[$server] = $server;
89       }
90     }
92     /* Set inherit checkbox state */
93     if(in_array("default",$this->gotoNtpServer)){
94       $this->inheritTimeServer = true;
95       $this->gotoNtpServer=array();
96     }
98     /* Create available ntp options */
99     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
100     foreach($this->gotoNtpServers as $key => $server){
101       if($server == "default"){
102         unset($this->gotoNtpServers[$key]);
103       }
104     }
107   }
109   function check()
110   {
111      /* Call common method to give check the hook */
112     $message= plugin::check();
114     if (chkacl($this->acl, "create") != ""){
115       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
116     }
118     /* Check for valid ntpServer selection */
119     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
120       $message[]= _("There must be at least one NTP server selected.");
121     }
122     return($message);
123   }
125   function remove_from_parent()
126   {
127     /* Workstation startup is using gotoWorkstationTemplate too,
128         if we remove this oc all other not manged attributes will cause errors */
129     if(isset($this->attrs['gotoKernelParameters'])){
130       $this->objectclasses = array();
131     }
133     /* Remove acc */
134     plugin::remove_from_parent();
135     $ldap = $this->config->get_ldap_link();
136     $ldap->cd($this->orig_dn);
137     $ldap->modify($this->attrs);
138     $this->handle_post_events("remove");
139   }
142   function update_term_member_FAIstate($act)
143   {
144     /* Get required informations */
145     $og    = $this->parent->by_object['ogroup'];  
146     $allobs= $og->objcache;
148     /* Get correct value for FAIstate */
149     $action= $this->mapActions[$act];
151     /* Get ldap connection */
152     $ldap= $this->config->get_ldap_link();
153     $ldap->cd ($this->config->current['BASE']);
155     /* Foreach member of mthis ogroup  ... */
156     foreach($og->member as $key ){
157   
158       /* check objectClasses and create attributes array */
159       $attrs = array("FAIstate" => $action);  
160       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
161         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
162       }
163       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
164         $attrs['objectClass'][] = "FAIobject";
165       }
166       if($attrs['FAIstate'] == ""){
167         $attrs['FAIstate'] = array();
168       }
170       /* If this objects is workstation,terminal or server upodate FAIstate */ 
171       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
172         $ldap->cd ($key);
173         $ldap->modify($attrs);
174         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
175         
176         if(!preg_match("/success/i",$ldap->get_error())) {
177           gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
178         }else{
179           gosa_log("OK.  Updating FAIstate to '".$action."' : ".$key);
180         }
181       }
182     }
183   }
186   function execute()
187   {
188     /* Call parent execute */
189     plugin::execute();
192     /*************** 
193       Handle requested action
194      ***************/
196     /* Watch for events */
197     if (isset($_POST['action'])){
198       $macaddresses="";
199       $names="";
200       foreach ($this->members as $cn => $macAddress){
201         $macaddresses.= "$macAddress ";
202         $names.= "$cn ";
203       }
205       if (isset($_POST['action']) && $this->acl == "#all#"){
207         /* Update members fai state */
208         $this->update_term_member_FAIstate(trim($_POST['saction']));
210         $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
211         if ($cmd == ""){
212           print_red(_("No ACTIONCMD definition found in your gosa.conf"));
213         } else {
214           exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
215           if ($retval != 0){
216             print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
217           } else {
218             $this->didAction= TRUE;
219           }
220         }
221       }
222     }
225     /*************** 
226       Add remove NTP server
227      ***************/
229     /* Add new ntp Server to our list */
230     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl == "#all#"){
231       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
232     }
234     /* Delete selected NtpServer for list of used servers  */
235     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl == "#all#"){
236       foreach($_POST['gotoNtpServerSelected'] as $name){
237         unset($this->gotoNtpServer[$name]);
238       }
239     }
242     /*************** 
243       Prepare smarty 
244      ***************/
246     /* Set government mode */
247     $smarty= get_smarty();
249     foreach($this->attributes as $attr){
250       $smarty->assign($attr,      $this->$attr);
251       $smarty->assign($attr."ACL",chkacl($this->acl,$this->$attr));
252     }
254     /* Variables */
255     foreach(array("gotoMode","gotoNtpServer") as $val){
256       $smarty->assign($val."_select", $this->$val);
257       $smarty->assign($val."ACL", chkacl($this->acl, $val));
258     }
260     /* Check if workstation is online */
261     $smarty->assign("actions", array(
262           "halt" =>             _("Switch off"), 
263           "reboot" =>           _("Reboot"),
264           "instant_update" =>   _("Instant update"),
265           "update" =>           _("Scheduled update"),
266           "reinstall" =>        _("Reinstall"),
267           "rescan" =>           _("Rescan hardware")));
269     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
270     $smarty->assign("modes", $this->modes);
271     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
272     $smarty->assign("gotoNtpServers",$this->gotoNtpServers); 
273     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
274     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
276     /* Show main page */
277     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
278   }
280   function save_object()
281   {
282     plugin::save_object();  
283     /* Set inherit mode */
284     if(isset($_POST['workgeneric_posted'])){
285       if(isset($_POST["inheritTimeServer"])){
286         $this->inheritTimeServer = true;
287       }else{
288         $this->inheritTimeServer = false;
289       }
290     }
291   }
293   /* Save to LDAP */
294   function save()
295   {
296     plugin::save();
298     /***************
299       Prepare special vars 
300      ***************/
302     /* Unset some special vars ... */
303     foreach (array("gotoSyslogServer") as $val){
304       if ($this->attrs[$val] == "default"){
305         $this->attrs[$val]= array();
306       }
307     }
309     /* Update ntp server settings */
310     if($this->inheritTimeServer){
311       $this->attrs['gotoNtpServer'] = "default";
312     }else{
313       /* Set ntpServers */
314       $this->attrs['gotoNtpServer'] = array();
315       foreach($this->gotoNtpServer as $server){
316         $this->attrs['gotoNtpServer'][] = $server;
317       }
318     }
321     /***************
322       Write to ldap 
323      ***************/
325     /* Write back to ldap */
326     $ldap= $this->config->get_ldap_link();
327     $ldap->cd($this->dn);
328     $this->cleanup();
329     $ldap->modify ($this->attrs);
331     if(!$this->didAction){
332       $this->handle_post_events("modify");
333     }
334     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
335   }
338 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
339 ?>