Code

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