Code

Added seperate list handler for gotomasses.
[gosa.git] / plugins / admin / devices / class_deviceManagement.inc
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['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     /****************
91       Copy & Paste handling
92      ****************/
94     /* Display the copy & paste dialog, if it is currently open */
95     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
96     if($ret){
97       return($ret);
98     }
101     /****************
102       Create a new device type
103      ****************/
105     /* New device type? */
106     $ui = get_userinfo();
107     $acl = $ui->get_permissions($this->DivListDevices->selectedBase,"devices/deviceGeneric");
108     if (($s_action=="new") && preg_match("/c/",$acl)){
110       /* By default we set 'dn' to 'new', all relevant plugins will
111          react on this. */
112       $this->dn= "new";
114       /* Create new usertab object */
115       $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
116       $this->devicetabs->set_acl_base($this->DivListDevices->selectedBase);
117     }
120     /****************
121       Edit entry canceled
122      ****************/
124     /* Cancel dialogs */
125     if (isset($_POST['edit_cancel'])){
126       del_lock ($this->devicetabs->dn);
127       unset ($this->devicetabs);
128       $this->devicetabs= NULL;
129       unset ($_SESSION['objectinfo']);
130     }
133     /****************
134       Edit entry finished
135      ****************/
137     /* Finish device edit is triggered by the tabulator dialog, so
138        the user wants to save edited data. Check and save at this point. */
139     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->devicetabs->config))){
141       /* Check tabs, will feed message array */
142       $this->devicetabs->save_object();
143       $message= $this->devicetabs->check();
145       /* Save, or display error message? */
146       if (count($message) == 0){
148         /* Save data data to ldap */
149 #        $this->devicetabs->set_release($this->DivListDevices->selectedRelease);
150         $this->devicetabs->save();
152         if (!isset($_POST['edit_apply'])){
153           /* device type has been saved successfully, remove lock from LDAP. */
154           if ($this->dn != "new"){
155             del_lock ($this->dn);
156           }
157           unset ($this->devicetabs);
158           $this->devicetabs= NULL;
159           unset ($_SESSION['objectinfo']);
160         }
161       } else {
162        /* Ok. There seem to be errors regarding to the tab data,
163            show message and continue as usual. */
164         show_errors($message);
165       }
166     }
169     /****************
170       Edit entry
171      ****************/
173     /* User wants to edit data? */
174     if (($s_action=="edit") && (!isset($this->devicetabs->config))){
176       /* Get 'dn' from posted 'devicelist', must be unique */
177       $this->dn= $this->devices[$s_entry]['dn'];
179       /* Check locking, save current plugin in 'back_plugin', so
180          the dialog knows where to return. */
181       if (($user= get_lock($this->dn)) != ""){
182         return(gen_locked_message ($user, $this->dn));
183       }
185       /* Lock the current entry, so everyone will get the
186          above dialog */
187       add_lock ($this->dn, $this->ui->dn);
190       /* Register devicetabs to trigger edit dialog */
191       $this->devicetabs= new devicetabs($this->config,$this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
192       $this->devicetabs->set_acl_base($this->dn);
193       $_SESSION['objectinfo']= $this->dn;
194     }
197     /********************
198       Delete MULTIPLE entries requested, display confirm dialog
199      ********************/
200     if ($s_action=="del_multiple"){
201       $ids = $this->list_get_selected_items();
203       if(count($ids)){
205         foreach($ids as $id){
206           $dn = $this->devices[$id]['dn'];
207           if (($user= get_lock($dn)) != ""){
208             return(gen_locked_message ($user, $dn));
209           }
210           $this->dns[$id] = $dn;
211         }
213         $dns_names = "<br><pre>";
214         foreach($this->dns as $dn){
215           add_lock ($dn, $this->ui->dn);
216           $dns_names .= $dn."\n";
217         }
218         $dns_names .="</pre>";
220         /* Lock the current entry, so nobody will edit it during deletion */
221         if (count($this->dns) == 1){
222           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
223         } else {
224           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
225         }
226         $smarty->assign("multiple", true);
227         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
228       }
229     }
232     /********************
233       Delete MULTIPLE entries confirmed
234      ********************/
236     /* Confirmation for deletion has been passed. Users should be deleted. */
237     if (isset($_POST['delete_multiple_device_confirm'])){
239       $ui = get_userinfo();
241       /* Remove user by user and check acls before removeing them */
242       foreach($this->dns as $key => $dn){
244         $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
245         if(preg_match("/d/",$acl)){
247           /* Delete request is permitted, perform LDAP action */
248           $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
249           $this->devicetabs->set_acl_base($dn);
250           $this->devicetabs->delete ();
251           unset ($this->devicetabs);
252           $this->devicetabs= NULL;
254         } else {
255           /* Normally this shouldn't be reached, send some extra
256              logs to notify the administrator */
257           print_red (_("You are not allowed to delete this device type!"));
258           new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
259         }
260         /* Remove lock file after successfull deletion */
261         del_lock ($dn);
262         unset($this->dns[$key]);
263       }
264     }
267     /********************
268       Delete MULTIPLE entries Canceled
269      ********************/
271     /* Remove lock */
272     if(isset($_POST['delete_multiple_device_cancel'])){
273       foreach($this->dns as $key => $dn){
274         del_lock ($dn);
275         unset($this->dns[$key]);
276       }
277     }
280     /****************
281       Delete device type
282      ****************/
284     /* Remove user was requested */
285     if ($s_action == "del"){
287       /* Get 'dn' from posted 'uid' */
288       $this->dn= $this->devices[$s_entry]['dn'];
290       /* Load permissions for selected 'dn' and check if
291          we're allowed to remove this 'dn' */
292       $ui = get_userinfo();
293       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
294       if (preg_match("/d/",$acl)){
296         /* Check locking, save current plugin in 'back_plugin', so
297            the dialog knows where to return. */
298         if (($user= get_lock($this->dn)) != ""){
299           return (gen_locked_message ($user, $this->dn));
300         }
302         /* Lock the current entry, so nobody will edit it during deletion */
303         add_lock ($this->dn, $this->ui->dn);
304         $smarty= get_smarty();
305         $smarty->assign("intro", sprintf(_("You're about to delete the device '%s'."), @LDAP::fix($this->dn)));
306         $smarty->assign("multiple", false);
307         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
308       } else {
310         /* Obviously the user isn't allowed to delete. Show message and
311            clean session. */
312        print_red (_("You are not allowed to delete this device!"));
313       }
314     }
317     /****************
318       Delete device confirmed
319      ****************/
321     /* Confirmation for deletion has been passed. Group should be deleted. */
322     if (isset($_POST['delete_device_confirm'])){
324       /* Some nice guy may send this as POST, so we've to check
325          for the permissions again. */
326       $ui = get_userinfo();
327       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
328       if(preg_match("/d/",$acl)){
330         /* Delete request is permitted, perform LDAP action */
331         $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
332         $this->devicetabs->set_acl_base($this->dn);
333         $this->devicetabs->delete ();
334         unset ($this->devicetabs);
335         $this->devicetabs= NULL;
337       } else {
339         /* Normally this shouldn't be reached, send some extra
340            logs to notify the administrator */
341         print_red (_("You are not allowed to delete this device!"));
342         new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
343       }
345       /* Remove lock file after successfull deletion */
346       del_lock ($this->dn);
347     }
350     /****************
351       Delete device canceled
352      ****************/
354     /* Delete device canceled? */
355     if (isset($_POST['delete_cancel'])){
356       del_lock ($this->dn);
357       unset($_SESSION['objectinfo']);
358   }
360     /* Show tab dialog if object is present */
361     if (($this->devicetabs) && (isset($this->devicetabs->config))){
362       $display= $this->devicetabs->execute();
364       /* Don't show buttons if tab dialog requests this */
365       if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
366         $display.= "<p style=\"text-align:right\">\n";
367         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
368         $display.= "&nbsp;\n";
369         if ($this->dn != "new"){
370           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
371           $display.= "&nbsp;\n";
372         }
373         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
374         $display.= "</p>";
375       }
376       return ($display);
377     }
380     /****************
381       Dialog display
382      ****************/
384         /* Check if there is a snapshot dialog open */
385     $base = $this->DivListDevices->selectedBase;
386     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
387       return($str);
388     }
390     /* Display dialog with system list */
391     $this->DivListDevices->parent = $this;
392     $this->DivListDevices->execute();
393     $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
394     $this->reload();
395     $this->DivListDevices->setEntries($this->devices);
396     return($this->DivListDevices->Draw());
398   }
400   function save_object() {
401     $this->DivListDevices->save_object();
402   }
405   /* Return departments, that will be included within snapshot detection */
406   function get_used_snapshot_bases()
407   {
408     return(array("ou=devices,".$this->DivListDevices->selectedBase));
409   }
411   function copyPasteHandling_from_queue($s_action,$s_entry)
412   {
413     /* Check if Copy & Paste is disabled */
414     if(!is_object($this->CopyPasteHandler)){
415       return("");
416     }
418     /* Add a single entry to queue */
419     if($s_action == "cut" || $s_action == "copy"){
421       /* Cleanup object queue */
422       $this->CopyPasteHandler->cleanup_queue();
423       $dn = $this->devices[$s_entry]['dn'];
424       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
425     }
427     /* Add entries to queue */
428     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
430       /* Cleanup object queue */
431       $this->CopyPasteHandler->cleanup_queue();
433       /* Add new entries to CP queue */
434       foreach($this->list_get_selected_items() as $id){
435         $dn = $this->devices[$id]['dn'];
437         if($s_action == "copy_multiple"){
438           $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
439         }
440         if($s_action == "cut_multiple"){
441           $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
442         }
443       }
444     }
446     /* Start pasting entries */
447     if($s_action == "editPaste"){
448       $this->start_pasting_copied_objects = TRUE;
449     }
451     /* Return C&P dialog */
452     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
454       /* Load entry from queue and set base */
455       $this->CopyPasteHandler->load_entry_from_queue();
456       $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
458       /* Get dialog */
459       $data = $this->CopyPasteHandler->execute();
461       /* Return dialog data */
462       if(!empty($data)){
463         return($data);
464       }
465     }
467     /* Automatically disable status for pasting */
468     if(!$this->CopyPasteHandler->entries_queued()){
469       $this->start_pasting_copied_objects = FALSE;
470     }
471     return("");
472   }
476   function reload()
477   {
478     /* Set base for all searches */
479     $base       = $this->DivListDevices->selectedBase;
480     $Regex      = $this->DivListDevices->Regex;
481     $SubSearch  = $this->DivListDevices->SubSearch;
482     $Flags      =  GL_NONE | GL_SIZELIMIT;
483     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
484     $tmp        = array();
486     /* In case of subsearch, add the subsearch flag */
487     if($SubSearch){
488       $Flags    |= GL_SUBSEARCH;
489     }else{
490       $base ="ou=devices,".$base;
491     }
493     /* Get results and create index */
494     $res= get_list($Filter, "devices", $base, array("cn","description","dn","objectClass"), $Flags);
495     foreach ($res as $val){
496       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
497     }
499     /* sort entries */
500     ksort($tmp);
501     $this->devices=array();
502     foreach($tmp as $val){
503       $this->devices[]=$val;
504     }
505     reset ($this->devices);
506   }
508   function remove_lock()
509   {
510     if (isset($this->devicetabs->dn)){
511       del_lock ($this->devicetabs->dn);
512     }
513   }
515   function list_get_selected_items()
516   {
517     $ids = array();
518     foreach($_POST as $name => $value){
519       if(preg_match("/^item_selected_[0-9]*$/",$name)){
520         $id   = preg_replace("/^item_selected_/","",$name);
521         $ids[$id] = $id;
522       }
523     }
524     return($ids);
525   }
528   function remove_from_parent()
529   {
530     /* This cannot be removed... */
531   }
533 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
534 ?>