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