f983a9d240397e2824cf60b5d2defcbb8b2d8738
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 /*! \brief The plugin base class
24 \author Cajus Pollmeier <pollmeier@gonicus.de>
25 \version 2.00
26 \date 24.07.2003
28 This is the base class for all plugins. It can be used standalone or
29 can be included by the tabs class. All management should be done
30 within this class. Extend your plugins from this class.
31 */
33 class plugin
34 {
35 /*!
36 \brief Reference to parent object
38 This variable is used when the plugin is included in tabs
39 and keeps reference to the tab class. Communication to other
40 tabs is possible by 'name'. So the 'fax' plugin can ask the
41 'userinfo' plugin for the fax number.
43 \sa tab
44 */
45 var $parent= NULL;
47 /*!
48 \brief Configuration container
50 Access to global configuration
51 */
52 var $config= NULL;
54 /*!
55 \brief Mark plugin as account
57 Defines whether this plugin is defined as an account or not.
58 This has consequences for the plugin to be saved from tab
59 mode. If it is set to 'FALSE' the tab will call the delete
60 function, else the save function. Should be set to 'TRUE' if
61 the construtor detects a valid LDAP object.
63 \sa plugin::plugin()
64 */
65 var $is_account= FALSE;
66 var $initially_was_account= FALSE;
68 /*!
69 \brief Mark plugin as template
71 Defines whether we are creating a template or a normal object.
72 Has conseqences on the way execute() shows the formular and how
73 save() puts the data to LDAP.
75 \sa plugin::save() plugin::execute()
76 */
77 var $is_template= FALSE;
78 var $ignore_account= FALSE;
79 var $is_modified= FALSE;
81 /*!
82 \brief Represent temporary LDAP data
84 This is only used internally.
85 */
86 var $attrs= array();
88 /* Keep set of conflicting plugins */
89 var $conflicts= array();
91 /* Save unit tags */
92 var $gosaUnitTag= "";
93 var $skipTagging= FALSE;
95 /*!
96 \brief Used standard values
98 dn
99 */
100 var $dn= "";
101 var $uid= "";
102 var $sn= "";
103 var $givenName= "";
104 var $acl= "*none*";
105 var $dialog= FALSE;
106 var $snapDialog = NULL;
108 /* attribute list for save action */
109 var $attributes= array();
110 var $objectclasses= array();
111 var $is_new= TRUE;
112 var $saved_attributes= array();
114 var $acl_base= "";
115 var $acl_category= "";
117 /* This can be set to render the tabulators in another stylesheet */
118 var $pl_notify= FALSE;
120 /* Object entry CSN */
121 var $entryCSN = "";
122 var $CSN_check_active = FALSE;
124 /* This variable indicates that this class can handle multiple dns at once. */
125 var $multiple_support = FALSE;
126 var $multi_attrs = array();
127 var $multi_attrs_all = array();
129 /* This aviable indicates, that we are currently in multiple edit handle */
130 var $multiple_support_active = FALSE;
131 var $selected_edit_values = array();
132 var $multi_boxes = array();
134 /*! \brief plugin constructor
136 If 'dn' is set, the node loads the given 'dn' from LDAP
138 \param dn Distinguished name to initialize plugin from
139 \sa plugin()
140 */
141 function plugin (&$config, $dn= NULL, $parent= NULL)
142 {
143 /* Configuration is fine, allways */
144 $this->config= &$config;
145 $this->dn= $dn;
147 /* Handle new accounts, don't read information from LDAP */
148 if ($dn == "new"){
149 return;
150 }
152 /* Save current dn as acl_base */
153 $this->acl_base= $dn;
155 /* Get LDAP descriptor */
156 if ($dn !== NULL){
158 /* Load data to 'attrs' and save 'dn' */
159 if ($parent !== NULL){
160 $this->attrs= $parent->attrs;
161 } else {
162 $ldap= $this->config->get_ldap_link();
163 $ldap->cat ($dn);
164 $this->attrs= $ldap->fetch();
165 }
167 /* Copy needed attributes */
168 foreach ($this->attributes as $val){
169 $found= array_key_ics($val, $this->attrs);
170 if ($found != ""){
171 $this->$val= $this->attrs["$found"][0];
172 }
173 }
175 /* gosaUnitTag loading... */
176 if (isset($this->attrs['gosaUnitTag'][0])){
177 $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
178 }
180 /* Set the template flag according to the existence of objectClass
181 gosaUserTemplate */
182 if (isset($this->attrs['objectClass'])){
183 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
184 $this->is_template= TRUE;
185 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
186 "found", "Template check");
187 }
188 }
190 /* Is Account? */
191 $found= TRUE;
192 foreach ($this->objectclasses as $obj){
193 if (preg_match('/top/i', $obj)){
194 continue;
195 }
196 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
197 $found= FALSE;
198 break;
199 }
200 }
201 if ($found){
202 $this->is_account= TRUE;
203 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
204 "found", "Object check");
205 }
207 /* Prepare saved attributes */
208 $this->saved_attributes= $this->attrs;
209 foreach ($this->saved_attributes as $index => $value){
210 if (preg_match('/^[0-9]+$/', $index)){
211 unset($this->saved_attributes[$index]);
212 continue;
213 }
214 if (!in_array($index, $this->attributes) && $index != "objectClass"){
215 unset($this->saved_attributes[$index]);
216 continue;
217 }
219 if (isset($this->saved_attributes[$index][0])){
220 if(!isset($this->saved_attributes[$index]["count"])){
221 $this->saved_attributes[$index]["count"] = count($this->saved_attributes[$index]);
222 }
223 if($this->saved_attributes[$index]["count"] == 1){
224 $tmp= $this->saved_attributes[$index][0];
225 unset($this->saved_attributes[$index]);
226 $this->saved_attributes[$index]= $tmp;
227 continue;
228 }
229 }
230 unset($this->saved_attributes["$index"]["count"]);
231 }
232 if(isset($this->attrs['gosaUnitTag'])){
233 $this->saved_attributes['gosaUnitTag'] = $this->attrs['gosaUnitTag'][0];
234 }
235 }
237 /* Save initial account state */
238 $this->initially_was_account= $this->is_account;
239 }
242 /*! \brief execute plugin
244 Generates the html output for this node
245 */
246 function execute()
247 {
248 /* This one is empty currently. Fabian - please fill in the docu code */
249 session::set('current_class_for_help',get_class($this));
251 /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
252 session::set('LOCK_VARS_TO_USE',array());
253 session::set('LOCK_VARS_USED',array());
254 }
256 /*! \brief execute plugin
257 Removes object from parent
258 */
259 function remove_from_parent()
260 {
261 /* include global link_info */
262 $ldap= $this->config->get_ldap_link();
264 /* Get current objectClasses in order to add the required ones */
265 $ldap->cat($this->dn);
266 $tmp= $ldap->fetch ();
267 $oc= array();
268 if (isset($tmp['objectClass'])){
269 $oc= $tmp['objectClass'];
270 unset($oc['count']);
271 }
273 /* Remove objectClasses from entry */
274 $ldap->cd($this->dn);
275 $this->attrs= array();
276 $this->attrs['objectClass']= array_remove_entries($this->objectclasses,$oc);
278 /* Unset attributes from entry */
279 foreach ($this->attributes as $val){
280 $this->attrs["$val"]= array();
281 }
283 /* Unset account info */
284 $this->is_account= FALSE;
286 /* Do not write in plugin base class, this must be done by
287 children, since there are normally additional attribs,
288 lists, etc. */
289 /*
290 $ldap->modify($this->attrs);
291 */
292 }
295 /*! \brief Save HTML posted data to object
296 */
297 function save_object()
298 {
299 /* Update entry CSN if it is empty. */
300 if(empty($this->entryCSN) && $this->CSN_check_active){
301 $this->entryCSN = getEntryCSN($this->dn);
302 }
304 /* Save values to object */
305 foreach ($this->attributes as $val){
306 if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
307 /* Check for modifications */
308 if (get_magic_quotes_gpc()) {
309 $data= stripcslashes($_POST["$val"]);
310 } else {
311 $data= $this->$val = $_POST["$val"];
312 }
313 if ($this->$val != $data){
314 $this->is_modified= TRUE;
315 }
317 /* Okay, how can I explain this fix ...
318 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
319 * So IE posts these 'unselectable' option, with value = chr(194)
320 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
321 * This was added for W3c compliance, but now causes these ... ldap errors ...
322 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
323 */
324 if(isset($data[0]) && $data[0] == chr(194)) {
325 $data = "";
326 }
327 $this->$val= $data;
328 }
329 }
330 }
333 /* Save data to LDAP, depending on is_account we save or delete */
334 function save()
335 {
336 /* include global link_info */
337 $ldap= $this->config->get_ldap_link();
339 /* Save all plugins */
340 $this->entryCSN = "";
342 /* Start with empty array */
343 $this->attrs= array();
345 /* Get current objectClasses in order to add the required ones */
346 $ldap->cat($this->dn);
348 $tmp= $ldap->fetch ();
350 $oc= array();
351 if (isset($tmp['objectClass'])){
352 $oc= $tmp["objectClass"];
353 $this->is_new= FALSE;
354 unset($oc['count']);
355 } else {
356 $this->is_new= TRUE;
357 }
359 /* Load (minimum) attributes, add missing ones */
360 $this->attrs['objectClass']= gosa_array_merge($oc,$this->objectclasses);
362 /* Copy standard attributes */
363 foreach ($this->attributes as $val){
364 if ($this->$val != ""){
365 $this->attrs["$val"]= $this->$val;
366 } elseif (!$this->is_new) {
367 $this->attrs["$val"]= array();
368 }
369 }
371 /* Handle tagging */
372 $this->tag_attrs($this->attrs);
373 }
376 function cleanup()
377 {
378 foreach ($this->attrs as $index => $value){
380 /* Convert arrays with one element to non arrays, if the saved
381 attributes are no array, too */
382 if (is_array($this->attrs[$index]) &&
383 count ($this->attrs[$index]) == 1 &&
384 isset($this->saved_attributes[$index]) &&
385 !is_array($this->saved_attributes[$index])){
387 $tmp= $this->attrs[$index][0];
388 $this->attrs[$index]= $tmp;
389 }
391 /* Remove emtpy arrays if they do not differ */
392 if (is_array($this->attrs[$index]) &&
393 count($this->attrs[$index]) == 0 &&
394 !isset($this->saved_attributes[$index])){
396 unset ($this->attrs[$index]);
397 continue;
398 }
400 /* Remove single attributes that do not differ */
401 if (!is_array($this->attrs[$index]) &&
402 isset($this->saved_attributes[$index]) &&
403 !is_array($this->saved_attributes[$index]) &&
404 $this->attrs[$index] == $this->saved_attributes[$index]){
406 unset ($this->attrs[$index]);
407 continue;
408 }
410 /* Remove arrays that do not differ */
411 if (is_array($this->attrs[$index]) &&
412 isset($this->saved_attributes[$index]) &&
413 is_array($this->saved_attributes[$index])){
415 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
416 unset ($this->attrs[$index]);
417 continue;
418 }
419 }
420 }
422 /* Update saved attributes and ensure that next cleanups will be successful too */
423 foreach($this->attrs as $name => $value){
424 $this->saved_attributes[$name] = $value;
425 }
426 }
428 /* Check formular input */
429 function check()
430 {
431 $message= array();
433 /* Skip if we've no config object */
434 if (!isset($this->config) || !is_object($this->config)){
435 return $message;
436 }
438 /* Find hooks entries for this class */
439 $command= $this->config->search(get_class($this), "CHECK", array('menu', 'tabs'));
441 if ($command != ""){
443 if (!check_command($command)){
444 $message[]= msgPool::cmdnotfound("CHECK", get_class($this));
445 } else {
447 /* Generate "ldif" for check hook */
448 $ldif= "dn: $this->dn\n";
450 /* ... objectClasses */
451 foreach ($this->objectclasses as $oc){
452 $ldif.= "objectClass: $oc\n";
453 }
455 /* ... attributes */
456 foreach ($this->attributes as $attr){
457 if ($this->$attr == ""){
458 continue;
459 }
460 if (is_array($this->$attr)){
461 foreach ($this->$attr as $val){
462 $ldif.= "$attr: $val\n";
463 }
464 } else {
465 $ldif.= "$attr: ".$this->$attr."\n";
466 }
467 }
469 /* Append empty line */
470 $ldif.= "\n";
472 /* Feed "ldif" into hook and retrieve result*/
473 $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
474 $fh= proc_open($command, $descriptorspec, $pipes);
475 if (is_resource($fh)) {
476 fwrite ($pipes[0], $ldif);
477 fclose($pipes[0]);
479 $result= stream_get_contents($pipes[1]);
480 if ($result != ""){
481 $message[]= $result;
482 }
484 fclose($pipes[1]);
485 fclose($pipes[2]);
486 proc_close($fh);
487 }
488 }
490 }
492 /* Check entryCSN */
493 if($this->CSN_check_active){
494 $current_csn = getEntryCSN($this->dn);
495 if($current_csn != $this->entryCSN && !empty($this->entryCSN) && !empty($current_csn)){
496 $this->entryCSN = $current_csn;
497 $message[] = _("The object has changed since opened in GOsa. All changes that may be done by others get lost if you save this entry!");
498 }
499 }
500 return ($message);
501 }
503 /* Adapt from template, using 'dn' */
504 function adapt_from_template($dn, $skip= array())
505 {
506 /* Include global link_info */
507 $ldap= $this->config->get_ldap_link();
509 /* Load requested 'dn' to 'attrs' */
510 $ldap->cat ($dn);
511 $this->attrs= $ldap->fetch();
513 /* Walk through attributes */
514 foreach ($this->attributes as $val){
516 /* Skip the ones in skip list */
517 if (in_array($val, $skip)){
518 continue;
519 }
521 if (isset($this->attrs["$val"][0])){
523 /* If attribute is set, replace dynamic parts:
524 %sn, %givenName and %uid. Fill these in our local variables. */
525 $value= $this->attrs["$val"][0];
527 foreach (array("sn", "givenName", "uid") as $repl){
528 if (preg_match("/%$repl/i", $value)){
529 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
530 }
531 }
532 $this->$val= $value;
533 }
534 }
536 /* Is Account? */
537 $found= TRUE;
538 foreach ($this->objectclasses as $obj){
539 if (preg_match('/top/i', $obj)){
540 continue;
541 }
542 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
543 $found= FALSE;
544 break;
545 }
546 }
547 if ($found){
548 $this->is_account= TRUE;
549 }
550 }
552 /* Indicate whether a password change is needed or not */
553 function password_change_needed()
554 {
555 return FALSE;
556 }
559 /* Show header message for tab dialogs */
560 function show_enable_header($button_text, $text, $disabled= FALSE)
561 {
562 if (($disabled == TRUE) || (!$this->acl_is_createable())){
563 $state= "disabled";
564 } else {
565 $state= "";
566 }
567 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
568 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
569 "><p class=\"seperator\"> </p></td></tr></table>";
571 return($display);
572 }
575 /* Show header message for tab dialogs */
576 function show_disable_header($button_text, $text, $disabled= FALSE)
577 {
578 if (($disabled == TRUE) || !$this->acl_is_removeable()){
579 $state= "disabled";
580 } else {
581 $state= "";
582 }
583 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
584 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
585 "><p class=\"seperator\"> </p></td></tr></table>";
587 return($display);
588 }
591 /* Show header message for tab dialogs */
592 function show_header($button_text, $text, $disabled= FALSE)
593 {
594 echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
595 if ($disabled == TRUE){
596 $state= "disabled";
597 } else {
598 $state= "";
599 }
600 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
601 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
602 ($this->acl_is_createable()?'':'disabled')." ".$state.
603 "><p class=\"seperator\"> </p></td></tr></table>";
605 return($display);
606 }
609 function postcreate($add_attrs= array())
610 {
611 /* Find postcreate entries for this class */
612 $command= $this->config->search(get_class($this), "POSTCREATE",array('menu', 'tabs'));
614 if ($command != ""){
616 /* Walk through attribute list */
617 foreach ($this->attributes as $attr){
618 if (!is_array($this->$attr)){
619 $add_attrs[$attr] = $this->$attr;
620 }
621 }
622 $add_attrs['dn']=$this->dn;
624 $tmp = array();
625 foreach($add_attrs as $name => $value){
626 $tmp[$name] = strlen($name);
627 }
628 arsort($tmp);
630 /* Additional attributes */
631 foreach ($tmp as $name => $len){
632 $value = $add_attrs[$name];
633 $command= preg_replace("/%$name/", "$value", $command);
634 }
636 if (check_command($command)){
637 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
638 $command, "Execute");
640 exec($command);
641 } else {
642 $message= msgPool::cmdnotfound("POSTCREATE", get_class($this));
643 msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
644 }
645 }
646 }
648 function postmodify($add_attrs= array())
649 {
650 /* Find postcreate entries for this class */
651 $command= $this->config->search(get_class($this), "POSTMODIFY",array('menu','tabs'));
653 if ($command != ""){
655 /* Walk through attribute list */
656 foreach ($this->attributes as $attr){
657 if (!is_array($this->$attr)){
658 $add_attrs[$attr] = $this->$attr;
659 }
660 }
661 $add_attrs['dn']=$this->dn;
663 $tmp = array();
664 foreach($add_attrs as $name => $value){
665 $tmp[$name] = strlen($name);
666 }
667 arsort($tmp);
669 /* Additional attributes */
670 foreach ($tmp as $name => $len){
671 $value = $add_attrs[$name];
672 $command= preg_replace("/%$name/", "$value", $command);
673 }
675 if (check_command($command)){
676 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,$command, "Execute");
677 exec($command);
678 } else {
679 $message= msgPool::cmdnotfound("POSTMODIFY", get_class($this));
680 msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
681 }
682 }
683 }
685 function postremove($add_attrs= array())
686 {
687 /* Find postremove entries for this class */
688 $command= $this->config->search(get_class($this), "POSTREMOVE",array('menu','tabs'));
689 if ($command != ""){
691 /* Walk through attribute list */
692 foreach ($this->attributes as $attr){
693 if (!is_array($this->$attr)){
694 $add_attrs[$attr] = $this->$attr;
695 }
696 }
697 $add_attrs['dn']=$this->dn;
699 $tmp = array();
700 foreach($add_attrs as $name => $value){
701 $tmp[$name] = strlen($name);
702 }
703 arsort($tmp);
705 /* Additional attributes */
706 foreach ($tmp as $name => $len){
707 $value = $add_attrs[$name];
708 $command= preg_replace("/%$name/", "$value", $command);
709 }
711 if (check_command($command)){
712 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
713 $command, "Execute");
715 exec($command);
716 } else {
717 $message= msgPool::cmdnotfound("POSTREMOVE", get_class($this));
718 msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
719 }
720 }
721 }
723 /* Create unique DN */
724 function create_unique_dn($attribute, $base)
725 {
726 $ldap= $this->config->get_ldap_link();
727 $base= preg_replace("/^,*/", "", $base);
729 /* Try to use plain entry first */
730 $dn= "$attribute=".$this->$attribute.",$base";
731 $ldap->cat ($dn, array('dn'));
732 if (!$ldap->fetch()){
733 return ($dn);
734 }
736 /* Look for additional attributes */
737 foreach ($this->attributes as $attr){
738 if ($attr == $attribute || $this->$attr == ""){
739 continue;
740 }
742 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
743 $ldap->cat ($dn, array('dn'));
744 if (!$ldap->fetch()){
745 return ($dn);
746 }
747 }
749 /* None found */
750 return ("none");
751 }
753 function rebind($ldap, $referral)
754 {
755 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
756 if (ldap_bind($ldap, $credentials['ADMIN'], $this->config->get_credentials($credentials['PASSWORD']))) {
757 $this->error = "Success";
758 $this->hascon=true;
759 $this->reconnect= true;
760 return (0);
761 } else {
762 $this->error = "Could not bind to " . $credentials['ADMIN'];
763 return NULL;
764 }
765 }
768 /* Recursively copy ldap object */
769 function _copy($src_dn,$dst_dn)
770 {
771 $ldap=$this->config->get_ldap_link();
772 $ldap->cat($src_dn);
773 $attrs= $ldap->fetch();
775 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
776 $ds= ldap_connect($this->config->current['SERVER']);
777 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
778 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
779 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
780 }
782 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
783 $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
785 /* Fill data from LDAP */
786 $new= array();
787 if ($sr) {
788 $ei=ldap_first_entry($ds, $sr);
789 if ($ei) {
790 foreach($attrs as $attr => $val){
791 if ($info = @ldap_get_values_len($ds, $ei, $attr)){
792 for ($i= 0; $i<$info['count']; $i++){
793 if ($info['count'] == 1){
794 $new[$attr]= $info[$i];
795 } else {
796 $new[$attr][]= $info[$i];
797 }
798 }
799 }
800 }
801 }
802 }
804 /* close conncetion */
805 ldap_unbind($ds);
807 /* Adapt naming attribute */
808 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
809 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
810 $new[$dst_name]= @LDAP::fix($dst_val);
812 /* Check if this is a department.
813 * If it is a dep. && there is a , override in his ou
814 * change \2C to , again, else this entry can't be saved ...
815 */
816 if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
817 $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
818 }
820 /* Save copy */
821 $ldap->connect();
822 $ldap->cd($this->config->current['BASE']);
824 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
826 /* FAIvariable=.../..., cn=..
827 could not be saved, because the attribute FAIvariable was different to
828 the dn FAIvariable=..., cn=... */
829 if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
830 $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
831 }
832 $ldap->cd($dst_dn);
833 $ldap->add($new);
835 if (!$ldap->success()){
836 trigger_error("Trying to save $dst_dn failed.",
837 E_USER_WARNING);
838 return(FALSE);
839 }
840 return(TRUE);
841 }
844 /* This is a workaround function. */
845 function copy($src_dn, $dst_dn)
846 {
847 /* Rename dn in possible object groups */
848 $ldap= $this->config->get_ldap_link();
849 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::prepare4filter($src_dn).'))',
850 array('cn'));
851 while ($attrs= $ldap->fetch()){
852 $og= new ogroup($this->config, $ldap->getDN());
853 unset($og->member[$src_dn]);
854 $og->member[$dst_dn]= $dst_dn;
855 $og->save ();
856 }
858 $ldap->cat($dst_dn);
859 $attrs= $ldap->fetch();
860 if (count($attrs)){
861 trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
862 E_USER_WARNING);
863 return (FALSE);
864 }
866 $ldap->cat($src_dn);
867 $attrs= $ldap->fetch();
868 if (!count($attrs)){
869 trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
870 E_USER_WARNING);
871 return (FALSE);
872 }
874 $ldap->cd($src_dn);
875 $ldap->search("objectClass=*",array("dn"));
876 while($attrs = $ldap->fetch()){
877 $src = $attrs['dn'];
878 $dst = preg_replace("/".normalizePreg($src_dn)."$/",$dst_dn,$attrs['dn']);
879 $this->_copy($src,$dst);
880 }
881 return (TRUE);
882 }
886 /*! \brief Move a given ldap object indentified by $src_dn \
887 to the given destination $dst_dn \
888 * Ensure that all references are updated (ogroups) \
889 * Update ACLs \
890 * Update accessTo \
891 @param String The source dn.
892 @param String The destination dn.
893 @return Boolean TRUE on success else FALSE.
894 */
895 function rename($src_dn, $dst_dn)
896 {
897 $start = microtime(1);
899 /* Try to move the source entry to the destination position */
900 $ldap = $this->config->get_ldap_link();
901 if (!$ldap->rename_dn($src_dn,$dst_dn)){
902 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $src_dn, "", get_class()));
903 return(FALSE);
904 }
906 /* Get list of groups within this tree,
907 maybe we have to update ACL references.
908 */
909 $leaf_groups = get_list("(objectClass=posixGroup)",array("all"),$dst_dn,
910 array("dn","objectClass"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
912 /* Get list of users within this tree,
913 maybe we have to update ACL references.
914 */
915 $leaf_users= get_list("(objectClass=gosaAccount)",array("all"),$dst_dn,
916 array("dn","objectClass"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
919 /* Updated acls set for this groups */
920 foreach($leaf_groups as $group){
921 $new_dn = $group['dn'];
922 $old_dn = preg_replace("/".normalizePreg($dst_dn)."$/i",$src_dn,$new_dn);
923 $this->update_acls($old_dn,$new_dn);
924 }
926 /* Updated acls set for this users */
927 foreach($leaf_users as $user){
928 $new_dn = $user['dn'];
929 $old_dn = preg_replace("/".normalizePreg($dst_dn)."$/i",$src_dn,$new_dn);
930 $this->update_acls($old_dn,$new_dn);
931 }
933 /* Get all objectGroups defined in this database.
934 and check if there is an entry matching the source dn,
935 if this is the case, then update this objectgroup to use the new dn.
936 */
937 $ogroups = get_sub_list("(&(objectClass=gosaGroupOfNames)(member=*))","ogroups",
938 array(get_ou("ogroupou")),$this->config->current['BASE'],array("member"),
939 GL_SUBSEARCH | GL_NO_ACL_CHECK) ;
941 /* Walk through all objectGroups and check if there are
942 members matching the source dn
943 */
944 foreach($ogroups as $ogroup){
945 if(isset($ogroup['member'])){
947 /* Reset class object, this will be initialized with class_ogroup on demand
948 */
949 $o_ogroup = NULL;
950 for($i = 0 ; $i < $ogroup['member']['count'] ; $i ++){
952 $c_mem = $ogroup['member'][$i];
954 if(preg_match("/".normalizePreg($src_dn)."$/i",$c_mem)){
956 $d_mem = preg_replace("/".normalizePreg($src_dn)."$/i",$dst_dn,$ogroup['member'][$i]);
958 if($o_ogroup == NULL){
959 $o_ogroup = new ogroup($this->config,$ogroup['dn']);
960 }
962 unset($o_ogroup->member[$c_mem]);
963 $o_ogroup->member[$d_mem]= $d_mem;
964 }
965 }
967 /* Save object group if there were changes made on the membership */
968 if($o_ogroup != NULL){
969 $o_ogroup->save();
970 }
971 }
972 }
974 /* Check if there are gosa departments moved.
975 If there were deps moved, the force reload of config->deps.
976 */
977 $leaf_deps= get_list("(objectClass=gosaDepartment)",array("all"),$dst_dn,
978 array("dn","objectClass"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
980 if(count($leaf_deps)){
981 $this->config->get_departments();
982 $this->config->make_idepartments();
983 session::set("config",$this->config);
984 $ui =get_userinfo();
985 $ui->reset_acl_cache();
986 }
988 return(TRUE);
989 }
993 function move($src_dn, $dst_dn)
994 {
995 /* Do not copy if only upper- lowercase has changed */
996 if(strtolower($src_dn) == strtolower($dst_dn)){
997 return(TRUE);
998 }
1001 /* Try to move the entry instead of copy & delete
1002 */
1003 if(TRUE){
1005 /* Try to move with ldap routines, if this was not successfull
1006 fall back to the old style copy & remove method
1007 */
1008 if($this->rename($src_dn, $dst_dn)){
1009 return(TRUE);
1010 }else{
1011 // See code below.
1012 }
1013 }
1015 /* Copy source to destination */
1016 if (!$this->copy($src_dn, $dst_dn)){
1017 return (FALSE);
1018 }
1020 /* Delete source */
1021 $ldap= $this->config->get_ldap_link();
1022 $ldap->rmdir_recursive($src_dn);
1023 if (!$ldap->success()){
1024 trigger_error("Trying to delete $src_dn failed.",
1025 E_USER_WARNING);
1026 return (FALSE);
1027 }
1029 return (TRUE);
1030 }
1033 /* Move/Rename complete trees */
1034 function recursive_move($src_dn, $dst_dn)
1035 {
1036 /* Check if the destination entry exists */
1037 $ldap= $this->config->get_ldap_link();
1039 /* Check if destination exists - abort */
1040 $ldap->cat($dst_dn, array('dn'));
1041 if ($ldap->fetch()){
1042 trigger_error("recursive_move $dst_dn already exists.",
1043 E_USER_WARNING);
1044 return (FALSE);
1045 }
1047 $this->copy($src_dn, $dst_dn);
1049 /* Remove src_dn */
1050 $ldap->cd($src_dn);
1051 $ldap->recursive_remove($src_dn);
1052 return (TRUE);
1053 }
1056 function handle_post_events($mode, $add_attrs= array())
1057 {
1058 switch ($mode){
1059 case "add":
1060 $this->postcreate($add_attrs);
1061 break;
1063 case "modify":
1064 $this->postmodify($add_attrs);
1065 break;
1067 case "remove":
1068 $this->postremove($add_attrs);
1069 break;
1070 }
1071 }
1074 function saveCopyDialog(){
1075 }
1078 function getCopyDialog(){
1079 return(array("string"=>"","status"=>""));
1080 }
1083 function PrepareForCopyPaste($source)
1084 {
1085 $todo = $this->attributes;
1086 if(isset($this->CopyPasteVars)){
1087 $todo = array_merge($todo,$this->CopyPasteVars);
1088 }
1090 if(count($this->objectclasses)){
1091 $this->is_account = TRUE;
1092 foreach($this->objectclasses as $class){
1093 if(!in_array($class,$source['objectClass'])){
1094 $this->is_account = FALSE;
1095 }
1096 }
1097 }
1099 foreach($todo as $var){
1100 if (isset($source[$var])){
1101 if(isset($source[$var]['count'])){
1102 if($source[$var]['count'] > 1){
1103 $this->$var = array();
1104 $tmp = array();
1105 for($i = 0 ; $i < $source[$var]['count']; $i++){
1106 $tmp = $source[$var][$i];
1107 }
1108 $this->$var = $tmp;
1109 }else{
1110 $this->$var = $source[$var][0];
1111 }
1112 }else{
1113 $this->$var= $source[$var];
1114 }
1115 }
1116 }
1117 }
1119 function tag_attrs(&$at, $dn= "", $tag= "", $show= false)
1120 {
1121 /* Skip tagging?
1122 If this is called from departmentGeneric, we have to skip this
1123 tagging procedure.
1124 */
1125 if($this->skipTagging){
1126 return;
1127 }
1129 /* No dn? Self-operation... */
1130 if ($dn == ""){
1131 $dn= $this->dn;
1133 /* No tag? Find it yourself... */
1134 if ($tag == ""){
1135 $len= strlen($dn);
1137 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
1138 $relevant= array();
1139 foreach ($this->config->adepartments as $key => $ntag){
1141 /* This one is bigger than our dn, its not relevant... */
1142 if ($len < strlen($key)){
1143 continue;
1144 }
1146 /* This one matches with the latter part. Break and don't fix this entry */
1147 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
1148 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
1149 $relevant[strlen($key)]= $ntag;
1150 continue;
1151 }
1153 }
1155 /* If we've some relevant tags to set, just get the longest one */
1156 if (count($relevant)){
1157 ksort($relevant);
1158 $tmp= array_keys($relevant);
1159 $idx= end($tmp);
1160 $tag= $relevant[$idx];
1161 $this->gosaUnitTag= $tag;
1162 }
1163 }
1164 }
1166 /* Remove tags that may already be here... */
1167 remove_objectClass("gosaAdministrativeUnitTag", $at);
1168 if (isset($at['gosaUnitTag'])){
1169 unset($at['gosaUnitTag']);
1170 }
1172 /* Set tag? */
1173 if ($tag != ""){
1174 add_objectClass("gosaAdministrativeUnitTag", $at);
1175 $at['gosaUnitTag']= $tag;
1176 }
1178 /* Initially this object was tagged.
1179 - But now, it is no longer inside a tagged department.
1180 So force the remove of the tag.
1181 (objectClass was already removed obove)
1182 */
1183 if($tag == "" && $this->gosaUnitTag){
1184 $at['gosaUnitTag'] = array();
1185 }
1186 }
1189 /* Add possibility to stop remove process */
1190 function allow_remove()
1191 {
1192 $reason= "";
1193 return $reason;
1194 }
1197 /* Create a snapshot of the current object */
1198 function create_snapshot($type= "snapshot", $description= array())
1199 {
1201 /* Check if snapshot functionality is enabled */
1202 if(!$this->snapshotEnabled()){
1203 return;
1204 }
1206 /* Get configuration from gosa.conf */
1207 $tmp = $this->config->current;
1209 /* Create lokal ldap connection */
1210 $ldap= $this->config->get_ldap_link();
1211 $ldap->cd($this->config->current['BASE']);
1213 /* check if there are special server configurations for snapshots */
1214 if(!isset($tmp['SNAPSHOT_SERVER'])){
1216 /* Source and destination server are both the same, just copy source to dest obj */
1217 $ldap_to = $ldap;
1218 $snapldapbase = $this->config->current['BASE'];
1220 }else{
1221 $server = $tmp['SNAPSHOT_SERVER'];
1222 $user = $tmp['SNAPSHOT_USER'];
1223 $password = $tmp['SNAPSHOT_PASSWORD'];
1224 $snapldapbase = $tmp['SNAPSHOT_BASE'];
1226 $ldap_to = new ldapMultiplexer(new LDAP($user,$password, $server));
1227 $ldap_to -> cd($snapldapbase);
1229 if (!$ldap_to->success()){
1230 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1231 }
1233 }
1235 /* check if the dn exists */
1236 if ($ldap->dn_exists($this->dn)){
1238 /* Extract seconds & mysecs, they are used as entry index */
1239 list($usec, $sec)= explode(" ", microtime());
1241 /* Collect some infos */
1242 $base = $this->config->current['BASE'];
1243 $snap_base = $tmp['SNAPSHOT_BASE'];
1244 $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1245 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1247 /* Create object */
1248 #$data = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1249 $data = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1250 $newName = preg_replace("/\./", "", $sec."-".$usec);
1251 $target= array();
1252 $target['objectClass'] = array("top", "gosaSnapshotObject");
1253 $target['gosaSnapshotData'] = gzcompress($data, 6);
1254 $target['gosaSnapshotType'] = $type;
1255 $target['gosaSnapshotDN'] = $this->dn;
1256 $target['description'] = $description;
1257 $target['gosaSnapshotTimestamp'] = $newName;
1259 /* Insert the new snapshot
1260 But we have to check first, if the given gosaSnapshotTimestamp
1261 is already used, in this case we should increment this value till there is
1262 an unused value. */
1263 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1264 $ldap_to->cat($new_dn);
1265 while($ldap_to->count()){
1266 $ldap_to->cat($new_dn);
1267 $newName = preg_replace("/\./", "", $sec."-".($usec++));
1268 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1269 $target['gosaSnapshotTimestamp'] = $newName;
1270 }
1272 /* Inset this new snapshot */
1273 $ldap_to->cd($snapldapbase);
1274 $ldap_to->create_missing_trees($snapldapbase);
1275 $ldap_to->create_missing_trees($new_base);
1276 $ldap_to->cd($new_dn);
1277 $ldap_to->add($target);
1278 if (!$ldap_to->success()){
1279 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $new_dn, LDAP_ADD, get_class()));
1280 }
1282 if (!$ldap->success()){
1283 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $new_base, "", get_class()));
1284 }
1286 }
1287 }
1289 function remove_snapshot($dn)
1290 {
1291 $ui = get_userinfo();
1292 $old_dn = $this->dn;
1293 $this->dn = $dn;
1294 $ldap = $this->config->get_ldap_link();
1295 $ldap->cd($this->config->current['BASE']);
1296 $ldap->rmdir_recursive($dn);
1297 $this->dn = $old_dn;
1298 }
1301 /* returns true if snapshots are enabled, and false if it is disalbed
1302 There will also be some errors psoted, if the configuration failed */
1303 function snapshotEnabled()
1304 {
1305 $tmp = $this->config->current;
1306 if(isset($tmp['ENABLE_SNAPSHOT'])){
1307 if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1309 /* Check if the snapshot_base is defined */
1310 if(!isset($tmp['SNAPSHOT_BASE'])){
1311 msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),"SNAPSHOT_BASE"), ERROR_DIALOG);
1312 return(FALSE);
1313 }
1315 /* check if there are special server configurations for snapshots */
1316 if(isset($tmp['SNAPSHOT_SERVER'])){
1318 /* check if all required vars are available to create a new ldap connection */
1319 $missing = "";
1320 foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_BASE") as $var){
1321 if(!isset($tmp[$var])){
1322 $missing .= $var." ";
1323 msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."), $missing), ERROR_DIALOG);
1324 return(FALSE);
1325 }
1326 }
1327 }
1328 return(TRUE);
1329 }
1330 }
1331 return(FALSE);
1332 }
1335 /* Return available snapshots for the given base
1336 */
1337 function Available_SnapsShots($dn,$raw = false)
1338 {
1339 if(!$this->snapshotEnabled()) return(array());
1341 /* Create an additional ldap object which
1342 points to our ldap snapshot server */
1343 $ldap= $this->config->get_ldap_link();
1344 $ldap->cd($this->config->current['BASE']);
1345 $cfg= &$this->config->current;
1347 /* check if there are special server configurations for snapshots */
1349 if(isset($cfg['SERVER']) && isset($cfg['SNAPSHOT_SERVER']) && $cfg['SERVER'] == $cfg['SNAPSHOT_SERVER']){
1350 $ldap_to = $ldap;
1351 }elseif(isset($cfg['SNAPSHOT_SERVER'])){
1352 $server = $cfg['SNAPSHOT_SERVER'];
1353 $user = $cfg['SNAPSHOT_USER'];
1354 $password = $cfg['SNAPSHOT_PASSWORD'];
1355 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1357 $ldap_to = new ldapMultiplexer(new LDAP($user,$password, $server));
1358 $ldap_to -> cd ($snapldapbase);
1359 if (!$ldap_to->success()){
1360 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1361 }
1362 }else{
1363 $ldap_to = $ldap;
1364 }
1366 /* Prepare bases and some other infos */
1367 $base = $this->config->current['BASE'];
1368 $snap_base = $cfg['SNAPSHOT_BASE'];
1369 $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1370 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1371 $tmp = array();
1373 /* Fetch all objects with gosaSnapshotDN=$dn */
1374 $ldap_to->cd($new_base);
1375 $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1376 array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1378 /* Put results into a list and add description if missing */
1379 while($entry = $ldap_to->fetch()){
1380 if(!isset($entry['description'][0])){
1381 $entry['description'][0] = "";
1382 }
1383 $tmp[] = $entry;
1384 }
1386 /* Return the raw array, or format the result */
1387 if($raw){
1388 return($tmp);
1389 }else{
1390 $tmp2 = array();
1391 foreach($tmp as $entry){
1392 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1393 }
1394 }
1395 return($tmp2);
1396 }
1399 function getAllDeletedSnapshots($base_of_object,$raw = false)
1400 {
1401 if(!$this->snapshotEnabled()) return(array());
1403 /* Create an additional ldap object which
1404 points to our ldap snapshot server */
1405 $ldap= $this->config->get_ldap_link();
1406 $ldap->cd($this->config->current['BASE']);
1407 $cfg= &$this->config->current;
1409 /* check if there are special server configurations for snapshots */
1410 if(isset($cfg['SNAPSHOT_SERVER'])){
1411 $server = $cfg['SNAPSHOT_SERVER'];
1412 $user = $cfg['SNAPSHOT_USER'];
1413 $password = $cfg['SNAPSHOT_PASSWORD'];
1414 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1415 $ldap_to = new ldapMultiplexer(new LDAP($user,$password, $server));
1416 $ldap_to->cd ($snapldapbase);
1417 if (!$ldap_to->success()){
1418 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1419 }
1420 }else{
1421 $ldap_to = $ldap;
1422 }
1424 /* Prepare bases */
1425 $base = $this->config->current['BASE'];
1426 $snap_base = $cfg['SNAPSHOT_BASE'];
1427 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1429 /* Fetch all objects and check if they do not exist anymore */
1430 $ui = get_userinfo();
1431 $tmp = array();
1432 $ldap_to->cd($new_base);
1433 $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1434 while($entry = $ldap_to->fetch()){
1436 $chk = str_replace($new_base,"",$entry['dn']);
1437 if(preg_match("/,ou=/",$chk)) continue;
1439 if(!isset($entry['description'][0])){
1440 $entry['description'][0] = "";
1441 }
1442 $tmp[] = $entry;
1443 }
1445 /* Check if entry still exists */
1446 foreach($tmp as $key => $entry){
1447 $ldap->cat($entry['gosaSnapshotDN'][0]);
1448 if($ldap->count()){
1449 unset($tmp[$key]);
1450 }
1451 }
1453 /* Format result as requested */
1454 if($raw) {
1455 return($tmp);
1456 }else{
1457 $tmp2 = array();
1458 foreach($tmp as $key => $entry){
1459 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1460 }
1461 }
1462 return($tmp2);
1463 }
1466 /* Restore selected snapshot */
1467 function restore_snapshot($dn)
1468 {
1469 if(!$this->snapshotEnabled()) return(array());
1471 $ldap= $this->config->get_ldap_link();
1472 $ldap->cd($this->config->current['BASE']);
1473 $cfg= &$this->config->current;
1475 /* check if there are special server configurations for snapshots */
1476 if(isset($cfg['SNAPSHOT_SERVER'])){
1477 $server = $cfg['SNAPSHOT_SERVER'];
1478 $user = $cfg['SNAPSHOT_USER'];
1479 $password = $cfg['SNAPSHOT_PASSWORD'];
1480 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1481 $ldap_to = new ldapMultiplexer(new LDAP($user,$password, $server));
1482 $ldap_to->cd ($snapldapbase);
1483 if (!$ldap_to->success()){
1484 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1485 }
1486 }else{
1487 $ldap_to = $ldap;
1488 }
1490 /* Get the snapshot */
1491 $ldap_to->cat($dn);
1492 $restoreObject = $ldap_to->fetch();
1494 /* Prepare import string */
1495 $data = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1497 /* Import the given data */
1498 $err = "";
1499 $ldap->import_complete_ldif($data,$err,false,false);
1500 if (!$ldap->success()){
1501 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, "", get_class()));
1502 }
1503 }
1506 function showSnapshotDialog($base,$baseSuffixe,&$parent)
1507 {
1508 $once = true;
1509 $ui = get_userinfo();
1510 $this->parent = $parent;
1512 if(!isset($this->acl_module)){
1513 return("VERDAMMT");
1514 }
1516 foreach($_POST as $name => $value){
1518 /* Create a new snapshot, display a dialog */
1519 if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1520 $once = false;
1521 $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1522 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1524 if(!empty($entry) && $ui->allow_snapshot_create($entry,$this->parent->acl_module)){
1525 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1526 }else{
1527 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),ERROR_DIALOG);
1528 }
1529 }
1531 /* Restore a snapshot, display a dialog with all snapshots of the current object */
1532 if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1533 $once = false;
1534 $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1535 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1536 if(!empty($entry) && $ui->allow_snapshot_restore($entry,$this->parent->acl_module)){
1537 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1538 $this->snapDialog->display_restore_dialog = true;
1539 }else{
1540 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),ERROR_DIALOG);
1541 }
1542 }
1544 /* Restore one of the already deleted objects */
1545 if(((isset($_POST['menu_action']) && $_POST['menu_action'] == "RestoreDeletedSnapShot")
1546 || preg_match("/^RestoreDeletedSnapShot_/",$name)) && $once){
1547 $once = false;
1549 if($ui->allow_snapshot_restore($base,$this->parent->acl_module)){
1550 $this->snapDialog = new SnapShotDialog($this->config,"",$this);
1551 $this->snapDialog->set_snapshot_bases($baseSuffixe);
1552 $this->snapDialog->display_restore_dialog = true;
1553 $this->snapDialog->display_all_removed_objects = true;
1554 }else{
1555 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$base),ERROR_DIALOG);
1556 }
1557 }
1559 /* Restore selected snapshot */
1560 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1561 $once = false;
1562 $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1563 $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1564 if(!empty($entry) && $ui->allow_snapshot_restore($entry,$this->parent->acl_module)){
1565 $this->restore_snapshot($entry);
1566 $this->snapDialog = NULL;
1567 }else{
1568 msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),ERROR_DIALOG);
1569 }
1570 }
1571 }
1573 /* Create a new snapshot requested, check
1574 the given attributes and create the snapshot*/
1575 if(isset($_POST['CreateSnapshot']) && is_object($this->snapDialog)){
1576 $this->snapDialog->save_object();
1577 $msgs = $this->snapDialog->check();
1578 if(count($msgs)){
1579 foreach($msgs as $msg){
1580 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
1581 }
1582 }else{
1583 $this->dn = $this->snapDialog->dn;
1584 $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1585 $this->snapDialog = NULL;
1586 }
1587 }
1589 /* Restore is requested, restore the object with the posted dn .*/
1590 if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1591 }
1593 if(isset($_POST['CancelSnapshot'])){
1594 $this->snapDialog = NULL;
1595 }
1597 if(is_object($this->snapDialog )){
1598 $this->snapDialog->save_object();
1599 return($this->snapDialog->execute());
1600 }
1601 }
1604 static function plInfo()
1605 {
1606 return array();
1607 }
1610 function set_acl_base($base)
1611 {
1612 $this->acl_base= $base;
1613 }
1616 function set_acl_category($category)
1617 {
1618 $this->acl_category= "$category/";
1619 }
1622 function acl_is_writeable($attribute,$skip_write = FALSE)
1623 {
1624 $ui= get_userinfo();
1625 return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1626 }
1629 function acl_is_readable($attribute)
1630 {
1631 $ui= get_userinfo();
1632 return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1633 }
1636 function acl_is_createable($base ="")
1637 {
1638 $ui= get_userinfo();
1639 if($base == "") $base = $this->acl_base;
1640 return preg_match('/c/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1641 }
1644 function acl_is_removeable($base ="")
1645 {
1646 $ui= get_userinfo();
1647 if($base == "") $base = $this->acl_base;
1648 return preg_match('/d/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1649 }
1652 function acl_is_moveable($base = "")
1653 {
1654 $ui= get_userinfo();
1655 if($base == "") $base = $this->acl_base;
1656 return preg_match('/m/', $ui->get_permissions($base, $this->acl_category.get_class($this), '0'));
1657 }
1660 function acl_have_any_permissions()
1661 {
1662 }
1665 function getacl($attribute,$skip_write= FALSE)
1666 {
1667 $ui= get_userinfo();
1668 return $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1669 }
1672 /*! \brief Returns a list of all available departments for this object.
1673 If this object is new, all departments we are allowed to create a new user in are returned.
1674 If this is an existing object, return all deps. we are allowed to move tis object too.
1676 @return Array [dn] => "..name" // All deps. we are allowed to act on.
1677 */
1678 function get_allowed_bases()
1679 {
1680 $ui = get_userinfo();
1681 $deps = array();
1683 /* Is this a new object ? Or just an edited existing object */
1684 if(!$this->initially_was_account && $this->is_account){
1685 $new = true;
1686 }else{
1687 $new = false;
1688 }
1690 foreach($this->config->idepartments as $dn => $name){
1691 if($new && $this->acl_is_createable($dn)){
1692 $deps[$dn] = $name;
1693 }elseif(!$new && $this->acl_is_moveable($dn)){
1694 $deps[$dn] = $name;
1695 }
1696 }
1698 /* Add current base */
1699 if(isset($this->base) && isset($this->config->idepartments[$this->base])){
1700 $deps[$this->base] = $this->config->idepartments[$this->base];
1701 }else{
1702 trigger_error("Cannot return list of departments, no default base found in class ".get_class($this).". ".$this->base);
1703 }
1704 return($deps);
1705 }
1708 /* This function modifies object acls too, if an object is moved.
1709 * $old_dn specifies the actually used dn
1710 * $new_dn specifies the destiantion dn
1711 */
1712 function update_acls($old_dn,$new_dn,$output_changes = FALSE)
1713 {
1714 /* Check if old_dn is empty. This should never happen */
1715 if(empty($old_dn) || empty($new_dn)){
1716 trigger_error("Failed to check acl dependencies, wrong dn given.");
1717 return;
1718 }
1720 /* Update userinfo if necessary */
1721 $ui = session::get('ui');
1722 if($ui->dn == $old_dn){
1723 $ui->dn = $new_dn;
1724 session::set('ui',$ui);
1725 new log("view","acl/".get_class($this),$this->dn,array(),"Updated current user dn from '".$old_dn."' to '".$new_dn."'");
1726 }
1728 /* Object was moved, ensure that all acls will be moved too */
1729 if($new_dn != $old_dn && $old_dn != "new"){
1731 /* get_ldap configuration */
1732 $update = array();
1733 $ldap = $this->config->get_ldap_link();
1734 $ldap->cd ($this->config->current['BASE']);
1735 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*))",array("cn","gosaAclEntry"));
1736 while($attrs = $ldap->fetch()){
1738 $acls = array();
1740 /* Reset vars */
1741 $found = false;
1743 /* Walk through acls */
1744 for($i = 0 ; $i < $attrs['gosaAclEntry']['count'] ; $i ++ ){
1746 /* Get Acl parts */
1747 $acl_parts = split(":",$attrs['gosaAclEntry'][$i]);
1749 /* Get every single member for this acl */
1750 $members = array();
1751 if(preg_match("/,/",$acl_parts[2])){
1752 $members = split(",",$acl_parts[2]);
1753 }else{
1754 $members = array($acl_parts[2]);
1755 }
1757 /* Check if member match current dn */
1758 foreach($members as $key => $member){
1759 $member = base64_decode($member);
1760 if($member == $old_dn){
1761 $found = true;
1762 $members[$key] = base64_encode($new_dn);
1763 }
1764 }
1766 /* Create new member string */
1767 $new_members = "";
1768 foreach($members as $member){
1769 $new_members .= $member.",";
1770 }
1771 $new_members = preg_replace("/,$/","",$new_members);
1772 $acl_parts[2] = $new_members;
1774 /* Reconstruckt acl entry */
1775 $acl_str ="";
1776 foreach($acl_parts as $t){
1777 $acl_str .= $t.":";
1778 }
1779 $acl_str = preg_replace("/:$/","",$acl_str);
1780 $acls[] = $acl_str;
1781 }
1783 /* Acls for this object must be adjusted */
1784 if($found){
1786 $debug_info= _("Changing ACL dn")." : <br> -"._("from")." <b> ".
1787 $old_dn."</b><br> -"._("to")." <b>".$new_dn."</b><br>";
1788 @DEBUG (DEBUG_ACL, __LINE__, __FUNCTION__, __FILE__,$debug_info,"ACL");
1790 $update[$attrs['dn']] =array();
1791 foreach($acls as $acl){
1792 $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1793 }
1794 }
1795 }
1797 /* Write updated acls */
1798 foreach($update as $dn => $attrs){
1799 $ldap->cd($dn);
1800 $ldap->modify($attrs);
1801 }
1802 }
1803 }
1807 /* This function enables the entry Serial ID check.
1808 * If an entry was edited while we have edited the entry too,
1809 * an error message will be shown.
1810 * To configure this check correctly read the FAQ.
1811 */
1812 function enable_CSN_check()
1813 {
1814 $this->CSN_check_active =TRUE;
1815 $this->entryCSN = getEntryCSN($this->dn);
1816 }
1819 /*! \brief Prepares the plugin to be used for multiple edit
1820 * Update plugin attributes with given array of attribtues.
1821 * @param array Array with attributes that must be updated.
1822 */
1823 function init_multiple_support($attrs,$all)
1824 {
1825 $ldap= $this->config->get_ldap_link();
1826 $this->multi_attrs = $attrs;
1827 $this->multi_attrs_all= $all;
1829 /* Copy needed attributes */
1830 foreach ($this->attributes as $val){
1831 $found= array_key_ics($val, $this->multi_attrs);
1832 if ($found != ""){
1833 if(isset($this->multi_attrs["$found"][0])){
1834 $this->$val= $this->multi_attrs["$found"][0];
1835 }
1836 }
1837 }
1838 }
1841 /*! \brief Enables multiple support for this plugin
1842 */
1843 function enable_multiple_support()
1844 {
1845 $this->ignore_account = TRUE;
1846 $this->multiple_support_active = TRUE;
1847 }
1850 /*! \brief Returns all values that have been modfied in multiple edit mode.
1851 @return array Cotaining all mdofied values.
1852 */
1853 function get_multi_edit_values()
1854 {
1855 $ret = array();
1856 foreach($this->attributes as $attr){
1857 if(in_array($attr,$this->multi_boxes)){
1858 $ret[$attr] = $this->$attr;
1859 }
1860 }
1861 return($ret);
1862 }
1865 /*! \brief Update class variables with values collected by multiple edit.
1866 */
1867 function set_multi_edit_values($attrs)
1868 {
1869 foreach($attrs as $name => $value){
1870 $this->$name = $value;
1871 }
1872 }
1875 /*! \brief execute plugin
1877 Generates the html output for this node
1878 */
1879 function multiple_execute()
1880 {
1881 /* This one is empty currently. Fabian - please fill in the docu code */
1882 session::set('current_class_for_help',get_class($this));
1884 /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
1885 session::set('LOCK_VARS_TO_USE',array());
1886 session::set('LOCK_VARS_USED',array());
1888 return("Multiple edit is currently not implemented for this plugin.");
1889 }
1892 /*! \brief Save HTML posted data to object for multiple edit
1893 */
1894 function multiple_save_object()
1895 {
1896 if(empty($this->entryCSN) && $this->CSN_check_active){
1897 $this->entryCSN = getEntryCSN($this->dn);
1898 }
1900 /* Save values to object */
1901 $this->multi_boxes = array();
1902 foreach ($this->attributes as $val){
1904 /* Get selected checkboxes from multiple edit */
1905 if(isset($_POST["use_".$val])){
1906 $this->multi_boxes[] = $val;
1907 }
1909 if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
1911 /* Check for modifications */
1912 if (get_magic_quotes_gpc()) {
1913 $data= stripcslashes($_POST["$val"]);
1914 } else {
1915 $data= $this->$val = $_POST["$val"];
1916 }
1917 if ($this->$val != $data){
1918 $this->is_modified= TRUE;
1919 }
1921 /* IE post fix */
1922 if(isset($data[0]) && $data[0] == chr(194)) {
1923 $data = "";
1924 }
1925 $this->$val= $data;
1926 }
1927 }
1928 }
1931 /*! \brief Returns all attributes of this plugin,
1932 to be able to detect multiple used attributes
1933 in multi_plugg::detect_multiple_used_attributes().
1934 @return array Attributes required for intialization of multi_plug
1935 */
1936 public function get_multi_init_values()
1937 {
1938 $attrs = $this->attrs;
1939 return($attrs);
1940 }
1943 /*! \brief Check given values in multiple edit
1944 @return array Error messages
1945 */
1946 function multiple_check()
1947 {
1948 $message = plugin::check();
1949 return($message);
1950 }
1953 /*! \brief Returns the snapshot header part for "Actions" menu in management dialogs
1954 @param $layer_menu
1955 */
1956 function get_snapshot_header($base,$category)
1957 {
1958 $str = "";
1959 $ui = get_userinfo();
1960 if($this->snapshotEnabled() && $ui->allow_snapshot_restore($base,$category)){
1962 $ok = false;
1963 foreach($this->get_used_snapshot_bases() as $base){
1964 $ok |= count($this->getAllDeletedSnapshots($base)) >= 1 ;
1965 }
1967 if($ok){
1968 $str = "..|<img class='center' src='images/lists/restore.png' ".
1969 "alt='"._("Restore")."'> "._("Restore"). "|RestoreDeletedSnapShot|\n";
1970 }else{
1971 $str = "..|<img class='center' src='images/lists/restore_grey.png' alt=''> "._("Restore")."||\n";
1972 }
1973 }
1974 return($str);
1975 }
1978 function get_snapshot_action($base,$category)
1979 {
1980 $str= "";
1981 $ui = get_userinfo();
1982 if($this->snapshotEnabled()){
1983 if ($ui->allow_snapshot_restore($base,$category)){
1985 if(count($this->Available_SnapsShots($base))){
1986 $str.= "<input class='center' type='image' src='images/lists/restore.png'
1987 alt='"._("Restore snapshot")."' name='RestoreSnapShotDialog_".base64_encode($base)."' title='"._("Restore snapshot")."'> ";
1988 } else {
1989 $str = "<img class='center' src='images/lists/restore_grey.png' alt=''> ";
1990 }
1991 }
1992 if($ui->allow_snapshot_create($base,$category)){
1993 $str.= "<input class='center' type='image' src='images/snapshot.png'
1994 alt='"._("Create snapshot")."' name='CreateSnapShotDialog_".base64_encode($base)."'
1995 title='"._("Create a new snapshot from this object")."'> ";
1996 }else{
1997 $str = "<img class='center' src='images/empty.png' alt=' '> ";
1998 }
1999 }
2001 return($str);
2002 }
2005 function get_copypaste_action($base,$category,$class,$copy = TRUE, $cut = TRUE)
2006 {
2007 $ui = get_userinfo();
2008 $action = "";
2009 if($this->CopyPasteHandler){
2010 if($cut){
2011 if($ui->is_cutable($base,$category,$class)){
2012 $action .= "<input class='center' type='image'
2013 src='images/lists/cut.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'> ";
2014 }else{
2015 $action.="<img src='images/empty.png' alt=' ' class='center'> ";
2016 }
2017 }
2018 if($copy){
2019 if($ui->is_copyable($base,$category,$class)){
2020 $action.= "<input class='center' type='image'
2021 src='images/lists/copy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'> ";
2022 }else{
2023 $action.="<img src='images/empty.png' alt=' ' class='center'> ";
2024 }
2025 }
2026 }
2028 return($action);
2029 }
2032 function get_copypaste_header($base,$category,$copy = TRUE, $cut = TRUE)
2033 {
2034 $s = "";
2035 $ui =get_userinfo();
2037 if(!is_array($category)){
2038 $category = array($category);
2039 }
2041 /* Check permissions for each category, if there is at least one category which
2042 support read or paste permissions for the given base, then display the specific actions.
2043 */
2044 $readable = $pasteable = TRUE;
2045 foreach($category as $cat){
2046 $readable |= $ui->get_category_permissions($base,$cat);
2047 $pasteable|= $ui->is_pasteable($base,$cat);
2048 }
2050 if(($cut || $copy) && isset($this->CopyPasteHandler) && is_object($this->CopyPasteHandler)){
2051 if($readable){
2052 $s.= "..|---|\n";
2053 if($copy){
2054 $s.= "..|<img src='images/lists/copy.png' alt='' border='0' class='center'>".
2055 " "._("Copy")."|"."multiple_copy_systems|\n";
2056 }
2057 if($cut){
2058 $s.= "..|<img src='images/lists/cut.png' alt='' border='0' class='center'>".
2059 " "._("Cut")."|"."multiple_cut_systems|\n";
2060 }
2061 }
2063 if($pasteable){
2064 if($this->CopyPasteHandler->entries_queued()){
2065 $img = "<img border='0' class='center' src='images/lists/paste.png' alt=''>";
2066 $s.="..|".$img." "._("Paste")."|editPaste|\n";
2067 }else{
2068 $img = "<img border='0' class='center' src='images/lists/paste-grey.png' alt=''>";
2069 $s.="..|".$img." "._("Paste")."\n";
2070 }
2071 }
2072 }
2073 return($s);
2074 }
2077 function get_used_snapshot_bases()
2078 {
2079 return(array());
2080 }
2081 }
2083 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
2084 ?>