1 <?php
3 class deviceManagement extends plugin
4 {
6 /* Definitions */
7 var $plHeadline = "Devices";
8 var $plDescription = "Manage 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 ="";
20 function deviceManagement(&$config, $dn= NULL)
21 {
22 plugin::plugin ($config, $dn);
23 $this->ui = get_userinfo();
25 /* Check if copy & paste is activated */
26 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
27 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
28 }
30 /* Creat dialog object */
31 $this->DivListDevices = new divListDevices($this->config,$this);
32 }
35 function execute()
36 {
37 /* Call parent execute */
38 plugin::execute();
40 /****************
41 Variable init
42 ****************/
44 /* These vars will be stored if you try to open a locked device,
45 to be able to perform your last requests after showing a warning message */
46 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^device_edit_/",
47 "/^device_del_/","/^item_selected/","/^remove_multiple_devices/"));
49 $smarty = get_smarty(); // Smarty instance
50 $s_action = ""; // Contains the action to proceed
51 $s_entry = ""; // The value for s_action
52 $base_back = ""; // The Link for Backbutton
54 /* Test Posts */
55 foreach($_POST as $key => $val){
57 if(preg_match("/device_del.*/",$key)){
58 $s_action = "del";
59 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
60 }elseif(preg_match("/device_edit_.*/",$key)){
61 $s_action="edit";
62 $s_entry = preg_replace("/device_".$s_action."_/i","",$key);
63 }elseif(preg_match("/^copy_.*/",$key)){
64 $s_action="copy";
65 $s_entry = preg_replace("/^copy_/i","",$key);
66 }elseif(preg_match("/^cut_.*/",$key)){
67 $s_action="cut";
68 $s_entry = preg_replace("/^cut_/i","",$key);
69 }elseif(preg_match("/^device_new.*/",$key)){
70 $s_action="new";
71 }elseif(preg_match("/^remove_multiple_devices/",$key)){
72 $s_action="del_multiple";
73 }elseif(preg_match("/^editPaste.*/i",$key)){
74 $s_action="editPaste";
75 }elseif(preg_match("/^multiple_copy_devices/",$key)){
76 $s_action = "copy_multiple";
77 }elseif(preg_match("/^multiple_cut_devices/",$key)){
78 $s_action = "cut_multiple";
79 }
80 }
82 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
83 $s_action ="edit";
84 $s_entry = $_GET['id'];
85 }
87 $s_entry = preg_replace("/_.$/","",$s_entry);
90 /* handle C&P from layers menu */
91 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
92 $s_action = "copy_multiple";
93 }
94 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
95 $s_action = "cut_multiple";
96 }
97 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
98 $s_action = "editPaste";
99 }
101 /* Create options */
102 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "device_new"){
103 $s_action = "new";
104 }
106 /* handle remove from layers menu */
107 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
108 $s_action = "del_multiple";
109 }
111 /****************
112 Copy & Paste handling
113 ****************/
115 /* Display the copy & paste dialog, if it is currently open */
116 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
117 if($ret){
118 return($ret);
119 }
121 /****************
122 Create a new device type
123 ****************/
125 /* New device type? */
126 $ui = get_userinfo();
127 $acl = $ui->get_permissions($this->DivListDevices->selectedBase,"devices/deviceGeneric");
128 if (($s_action=="new") && preg_match("/c/",$acl)){
130 /* By default we set 'dn' to 'new', all relevant plugins will
131 react on this. */
132 $this->dn= "new";
134 /* Create new usertab object */
135 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
136 $this->devicetabs->set_acl_base($this->DivListDevices->selectedBase);
137 }
140 /****************
141 Edit entry canceled
142 ****************/
144 /* Cancel dialogs */
145 if (isset($_POST['edit_cancel']) && is_object($this->devicetabs)){
146 del_lock ($this->devicetabs->dn);
147 unset ($this->devicetabs);
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->set_release($this->DivListDevices->selectedRelease);
170 $this->devicetabs->save();
172 if (!isset($_POST['edit_apply'])){
173 /* device type has been saved successfully, remove lock from LDAP. */
174 if ($this->dn != "new"){
175 del_lock ($this->dn);
176 }
177 unset ($this->devicetabs);
178 $this->devicetabs= NULL;
179 session::un_set('objectinfo');
180 }
181 } else {
182 /* Ok. There seem to be errors regarding to the tab data,
183 show message and continue as usual. */
184 show_errors($message);
185 }
186 }
189 /****************
190 Edit entry
191 ****************/
193 /* User wants to edit data? */
194 if (($s_action=="edit") && (!isset($this->devicetabs->config))){
196 /* Get 'dn' from posted 'devicelist', must be unique */
197 $this->dn= $this->devices[$s_entry]['dn'];
199 /* Check locking, save current plugin in 'back_plugin', so
200 the dialog knows where to return. */
201 if (($user= get_lock($this->dn)) != ""){
202 return(gen_locked_message ($user, $this->dn));
203 }
205 /* Lock the current entry, so everyone will get the
206 above dialog */
207 add_lock ($this->dn, $this->ui->dn);
210 /* Register devicetabs to trigger edit dialog */
211 $this->devicetabs= new devicetabs($this->config,$this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
212 $this->devicetabs->set_acl_base($this->dn);
213 session::set('objectinfo',$this->dn);
214 }
217 /********************
218 Delete MULTIPLE entries requested, display confirm dialog
219 ********************/
220 if ($s_action=="del_multiple"){
221 $ids = $this->list_get_selected_items();
223 if(count($ids)){
225 foreach($ids as $id){
226 $dn = $this->devices[$id]['dn'];
227 if (($user= get_lock($dn)) != ""){
228 return(gen_locked_message ($user, $dn));
229 }
230 $this->dns[$id] = $dn;
231 }
233 $dns_names = "<br><pre>";
234 foreach($this->dns as $dn){
235 add_lock ($dn, $this->ui->dn);
236 $dns_names .= $dn."\n";
237 }
238 $dns_names .="</pre>";
240 /* Lock the current entry, so nobody will edit it during deletion */
241 if (count($this->dns) == 1){
242 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
243 } else {
244 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
245 }
246 $smarty->assign("multiple", true);
247 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
248 }
249 }
252 /********************
253 Delete MULTIPLE entries confirmed
254 ********************/
256 /* Confirmation for deletion has been passed. Users should be deleted. */
257 if (isset($_POST['delete_multiple_device_confirm'])){
259 $ui = get_userinfo();
261 /* Remove user by user and check acls before removeing them */
262 foreach($this->dns as $key => $dn){
264 $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
265 if(preg_match("/d/",$acl)){
267 /* Delete request is permitted, perform LDAP action */
268 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
269 $this->devicetabs->set_acl_base($dn);
270 $this->devicetabs->delete ();
271 unset ($this->devicetabs);
272 $this->devicetabs= NULL;
274 } else {
275 /* Normally this shouldn't be reached, send some extra
276 logs to notify the administrator */
277 print_red (_("You are not allowed to delete this device type!"));
278 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
279 }
280 /* Remove lock file after successfull deletion */
281 del_lock ($dn);
282 unset($this->dns[$key]);
283 }
284 }
287 /********************
288 Delete MULTIPLE entries Canceled
289 ********************/
291 /* Remove lock */
292 if(isset($_POST['delete_multiple_device_cancel'])){
293 foreach($this->dns as $key => $dn){
294 del_lock ($dn);
295 unset($this->dns[$key]);
296 }
297 }
300 /****************
301 Delete device type
302 ****************/
304 /* Remove user was requested */
305 if ($s_action == "del"){
307 /* Get 'dn' from posted 'uid' */
308 $this->dn= $this->devices[$s_entry]['dn'];
310 /* Load permissions for selected 'dn' and check if
311 we're allowed to remove this 'dn' */
312 $ui = get_userinfo();
313 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
314 if (preg_match("/d/",$acl)){
316 /* Check locking, save current plugin in 'back_plugin', so
317 the dialog knows where to return. */
318 if (($user= get_lock($this->dn)) != ""){
319 return (gen_locked_message ($user, $this->dn));
320 }
322 /* Lock the current entry, so nobody will edit it during deletion */
323 add_lock ($this->dn, $this->ui->dn);
324 $smarty= get_smarty();
325 $smarty->assign("intro", sprintf(_("You're about to delete the device '%s'."), @LDAP::fix($this->dn)));
326 $smarty->assign("multiple", false);
327 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
328 } else {
330 /* Obviously the user isn't allowed to delete. Show message and
331 clean session. */
332 print_red (_("You are not allowed to delete this device!"));
333 }
334 }
337 /****************
338 Delete device confirmed
339 ****************/
341 /* Confirmation for deletion has been passed. Group should be deleted. */
342 if (isset($_POST['delete_device_confirm'])){
344 /* Some nice guy may send this as POST, so we've to check
345 for the permissions again. */
346 $ui = get_userinfo();
347 $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
348 if(preg_match("/d/",$acl)){
350 /* Delete request is permitted, perform LDAP action */
351 $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
352 $this->devicetabs->set_acl_base($this->dn);
353 $this->devicetabs->delete ();
354 unset ($this->devicetabs);
355 $this->devicetabs= NULL;
357 } else {
359 /* Normally this shouldn't be reached, send some extra
360 logs to notify the administrator */
361 print_red (_("You are not allowed to delete this device!"));
362 new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
363 }
365 /* Remove lock file after successfull deletion */
366 del_lock ($this->dn);
367 }
370 /****************
371 Delete device canceled
372 ****************/
374 /* Delete device canceled? */
375 if (isset($_POST['delete_cancel'])){
376 del_lock ($this->dn);
377 session::un_set('objectinfo');
378 }
380 /* Show tab dialog if object is present */
381 if (($this->devicetabs) && (isset($this->devicetabs->config))){
382 $display= $this->devicetabs->execute();
384 /* Don't show buttons if tab dialog requests this */
385 if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
386 $display.= "<p style=\"text-align:right\">\n";
387 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
388 $display.= " \n";
389 if ($this->dn != "new"){
390 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
391 $display.= " \n";
392 }
393 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
394 $display.= "</p>";
395 }
396 return ($display);
397 }
400 /****************
401 Dialog display
402 ****************/
404 /* Check if there is a snapshot dialog open */
405 $base = $this->DivListDevices->selectedBase;
406 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
407 return($str);
408 }
410 /* Display dialog with system list */
411 $this->DivListDevices->parent = $this;
412 $this->DivListDevices->execute();
413 $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
414 $this->reload();
415 $this->DivListDevices->setEntries($this->devices);
416 return($this->DivListDevices->Draw());
418 }
420 function save_object() {
421 $this->DivListDevices->save_object();
422 }
425 /* Return departments, that will be included within snapshot detection */
426 function get_used_snapshot_bases()
427 {
428 return(array(get_ou('deviceou').$this->DivListDevices->selectedBase));
429 }
431 function copyPasteHandling_from_queue($s_action,$s_entry)
432 {
433 /* Check if Copy & Paste is disabled */
434 if(!is_object($this->CopyPasteHandler)){
435 return("");
436 }
438 /* Add a single entry to queue */
439 if($s_action == "cut" || $s_action == "copy"){
441 /* Cleanup object queue */
442 $this->CopyPasteHandler->cleanup_queue();
443 $dn = $this->devices[$s_entry]['dn'];
444 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
445 }
447 /* Add entries to queue */
448 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
450 /* Cleanup object queue */
451 $this->CopyPasteHandler->cleanup_queue();
453 /* Add new entries to CP queue */
454 foreach($this->list_get_selected_items() as $id){
455 $dn = $this->devices[$id]['dn'];
457 if($s_action == "copy_multiple"){
458 $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
459 }
460 if($s_action == "cut_multiple"){
461 $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
462 }
463 }
464 }
466 /* Start pasting entries */
467 if($s_action == "editPaste"){
468 $this->start_pasting_copied_objects = TRUE;
469 }
471 /* Return C&P dialog */
472 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
474 /* Get dialog */
475 $data = $this->CopyPasteHandler->execute();
476 $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
478 /* Return dialog data */
479 if(!empty($data)){
480 return($data);
481 }
482 }
484 /* Automatically disable status for pasting */
485 if(!$this->CopyPasteHandler->entries_queued()){
486 $this->start_pasting_copied_objects = FALSE;
487 }
488 return("");
489 }
493 function reload()
494 {
495 /* Set base for all searches */
496 $base = $this->DivListDevices->selectedBase;
497 $Regex = $this->DivListDevices->Regex;
498 $SubSearch = $this->DivListDevices->SubSearch;
499 $Flags = GL_NONE | GL_SIZELIMIT;
500 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
501 $tmp = array();
503 /* In case of subsearch, add the subsearch flag */
504 if($SubSearch){
505 $Flags |= GL_SUBSEARCH;
506 }else{
507 $base = get_ou('deviceou').$base;
508 }
510 /* Get results and create index */
511 $res= get_sub_list($Filter,"devices",get_ou('deviceou'), $base, array("cn","description","dn","objectClass"), $Flags);
512 $tmp2 = array();
513 foreach ($res as $val){
514 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
515 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
516 }
518 /* sort entries */
519 natcasesort($tmp2);
520 $this->devices=array();
521 foreach($tmp2 as $val){
522 $this->devices[]=$tmp[$val];
523 }
524 reset ($this->devices);
525 }
528 function remove_lock()
529 {
530 if (isset($this->devicetabs->dn)){
531 del_lock ($this->devicetabs->dn);
532 }
533 }
535 function list_get_selected_items()
536 {
537 $ids = array();
538 foreach($_POST as $name => $value){
539 if(preg_match("/^item_selected_[0-9]*$/",$name)){
540 $id = preg_replace("/^item_selected_/","",$name);
541 $ids[$id] = $id;
542 }
543 }
544 return($ids);
545 }
548 function remove_from_parent()
549 {
550 /* This cannot be removed... */
551 }
552 }
553 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
554 ?>