Code

0d0f43ba0b16de5abe7e29f40f937a1a5a90ac39
[gosa.git] / gosa-core / include / class_management.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_plugin.inc 14584 2009-10-12 14:04:22Z hickert $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class management
24 {
25   // Public 
26   public $config = null;
27   public $ui     = null;
29   public $plIcon        = "";
30   public $plDescription = "";
31   public $plHeadline    = "";
33   // Protected 
34   protected $dn = "";
35   protected $dns = array();
37   protected $storagePoints = array();
39   protected $last_dn = "";
40   protected $last_dns = array();
42   protected $tabClass = "";
43   protected $tabType = "";
44   protected $aclCategory = "";
45   protected $objectName = "";
46   protected $tabObject = null;
47   protected $dialogObject = null;
49   protected $last_tabObject = null;
50   protected $last_dialogObject = null;
52   protected $displayApplyBtn = "";
53   protected $cpHandler = null;
54   protected $cpPastingStarted = FALSE;
55  
56   protected $snapHandler = null;
58   // Private
59   protected $plugname = "";
60   protected $headpage = null;
61   protected $filter = null;
62   protected $actions = array();
63   
64   function  __construct($config,$ui,$plugname, $headpage)
65   {
66     $this->plugname = $plugname;
67     $this->headpage = $headpage;
68     $this->ui = $ui;
69     $this->config = $config;
71     if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
72     if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
74     if(empty($this->plIcon)){
75       $this->plIcon = "plugins/".$plugname."/images/plugin.png";
76     }
77   }
79   function execute()
80   {
81     // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
82     $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
83     session::set('LOCK_VARS_TO_USE',$vars);
85     /* Display the copy & paste dialog, if it is currently open */
86     $ret = $this->copyPasteHandler("",array());
87     if($ret){
88       return($this->getHeader().$ret);
89     }
91     // Update filter
92     if ($this->filter) {
93       $this->filter->update();
94       session::global_set(get_class($this)."_filter", $this->filter);
95       session::set('autocomplete', $this->filter);
96       if (!$this->filter->isValid()){
97         msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
98       }
99     }
101     // Handle actions (POSTs and GETs)
102     $str = $this->handleActions($this->detectPostActions());
103     if($str) return($this->getHeader().$str);
105     // Open single dialog objects
106     if(is_object($this->dialogObject)){
107       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
108       if(method_exists($this->dialogObject,'execute')){
109         $display = $this->dialogObject->execute(); 
110         $display.= $this->_getTabFooter();
111         return($this->getHeader().$display);
112       } 
113     }
115     // Display tab object.
116     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
117       $this->tabObject->save_object();
118       $display = $this->tabObject->execute();
119       $display.= $this->_getTabFooter();
120       return($this->getHeader().$display);
121     }
123     // Set current restore base for snapshot handling.
124     if(is_object($this->snapHandler)){
125       $bases = array();
126       foreach($this->storagePoints as $sp){
127         $bases[] = $sp.$this->headpage->getBase();
128       }
129       $this->snapHandler->setSnapshotBases($bases);
130     }
131       
132     $this->headpage->update();
133     $display = $this->headpage->render();
134     return($this->getHeader().$display);
135   }
137   protected function getHeader()
138   {
139     if (get_object_info() != ""){
140       $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
141       "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
142         LDAP::fix(get_object_info()));
143     } else {
144       $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
145     }
146     return($display);
147   }
150   protected function _getTabFooter()
151   { 
152     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
153       return("");
154     }
155   
156     if($this->tabObject->by_object[$this->tabObject->current]){
157       $current = $this->tabObject->by_object[$this->tabObject->current];  
158       if(is_object($current->dialog)){
159         return("");
160       }
161     }
163     $str = "";
164     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
165       $str.= "<p style=\"text-align:right\">
166         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
167         </p>";
168       return($str);
169     }else{
170       $str.= "<p style=\"text-align:right\">\n";
171       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
172       $str.= "&nbsp;\n";
173       if($this->displayApplyBtn){
174         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
175         $str.= "&nbsp;\n";
176       }
177       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
178       $str.= "</p>";
179     }
180     return($str);
181   }
184   protected function removeEntryRequested($action,$entry,$all)
185   {
186     $disallowed = array();
187     $this->dns = array();
188     foreach($entry as $dn){
189       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
190       if(preg_match("/d/",$acl)){
191         $this->dns[] = $dn;
192       }else{
193         $disallowed[] = $dn;
194       }
195     }
197     if(count($disallowed)){
198       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
199     }
201     if(count($this->dns)){
203       /* Check locks */
204       if ($user= get_multiple_locks($this->dns)){
205         return(gen_locked_message($user,$this->dns));
206       }
208       $dns_names = array();
209       foreach($this->dns as $dn){
210         $dns_names[] =LDAP::fix($dn);
211       }
212       add_lock ($this->dns, $this->ui->dn);
214       /* Lock the current entry, so nobody will edit it during deletion */
215       $smarty = get_smarty();
216       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
217       $smarty->assign("multiple", true);
218       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
219     }
220   }  
223   protected function removeEntryConfirmed()
224   {
225     foreach($this->dns as $key => $dn){
227       /* Load permissions for selected 'dn' and check if
228          we're allowed to remove this 'dn' */
229       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
230       if(preg_match("/d/",$acl)){
232         /* Delete request is permitted, perform LDAP action */
233         $this->dn = $dn;
234         $tab = $this->tabClass;
235         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
236         $this->tabObject->set_acl_base($this->dn);
237         $this->tabObject->delete ();
238         del_lock($this->dn);        
239       } else {
241         /* Normally this shouldn't be reached, send some extra
242            logs to notify the administrator */
243         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
244         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
245       }
246     }
248     $this->remove_lock();
249     $this->closeDialogs();
250   }
253   function detectPostActions()
254   {
255     $action= $this->headpage->getAction();
257     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
258     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
259     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
260     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
262     // Detect Snapshot actions
263     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
264     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
265     foreach($_POST as $name => $value){
266       $once =TRUE;
267       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
268         $once = FALSE;
269         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
270         $action['action'] = "restoreSnapshot";
271         $action['targets'] = array($entry);
272       }
273     }
275     return($action);
276   }
278   function handleActions($action)
279   {
280     // Start action  
281     if(isset($this->actions[$action['action']])){
282       $func = $this->actions[$action['action']];
283       if(!isset($action['targets']))$action['targets']= array(); 
284       return($this->$func($action['action'],$action['targets'],$action));
285     }
286   } 
288   function createSnapshotDialog($action="",$target=array(),$all=array())
289   {
290     foreach($target as $entry){
291       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
292         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
293         $this->dialogObject->aclCategories = array($this->aclCategory);
294         
295       }else{
296         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
297             ERROR_DIALOG);
298       }
299     }
300   }
303   function saveSnapshot()
304   {
305     $this->dialogObject->save_object();
306     $msgs = $this->dialogObject->check();
307     if(count($msgs)){
308       foreach($msgs as $msg){
309         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
310       }
311     }else{
312       $this->dn =  $this->dialogObject->dn;
313       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
314       $this->closeDialogs();
315     }
316   }
319   function restoreSnapshot($action="",$target=array(),$all=array())
320   {
321     $entry = array_pop($target);
322     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
323       $this->snapHandler->restore_snapshot($entry);
324       $this->closeDialogs();
325     }else{
326       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
327           ERROR_DIALOG);
328     }
329   }
332   function restoreSnapshotDialog($action="",$target=array(),$all=array())
333   {
334     // Set current restore base for snapshot handling.
335     if(is_object($this->snapHandler)){
336       $bases = array();
337       foreach($this->storagePoints as $sp){
338         $bases[] = $sp.$this->headpage->getBase();
339       }
340     }
341     if(!count($target)){ 
342       $entry = $this->headpage->getBase();
343       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
344         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
345         $this->dialogObject->set_snapshot_bases($bases);
346         $this->dialogObject->display_all_removed_objects = true;
347         $this->dialogObject->display_restore_dialog = true;
348       }else{
349         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
350             ERROR_DIALOG);
351       } 
352     }else{
353       $entry = array_pop($target);
354       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
355         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
356         $this->dialogObject->set_snapshot_bases($bases);
357         $this->dialogObject->display_restore_dialog = true;
358       }else{
359         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
360             ERROR_DIALOG);
361       } 
362     }
363   }
366   function newEntry($action="",$target=array(),$all=array())
367   {
368     // Check locking & lock entry if required 
369     $this->displayApplyBtn = FALSE;
370     $this->dn = "new";
371     $this->is_new = TRUE;
372     $this->is_single_edit = FALSE;
373     $this->is_multiple_edit = FALSE;
375     set_object_info($this->dn);
377     // Open object.
378     if(empty($this->tabClass) || empty($this->tabType)){
379       // No tab type defined
380     }else{
381       $tab = $this->tabClass;
382       $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
383       $this->tabObject->set_acl_base($this->headpage->getBase());
384     }
385   }
387   function editEntry($action,$target,$all)
388   {
389     if(count($target) == 0){
390       //nothing 
391     }elseif(count($target) == 1){
393       // Check locking & lock entry if required 
394       $this->displayApplyBtn = TRUE;
396       $this->is_new = FALSE;
397       $this->is_single_edit = TRUE;
398       $this->is_multiple_edit = FALSE;
400       $this->dn = array_pop($target);
401       set_object_info($this->dn);
402       $user = get_lock($this->dn);
403       if ($user != ""){
404         return(gen_locked_message ($user, $this->dn,TRUE));
405       }
406       add_lock ($this->dn, $this->ui->dn);
408       // Open object.
409       if(empty($this->tabClass) || empty($this->tabType)){
410         // No tab type defined
411       }else{
412         $tab = $this->tabClass;
413         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
414         $this->tabObject->set_acl_base($this->dn);
415       }
416     }else{
418       $this->is_new = FALSE;
419       $this->is_singel_edit = FALSE;
420       $this->is_multiple_edit = TRUE;
422       $this->dns = $target;
423       $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
424             $this->dns,$this->headpage->getBase(),$this->aclCategory);
425       if ($tmp->entries_locked()){
426         return($tmp->display_lock_message());
427       }
428       $tmp->lock_entries($this->ui->dn);
429       if($tmp->multiple_available()){
430         $this->tabObject = $tmp;
431         set_object_info($this->tabObject->get_object_info());
432       }
434     }
435   }
437   protected function saveChanges()
438   {
439     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
440       $this->tabObject->save_object();
441       $msgs = $this->tabObject->check();
442       if(count($msgs)){
443         msg_dialog::displayChecks($msgs); 
444         return("");
445       }else{
446         $this->tabObject->save();
447         $this->remove_lock();
448         $this->closeDialogs();
449       }
450     }
451   }
453   protected function applyChanges()
454   {
455   if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
456     $this->tabObject->save_object();
457       $msgs = $this->tabObject->check();
458       if(count($msgs)){
459         msg_dialog::displayChecks($msgs); 
460         return("");
461       }else{
462         $this->tabObject->save();
463         $this->tabObject->re_init();
464       }
465     }
466   }
467   
468   protected function closeDialogs()
469   {
470     $this->last_dn = $this->dn;
471     $this->last_dns = $this->dns;
472     $this->last_tabObject = $this->tabObject;
473     $this->last_dialogObject = $this->dialogObject;
475     $this->dn = "";
476     $this->dns = array();
477     $this->tabObject = null;
478     $this->dialogObject = null;
479     set_object_info();
480   }
482   protected function cancelEdit()
483   {
484     $this->remove_lock();
485     $this->closeDialogs();
486   }
489  
491   function registerAction($action,$target)
492   {
493     $this->actions[$action] = $target;
494   }
496   function remove_lock()
497   {
498     if(!empty($this->dn) && $this->dn != "new"){
499       del_lock($this->dn);
500     }
501     if(count($this->dns)){
502       del_lock($this->dns);
503     }
504   }
506   function copyPasteHandler($s_action,$s_entry)
507   {
509     /* Check if Copy & Paste is disabled */
510     if(!is_object($this->cpHandler)){
511       return("");
512     }
514     $this->cpHandler->save_object();
516     /* Add entries to queue */
517     if($s_action == "copy" || $s_action == "cut"){
519       /* Cleanup object queue */
520       $this->cpHandler->cleanup_queue();
522       /* Add new entries to CP queue */
523       foreach($s_entry as $dn){
524         if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
525           $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
526         }
527         if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
528           $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
529         }
530       }
531     }
533     /* Start pasting entries */
534     if($s_action == "paste"){
535       $this->cpPastingStarted = TRUE;
536     }
538     /* Return C&P dialog */
539     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
541       /* Get dialog */
542       $this->cpHandler->SetVar("base",$this->headpage->getBase());
543       $data = $this->cpHandler->execute();
545       /* Return dialog data */
546       if(!empty($data)){
547         return($data);
548       }
549     }
551     /* Automatically disable status for pasting */
552     if(!$this->cpHandler->entries_queued()){
553       $this->cpPastingStarted = FALSE;
554     }
555     return("");
556   }
559   function setDescription($str) {
560     $this->plDescription = $str;
561   } 
564   function setHeadpage($str) {
565     $this->headpage = $str;
566   } 
569   function setFilter($str) {
570     $this->filter = $str;
571   } 
574   function setIcon($str) {
575     $this->plIcon = $str;
576   } 
579   function setHeadline($str) {
580     $this->plHeadline = $str;
581   } 
584 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
585 ?>