Code

dc2bb580ed7f0e843595837c1fd5797729f52536
[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;
21   function gotomasses($config, $dn= NULL)
22   {
23     /* Define source file */
24     $this->data_file = CONFIG_DIR."/gotomasses_machines";
25   
26     /* Include config object */
27     $this->config= $config;
28     $this->load_gotomasses_data();
29   }
32   function execute()
33   {
34     $smarty = get_smarty();
35     /************
36      * Handle posts 
37      ************/
39     $s_entry = $s_action = "";
40     $arr = array("/^edit_task_/"=>"edit","/^remove_task_/"=>"remove","/^new_task_/"=>"new_task");
41     foreach($arr as $regex => $action){
42       foreach($_POST as $name => $value){
43         if(preg_match($regex,$name)){
44           $s_action = $action;
45           $s_entry  = preg_replace($regex,"",$name);
46           $s_entry  = preg_replace("/_(x|y)$/","",$s_entry);
47         }
48       }
49     }
51     /************
52      * List posts 
53      ************/
55     /* Remove entry from list */
56     if($s_action == "remove" && isset($this->tasks[$s_entry])){
57       if(!$this->acl_is_removeable()){
58         print_red(_("You are not allowed to remove a task."));
59       }else{
60         $smarty->assign("info",_("Your are about to delete a gotomasses task."));
61         $this->current = $s_entry;
62         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
63       }
64     }
66     /* Remove entry, remove is confirmed */
67     if($this->current != -1 && isset($_POST['delete_confirm'])){
68       unset($this->tasks[$this->current]);
69       $this->current = -1;
70       $this->save();
71     }
73     /* Edit selected entry */
74     if($s_action == "edit" && isset($this->tasks[$s_entry])){
75       $entry = $this->tasks[$s_entry];
76       $this->dialog = new goto_task($this->config,$this,$entry);
77       $this->current = $s_entry;
78     }
80     /* New entry */
81     if($s_action== "new_task" && $this->acl_is_createable()){
82       $this->dialog = new goto_task($this->config,$this);
83       $this->current = -1;
84     }
85   
86     /* Close dialog */
87     if(isset($_POST['close_goto_task'])){
88       $this->dialog = FALSE;
89       $this->current = -1;
90     }
92     /* Close dialog */
93     if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
94       $this->dialog->save_object();
95       $msgs = $this->dialog->check();
96       if(count($msgs)){
97         foreach($msgs as $msg){
98           print_red($msg);  
99         }
100       }else{
101         if(isset($this->tasks[$this->current]) && $this->current != -1){
102           $this->tasks[$this->current] = $this->dialog->save();
103         }else{
104           $this->tasks[] = $this->dialog->save();
105         }
106         if(!isset($_POST['apply_goto_task'])){
107           $this->dialog = FALSE;
108           $this->current = -1;
109         }
110         $this->save();
111       }
112     }
114     /* Display dialogs if currently opened */
115     if(is_object($this->dialog)){
116       $this->dialog->save_object();
117       return($this->dialog->execute());
118     }
121     /************
122      * Handle Divlist 
123      ************/
125     $plug = $_GET['plug'];
126     $divlist = new MultiSelectWindow($this->config,"GotoMasses","gotomassses");
127     $divlist->SetSummary(_("Gotomasses tasks"));
128     $divlist->SetHeadpageMode();
129     $divlist->EnableCloseButton(FALSE);
130     $divlist->EnableSaveButton(FALSE);
131     $divlist->SetInformation(_("This menu allows you to add, remove and change the properties of gotomasses tasks."));
132  
133     /* Display add button if allowed */ 
134     if($this->acl_is_createable()){ 
135       $header = "<div style='padding:5px'>
136         <input type='image' src='images/gotomasses_new_task.png' name='new_task' class='center'>
137         </div>";
138     }else{
139       $header = "";
140     }
142     /* Get Permissions */ 
143     $ui = get_userinfo();
144     $acls = $this->getacl("");
145     foreach($ui->get_module_departments("gotomasses") as $dep){
146       $acls .= $ui->get_category_permissions($dep,"gotomasses");
147     }
148  
149     /* Create divlist */
150     $divlist->SetListHeader($header);
151     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=object'>"._("Target")."</a>"));
152     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=schedule'>"._("Schedule")."</a>",
153                                       "attach"=>"style='width:100px;'"));
154     $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&amp;sort=action'>"._("Type")."</a>",
155                                       "attach"=>"style='width:80px;'"));
156     $divlist->AddHeader(array("string"=>_("Action"),
157                                       "attach"=>"style='border-right:0px;width:40px;'"));
159     if(!empty($acls)){
160       foreach($this->tasks as $key => $task){
161         $action = "<input type='image' src='images/edit.png' name='edit_task_".$key."' class='center' alt='"._("Edit")."'>";
162         if($this->acl_is_removeable()){
163           $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$key."' class='center' alt='"._("Reomve")."'>";
164         }
165         $field1 = array("string" => "<div style='width:100%;overflow:hidden;'><nobr>".$this->target_to_string($task)."</nobr></div>");
166         $field2 = array("string" => $this->time_to_string($task),"attach" => "style='width:100px;'");
167         $field3 = array("string" => $this->action_to_string($task),"attach" => "style='width:80px;'");
168         $field4 = array("string" => $action,"attach" => "style='text-align:right;width:40px;border-right:0px;'");
169         $divlist->AddElement(array($field1,$field2,$field3,$field4));
170       }
171     }
172  
173     return($divlist->Draw());
174   }
176   
177   function target_to_string($data)
178   {
179     $ret = "";
180     if($data['Action'] == "initial_install"){
181       foreach($data['Initial_Target'] as $target){
182         $ret .= $target['MAC'].", ";
183       } 
184     }else{
185       foreach($data['Target'] as $target){
186         $ret .= $target.", ";
187       } 
188     }
189     return(preg_replace("/, $/","",$ret));
190   }
192   
193   function time_to_string($data)
194   {
195     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
196   }
198   
199   function action_to_string($data)
200   {
201     $tmp = $this->get_actions();
202     if(isset($tmp[$data['Action']])){
203       return($tmp[$data['Action']]);
204     }else{
205       return(_("Unknown"));
206     }
207   }
209   
210   function load_gotomasses_data()
211   {
212     $ui = get_userinfo();
214     /* Check file existence */
215     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
216       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
217       return(FALSE);
218     }
220     /* check if file is readable */
221     $fp = @fopen($this->data_file,"r");
222     if(!$fp){
223       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
224       return(FALSE);
225     }
227     /* Get file contents */
228     $data ="";
229     while(!feof($fp)){
230       $data.= fread($fp,512);
231     }
233     /* Get lines from file */
234     $this->tasks  = array(); 
235     $comment      = "";
236     $rows         = split("\n",$data);
238     /* Walk trough rows and parse data */
239     foreach($rows as $row){
241       /* Skip empty lines */
242       $row = trim($row);
243       if(empty($row)){
244         continue;
245       }
247       /* Get comment, if available */     
248       if(preg_match("/^#/",$row)){
249         $comment = preg_replace("/^#/","",$row);
250         continue;
251       }
253       /* Split row into minutes/ hours ...*/ 
254       $row    = preg_replace('/[\t ]/umi'," ",$row);
255       $row    = preg_replace('/  */umi'," ",$row);
256       $parts  = split(" ",$row);
258       if(count($parts) != 8){
259         print_red(_("Entry broken, skipped."));
260       }else{
262         $entry = array();
263         $entry['Minute']  = $parts[0];  
264         $entry['Hour']    = $parts[1];  
265         $entry['Day']     = $parts[2];  
266         $entry['Month']   = $parts[3];  
267         $entry['Weekday'] = $parts[4];  
268         $entry['Action']  = $parts[5];  
269         $entry['OGroup']  = $parts[6];  
270         if($entry['Action'] == "initial_install"){
271           $tmp2 = split(";",$parts[7]);
272           foreach($tmp2 as $target){
273             $tmp = split(",",$target);
274             $entry['Initial_Target'][]  = array("MAC" => $tmp[0],"IP"=>$tmp[1],"NAME" => $tmp[2]);
275           }
276           $entry['Target']  = array();
277         }else{
278           $entry['Initial_Target']  = array();
279           $entry['Target']  = split(";",$parts[7]);
280         }
281         $entry['Comment'] = $comment;  
282         $this->tasks []   = $entry;
283       }
284     } 
285   }
288   function save_gotomasses_data()
289   {
290     $str = "#GOsa generated file, please just modify if you realy know what you do.";
291     foreach($this->tasks as $task){
292       $str .= "\n#".trim($task['Comment']);
293       $str .= "\n";
294       if($task['Action'] == "initial_install"){
295         $str .= "*     *     *     *     *     ";
296       }else{
297         $str .= str_pad($task['Minute'] ,5," ")." ";
298         $str .= str_pad($task['Hour']   ,5," ")." ";
299         $str .= str_pad($task['Day']    ,5," ")." ";
300         $str .= str_pad($task['Month']  ,5," ")." ";
301         $str .= str_pad($task['Weekday'],5," ")." ";
302       }
303       $str .= str_pad($task['Action'] ,5," ")." ";
304       $str .= str_pad($task['OGroup'] ,5," ")." ";
305       if($task['Action'] == "initial_install"){
306         foreach($task['Initial_Target'] as $target){
307           $str .= $target['MAC'].",".$target['IP'].",".$target['NAME'].";";
308         }
309       }else{
310         foreach($task['Target'] as $target){
311           $str .= $target.";";
312         }
313       }
314       $str = preg_replace("/;$/","",$str);
315     }
317     /* check file existence*/
318     $ui = get_userinfo();
319     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
320       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
321       return(FALSE);
322     }
324     /* check permissions */
325     $fp = @fopen($this->data_file,"w");
326     if(!$fp){
327       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
328       return(FALSE);
329     }
331     /* Write contents */
332     $str .= "\n";
333     fwrite($fp,$str);
334     fclose($fp);
335   }
338   function save_object()
339   {
340   }
343   /* Return list of object groups */
344   function get_object_groups()
345   {
346     $ret = array();
347     $ldap = $this->config->get_ldap_link();
348     $ldap->cd($this->config->current['BASE']);
349     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
350     while($attrs = $ldap->fetch()){
351       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
352     }
353     return($ret); 
354   }
356   
357   function save()
358   {
359     $this->save_gotomasses_data();
360   }
363   function get_actions()
364   {
365     /* Prepare list of available actions */
366     $actions = array(       "reboot"          => _("Reboot"),
367                             "localboot"       => _("Localboot"),
368                             "halt"            => _("Halt system"),
369                             "initial_install" => _("Initial installation"),
370                             "update"          => _("Update"),
371                             "reinstall"       => _("(Re)Install"),
372                             "rescan"          => _("Rescan"),
373                             "wake"            => _("Wake"),
374                             "memcheck"        => _("Memory check"));
375     return($actions);
376   }
379   function plInfo()
380   {
381     return (array(
382         "plShortName"   => _("System mass deployment"),
383         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
384         "plSelfModify"  => FALSE,
385         "plDepends"     => array(),
386         "plPriority"    => 0,
387         "plSection"     => array("addon"),
388         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
389         "plProvidedAcls" => array("Comment"   => _("Description"), 
390                                   "Action"    => _("Action"),
391                                   "Day"       => _("Day"),
392                                   "Minute"    => _("Minute"),
393                                   "Hour"      => _("Hour"),
394                                   "Month"     => _("Month"),
395                                   "Weekday"   => _("Week day"),
396                                   "Target"    => _("Target"))
397         ));
398   }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>