Code

Added missing attribute
[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 ="";
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         $disallowed = array();
232         foreach($ids as $id){
233           $dn = $this->devices[$id]['dn'];
234           $acl = $this->ui->get_permissions($dn, "devices/deviceGeneric");
235           if(preg_match("/d/",$acl)){
236             $this->dns[$id] = $dn;
237           }else{
238             $disallowed[] = $dn;
239           }
240         }
242         if(count($disallowed)){
243           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
244         }
246         if(count($this->dns)){
248           /* Check locks */
249           if ($user= get_multiple_locks($this->dns)){
250             return(gen_locked_message($user,$this->dns));
251           }
253           $dns_names = array();
254           foreach($this->dns as $dn){
255             $dns_names[] =@LDAP::fix($dn);
256           }
257           add_lock ($this->dns, $this->ui->dn);
259           /* Lock the current entry, so nobody will edit it during deletion */
260           $smarty->assign("intro", msgPool::deleteInfo($dns_names),_("device"));
261           $smarty->assign("multiple", true);
262           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
263         }
264       }
265     }
268     /********************
269       Delete MULTIPLE entries confirmed
270      ********************/
272     /* Confirmation for deletion has been passed. Users should be deleted. */
273     if (isset($_POST['delete_multiple_device_confirm'])){
275       $ui = get_userinfo();
277       /* Remove user by user and check acls before removeing them */
278       foreach($this->dns as $key => $dn){
280         $acl = $ui->get_permissions($dn,"devices/deviceGeneric");
281         if(preg_match("/d/",$acl)){
283           /* Delete request is permitted, perform LDAP action */
284           $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $dn,"devices");
285           $this->devicetabs->set_acl_base($dn);
286           $this->devicetabs->delete ();
287           unset ($this->devicetabs);
288           $this->devicetabs= NULL;
290         } else {
291           /* Normally this shouldn't be reached, send some extra
292              logs to notify the administrator */
293           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
294           new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
295         }
296       }
298       /* Remove lock file after successfull deletion */
299       $this->remove_lock();
300       $this->dns = array();
301     }
304     /********************
305       Delete MULTIPLE entries Canceled
306      ********************/
308     /* Remove lock */
309     if(isset($_POST['delete_multiple_device_cancel'])){
311       /* Remove lock file after successfull deletion */
312       $this->remove_lock();
313       $this->dns = array();
314     }
317     /****************
318       Delete device type
319      ****************/
321     /* Remove user was requested */
322     if ($s_action == "del"){
324       /* Get 'dn' from posted 'uid' */
325       $this->dn= $this->devices[$s_entry]['dn'];
327       /* Load permissions for selected 'dn' and check if
328          we're allowed to remove this 'dn' */
329       $ui = get_userinfo();
330       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
331       if (preg_match("/d/",$acl)){
333         /* Check locking, save current plugin in 'back_plugin', so
334            the dialog knows where to return. */
335         if (($user= get_lock($this->dn)) != ""){
336           return (gen_locked_message ($user, $this->dn));
337         }
339         /* Lock the current entry, so nobody will edit it during deletion */
340         add_lock ($this->dn, $this->ui->dn);
341         $smarty= get_smarty();
342         $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("device")));
343         $smarty->assign("multiple", false);
344         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
345       } else {
347         /* Obviously the user isn't allowed to delete. Show message and
348            clean session. */
349           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
350       }
351     }
354     /****************
355       Delete device confirmed
356      ****************/
358     /* Confirmation for deletion has been passed. Group should be deleted. */
359     if (isset($_POST['delete_device_confirm'])){
361       /* Some nice guy may send this as POST, so we've to check
362          for the permissions again. */
363       $ui = get_userinfo();
364       $acl = $ui->get_permissions($this->dn,"devices/deviceGeneric");
365       if(preg_match("/d/",$acl)){
367         /* Delete request is permitted, perform LDAP action */
368         $this->devicetabs= new devicetabs($this->config, $this->config->data['TABS']['DEVICETABS'], $this->dn,"devices");
369         $this->devicetabs->set_acl_base($this->dn);
370         $this->devicetabs->delete ();
371         unset ($this->devicetabs);
372         $this->devicetabs= NULL;
374       } else {
376         /* Normally this shouldn't be reached, send some extra
377            logs to notify the administrator */
378         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
379         new log("security","devices/".get_class($this),$dn,array(),"Tried to trick deletion.");
380       }
382       /* Remove lock file after successfull deletion */
383       $this->remove_lock();
384       $this->dns = array();
385     }
388     /****************
389       Delete device canceled
390      ****************/
392     /* Delete device canceled? */
393     if (isset($_POST['delete_cancel'])){
394       $this->remove_lock();
395       session::un_set('objectinfo');
396     }
398     /* Show tab dialog if object is present */
399     if (($this->devicetabs) && (isset($this->devicetabs->config))){
400       $display= $this->devicetabs->execute();
402       /* Don't show buttons if tab dialog requests this */
403       if (!$this->devicetabs->by_object[$this->devicetabs->current]->dialog){
404         $display.= "<p style=\"text-align:right\">\n";
405         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
406         $display.= "&nbsp;\n";
407         if ($this->dn != "new"){
408           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
409           $display.= "&nbsp;\n";
410         }
411         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
412         $display.= "</p>";
413       }
414       return ($display);
415     }
418     /****************
419       Dialog display
420      ****************/
422         /* Check if there is a snapshot dialog open */
423     $base = $this->DivListDevices->selectedBase;
424     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
425       return($str);
426     }
428     /* Display dialog with system list */
429     $this->DivListDevices->parent = $this;
430     $this->DivListDevices->execute();
431     $this->DivListDevices->AddDepartments($this->DivListDevices->selectedBase,3,1);
432     $this->reload();
433     $this->DivListDevices->setEntries($this->devices);
434     return($this->DivListDevices->Draw());
436   }
438   function save_object() {
439     $this->DivListDevices->save_object();
440     if(is_object($this->CopyPasteHandler)){
441       $this->CopyPasteHandler->save_object();
442     }
443   }
446   /* Return departments, that will be included within snapshot detection */
447   function get_used_snapshot_bases()
448   {
449     return(array(get_ou('deviceou').$this->DivListDevices->selectedBase));
450   }
452   function copyPasteHandling_from_queue($s_action,$s_entry)
453   {
454     /* Check if Copy & Paste is disabled */
455     if(!is_object($this->CopyPasteHandler)){
456       return("");
457     }
459     $ui = get_userinfo();
461     /* Add a single entry to queue */
462     if($s_action == "cut" || $s_action == "copy"){
464       /* Cleanup object queue */
465       $this->CopyPasteHandler->cleanup_queue();
466       $dn = $this->devices[$s_entry]['dn'];
467       if($s_action == "copy" && preg_match("/r/",$ui->has_complete_category_acls($dn,"devices"))){
468         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
469       }
470       if($s_action == "cut" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"devices"))){
471         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"devicetabs","DEVICETABS","devices");
472       }
473     }
475     /* Add entries to queue */
476     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
478       /* Cleanup object queue */
479       $this->CopyPasteHandler->cleanup_queue();
481       /* Add new entries to CP queue */
482       foreach($this->list_get_selected_items() as $id){
483         $dn = $this->devices[$id]['dn'];
485         if($s_action == "copy_multiple" && preg_match("/r/",$ui->has_complete_category_acls($dn,"devices"))){
486           $this->CopyPasteHandler->add_to_queue($dn,"copy","devicetabs","DEVICETABS","devices");
487         }
488         if($s_action == "cut_multiple" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"devices"))){
489           $this->CopyPasteHandler->add_to_queue($dn,"cut","devicetabs","DEVICETABS","devices");
490         }
491       }
492     }
494     /* Start pasting entries */
495     if($s_action == "editPaste"){
496       $this->start_pasting_copied_objects = TRUE;
497     }
499     /* Return C&P dialog */
500     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
502       /* Get dialog */
503       $data = $this->CopyPasteHandler->execute();
504       $this->CopyPasteHandler->SetVar("base",$this->DivListDevices->selectedBase);
506       /* Return dialog data */
507       if(!empty($data)){
508         return($data);
509       }
510     }
512     /* Automatically disable status for pasting */
513     if(!$this->CopyPasteHandler->entries_queued()){
514       $this->start_pasting_copied_objects = FALSE;
515     }
516     return("");
517   }
521   function reload()
522   {
523     /* Set base for all searches */
524     $base       = $this->DivListDevices->selectedBase;
525     $Regex      = $this->DivListDevices->Regex;
526     $SubSearch  = $this->DivListDevices->SubSearch;
527     $Flags      =  GL_NONE | GL_SIZELIMIT;
528     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoDevice))";
529     $tmp        = array();
531     /* In case of subsearch, add the subsearch flag */
532     if($SubSearch){
533       $Flags    |= GL_SUBSEARCH;
534     }else{
535       $base = get_ou('deviceou').$base;
536     }
538     /* Get results and create index */
539     $res= get_sub_list($Filter,"devices",get_ou('deviceou'), $base, array("cn","gotoHotplugDevice","description","dn","objectClass"), $Flags);
540     $tmp2 = array();
541     foreach ($res as $val){
542       if (!isset($val['description']) && isset($val['gotoHotplugDevice'][0])) {
543         $dsc= preg_replace("/\|.*$/", "", $val['gotoHotplugDevice'][0]);
544         if ($dsc != ""){
545           $val['description']= array("count" => 1, 0 => $dsc);
546         }
547       }
548       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
549       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
550     }
552     /* sort entries */
553     natcasesort($tmp2);
554     $this->devices=array();
555     foreach($tmp2 as $val){
556       $this->devices[]=$tmp[$val];
557     }
558     reset ($this->devices);
559   }
562   function remove_lock()
563   {
564     if (isset($this->dn) && !empty($this->dn)){
565       del_lock ($this->dn);
566     }
567     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
568       del_lock ($this->dns);
569     }
570   }
572   function list_get_selected_items()
573   {
574     $ids = array();
575     foreach($_POST as $name => $value){
576       if(preg_match("/^item_selected_[0-9]*$/",$name)){
577         $id   = preg_replace("/^item_selected_/","",$name);
578         $ids[$id] = $id;
579       }
580     }
581     return($ids);
582   }
585   function remove_from_parent()
586   {
587     /* This cannot be removed... */
588   }
590 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
591 ?>