7f2e02171774b5512d865770bea1a5deb55cc8de
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,TRUE));
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 */
407 if(($this->devicetabs instanceOf tabs || $this->devicetabs instanceOf plugin) && $this->devicetabs->read_only == TRUE){
408 $display.= "<p style=\"text-align:right\">
409 <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
410 </p>";
411 }elseif (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
412 $display.= "<p style=\"text-align:right\">\n";
413 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
414 $display.= " \n";
415 if ($this->dn != "new"){
416 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
417 $display.= " \n";
418 }
419 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
420 $display.= "</p>";
421 }
422 return ($display);
423 }
426 /****************
427 Dialog display
428 ****************/
430 /* Check if there is a snapshot dialog open */
431 $base = $this->DivListDevices->selectedBase;
432 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
433 return($str);
434 }
436 /* Display dialog with system list */
437 $this->DivListDevices->parent = $this;
438 $this->DivListDevices->execute();
439 $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
440 $this->reload();
441 $this->DivListDevices->setEntries($this->devices);
442 return($this->DivListDevices->Draw());
444 }
446 function save_object() {
447 $this->DivListDevices->save_object();
448 if(is_object($this->CopyPasteHandler)){
449 $this->CopyPasteHandler->save_object();
450 }
451 }
454 /* Return departments, that will be included within snapshot detection */
455 function get_used_snapshot_bases()
456 {
457 return(array(get_ou('deviceRDN').$this->DivListDevices->selectedBase));
458 }
460 function copyPasteHandling_from_queue($s_action,$s_entry)
461 {
462 /* Check if Copy & Paste is disabled */
463 if(!is_object($this->CopyPasteHandler)){
464 return("");
465 }
467 $ui = get_userinfo();
469 /* Add a single entry to queue */
470 if($s_action == "cut" || $s_action == "copy"){
472 /* Cleanup object queue */
473 $this->CopyPasteHandler->cleanup_queue();
474 $dn = $this->devices[$s_entry]['dn'];
475 if($s_action == "copy" && $ui->is_copyable($dn,"devices","deviceGeneric")){
476 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
477 }
478 if($s_action == "cut" && $ui->is_cutable($dn,"devices","deviceGeneric")){
479 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
480 }
481 }
483 /* Add entries to queue */
484 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
486 /* Cleanup object queue */
487 $this->CopyPasteHandler->cleanup_queue();
489 /* Add new entries to CP queue */
490 foreach($this->list_get_selected_items() as $id){
491 $dn = $this->devices[$id]['dn'];
493 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"devices","deviceGeneric")){
494 $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
495 }
496 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"devices","deviceGeneric")){
497 $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
498 }
499 }
500 }
502 /* Start pasting entries */
503 if($s_action == "editPaste"){
504 $this->start_pasting_copied_objects = TRUE;
505 }
507 /* Return C&P dialog */
508 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
510 /* Get dialog */
511 $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
512 $data = $this->CopyPasteHandler->execute();
514 /* Return dialog data */
515 if(!empty($data)){
516 return($data);
517 }
518 }
520 /* Automatically disable status for pasting */
521 if(!$this->CopyPasteHandler->entries_queued()){
522 $this->start_pasting_copied_objects = FALSE;
523 }
524 return("");
525 }
529 function reload()
530 {
531 /* Set base for all searches */
532 $base = $this->DivListDevices->selectedBase;
533 $Regex = $this->DivListDevices->Regex;
534 $SubSearch = $this->DivListDevices->SubSearch;
535 $Flags = GL_NONE | GL_SIZELIMIT;
536 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
537 $tmp = array();
539 /* In case of subsearch, add the subsearch flag */
540 if($SubSearch){
541 $Flags |= GL_SUBSEARCH;
542 }else{
543 $base = get_ou('deviceRDN').$base;
544 }
546 /* Get results and create index */
547 $res= get_sub_list($Filter,"devices",get_ou('deviceRDN'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
548 $tmp2 = array();
549 foreach ($res as $val){
550 if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
551 $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
552 if ($dsc != ""){
553 $val['description']= array("count" => 1, 0 => $dsc);
554 }
555 }
556 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
557 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
558 }
560 /* sort entries */
561 natcasesort($tmp2);
562 $this->devices=array();
563 foreach($tmp2 as $val){
564 $this->devices[]=$tmp[$val];
565 }
566 reset ($this->devices);
567 }
570 function remove_lock()
571 {
572 if (isset($this->dn) && !empty($this->dn)){
573 del_lock ($this->dn);
574 }
575 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
576 del_lock ($this->dns);
577 }
578 }
580 function list_get_selected_items()
581 {
582 $ids = array();
583 foreach($_POST as $name => $value){
584 if(preg_match("/^item_selected_[0-9]*$/",$name)){
585 $id = preg_replace("/^item_selected_/","",$name);
586 $ids[$id] = $id;
587 }
588 }
589 return($ids);
590 }
593 function remove_from_parent()
594 {
595 /* This cannot be removed... */
596 }
597 }
598 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>