Code

Renoved debug tag
[gosa.git] / plugins / addons / gotomasses / class_gotomasses.inc
1 <?php
3 class gotomasses extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "System deployment";
7   var $plDescription  = "This does something";
9   /* attribute list for save action */
10   var $attributes= array();
11   var $objectclasses= array();
13   /* Source file that contains the gotomasses data */
14   var $data_file = "Undefined"; #Set in constructor 
16   /* Queue tasks */
17   var $tasks = array();
18   var $current =false;
19   var $dialog = FALSE;
20   var $ids_to_remove = array();
21   var $divlist = NULL;
23   function gotomasses(&$config, $dn= NULL)
24   {
25     /* Include config object */
26     $this->config= &$config;
28     /* Define source file */
29     $this->data_file = CONFIG_DIR."/gotomasses_machines";
30     $file = $this->config->search("gotomasses", "STORAGE_FILE",array('menu'));
31     if(!empty($file)){
32       $this->data_file = $file;
33     }
34     $this->divlist = new divListMasses($this->config,$this);
35     $this->load_gotomasses_data();
36   }
39   function execute()
40   {
41     $smarty = get_smarty();
42     /************
43      * Handle posts 
44      ************/
46     $s_entry = $s_action = "";
47     $arr = array( "/^edit_task_/"=>"edit","/^remove_task_/"=>"remove",
48                   "/^new_task_/"=>"new_task","/^remove_multiple_task_/" => "remove_multiple");
49     foreach($arr as $regex => $action){
50       foreach($_POST as $name => $value){
51         if(preg_match($regex,$name)){
52           $s_action = $action;
53           $s_entry  = preg_replace($regex,"",$name);
54           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
55         }
56       }
57     }
59     /* Edit posted from list link */
60     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
61       $s_action = "edit";
62       $s_entry = $_GET['id'];
63     }
65     /************
66      * List posts 
67      ************/
69     /* Remove multiple */
70     if($s_action == "remove_multiple"){
71       if(!$this->acl_is_removeable()){
72         print_red(_("You are not allowed to remove a task."));
73       }else{
74         $this->ids_to_remove = $this->list_get_selected_items();
75         $tmp = "";
76         foreach($this->ids_to_remove as $key => $id){
77           if(isset($this->tasks[$id])){
78             $task = $this->tasks[$id];
79             $tmp.= "\n".$this->target_to_string($task);
80           }else{
81             unset($this->ids_to_remove[$key]);
82           }
83         }
84         $smarty->assign("multiple", TRUE); 
85         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
86         $this->current = $s_entry;
87         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
88       }
89     }
91     /* Remove specified tasks */
92     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
93       foreach($this->ids_to_remove as $id){
94         if(isset($this->tasks[$id])){
95           unset($this->tasks[$id]);
96         }
97       }
98       $this->save();
99     }
101     /* Remove entry from list */
102     if($s_action == "remove" && isset($this->tasks[$s_entry])){
103       if(!$this->acl_is_removeable()){
104         print_red(_("You are not allowed to remove a task."));
105       }else{
106         $entry = $this->tasks[$s_entry];
107         $this->current = $s_entry;
108         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),
109                                     "<pre>".$this->target_to_string($entry)."</pre>"));
110         $smarty->assign("multiple", FALSE); 
111         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
112       }
113     }
115     /* Remove entry, remove is confirmed */
116     if($this->current != -1 && isset($_POST['delete_confirm'])){
117       unset($this->tasks[$this->current]);
118       $this->current = -1;
119       $this->save();
120     }
122     /* Remove aborted */
123     if(isset($_POST['delete_cancel'])){
124       $this->ids_to_remove = array();;
125     }
127     /* Edit selected entry */
128     if($s_action == "edit" && isset($this->tasks[$s_entry])){
129       $entry = $this->tasks[$s_entry];
130       $this->dialog = new goto_task($this->config,$this,$entry);
131       $this->current = $s_entry;
132     }
134     /* New entry */
135     if($s_action== "new_task" && $this->acl_is_createable()){
136       $this->dialog = new goto_task($this->config,$this);
137       $this->current = -1;
138     }
139   
140     /* Close dialog */
141     if(isset($_POST['close_goto_task'])){
142       $this->dialog = FALSE;
143       $this->current = -1;
144     }
146     /* Close dialog */
147     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
148       $this->dialog->save_object();
149       $msgs = $this->dialog->check();
150       if(count($msgs)){
151         foreach($msgs as $msg){
152           print_red($msg);  
153         }
154       }else{
155         if(isset($this->tasks[$this->current]) && $this->current != -1){
156           $this->tasks[$this->current] = $this->dialog->save();
157         }else{
158           $this->tasks[] = $this->dialog->save();
159         }
160         if(!isset($_POST['apply_goto_task'])){
161           $this->dialog = FALSE;
162           $this->current = -1;
163         }
164         $this->save();
165       }
166     }
168     /* Display dialogs if currently opened */
169     if(is_object($this->dialog)){
170       $this->dialog->save_object();
171       return($this->dialog->execute());
172     }
175     /************
176      * Handle Divlist 
177      ************/
179     $this->divlist->execute();
180     $this->divlist->SetEntries($this->tasks); 
181     return($this->divlist->Draw());
182   }
184   
185   function target_to_string($data)
186   {
187     $ret = "";
188     if($data['Action'] == "initial_install"){
189       foreach($data['Initial_Target'] as $target){
190         $ret .= $target['MAC'].", ";
191       } 
192     }else{
193       foreach($data['Target'] as $target){
194         $ret .= $target.", ";
195       } 
196     }
197     return(preg_replace("/, $/","",$ret));
198   }
200   
201   function time_to_string($data)
202   {
203     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
204   }
206   
207   function action_to_string($data)
208   {
209     $tmp = $this->get_actions();
210     if(isset($tmp[$data['Action']])){
211       return($tmp[$data['Action']]);
212     }else{
213       return(_("Unknown"));
214     }
215   }
217   
218   function load_gotomasses_data()
219   {
220     $ui = get_userinfo();
222     /* Check file existence */
223     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
224       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
225       return(FALSE);
226     }
228     /* check if file is readable */
229     $fp = @fopen($this->data_file,"r");
230     if(!$fp){
231       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
232       return(FALSE);
233     }
235     /* Get file contents */
236     $data ="";
237     while(!feof($fp)){
238       $data.= fread($fp,512);
239     }
241     /* Get lines from file */
242     $this->tasks  = array(); 
243     $comment      = "";
244     $rows         = split("\n",$data);
246     /* Walk trough rows and parse data */
247     foreach($rows as $row){
249       /* Skip empty lines */
250       $row = trim($row);
251       if(empty($row)){
252         continue;
253       }
255       /* Get comment, if available */     
256       if(preg_match("/^#/",$row)){
257         $comment = preg_replace("/^#/","",$row);
258         continue;
259       }
261       /* Split row into minutes/ hours ...*/ 
262       $row    = preg_replace('/[\t ]/umi'," ",$row);
263       $row    = preg_replace('/  */umi'," ",$row);
264       $parts  = split(" ",$row);
266       if(count($parts) != 10){
267         print_red(_("Entry broken, skipped."));
268       }else{
270         $entry = array();
271         $entry['Minute']  = $parts[0];  
272         $entry['Hour']    = $parts[1];  
273         $entry['Day']     = $parts[2];  
274         $entry['Month']   = $parts[3];  
275         $entry['Weekday'] = $parts[4];  
276         $entry['Action']  = $parts[5];  
277         $entry['OGroup']  = $parts[6];  
278         $entry['Zone']    = $parts[7];  
279         $entry['Section'] = $parts[8];  
280         if($entry['Action'] == "initial_install"){
281           $tmp2 = split(";",$parts[9]);
282           foreach($tmp2 as $target){
283             $tmp = split(",",$target);
284             $entry['Initial_Target'][]  = array(
285                           "MAC"     => $tmp[0],
286                           "IP"      => $tmp[1],
287                           "NAME"    => $tmp[2]);
288           }
289           $entry['Target']  = array();
290         }else{
291           $entry['Initial_Target']  = array();
292           $entry['Target']  = split(";",$parts[7]);
293         }
294         $entry['Comment'] = $comment;  
295         $this->tasks []   = $entry;
296       }
297     } 
298   }
301   function save_gotomasses_data()
302   {
303     $str = "#GOsa generated file, please just modify if you really know what you do.";
304     foreach($this->tasks as $task){
305       $str .= "\n#".trim($task['Comment']);
306       $str .= "\n";
307       if($task['Action'] == "initial_install"){
308         $str .= "*     *     *     *     *     ";
309       }else{
310         $str .= str_pad($task['Minute'] ,5," ")." ";
311         $str .= str_pad($task['Hour']   ,5," ")." ";
312         $str .= str_pad($task['Day']    ,5," ")." ";
313         $str .= str_pad($task['Month']  ,5," ")." ";
314         $str .= str_pad($task['Weekday'],5," ")." ";
315       }
316       $str .= str_pad($task['Action'] ,5," ")." ";
317       $str .= str_pad($task['OGroup'] ,5," ")." ";
318       $str .= str_pad($task['Zone']   ,5," ")." ";
319       $str .= str_pad($task['Section'],5," ")." ";
320       if($task['Action'] == "initial_install"){
321         foreach($task['Initial_Target'] as $target){
322           $str .= trim($target['MAC']).",".trim($target['IP']).",".trim($target['NAME']).";";
323         }
324       }else{
325         foreach($task['Target'] as $target){
326           $str .= $target.";";
327         }
328       }
329       $str = preg_replace("/;$/","",$str);
330     }
332     /* check file existence*/
333     $ui = get_userinfo();
334     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
335       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
336       return(FALSE);
337     }
339     /* check permissions */
340     $fp = @fopen($this->data_file,"w");
341     if(!$fp){
342       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
343       return(FALSE);
344     }
346     /* Write contents */
347     $str .= "\n";
348     fwrite($fp,$str);
349     fclose($fp);
350   }
353   function save_object()
354   {
355     $this->divlist->save_object();
356   }
359   /* Return list of object groups */
360   function get_object_groups()
361   {
362     $ret = array();
363     $ldap = $this->config->get_ldap_link();
364     $ldap->cd($this->config->current['BASE']);
365     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
366     while($attrs = $ldap->fetch()){
367       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
368     }
369     return($ret); 
370   }
372   
373   function save()
374   {
375     $this->save_gotomasses_data();
376   }
379   function list_get_selected_items()
380   {
381     $ids = array();
382     foreach($_POST as $name => $value){
383       if(preg_match("/^item_selected_[0-9]*$/",$name)){
384         $id   = preg_replace("/^item_selected_/","",$name);
385         $ids[$id] = $id;
386       }
387     }
388     return($ids);
389   }
392   function get_actions()
393   {
394     /* Prepare list of available actions */
395     $actions = array(       "reboot"          => _("Reboot"),
396                             "localboot"       => _("Localboot"),
397                             "halt"            => _("Halt system"),
398                             "initial_install" => _("Initial installation"),
399                             "update"          => _("Update"),
400                             "reinstall"       => _("(Re)Install"),
401                             "rescan"          => _("Rescan"),
402                             "wake"            => _("Wake"),
403                             "memcheck"        => _("Memory check"));
404     return($actions);
405   }
408   function plInfo()
409   {
410     return (array(
411         "plShortName"   => _("System mass deployment"),
412         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
413         "plSelfModify"  => FALSE,
414         "plDepends"     => array(),
415         "plPriority"    => 0,
416         "plSection"     => array("addon"),
417         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
418         "plProvidedAcls" => array("Comment"   => _("Description"), 
419                                   "Action"    => _("Action"),
420                                   "Day"       => _("Day"),
421                                   "Minute"    => _("Minute"),
422                                   "Hour"      => _("Hour"),
423                                   "Month"     => _("Month"),
424                                   "Weekday"   => _("Week day"),
425                                   "Target"    => _("Target"))
426         ));
427   }
429 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
430 ?>