Code

Shortened string
[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:60px;'"));
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:60px;'");
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     foreach($data['Target'] as $target){
183       $ret .= $target.", ";
184     } 
185     return(preg_replace("/, $/","",$ret));
186   }
188   
189   function time_to_string($data)
190   {
191     return($data['Minute']." ".$data['Hour']." ".$data['Day']." ".$data['Month']." ".$data['Weekday']);
192   }
194   
195   function action_to_string($data)
196   {
197     switch($data['Action']){
199       case 'reinstall' : return("Reinstall");break;
200       case 'install' : return("Install");break;
201       case 'reboot' : return("Restart");break;
202       case 'update' : return("Update");break;
203       default : return("Unknown");
204     }
205   }
207   
208   function load_gotomasses_data()
209   {
210     $ui = get_userinfo();
212     /* Check file existence */
213     if(!file_exists($this->data_file) || !is_readable($this->data_file)){
214       print_red(sprintf(_("Can't locate or read gotomasses storage file '%s'."),$this->data_file));
215       return(FALSE);
216     }
218     /* check if file is readable */
219     $fp = @fopen($this->data_file,"r");
220     if(!$fp){
221       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
222       return(FALSE);
223     }
225     /* Get file contents */
226     $data ="";
227     while(!feof($fp)){
228       $data.= fread($fp,512);
229     }
231     /* Get lines from file */
232     $this->tasks  = array(); 
233     $comment      = "";
234     $rows         = split("\n",$data);
236     /* Walk trough rows and parse data */
237     foreach($rows as $row){
239       /* Skip empty lines */
240       $row = trim($row);
241       if(empty($row)){
242         continue;
243       }
245       /* Get comment, if available */     
246       if(preg_match("/^#/",$row)){
247         $comment = preg_replace("/^#/","",$row);
248         continue;
249       }
251       /* Split row into minutes/ hours ...*/ 
252       $row    = preg_replace('/[\t ]/umi'," ",$row);
253       $row    = preg_replace('/  */umi'," ",$row);
254       $parts  = split(" ",$row);
256       if(count($parts) != 8){
257         print_red(_("Entry broken, skipped."));
258       }else{
260         $entry = array();
261         $entry['Minute']  = $parts[0];  
262         $entry['Hour']    = $parts[1];  
263         $entry['Day']     = $parts[2];  
264         $entry['Month']   = $parts[3];  
265         $entry['Weekday'] = $parts[4];  
266         $entry['Action']  = $parts[5];  
267         $entry['OGroup']  = $parts[6];  
268         $entry['Target']  = split(",",$parts[7]);  
269         $entry['Comment'] = $comment;  
270         $this->tasks []   = $entry;
271       }
272     } 
273   }
276   function save_gotomasses_data()
277   {
278     $str = "#GOsa generated file, please just modify if you realy know what you do.";
279     foreach($this->tasks as $task){
280       $str .= "\n#".trim($task['Comment']);
281       $str .= "\n";
282       $str .= str_pad($task['Minute'] ,5," ")." ";
283       $str .= str_pad($task['Hour']   ,5," ")." ";
284       $str .= str_pad($task['Day']    ,5," ")." ";
285       $str .= str_pad($task['Month']  ,5," ")." ";
286       $str .= str_pad($task['Weekday'],5," ")." ";
287       $str .= str_pad($task['Action'] ,5," ")." ";
288       $str .= str_pad($task['OGroup'] ,5," ")." ";
289       foreach($task['Target'] as $target){
290         $str .= $target.",";
291       }
292       $str = preg_replace("/,$/","",$str);
293     }
295     /* check file existence*/
296     $ui = get_userinfo();
297     if(!file_exists($this->data_file) || !is_writeable($this->data_file)){
298       print_red(sprintf(_("Can't locate or write gotomasses storage file '%s'."),$this->data_file));
299       return(FALSE);
300     }
302     /* check permissions */
303     $fp = @fopen($this->data_file,"w");
304     if(!$fp){
305       print_red(sprintf(_("Can't read gotomasses storage file '%s'."),$this->data_file));
306       return(FALSE);
307     }
309     /* Write contents */
310     $str .= "\n";
311     fwrite($fp,$str);
312     fclose($fp);
313   }
316   function save_object()
317   {
318   }
321   /* Return list of object groups */
322   function get_object_groups()
323   {
324     $ret = array();
325     $ldap = $this->config->get_ldap_link();
326     $ldap->cd($this->config->current['BASE']);
327     $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
328     while($attrs = $ldap->fetch()){
329       $ret[$attrs['cn'][0]] = $attrs['cn'][0];
330     }
331     return($ret); 
332   }
334   
335   function save()
336   {
337     $this->save_gotomasses_data();
338   }
341   function get_actions()
342   {
343     /* Prepare list of available actions */
344     $actions = array( "reboot"          => _("Reboot"),
345                             "localboot"       => _("Localboot"),
346                             "halt"            => _("Halt system"),
347                             "update"          => _("Update"),
348                             "reinstall"       => _("(Re)Install"),
349                             "rescan"          => _("Rescan"),
350                             "memcheck"        => _("Memory check"));
351     return($actions);
352   }
355   function plInfo()
356   {
357     return (array(
358         "plShortName"   => _("System mass deployment"),
359         "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
360         "plSelfModify"  => FALSE,
361         "plDepends"     => array(),
362         "plPriority"    => 0,
363         "plSection"     => array("addon"),
364         "plCategory"    => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
365         "plProvidedAcls" => array("Comment"   => _("Description"), 
366                                   "Action"    => _("Action"),
367                                   "Day"       => _("Day"),
368                                   "Minute"    => _("Minute"),
369                                   "Hour"      => _("Hour"),
370                                   "Month"     => _("Month"),
371                                   "Weekday"   => _("Week day"),
372                                   "Target"    => _("Target"))
373         ));
374   }
376 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
377 ?>