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