Code

More speed optimizations
[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"          => "sceduledupdate",
25                             "reinstall"       => "install",
26                             "rescan"          => "",
27                             "memcheck"        => "memcheck",
28                             "sysinfo"         => "sysinfo");
31   function termgroup ($config, $dn= NULL, $parent= NULL)
32   {
33     /***************
34       Some  initialisations
35      ***************/
37     plugin::plugin($config, $dn, $parent);
39     $ldap= $config->get_ldap_link();
41     $this->is_account = true;
42     $ui               = get_userinfo();
43     $acl              = get_permissions ($this->dn, $ui->subtreeACL);
44     $this->acl        = get_module_permission($acl, "group", $this->dn);
46     $this->modes["active"]= _("Activated");
47     $this->modes["locked"]= _("Locked");
48     $this->modes["memcheck"]= _("Memory test");
49     $this->modes["sysinfo"]= _("System analysis");
51     $this->orig_dn =    $this->dn;
53     /*************** 
54       Get mac addresses from member objects  
55      ***************/
57     /* We're only interested in the terminal members here, evaluate
58        these... */
59     if(isset($this->attrs['member'])){
60       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
61         $member= $this->attrs['member'][$i];
62         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
63         if (preg_match("/success/i", $ldap->error)){
64           $attrs = $ldap->fetch();
65           if (in_array("gotoTerminal", $attrs['objectClass']) ||
66               in_array("gotoWorkstation", $attrs['objectClass'])){
67             if (isset($attrs['macAddress'])){
68               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
69             } else {
70               $this->members[$attrs['cn'][0]]= "";
71             }
72           }
73         }
74       }
75     }
77     /*************** 
78       Perpare NTP settings 
79      ***************/
81     /* Create used ntp server array */
82     $this->gotoNtpServer= array();
83     if(isset($this->attrs['gotoNtpServer'])){
84       $this->inheritTimeServer = false;
85       unset($this->attrs['gotoNtpServer']['count']);
86       foreach($this->attrs['gotoNtpServer'] as $server){
87         $this->gotoNtpServer[$server] = $server;
88       }
89     }
91     /* Set inherit checkbox state */
92     if(in_array("default",$this->gotoNtpServer)){
93       $this->inheritTimeServer = true;
94       $this->gotoNtpServer=array();
95     }
97     /* Create available ntp options */
98     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
99     foreach($this->gotoNtpServers as $key => $server){
100       if($server == "default"){
101         unset($this->gotoNtpServers[$key]);
102       }
103     }
106   }
108   function check()
109   {
110      /* Call common method to give check the hook */
111     $message= plugin::check();
113     if (chkacl($this->acl, "create") != ""){
114       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
115     }
117     /* Check for valid ntpServer selection */
118     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
119       $message[]= _("There must be at least one NTP server selected.");
120     }
121     return($message);
122   }
124   function remove_from_parent()
125   {
126     /* Workstation startup is using gotoWorkstationTemplate too,
127         if we remove this oc all other not manged attributes will cause errors */
128     if(isset($this->attrs['gotoKernelParameters'])){
129       $this->objectclasses = array();
130     }
132     /* Remove acc */
133     plugin::remove_from_parent();
134     $ldap = $this->config->get_ldap_link();
135     $ldap->cd($this->orig_dn);
136     $ldap->modify($this->attrs);
137     $this->handle_post_events("remove");
138   }
141   function update_term_member_FAIstate($act)
142   {
143     /* Get required informations */
144     $og    = $this->parent->by_object['ogroup'];
145     $allobs= $og->allobjects;
147     /* Get correct value for FAIstate */
148     $action= $this->mapActions[$act];
150     /* Get ldap connection */
151     $ldap= $this->config->get_ldap_link();
152     $ldap->cd ($this->config->current['BASE']);
154     /* Foreach member of mthis ogroup  ... */
155     foreach($og->member as $key ){
156   
157       /* check objectClasses and create attributes array */
158       $attrs = array("FAIstate" => $action);  
159       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
160         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
161       }
162       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
163         $attrs['objectClass'][] = "FAIobject";
164       }
165       if($attrs['FAIstate'] == ""){
166         $attrs['FAIstate'] = array();
167       }
169       /* If this objects is workstation,terminal or server upodate FAIstate */ 
170       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
171         $ldap->cd ($key);
172         $ldap->modify($attrs);
173         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
174         
175         if(!preg_match("/success/i",$ldap->get_error())) {
176           gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
177         }else{
178           gosa_log("OK.  Updating FAIstate to '".$action."' : ".$key);
179         }
180       }
181     }
182   }
185   function execute()
186   {
187     /* Call parent execute */
188     plugin::execute();
191     /*************** 
192       Handle requested action
193      ***************/
195     /* Watch for events */
196     if (isset($_POST['action'])){
197       $macaddresses="";
198       $names="";
199       foreach ($this->members as $cn => $macAddress){
200         $macaddresses.= "$macAddress ";
201         $names.= "$cn ";
202       }
204       if (isset($_POST['action'])){
206         /* Update members fai state */
207         $this->update_term_member_FAIstate(trim($_POST['saction']));
209         $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
210         if ($cmd == ""){
211           print_red(_("No ACTIONCMD definition found in your gosa.conf"));
212         } else {
213           exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
214           if ($retval != 0){
215             print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
216           } else {
217             $this->didAction= TRUE;
218           }
219         }
220       }
221     }
224     /*************** 
225       Add remove NTP server
226      ***************/
228     /* Add new ntp Server to our list */
229     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
230       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
231     }
233     /* Delete selected NtpServer for list of used servers  */
234     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
235       foreach($_POST['gotoNtpServerSelected'] as $name){
236         unset($this->gotoNtpServer[$name]);
237       }
238     }
241     /*************** 
242       Prepare smarty 
243      ***************/
245     /* Set government mode */
246     $smarty= get_smarty();
248     foreach($this->attributes as $attr){
249       $smarty->assign($attr,      $this->$attr);
250       $smarty->assign($attr."ACL",chkacl($this->acl,$this->$attr));
251     }
253     /* Variables */
254     foreach(array("gotoMode","gotoNtpServer") as $val){
255       $smarty->assign($val."_select", $this->$val);
256       $smarty->assign($val."ACL", chkacl($this->acl, $val));
257     }
259     /* Check if workstation is online */
260     $smarty->assign("actions", array(
261           "halt" =>             _("Switch off"), 
262           "reboot" =>           _("Reboot"),
263           "instant_update" =>   _("Instant update"),
264           "update" =>           _("Scheduled update"),
265           "reinstall" =>        _("Reinstall"),
266           "rescan" =>           _("Rescan hardware")));
268     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
269     $smarty->assign("modes", $this->modes);
270     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
271     $smarty->assign("gotoNtpServers",$this->gotoNtpServers); 
272     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
273     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
275     /* Show main page */
276     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
277   }
279   function save_object()
280   {
281     plugin::save_object();  
282     /* Set inherit mode */
283     if(isset($_POST['workgeneric_posted'])){
284       if(isset($_POST["inheritTimeServer"])){
285         $this->inheritTimeServer = true;
286       }else{
287         $this->inheritTimeServer = false;
288       }
289     }
290   }
292   /* Save to LDAP */
293   function save()
294   {
295     plugin::save();
297     /***************
298       Prepare special vars 
299      ***************/
301     /* Unset some special vars ... */
302     foreach (array("gotoSyslogServer") as $val){
303       if ($this->attrs[$val] == "default"){
304         $this->attrs[$val]= array();
305       }
306     }
308     /* Update ntp server settings */
309     if($this->inheritTimeServer){
310       $this->attrs['gotoNtpServer'] = "default";
311     }else{
312       /* Set ntpServers */
313       $this->attrs['gotoNtpServer'] = array();
314       foreach($this->gotoNtpServer as $server){
315         $this->attrs['gotoNtpServer'][] = $server;
316       }
317     }
320     /***************
321       Write to ldap 
322      ***************/
324     /* Write back to ldap */
325     $ldap= $this->config->get_ldap_link();
326     $ldap->cd($this->dn);
327     $this->cleanup();
328     $ldap->modify ($this->attrs);
330     if(!$this->didAction){
331       $this->handle_post_events("modify");
332     }
333     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
334   }
337 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
338 ?>