Code

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