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 $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 ********/
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"){
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 }
109 }
110 }else{
112 /* Add target */
113 if($this->ID ==0 && isset($_POST['add_target']) && !empty($_POST['task_MAC'])){
114 $MAC = $_POST['task_MAC'];
115 $NAME= "";
116 $IP = "";
117 $DNS = "";
118 $DHCP = "";
119 if(isset($_POST['task_Name'])){
120 $NAME = $_POST['task_Name'];
121 }
122 if(isset($_POST['task_IP']) && is_ip($_POST['task_IP'])){
123 $IP = $_POST['task_IP'];
124 }
125 if(isset($_POST['configure_dns']) && isset($_POST['Zone']) && isset($this->Zones[$_POST['Zone']])){
126 $DNS = $_POST['Zone'];
127 }
128 if(isset($_POST['configure_dhcp']) && isset($_POST['Section']) && isset($this->Sections[$_POST['Section']])){
129 $DHCP = $_POST['Section'];
130 }
131 if(is_mac($MAC)){
132 $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
133 }
134 }
135 }
137 /* Add via csv */
138 if($this->ID == 0 && isset($_FILES['import_file'])){
139 $file = $_FILES['import_file']['tmp_name'];
140 if(file_exists($file) && is_readable($file)){
141 $str ="";
142 $fp = fopen($file,"r");
143 while(!feof($fp)){
144 $line = fgets($fp,512);
145 $tmp = preg_split("/(,|;)/",$line);
147 $MAC = $IP = $NAME;
148 if(isset($tmp[0])){
149 $MAC = trim($tmp[0]);
150 }
151 if(isset($tmp[1])){
152 $IP = trim($tmp[1]);
153 }
154 if(isset($tmp[2])){
155 $NAME = trim($tmp[2]);
156 }
157 if(is_mac($MAC)){
158 $this->Initial_Target[] = array("MAC"=>$MAC,"IP"=>$IP,"NAME"=>$NAME);
159 }
160 }
161 }
162 }
164 /********
165 * Add target from list
166 ********/
168 /* If add from list is was requsted, display this list */
169 if(isset($_POST['add_from_list'])){
170 $this->dialog = new target_list($this->config,$this->Target);
171 }
173 /* Save selected objects as target */
174 if(isset($_POST['SaveMultiSelectWindow']) && is_object($this->dialog)){
175 $this->dialog->save_object();
176 $ret = $this->dialog->save();
177 foreach($ret as $entry){
178 $this->Target[] = $entry['cn'][0];
179 }
180 $this->dialog = FALSE;
181 }
183 /* Cancel object listing */
184 if(isset($_POST['CloseMultiSelectWindow'])){
185 $this->dialog = FALSE;
186 }
188 /* Display object Listing */
189 if(is_object($this->dialog)){
190 $this->dialog->save_object();
191 return($this->dialog->execute());
192 }
194 /********
195 * Display this plugin
196 ********/
198 $divlist = new divlist("goto_task");
199 # $divlist->SetPluginMode();
200 $divlist->setHeight(200);
201 $divlist->SetWidth("100%");
202 $divlist->SetEntriesPerPage(0);
204 $acl_target = $this->parent->getacl("Target");
205 if(preg_match("/w/i",$acl_target)){
206 $field_del = array("string" => "<input type='image' src='images/edittrash.png' name='remove_%KEY%'>" ,
207 "attach" => "style='width:44px;border-right:0px;'");
208 }else{
209 $field_del = array("string" => "",
210 "attach" => "style='width:44px;border-right:0px;'");
211 }
213 /* Add entries to divlist */
214 if($this->Action == "initial_install"){
215 $divlist->SetHeader(array(
216 array("string" => _("Target systems") ,"attach" => "style='width:120px;'"),
217 array("string" => _("IP") , "attach" => "style='width:90px;'"),
218 array("string" => _("Name") , "attach" => "style='width:150px;'"),
219 array("string" => _("Actions") , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
220 foreach($this->Initial_Target as $key => $target){
221 $field1 = array("string" => $target['MAC'] ,"attach" => "style='width:120px;'");
222 $field2 = array("string" => $target['IP'] ,"attach" => "style='width:90px;'");
223 $field3 = array("string" => $target['NAME'] ,"attach" => "style='width:150px;'");
224 $divlist->AddEntry(array($field1,$field2,$field3,preg_replace("/%KEY%/",$key,$field_del)));
225 }
226 }else{
227 $divlist->SetHeader(array(
228 array("string" => "Target", "attach" => "style=''"),
229 array("string" => "Actions" , "attach" => "style='width:44px;border-right:0px;text-align:right;'")));
230 foreach($this->Target as $key => $target){
231 $field1 = array("string" => $target);
232 $divlist->AddEntry(array($field1,preg_replace("/%KEY%/",$key,$field_del)));
233 }
234 }
236 $smarty = get_smarty();
237 foreach($this->attributes as $attr){
238 $smarty->assign($attr."ACL", $this->parent->getacl($attr));
239 $smarty->assign($attr,$this->$attr);
240 }
242 $smarty->assign("Zones", $this->Zones);
243 $smarty->assign("Sections", $this->Sections);
245 $smarty->assign("ID",$this->ID);
247 $smarty->assign("Zone", $this->Zone);
248 $smarty->assign("Section", $this->Section);
250 $smarty->assign("configure_dhcp", $this->configure_dhcp);
251 $smarty->assign("configure_dns", $this->configure_dns);
253 $tmp = $this->get_array_values();
254 $smarty->assign("JS" , $_SESSION['js']);
255 $smarty->assign("Minutes" , $tmp['Minute']);
256 $smarty->assign("Hours" , $tmp['Hour']);
257 $smarty->assign("Days" , $tmp['Day']);
258 $smarty->assign("Months" , $tmp['Month']);
259 $smarty->assign("Weekdays", $tmp['Weekday']);
260 $smarty->assign("OGroups" , $this->OGroups);
261 $smarty->assign("Actions" , $this->Actions);
262 $smarty->assign("Target_list" , $divlist->DrawList());
263 $smarty->assign("new" , $this->new);
264 return ($smarty->fetch (get_template_path('goto_task.tpl', TRUE)));
265 }
268 function create_tree($arr,$base,$current = "")
269 {
270 $ret = array();
271 foreach($arr as $r => $name){
272 $base_part = str_replace($base,"",$r);
273 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
274 $ret[$r] = $current.$name;
275 $tmp = $this->create_tree($arr,$r,$current.". ");
276 foreach($tmp as $sub_key => $sub_name){
277 $ret[$sub_key] = $sub_name;
278 }
279 }
280 }
281 return($ret);
282 }
286 function get_dhcp_sections()
287 {
288 $ldap = $this->config->get_ldap_link();
289 $ldap->cd($this->config->current['BASE']);
290 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
292 $tmp = array();
293 $tmp2 = array();
295 $dhcp_dns = array();
296 while($attr = $ldap->fetch()){
297 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
298 }
300 foreach($dhcp_dns as $key => $pri_dns){
301 $ldap->cat($pri_dns,array("cn"));
302 $tmp = $ldap->fetch();
303 $dhcp_dns[$key] = $tmp['cn'][0];
304 }
306 foreach($dhcp_dns as $dn => $cn){
307 $ldap->cd($dn);
308 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
309 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
310 $tmp = array();
311 while($attr = $ldap->fetch()){
312 $tmp[$attr['dn']] = $attr['cn'][0];
313 }
314 $tmp2 = $this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") ");
315 }
316 $ret = array();
317 foreach($tmp2 as $key => $label){
318 $ret[$tmp[$key]] = $label;
319 }
320 return($ret);
321 }
324 /* check given values */
325 function check()
326 {
327 $message = plugin::check();
328 $tmp = array(
329 "OGroup" => _("Object group") ,"Minute" => _("Minute"),
330 "Hour" => _("Hour") ,"Day" => _("Day"),
331 "Month" => _("Month") ,"Weekday"=> _("Week day"),
332 "Action" => _("Action") ,"Comment"=> _("Description"));
334 foreach($tmp as $name => $desc){
335 if(empty($this->$name) && $this->$name != 0){
336 $message[] = sprintf(_("The given value for attribute '%s' is invalid."),$desc);
337 }
338 }
339 if(count($this->Target) == 0 && $this->Action != "initial_install"){
340 $message[] = sprintf(_("You must specify at least one target"));
341 }
342 if(count($this->Initial_Target) == 0 && $this->Action == "initial_install"){
343 $message[] = sprintf(_("You must specify at least one target"));
344 }
345 return($message);
346 }
349 /* Map acl function, to be able to use plugin::save_object() */
350 function acl_is_writeable($attribute,$skip_write = FALSE)
351 {
352 return($this->parent->acl_is_writeable($attribute,$skip_write));
353 }
356 function save_object()
357 {
358 if(isset($_POST['goto_task_posted'])){
360 if($this->Action == "initial_install"){
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->Zone = "\"\"";
368 $this->configure_dns = FALSE;
369 }
371 if(isset($_POST['configure_dhcp'])){
372 $this->configure_dhcp = TRUE;
373 if(isset($_POST['Section'])){
374 $this->Section = get_post("Section");
375 }
376 }else{
377 $this->configure_dhcp = FALSE;
378 $this->Section = "\"\"";
379 }
380 }
381 plugin::save_object();
382 }
383 }
386 /* Check if given target is vald.
387 * It must either be a valid MAC address or an existing object group
388 */
389 function is_valid_target($str)
390 {
391 if(is_mac($str)){
392 return(TRUE);
393 }else{
394 $ldap = $this->config->get_ldap_link();
395 $ldap->cd($this->config->current['BASE']);
396 $ldap->search("(&(objectClassgosaGroupOfNames)(cn=".$str."))",array("cn"));
397 if($ldap->count()){
398 return(TRUE);
399 }
400 }
401 }
403 function save()
404 {
405 $tmp = array();
406 foreach($this->attributes as $attr){
407 $tmp[$attr] = $this->$attr;
408 }
410 if($this->Action != "initial_install" || !$this->configure_dns){
411 $tmp['Zone'] = "\"\"";
412 }
413 if($this->Action != "initial_install" || !$this->configure_dhcp){
414 $tmp['Section'] = "\"\"";
415 }
416 return($tmp);
417 }
420 /* Return values for listboxes.
421 */
422 function get_array_values()
423 {
424 $ret = array();
426 /* Create minute array */
427 $Minute = array( "*" => "*",
428 "*/1" => "*/1",
429 "*/3" => "*/3",
430 "*/5" => "*/5",
431 "*/10" => "*/10",
432 "*/15" => "*/15",
433 "*/30" => "*/30",
434 "*/45" => "*/45",
435 "*/60" => "*/60");
436 for($i = 0; $i < 60 ; $i ++){
437 $Minute[$i] = $i;
438 }
440 /* Create hour array */
441 $Hour = array( "*" => "*");
442 for($i = 1 ; $i < 24 ; $i ++ ){
443 $Hour["*/".$i] = "*/".$i;
444 }
445 for($i = 0 ; $i < 24 ; $i ++ ){
446 $Hour[$i] = $i;
447 }
449 /* Create hour array */
450 $Day = array( "*" => "*");
451 for($i = 1 ; $i < 32 ; $i ++ ){
452 $Day["*/".$i] = "*/".$i;
453 }
454 for($i = 1 ; $i < 32 ; $i ++ ){
455 $Day[$i] = $i;
456 }
458 /* Create month array */
459 $Month = array( "*" => "*");
460 for($i = 1 ; $i <= 12 ; $i ++ ){
461 $Month["*/".$i] = "*/".$i;
462 }
463 for($i = 1 ; $i <= 12 ; $i ++ ){
464 $Month[$i] = $i;
465 }
467 /* Create week day array */
468 $Weekday = array( "*" => "*");
469 for($i = 1 ; $i <= 7 ; $i ++ ){
470 $Weekday["*/".$i] = "*/".$i;
471 }
472 for($i = 0 ; $i <= 7 ; $i ++ ){
473 $Weekday[$i] = $i;
474 }
476 foreach(array("Minute","Weekday","Hour","Day","Month") as $var){
477 $ret[$var] = $$var;
478 }
479 return($ret);
480 }
481 }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>