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$$
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 // The plugin description
30 public $plugname = "unconfigured";
31 public $plIcon = "unconfigured";
32 public $plDescription = "unconfigured";
33 public $plHeadline = "unconfigured";
35 // The currently used object(s) (e.g. in edit, removal)
36 public $dn = ""; // this is public due to some compatibility problems with class plugin..
37 protected $dns = array();
39 // The last used object(s).
40 protected $last_dn = "";
41 protected $last_dns = array();
43 // The common places the displayed objects are stored in. (e.g. array("ou=groups,",".."))
44 protected $storagePoints = array();
46 // The tab definitions to use for the current object.
47 protected $tabClass = ""; // e.g. usertabs
48 protected $tabType = ""; // e.g. USERTABS
49 protected $aclPlugin = ""; // e.g. generic
50 protected $aclCategory = ""; // e.g. users
51 protected $objectName = ""; // e.g. users
53 // The opened object.
54 protected $tabObject = null;
55 protected $dialogObject = null;
57 // The last opened object.
58 protected $last_tabObject = null;
59 protected $last_dialogObject = null;
61 // Whether to display the apply button or not
62 protected $displayApplyBtn = FALSE;
64 // Whether to display a header or not.
65 protected $skipHeader = false;
67 // Whether to display a footer or not.
68 protected $skipFooter = false;
70 // Copy&Paste handler
71 protected $cpHandler = null;
73 // Indicates that we want to paste objects right now.
74 protected $cpPastingStarted = FALSE;
76 // The Snapshot handler class.
77 protected $snapHandler = null;
79 // The listing handlers
80 private $headpage = null;
81 private $filter = null;
83 // A list of configured actions/events
84 protected $actions = array();
86 // Attributes managed by this plugin, can be used in post events;
87 public $attributes = array();
89 function __construct(&$config,$ui,$plugname, $headpage)
90 {
91 $this->plugname = $plugname;
92 $this->headpage = $headpage;
93 $this->ui = $ui;
94 $this->config = $config;
96 if($this->cpHandler) $this->headpage->setCopyPasteHandler($this->cpHandler);
97 if($this->snapHandler) $this->headpage->setSnapshotHandler($this->snapHandler);
99 if(empty($this->plIcon)){
100 $this->plIcon = "plugins/".$plugname."/images/plugin.png";
101 }
103 // Register default actions
104 $this->registerAction("new", "newEntry");
105 $this->registerAction("edit", "editEntry");
106 $this->registerAction("apply", "applyChanges");
107 $this->registerAction("save", "saveChanges");
108 $this->registerAction("cancel", "cancelEdit");
109 $this->registerAction("cancelDelete", "cancelEdit");
110 $this->registerAction("remove", "removeEntryRequested");
111 $this->registerAction("removeConfirmed", "removeEntryConfirmed");
113 $this->registerAction("copy", "copyPasteHandler");
114 $this->registerAction("cut", "copyPasteHandler");
115 $this->registerAction("paste", "copyPasteHandler");
117 $this->registerAction("snapshot", "createSnapshotDialog");
118 $this->registerAction("restore", "restoreSnapshotDialog");
119 $this->registerAction("saveSnapshot","saveSnapshot");
120 $this->registerAction("restoreSnapshot","restoreSnapshot");
121 $this->registerAction("cancelSnapshot","closeDialogs");
123 $this->registerAction("config-filter","editFilter");
124 $this->registerAction("saveFilter","saveFilter");
125 $this->registerAction("cancelFilter","cancelFilter");
127 // To temporay disable the filter caching UNcomment this line.
128 #session::global_un_set(get_class($this)."_filter");
129 }
131 /*! \brief Execute this plugin
132 * Handle actions/events, locking, snapshots, dialogs, tabs,...
133 */
134 function execute()
135 {
136 // Ensure that html posts and gets are kept even if we see a 'Entry islocked' dialog.
137 $vars = array('/^act$/','/^listing/','/^PID$/','/^FILTER_PID$/');
138 session::set('LOCK_VARS_TO_USE',$vars);
140 pathNavigator::registerPlugin($this);
142 /* Display the copy & paste dialog, if it is currently open */
143 $ret = $this->copyPasteHandler("",array());
144 if($ret){
145 return($this->getHeader().$ret);
146 }
148 // Update filter
149 if ($this->filter) {
150 $this->filter->update();
151 session::global_set(get_class($this)."_filter", $this->filter);
152 session::set('autocomplete', $this->filter);
153 }
155 // Handle actions (POSTs and GETs)
156 $str = $this->handleActions($this->detectPostActions());
157 if($str) return($this->getHeader().$str);
159 // Open single dialog objects
160 if(is_object($this->dialogObject)){
161 if(method_exists($this->dialogObject,'save_object')) $this->dialogObject->save_object();
162 if(method_exists($this->dialogObject,'execute')){
163 $display = $this->dialogObject->execute();
164 $display.= $this->_getTabFooter();
165 return($this->getHeader().$display);
166 }
167 }
169 // Display tab object.
170 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
171 # $this->tabObject->save_object();
172 $display = $this->tabObject->execute();
173 $display.= $this->_getTabFooter();
174 return($this->getHeader().$display);
175 }
177 // Set current restore base for snapshot handling.
178 if(is_object($this->snapHandler)){
179 $bases = array();
180 foreach($this->storagePoints as $sp){
181 $bases[] = $sp.$this->headpage->getBase();
182 }
184 // No bases specified? Try base
185 if(!count($bases)) $bases[] = $this->headpage->getBase();
187 $this->snapHandler->setSnapshotBases($bases);
188 }
190 // Display list
191 return($this->renderList());
192 }
194 function editFilter()
195 {
196 $this->dialogObject = new userFilter($this->config,$this->getHeadpage());
197 }
199 function renderList()
200 {
201 $this->headpage->update();
202 $display = $this->headpage->render();
203 return($this->getHeader().$display);
204 }
206 function getHeadpage()
207 {
208 return($this->headpage);
209 }
211 function getFilter()
212 {
213 return($this->filter);
214 }
216 /*! \brief Generates the plugin header which is displayed whenever a tab object is
217 * opened.
218 */
219 protected function getHeader()
220 {
221 // We do not display any headers right now.
222 if(1 || $this->skipHeader) return("");
223 }
226 /*! \brief Generates the footer which is used whenever a tab object is
227 * displayed.
228 */
229 protected function _getTabFooter()
230 {
231 // Do not display tab footer for non tab objects
232 if(!($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug)){
233 return("");
234 }
236 // Check if there is a dialog opened - We don't need any buttons in this case.
237 if($this->tabObject->by_object[$this->tabObject->current]){
238 $current = $this->tabObject->by_object[$this->tabObject->current];
239 if(isset($current->dialog) && (is_object($current->dialog) || $current->dialog)){
240 return("");
241 }
242 }
244 // Skip footer if requested;
245 if($this->skipFooter) return("");
247 // In case an of locked entry, we may have opened a read-only tab.
248 $str = "";
249 if(isset($this->tabObject->read_only) && $this->tabObject->read_only == TRUE){
250 $str.= "
251 <p style=\"text-align:right\">
252 <button type=submit name=\"edit_cancel\">".msgPool::cancelButton()."</button>
253 </p>";
254 return($str);
255 }else{
257 // Display ok, (apply) and cancel buttons
258 $str.= "<p style=\"text-align:right\">\n";
259 $str.= "<button type=\"submit\" name=\"edit_finish\">".msgPool::okButton()."</button>\n";
260 $str.= " \n";
261 if($this->displayApplyBtn){
262 $str.= "<button type=\"submit\" name=\"edit_apply\">".msgPool::applyButton()."</button>\n";
263 $str.= " \n";
264 }
265 $str.= "<button type=\"submit\" name=\"edit_cancel\">".msgPool::cancelButton()."</button>\n";
266 $str.= "</p>";
267 }
268 return($str);
269 }
272 /*! \brief Initiates the removal for the given entries
273 * and displays a confirmation dialog.
274 *
275 * @param String 'action' The name of the action which was the used as trigger.
276 * @param Array 'target' A list of object dns, which should be affected by this method.
277 * @param Array 'all' A combination of both 'action' and 'target'.
278 */
279 protected function removeEntryRequested($action="",$target=array(),$all=array())
280 {
281 // Close dialogs and remove locks for currently handled dns
282 $this->cancelEdit();
284 $disallowed = array();
285 $this->dns = array();
287 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
289 // Check permissons for each target
290 $h = $this->getHeadpage();
291 $oTypes = array_reverse($h->objectTypes);
292 foreach($target as $dn){
293 $entry = $h->getEntry($dn);
294 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
295 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
296 if(preg_match("/d/",$acl)){
297 $this->dns[] = $dn;
298 }else{
299 $disallowed[] = $dn;
300 }
301 }
302 if(count($disallowed)){
303 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
304 }
306 // We've at least one entry to delete.
307 if(count($this->dns)){
309 // check locks
310 if ($user= get_multiple_locks($this->dns)){
311 return(gen_locked_message($user,$this->dns));
312 }
314 // Add locks
315 $dns_names = array();
316 $types = array();
318 // Build list of object -labels
319 foreach($h->objectTypes as $type){
320 $map[$type['objectClass']]= $type['label'];
321 }
323 foreach($this->dns as $dn){
324 $tmp = $h->getType($dn);
325 if(isset($map[$tmp])){
326 $dns_names[LDAP::fix($dn)] = _($map[$tmp]);
327 }else{
328 $dns_names[] =LDAP::fix($dn);
329 }
330 }
331 add_lock ($this->dns, $this->ui->dn);
333 // Display confirmation dialog.
334 $smarty = get_smarty();
335 $smarty->assign("info", msgPool::deleteInfo($dns_names));
336 $smarty->assign("multiple", true);
337 return($smarty->fetch(get_template_path('removeEntries.tpl')));
338 }
339 }
342 /*! \brief Object removal was confirmed, now remove the requested entries.
343 *
344 * @param String 'action' The name of the action which was the used as trigger.
345 * @param Array 'target' A list of object dns, which should be affected by this method.
346 * @param Array 'all' A combination of both 'action' and 'target'.
347 */
348 function removeEntryConfirmed($action="",$target=array(),$all=array(),
349 $altTabClass="",$altTabType="", $altAclCategory="",$altAclPlugin="")
350 {
351 $tabType = $this->tabType;
352 $tabClass = $this->tabClass;
353 $aclCategory = $this->aclCategory;
354 $aclPlugin = $this->aclPlugin;
355 if(!empty($altTabClass)) $tabClass = $altTabClass;
356 if(!empty($altTabType)) $tabType = $altTabType;
357 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
358 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
360 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel confirmed!");
362 // Check permissons for each target
363 $h = $this->getHeadpage();
364 $oTypes = array_reverse($h->objectTypes);
365 foreach($this->dns as $key => $dn){
366 $entry = $h->getEntry($dn);
367 $obj = $h->getObjectType($oTypes, $entry['objectClass']);
368 $acl = $this->ui->get_permissions($dn, $obj['category']."/".$obj['class']);
370 // Check permissions, are we allowed to remove this object?
371 if(preg_match("/d/",$acl)){
373 // Delete the object
374 $this->dn = $dn;
375 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn,
376 $aclCategory, true, true);
377 $this->tabObject->set_acl_base($this->dn);
378 $this->tabObject->parent = &$this;
379 $this->tabObject->delete ();
381 // Remove the lock for the current object.
382 del_lock($this->dn);
383 } else {
384 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
385 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
386 }
387 }
389 // Cleanup
390 $this->remove_lock();
391 $this->closeDialogs();
392 }
395 /*! \brief Detects actions/events send by the ui
396 * and the corresponding targets.
397 */
398 function detectPostActions()
399 {
400 if(!is_object($this->headpage)){
401 trigger_error("No valid headpage given....!");
402 return(array());
403 }
404 $action= $this->headpage->getAction();
405 if(isset($_POST['edit_apply'])) $action['action'] = "apply";
406 if(isset($_POST['edit_finish'])) $action['action'] = "save";
407 if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
408 if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";
409 if(isset($_POST['delete_cancel'])) $action['action'] = "cancelDelete";
410 if(isset($_POST['saveFilter'])) $action['action'] = "saveFilter";
411 if(isset($_POST['cancelFilter'])) $action['action'] = "cancelFilter";
413 // Detect Snapshot actions
414 if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";
415 if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";
416 foreach($_POST as $name => $value){
417 $once =TRUE;
418 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
419 $once = FALSE;
420 $entry = base64_decode(preg_replace("/^RestoreSnapShot_(.*)$/i","\\1",$name));
421 $action['action'] = "restoreSnapshot";
422 $action['targets'] = array($entry);
423 }
424 }
426 return($action);
427 }
430 /*! \brief Calls the registered method for a given action/event.
431 */
432 function handleActions($action)
433 {
434 // Start action
435 if(isset($this->actions[$action['action']])){
436 $func = $this->actions[$action['action']];
437 if(!isset($action['targets']))$action['targets']= array();
438 return($this->$func($action['action'],$action['targets'],$action));
439 }
440 }
443 /*! \brief Opens the snapshot creation dialog for the given target.
444 *
445 * @param String 'action' The name of the action which was the used as trigger.
446 * @param Array 'target' A list of object dns, which should be affected by this method.
447 * @param Array 'all' A combination of both 'action' and 'target'.
448 */
449 function createSnapshotDialog($action="",$target=array(),$all=array())
450 {
451 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Snaptshot creation initiated!");
453 foreach($target as $entry){
454 if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
455 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
456 $this->dialogObject->aclCategories = array($this->aclCategory);
457 $this->dialogObject->parent = &$this;
459 }else{
460 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s!"), bold($entry)),
461 ERROR_DIALOG);
462 }
463 }
464 }
467 /*! \brief Creates a snapshot new entry - This method is called when the somebody
468 * clicks 'save' in the "Create snapshot dialog" (see ::createSnapshotDialog).
469 *
470 * @param String 'action' The name of the action which was the used as trigger.
471 * @param Array 'target' A list of object dns, which should be affected by this method.
472 * @param Array 'all' A combination of both 'action' and 'target'.
473 */
474 function saveSnapshot($action="",$target=array(),$all=array())
475 {
476 if(!is_object($this->dialogObject)) return;
477 $this->dialogObject->save_object();
478 $msgs = $this->dialogObject->check();
479 if(count($msgs)){
480 foreach($msgs as $msg){
481 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
482 }
483 }else{
484 $this->dn = $this->dialogObject->dn;
485 $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
486 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot created!");
487 $this->closeDialogs();
488 }
489 }
492 /*! \brief Restores a snapshot object.
493 * The dn of the snapshot entry has to be given as ['target'] parameter.
494 *
495 * @param String 'action' The name of the action which was the used as trigger.
496 * @param Array 'target' A list of object dns, which should be affected by this method.
497 * @param Array 'all' A combination of both 'action' and 'target'.
498 */
499 function restoreSnapshot($action="",$target=array(),$all=array())
500 {
501 $entry = array_pop($target);
502 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
503 $this->snapHandler->restore_snapshot($entry);
504 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Snaptshot restored!");
505 $this->closeDialogs();
506 }else{
507 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
508 ERROR_DIALOG);
509 }
510 }
513 /*! \brief Displays the "Restore snapshot dialog" for a given target.
514 * If no target is specified, open the restore removed object
515 * dialog.
516 * @param String 'action' The name of the action which was the used as trigger.
517 * @param Array 'target' A list of object dns, which should be affected by this method.
518 * @param Array 'all' A combination of both 'action' and 'target'.
519 */
520 function restoreSnapshotDialog($action="",$target=array(),$all=array())
521 {
522 // Set current restore base for snapshot handling.
523 if(is_object($this->snapHandler)){
524 $bases = array();
525 foreach($this->storagePoints as $sp){
526 $bases[] = $sp.$this->headpage->getBase();
527 }
528 }
530 // No bases specified? Try base
531 if(!count($bases)) $bases[] = $this->headpage->getBase();
533 // No target, open the restore removed object dialog.
534 if(!count($target)){
535 $entry = $this->headpage->getBase();
536 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
537 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
538 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
539 $this->dialogObject->set_snapshot_bases($bases);
540 $this->dialogObject->display_all_removed_objects = true;
541 $this->dialogObject->display_restore_dialog = true;
542 $this->dialogObject->parent = &$this;
543 }else{
544 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
545 ERROR_DIALOG);
546 }
547 }else{
549 // Display the restore points for a given object.
550 $entry = array_pop($target);
551 if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
552 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$entry,"Snaptshot restoring initiated!");
553 $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
554 $this->dialogObject->set_snapshot_bases($bases);
555 $this->dialogObject->display_restore_dialog = true;
556 $this->dialogObject->parent = &$this;
557 }else{
558 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s!"), bold($entry)),
559 ERROR_DIALOG);
560 }
561 }
562 }
565 /*! \brief This method intiates the object creation.
566 *
567 * @param String 'action' The name of the action which was the used as trigger.
568 * @param Array 'target' A list of object dns, which should be affected by this method.
569 * @param Array 'all' A combination of both 'action' and 'target'.
570 */
571 function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
572 {
573 /* To handle mutliple object types overload this method.
574 * ...
575 * registerAction('newUser', 'newEntry');
576 * registerAction('newGroup','newEntry');
577 * ...
578 *
579 * function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
580 * {
581 * switch($action){
582 * case 'newUser' : {
583 * mangement::newEntry($action,$target,$all,"usertabs","USERTABS","users");
584 * }
585 * case 'newGroup' : {
586 * mangement::newEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
587 * }
588 * }
589 * }
590 **/
591 $tabType = $this->tabType;
592 $tabClass = $this->tabClass;
593 $aclCategory = $this->aclCategory;
594 if(!empty($altTabClass)) $tabClass = $altTabClass;
595 if(!empty($altTabType)) $tabType = $altTabType;
596 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
598 // Check locking & lock entry if required
599 $this->displayApplyBtn = FALSE;
600 $this->dn = "new";
601 $this->is_new = TRUE;
602 $this->is_single_edit = FALSE;
603 $this->is_multiple_edit = FALSE;
605 set_object_info($this->dn);
607 // Open object.
608 if(empty($tabClass) || empty($tabType)){
609 // No tab type defined
610 }else{
611 if (isset($this->config->data['TABS'][$tabType])) {
612 $this->tabObject= new $tabClass($this->config,$this->config->data['TABS'][$tabType], $this->dn, $aclCategory);
613 $this->tabObject->set_acl_base($this->headpage->getBase());
614 $this->tabObject->parent = &$this;
615 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Create new entry initiated!");
616 } else {
617 msg_dialog::display(_("Error"), sprintf(_("No tab definition for %s found in configuration file: cannot create plugin instance!"), bold($tabType)), ERROR_DIALOG);
618 }
619 }
620 }
623 /*! \brief This method opens an existing object or a list of existing objects to be edited.
624 *
625 *
626 * @param String 'action' The name of the action which was the used as trigger.
627 * @param Array 'target' A list of object dns, which should be affected by this method.
628 * @param Array 'all' A combination of both 'action' and 'target'.
629 */
630 function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
631 {
632 /* To handle mutliple object types overload this method.
633 * ...
634 * registerAction('editUser', 'editEntry');
635 * registerAction('editGroup','editEntry');
636 * ...
637 *
638 * function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory)
639 * {
640 * switch($action){
641 * case 'editUser' : {
642 * mangement::editEntry($action,$target,$all,"usertabs","USERTABS","users");
643 * }
644 * case 'editGroup' : {
645 * mangement::editEntry($action,$target,$all,"grouptabs","GROUPTABS","groups");
646 * }
647 * }
648 * }
649 **/
651 // Do not create a new tabObject while there is already one opened,
652 // the user may have just pressed F5 to reload the page.
653 if(is_object($this->tabObject)){
654 return;
655 }
657 $tabType = $this->tabType;
658 $tabClass = $this->tabClass;
659 $aclCategory = $this->aclCategory;
660 if(!empty($altTabClass)) $tabClass = $altTabClass;
661 if(!empty($altTabType)) $tabType = $altTabType;
662 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
664 // Single edit - we only got one object dn.
665 if(count($target) == 1){
666 $this->displayApplyBtn = TRUE;
667 $this->is_new = FALSE;
668 $this->is_single_edit = TRUE;
669 $this->is_multiple_edit = FALSE;
671 // Get the dn of the object and creates lock
672 $this->dn = array_pop($target);
673 set_object_info($this->dn);
674 $user = get_lock($this->dn);
675 if ($user != ""){
676 return(gen_locked_message ($user, array($this->dn),TRUE));
677 }
678 add_lock ($this->dn, $this->ui->dn);
680 // Open object.
681 if(empty($tabClass) || empty($tabType)){
682 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
683 }else{
684 $tab = $tabClass;
685 $this->tabObject= new $tab($this->config,$this->config->data['TABS'][$tabType], $this->dn,$aclCategory);
686 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dn,"Edit entry initiated!");
687 $this->tabObject->set_acl_base($this->dn);
688 $this->tabObject->parent = &$this;
689 }
690 }else{
692 // We've multiple entries to edit.
693 $this->is_new = FALSE;
694 $this->is_singel_edit = FALSE;
695 $this->is_multiple_edit = TRUE;
697 // Open multiple edit handler.
698 if(empty($tabClass) || empty($tabType)){
699 trigger_error("We can't edit any object(s). 'tabClass' or 'tabType' is empty!");
700 }else{
701 $this->dns = $target;
702 $tmp = new multi_plug($this->config,$tabClass,$this->config->data['TABS'][$tabType],
703 $this->dns,$this->headpage->getBase(),$aclCategory);
705 // Check for locked entries
706 if ($tmp->entries_locked()){
707 return($tmp->display_lock_message());
708 }
710 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Edit entry initiated!");
712 // Now lock entries.
713 if($tmp->multiple_available()){
714 $tmp->lock_entries($this->ui->dn);
715 $this->tabObject = $tmp;
716 set_object_info($this->tabObject->get_object_info());
717 }
718 }
719 }
720 }
723 /*! \brief Close filter dialog
724 */
725 protected function cancelFilter()
726 {
727 if($this->dialogObject instanceOf userFilter){
728 $this->remove_lock();
729 $this->closeDialogs();
730 }
731 }
734 /*! \brief Save filter modifcations.
735 */
736 protected function saveFilter()
737 {
738 if($this->dialogObject instanceOf userFilter){
739 $msgs = $this->dialogObject->check();
740 if(count($msgs)){
741 msg_dialog::displayChecks($msgs);
742 return("");
743 }else{
744 $this->dialogObject->save();
745 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
746 $this->remove_lock();
747 $this->closeDialogs();
749 // Ask filter to reload information
750 $this->filter->reloadFilters();
751 }
752 }
753 }
756 /*! \brief Save object modifications and closes dialogs (returns to object listing).
757 * - Calls '::check' to validate the given input.
758 * - Calls '::save' to save back object modifications (e.g. to ldap).
759 * - Calls '::remove_locks' to remove eventually created locks.
760 * - Calls '::closeDialogs' to return to the object listing.
761 */
762 protected function saveChanges()
763 {
764 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
765 $this->tabObject->save_object();
766 $msgs = $this->tabObject->check();
767 if(count($msgs)){
768 msg_dialog::displayChecks($msgs);
769 return("");
770 }else{
771 $this->tabObject->save();
772 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
773 $this->remove_lock();
774 $this->closeDialogs();
775 }
776 }elseif($this->dialogObject instanceOf plugin){
777 $this->dialogObject->save_object();
778 $msgs = $this->dialogObject->check();
779 if(count($msgs)){
780 msg_dialog::displayChecks($msgs);
781 return("");
782 }else{
783 $this->dialogObject->save();
784 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Entry saved!");
785 $this->remove_lock();
786 $this->closeDialogs();
787 }
788 }
789 }
792 /*! \brief Save object modifications and keep dialogs opened.
793 * - Calls '::check' to validate the given input.
794 * - Calls '::save' to save back object modifications (e.g. to ldap).
795 */
796 protected function applyChanges()
797 {
798 if($this->tabObject instanceOf tabs || $this->tabObject instanceOf multi_plug){
799 $this->tabObject->save_object();
800 $msgs = $this->tabObject->check();
801 if(count($msgs)){
802 msg_dialog::displayChecks($msgs);
803 return("");
804 }else{
805 $this->tabObject->save();
806 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$this->dns,"Modifications applied!");
807 $this->tabObject->re_init();
808 }
809 }
810 }
813 /*! \brief This method closes dialogs
814 * and cleans up the cached object info and the ui.
815 */
816 protected function closeDialogs()
817 {
818 $this->last_dn = $this->dn;
819 $this->last_dns = $this->dns;
820 $this->last_tabObject = $this->tabObject;
821 $this->last_dialogObject = $this->dialogObject;
822 $this->dn = "";
823 $this->dns = array();
824 $this->tabObject = null;
825 $this->dialogObject = null;
826 $this->skipFooter = FALSE;
827 set_object_info();
828 }
831 /*! \brief Editing an object was caneled.
832 * Close dialogs/tabs and remove locks.
833 */
834 protected function cancelEdit()
835 {
836 $this->remove_lock();
837 $this->closeDialogs();
838 }
841 /*! \brief Every click in the list user interface sends an event
842 * here can we connect those events to a method.
843 * eg. ::registerEvent('new','createUser')
844 * When the action/event new is send, the method 'createUser'
845 * will be called.
846 */
847 function registerAction($action,$target)
848 {
849 $this->actions[$action] = $target;
850 }
853 /*! \brief Removes ldap object locks created by this class.
854 * Whenever an object is edited, we create locks to avoid
855 * concurrent modifications.
856 * This locks will automatically removed here.
857 */
858 function remove_lock()
859 {
860 if(!empty($this->dn) && $this->dn != "new"){
861 del_lock($this->dn);
862 }
863 if(count($this->dns)){
864 del_lock($this->dns);
865 }
866 }
869 /*! \brief This method is used to queue and process copy&paste actions.
870 * Allows to copy, cut and paste mutliple entries at once.
871 * @param String 'action' The name of the action which was the used as trigger.
872 * @param Array 'target' A list of object dns, which should be affected by this method.
873 * @param Array 'all' A combination of both 'action' and 'target'.
874 */
875 function copyPasteHandler($action="",$target=array(),$all=array(),
876 $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
877 {
878 // Return without any actions while copy&paste handler is disabled.
879 if(!is_object($this->cpHandler)) return("");
881 $tabType = $this->tabType;
882 $tabClass = $this->tabClass;
883 $aclCategory = $this->aclCategory;
884 $aclPlugin = $this->aclPlugin;
885 if(!empty($altTabClass)) $tabClass = $altTabClass;
886 if(!empty($altTabType)) $tabType = $altTabType;
887 if(!empty($altAclCategory)) $aclCategory = $altAclCategory;
888 if(!empty($altAclPlugin)) $aclPlugin = $altAclPlugin;
890 // Save user input
891 $this->cpHandler->save_object();
893 // Add entries to queue
894 if($action == "copy" || $action == "cut"){
895 $this->cpHandler->cleanup_queue();
896 foreach($target as $dn){
897 if($action == "copy" && $this->ui->is_copyable($dn,$aclCategory,$aclPlugin)){
898 $this->cpHandler->add_to_queue($dn,"copy",$tabClass,$tabType,$aclCategory,$this);
899 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
900 }
901 if($action == "cut" && $this->ui->is_cutable($dn,$aclCategory,$aclPlugin)){
902 $this->cpHandler->add_to_queue($dn,"cut",$tabClass,$tabType,$aclCategory,$this);
903 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry cutted!");
904 }
905 }
906 }
908 // Initiate pasting
909 if($action == "paste"){
910 $this->cpPastingStarted = TRUE;
911 }
913 // Display any c&p dialogs, eg. object modifications required before pasting.
914 if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
915 $this->cpHandler->SetVar("base",$this->headpage->getBase());
916 $data = $this->cpHandler->execute();
917 if(!empty($data)){
918 return($data);
919 }
920 }
922 // Automatically disable pasting process since there is no entry left to paste.
923 if(!$this->cpHandler->entries_queued()){
924 $this->cpPastingStarted = FALSE;
925 }
926 return("");
927 }
930 function setFilter($str) {
931 $this->filter = $str;
932 }
935 function postcreate() {
936 $this->_handlePostEvent('POSTCREATE');
937 }
938 function postmodify(){
939 $this->_handlePostEvent('POSTMODIFY');
940 }
941 function postremove(){
942 $this->_handlePostEvent('POSTREMOVE');
943 }
945 function _handlePostEvent($type)
946 {
948 /* Find postcreate entries for this class */
949 $command= $this->config->search(get_class($this), $type,array('menu', 'tabs'));
950 if ($command != ""){
952 /* Walk through attribute list */
953 foreach ($this->attributes as $attr){
954 if (!is_array($this->$attr)){
955 $add_attrs[$attr] = $this->$attr;
956 }
957 }
958 $add_attrs['dn']=$this->dn;
960 $tmp = array();
961 foreach($add_attrs as $name => $value){
962 $tmp[$name] = strlen($name);
963 }
964 arsort($tmp);
966 /* Additional attributes */
967 foreach ($tmp as $name => $len){
968 $value = $add_attrs[$name];
969 $command= str_replace("%$name", "$value", $command);
970 }
972 if (check_command($command)){
973 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
974 $command, "Execute");
975 exec($command,$arr);
976 foreach($arr as $str){
977 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
978 $command, "Result: ".$str);
979 }
980 } else {
981 $message= msgPool::cmdnotfound($type, get_class($this));
982 msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
983 }
984 }
985 }
987 function is_modal_dialog()
988 {
989 return(is_object($this->tabObject) || is_object($this->dialogObject));
990 }
993 /*! \brief Forward command execution request
994 * to the correct method.
995 */
996 function handle_post_events($mode, $addAttrs= array())
997 {
998 if(!in_array($mode, array('add','remove','modify'))){
999 trigger_error(sprintf("Invalid post event type given %s! Valid types are [add,modify,remove].", bold($mode)));
1000 return;
1001 }
1002 switch ($mode){
1003 case "add":
1004 plugin::callHook($this,"POSTCREATE", $addAttrs);
1005 break;
1007 case "modify":
1008 plugin::callHook($this,"POSTMODIFY", $addAttrs);
1009 break;
1011 case "remove":
1012 plugin::callHook($this,"POSTREMOVE", $addAttrs);
1013 break;
1014 }
1015 }
1016 }
1018 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1019 ?>