Code

Starting move
[gosa.git] / gosa-core / plugins / addons / gotomasses / class_goto_task.inc
1 <?php
3 class goto_task extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "System mass deployment";
7   var $plDescription  = "This does something";
8  
9   var $parent   = NULL;
10  
11   var $Minute   = "*";
12   var $Hour     = "*";
13   var $Day      = "*";
14   var $Month    = "*";
15   var $Weekday  = "*";
16   var $Action   = "install";
18   var $Zone     = "";
19   var $Section  = "";
21   var $Zones    = array();
22   var $Sections = array();
24   var $Comment  = "";
25   var $OGroup   = "keep_current";
26   var $OGroups  = array();
27   var $Target   = array();
28   var $Initial_Target   = array();
29   var $Actions  = array();
30   var $new      = FALSE;
31   var $ID = 0;
32   var $TASK_ID =0;
33   var $attributes     = array("ID","TASK_ID","Zone","Section","OGroup","Minute","Hour","Day",
34                               "Month","Weekday","Action","Comment","Target","Initial_Target");
36   var $configure_dhcp = FALSE;
37   var $configure_dns  = FALSE;
40   function goto_task(&$config, &$parent, $data = array())
41   {
42     /* Set parent object */
43     $this->parent = &$parent;
45     /* Intialize plugin */
46     $this->config = &$config;
47     if(count($data)){
48       $tmp = $this->get_array_values();
49       foreach($this->attributes as $attr){
50         if(!isset($data[$attr])){
51           trigger_error("Missing parameter: '".$attr."' for goto_task contruction.");
52         }else{
53           $this->$attr = $data[$attr];
54         }
55       }
56     }else{
57       $this->new = TRUE;
58     }
60     if(!empty($this->Zone) && !preg_match("/^\"\"$/",$this->Zone)){
61       $this->configure_dns = TRUE;
62     }
63     if(!empty($this->Section) && !preg_match("/^\"\"$/",$this->Section)){
64       $this->configure_dhcp = TRUE;
65     }
67     /* Set dns and dhcp attributes */
68     foreach(getAvailableZones($this->config) as $zone){
69       $name = preg_replace("#^[^/]+/#","",$zone);
70       $this->Zones[$name] = $zone; 
71     }
72     $this->Sections = $this->get_dhcp_sections();
74     /* Create ogroup select list */
75     $this->OGroups = array("\"\""     => "["._("Keep current")."]");
76     $this->OGroups = array_merge($this->OGroups,$this->parent->get_object_groups());
78     /* Prepare list of available actions */
79     $this->Actions = $this->parent->get_actions();
80   }
83   function execute()
84   {
85     /********
86      * Handle Posts  
87      ********/
88   
89     /* Handle Target options */
90     foreach($_POST as $name => $value){
91       if(preg_match("/^remove_/",$name)){
92         $value = preg_replace("/^remove_([0-9]*)_(x|y)$/i","\\1",$name);
93         if(isset($this->Target[$value]) && $this->Action != "initial_install"){
94           unset($this->Target[$value]);
95         }elseif(isset($this->Initial_Target[$value]) && $this->Action == "initial_install"){
96           unset($this->Initial_Target[$value]);
97         }
98       }
99     }
101     if($this->Action != "initial_install"){
102       
103       /* Add target */
104       if($this->ID == 0 && isset($_POST['add_target']) && !empty($_POST['target_text'])){
105         $target = get_post("target_text");
106         if($this->is_valid_target($target) && !in_array($target,$this->Target)){
107           $this->Target[] = $target;
108         }else{
109           print_red(_("You must specify a valid MAC address or host name."));
110         }
111       }
112     }else{
114       /* Add target */
115       if($this->ID ==0 && isset($_POST['add_target']) && !empty($_POST['task_MAC'])){
116         $MAC = $_POST['task_MAC'];
117         $NAME= "";
118         $IP  = "";
119         $DNS   = "";
120         $DHCP  = "";
121         if(isset($_POST['task_Name'])){
122           $NAME = $_POST['task_Name'];
123         }
124         if(isset($_POST['task_IP']) && is_ip($_POST['task_IP'])){
125           $IP = $_POST['task_IP'];
126         }
127         if(isset($_POST['configure_dns']) && isset($_POST['Zone']) && isset($this->Zones[$_POST['Zone']])){
128           $DNS = $_POST['Zone'];
129         }
130         if(isset($_POST['configure_dhcp']) && isset($_POST['Section']) && isset($this->Sections[$_POST['Section']])){
131           $DHCP = $_POST['Section'];
132         }
133         if(is_mac($MAC)){
134           $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
135         }
136       }
137     }
139     /* Add via csv */
140     if($this->ID == 0 && isset($_FILES['import_file'])){
141       $file = $_FILES['import_file']['tmp_name'];
142       if(file_exists($file) && is_readable($file)){
143         $str ="";
144         $fp = fopen($file,"r");
145         while(!feof($fp)){
146           $line = fgets($fp,512);
147           $tmp = preg_split("/(,|;)/",$line);
149           $MAC = $IP = $NAME; 
150           if(isset($tmp[0])){
151             $MAC = trim($tmp[0]);
152           }
153           if(isset($tmp[1])){
154             $IP = trim($tmp[1]);
155           }
156           if(isset($tmp[2])){
157             $NAME = trim($tmp[2]);
158           }
159           if(is_mac($MAC)){
160             $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
161           }
162         }
163       }
164     }
165   
166     /********
167      * Add target from list 
168      ********/
170     /* If add from list is was requsted, display this list */ 
171     if(isset($_POST['add_from_list'])){
172       $this->dialog = new target_list($this->config,$this->Target);
173     }
175     /* Save selected objects as target */
176     if(isset($_POST['SaveMultiSelectWindow']) && is_object($this->dialog)){
177       $this->dialog->save_object();
178       $ret = $this->dialog->save();
179       foreach($ret as $entry){
180         $this->Target[] = $entry['cn'][0];
181       }
182       $this->dialog = FALSE;
183     }
185     /* Cancel object listing */
186     if(isset($_POST['CloseMultiSelectWindow'])){
187       $this->dialog = FALSE;
188     }
190     /* Display object Listing */
191     if(is_object($this->dialog)){
192       $this->dialog->save_object();
193       return($this->dialog->execute());
194     }
196     /********
197      * Display this plugin  
198      ********/
200     $divlist = new divlist("goto_task");
201 #    $divlist->SetPluginMode(); 
202     $divlist->setHeight(200);
203     $divlist->SetWidth("100%");
204     $divlist->SetEntriesPerPage(0);
206     $acl_target = $this->parent->getacl("Target");
207     if(preg_match("/w/i",$acl_target)){
208       $field_del = array("string" => "<input type='image' src='images/edittrash.png' name='remove_%KEY%'>" , 
209           "attach" => "style='width:44px;border-right:0px;'");
210     }else{
211       $field_del = array("string" => "",
212           "attach" => "style='width:44px;border-right:0px;'");
213     }
215     /* Add entries to divlist */
216     if($this->Action == "initial_install"){
217       $divlist->SetHeader(array(
218                             array("string" => _("Target systems") ,"attach" => "style='width:120px;'"),
219                             array("string" => _("IP")      , "attach" => "style='width:90px;'"),
220                             array("string" => _("Name")    , "attach" => "style='width:150px;'"),
221                             array("string" => _("Actions") , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
222       foreach($this->Initial_Target as $key => $target){
223         $field1 = array("string" => $target['MAC']  ,"attach" => "style='width:120px;'");
224         $field2 = array("string" => $target['IP']   ,"attach" => "style='width:90px;'");
225         $field3 = array("string" =>  $target['NAME'] ,"attach" => "style='width:150px;'");
226         $divlist->AddEntry(array($field1,$field2,$field3,preg_replace("/%KEY%/",$key,$field_del)));
227       } 
228     }else{
229       $divlist->SetHeader(array(
230                             array("string" => "Target", "attach" => "style=''"),
231                             array("string" => "Actions" , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
232       foreach($this->Target as $key => $target){
233         $field1 = array("string" => $target);
234         $divlist->AddEntry(array($field1,preg_replace("/%KEY%/",$key,$field_del)));
235       } 
236     }
238     $smarty = get_smarty();
239     foreach($this->attributes as $attr){
240       $smarty->assign($attr."ACL", $this->parent->getacl($attr));
241       $smarty->assign($attr,$this->$attr);
242     }
244     $smarty->assign("Zones", $this->Zones);
245     $smarty->assign("Sections", $this->Sections);
247     $smarty->assign("ID",$this->ID);
248   
249     $smarty->assign("Zone", $this->Zone);
250     $smarty->assign("Section", $this->Section);
252     $smarty->assign("configure_dhcp", $this->configure_dhcp);
253     $smarty->assign("configure_dns", $this->configure_dns);
255     $tmp = $this->get_array_values();
256     $smarty->assign("JS"      , $_SESSION['js']);
257     $smarty->assign("Minutes" , $tmp['Minute']);
258     $smarty->assign("Hours"   , $tmp['Hour']);
259     $smarty->assign("Days"    , $tmp['Day']);
260     $smarty->assign("Months"  , $tmp['Month']);
261     $smarty->assign("Weekdays", $tmp['Weekday']);
262     $smarty->assign("OGroups" , $this->OGroups);
263     $smarty->assign("Actions"     , $this->Actions);
264     $smarty->assign("Target_list" , $divlist->DrawList());
265     $smarty->assign("new"      , $this->new);
266     return ($smarty->fetch (get_template_path('goto_task.tpl', TRUE)));
267   }
270   function create_tree($arr,$base,$current = "")
271   {
272     $ret = array();
273     foreach($arr as $r => $name){
274       $base_part = str_replace($base,"",$r);
275       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
276         $ret[$r] = $current.$name;
277         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
278         foreach($tmp as $sub_key => $sub_name){
279           $ret[$sub_key] = $sub_name;
280         }
281       }
282     }
283     return($ret);
284   }
288   function get_dhcp_sections()
289   {
290     $ldap = $this->config->get_ldap_link();
291     $ldap->cd($this->config->current['BASE']);
292     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
294     $tmp   = array();
295     $tmp2  = array();
297     $dhcp_dns = array();
298     while($attr = $ldap->fetch()){
299       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
300     }
302     foreach($dhcp_dns as $key => $pri_dns){
303       $ldap->cat($pri_dns,array("cn"));
304       $tmp = $ldap->fetch();
305       $dhcp_dns[$key] = $tmp['cn'][0];
306     }
308     foreach($dhcp_dns as $dn => $cn){
309       $ldap->cd($dn);
310       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
311                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
312       $tmp = array();
313       while($attr = $ldap->fetch()){
314         $tmp[$attr['dn']] = $attr['cn'][0];
315       }
316       $tmp2 = $this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;");
317     }
318     $ret = array();
319     foreach($tmp2 as $key => $label){
320       $ret[$tmp[$key]] = $label;
321     }
322     return($ret);
323   }
325   
326   /* check given values */
327   function check()
328   {
329     $message = plugin::check();
330     $tmp = array(
331       "OGroup" => _("Object group") ,"Minute" => _("Minute"),
332       "Hour"   => _("Hour")         ,"Day"    => _("Day"),
333       "Month"  => _("Month")        ,"Weekday"=> _("Week day"),
334       "Action" => _("Action")       ,"Comment"=> _("Description"));
335     
336     foreach($tmp as $name => $desc){
337       if(empty($this->$name) && $this->$name != 0){
338         $message[] = sprintf(_("The given value for attribute '%s' is invalid."),$desc);
339       }
340     }
341     if(count($this->Target) == 0 && $this->Action != "initial_install"){
342       $message[] = sprintf(_("You must specify at least one target"));
343     }
344     if(count($this->Initial_Target) == 0 && $this->Action == "initial_install"){
345       $message[] = sprintf(_("You must specify at least one target"));
346     }
347     return($message);
348   }
349   
351   /* Map acl function, to be able to use plugin::save_object() */
352   function acl_is_writeable($attribute,$skip_write = FALSE)
353   {
354     return($this->parent->acl_is_writeable($attribute,$skip_write));
355   }
358   function save_object()
359   {
360     if(isset($_POST['goto_task_posted'])){
362       if($this->Action == "initial_install"){
363         if(isset($_POST['configure_dns'])){
364           $this->configure_dns = TRUE;
365           if(isset($_POST['Zone'])){
366             $this->Zone = get_post("Zone");
367           }
368         }else{
369           $this->Zone = "\"\"";
370           $this->configure_dns = FALSE;
371         }
373         if(isset($_POST['configure_dhcp'])){
374           $this->configure_dhcp = TRUE;
375           if(isset($_POST['Section'])){
376             $this->Section = get_post("Section");
377           }
378         }else{
379           $this->configure_dhcp = FALSE;
380           $this->Section = "\"\"";
381         }
382       }
383       plugin::save_object();
384     }
385   }  
388   /* Check if given target is vald.
389    * It must either be a valid MAC address or an existing object group
390    */
391   function is_valid_target($str)
392   {
393     if(is_mac($str)){
394       return(TRUE);
395     }else{
396       $ldap = $this->config->get_ldap_link();
397       $ldap->cd($this->config->current['BASE']);
398       $ldap->search("(&(objectClassgosaGroupOfNames)(cn=".$str."))",array("cn"));
399       if($ldap->count()){
400         return(TRUE);
401       }
402     }
403   }
405   function save()
406   {
407     $tmp = array();
408     foreach($this->attributes as $attr){
409       $tmp[$attr] = $this->$attr;
410     }
412     if($this->Action != "initial_install" || !$this->configure_dns){
413       $tmp['Zone'] = "\"\"";
414     }
415     if($this->Action != "initial_install" || !$this->configure_dhcp){
416       $tmp['Section'] = "\"\"";
417     }
418     return($tmp);
419   }
422   /* Return values for listboxes. 
423    */
424   function get_array_values()
425   {
426     $ret = array();    
428     /* Create minute array */
429     $Minute = array( "*"    => "*",
430                       "*/1"  => "*/1",
431                       "*/3"  => "*/3",
432                       "*/5"  => "*/5",
433                       "*/10" => "*/10",
434                       "*/15" => "*/15",
435                       "*/30" => "*/30",
436                       "*/45" => "*/45",
437                       "*/60" => "*/60"); 
438     for($i = 0; $i < 60 ; $i ++){
439       $Minute[$i] = $i;
440     }
442     /* Create hour array */
443     $Hour = array(   "*" => "*");
444     for($i = 1 ; $i < 24 ; $i ++ ){
445       $Hour["*/".$i] = "*/".$i;
446     } 
447     for($i = 0 ; $i < 24 ; $i ++ ){
448       $Hour[$i] = $i;
449     } 
451     /* Create hour array */
452     $Day = array( "*" => "*");
453     for($i = 1 ; $i < 32 ; $i ++ ){
454       $Day["*/".$i] = "*/".$i;
455     } 
456     for($i = 1 ; $i < 32 ; $i ++ ){
457       $Day[$i] = $i;
458     } 
459    
460     /* Create month array */
461     $Month = array( "*" => "*");
462     for($i = 1 ; $i <= 12 ; $i ++ ){
463       $Month["*/".$i] = "*/".$i;
464     } 
465     for($i = 1 ; $i <= 12 ; $i ++ ){
466       $Month[$i] = $i;
467     } 
468    
469     /* Create week day array */
470     $Weekday = array( "*" => "*");
471     for($i = 1 ; $i <= 7 ; $i ++ ){
472       $Weekday["*/".$i] = "*/".$i;
473     } 
474     for($i = 0 ; $i <= 7 ; $i ++ ){
475       $Weekday[$i] = $i;
476     } 
477    
478     foreach(array("Minute","Weekday","Hour","Day","Month") as $var){
479       $ret[$var] = $$var;
480     }
481     return($ret);
482   }
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>