Code

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