Code

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