1 <?php
3 class deviceManagement extends plugin
4 {
6 /* Definitions */
7 var $plHeadline = "Hotplug devices";
8 var $plDescription = "Manage hotplug devices";
10 /* Dialog attributes */
11 var $ui = NULL;
12 var $DivListDevices = NULL;
13 var $enableReleaseManagement = false;
14 var $devicetabs = NULL;
15 var $snapDialog = NULL;
16 var $CopyPasteHandler = NULL;
17 var $start_pasting_copied_objects;
18 var $dn ="";
19 var $dns = array();
21 var $acl_module = array("devices");
23 function deviceManagement(&$config, $dn= NULL)
24 {
25 plugin::plugin ($config, $dn);
26 $this->ui = get_userinfo();
28 /* Check if copy & paste is activated */
29 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
30 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
31 }
33 /* Creat dialog object */
34 $this->DivListDevices = new divListDevices($this->config,$this);
35 }
38 function execute()
39 {
40 /* Call parent execute */
41 plugin::execute();
43 /****************
44 Variable init
45 ****************/
47 /* These vars will be stored if you try to open a locked device,
48 to be able to perform your last requests after showing a warning message */
49 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^device_edit_/","/^menu_action/",
50 "/^device_del_/","/^item_selected/","/^remove_multiple_devices/"));
52 $smarty = get_smarty(); // Smarty instance
53 $s_action = ""; // Contains the action to proceed
54 $s_entry = ""; // The value for s_action
55 $base_back = ""; // The Link for Backbutton
57 /* Test Posts */
58 foreach($_POST as $key => $val){
60 if(preg_match("/device_del.*/",$key)){
61 $s_action = "del";
62 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
63 }elseif(preg_match("/device_edit_.*/",$key)){
64 $s_action="edit";
65 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
66 }elseif(preg_match("/^copy_.*/",$key)){
67 $s_action="copy";
68 $s_entry = preg_replace("/^copy_/i","",$key);
69 }elseif(preg_match("/^cut_.*/",$key)){
70 $s_action="cut";
71 $s_entry = preg_replace("/^cut_/i","",$key);
72 }elseif(preg_match("/^device_new.*/",$key)){
73 $s_action="new";
74 }elseif(preg_match("/^remove_multiple_devices/",$key)){
75 $s_action="del_multiple";
76 }elseif(preg_match("/^editPaste.*/i",$key)){
77 $s_action="editPaste";
78 }elseif(preg_match("/^multiple_copy_devices/",$key)){
79 $s_action = "copy_multiple";
80 }elseif(preg_match("/^multiple_cut_devices/",$key)){
81 $s_action = "cut_multiple";
82 }
83 }
85 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
86 $s_action ="edit";
87 $s_entry = $_GET['id'];
88 }
90 $s_entry = preg_replace("/_.$/","",$s_entry);
93 /* handle C&P from layers menu */
94 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
95 $s_action = "copy_multiple";
96 }
97 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
98 $s_action = "cut_multiple";
99 }
100 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
101 $s_action = "editPaste";
102 }
104 /* Create options */
105 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "device_new"){
106 $s_action = "new";
107 }
109 /* handle remove from layers menu */
110 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
111 $s_action = "del_multiple";
112 }
114 /****************
115 Copy & Paste handling
116 ****************/
118 /* Display the copy & paste dialog, if it is currently open */
119 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
120 if($ret){
121 return($ret);
122 }
124 /****************
125 Create a new device type
126 ****************/
128 /* New device type? */
129 $ui = get_userinfo();
130 $acl = $ui->get_permissions($this->DivListDevices->selectedBase,"devices/deviceGeneric");
131 if (($s_action=="new") && preg_match("/c/",$acl)){
133 /* By default we set 'dn' to 'new', all relevant plugins will
134 react on this. */
135 $this->dn= "new";
137 /* Create new usertab object */
138 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
139 $this->devicetabs->set_acl_base($this->DivListDevices->selectedBase);
140 }
143 /****************
144 Edit entry canceled
145 ****************/
147 /* Cancel dialogs */
148 if (isset($_POST['edit_cancel']) && is_object($this->devicetabs)){
149 $this->remove_lock();
150 $this->devicetabs= NULL;
151 session::un_set('objectinfo');
152 }
155 /****************
156 Edit entry finished
157 ****************/
159 /* Finish device edit is triggered by the tabulator dialog, so
160 the user wants to save edited data. Check and save at this point. */
161 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->devicetabs->config))){
163 /* Check tabs, will feed message array */
164 $this->devicetabs->save_object();
165 $message= $this->devicetabs->check();
167 /* Save, or display error message? */
168 if (count($message) == 0){
170 /* Save data data to ldap */
171 $this->devicetabs->save();
173 if (!isset($_POST['edit_apply'])){
175 /* device type has been saved successfully, remove lock from LDAP. */
176 if ($this->dn != "new"){
177 $this->remove_lock();
178 }
179 unset ($this->devicetabs);
180 $this->devicetabs= NULL;
181 session::un_set('objectinfo');
182 }else{
184 /* Reinitialize tab */
185 if($this->devicetabs instanceof tabs){
186 $this->devicetabs->re_init();
187 }
188 }
189 } else {
190 /* Ok. There seem to be errors regarding to the tab data,
191 show message and continue as usual. */
192 msg_dialog::displayChecks($message);
193 }
194 }
197 /****************
198 Edit entry
199 ****************/
201 /* User wants to edit data? */
202 if (($s_action=="edit") && (!isset($this->devicetabs->config))){
204 /* Get 'dn' from posted 'devicelist', must be unique */
205 $this->dn= $this->devices[$s_entry]['dn'];
207 /* Check locking, save current plugin in 'back_plugin', so
208 the dialog knows where to return. */
209 if (($user= get_lock($this->dn)) != ""){
210 return(gen_locked_message ($user, $this->dn));
211 }
213 /* Lock the current entry, so everyone will get the
214 above dialog */
215 add_lock ($this->dn, $this->ui->dn);
218 /* Register devicetabs to trigger edit dialog */
219 $this->devicetabs= new devicetabs($this->config,$this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
220 $this->devicetabs->set_acl_base($this->dn);
221 session::set('objectinfo',$this->dn);
222 }
225 /********************
226 Delete MULTIPLE entries requested, display confirm dialog
227 ********************/
228 if ($s_action=="del_multiple"){
229 $ids = $this->list_get_selected_items();
231 if(count($ids)){
233 $disallowed = array();
234 foreach($ids as $id){
235 $dn = $this->devices[$id]['dn'];
236 $acl = $this->ui->get_permissions($dn, "devices/deviceGeneric");
237 if(preg_match("/d/",$acl)){
238 $this->dns[$id] = $dn;
239 }else{
240 $disallowed[] = $dn;
241 }
242 }
244 if(count($disallowed)){
245 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
246 }
248 if(count($this->dns)){
250 /* Check locks */
251 if ($user= get_multiple_locks($this->dns)){
252 return(gen_locked_message($user,$this->dns));
253 }
255 $dns_names = array();
256 foreach($this->dns as $dn){
257 $dns_names[] =@LDAP::fix($dn);
258 }
259 add_lock ($this->dns, $this->ui->dn);
261 /* Lock the current entry, so nobody will edit it during deletion */
262 $smarty->assign("intro", msgPool::deleteInfo($dns_names),_("device"));
263 $smarty->assign("multiple", true);
264 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
265 }
266 }
267 }
270 /********************
271 Delete MULTIPLE entries confirmed
272 ********************/
274 /* Confirmation for deletion has been passed. Users should be deleted. */
275 if (isset($_POST['delete_multiple_device_confirm'])){
277 $ui = get_userinfo();
279 /* Remove user by user and check acls before removeing them */
280 foreach($this->dns as $key => $dn){
282 $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
283 if(preg_match("/d/",$acl)){
285 /* Delete request is permitted, perform LDAP action */
286 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
287 $this->devicetabs->set_acl_base($dn);
288 $this->devicetabs->delete ();
289 unset ($this->devicetabs);
290 $this->devicetabs= NULL;
292 } else {
293 /* Normally this shouldn't be reached, send some extra
294 logs to notify the administrator */
295 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
296 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
297 }
298 }
300 /* Remove lock file after successfull deletion */
301 $this->remove_lock();
302 $this->dns = array();
303 }
306 /********************
307 Delete MULTIPLE entries Canceled
308 ********************/
310 /* Remove lock */
311 if(isset($_POST['delete_multiple_device_cancel'])){
313 /* Remove lock file after successfull deletion */
314 $this->remove_lock();
315 $this->dns = array();
316 }
319 /****************
320 Delete device type
321 ****************/
323 /* Remove user was requested */
324 if ($s_action == "del"){
326 /* Get 'dn' from posted 'uid' */
327 $this->dn= $this->devices[$s_entry]['dn'];
329 /* Load permissions for selected 'dn' and check if
330 we're allowed to remove this 'dn' */
331 $ui = get_userinfo();
332 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
333 if (preg_match("/d/",$acl)){
335 /* Check locking, save current plugin in 'back_plugin', so
336 the dialog knows where to return. */
337 if (($user= get_lock($this->dn)) != ""){
338 return (gen_locked_message ($user, $this->dn));
339 }
341 /* Lock the current entry, so nobody will edit it during deletion */
342 add_lock ($this->dn, $this->ui->dn);
343 $smarty= get_smarty();
344 $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("device")));
345 $smarty->assign("multiple", false);
346 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
347 } else {
349 /* Obviously the user isn't allowed to delete. Show message and
350 clean session. */
351 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
352 }
353 }
356 /****************
357 Delete device confirmed
358 ****************/
360 /* Confirmation for deletion has been passed. Group should be deleted. */
361 if (isset($_POST['delete_device_confirm'])){
363 /* Some nice guy may send this as POST, so we've to check
364 for the permissions again. */
365 $ui = get_userinfo();
366 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
367 if(preg_match("/d/",$acl)){
369 /* Delete request is permitted, perform LDAP action */
370 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
371 $this->devicetabs->set_acl_base($this->dn);
372 $this->devicetabs->delete ();
373 unset ($this->devicetabs);
374 $this->devicetabs= NULL;
376 } else {
378 /* Normally this shouldn't be reached, send some extra
379 logs to notify the administrator */
380 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
381 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
382 }
384 /* Remove lock file after successfull deletion */
385 $this->remove_lock();
386 $this->dns = array();
387 }
390 /****************
391 Delete device canceled
392 ****************/
394 /* Delete device canceled? */
395 if (isset($_POST['delete_cancel'])){
396 $this->remove_lock();
397 session::un_set('objectinfo');
398 }
400 /* Show tab dialog if object is present */
401 if (($this->devicetabs) && (isset($this->devicetabs->config))){
402 $display= $this->devicetabs->execute();
404 /* Don't show buttons if tab dialog requests this */
405 if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
406 $display.= "<p style=\"text-align:right\">\n";
407 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
408 $display.= " \n";
409 if ($this->dn != "new"){
410 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
411 $display.= " \n";
412 }
413 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
414 $display.= "</p>";
415 }
416 return ($display);
417 }
420 /****************
421 Dialog display
422 ****************/
424 /* Check if there is a snapshot dialog open */
425 $base = $this->DivListDevices->selectedBase;
426 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
427 return($str);
428 }
430 /* Display dialog with system list */
431 $this->DivListDevices->parent = $this;
432 $this->DivListDevices->execute();
433 $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
434 $this->reload();
435 $this->DivListDevices->setEntries($this->devices);
436 return($this->DivListDevices->Draw());
438 }
440 function save_object() {
441 $this->DivListDevices->save_object();
442 if(is_object($this->CopyPasteHandler)){
443 $this->CopyPasteHandler->save_object();
444 }
445 }
448 /* Return departments, that will be included within snapshot detection */
449 function get_used_snapshot_bases()
450 {
451 return(array(get_ou('deviceou').$this->DivListDevices->selectedBase));
452 }
454 function copyPasteHandling_from_queue($s_action,$s_entry)
455 {
456 /* Check if Copy & Paste is disabled */
457 if(!is_object($this->CopyPasteHandler)){
458 return("");
459 }
461 $ui = get_userinfo();
463 /* Add a single entry to queue */
464 if($s_action == "cut" || $s_action == "copy"){
466 /* Cleanup object queue */
467 $this->CopyPasteHandler->cleanup_queue();
468 $dn = $this->devices[$s_entry]['dn'];
469 if($s_action == "copy" && $ui->is_copyable($dn,"devices","deviceGeneric")){
470 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
471 }
472 if($s_action == "cut" && $ui->is_cutable($dn,"devices","deviceGeneric")){
473 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
474 }
475 }
477 /* Add entries to queue */
478 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
480 /* Cleanup object queue */
481 $this->CopyPasteHandler->cleanup_queue();
483 /* Add new entries to CP queue */
484 foreach($this->list_get_selected_items() as $id){
485 $dn = $this->devices[$id]['dn'];
487 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"devices","deviceGeneric")){
488 $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
489 }
490 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"devices","deviceGeneric")){
491 $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
492 }
493 }
494 }
496 /* Start pasting entries */
497 if($s_action == "editPaste"){
498 $this->start_pasting_copied_objects = TRUE;
499 }
501 /* Return C&P dialog */
502 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
504 /* Get dialog */
505 $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
506 $data = $this->CopyPasteHandler->execute();
508 /* Return dialog data */
509 if(!empty($data)){
510 return($data);
511 }
512 }
514 /* Automatically disable status for pasting */
515 if(!$this->CopyPasteHandler->entries_queued()){
516 $this->start_pasting_copied_objects = FALSE;
517 }
518 return("");
519 }
523 function reload()
524 {
525 /* Set base for all searches */
526 $base = $this->DivListDevices->selectedBase;
527 $Regex = $this->DivListDevices->Regex;
528 $SubSearch = $this->DivListDevices->SubSearch;
529 $Flags = GL_NONE | GL_SIZELIMIT;
530 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
531 $tmp = array();
533 /* In case of subsearch, add the subsearch flag */
534 if($SubSearch){
535 $Flags |= GL_SUBSEARCH;
536 }else{
537 $base = get_ou('deviceou').$base;
538 }
540 /* Get results and create index */
541 $res= get_sub_list($Filter,"devices",get_ou('deviceou'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
542 $tmp2 = array();
543 foreach ($res as $val){
544 if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
545 $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
546 if ($dsc != ""){
547 $val['description']= array("count" => 1, 0 => $dsc);
548 }
549 }
550 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
551 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
552 }
554 /* sort entries */
555 natcasesort($tmp2);
556 $this->devices=array();
557 foreach($tmp2 as $val){
558 $this->devices[]=$tmp[$val];
559 }
560 reset ($this->devices);
561 }
564 function remove_lock()
565 {
566 if (isset($this->dn) && !empty($this->dn)){
567 del_lock ($this->dn);
568 }
569 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
570 del_lock ($this->dns);
571 }
572 }
574 function list_get_selected_items()
575 {
576 $ids = array();
577 foreach($_POST as $name => $value){
578 if(preg_match("/^item_selected_[0-9]*$/",$name)){
579 $id = preg_replace("/^item_selected_/","",$name);
580 $ids[$id] = $id;
581 }
582 }
583 return($ids);
584 }
587 function remove_from_parent()
588 {
589 /* This cannot be removed... */
590 }
591 }
592 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
593 ?>