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 $tabClass = "";
38 protected $tabType = "";
39 protected $aclCategory = "";
40 protected $objectName = "";
41 protected $tabObject = null;
43 protected $displayApplyBtn = "";
44 protected $cpHandler = null;
45 protected $cpPastingStarted = FALSE;
47 protected $snapHandler = null;
49 // Private
50 protected $plugname = "";
51 protected $headpage = null;
52 protected $filter = null;
53 protected $actions = array();
55 function __construct($config,$ui,$plugname, $headpage)
56 {
57 $this->plugname = $plugname;
58 $this->headpage = $headpage;
59 $this->ui = $ui;
60 $this->config = $config;
62 if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
63 if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
65 if(empty($this->plIcon)){
66 $this->plIcon = "plugins/".$plugname."/images/plugin.png";
67 }
68 }
70 function execute()
71 {
72 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
73 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
74 session::set('LOCK_VARS_TO_USE',$vars);
76 /* Display the copy & paste dialog, if it is currently open */
77 $ret = $this->copyPasteHandler("",array());
78 if($ret){
79 return($this->getHeader().$ret);
80 }
82 // Handle actions (POSTs and GETs)
83 $str = $this->handleActions($this->detectPostActions());
84 if($str) return($this->getHeader().$str);
86 // Display tab object.
87 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
88 $this->tabObject->save_object();
89 $display = $this->tabObject->execute();
90 $display.= $this->_getTabFooter();
91 return($this->getHeader().$display);
92 }
94 $this->headpage->update();
95 $display = $this->headpage->render();
96 return($this->getHeader().$display);
97 }
99 protected function getHeader()
100 {
101 if (get_object_info() != ""){
102 $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
103 "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
104 LDAP::fix(get_object_info()));
105 } else {
106 $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
107 }
108 return($display);
109 }
112 protected function _getTabFooter()
113 {
114 if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
115 return("");
116 }
118 if($this->tabObject->by_object[$this->tabObject->current]){
119 $current = $this->tabObject->by_object[$this->tabObject->current];
120 if(is_object($current->dialog)){
121 return("");
122 }
123 }
125 $str = "";
126 if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
127 $str.= "<p style=\"text-align:right\">
128 <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
129 </p>";
130 return($str);
131 }else{
132 $str.= "<p style=\"text-align:right\">\n";
133 $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
134 $str.= " \n";
135 if($this->displayApplyBtn){
136 $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
137 $str.= " \n";
138 }
139 $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
140 $str.= "</p>";
141 }
142 return($str);
143 }
146 protected function removeEntryRequested($action,$entry,$all)
147 {
148 $disallowed = array();
149 $this->dns = array();
150 foreach($entry as $dn){
151 $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
152 if(preg_match("/d/",$acl)){
153 $this->dns[] = $dn;
154 }else{
155 $disallowed[] = $dn;
156 }
157 }
159 if(count($disallowed)){
160 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
161 }
163 if(count($this->dns)){
165 /* Check locks */
166 if ($user= get_multiple_locks($this->dns)){
167 return(gen_locked_message($user,$this->dns));
168 }
170 $dns_names = array();
171 foreach($this->dns as $dn){
172 $dns_names[] =LDAP::fix($dn);
173 }
174 add_lock ($this->dns, $this->ui->dn);
176 /* Lock the current entry, so nobody will edit it during deletion */
177 $smarty = get_smarty();
178 $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
179 $smarty->assign("multiple", true);
180 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
181 }
182 }
185 protected function removeEntryConfirmed()
186 {
187 foreach($this->dns as $key => $dn){
189 /* Load permissions for selected 'dn' and check if
190 we're allowed to remove this 'dn' */
191 $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
192 if(preg_match("/d/",$acl)){
194 /* Delete request is permitted, perform LDAP action */
195 $this->dn = $dn;
196 $tab = $this->tabClass;
197 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
198 $this->tabObject->set_acl_base($this->dn);
199 $this->tabObject->delete ();
200 $this->closeDialogs();
201 del_lock($this->dn);
202 } else {
204 /* Normally this shouldn't be reached, send some extra
205 logs to notify the administrator */
206 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
207 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
208 return;
209 }
210 }
212 $this->closeDialogs();
213 $this->remove_lock();
214 }
217 function detectPostActions()
218 {
219 $action= $this->headpage->getAction();
221 if(isset($_POST['edit_apply'])) $action['action'] = "apply";
222 if(isset($_POST['edit_finish'])) $action['action'] = "save";
223 if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
224 if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";
226 return($action);
227 }
229 function handleActions($action)
230 {
231 // Start action
232 if(isset($this->actions[$action['action']])){
233 $func = $this->actions[$action['action']];
234 if(!isset($action['targets']))$action['targets']= array();
235 return($this->$func($action['action'],$action['targets'],$action));
236 }
237 }
239 function newEntry($action="",$target=array(),$all=array())
240 {
241 // Check locking & lock entry if required
242 $this->displayApplyBtn = FALSE;
243 $this->dn = "new";
244 $this->is_new = TRUE;
245 $this->is_single_edit = FALSE;
246 $this->is_multiple_edit = FALSE;
248 set_object_info($this->dn);
250 // Open object.
251 if(empty($this->tabClass) || empty($this->tabType)){
252 // No tab type defined
253 }else{
254 $tab = $this->tabClass;
255 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
256 $this->tabObject->set_acl_base($this->headpage->getBase());
257 }
258 }
260 function editEntry($action,$target,$all)
261 {
262 if(count($target) == 0){
263 //nothing
264 }elseif(count($target) == 1){
266 // Check locking & lock entry if required
267 $this->displayApplyBtn = TRUE;
269 $this->is_new = FALSE;
270 $this->is_single_edit = TRUE;
271 $this->is_multiple_edit = FALSE;
273 $this->dn = array_pop($target);
274 set_object_info($this->dn);
275 $user = get_lock($this->dn);
276 if ($user != ""){
277 return(gen_locked_message ($user, $this->dn,TRUE));
278 }
279 add_lock ($this->dn, $this->ui->dn);
281 // Open object.
282 if(empty($this->tabClass) || empty($this->tabType)){
283 // No tab type defined
284 }else{
285 $tab = $this->tabClass;
286 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
287 $this->tabObject->set_acl_base($this->dn);
288 }
289 }else{
291 $this->is_new = FALSE;
292 $this->is_singel_edit = FALSE;
293 $this->is_multiple_edit = TRUE;
295 $this->dns = $target;
296 $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
297 $this->dns,$this->headpage->getBase(),$this->aclCategory);
298 if ($tmp->entries_locked()){
299 return($tmp->display_lock_message());
300 }
301 $tmp->lock_entries($this->ui->dn);
302 if($tmp->multiple_available()){
303 $this->tabObject = $tmp;
304 set_object_info($this->tabObject->get_object_info());
305 }
307 }
308 }
310 protected function saveChanges()
311 {
312 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
313 $this->tabObject->save_object();
314 $msgs = $this->tabObject->check();
315 if(count($msgs)){
316 msg_dialog::displayChecks($msgs);
317 return("");
318 }else{
319 $this->tabObject->save();
320 $this->closeDialogs();
321 $this->remove_lock();
322 }
323 }
324 }
326 protected function applyChanges()
327 {
328 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
329 $this->tabObject->save_object();
330 $msgs = $this->tabObject->check();
331 if(count($msgs)){
332 msg_dialog::displayChecks($msgs);
333 return("");
334 }else{
335 $this->tabObject->save();
336 $this->tabObject->re_init();
337 }
338 }
339 }
341 protected function closeDialogs()
342 {
343 $this->dn = "";
344 $this->dns = array();
345 $this->tabObject = null;
346 set_object_info();
347 }
349 protected function cancelEdit()
350 {
351 $this->closeDialogs();
352 $this->remove_lock();
353 }
358 function registerAction($action,$target)
359 {
360 $this->actions[$action] = $target;
361 }
363 function remove_lock()
364 {
365 if(!empty($this->dn) && $this->dn != "new"){
366 del_lock($this->dn);
367 }
368 if(count($this->dns)){
369 del_lock($this->dns);
370 }
371 }
373 function copyPasteHandler($s_action,$s_entry)
374 {
376 /* Check if Copy & Paste is disabled */
377 if(!is_object($this->cpHandler)){
378 return("");
379 }
381 $this->cpHandler->save_object();
383 /* Add entries to queue */
384 if($s_action == "copy" || $s_action == "cut"){
386 /* Cleanup object queue */
387 $this->cpHandler->cleanup_queue();
389 /* Add new entries to CP queue */
390 foreach($s_entry as $dn){
391 if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
392 $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
393 }
394 if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
395 $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
396 }
397 }
398 }
400 /* Start pasting entries */
401 if($s_action == "paste"){
402 $this->cpPastingStarted = TRUE;
403 }
405 /* Return C&P dialog */
406 if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
408 /* Get dialog */
409 $this->cpHandler->SetVar("base",$this->headpage->getBase());
410 $data = $this->cpHandler->execute();
412 /* Return dialog data */
413 if(!empty($data)){
414 return($data);
415 }
416 }
418 /* Automatically disable status for pasting */
419 if(!$this->cpHandler->entries_queued()){
420 $this->cpPastingStarted = FALSE;
421 }
422 return("");
423 }
426 function setDescription($str) {
427 $this->plDescription = $str;
428 }
429 function setHeadpage($str) {
430 $this->headpage = $str;
431 }
432 function setFilter($str) {
433 $this->filter = $str;
434 }
435 function setIcon($str) {
436 $this->plIcon = $str;
437 }
438 function setHeadline($str) {
439 $this->plHeadline = $str;
440 }
441 }
443 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
444 ?>