b957567d12a77cab1a76cacc81634f114a3fc025
1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 /*! \brief The plugin base class
22 \author Cajus Pollmeier <pollmeier@gonicus.de>
23 \version 2.00
24 \date 24.07.2003
26 This is the base class for all plugins. It can be used standalone or
27 can be included by the tabs class. All management should be done
28 within this class. Extend your plugins from this class.
29 */
31 class plugin
32 {
33 /*!
34 \brief Reference to parent object
36 This variable is used when the plugin is included in tabs
37 and keeps reference to the tab class. Communication to other
38 tabs is possible by 'name'. So the 'fax' plugin can ask the
39 'userinfo' plugin for the fax number.
41 \sa tab
42 */
43 var $parent= NULL;
45 /*!
46 \brief Configuration container
48 Access to global configuration
49 */
50 var $config= NULL;
52 /*!
53 \brief Mark plugin as account
55 Defines whether this plugin is defined as an account or not.
56 This has consequences for the plugin to be saved from tab
57 mode. If it is set to 'FALSE' the tab will call the delete
58 function, else the save function. Should be set to 'TRUE' if
59 the construtor detects a valid LDAP object.
61 \sa plugin::plugin()
62 */
63 var $is_account= FALSE;
64 var $initially_was_account= FALSE;
66 /*!
67 \brief Mark plugin as template
69 Defines whether we are creating a template or a normal object.
70 Has conseqences on the way execute() shows the formular and how
71 save() puts the data to LDAP.
73 \sa plugin::save() plugin::execute()
74 */
75 var $is_template= FALSE;
76 var $ignore_account= FALSE;
77 var $is_modified= FALSE;
79 /*!
80 \brief Represent temporary LDAP data
82 This is only used internally.
83 */
84 var $attrs= array();
86 /* Keep set of conflicting plugins */
87 var $conflicts= array();
89 /* Save unit tags */
90 var $gosaUnitTag= "";
92 /*!
93 \brief Used standard values
95 dn
96 */
97 var $dn= "";
98 var $uid= "";
99 var $sn= "";
100 var $givenName= "";
101 var $acl= "*none*";
102 var $dialog= FALSE;
103 var $snapDialog = NULL;
105 /* attribute list for save action */
106 var $attributes= array();
107 var $objectclasses= array();
108 var $is_new= TRUE;
109 var $saved_attributes= array();
111 var $acl_base= "";
112 var $acl_category= "";
114 /* Plugin identifier */
115 var $plHeadline= "";
116 var $plDescription= "";
118 /* This can be set to render the tabulators in another stylesheet */
119 var $pl_notify= FALSE;
121 /*! \brief plugin constructor
123 If 'dn' is set, the node loads the given 'dn' from LDAP
125 \param dn Distinguished name to initialize plugin from
126 \sa plugin()
127 */
128 function plugin (&$config, $dn= NULL, $parent= NULL)
129 {
130 /* Configuration is fine, allways */
131 $this->config= &$config;
132 $this->dn= $dn;
134 /* Handle new accounts, don't read information from LDAP */
135 if ($dn == "new"){
136 return;
137 }
139 /* Save current dn as acl_base */
140 $this->acl_base= $dn;
142 /* Get LDAP descriptor */
143 $ldap= $this->config->get_ldap_link();
144 if ($dn !== NULL){
146 /* Load data to 'attrs' and save 'dn' */
147 if ($parent !== NULL){
148 $this->attrs= $parent->attrs;
149 } else {
150 $ldap->cat ($dn);
151 $this->attrs= $ldap->fetch();
152 }
154 /* Copy needed attributes */
155 foreach ($this->attributes as $val){
156 $found= array_key_ics($val, $this->attrs);
157 if ($found != ""){
158 $this->$val= $this->attrs["$found"][0];
159 }
160 }
162 /* gosaUnitTag loading... */
163 if (isset($this->attrs['gosaUnitTag'][0])){
164 $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
165 }
167 /* Set the template flag according to the existence of objectClass
168 gosaUserTemplate */
169 if (isset($this->attrs['objectClass'])){
170 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
171 $this->is_template= TRUE;
172 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
173 "found", "Template check");
174 }
175 }
177 /* Is Account? */
178 $found= TRUE;
179 foreach ($this->objectclasses as $obj){
180 if (preg_match('/top/i', $obj)){
181 continue;
182 }
183 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
184 $found= FALSE;
185 break;
186 }
187 }
188 if ($found){
189 $this->is_account= TRUE;
190 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
191 "found", "Object check");
192 }
194 /* Prepare saved attributes */
195 $this->saved_attributes= $this->attrs;
196 foreach ($this->saved_attributes as $index => $value){
197 if (preg_match('/^[0-9]+$/', $index)){
198 unset($this->saved_attributes[$index]);
199 continue;
200 }
201 if (!in_array($index, $this->attributes) && $index != "objectClass"){
202 unset($this->saved_attributes[$index]);
203 continue;
204 }
205 if ($this->saved_attributes[$index]["count"] == 1){
206 $tmp= $this->saved_attributes[$index][0];
207 unset($this->saved_attributes[$index]);
208 $this->saved_attributes[$index]= $tmp;
209 continue;
210 }
212 unset($this->saved_attributes["$index"]["count"]);
213 }
214 }
216 /* Save initial account state */
217 $this->initially_was_account= $this->is_account;
218 }
220 /*! \brief execute plugin
222 Generates the html output for this node
223 */
224 function execute()
225 {
226 /* This one is empty currently. Fabian - please fill in the docu code */
227 $_SESSION['current_class_for_help'] = get_class($this);
229 /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
230 $_SESSION['LOCK_VARS_TO_USE'] = $_SESSION['LOCK_VARS_USED'] =array();
231 }
233 /*! \brief execute plugin
234 Removes object from parent
235 */
236 function remove_from_parent()
237 {
238 /* include global link_info */
239 $ldap= $this->config->get_ldap_link();
241 /* Get current objectClasses in order to add the required ones */
242 $ldap->cat($this->dn);
243 $tmp= $ldap->fetch ();
244 $oc= array();
245 if (isset($tmp['objectClass'])){
246 $oc= $tmp['objectClass'];
247 unset($oc['count']);
248 }
250 /* Remove objectClasses from entry */
251 $ldap->cd($this->dn);
252 $this->attrs= array();
253 $this->attrs['objectClass']= array_remove_entries($this->objectclasses,$oc);
255 /* Unset attributes from entry */
256 foreach ($this->attributes as $val){
257 $this->attrs["$val"]= array();
258 }
260 /* Unset account info */
261 $this->is_account= FALSE;
263 /* Do not write in plugin base class, this must be done by
264 children, since there are normally additional attribs,
265 lists, etc. */
266 /*
267 $ldap->modify($this->attrs);
268 */
269 }
272 /* Save data to object */
273 function save_object()
274 {
275 /* Save values to object */
276 foreach ($this->attributes as $val){
277 if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
278 /* Check for modifications */
279 if (get_magic_quotes_gpc()) {
280 $data= stripcslashes($_POST["$val"]);
281 } else {
282 $data= $this->$val = $_POST["$val"];
283 }
284 if ($this->$val != $data){
285 $this->is_modified= TRUE;
286 }
288 /* Okay, how can I explain this fix ...
289 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
290 * So IE posts these 'unselectable' option, with value = chr(194)
291 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
292 * This was added for W3c compliance, but now causes these ... ldap errors ...
293 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
294 */
295 if(isset($data[0]) && $data[0] == chr(194)) {
296 $data = "";
297 }
298 $this->$val= $data;
299 //echo "<font color='blue'>".$val."</font><br>";
300 }else{
301 //echo "<font color='red'>".$val."</font><br>";
302 }
303 }
304 }
307 /* Save data to LDAP, depending on is_account we save or delete */
308 function save()
309 {
310 /* include global link_info */
311 $ldap= $this->config->get_ldap_link();
313 /* Start with empty array */
314 $this->attrs= array();
316 /* Get current objectClasses in order to add the required ones */
317 $ldap->cat($this->dn);
319 $tmp= $ldap->fetch ();
321 $oc= array();
322 if (isset($tmp['objectClass'])){
323 $oc= $tmp["objectClass"];
324 $this->is_new= FALSE;
325 unset($oc['count']);
326 } else {
327 $this->is_new= TRUE;
328 }
330 /* Load (minimum) attributes, add missing ones */
331 $this->attrs['objectClass']= gosa_array_merge($oc,$this->objectclasses);
333 /* Copy standard attributes */
334 foreach ($this->attributes as $val){
335 if ($this->$val != ""){
336 $this->attrs["$val"]= $this->$val;
337 } elseif (!$this->is_new) {
338 $this->attrs["$val"]= array();
339 }
340 }
342 }
345 function cleanup()
346 {
347 foreach ($this->attrs as $index => $value){
349 /* Convert arrays with one element to non arrays, if the saved
350 attributes are no array, too */
351 if (is_array($this->attrs[$index]) &&
352 count ($this->attrs[$index]) == 1 &&
353 isset($this->saved_attributes[$index]) &&
354 !is_array($this->saved_attributes[$index])){
356 $tmp= $this->attrs[$index][0];
357 $this->attrs[$index]= $tmp;
358 }
360 /* Remove emtpy arrays if they do not differ */
361 if (is_array($this->attrs[$index]) &&
362 count($this->attrs[$index]) == 0 &&
363 !isset($this->saved_attributes[$index])){
365 unset ($this->attrs[$index]);
366 continue;
367 }
369 /* Remove single attributes that do not differ */
370 if (!is_array($this->attrs[$index]) &&
371 isset($this->saved_attributes[$index]) &&
372 !is_array($this->saved_attributes[$index]) &&
373 $this->attrs[$index] == $this->saved_attributes[$index]){
375 unset ($this->attrs[$index]);
376 continue;
377 }
379 /* Remove arrays that do not differ */
380 if (is_array($this->attrs[$index]) &&
381 isset($this->saved_attributes[$index]) &&
382 is_array($this->saved_attributes[$index])){
384 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
385 unset ($this->attrs[$index]);
386 continue;
387 }
388 }
389 }
391 /* Update saved attributes and ensure that next cleanups will be successful too */
392 foreach($this->attrs as $name => $value){
393 $this->saved_attributes[$name] = $value;
394 }
395 }
397 /* Check formular input */
398 function check()
399 {
400 $message= array();
402 /* Skip if we've no config object */
403 if (!isset($this->config) || !is_object($this->config)){
404 return $message;
405 }
407 /* Find hooks entries for this class */
408 $command= $this->config->search(get_class($this), "CHECK", array('menu', 'tabs'));
410 if ($command != ""){
412 if (!check_command($command)){
413 $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
414 get_class($this));
415 } else {
417 /* Generate "ldif" for check hook */
418 $ldif= "dn: $this->dn\n";
420 /* ... objectClasses */
421 foreach ($this->objectclasses as $oc){
422 $ldif.= "objectClass: $oc\n";
423 }
425 /* ... attributes */
426 foreach ($this->attributes as $attr){
427 if ($this->$attr == ""){
428 continue;
429 }
430 if (is_array($this->$attr)){
431 foreach ($this->$attr as $val){
432 $ldif.= "$attr: $val\n";
433 }
434 } else {
435 $ldif.= "$attr: ".$this->$attr."\n";
436 }
437 }
439 /* Append empty line */
440 $ldif.= "\n";
442 /* Feed "ldif" into hook and retrieve result*/
443 $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
444 $fh= proc_open($command, $descriptorspec, $pipes);
445 if (is_resource($fh)) {
446 fwrite ($pipes[0], $ldif);
447 fclose($pipes[0]);
449 $result= stream_get_contents($pipes[1]);
450 if ($result != ""){
451 $message[]= $result;
452 }
454 fclose($pipes[1]);
455 fclose($pipes[2]);
456 proc_close($fh);
457 }
458 }
460 }
462 return ($message);
463 }
465 /* Adapt from template, using 'dn' */
466 function adapt_from_template($dn)
467 {
468 /* Include global link_info */
469 $ldap= $this->config->get_ldap_link();
471 /* Load requested 'dn' to 'attrs' */
472 $ldap->cat ($dn);
473 $this->attrs= $ldap->fetch();
475 /* Walk through attributes */
476 foreach ($this->attributes as $val){
478 if (isset($this->attrs["$val"][0])){
480 /* If attribute is set, replace dynamic parts:
481 %sn, %givenName and %uid. Fill these in our local variables. */
482 $value= $this->attrs["$val"][0];
484 foreach (array("sn", "givenName", "uid") as $repl){
485 if (preg_match("/%$repl/i", $value)){
486 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
487 }
488 }
489 $this->$val= $value;
490 }
491 }
493 /* Is Account? */
494 $found= TRUE;
495 foreach ($this->objectclasses as $obj){
496 if (preg_match('/top/i', $obj)){
497 continue;
498 }
499 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
500 $found= FALSE;
501 break;
502 }
503 }
504 if ($found){
505 $this->is_account= TRUE;
506 }
507 }
509 /* Indicate whether a password change is needed or not */
510 function password_change_needed()
511 {
512 return FALSE;
513 }
516 /* Show header message for tab dialogs */
517 function show_enable_header($button_text, $text, $disabled= FALSE)
518 {
519 if (($disabled == TRUE) || (!$this->acl_is_createable())){
520 $state= "disabled";
521 } else {
522 $state= "";
523 }
524 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
525 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
526 "><p class=\"seperator\"> </p></td></tr></table>";
528 return($display);
529 }
532 /* Show header message for tab dialogs */
533 function show_disable_header($button_text, $text, $disabled= FALSE)
534 {
535 if (($disabled == TRUE) || !$this->acl_is_removeable()){
536 $state= "disabled";
537 } else {
538 $state= "";
539 }
540 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
541 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
542 "><p class=\"seperator\"> </p></td></tr></table>";
544 return($display);
545 }
548 /* Show header message for tab dialogs */
549 function show_header($button_text, $text, $disabled= FALSE)
550 {
551 echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
552 if ($disabled == TRUE){
553 $state= "disabled";
554 } else {
555 $state= "";
556 }
557 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
558 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
559 ($this->acl_is_createable()?'':'disabled')." ".$state.
560 "><p class=\"seperator\"> </p></td></tr></table>";
562 return($display);
563 }
566 function postcreate($add_attrs= array())
567 {
568 /* Find postcreate entries for this class */
569 $command= $this->config->search(get_class($this), "POSTCREATE",array('menu', 'tabs'));
571 if ($command != ""){
573 /* Additional attributes */
574 foreach ($add_attrs as $name => $value){
575 $command= preg_replace("/%$name/", $value, $command);
576 }
578 /* Walk through attribute list */
579 foreach ($this->attributes as $attr){
580 if (!is_array($this->$attr)){
581 $command= preg_replace("/%$attr/", $this->$attr, $command);
582 }
583 }
584 $command= preg_replace("/%dn/", $this->dn, $command);
586 if (check_command($command)){
587 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
588 $command, "Execute");
590 exec($command);
591 } else {
592 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
593 print_red ($message);
594 }
595 }
596 }
598 function postmodify($add_attrs= array())
599 {
600 /* Find postcreate entries for this class */
601 $command= $this->config->search(get_class($this), "POSTMODIFY",array('menu'));
602 if ($command == "" && isset($this->config->data['TABS'])){
603 $command= $this->config->search(get_class($this), "POSTMODIFY",array('tabs'));
604 }
606 if ($command != ""){
608 /* Additional attributes */
609 foreach ($add_attrs as $name => $value){
610 $command= preg_replace("/%$name/", $value, $command);
611 }
613 /* Walk through attribute list */
614 foreach ($this->attributes as $attr){
615 if (!is_array($this->$attr)){
616 $command= preg_replace("/%$attr/", $this->$attr, $command);
617 }
618 }
619 $command= preg_replace("/%dn/", $this->dn, $command);
621 if (check_command($command)){
622 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
623 $command, "Execute");
625 exec($command);
626 } else {
627 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
628 print_red ($message);
629 }
630 }
631 }
633 function postremove($add_attrs= array())
634 {
635 /* Find postremove entries for this class */
636 $command= $this->config->search(get_class($this), "POSTREMOVE",array('menu'));
637 if ($command == "" && isset($this->config->data['TABS'])){
638 $command= $this->config->search(get_class($this), "POSTREMOVE",array('tabs'));
639 }
641 if ($command != ""){
643 /* Additional attributes */
644 foreach ($add_attrs as $name => $value){
645 $command= preg_replace("/%$name/", $value, $command);
646 }
648 /* Walk through attribute list */
649 foreach ($this->attributes as $attr){
650 if (!is_array($this->$attr)){
651 $command= preg_replace("/%$attr/", $this->$attr, $command);
652 }
653 }
654 $command= preg_replace("/%dn/", $this->dn, $command);
656 /* Additional attributes */
657 foreach ($add_attrs as $name => $value){
658 $command= preg_replace("/%$name/", $value, $command);
659 }
661 if (check_command($command)){
662 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
663 $command, "Execute");
665 exec($command);
666 } else {
667 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
668 print_red ($message);
669 }
670 }
671 }
673 /* Create unique DN */
674 function create_unique_dn($attribute, $base)
675 {
676 $ldap= $this->config->get_ldap_link();
677 $base= preg_replace("/^,*/", "", $base);
679 /* Try to use plain entry first */
680 $dn= "$attribute=".$this->$attribute.",$base";
681 $ldap->cat ($dn, array('dn'));
682 if (!$ldap->fetch()){
683 return ($dn);
684 }
686 /* Look for additional attributes */
687 foreach ($this->attributes as $attr){
688 if ($attr == $attribute || $this->$attr == ""){
689 continue;
690 }
692 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
693 $ldap->cat ($dn, array('dn'));
694 if (!$ldap->fetch()){
695 return ($dn);
696 }
697 }
699 /* None found */
700 return ("none");
701 }
703 function rebind($ldap, $referral)
704 {
705 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
706 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
707 $this->error = "Success";
708 $this->hascon=true;
709 $this->reconnect= true;
710 return (0);
711 } else {
712 $this->error = "Could not bind to " . $credentials['ADMIN'];
713 return NULL;
714 }
715 }
717 /* This is a workaround function. */
718 function copy($src_dn, $dst_dn)
719 {
720 /* Rename dn in possible object groups */
721 $ldap= $this->config->get_ldap_link();
722 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
723 array('cn'));
724 while ($attrs= $ldap->fetch()){
725 $og= new ogroup($this->config, $ldap->getDN());
726 unset($og->member[$src_dn]);
727 $og->member[$dst_dn]= $dst_dn;
728 $og->save ();
729 }
731 $ldap->cat($dst_dn);
732 $attrs= $ldap->fetch();
733 if (count($attrs)){
734 trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
735 E_USER_WARNING);
736 return (FALSE);
737 }
739 $ldap->cat($src_dn);
740 $attrs= $ldap->fetch();
741 if (!count($attrs)){
742 trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
743 E_USER_WARNING);
744 return (FALSE);
745 }
747 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
748 $ds= ldap_connect($this->config->current['SERVER']);
749 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
750 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
751 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
752 }
754 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
755 $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
757 /* Fill data from LDAP */
758 $new= array();
759 if ($sr) {
760 $ei=ldap_first_entry($ds, $sr);
761 if ($ei) {
762 foreach($attrs as $attr => $val){
763 if ($info = @ldap_get_values_len($ds, $ei, $attr)){
764 for ($i= 0; $i<$info['count']; $i++){
765 if ($info['count'] == 1){
766 $new[$attr]= $info[$i];
767 } else {
768 $new[$attr][]= $info[$i];
769 }
770 }
771 }
772 }
773 }
774 }
776 /* close conncetion */
777 ldap_unbind($ds);
779 /* Adapt naming attribute */
780 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
781 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
782 $new[$dst_name]= @LDAP::fix($dst_val);
784 /* Check if this is a department.
785 * If it is a dep. && there is a , override in his ou
786 * change \2C to , again, else this entry can't be saved ...
787 */
788 if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
789 $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
790 }
792 /* Save copy */
793 $ldap->connect();
794 $ldap->cd($this->config->current['BASE']);
796 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
798 /* FAIvariable=.../..., cn=..
799 could not be saved, because the attribute FAIvariable was different to
800 the dn FAIvariable=..., cn=... */
801 if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
802 $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
803 }
804 $ldap->cd($dst_dn);
805 $ldap->add($new);
807 if ($ldap->error != "Success"){
808 trigger_error("Trying to save $dst_dn failed.",
809 E_USER_WARNING);
810 return(FALSE);
811 }
813 return (TRUE);
814 }
817 function move($src_dn, $dst_dn)
818 {
819 /* Copy source to destination */
820 if (!$this->copy($src_dn, $dst_dn)){
821 return (FALSE);
822 }
824 /* Delete source */
825 $ldap= $this->config->get_ldap_link();
826 $ldap->rmdir($src_dn);
827 if ($ldap->error != "Success"){
828 trigger_error("Trying to delete $src_dn failed.",
829 E_USER_WARNING);
830 return (FALSE);
831 }
833 return (TRUE);
834 }
837 /* Move/Rename complete trees */
838 function recursive_move($src_dn, $dst_dn)
839 {
840 /* Check if the destination entry exists */
841 $ldap= $this->config->get_ldap_link();
843 /* Check if destination exists - abort */
844 $ldap->cat($dst_dn, array('dn'));
845 if ($ldap->fetch()){
846 trigger_error("recursive_move $dst_dn already exists.",
847 E_USER_WARNING);
848 return (FALSE);
849 }
851 /* Perform a search for all objects to be moved */
852 $objects= array();
853 $ldap->cd($src_dn);
854 $ldap->search("(objectClass=*)", array("dn"));
855 while($attrs= $ldap->fetch()){
856 $dn= $attrs['dn'];
857 $objects[$dn]= strlen($dn);
858 }
860 /* Sort objects by indent level */
861 asort($objects);
862 reset($objects);
864 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
865 foreach ($objects as $object => $len){
866 $src= $object;
867 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
868 if (!$this->copy($src, $dst)){
869 return (FALSE);
870 }
871 }
873 /* Remove src_dn */
874 $ldap->cd($src_dn);
875 $ldap->recursive_remove();
876 return (TRUE);
877 }
880 function handle_post_events($mode, $add_attrs= array())
881 {
882 switch ($mode){
883 case "add":
884 $this->postcreate($add_attrs);
885 break;
887 case "modify":
888 $this->postmodify($add_attrs);
889 break;
891 case "remove":
892 $this->postremove($add_attrs);
893 break;
894 }
895 }
898 function saveCopyDialog(){
899 }
902 function getCopyDialog(){
903 return(array("string"=>"","status"=>""));
904 }
907 function PrepareForCopyPaste($source)
908 {
909 $todo = $this->attributes;
910 if(isset($this->CopyPasteVars)){
911 $todo = array_merge($todo,$this->CopyPasteVars);
912 }
914 if(count($this->objectclasses)){
915 $this->is_account = TRUE;
916 foreach($this->objectclasses as $class){
917 if(!in_array($class,$source['objectClass'])){
918 $this->is_account = FALSE;
919 }
920 }
921 }
923 foreach($todo as $var){
924 if (isset($source[$var])){
925 if(isset($source[$var]['count'])){
926 if($source[$var]['count'] > 1){
927 $this->$var = array();
928 $tmp = array();
929 for($i = 0 ; $i < $source[$var]['count']; $i++){
930 $tmp = $source[$var][$i];
931 }
932 $this->$var = $tmp;
933 # echo $var."=".$tmp."<br>";
934 }else{
935 $this->$var = $source[$var][0];
936 # echo $var."=".$source[$var][0]."<br>";
937 }
938 }else{
939 $this->$var= $source[$var];
940 # echo $var."=".$source[$var]."<br>";
941 }
942 }
943 }
944 }
947 function handle_object_tagging($dn= "", $tag= "", $show= false)
948 {
949 //FIXME: How to optimize this? We have at least two
950 // LDAP accesses per object. It would be a good
951 // idea to have it integrated.
953 /* No dn? Self-operation... */
954 if ($dn == ""){
955 $dn= $this->dn;
957 /* No tag? Find it yourself... */
958 if ($tag == ""){
959 $len= strlen($dn);
961 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
962 $relevant= array();
963 foreach ($this->config->adepartments as $key => $ntag){
965 /* This one is bigger than our dn, its not relevant... */
966 if ($len <= strlen($key)){
967 continue;
968 }
970 /* This one matches with the latter part. Break and don't fix this entry */
971 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
972 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
973 $relevant[strlen($key)]= $ntag;
974 continue;
975 }
977 }
979 /* If we've some relevant tags to set, just get the longest one */
980 if (count($relevant)){
981 ksort($relevant);
982 $tmp= array_keys($relevant);
983 $idx= end($tmp);
984 $tag= $relevant[$idx];
985 $this->gosaUnitTag= $tag;
986 }
987 }
988 }
991 /* Set tag? */
992 if ($tag != ""){
993 /* Set objectclass and attribute */
994 $ldap= $this->config->get_ldap_link();
995 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
996 $attrs= $ldap->fetch();
997 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
998 if ($show) {
999 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
1000 flush();
1001 }
1002 return;
1003 }
1004 if (count($attrs)){
1005 if ($show){
1006 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
1007 flush();
1008 }
1009 $nattrs= array("gosaUnitTag" => $tag);
1010 $nattrs['objectClass']= array();
1011 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
1012 $oc= $attrs['objectClass'][$i];
1013 if ($oc != "gosaAdministrativeUnitTag"){
1014 $nattrs['objectClass'][]= $oc;
1015 }
1016 }
1017 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
1018 $ldap->cd($dn);
1019 $ldap->modify($nattrs);
1020 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
1021 } else {
1022 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
1023 }
1025 } else {
1026 /* Remove objectclass and attribute */
1027 $ldap= $this->config->get_ldap_link();
1028 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
1029 $attrs= $ldap->fetch();
1030 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
1031 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
1032 return;
1033 }
1034 if (count($attrs)){
1035 if ($show){
1036 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
1037 flush();
1038 }
1039 $nattrs= array("gosaUnitTag" => array());
1040 $nattrs['objectClass']= array();
1041 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
1042 $oc= $attrs['objectClass'][$i];
1043 if ($oc != "gosaAdministrativeUnitTag"){
1044 $nattrs['objectClass'][]= $oc;
1045 }
1046 }
1047 $ldap->cd($dn);
1048 $ldap->modify($nattrs);
1049 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
1050 } else {
1051 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
1052 }
1053 }
1055 }
1058 /* Add possibility to stop remove process */
1059 function allow_remove()
1060 {
1061 $reason= "";
1062 return $reason;
1063 }
1066 /* Create a snapshot of the current object */
1067 function create_snapshot($type= "snapshot", $description= array())
1068 {
1070 /* Check if snapshot functionality is enabled */
1071 if(!$this->snapshotEnabled()){
1072 return;
1073 }
1075 /* Get configuration from gosa.conf */
1076 $tmp = $this->config->current;
1078 /* Create lokal ldap connection */
1079 $ldap= $this->config->get_ldap_link();
1080 $ldap->cd($this->config->current['BASE']);
1082 /* check if there are special server configurations for snapshots */
1083 if(!isset($tmp['SNAPSHOT_SERVER'])){
1085 /* Source and destination server are both the same, just copy source to dest obj */
1086 $ldap_to = $ldap;
1087 $snapldapbase = $this->config->current['BASE'];
1089 }else{
1090 $server = $tmp['SNAPSHOT_SERVER'];
1091 $user = $tmp['SNAPSHOT_USER'];
1092 $password = $tmp['SNAPSHOT_PASSWORD'];
1093 $snapldapbase = $tmp['SNAPSHOT_BASE'];
1095 $ldap_to = new LDAP($user,$password, $server);
1096 $ldap_to -> cd($snapldapbase);
1097 show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$snapldapbase));
1098 }
1100 /* check if the dn exists */
1101 if ($ldap->dn_exists($this->dn)){
1103 /* Extract seconds & mysecs, they are used as entry index */
1104 list($usec, $sec)= explode(" ", microtime());
1106 /* Collect some infos */
1107 $base = $this->config->current['BASE'];
1108 $snap_base = $tmp['SNAPSHOT_BASE'];
1109 $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1110 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1112 /* Create object */
1113 #$data = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1114 $data = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1115 $newName = preg_replace("/\./", "", $sec."-".$usec);
1116 $target= array();
1117 $target['objectClass'] = array("top", "gosaSnapshotObject");
1118 $target['gosaSnapshotData'] = gzcompress($data, 6);
1119 $target['gosaSnapshotType'] = $type;
1120 $target['gosaSnapshotDN'] = $this->dn;
1121 $target['description'] = $description;
1122 $target['gosaSnapshotTimestamp'] = $newName;
1124 /* Insert the new snapshot
1125 But we have to check first, if the given gosaSnapshotTimestamp
1126 is already used, in this case we should increment this value till there is
1127 an unused value. */
1128 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1129 $ldap_to->cat($new_dn);
1130 while($ldap_to->count()){
1131 $ldap_to->cat($new_dn);
1132 $newName = preg_replace("/\./", "", $sec."-".($usec++));
1133 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1134 $target['gosaSnapshotTimestamp'] = $newName;
1135 }
1137 /* Inset this new snapshot */
1138 $ldap_to->cd($snapldapbase);
1139 $ldap_to->create_missing_trees($new_base);
1140 $ldap_to->cd($new_dn);
1141 $ldap_to->add($target);
1143 show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1144 show_ldap_error($ldap_to->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1145 }
1146 }
1148 function remove_snapshot($dn)
1149 {
1150 $ui = get_userinfo();
1151 $old_dn = $this->dn;
1152 $this->dn = $dn;
1153 $ldap = $this->config->get_ldap_link();
1154 $ldap->cd($this->config->current['BASE']);
1155 $ldap->rmdir_recursive($dn);
1156 $this->dn = $old_dn;
1157 }
1160 /* returns true if snapshots are enabled, and false if it is disalbed
1161 There will also be some errors psoted, if the configuration failed */
1162 function snapshotEnabled()
1163 {
1164 $tmp = $this->config->current;
1165 if(isset($tmp['ENABLE_SNAPSHOT'])){
1166 if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1168 /* Check if the snapshot_base is defined */
1169 if(!isset($tmp['SNAPSHOT_BASE'])){
1170 print_red(sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not configured in your gosa.conf."),"SNAPSHOT_BASE"));
1171 return(FALSE);
1172 }
1174 /* check if there are special server configurations for snapshots */
1175 if(isset($tmp['SNAPSHOT_SERVER'])){
1177 /* check if all required vars are available to create a new ldap connection */
1178 $missing = "";
1179 foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_BASE") as $var){
1180 if(!isset($tmp[$var])){
1181 $missing .= $var." ";
1182 print_red(sprintf(_("The snapshot functionality is enabled, but the required variable(s) '%s' is not configured in your gosa.conf."),$missing));
1183 return(FALSE);
1184 }
1185 }
1186 }
1187 return(TRUE);
1188 }
1189 }
1190 return(FALSE);
1191 }
1194 /* Return available snapshots for the given base
1195 */
1196 function Available_SnapsShots($dn,$raw = false)
1197 {
1198 if(!$this->snapshotEnabled()) return(array());
1200 /* Create an additional ldap object which
1201 points to our ldap snapshot server */
1202 $ldap= $this->config->get_ldap_link();
1203 $ldap->cd($this->config->current['BASE']);
1204 $cfg= &$this->config->current;
1206 /* check if there are special server configurations for snapshots */
1207 if(isset($cfg['SNAPSHOT_SERVER'])){
1208 $server = $cfg['SNAPSHOT_SERVER'];
1209 $user = $cfg['SNAPSHOT_USER'];
1210 $password = $cfg['SNAPSHOT_PASSWORD'];
1211 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1212 $ldap_to = new LDAP($user,$password, $server);
1213 $ldap_to -> cd ($snapldapbase);
1214 show_ldap_error($ldap->get_error(), sprintf(_("Method get available snapshots with dn '%s' failed."),$this->dn));
1215 }else{
1216 $ldap_to = $ldap;
1217 }
1219 /* Prepare bases and some other infos */
1220 $base = $this->config->current['BASE'];
1221 $snap_base = $cfg['SNAPSHOT_BASE'];
1222 $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1223 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1224 $tmp = array();
1226 /* Fetch all objects with gosaSnapshotDN=$dn */
1227 $ldap_to->cd($new_base);
1228 $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1229 array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1231 /* Put results into a list and add description if missing */
1232 while($entry = $ldap_to->fetch()){
1233 if(!isset($entry['description'][0])){
1234 $entry['description'][0] = "";
1235 }
1236 $tmp[] = $entry;
1237 }
1239 /* Return the raw array, or format the result */
1240 if($raw){
1241 return($tmp);
1242 }else{
1243 $tmp2 = array();
1244 foreach($tmp as $entry){
1245 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1246 }
1247 }
1248 return($tmp2);
1249 }
1252 function getAllDeletedSnapshots($base_of_object,$raw = false)
1253 {
1254 if(!$this->snapshotEnabled()) return(array());
1256 /* Create an additional ldap object which
1257 points to our ldap snapshot server */
1258 $ldap= $this->config->get_ldap_link();
1259 $ldap->cd($this->config->current['BASE']);
1260 $cfg= &$this->config->current;
1262 /* check if there are special server configurations for snapshots */
1263 if(isset($cfg['SNAPSHOT_SERVER'])){
1264 $server = $cfg['SNAPSHOT_SERVER'];
1265 $user = $cfg['SNAPSHOT_USER'];
1266 $password = $cfg['SNAPSHOT_PASSWORD'];
1267 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1268 $ldap_to = new LDAP($user,$password, $server);
1269 $ldap_to->cd ($snapldapbase);
1270 show_ldap_error($ldap->get_error(), sprintf(_("Method get deleted snapshots with dn '%s' failed."),$this->dn));
1271 }else{
1272 $ldap_to = $ldap;
1273 }
1275 /* Prepare bases */
1276 $base = $this->config->current['BASE'];
1277 $snap_base = $cfg['SNAPSHOT_BASE'];
1278 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1280 /* Fetch all objects and check if they do not exist anymore */
1281 $ui = get_userinfo();
1282 $tmp = array();
1283 $ldap_to->cd($new_base);
1284 $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1285 while($entry = $ldap_to->fetch()){
1287 $chk = str_replace($new_base,"",$entry['dn']);
1288 if(preg_match("/,ou=/",$chk)) continue;
1290 if(!isset($entry['description'][0])){
1291 $entry['description'][0] = "";
1292 }
1293 $tmp[] = $entry;
1294 }
1296 /* Check if entry still exists */
1297 foreach($tmp as $key => $entry){
1298 $ldap->cat($entry['gosaSnapshotDN'][0]);
1299 if($ldap->count()){
1300 unset($tmp[$key]);
1301 }
1302 }
1304 /* Format result as requested */
1305 if($raw) {
1306 return($tmp);
1307 }else{
1308 $tmp2 = array();
1309 foreach($tmp as $key => $entry){
1310 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1311 }
1312 }
1313 return($tmp2);
1314 }
1317 /* Restore selected snapshot */
1318 function restore_snapshot($dn)
1319 {
1320 if(!$this->snapshotEnabled()) return(array());
1322 $ldap= $this->config->get_ldap_link();
1323 $ldap->cd($this->config->current['BASE']);
1324 $cfg= &$this->config->current;
1326 /* check if there are special server configurations for snapshots */
1327 if(isset($cfg['SNAPSHOT_SERVER'])){
1328 $server = $cfg['SNAPSHOT_SERVER'];
1329 $user = $cfg['SNAPSHOT_USER'];
1330 $password = $cfg['SNAPSHOT_PASSWORD'];
1331 $snapldapbase = $cfg['SNAPSHOT_BASE'];
1332 $ldap_to = new LDAP($user,$password, $server);
1333 $ldap_to->cd ($snapldapbase);
1334 show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$snapldapbase));
1335 }else{
1336 $ldap_to = $ldap;
1337 }
1339 /* Get the snapshot */
1340 $ldap_to->cat($dn);
1341 $restoreObject = $ldap_to->fetch();
1343 /* Prepare import string */
1344 $data = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1346 /* Import the given data */
1347 $ldap->import_complete_ldif($data,$err,false,false);
1348 show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$dn));
1349 }
1352 function showSnapshotDialog($base,$baseSuffixe)
1353 {
1354 $once = true;
1355 foreach($_POST as $name => $value){
1357 /* Create a new snapshot, display a dialog */
1358 if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1359 $once = false;
1360 $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1361 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1362 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1363 }
1365 /* Restore a snapshot, display a dialog with all snapshots of the current object */
1366 if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1367 $once = false;
1368 $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1369 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1370 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1371 $this->snapDialog->display_restore_dialog = true;
1372 }
1374 /* Restore one of the already deleted objects */
1375 if(preg_match("/^RestoreDeletedSnapShot_/",$name) && $once){
1376 $once = false;
1377 $this->snapDialog = new SnapShotDialog($this->config,"",$this);
1378 $this->snapDialog->set_snapshot_bases($baseSuffixe);
1379 $this->snapDialog->display_restore_dialog = true;
1380 $this->snapDialog->display_all_removed_objects = true;
1381 }
1383 /* Restore selected snapshot */
1384 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1385 $once = false;
1386 $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1387 $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1388 if(!empty($entry)){
1389 $this->restore_snapshot($entry);
1390 $this->snapDialog = NULL;
1391 }
1392 }
1393 }
1395 /* Create a new snapshot requested, check
1396 the given attributes and create the snapshot*/
1397 if(isset($_POST['CreateSnapshot'])){
1398 $this->snapDialog->save_object();
1399 $msgs = $this->snapDialog->check();
1400 if(count($msgs)){
1401 foreach($msgs as $msg){
1402 print_red($msg);
1403 }
1404 }else{
1405 $this->dn = $this->snapDialog->dn;
1406 $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1407 $this->snapDialog = NULL;
1408 }
1409 }
1411 /* Restore is requested, restore the object with the posted dn .*/
1412 if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1413 }
1415 if(isset($_POST['CancelSnapshot'])){
1416 $this->snapDialog = NULL;
1417 }
1419 if(is_object($this->snapDialog )){
1420 $this->snapDialog->save_object();
1421 return($this->snapDialog->execute());
1422 }
1423 }
1426 function plInfo()
1427 {
1428 return array();
1429 }
1432 function set_acl_base($base)
1433 {
1434 $this->acl_base= $base;
1435 }
1438 function set_acl_category($category)
1439 {
1440 $this->acl_category= "$category/";
1441 }
1444 function acl_is_writeable($attribute,$skip_write = FALSE)
1445 {
1446 $ui= get_userinfo();
1447 return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1448 }
1451 function acl_is_readable($attribute)
1452 {
1453 $ui= get_userinfo();
1454 return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1455 }
1458 function acl_is_createable()
1459 {
1460 $ui= get_userinfo();
1461 return preg_match('/c/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1462 }
1465 function acl_is_removeable()
1466 {
1467 $ui= get_userinfo();
1468 return preg_match('/d/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1469 }
1472 function acl_is_moveable()
1473 {
1474 $ui= get_userinfo();
1475 return preg_match('/m/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1476 }
1479 function acl_have_any_permissions()
1480 {
1481 }
1484 function getacl($attribute,$skip_write= FALSE)
1485 {
1486 $ui= get_userinfo();
1487 return $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1488 }
1490 /* Get all allowed bases to move an object to or to create a new object.
1491 Idepartments also contains all base departments which lead to the allowed bases */
1492 function get_allowed_bases($category = "")
1493 {
1494 $ui = get_userinfo();
1495 $deps = array();
1497 /* Set category */
1498 if(empty($category)){
1499 $category = $this->acl_category.get_class($this);
1500 }
1502 /* Is this a new object ? Or just an edited existing object */
1503 if(!$this->initially_was_account && $this->is_account){
1504 $new = true;
1505 }else{
1506 $new = false;
1507 }
1509 $cat_bases = $ui->get_module_departments(preg_replace("/\/.*$/","",$category));
1510 foreach($this->config->idepartments as $dn => $name){
1512 if(!in_array_ics($dn,$cat_bases)){
1513 continue;
1514 }
1516 $acl = $ui->get_permissions($dn,$category);
1517 if($new && preg_match("/c/",$acl)){
1518 $deps[$dn] = $name;
1519 }elseif(!$new && preg_match("/m/",$acl)){
1520 $deps[$dn] = $name;
1521 }
1522 }
1524 /* Add current base */
1525 if(isset($this->base) && isset($this->config->idepartments[$this->base])){
1526 $deps[$this->base] = $this->config->idepartments[$this->base];
1527 }else{
1528 echo "No default base found. ".$this->base."<br> ";
1529 }
1531 return($deps);
1532 }
1534 /* This function modifies object acls too, if an object is moved.
1535 * $old_dn specifies the actually used dn
1536 * $new_dn specifies the destiantion dn
1537 */
1538 function update_acls($old_dn,$new_dn,$output_changes = FALSE)
1539 {
1540 /* Check if old_dn is empty. This should never happen */
1541 if(empty($old_dn) || empty($new_dn)){
1542 trigger_error("Failed to check acl dependencies, wrong dn given.");
1543 return;
1544 }
1546 /* Update userinfo if necessary */
1547 if($_SESSION['ui']->dn == $old_dn){
1548 $_SESSION['ui']->dn = $new_dn;
1549 new log("view","acl/".get_class($this),$this->dn,array(),"Updated current user dn from '".$old_dn."' to '".$new_dn."'");
1550 }
1552 /* Object was moved, ensure that all acls will be moved too */
1553 if($new_dn != $old_dn && $old_dn != "new"){
1555 /* get_ldap configuration */
1556 $update = array();
1557 $ldap = $this->config->get_ldap_link();
1558 $ldap->cd ($this->config->current['BASE']);
1559 $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*))",array("cn","gosaAclEntry"));
1560 while($attrs = $ldap->fetch()){
1562 $acls = array();
1564 /* Walk through acls */
1565 for($i = 0 ; $i < $attrs['gosaAclEntry']['count'] ; $i ++ ){
1567 /* Reset vars */
1568 $found = false;
1570 /* Get Acl parts */
1571 $acl_parts = split(":",$attrs['gosaAclEntry'][$i]);
1573 /* Get every single member for this acl */
1574 $members = array();
1575 if(preg_match("/,/",$acl_parts[2])){
1576 $members = split(",",$acl_parts[2]);
1577 }else{
1578 $members = array($acl_parts[2]);
1579 }
1581 /* Check if member match current dn */
1582 foreach($members as $key => $member){
1583 $member = base64_decode($member);
1584 if($member == $old_dn){
1585 $found = true;
1586 $members[$key] = base64_encode($new_dn);
1587 }
1588 }
1590 /* Create new member string */
1591 $new_members = "";
1592 foreach($members as $member){
1593 $new_members .= $member.",";
1594 }
1595 $new_members = preg_replace("/,$/","",$new_members);
1596 $acl_parts[2] = $new_members;
1598 /* Reconstruckt acl entry */
1599 $acl_str ="";
1600 foreach($acl_parts as $t){
1601 $acl_str .= $t.":";
1602 }
1603 $acl_str = preg_replace("/:$/","",$acl_str);
1604 }
1606 /* Acls for this object must be adjusted */
1607 if($found){
1609 if($output_changes){
1610 echo "<font color='green'>".
1611 _("Changing ACL dn")." : <br> -"._("from")." <b> ".
1612 $old_dn.
1613 "</b><br> -"._("to")." <b>".
1614 $new_dn.
1615 "</b></font><br>";
1616 }
1617 $update[$attrs['dn']] =array();
1618 foreach($acls as $acl){
1619 $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1620 }
1621 }
1622 }
1624 /* Write updated acls */
1625 foreach($update as $dn => $attrs){
1626 $ldap->cd($dn);
1627 $ldap->modify($attrs);
1628 }
1629 }
1630 }
1631 }
1632 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1633 ?>