Code

fcd690c0c295561ed9d080c5f1d57c5570667dfa
[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::set('autocomplete', $this->filter);
95       if (!$this->filter->isValid()){
96         msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
97       }
98     }
100     // Handle actions (POSTs and GETs)
101     $str = $this->handleActions($this->detectPostActions());
102     if($str) return($this->getHeader().$str);
104     // Open single dialog objects
105     if(is_object($this->dialogObject)){
106       if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object(); 
107       if(method_exists($this->dialogObject,'execute')){
108         $display = $this->dialogObject->execute(); 
109         $display.= $this->_getTabFooter();
110         return($this->getHeader().$display);
111       } 
112     }
114     // Display tab object.
115     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
116       $this->tabObject->save_object();
117       $display = $this->tabObject->execute();
118       $display.= $this->_getTabFooter();
119       return($this->getHeader().$display);
120     }
122     // Set current restore base for snapshot handling.
123     if(is_object($this->snapHandler)){
124       $bases = array();
125       foreach($this->storagePoints as $sp){
126         $bases[] = $sp.$this->headpage->getBase();
127       }
128       $this->snapHandler->setSnapshotBases($bases);
129     }
130       
131     $this->headpage->update();
132     $display = $this->headpage->render();
133     return($this->getHeader().$display);
134   }
136   protected function getHeader()
137   {
138     if (get_object_info() != ""){
139       $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
140       "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
141         LDAP::fix(get_object_info()));
142     } else {
143       $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
144     }
145     return($display);
146   }
149   protected function _getTabFooter()
150   { 
151     if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
152       return("");
153     }
154   
155     if($this->tabObject->by_object[$this->tabObject->current]){
156       $current = $this->tabObject->by_object[$this->tabObject->current];  
157       if(is_object($current->dialog)){
158         return("");
159       }
160     }
162     $str = "";
163     if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
164       $str.= "<p style=\"text-align:right\">
165         <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
166         </p>";
167       return($str);
168     }else{
169       $str.= "<p style=\"text-align:right\">\n";
170       $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
171       $str.= "&nbsp;\n";
172       if($this->displayApplyBtn){
173         $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
174         $str.= "&nbsp;\n";
175       }
176       $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
177       $str.= "</p>";
178     }
179     return($str);
180   }
183   protected function removeEntryRequested($action,$entry,$all)
184   {
185     $disallowed = array();
186     $this->dns = array();
187     foreach($entry as $dn){
188       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
189       if(preg_match("/d/",$acl)){
190         $this->dns[] = $dn;
191       }else{
192         $disallowed[] = $dn;
193       }
194     }
196     if(count($disallowed)){
197       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
198     }
200     if(count($this->dns)){
202       /* Check locks */
203       if ($user= get_multiple_locks($this->dns)){
204         return(gen_locked_message($user,$this->dns));
205       }
207       $dns_names = array();
208       foreach($this->dns as $dn){
209         $dns_names[] =LDAP::fix($dn);
210       }
211       add_lock ($this->dns, $this->ui->dn);
213       /* Lock the current entry, so nobody will edit it during deletion */
214       $smarty = get_smarty();
215       $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
216       $smarty->assign("multiple", true);
217       return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
218     }
219   }  
222   protected function removeEntryConfirmed()
223   {
224     foreach($this->dns as $key => $dn){
226       /* Load permissions for selected 'dn' and check if
227          we're allowed to remove this 'dn' */
228       $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
229       if(preg_match("/d/",$acl)){
231         /* Delete request is permitted, perform LDAP action */
232         $this->dn = $dn;
233         $tab = $this->tabClass;
234         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
235         $this->tabObject->set_acl_base($this->dn);
236         $this->tabObject->delete ();
237         del_lock($this->dn);        
238       } else {
240         /* Normally this shouldn't be reached, send some extra
241            logs to notify the administrator */
242         msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
243         new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
244       }
245     }
247     $this->remove_lock();
248     $this->closeDialogs();
249   }
252   function detectPostActions()
253   {
254     $action= $this->headpage->getAction();
256     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
257     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
258     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
259     if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
261     // Detect Snapshot actions
262     if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
263     if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
264     foreach($_POST as $name => $value){
265       $once =TRUE;
266       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
267         $once = FALSE;
268         $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
269         $action['action'] = "restoreSnapshot";
270         $action['targets'] = array($entry);
271       }
272     }
274     return($action);
275   }
277   function handleActions($action)
278   {
279     // Start action  
280     if(isset($this->actions[$action['action']])){
281       $func = $this->actions[$action['action']];
282       if(!isset($action['targets']))$action['targets']= array(); 
283       return($this->$func($action['action'],$action['targets'],$action));
284     }
285   } 
287   function createSnapshotDialog($action="",$target=array(),$all=array())
288   {
289     foreach($target as $entry){
290       if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
291         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
292         $this->dialogObject->aclCategories = array($this->aclCategory);
293         
294       }else{
295         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
296             ERROR_DIALOG);
297       }
298     }
299   }
302   function saveSnapshot()
303   {
304     $this->dialogObject->save_object();
305     $msgs = $this->dialogObject->check();
306     if(count($msgs)){
307       foreach($msgs as $msg){
308         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
309       }
310     }else{
311       $this->dn =  $this->dialogObject->dn;
312       $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
313       $this->closeDialogs();
314     }
315   }
318   function restoreSnapshot($action="",$target=array(),$all=array())
319   {
320     $entry = array_pop($target);
321     if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
322       $this->snapHandler->restore_snapshot($entry);
323       $this->closeDialogs();
324     }else{
325       msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
326           ERROR_DIALOG);
327     }
328   }
331   function restoreSnapshotDialog($action="",$target=array(),$all=array())
332   {
333     // Set current restore base for snapshot handling.
334     if(is_object($this->snapHandler)){
335       $bases = array();
336       foreach($this->storagePoints as $sp){
337         $bases[] = $sp.$this->headpage->getBase();
338       }
339     }
340     if(!count($target)){ 
341       $entry = $this->headpage->getBase();
342       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
343         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
344         $this->dialogObject->set_snapshot_bases($bases);
345         $this->dialogObject->display_all_removed_objects = true;
346         $this->dialogObject->display_restore_dialog = true;
347       }else{
348         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
349             ERROR_DIALOG);
350       } 
351     }else{
352       $entry = array_pop($target);
353       if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
354         $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
355         $this->dialogObject->set_snapshot_bases($bases);
356         $this->dialogObject->display_restore_dialog = true;
357       }else{
358         msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
359             ERROR_DIALOG);
360       } 
361     }
362   }
365   function newEntry($action="",$target=array(),$all=array())
366   {
367     // Check locking & lock entry if required 
368     $this->displayApplyBtn = FALSE;
369     $this->dn = "new";
370     $this->is_new = TRUE;
371     $this->is_single_edit = FALSE;
372     $this->is_multiple_edit = FALSE;
374     set_object_info($this->dn);
376     // Open object.
377     if(empty($this->tabClass) || empty($this->tabType)){
378       // No tab type defined
379     }else{
380       $tab = $this->tabClass;
381       $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
382       $this->tabObject->set_acl_base($this->headpage->getBase());
383     }
384   }
386   function editEntry($action,$target,$all)
387   {
388     if(count($target) == 0){
389       //nothing 
390     }elseif(count($target) == 1){
392       // Check locking & lock entry if required 
393       $this->displayApplyBtn = TRUE;
395       $this->is_new = FALSE;
396       $this->is_single_edit = TRUE;
397       $this->is_multiple_edit = FALSE;
399       $this->dn = array_pop($target);
400       set_object_info($this->dn);
401       $user = get_lock($this->dn);
402       if ($user != ""){
403         return(gen_locked_message ($user, $this->dn,TRUE));
404       }
405       add_lock ($this->dn, $this->ui->dn);
407       // Open object.
408       if(empty($this->tabClass) || empty($this->tabType)){
409         // No tab type defined
410       }else{
411         $tab = $this->tabClass;
412         $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
413         $this->tabObject->set_acl_base($this->dn);
414       }
415     }else{
417       $this->is_new = FALSE;
418       $this->is_singel_edit = FALSE;
419       $this->is_multiple_edit = TRUE;
421       $this->dns = $target;
422       $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
423             $this->dns,$this->headpage->getBase(),$this->aclCategory);
424       if ($tmp->entries_locked()){
425         return($tmp->display_lock_message());
426       }
427       $tmp->lock_entries($this->ui->dn);
428       if($tmp->multiple_available()){
429         $this->tabObject = $tmp;
430         set_object_info($this->tabObject->get_object_info());
431       }
433     }
434   }
436   protected function saveChanges()
437   {
438     if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
439       $this->tabObject->save_object();
440       $msgs = $this->tabObject->check();
441       if(count($msgs)){
442         msg_dialog::displayChecks($msgs); 
443         return("");
444       }else{
445         $this->tabObject->save();
446         $this->remove_lock();
447         $this->closeDialogs();
448       }
449     }
450   }
452   protected function applyChanges()
453   {
454   if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
455     $this->tabObject->save_object();
456       $msgs = $this->tabObject->check();
457       if(count($msgs)){
458         msg_dialog::displayChecks($msgs); 
459         return("");
460       }else{
461         $this->tabObject->save();
462         $this->tabObject->re_init();
463       }
464     }
465   }
466   
467   protected function closeDialogs()
468   {
469     $this->last_dn = $this->dn;
470     $this->last_dns = $this->dns;
471     $this->last_tabObject = $this->tabObject;
472     $this->last_dialogObject = $this->dialogObject;
474     $this->dn = "";
475     $this->dns = array();
476     $this->tabObject = null;
477     $this->dialogObject = null;
478     set_object_info();
479   }
481   protected function cancelEdit()
482   {
483     $this->remove_lock();
484     $this->closeDialogs();
485   }
488  
490   function registerAction($action,$target)
491   {
492     $this->actions[$action] = $target;
493   }
495   function remove_lock()
496   {
497     if(!empty($this->dn) && $this->dn != "new"){
498       del_lock($this->dn);
499     }
500     if(count($this->dns)){
501       del_lock($this->dns);
502     }
503   }
505   function copyPasteHandler($s_action,$s_entry)
506   {
508     /* Check if Copy & Paste is disabled */
509     if(!is_object($this->cpHandler)){
510       return("");
511     }
513     $this->cpHandler->save_object();
515     /* Add entries to queue */
516     if($s_action == "copy" || $s_action == "cut"){
518       /* Cleanup object queue */
519       $this->cpHandler->cleanup_queue();
521       /* Add new entries to CP queue */
522       foreach($s_entry as $dn){
523         if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
524           $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
525         }
526         if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
527           $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
528         }
529       }
530     }
532     /* Start pasting entries */
533     if($s_action == "paste"){
534       $this->cpPastingStarted = TRUE;
535     }
537     /* Return C&P dialog */
538     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
540       /* Get dialog */
541       $this->cpHandler->SetVar("base",$this->headpage->getBase());
542       $data = $this->cpHandler->execute();
544       /* Return dialog data */
545       if(!empty($data)){
546         return($data);
547       }
548     }
550     /* Automatically disable status for pasting */
551     if(!$this->cpHandler->entries_queued()){
552       $this->cpPastingStarted = FALSE;
553     }
554     return("");
555   }
558   function setDescription($str) {
559     $this->plDescription = $str;
560   } 
561   function setHeadpage($str) {
562     $this->headpage = $str;
563   } 
564   function setFilter($str) {
565     $this->filter = $str;
566   } 
567   function setIcon($str) {
568     $this->plIcon = $str;
569   } 
570   function setHeadline($str) {
571     $this->plHeadline = $str;
572   } 
575 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
576 ?>