Code

51822cf1761259ffbeb0d410e48cfc5b5c52cd8d
[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();
22   function gotomasses($config, $dn= NULL)
23   {
24     /* Define source file */
25     $this->data_file = CONFIG_DIR."/gotomasses_machines";
26   
27     /* Include config object */
28     $this->config= $config;
29     $this->load_gotomasses_data();
30   }
33   function execute()
34   {
35     $smarty = get_smarty();
36     /************
37      * Handle posts 
38      ************/
40     $s_entry = $s_action = "";
41     $arr = array( "/^edit_task_/"=>"edit","/^remove_task_/"=>"remove",
42                   "/^new_task_/"=>"new_task","/^remove_multiple_task_/" => "remove_multiple");
43     foreach($arr as $regex => $action){
44       foreach($_POST as $name => $value){
45         if(preg_match($regex,$name)){
46           $s_action = $action;
47           $s_entry  = preg_replace($regex,"",$name);
48           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
49         }
50       }
51     }
53     /* Edit posted from list link */
54     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
55       $s_action = "edit";
56       $s_entry = $_GET['id'];
57     }
59     /************
60      * List posts 
61      ************/
63     /* Remove multiple */
64     if($s_action == "remove_multiple"){
65       if(!$this->acl_is_removeable()){
66         print_red(_("You are not allowed to remove a task."));
67       }else{
68         $this->ids_to_remove = $this->list_get_selected_items();
69         $tmp = "";
70         foreach($this->ids_to_remove as $key => $id){
71           if(isset($this->tasks[$id])){
72             $task = $this->tasks[$id];
73             $tmp.= "\n".$this->target_to_string($task);
74           }else{
75             unset($this->ids_to_remove[$key]);
76           }
77         }
78         $smarty->assign("multiple", TRUE); 
79         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
80         $this->current = $s_entry;
81         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
82       }
83     }
85     /* Remove specified tasks */
86     if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
87       foreach($this->ids_to_remove as $id){
88         if(isset($this->tasks[$id])){
89           unset($this->tasks[$id]);
90         }
91       }
92       $this->save();
93     }
95     /* Remove entry from list */
96     if($s_action == "remove" && isset($this->tasks[$s_entry])){
97       if(!$this->acl_is_removeable()){
98         print_red(_("You are not allowed to remove a task."));
99       }else{
100         $entry = $this->tasks[$s_entry];
101         $this->current = $s_entry;
102         $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),
103                                     "<pre>".$this->target_to_string($entry)."</pre>"));
104         $smarty->assign("multiple", FALSE); 
105         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
106       }
107     }
109     /* Remove entry, remove is confirmed */
110     if($this->current != -1 && isset($_POST['delete_confirm'])){
111       unset($this->tasks[$this->current]);
112       $this->current = -1;
113       $this->save();
114     }
116     /* Remove aborted */
117     if(isset($_POST['delete_cancel'])){
118       $this->ids_to_remove = array();;
119     }
121     /* Edit selected entry */
122     if($s_action == "edit" && isset($this->tasks[$s_entry])){
123       $entry = $this->tasks[$s_entry];
124       $this->dialog = new goto_task($this->config,$this,$entry);
125       $this->current = $s_entry;
126     }
128     /* New entry */
129     if($s_action== "new_task" && $this->acl_is_createable()){
130       $this->dialog = new goto_task($this->config,$this);
131       $this->current = -1;
132     }
133   
134     /* Close dialog */
135     if(isset($_POST['close_goto_task'])){
136       $this->dialog = FALSE;
137       $this->current = -1;
138     }
140     /* Close dialog */
141     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
142       $this->dialog->save_object();
143       $msgs = $this->dialog->check();
144       if(count($msgs)){
145         foreach($msgs as $msg){
146           print_red($msg);  
147         }
148       }else{
149         if(isset($this->tasks[$this->current]) && $this->current != -1){
150           $this->tasks[$this->current] = $this->dialog->save();
151         }else{
152           $this->tasks[] = $this->dialog->save();
153         }
154         if(!isset($_POST['apply_goto_task'])){
155           $this->dialog = FALSE;
156           $this->current = -1;
157         }
158         $this->save();
159       }
160     }
162     /* Display dialogs if currently opened */
163     if(is_object($this->dialog)){
164       $this->dialog->save_object();
165       return($this->dialog->execute());
166     }
169     /************
170      * Handle Divlist 
171      ************/
173     $plug = $_GET['plug'];
174     $divlist = new MultiSelectWindow($this->config,"GotoMasses","gotomassses");
175     $divlist->SetSummary(_("List of system deployment tasks"));
176     $divlist->SetHeadpageMode();
177     $divlist->EnableCloseButton(FALSE);
178     $divlist->EnableSaveButton(FALSE);
179     $divlist->SetInformation(_("This menu allows you to add, remove and change the properties of system deployment tasks."));
180  
181     /* Display add button if allowed */ 
182     $header = "<div style='padding:5px'>";
183     if($this->acl_is_createable()){ 
184       $header .= "<input type='image' src='images/gotomasses_new_task.png' name='new_task' class='center'>&nbsp;";
185     }
187     /* Display add button if allowed */ 
188     if($this->acl_is_removeable()){ 
189       $header .= "<input type='image' src='images/edittrash.png' name='remove_multiple_task' class='center'>&nbsp;";
190     }
191     $header .= "</div>";
193     /* Get Permissions */ 
194     $ui = get_userinfo();
195     $acls = $this->getacl("");
196     foreach($ui->get_module_departments("gotomasses") as $dep){
197       $acls .= $ui->get_category_permissions($dep,"gotomasses");
198     }
199  
200     /* Create divlist */
201     $divlist->SetListHeader($header);
202  /* Toggle all selected / deselected */
203     $chk = "<input type='checkbox' id='select_all' name='select_all'
204                onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
206     /* set Page header */
207     $divlist->AddHeader(array("string"=> $chk,          "attach"=>"style='width:20px;'"));
209     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"));
210     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
211                                       "attach"=>"style='width:100px;'"));
212     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
213                                       "attach"=>"style='width:80px;'"));
214     $divlist->AddHeader(array("string"=>_("Action"),
215                                       "attach"=>"style='border-right:0px;width:40px;'"));
217     /* Create edit link */
218     $edit_link = "<div style='width:100%;overflow:hidden;'><nobr><a href='?plug=".$_GET['plug']."&act=edit&id=%key%'>%str%</nobr></div>";
219   
220     if(!empty($acls)){
221       foreach($this->tasks as $key => $task){
222         $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
223         if($this->acl_is_removeable()){
224           $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
225         }
226         /* Create each field */
227         $field0 = array("string" => "<input type='checkbox' id='item_selected_".$key."' name='item_selected_".$key."'>" ,
228                         "attach" => "style='width:20px;'");
230         $field1 = array("string" => preg_replace(array("/%key%/","/%str%/"),array($key,$this->target_to_string($task)),$edit_link));
231         $field2 = array("string" => $this->time_to_string($task),"attach" => "style='width:100px;'");
232         $field3 = array("string" => $this->action_to_string($task),"attach" => "style='width:80px;'");
233         $field4 = array("string" => $action,"attach" => "style='text-align:right;width:40px;border-right:0px;'");
234         $divlist->AddElement(array($field0,$field1,$field2,$field3,$field4));
235       }
236     }
237  
238     return($divlist->Draw());
239   }
241   
242   function target_to_string($data)
243   {
244     $ret = "";
245     if($data['Action'] == "initial_install"){
246       foreach($data['Initial_Target'] as $target){
247         $ret .= $target['MAC'].", ";
248       } 
249     }else{
250       foreach($data['Target'] as $target){
251         $ret .= $target.", ";
252       } 
253     }
254     return(preg_replace("/, $/","",$ret));
255   }
257   
258   function time_to_string($data)
259   {
260     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
261   }
263   
264   function action_to_string($data)
265   {
266     $tmp = $this->get_actions();
267     if(isset($tmp[$data['Action']])){
268       return($tmp[$data['Action']]);
269     }else{
270       return(_("Unknown"));
271     }
272   }
274   
275   function load_gotomasses_data()
276   {
277     $ui = get_userinfo();
279     /* Check file existence */
280     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
281       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
282       return(FALSE);
283     }
285     /* check if file is readable */
286     $fp = @fopen($this->data_file,"r");
287     if(!$fp){
288       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
289       return(FALSE);
290     }
292     /* Get file contents */
293     $data ="";
294     while(!feof($fp)){
295       $data.= fread($fp,512);
296     }
298     /* Get lines from file */
299     $this->tasks  = array(); 
300     $comment      = "";
301     $rows         = split("\n",$data);
303     /* Walk trough rows and parse data */
304     foreach($rows as $row){
306       /* Skip empty lines */
307       $row = trim($row);
308       if(empty($row)){
309         continue;
310       }
312       /* Get comment, if available */     
313       if(preg_match("/^#/",$row)){
314         $comment = preg_replace("/^#/","",$row);
315         continue;
316       }
318       /* Split row into minutes/ hours ...*/ 
319       $row    = preg_replace('/[\t ]/umi'," ",$row);
320       $row    = preg_replace('/  */umi'," ",$row);
321       $parts  = split(" ",$row);
323       if(count($parts) != 8){
324         print_red(_("Entry broken, skipped."));
325       }else{
327         $entry = array();
328         $entry['Minute']  = $parts[0];  
329         $entry['Hour']    = $parts[1];  
330         $entry['Day']     = $parts[2];  
331         $entry['Month']   = $parts[3];  
332         $entry['Weekday'] = $parts[4];  
333         $entry['Action']  = $parts[5];  
334         $entry['OGroup']  = $parts[6];  
335         if($entry['Action'] == "initial_install"){
336           $tmp2 = split(";",$parts[7]);
337           foreach($tmp2 as $target){
338             $tmp = split(",",$target);
339             $entry['Initial_Target'][]  = array("MAC" => $tmp[0],"IP"=>$tmp[1],"NAME" => $tmp[2]);
340           }
341           $entry['Target']  = array();
342         }else{
343           $entry['Initial_Target']  = array();
344           $entry['Target']  = split(";",$parts[7]);
345         }
346         $entry['Comment'] = $comment;  
347         $this->tasks []   = $entry;
348       }
349     } 
350   }
353   function save_gotomasses_data()
354   {
355     $str = "#GOsa generated file, please just modify if you realy know what you do.";
356     foreach($this->tasks as $task){
357       $str .= "\n#".trim($task['Comment']);
358       $str .= "\n";
359       if($task['Action'] == "initial_install"){
360         $str .= "*     *     *     *     *     ";
361       }else{
362         $str .= str_pad($task['Minute'] ,5," ")." ";
363         $str .= str_pad($task['Hour']   ,5," ")." ";
364         $str .= str_pad($task['Day']    ,5," ")." ";
365         $str .= str_pad($task['Month']  ,5," ")." ";
366         $str .= str_pad($task['Weekday'],5," ")." ";
367       }
368       $str .= str_pad($task['Action'] ,5," ")." ";
369       $str .= str_pad($task['OGroup'] ,5," ")." ";
370       if($task['Action'] == "initial_install"){
371         foreach($task['Initial_Target'] as $target){
372           $str .= trim($target['MAC']).",".trim($target['IP']).",".trim($target['NAME']).";";
373         }
374       }else{
375         foreach($task['Target'] as $target){
376           $str .= $target.";";
377         }
378       }
379       $str = preg_replace("/;$/","",$str);
380     }
382     /* check file existence*/
383     $ui = get_userinfo();
384     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
385       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
386       return(FALSE);
387     }
389     /* check permissions */
390     $fp = @fopen($this->data_file,"w");
391     if(!$fp){
392       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
393       return(FALSE);
394     }
396     /* Write contents */
397     $str .= "\n";
398     fwrite($fp,$str);
399     fclose($fp);
400   }
403   function save_object()
404   {
405   }
408   /* Return list of object groups */
409   function get_object_groups()
410   {
411     $ret = array();
412     $ldap = $this->config->get_ldap_link();
413     $ldap->cd($this->config->current['BASE']);
414     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
415     while($attrs = $ldap->fetch()){
416       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
417     }
418     return($ret); 
419   }
421   
422   function save()
423   {
424     $this->save_gotomasses_data();
425   }
428   function list_get_selected_items()
429   {
430     $ids = array();
431     foreach($_POST as $name => $value){
432       if(preg_match("/^item_selected_[0-9]*$/",$name)){
433         $id   = preg_replace("/^item_selected_/","",$name);
434         $ids[$id] = $id;
435       }
436     }
437     return($ids);
438   }
441   function get_actions()
442   {
443     /* Prepare list of available actions */
444     $actions = array(       "reboot"          => _("Reboot"),
445                             "localboot"       => _("Localboot"),
446                             "halt"            => _("Halt system"),
447                             "initial_install" => _("Initial installation"),
448                             "update"          => _("Update"),
449                             "reinstall"       => _("(Re)Install"),
450                             "rescan"          => _("Rescan"),
451                             "wake"            => _("Wake"),
452                             "memcheck"        => _("Memory check"));
453     return($actions);
454   }
457   function plInfo()
458   {
459     return (array(
460         "plShortName"   => _("System mass deployment"),
461         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
462         "plSelfModify"  => FALSE,
463         "plDepends"     => array(),
464         "plPriority"    => 0,
465         "plSection"     => array("addon"),
466         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
467         "plProvidedAcls" => array("Comment"   => _("Description"), 
468                                   "Action"    => _("Action"),
469                                   "Day"       => _("Day"),
470                                   "Minute"    => _("Minute"),
471                                   "Hour"      => _("Hour"),
472                                   "Month"     => _("Month"),
473                                   "Weekday"   => _("Week day"),
474                                   "Target"    => _("Target"))
475         ));
476   }
478 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
479 ?>