1 <?php
3 class deviceManagement extends plugin
4 {
6 /* Definitions */
7 var $plHeadline = "Hotplug devices";
8 var $plDescription = "Manage hotplug devices";
9 var $plIcon = "plugins/goto/images/devices.png";
11 /* Dialog attributes */
12 var $ui = NULL;
13 var $DivListDevices = NULL;
14 var $enableReleaseManagement = false;
15 var $devicetabs = NULL;
16 var $snapDialog = NULL;
17 var $CopyPasteHandler = NULL;
18 var $start_pasting_copied_objects;
19 var $dn ="";
20 var $dns = array();
22 var $acl_module = array("devices");
24 function deviceManagement(&$config, $dn= NULL)
25 {
26 plugin::plugin ($config, $dn);
27 $this->ui = get_userinfo();
29 /* Check if copy & paste is activated */
30 if($this->config->get_cfg_value("copyPaste") == "true"){
31 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
32 }
34 /* Creat dialog object */
35 $this->DivListDevices = new divListDevices($this->config,$this);
36 }
39 function execute()
40 {
41 /* Call parent execute */
42 plugin::execute();
44 /****************
45 Variable init
46 ****************/
48 /* These vars will be stored if you try to open a locked device,
49 to be able to perform your last requests after showing a warning message */
50 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^device_edit_/","/^menu_action/",
51 "/^device_del_/","/^item_selected/","/^remove_multiple_devices/"));
53 $smarty = get_smarty(); // Smarty instance
54 $s_action = ""; // Contains the action to proceed
55 $s_entry = ""; // The value for s_action
56 $base_back = ""; // The Link for Backbutton
58 /* Test Posts */
59 foreach($_POST as $key => $val){
61 if(preg_match("/device_del.*/",$key)){
62 $s_action = "del";
63 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
64 }elseif(preg_match("/device_edit_.*/",$key)){
65 $s_action="edit";
66 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
67 }elseif(preg_match("/^copy_.*/",$key)){
68 $s_action="copy";
69 $s_entry = preg_replace("/^copy_/i","",$key);
70 }elseif(preg_match("/^cut_.*/",$key)){
71 $s_action="cut";
72 $s_entry = preg_replace("/^cut_/i","",$key);
73 }elseif(preg_match("/^device_new.*/",$key)){
74 $s_action="new";
75 }elseif(preg_match("/^remove_multiple_devices/",$key)){
76 $s_action="del_multiple";
77 }elseif(preg_match("/^editPaste.*/i",$key)){
78 $s_action="editPaste";
79 }elseif(preg_match("/^multiple_copy_devices/",$key)){
80 $s_action = "copy_multiple";
81 }elseif(preg_match("/^multiple_cut_devices/",$key)){
82 $s_action = "cut_multiple";
83 }
84 }
86 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
87 $s_action ="edit";
88 $s_entry = $_GET['id'];
89 }
91 $s_entry = preg_replace("/_.$/","",$s_entry);
94 /* handle C&P from layers menu */
95 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
96 $s_action = "copy_multiple";
97 }
98 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
99 $s_action = "cut_multiple";
100 }
101 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
102 $s_action = "editPaste";
103 }
105 /* Create options */
106 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "device_new"){
107 $s_action = "new";
108 }
110 /* handle remove from layers menu */
111 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
112 $s_action = "del_multiple";
113 }
115 /****************
116 Copy & Paste handling
117 ****************/
119 /* Display the copy & paste dialog, if it is currently open */
120 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
121 if($ret){
122 return($ret);
123 }
125 /****************
126 Create a new device type
127 ****************/
129 /* New device type? */
130 $ui = get_userinfo();
131 $acl = $ui->get_permissions($this->DivListDevices->selectedBase,"devices/deviceGeneric");
132 if (($s_action=="new") && preg_match("/c/",$acl)){
134 /* By default we set 'dn' to 'new', all relevant plugins will
135 react on this. */
136 $this->dn= "new";
138 /* Create new usertab object */
139 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
140 $this->devicetabs->set_acl_base($this->DivListDevices->selectedBase);
141 }
144 /****************
145 Edit entry canceled
146 ****************/
148 /* Cancel dialogs */
149 if (isset($_POST['edit_cancel']) && is_object($this->devicetabs)){
150 $this->remove_lock();
151 $this->devicetabs= NULL;
152 session::un_set('objectinfo');
153 }
156 /****************
157 Edit entry finished
158 ****************/
160 /* Finish device edit is triggered by the tabulator dialog, so
161 the user wants to save edited data. Check and save at this point. */
162 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->devicetabs->config))){
164 /* Check tabs, will feed message array */
165 $this->devicetabs->save_object();
166 $message= $this->devicetabs->check();
168 /* Save, or display error message? */
169 if (count($message) == 0){
171 /* Save data data to ldap */
172 $this->devicetabs->save();
174 if (!isset($_POST['edit_apply'])){
176 /* device type has been saved successfully, remove lock from LDAP. */
177 if ($this->dn != "new"){
178 $this->remove_lock();
179 }
180 unset ($this->devicetabs);
181 $this->devicetabs= NULL;
182 session::un_set('objectinfo');
183 }else{
185 /* Reinitialize tab */
186 if($this->devicetabs instanceof tabs){
187 $this->devicetabs->re_init();
188 }
189 }
190 } else {
191 /* Ok. There seem to be errors regarding to the tab data,
192 show message and continue as usual. */
193 msg_dialog::displayChecks($message);
194 }
195 }
198 /****************
199 Edit entry
200 ****************/
202 /* User wants to edit data? */
203 if (($s_action=="edit") && (!isset($this->devicetabs->config))){
205 /* Get 'dn' from posted 'devicelist', must be unique */
206 $this->dn= $this->devices[$s_entry]['dn'];
208 /* Check locking, save current plugin in 'back_plugin', so
209 the dialog knows where to return. */
210 if (($user= get_lock($this->dn)) != ""){
211 return(gen_locked_message ($user, $this->dn));
212 }
214 /* Lock the current entry, so everyone will get the
215 above dialog */
216 add_lock ($this->dn, $this->ui->dn);
219 /* Register devicetabs to trigger edit dialog */
220 $this->devicetabs= new devicetabs($this->config,$this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
221 $this->devicetabs->set_acl_base($this->dn);
222 session::set('objectinfo',$this->dn);
223 }
226 /********************
227 Delete MULTIPLE entries requested, display confirm dialog
228 ********************/
229 if ($s_action=="del_multiple"){
230 $ids = $this->list_get_selected_items();
232 if(count($ids)){
234 $disallowed = array();
235 foreach($ids as $id){
236 $dn = $this->devices[$id]['dn'];
237 $acl = $this->ui->get_permissions($dn, "devices/deviceGeneric");
238 if(preg_match("/d/",$acl)){
239 $this->dns[$id] = $dn;
240 }else{
241 $disallowed[] = $dn;
242 }
243 }
245 if(count($disallowed)){
246 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
247 }
249 if(count($this->dns)){
251 /* Check locks */
252 if ($user= get_multiple_locks($this->dns)){
253 return(gen_locked_message($user,$this->dns));
254 }
256 $dns_names = array();
257 foreach($this->dns as $dn){
258 $dns_names[] =LDAP::fix($dn);
259 }
260 add_lock ($this->dns, $this->ui->dn);
262 /* Lock the current entry, so nobody will edit it during deletion */
263 $smarty->assign("intro", msgPool::deleteInfo($dns_names),_("device"));
264 $smarty->assign("multiple", true);
265 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
266 }
267 }
268 }
271 /********************
272 Delete MULTIPLE entries confirmed
273 ********************/
275 /* Confirmation for deletion has been passed. Users should be deleted. */
276 if (isset($_POST['delete_multiple_device_confirm'])){
278 $ui = get_userinfo();
280 /* Remove user by user and check acls before removeing them */
281 foreach($this->dns as $key => $dn){
283 $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
284 if(preg_match("/d/",$acl)){
286 /* Delete request is permitted, perform LDAP action */
287 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
288 $this->devicetabs->set_acl_base($dn);
289 $this->devicetabs->delete ();
290 unset ($this->devicetabs);
291 $this->devicetabs= NULL;
293 } else {
294 /* Normally this shouldn't be reached, send some extra
295 logs to notify the administrator */
296 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
297 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
298 }
299 }
301 /* Remove lock file after successfull deletion */
302 $this->remove_lock();
303 $this->dns = array();
304 }
307 /********************
308 Delete MULTIPLE entries Canceled
309 ********************/
311 /* Remove lock */
312 if(isset($_POST['delete_multiple_device_cancel'])){
314 /* Remove lock file after successfull deletion */
315 $this->remove_lock();
316 $this->dns = array();
317 }
320 /****************
321 Delete device type
322 ****************/
324 /* Remove user was requested */
325 if ($s_action == "del"){
327 /* Get 'dn' from posted 'uid' */
328 $this->dn= $this->devices[$s_entry]['dn'];
330 /* Load permissions for selected 'dn' and check if
331 we're allowed to remove this 'dn' */
332 $ui = get_userinfo();
333 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
334 if (preg_match("/d/",$acl)){
336 /* Check locking, save current plugin in 'back_plugin', so
337 the dialog knows where to return. */
338 if (($user= get_lock($this->dn)) != ""){
339 return (gen_locked_message ($user, $this->dn));
340 }
342 /* Lock the current entry, so nobody will edit it during deletion */
343 add_lock ($this->dn, $this->ui->dn);
344 $smarty= get_smarty();
345 $smarty->assign("intro", msgPool::deleteInfo(LDAP::fix($this->dn),_("device")));
346 $smarty->assign("multiple", false);
347 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
348 } else {
350 /* Obviously the user isn't allowed to delete. Show message and
351 clean session. */
352 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
353 }
354 }
357 /****************
358 Delete device confirmed
359 ****************/
361 /* Confirmation for deletion has been passed. Group should be deleted. */
362 if (isset($_POST['delete_device_confirm'])){
364 /* Some nice guy may send this as POST, so we've to check
365 for the permissions again. */
366 $ui = get_userinfo();
367 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
368 if(preg_match("/d/",$acl)){
370 /* Delete request is permitted, perform LDAP action */
371 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
372 $this->devicetabs->set_acl_base($this->dn);
373 $this->devicetabs->delete ();
374 unset ($this->devicetabs);
375 $this->devicetabs= NULL;
377 } else {
379 /* Normally this shouldn't be reached, send some extra
380 logs to notify the administrator */
381 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
382 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
383 }
385 /* Remove lock file after successfull deletion */
386 $this->remove_lock();
387 $this->dns = array();
388 }
391 /****************
392 Delete device canceled
393 ****************/
395 /* Delete device canceled? */
396 if (isset($_POST['delete_cancel'])){
397 $this->remove_lock();
398 session::un_set('objectinfo');
399 }
401 /* Show tab dialog if object is present */
402 if (($this->devicetabs) && (isset($this->devicetabs->config))){
403 $display= $this->devicetabs->execute();
405 /* Don't show buttons if tab dialog requests this */
406 if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
407 $display.= "<p style=\"text-align:right\">\n";
408 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
409 $display.= " \n";
410 if ($this->dn != "new"){
411 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
412 $display.= " \n";
413 }
414 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
415 $display.= "</p>";
416 }
417 return ($display);
418 }
421 /****************
422 Dialog display
423 ****************/
425 /* Check if there is a snapshot dialog open */
426 $base = $this->DivListDevices->selectedBase;
427 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
428 return($str);
429 }
431 /* Display dialog with system list */
432 $this->DivListDevices->parent = $this;
433 $this->DivListDevices->execute();
434 $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
435 $this->reload();
436 $this->DivListDevices->setEntries($this->devices);
437 return($this->DivListDevices->Draw());
439 }
441 function save_object() {
442 $this->DivListDevices->save_object();
443 if(is_object($this->CopyPasteHandler)){
444 $this->CopyPasteHandler->save_object();
445 }
446 }
449 /* Return departments, that will be included within snapshot detection */
450 function get_used_snapshot_bases()
451 {
452 return(array(get_ou('deviceRDN').$this->DivListDevices->selectedBase));
453 }
455 function copyPasteHandling_from_queue($s_action,$s_entry)
456 {
457 /* Check if Copy & Paste is disabled */
458 if(!is_object($this->CopyPasteHandler)){
459 return("");
460 }
462 $ui = get_userinfo();
464 /* Add a single entry to queue */
465 if($s_action == "cut" || $s_action == "copy"){
467 /* Cleanup object queue */
468 $this->CopyPasteHandler->cleanup_queue();
469 $dn = $this->devices[$s_entry]['dn'];
470 if($s_action == "copy" && $ui->is_copyable($dn,"devices","deviceGeneric")){
471 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
472 }
473 if($s_action == "cut" && $ui->is_cutable($dn,"devices","deviceGeneric")){
474 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
475 }
476 }
478 /* Add entries to queue */
479 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
481 /* Cleanup object queue */
482 $this->CopyPasteHandler->cleanup_queue();
484 /* Add new entries to CP queue */
485 foreach($this->list_get_selected_items() as $id){
486 $dn = $this->devices[$id]['dn'];
488 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"devices","deviceGeneric")){
489 $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
490 }
491 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"devices","deviceGeneric")){
492 $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
493 }
494 }
495 }
497 /* Start pasting entries */
498 if($s_action == "editPaste"){
499 $this->start_pasting_copied_objects = TRUE;
500 }
502 /* Return C&P dialog */
503 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
505 /* Get dialog */
506 $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
507 $data = $this->CopyPasteHandler->execute();
509 /* Return dialog data */
510 if(!empty($data)){
511 return($data);
512 }
513 }
515 /* Automatically disable status for pasting */
516 if(!$this->CopyPasteHandler->entries_queued()){
517 $this->start_pasting_copied_objects = FALSE;
518 }
519 return("");
520 }
524 function reload()
525 {
526 /* Set base for all searches */
527 $base = $this->DivListDevices->selectedBase;
528 $Regex = $this->DivListDevices->Regex;
529 $SubSearch = $this->DivListDevices->SubSearch;
530 $Flags = GL_NONE | GL_SIZELIMIT;
531 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
532 $tmp = array();
534 /* In case of subsearch, add the subsearch flag */
535 if($SubSearch){
536 $Flags |= GL_SUBSEARCH;
537 }else{
538 $base = get_ou('deviceRDN').$base;
539 }
541 /* Get results and create index */
542 $res= get_sub_list($Filter,"devices",get_ou('deviceRDN'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
543 $tmp2 = array();
544 foreach ($res as $val){
545 if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
546 $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
547 if ($dsc != ""){
548 $val['description']= array("count" => 1, 0 => $dsc);
549 }
550 }
551 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
552 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
553 }
555 /* sort entries */
556 natcasesort($tmp2);
557 $this->devices=array();
558 foreach($tmp2 as $val){
559 $this->devices[]=$tmp[$val];
560 }
561 reset ($this->devices);
562 }
565 function remove_lock()
566 {
567 if (isset($this->dn) && !empty($this->dn)){
568 del_lock ($this->dn);
569 }
570 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
571 del_lock ($this->dns);
572 }
573 }
575 function list_get_selected_items()
576 {
577 $ids = array();
578 foreach($_POST as $name => $value){
579 if(preg_match("/^item_selected_[0-9]*$/",$name)){
580 $id = preg_replace("/^item_selected_/","",$name);
581 $ids[$id] = $id;
582 }
583 }
584 return($ids);
585 }
588 function remove_from_parent()
589 {
590 /* This cannot be removed... */
591 }
592 }
593 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
594 ?>