00629634d68a977af09248dff77c905c308f6a18
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 // Update filter
83 if ($this->filter) {
84 $this->filter->update();
85 session::set('autocomplete', $this->filter);
86 if (!$this->filter->isValid()){
87 msg_dialog::display(_("Filter error"), _("The filter is incomplete!"), ERROR_DIALOG);
88 }
89 }
91 // Handle actions (POSTs and GETs)
92 $str = $this->handleActions($this->detectPostActions());
93 if($str) return($this->getHeader().$str);
95 // Open single dialog objects
96 if(is_object($this->dialogObject)){
97 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
98 if(method_exists($this->dialogObject,'execute')){
99 $display = $this->dialogObject->execute();
100 $display.= $this->_getTabFooter();
101 return($this->getHeader().$display);
102 }
103 }
105 // Display tab object.
106 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
107 $this->tabObject->save_object();
108 $display = $this->tabObject->execute();
109 $display.= $this->_getTabFooter();
110 return($this->getHeader().$display);
111 }
113 $this->headpage->update();
114 $display = $this->headpage->render();
115 return($this->getHeader().$display);
116 }
118 protected function getHeader()
119 {
120 if (get_object_info() != ""){
121 $display= print_header(get_template_path($this->plIcon),_($this->plDescription),
122 "<img alt=\"\" class=\"center\" src=\"".get_template_path('images/lists/locked.png')."\">".
123 LDAP::fix(get_object_info()));
124 } else {
125 $display= print_header(get_template_path($this->plIcon),_($this->plDescription));
126 }
127 return($display);
128 }
131 protected function _getTabFooter()
132 {
133 if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
134 return("");
135 }
137 if($this->tabObject->by_object[$this->tabObject->current]){
138 $current = $this->tabObject->by_object[$this->tabObject->current];
139 if(is_object($current->dialog)){
140 return("");
141 }
142 }
144 $str = "";
145 if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
146 $str.= "<p style=\"text-align:right\">
147 <input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">
148 </p>";
149 return($str);
150 }else{
151 $str.= "<p style=\"text-align:right\">\n";
152 $str.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
153 $str.= " \n";
154 if($this->displayApplyBtn){
155 $str.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
156 $str.= " \n";
157 }
158 $str.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
159 $str.= "</p>";
160 }
161 return($str);
162 }
165 protected function removeEntryRequested($action,$entry,$all)
166 {
167 $disallowed = array();
168 $this->dns = array();
169 foreach($entry as $dn){
170 $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
171 if(preg_match("/d/",$acl)){
172 $this->dns[] = $dn;
173 }else{
174 $disallowed[] = $dn;
175 }
176 }
178 if(count($disallowed)){
179 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
180 }
182 if(count($this->dns)){
184 /* Check locks */
185 if ($user= get_multiple_locks($this->dns)){
186 return(gen_locked_message($user,$this->dns));
187 }
189 $dns_names = array();
190 foreach($this->dns as $dn){
191 $dns_names[] =LDAP::fix($dn);
192 }
193 add_lock ($this->dns, $this->ui->dn);
195 /* Lock the current entry, so nobody will edit it during deletion */
196 $smarty = get_smarty();
197 $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
198 $smarty->assign("multiple", true);
199 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
200 }
201 }
204 protected function removeEntryConfirmed()
205 {
206 foreach($this->dns as $key => $dn){
208 /* Load permissions for selected 'dn' and check if
209 we're allowed to remove this 'dn' */
210 $acl = $this->ui->get_permissions($dn, $this->aclCategory."/".$this->aclPlugin);
211 if(preg_match("/d/",$acl)){
213 /* Delete request is permitted, perform LDAP action */
214 $this->dn = $dn;
215 $tab = $this->tabClass;
216 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
217 $this->tabObject->set_acl_base($this->dn);
218 $this->tabObject->delete ();
219 del_lock($this->dn);
220 } else {
222 /* Normally this shouldn't be reached, send some extra
223 logs to notify the administrator */
224 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
225 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
226 }
227 }
229 $this->remove_lock();
230 $this->closeDialogs();
231 }
234 function detectPostActions()
235 {
236 $action= $this->headpage->getAction();
238 if(isset($_POST['edit_apply'])) $action['action'] = "apply";
239 if(isset($_POST['edit_finish'])) $action['action'] = "save";
240 if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
241 if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";
243 return($action);
244 }
246 function handleActions($action)
247 {
248 // Start action
249 if(isset($this->actions[$action['action']])){
250 $func = $this->actions[$action['action']];
251 if(!isset($action['targets']))$action['targets']= array();
252 return($this->$func($action['action'],$action['targets'],$action));
253 }
254 }
256 function newEntry($action="",$target=array(),$all=array())
257 {
258 // Check locking & lock entry if required
259 $this->displayApplyBtn = FALSE;
260 $this->dn = "new";
261 $this->is_new = TRUE;
262 $this->is_single_edit = FALSE;
263 $this->is_multiple_edit = FALSE;
265 set_object_info($this->dn);
267 // Open object.
268 if(empty($this->tabClass) || empty($this->tabType)){
269 // No tab type defined
270 }else{
271 $tab = $this->tabClass;
272 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn, $this->aclCategory);
273 $this->tabObject->set_acl_base($this->headpage->getBase());
274 }
275 }
277 function editEntry($action,$target,$all)
278 {
279 if(count($target) == 0){
280 //nothing
281 }elseif(count($target) == 1){
283 // Check locking & lock entry if required
284 $this->displayApplyBtn = TRUE;
286 $this->is_new = FALSE;
287 $this->is_single_edit = TRUE;
288 $this->is_multiple_edit = FALSE;
290 $this->dn = array_pop($target);
291 set_object_info($this->dn);
292 $user = get_lock($this->dn);
293 if ($user != ""){
294 return(gen_locked_message ($user, $this->dn,TRUE));
295 }
296 add_lock ($this->dn, $this->ui->dn);
298 // Open object.
299 if(empty($this->tabClass) || empty($this->tabType)){
300 // No tab type defined
301 }else{
302 $tab = $this->tabClass;
303 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$this->tabType], $this->dn,$this->aclCategory);
304 $this->tabObject->set_acl_base($this->dn);
305 }
306 }else{
308 $this->is_new = FALSE;
309 $this->is_singel_edit = FALSE;
310 $this->is_multiple_edit = TRUE;
312 $this->dns = $target;
313 $tmp = new multi_plug($this->config,$this->tabClass,$this->config->data['TABS'][$this->tabType],
314 $this->dns,$this->headpage->getBase(),$this->aclCategory);
315 if ($tmp->entries_locked()){
316 return($tmp->display_lock_message());
317 }
318 $tmp->lock_entries($this->ui->dn);
319 if($tmp->multiple_available()){
320 $this->tabObject = $tmp;
321 set_object_info($this->tabObject->get_object_info());
322 }
324 }
325 }
327 protected function saveChanges()
328 {
329 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
330 $this->tabObject->save_object();
331 $msgs = $this->tabObject->check();
332 if(count($msgs)){
333 msg_dialog::displayChecks($msgs);
334 return("");
335 }else{
336 $this->tabObject->save();
337 $this->remove_lock();
338 $this->closeDialogs();
339 }
340 }
341 }
343 protected function applyChanges()
344 {
345 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
346 $this->tabObject->save_object();
347 $msgs = $this->tabObject->check();
348 if(count($msgs)){
349 msg_dialog::displayChecks($msgs);
350 return("");
351 }else{
352 $this->tabObject->save();
353 $this->tabObject->re_init();
354 }
355 }
356 }
358 protected function closeDialogs()
359 {
360 $this->dn = "";
361 $this->dns = array();
362 $this->tabObject = null;
363 $this->dialogObject = null;
364 set_object_info();
365 }
367 protected function cancelEdit()
368 {
369 $this->remove_lock();
370 $this->closeDialogs();
371 }
376 function registerAction($action,$target)
377 {
378 $this->actions[$action] = $target;
379 }
381 function remove_lock()
382 {
383 if(!empty($this->dn) && $this->dn != "new"){
384 del_lock($this->dn);
385 }
386 if(count($this->dns)){
387 del_lock($this->dns);
388 }
389 }
391 function copyPasteHandler($s_action,$s_entry)
392 {
394 /* Check if Copy & Paste is disabled */
395 if(!is_object($this->cpHandler)){
396 return("");
397 }
399 $this->cpHandler->save_object();
401 /* Add entries to queue */
402 if($s_action == "copy" || $s_action == "cut"){
404 /* Cleanup object queue */
405 $this->cpHandler->cleanup_queue();
407 /* Add new entries to CP queue */
408 foreach($s_entry as $dn){
409 if($s_action == "copy" && $this->ui->is_copyable($dn,$this->aclCategory,$this->aclPlugin)){
410 $this->cpHandler->add_to_queue($dn,"copy",$this->tabClass,$this->tabType,$this->aclCategory);
411 }
412 if($s_action == "cut" && $this->ui->is_cutable($dn,$this->aclCategory,$this->aclPlugin)){
413 $this->cpHandler->add_to_queue($dn,"cut",$this->tabClass,$this->tabType,$this->aclCategory);
414 }
415 }
416 }
418 /* Start pasting entries */
419 if($s_action == "paste"){
420 $this->cpPastingStarted = TRUE;
421 }
423 /* Return C&P dialog */
424 if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
426 /* Get dialog */
427 $this->cpHandler->SetVar("base",$this->headpage->getBase());
428 $data = $this->cpHandler->execute();
430 /* Return dialog data */
431 if(!empty($data)){
432 return($data);
433 }
434 }
436 /* Automatically disable status for pasting */
437 if(!$this->cpHandler->entries_queued()){
438 $this->cpPastingStarted = FALSE;
439 }
440 return("");
441 }
444 function setDescription($str) {
445 $this->plDescription = $str;
446 }
447 function setHeadpage($str) {
448 $this->headpage = $str;
449 }
450 function setFilter($str) {
451 $this->filter = $str;
452 }
453 function setIcon($str) {
454 $this->plIcon = $str;
455 }
456 function setHeadline($str) {
457 $this->plHeadline = $str;
458 }
459 }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>