Code

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