Code

4e3338458434a6c5a7169d9078bafa2a373dd65f
[gosa.git] / gosa-plugins / goto / admin / ogroups / goto / class_termgroup.inc
1 <?php
3 class termgroup extends plugin
4 {
5   /* attribute list for save action */
6   var $members= array();
8   var $gotoMode           = "locked";
9   var $gotoSyslogServer   = "";
10   var $gotoNtpServer      = array();
11   var $gotoNtpServers     = array();
12   var $modes              = array();
13   var $inheritTimeServer  = true;
14   var $is_account                     = true; 
15   var $orig_dn            = "";
16   var $didAction          = FALSE;
17   var $mapActions   = array("reboot"          => "localboot",
18                             "instant_update"  => "softupdate",
19                             "update"          => "sceduledupdate",
20                             "reinstall"       => "install",
21                             "rescan"          => "",
22                             "wake"            => "",
23                             "memcheck"        => "memcheck",
24                             "sysinfo"         => "sysinfo");
26   var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer");
27   var $objectclasses  = array("gotoWorkstationTemplate");
28   var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
29   var $view_logged    = FALSE;
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     $this->modes["active"]= _("Activated");
44     $this->modes["locked"]= _("Locked");
45 //    $this->modes["memcheck"]= _("Memory test");
46 //    $this->modes["sysinfo"]= _("System analysis");
48     $this->orig_dn =    $this->dn;
50     /*************** 
51       Get mac addresses from member objects  
52      ***************/
54     /* We're only interested in the terminal members here, evaluate
55        these... */
56     if(isset($this->attrs['member'])){
57       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
58         $member= $this->attrs['member'][$i];
59         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
60         if (preg_match("/success/i", $ldap->error)){
61           $attrs = $ldap->fetch();
62           if (in_array("gotoTerminal", $attrs['objectClass']) ||
63               in_array("gotoWorkstation", $attrs['objectClass'])){
64             if (isset($attrs['macAddress'])){
65               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
66             } else {
67               $this->members[$attrs['cn'][0]]= "";
68             }
69           }
70         }
71       }
72     }
74     /*************** 
75       Perpare NTP settings 
76      ***************/
78     /* Create used ntp server array */
79     $this->gotoNtpServer= array();
80     if(isset($this->attrs['gotoNtpServer'])){
81       $this->inheritTimeServer = false;
82       unset($this->attrs['gotoNtpServer']['count']);
83       foreach($this->attrs['gotoNtpServer'] as $server){
84         $this->gotoNtpServer[$server] = $server;
85       }
86     }
88     /* Set inherit checkbox state */
89     if(in_array("default",$this->gotoNtpServer)){
90       $this->inheritTimeServer = true;
91       $this->gotoNtpServer=array();
92     }
94     /* Create available ntp options */
95     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
96     foreach($this->gotoNtpServers as $key => $server){
97       if($server == "default"){
98         unset($this->gotoNtpServers[$key]);
99       }
100     }
101   }
103   function check()
104   {
105      /* Call common method to give check the hook */
106     $message= plugin::check();
108     if (!$this->acl_is_createable() && $this->dn == "new"){
109       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
110     }
112     /* Check for valid ntpServer selection */
113     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
114       $message[]= _("There must be at least one NTP server selected.");
115     }
116     return($message);
117   }
119   function remove_from_parent()
120   {
121     /* Workstation startup is using gotoWorkstationTemplate too,
122         if we remove this oc all other not manged attributes will cause errors */
123     if(isset($this->attrs['gotoKernelParameters'])){
124       $this->objectclasses = array();
125     }
127     /* Remove acc */
128     plugin::remove_from_parent();
129     $ldap = $this->config->get_ldap_link();
130     $ldap->cd($this->orig_dn);
131     $ldap->modify($this->attrs);
132     $this->handle_post_events("remove");
133     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
134   }
137   function update_term_member_FAIstate($act)
138   {
139     /* Get required informations */
140     $og     = $this->parent->by_object['ogroup'];
141     $allobs = $og->objcache;
143     /* Get correct value for FAIstate */
144     $action = $this->mapActions[$act];
146     /* Get ldap connection */
147     $ldap = $this->config->get_ldap_link();
148     $ldap->cd ($this->config->current['BASE']);
150     /* Foreach member of mthis ogroup  ... */
151     foreach($og->member  as $key ){
152   
153       /* check objectClasses and create attributes array */
154       $attrs = array("FAIstate"=>$action);  
155       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
156         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
157       }
158       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
159         $attrs['objectClass'][] = "FAIobject";
160       }
161       if($attrs['FAIstate'] == ""){
162         $attrs['FAIstate'] = array();
163       }
165       /* If this objects is workstation,terminal or server upodate FAIstate */ 
166       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
167         $ldap->cd ($key);
168         $ldap->modify($attrs);
169         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
170       }
171     }
172   }
174   function execute()
175   {
176     /* Call parent execute */
177     plugin::execute();
179     if(!$this->view_logged){
180       $this->view_logged = TRUE;
181       new log("view","ogroups/".get_class($this),$this->dn);
182     }
184     /*************** 
185       Handle requested action
186      ***************/
188     /* Watch for events */
189     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
190       $macaddresses= array();
191       foreach ($this->members as $cn => $macAddress){
192         $macaddresses[]= $macAddress;
193       }
195       print_red("trigger action is missing");
196       #gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], "255.255.255.255", $macaddresses);
198       /* Update members fai state */
199       $this->update_term_member_FAIstate(trim($_POST['saction']));
201       $this->didAction= TRUE;
203       /* Get dns from member objects. Create ldap object  */
204       $member = $this->parent->by_object['ogroup']->member;
205       $ldap = $this->config->get_ldap_link();
206       $ldap->cd($this->config->current['BASE']);
208       /* walk trough members and add FAIstate */ 
209       if ($_POST['saction'] != "wake"){
210         foreach($member as $dn =>  $object){
212               /* Get object */
213               $ldap->cat($dn,array("objectClass"));
214               $res                = $ldap->fetch();
215               $attrs              = array();
217               /* Add FAI state */
218               $attrs['FAIstate']  = "";
219               if(isset($this->mapActions[$_POST['saction']])){
220                       $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
221               }
223               /* Fix objectClass index */
224               for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
225                       $attrs['objectClass'][] = $res['objectClass'][$i];
226               }
228               /* Check if we must add the objectClass */
229               if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
230                       $attrs['objectClass'][] = "FAIobject";
231               }
233               if($attrs['FAIstate'] == ""){
234                       $attrs['FAIstate'] = array();
235               }
237               $ldap->cd($dn);
238               $ldap->modify($attrs);
239               show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
240         }
241       }
242     }
245     /*************** 
246       Add remove NTP server
247      ***************/
249     /* Add new ntp Server to our list */
250     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
251       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
252     }
254     /* Delete selected NtpServer for list of used servers  */
255     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
256       foreach($_POST['gotoNtpServerSelected'] as $name){
257         unset($this->gotoNtpServer[$name]);
258       }
259     }
262     /*************** 
263       Prepare smarty 
264      ***************/
266     /* Set government mode */
267     $smarty= get_smarty();
269     $tmp = $this->plInfo();
270     foreach($tmp['plProvidedAcls'] as $name => $translated) {
271       $smarty->assign($name."ACL",$this->getacl($name));
272     }
274     foreach($this->attributes as $attr){
275       $smarty->assign($attr,      $this->$attr);
276     }
278     /* Variables */
279     foreach(array("gotoMode","gotoNtpServer") as $val){
280       $smarty->assign($val."_select", $this->$val);
281     }
283     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
284           "instant_update" => _("Instant update"),
285           "update" => _("Scheduled update"),
286           "wake" => _("Wake"),
287           "reinstall" => _("Reinstall"),
288           "rescan" => _("Rescan hardware"),
289           "memcheck" => _("Memory test"),
290           "sysinfo"  => _("System analysis")));
292     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
293     $smarty->assign("modes", $this->modes);
295     $tmp = array();
296     foreach($this->gotoNtpServers as $server){
297       if(!in_array($server,$this->gotoNtpServer)){
298         $tmp[$server] = $server;
299       }
300     }
302     $smarty->assign("gotoNtpServers",$tmp); 
303     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
304     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
306     /* Show main page */
307     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
308   }
310   function save_object()
311   {
312     plugin::save_object();  
313     /* Set inherit mode */
314     if(isset($_POST['workgeneric_posted'])){
315       if(isset($_POST["inheritTimeServer"])){
316         $this->inheritTimeServer = true;
317       }else{
318         $this->inheritTimeServer = false;
319       }
320     }
321   }
323   /* Save to LDAP */
324   function save()
325   {
326     plugin::save();
328     /***************
329       Prepare special vars 
330      ***************/
332     /* Unset some special vars ... */
333     foreach (array("gotoSyslogServer") as $val){
334       if ($this->attrs[$val] == "default"){
335         $this->attrs[$val]= array();
336       }
337     }
339     /* Update ntp server settings */
340     if($this->inheritTimeServer){
341       $this->attrs['gotoNtpServer'] = "default";
342     }else{
343       /* Set ntpServers */
344       $this->attrs['gotoNtpServer'] = array();
345       foreach($this->gotoNtpServer as $server){
346         $this->attrs['gotoNtpServer'][] = $server;
347       }
348     }
351     /***************
352       Write to ldap 
353      ***************/
355     /* Write back to ldap */
356     $ldap= $this->config->get_ldap_link();
357     $ldap->cd($this->dn);
358     $this->cleanup();
359     $ldap->modify ($this->attrs);
361     if($this->initially_was_account){
362       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
363     }else{
364       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
365     }
367     if(!$this->didAction){
368       $this->handle_post_events("modify");
369     }
370     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
372   }
373   
374   static function plInfo()
375   {
376     return (array(
377           "plShortName"   => _("System"),
378           "plDescription" => _("System group"),
379           "plSelfModify"  => FALSE,
380           "plDepends"     => array(),
381           "plPriority"    => 5,
382           "plSection"     => array("administration"),
383           "plCategory"    => array("ogroups"),
384           "plProvidedAcls"=> array(
385             "gotoMode"          => _("Mode"),     
386             "gotoSyslogServer"  => _("Syslog server"), 
387             "FAIstate"          => _("Action flag"), 
388             "gotoNtpServer"     => _("Ntp server"))
389           ));
390   }
392   function PrepareForCopyPaste($source)
393   {
394     /* Create used ntp server array */
395     $this->gotoNtpServer= array();
397     if(isset($source['gotoNtpServer'])){
398       $this->inheritTimeServer = false;
399       unset($source['gotoNtpServer']['count']);
400       foreach($source['gotoNtpServer'] as $server){
401         $this->gotoNtpServer[$server] = $server;
402       }
403     }
405     /* Set inherit checkbox state */
406     if(in_array("default",$this->gotoNtpServer)){
407       $this->inheritTimeServer = true;
408       $this->gotoNtpServer=array();
409     }
411     /* Create available ntp options */
412     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
413     foreach($this->gotoNtpServers as $key => $server){
414       if($server == "default"){
415         unset($this->gotoNtpServers[$key]);
416       }
417     }
418   }
421 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
422 ?>