Code

d30870d5fcf3443b512f0a60f1acc14e87091866
[gosa.git] / gosa-plugins / goto / admin / devices / class_deviceManagement.inc
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 ="";
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         }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         foreach($ids as $id){
232           $dn = $this->devices[$id]['dn'];
233           if (($user= get_lock($dn)) != ""){
234             return(gen_locked_message ($user, $dn));
235           }
236           $this->dns[$id] = $dn;
237         }
239         $dns_names = array();
240         foreach($this->dns as $dn){
241           add_lock ($dn, $this->ui->dn);
242           $dns_names[] =@LDAP::fix($dn);
243         }
245         /* Lock the current entry, so nobody will edit it during deletion */
246         $smarty->assign("intro", msgPool::deleteInfo($dns_names),_("device"));
247         $smarty->assign("multiple", true);
248         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
249       }
250     }
253     /********************
254       Delete MULTIPLE entries confirmed
255      ********************/
257     /* Confirmation for deletion has been passed. Users should be deleted. */
258     if (isset($_POST['delete_multiple_device_confirm'])){
260       $ui = get_userinfo();
262       /* Remove user by user and check acls before removeing them */
263       foreach($this->dns as $key => $dn){
265         $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
266         if(preg_match("/d/",$acl)){
268           /* Delete request is permitted, perform LDAP action */
269           $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
270           $this->devicetabs->set_acl_base($dn);
271           $this->devicetabs->delete ();
272           unset ($this->devicetabs);
273           $this->devicetabs= NULL;
275         } else {
276           /* Normally this shouldn't be reached, send some extra
277              logs to notify the administrator */
278           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
279           new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
280         }
281         /* Remove lock file after successfull deletion */
282         del_lock ($dn);
283         unset($this->dns[$key]);
284       }
285     }
288     /********************
289       Delete MULTIPLE entries Canceled
290      ********************/
292     /* Remove lock */
293     if(isset($_POST['delete_multiple_device_cancel'])){
294       foreach($this->dns as $key => $dn){
295         del_lock ($dn);
296         unset($this->dns[$key]);
297       }
298     }
301     /****************
302       Delete device type
303      ****************/
305     /* Remove user was requested */
306     if ($s_action == "del"){
308       /* Get 'dn' from posted 'uid' */
309       $this->dn= $this->devices[$s_entry]['dn'];
311       /* Load permissions for selected 'dn' and check if
312          we're allowed to remove this 'dn' */
313       $ui = get_userinfo();
314       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
315       if (preg_match("/d/",$acl)){
317         /* Check locking, save current plugin in 'back_plugin', so
318            the dialog knows where to return. */
319         if (($user= get_lock($this->dn)) != ""){
320           return (gen_locked_message ($user, $this->dn));
321         }
323         /* Lock the current entry, so nobody will edit it during deletion */
324         add_lock ($this->dn, $this->ui->dn);
325         $smarty= get_smarty();
326         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("device")));
327         $smarty->assign("multiple", false);
328         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
329       } else {
331         /* Obviously the user isn't allowed to delete. Show message and
332            clean session. */
333           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
334       }
335     }
338     /****************
339       Delete device confirmed
340      ****************/
342     /* Confirmation for deletion has been passed. Group should be deleted. */
343     if (isset($_POST['delete_device_confirm'])){
345       /* Some nice guy may send this as POST, so we've to check
346          for the permissions again. */
347       $ui = get_userinfo();
348       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
349       if(preg_match("/d/",$acl)){
351         /* Delete request is permitted, perform LDAP action */
352         $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
353         $this->devicetabs->set_acl_base($this->dn);
354         $this->devicetabs->delete ();
355         unset ($this->devicetabs);
356         $this->devicetabs= NULL;
358       } else {
360         /* Normally this shouldn't be reached, send some extra
361            logs to notify the administrator */
362         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
363         new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
364       }
366       /* Remove lock file after successfull deletion */
367       del_lock ($this->dn);
368     }
371     /****************
372       Delete device canceled
373      ****************/
375     /* Delete device canceled? */
376     if (isset($_POST['delete_cancel'])){
377       del_lock ($this->dn);
378       session::un_set('objectinfo');
379   }
381     /* Show tab dialog if object is present */
382     if (($this->devicetabs) && (isset($this->devicetabs->config))){
383       $display= $this->devicetabs->execute();
385       /* Don't show buttons if tab dialog requests this */
386       if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
387         $display.= "<p style=\"text-align:right\">\n";
388         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
389         $display.= "&nbsp;\n";
390         if ($this->dn != "new"){
391           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
392           $display.= "&nbsp;\n";
393         }
394         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
395         $display.= "</p>";
396       }
397       return ($display);
398     }
401     /****************
402       Dialog display
403      ****************/
405         /* Check if there is a snapshot dialog open */
406     $base = $this->DivListDevices->selectedBase;
407     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
408       return($str);
409     }
411     /* Display dialog with system list */
412     $this->DivListDevices->parent = $this;
413     $this->DivListDevices->execute();
414     $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
415     $this->reload();
416     $this->DivListDevices->setEntries($this->devices);
417     return($this->DivListDevices->Draw());
419   }
421   function save_object() {
422     $this->DivListDevices->save_object();
423     if(is_object($this->CopyPasteHandler)){
424       $this->CopyPasteHandler->save_object();
425     }
426   }
429   /* Return departments, that will be included within snapshot detection */
430   function get_used_snapshot_bases()
431   {
432     return(array(get_ou('deviceou').$this->DivListDevices->selectedBase));
433   }
435   function copyPasteHandling_from_queue($s_action,$s_entry)
436   {
437     /* Check if Copy & Paste is disabled */
438     if(!is_object($this->CopyPasteHandler)){
439       return("");
440     }
442     /* Add a single entry to queue */
443     if($s_action == "cut" || $s_action == "copy"){
445       /* Cleanup object queue */
446       $this->CopyPasteHandler->cleanup_queue();
447       $dn = $this->devices[$s_entry]['dn'];
448       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
449     }
451     /* Add entries to queue */
452     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
454       /* Cleanup object queue */
455       $this->CopyPasteHandler->cleanup_queue();
457       /* Add new entries to CP queue */
458       foreach($this->list_get_selected_items() as $id){
459         $dn = $this->devices[$id]['dn'];
461         if($s_action == "copy_multiple"){
462           $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
463         }
464         if($s_action == "cut_multiple"){
465           $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
466         }
467       }
468     }
470     /* Start pasting entries */
471     if($s_action == "editPaste"){
472       $this->start_pasting_copied_objects = TRUE;
473     }
475     /* Return C&P dialog */
476     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
478       /* Get dialog */
479       $data = $this->CopyPasteHandler->execute();
480       $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
482       /* Return dialog data */
483       if(!empty($data)){
484         return($data);
485       }
486     }
488     /* Automatically disable status for pasting */
489     if(!$this->CopyPasteHandler->entries_queued()){
490       $this->start_pasting_copied_objects = FALSE;
491     }
492     return("");
493   }
497   function reload()
498   {
499     /* Set base for all searches */
500     $base       = $this->DivListDevices->selectedBase;
501     $Regex      = $this->DivListDevices->Regex;
502     $SubSearch  = $this->DivListDevices->SubSearch;
503     $Flags      =  GL_NONE | GL_SIZELIMIT;
504     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
505     $tmp        = array();
507     /* In case of subsearch, add the subsearch flag */
508     if($SubSearch){
509       $Flags    |= GL_SUBSEARCH;
510     }else{
511       $base = get_ou('deviceou').$base;
512     }
514     /* Get results and create index */
515     $res= get_sub_list($Filter,"devices",get_ou('deviceou'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
516     $tmp2 = array();
517     foreach ($res as $val){
518       if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
519         $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
520         if ($dsc != ""){
521           $val['description']= array("count" => 1, 0 => $dsc);
522         }
523       }
524       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
525       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
526     }
528     /* sort entries */
529     natcasesort($tmp2);
530     $this->devices=array();
531     foreach($tmp2 as $val){
532       $this->devices[]=$tmp[$val];
533     }
534     reset ($this->devices);
535   }
538   function remove_lock()
539   {
540     if (isset($this->devicetabs->dn)){
541       del_lock ($this->devicetabs->dn);
542     }
543   }
545   function list_get_selected_items()
546   {
547     $ids = array();
548     foreach($_POST as $name => $value){
549       if(preg_match("/^item_selected_[0-9]*$/",$name)){
550         $id   = preg_replace("/^item_selected_/","",$name);
551         $ids[$id] = $id;
552       }
553     }
554     return($ids);
555   }
558   function remove_from_parent()
559   {
560     /* This cannot be removed... */
561   }
563 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
564 ?>