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 function deviceManagement(&$config, $dn= NULL)
22 {
23 plugin::plugin ($config, $dn);
24 $this->ui = get_userinfo();
26 /* Check if copy & paste is activated */
27 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
28 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
29 }
31 /* Creat dialog object */
32 $this->DivListDevices = new divListDevices($this->config,$this);
33 }
36 function execute()
37 {
38 /* Call parent execute */
39 plugin::execute();
41 /****************
42 Variable init
43 ****************/
45 /* These vars will be stored if you try to open a locked device,
46 to be able to perform your last requests after showing a warning message */
47 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^device_edit_/","/^menu_action/",
48 "/^device_del_/","/^item_selected/","/^remove_multiple_devices/"));
50 $smarty = get_smarty(); // Smarty instance
51 $s_action = ""; // Contains the action to proceed
52 $s_entry = ""; // The value for s_action
53 $base_back = ""; // The Link for Backbutton
55 /* Test Posts */
56 foreach($_POST as $key => $val){
58 if(preg_match("/device_del.*/",$key)){
59 $s_action = "del";
60 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
61 }elseif(preg_match("/device_edit_.*/",$key)){
62 $s_action="edit";
63 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
64 }elseif(preg_match("/^copy_.*/",$key)){
65 $s_action="copy";
66 $s_entry = preg_replace("/^copy_/i","",$key);
67 }elseif(preg_match("/^cut_.*/",$key)){
68 $s_action="cut";
69 $s_entry = preg_replace("/^cut_/i","",$key);
70 }elseif(preg_match("/^device_new.*/",$key)){
71 $s_action="new";
72 }elseif(preg_match("/^remove_multiple_devices/",$key)){
73 $s_action="del_multiple";
74 }elseif(preg_match("/^editPaste.*/i",$key)){
75 $s_action="editPaste";
76 }elseif(preg_match("/^multiple_copy_devices/",$key)){
77 $s_action = "copy_multiple";
78 }elseif(preg_match("/^multiple_cut_devices/",$key)){
79 $s_action = "cut_multiple";
80 }
81 }
83 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
84 $s_action ="edit";
85 $s_entry = $_GET['id'];
86 }
88 $s_entry = preg_replace("/_.$/","",$s_entry);
91 /* handle C&P from layers menu */
92 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
93 $s_action = "copy_multiple";
94 }
95 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
96 $s_action = "cut_multiple";
97 }
98 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
99 $s_action = "editPaste";
100 }
102 /* Create options */
103 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "device_new"){
104 $s_action = "new";
105 }
107 /* handle remove from layers menu */
108 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
109 $s_action = "del_multiple";
110 }
112 /****************
113 Copy & Paste handling
114 ****************/
116 /* Display the copy & paste dialog, if it is currently open */
117 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
118 if($ret){
119 return($ret);
120 }
122 /****************
123 Create a new device type
124 ****************/
126 /* New device type? */
127 $ui = get_userinfo();
128 $acl = $ui->get_permissions($this->DivListDevices->selectedBase,"devices/deviceGeneric");
129 if (($s_action=="new") && preg_match("/c/",$acl)){
131 /* By default we set 'dn' to 'new', all relevant plugins will
132 react on this. */
133 $this->dn= "new";
135 /* Create new usertab object */
136 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
137 $this->devicetabs->set_acl_base($this->DivListDevices->selectedBase);
138 }
141 /****************
142 Edit entry canceled
143 ****************/
145 /* Cancel dialogs */
146 if (isset($_POST['edit_cancel']) && is_object($this->devicetabs)){
147 $this->remove_lock();
148 $this->devicetabs= NULL;
149 session::un_set('objectinfo');
150 }
153 /****************
154 Edit entry finished
155 ****************/
157 /* Finish device edit is triggered by the tabulator dialog, so
158 the user wants to save edited data. Check and save at this point. */
159 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->devicetabs->config))){
161 /* Check tabs, will feed message array */
162 $this->devicetabs->save_object();
163 $message= $this->devicetabs->check();
165 /* Save, or display error message? */
166 if (count($message) == 0){
168 /* Save data data to ldap */
169 $this->devicetabs->save();
171 if (!isset($_POST['edit_apply'])){
173 /* device type has been saved successfully, remove lock from LDAP. */
174 if ($this->dn != "new"){
175 $this->remove_lock();
176 }
177 unset ($this->devicetabs);
178 $this->devicetabs= NULL;
179 session::un_set('objectinfo');
180 }else{
182 /* Reinitialize tab */
183 if($this->devicetabs instanceof tabs){
184 $this->devicetabs->re_init();
185 }
186 }
187 } else {
188 /* Ok. There seem to be errors regarding to the tab data,
189 show message and continue as usual. */
190 msg_dialog::displayChecks($message);
191 }
192 }
195 /****************
196 Edit entry
197 ****************/
199 /* User wants to edit data? */
200 if (($s_action=="edit") && (!isset($this->devicetabs->config))){
202 /* Get 'dn' from posted 'devicelist', must be unique */
203 $this->dn= $this->devices[$s_entry]['dn'];
205 /* Check locking, save current plugin in 'back_plugin', so
206 the dialog knows where to return. */
207 if (($user= get_lock($this->dn)) != ""){
208 return(gen_locked_message ($user, $this->dn));
209 }
211 /* Lock the current entry, so everyone will get the
212 above dialog */
213 add_lock ($this->dn, $this->ui->dn);
216 /* Register devicetabs to trigger edit dialog */
217 $this->devicetabs= new devicetabs($this->config,$this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
218 $this->devicetabs->set_acl_base($this->dn);
219 session::set('objectinfo',$this->dn);
220 }
223 /********************
224 Delete MULTIPLE entries requested, display confirm dialog
225 ********************/
226 if ($s_action=="del_multiple"){
227 $ids = $this->list_get_selected_items();
229 if(count($ids)){
231 /* collect entries */
232 $this->dns = array();
233 foreach($ids as $id){
234 $this->dns[$id] = $this->devices[$id]['dn'];
235 }
237 /* Check locks */
238 if ($user= get_multiple_locks($this->dns)){
239 return(gen_locked_message($user,$this->dns));
240 }
242 $dns_names = array();
243 foreach($this->dns as $dn){
244 $dns_names[] =@LDAP::fix($dn);
245 }
246 add_lock ($this->dns, $this->ui->dn);
248 /* Lock the current entry, so nobody will edit it during deletion */
249 $smarty->assign("intro", msgPool::deleteInfo($dns_names),_("device"));
250 $smarty->assign("multiple", true);
251 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
252 }
253 }
256 /********************
257 Delete MULTIPLE entries confirmed
258 ********************/
260 /* Confirmation for deletion has been passed. Users should be deleted. */
261 if (isset($_POST['delete_multiple_device_confirm'])){
263 $ui = get_userinfo();
265 /* Remove user by user and check acls before removeing them */
266 foreach($this->dns as $key => $dn){
268 $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
269 if(preg_match("/d/",$acl)){
271 /* Delete request is permitted, perform LDAP action */
272 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
273 $this->devicetabs->set_acl_base($dn);
274 $this->devicetabs->delete ();
275 unset ($this->devicetabs);
276 $this->devicetabs= NULL;
278 } else {
279 /* Normally this shouldn't be reached, send some extra
280 logs to notify the administrator */
281 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
282 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
283 }
284 }
286 /* Remove lock file after successfull deletion */
287 $this->remove_lock();
288 $this->dns = array();
289 }
292 /********************
293 Delete MULTIPLE entries Canceled
294 ********************/
296 /* Remove lock */
297 if(isset($_POST['delete_multiple_device_cancel'])){
299 /* Remove lock file after successfull deletion */
300 $this->remove_lock();
301 $this->dns = array();
302 }
305 /****************
306 Delete device type
307 ****************/
309 /* Remove user was requested */
310 if ($s_action == "del"){
312 /* Get 'dn' from posted 'uid' */
313 $this->dn= $this->devices[$s_entry]['dn'];
315 /* Load permissions for selected 'dn' and check if
316 we're allowed to remove this 'dn' */
317 $ui = get_userinfo();
318 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
319 if (preg_match("/d/",$acl)){
321 /* Check locking, save current plugin in 'back_plugin', so
322 the dialog knows where to return. */
323 if (($user= get_lock($this->dn)) != ""){
324 return (gen_locked_message ($user, $this->dn));
325 }
327 /* Lock the current entry, so nobody will edit it during deletion */
328 add_lock ($this->dn, $this->ui->dn);
329 $smarty= get_smarty();
330 $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("device")));
331 $smarty->assign("multiple", false);
332 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
333 } else {
335 /* Obviously the user isn't allowed to delete. Show message and
336 clean session. */
337 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
338 }
339 }
342 /****************
343 Delete device confirmed
344 ****************/
346 /* Confirmation for deletion has been passed. Group should be deleted. */
347 if (isset($_POST['delete_device_confirm'])){
349 /* Some nice guy may send this as POST, so we've to check
350 for the permissions again. */
351 $ui = get_userinfo();
352 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
353 if(preg_match("/d/",$acl)){
355 /* Delete request is permitted, perform LDAP action */
356 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
357 $this->devicetabs->set_acl_base($this->dn);
358 $this->devicetabs->delete ();
359 unset ($this->devicetabs);
360 $this->devicetabs= NULL;
362 } else {
364 /* Normally this shouldn't be reached, send some extra
365 logs to notify the administrator */
366 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
367 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
368 }
370 /* Remove lock file after successfull deletion */
371 $this->remove_lock();
372 $this->dns = array();
373 }
376 /****************
377 Delete device canceled
378 ****************/
380 /* Delete device canceled? */
381 if (isset($_POST['delete_cancel'])){
382 $this->remove_lock();
383 session::un_set('objectinfo');
384 }
386 /* Show tab dialog if object is present */
387 if (($this->devicetabs) && (isset($this->devicetabs->config))){
388 $display= $this->devicetabs->execute();
390 /* Don't show buttons if tab dialog requests this */
391 if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
392 $display.= "<p style=\"text-align:right\">\n";
393 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
394 $display.= " \n";
395 if ($this->dn != "new"){
396 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
397 $display.= " \n";
398 }
399 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
400 $display.= "</p>";
401 }
402 return ($display);
403 }
406 /****************
407 Dialog display
408 ****************/
410 /* Check if there is a snapshot dialog open */
411 $base = $this->DivListDevices->selectedBase;
412 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
413 return($str);
414 }
416 /* Display dialog with system list */
417 $this->DivListDevices->parent = $this;
418 $this->DivListDevices->execute();
419 $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
420 $this->reload();
421 $this->DivListDevices->setEntries($this->devices);
422 return($this->DivListDevices->Draw());
424 }
426 function save_object() {
427 $this->DivListDevices->save_object();
428 if(is_object($this->CopyPasteHandler)){
429 $this->CopyPasteHandler->save_object();
430 }
431 }
434 /* Return departments, that will be included within snapshot detection */
435 function get_used_snapshot_bases()
436 {
437 return(array(get_ou('deviceou').$this->DivListDevices->selectedBase));
438 }
440 function copyPasteHandling_from_queue($s_action,$s_entry)
441 {
442 /* Check if Copy & Paste is disabled */
443 if(!is_object($this->CopyPasteHandler)){
444 return("");
445 }
447 /* Add a single entry to queue */
448 if($s_action == "cut" || $s_action == "copy"){
450 /* Cleanup object queue */
451 $this->CopyPasteHandler->cleanup_queue();
452 $dn = $this->devices[$s_entry]['dn'];
453 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
454 }
456 /* Add entries to queue */
457 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
459 /* Cleanup object queue */
460 $this->CopyPasteHandler->cleanup_queue();
462 /* Add new entries to CP queue */
463 foreach($this->list_get_selected_items() as $id){
464 $dn = $this->devices[$id]['dn'];
466 if($s_action == "copy_multiple"){
467 $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
468 }
469 if($s_action == "cut_multiple"){
470 $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
471 }
472 }
473 }
475 /* Start pasting entries */
476 if($s_action == "editPaste"){
477 $this->start_pasting_copied_objects = TRUE;
478 }
480 /* Return C&P dialog */
481 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
483 /* Get dialog */
484 $data = $this->CopyPasteHandler->execute();
485 $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
487 /* Return dialog data */
488 if(!empty($data)){
489 return($data);
490 }
491 }
493 /* Automatically disable status for pasting */
494 if(!$this->CopyPasteHandler->entries_queued()){
495 $this->start_pasting_copied_objects = FALSE;
496 }
497 return("");
498 }
502 function reload()
503 {
504 /* Set base for all searches */
505 $base = $this->DivListDevices->selectedBase;
506 $Regex = $this->DivListDevices->Regex;
507 $SubSearch = $this->DivListDevices->SubSearch;
508 $Flags = GL_NONE | GL_SIZELIMIT;
509 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
510 $tmp = array();
512 /* In case of subsearch, add the subsearch flag */
513 if($SubSearch){
514 $Flags |= GL_SUBSEARCH;
515 }else{
516 $base = get_ou('deviceou').$base;
517 }
519 /* Get results and create index */
520 $res= get_sub_list($Filter,"devices",get_ou('deviceou'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
521 $tmp2 = array();
522 foreach ($res as $val){
523 if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
524 $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
525 if ($dsc != ""){
526 $val['description']= array("count" => 1, 0 => $dsc);
527 }
528 }
529 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
530 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
531 }
533 /* sort entries */
534 natcasesort($tmp2);
535 $this->devices=array();
536 foreach($tmp2 as $val){
537 $this->devices[]=$tmp[$val];
538 }
539 reset ($this->devices);
540 }
543 function remove_lock()
544 {
545 if (isset($this->dn) && !empty($this->dn)){
546 del_lock ($this->dn);
547 }
548 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
549 del_lock ($this->dns);
550 }
551 }
553 function list_get_selected_items()
554 {
555 $ids = array();
556 foreach($_POST as $name => $value){
557 if(preg_match("/^item_selected_[0-9]*$/",$name)){
558 $id = preg_replace("/^item_selected_/","",$name);
559 $ids[$id] = $id;
560 }
561 }
562 return($ids);
563 }
566 function remove_from_parent()
567 {
568 /* This cannot be removed... */
569 }
570 }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>