Code

Updated framework
[gosa.git] / include / class_plugin.inc
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   /*! \brief plugin constructor
120     If 'dn' is set, the node loads the given 'dn' from LDAP
122     \param dn Distinguished name to initialize plugin from
123     \sa plugin()
124    */
125   function plugin ($config, $dn= NULL, $parent= NULL)
126   {
127     /* Configuration is fine, allways */
128     $this->config= $config;     
129     $this->dn= $dn;
131     /* Handle new accounts, don't read information from LDAP */
132     if ($dn == "new"){
133       return;
134     }
136     /* Save current dn as acl_base */
137     $this->acl_base= $dn;
139     /* Get LDAP descriptor */
140     $ldap= $this->config->get_ldap_link();
141     if ($dn != NULL){
143       /* Load data to 'attrs' and save 'dn' */
144       if ($parent != NULL){
145         $this->attrs= $parent->attrs;
146       } else {
147         $ldap->cat ($dn);
148         $this->attrs= $ldap->fetch();
149       }
151       /* Copy needed attributes */
152       foreach ($this->attributes as $val){
153         $found= array_key_ics($val, $this->attrs);
154         if ($found != ""){
155           $this->$val= $this->attrs["$found"][0];
156         }
157       }
159       /* gosaUnitTag loading... */
160       if (isset($this->attrs['gosaUnitTag'][0])){
161         $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
162       }
164       /* Set the template flag according to the existence of objectClass
165          gosaUserTemplate */
166       if (isset($this->attrs['objectClass'])){
167         if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
168           $this->is_template= TRUE;
169           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
170               "found", "Template check");
171         }
172       }
174       /* Is Account? */
175       error_reporting(0);
176       $found= TRUE;
177       foreach ($this->objectclasses as $obj){
178         if (preg_match('/top/i', $obj)){
179           continue;
180         }
181         if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
182           $found= FALSE;
183           break;
184         }
185       }
186       error_reporting(E_ALL);
187       if ($found){
188         $this->is_account= TRUE;
189         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
190             "found", "Object check");
191       }
193       /* Prepare saved attributes */
194       $this->saved_attributes= $this->attrs;
195       foreach ($this->saved_attributes as $index => $value){
196         if (preg_match('/^[0-9]+$/', $index)){
197           unset($this->saved_attributes[$index]);
198           continue;
199         }
200         if (!in_array($index, $this->attributes) && $index != "objectClass"){
201           unset($this->saved_attributes[$index]);
202           continue;
203         }
204         if ($this->saved_attributes[$index]["count"] == 1){
205           $tmp= $this->saved_attributes[$index][0];
206           unset($this->saved_attributes[$index]);
207           $this->saved_attributes[$index]= $tmp;
208           continue;
209         }
211         unset($this->saved_attributes["$index"]["count"]);
212       }
213     }
215     /* Save initial account state */
216     $this->initially_was_account= $this->is_account;
217   }
219   /*! \brief execute plugin
221     Generates the html output for this node
222    */
223   function execute()
224   {
225     /* This one is empty currently. Fabian - please fill in the docu code */
226     $_SESSION['current_class_for_help'] = get_class($this);
228     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
229     $_SESSION['LOCK_VARS_TO_USE'] = $_SESSION['LOCK_VARS_USED'] =array();
230   }
232   /*! \brief execute plugin
233      Removes object from parent
234    */
235   function remove_from_parent()
236   {
237     /* include global link_info */
238     $ldap= $this->config->get_ldap_link();
240     /* Get current objectClasses in order to add the required ones */
241     $ldap->cat($this->dn);
242     $tmp= $ldap->fetch ();
243     if (isset($tmp['objectClass'])){
244       $oc= $tmp['objectClass'];
245     } else {
246       $oc= array("count" => 0);
247     }
249     /* Remove objectClasses from entry */
250     $ldap->cd($this->dn);
251     $this->attrs= array();
252     $this->attrs['objectClass']= array();
253     for ($i= 0; $i<$oc["count"]; $i++){
254       if (!in_array_ics($oc[$i], $this->objectclasses)){
255         $this->attrs['objectClass'][]= $oc[$i];
256       }
257     }
259     /* Unset attributes from entry */
260     foreach ($this->attributes as $val){
261       $this->attrs["$val"]= array();
262     }
264     /* Unset account info */
265     $this->is_account= FALSE;
267     /* Do not write in plugin base class, this must be done by
268        children, since there are normally additional attribs,
269        lists, etc. */
270     /*
271        $ldap->modify($this->attrs);
272      */
273   }
276   /* Save data to object */
277   function save_object()
278   {
279     /* Save values to object */
280     foreach ($this->attributes as $val){
281       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
282         /* Check for modifications */
283         if (get_magic_quotes_gpc()) {
284           $data= stripcslashes($_POST["$val"]);
285         } else {
286           $data= $this->$val = $_POST["$val"];
287         }
288         if ($this->$val != $data){
289           $this->is_modified= TRUE;
290         }
291     
292         /* Okay, how can I explain this fix ... 
293          * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds. 
294          * So IE posts these 'unselectable' option, with value = chr(194) 
295          * chr(194) seems to be the &nbsp; in between the ...option>&nbsp;</option.. because there is no value=".." specified in these option fields  
296          * This &nbsp; was added for W3c compliance, but now causes these ... ldap errors ... 
297          * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
298          */
299         if(isset($data[0]) && $data[0] == chr(194)) {
300           $data = "";  
301         }
302         $this->$val= $data;
303         //echo "<font color='blue'>".$val."</font><br>";
304       }else{
305         //echo "<font color='red'>".$val."</font><br>";
306       }
307     }
308   }
311   /* Save data to LDAP, depending on is_account we save or delete */
312   function save()
313   {
314     /* include global link_info */
315     $ldap= $this->config->get_ldap_link();
317     /* Start with empty array */
318     $this->attrs= array();
320     /* Get current objectClasses in order to add the required ones */
321     $ldap->cat($this->dn);
322     
323     $tmp= $ldap->fetch ();
324     
325     if (isset($tmp['objectClass'])){
326       $oc= $tmp["objectClass"];
327       $this->is_new= FALSE;
328     } else {
329       $oc= array("count" => 0);
330       $this->is_new= TRUE;
331     }
333     /* Load (minimum) attributes, add missing ones */
334     $this->attrs['objectClass']= $this->objectclasses;
335     for ($i= 0; $i<$oc["count"]; $i++){
336       if (!in_array_ics($oc[$i], $this->objectclasses)){
337         $this->attrs['objectClass'][]= $oc[$i];
338       }
339     }
341     /* Copy standard attributes */
342     foreach ($this->attributes as $val){
343       if ($this->$val != ""){
344         $this->attrs["$val"]= $this->$val;
345       } elseif (!$this->is_new) {
346         $this->attrs["$val"]= array();
347       }
348     }
350   }
353   function cleanup()
354   {
355     foreach ($this->attrs as $index => $value){
357       /* Convert arrays with one element to non arrays, if the saved
358          attributes are no array, too */
359       if (is_array($this->attrs[$index]) && 
360           count ($this->attrs[$index]) == 1 &&
361           isset($this->saved_attributes[$index]) &&
362           !is_array($this->saved_attributes[$index])){
363           
364         $tmp= $this->attrs[$index][0];
365         $this->attrs[$index]= $tmp;
366       }
368       /* Remove emtpy arrays if they do not differ */
369       if (is_array($this->attrs[$index]) &&
370           count($this->attrs[$index]) == 0 &&
371           !isset($this->saved_attributes[$index])){
372           
373         unset ($this->attrs[$index]);
374         continue;
375       }
377       /* Remove single attributes that do not differ */
378       if (!is_array($this->attrs[$index]) &&
379           isset($this->saved_attributes[$index]) &&
380           !is_array($this->saved_attributes[$index]) &&
381           $this->attrs[$index] == $this->saved_attributes[$index]){
383         unset ($this->attrs[$index]);
384         continue;
385       }
387       /* Remove arrays that do not differ */
388       if (is_array($this->attrs[$index]) && 
389           isset($this->saved_attributes[$index]) &&
390           is_array($this->saved_attributes[$index])){
391           
392         if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
393           unset ($this->attrs[$index]);
394           continue;
395         }
396       }
397     }
398   }
400   /* Check formular input */
401   function check()
402   {
403     $message= array();
405     /* Skip if we've no config object */
406     if (!isset($this->config)){
407       return $message;
408     }
410     /* Find hooks entries for this class */
411     $command= search_config($this->config->data['MENU'], get_class($this), "CHECK");
412     if ($command == "" && isset($this->config->data['TABS'])){
413       $command= search_config($this->config->data['TABS'], get_class($this), "CHECK");
414     }
416     if ($command != ""){
418       if (!check_command($command)){
419         $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
420                             get_class($this));
421       } else {
423         /* Generate "ldif" for check hook */
424         $ldif= "dn: $this->dn\n";
425         
426         /* ... objectClasses */
427         foreach ($this->objectclasses as $oc){
428           $ldif.= "objectClass: $oc\n";
429         }
430         
431         /* ... attributes */
432         foreach ($this->attributes as $attr){
433           if ($this->$attr == ""){
434             continue;
435           }
436           if (is_array($this->$attr)){
437             foreach ($this->$attr as $val){
438               $ldif.= "$attr: $val\n";
439             }
440           } else {
441               $ldif.= "$attr: ".$this->$attr."\n";
442           }
443         }
445         /* Append empty line */
446         $ldif.= "\n";
448         /* Feed "ldif" into hook and retrieve result*/
449         $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
450         $fh= proc_open($command, $descriptorspec, $pipes);
451         if (is_resource($fh)) {
452           fwrite ($pipes[0], $ldif);
453           fclose($pipes[0]);
454           
455           $result= stream_get_contents($pipes[1]);
456           if ($result != ""){
457             $message[]= $result;
458           }
459           
460           fclose($pipes[1]);
461           fclose($pipes[2]);
462           proc_close($fh);
463         }
464       }
466     }
468     return ($message);
469   }
471   /* Adapt from template, using 'dn' */
472   function adapt_from_template($dn)
473   {
474     /* Include global link_info */
475     $ldap= $this->config->get_ldap_link();
477     /* Load requested 'dn' to 'attrs' */
478     $ldap->cat ($dn);
479     $this->attrs= $ldap->fetch();
481     /* Walk through attributes */
482     foreach ($this->attributes as $val){
484       if (isset($this->attrs["$val"][0])){
486         /* If attribute is set, replace dynamic parts: 
487            %sn, %givenName and %uid. Fill these in our local variables. */
488         $value= $this->attrs["$val"][0];
490         foreach (array("sn", "givenName", "uid") as $repl){
491           if (preg_match("/%$repl/i", $value)){
492             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
493           }
494         }
495         $this->$val= $value;
496       }
497     }
499     /* Is Account? */
500     $found= TRUE;
501     foreach ($this->objectclasses as $obj){
502       if (preg_match('/top/i', $obj)){
503         continue;
504       }
505       if (!in_array_ics ($obj, $this->attrs['objectClass'])){
506         $found= FALSE;
507         break;
508       }
509     }
510     if ($found){
511       $this->is_account= TRUE;
512     }
513   }
515   /* Indicate whether a password change is needed or not */
516   function password_change_needed()
517   {
518     return FALSE;
519   }
522   /* Show header message for tab dialogs */
523   function show_enable_header($button_text, $text, $disabled= FALSE)
524   {
525     if (($disabled == TRUE) || (!$this->acl_is_createable())){
526       $state= "disabled";
527     } else {
528       $state= "";
529     }
530     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
531     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
532       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
534     return($display);
535   }
538   /* Show header message for tab dialogs */
539   function show_disable_header($button_text, $text, $disabled= FALSE)
540   {
541     if (($disabled == TRUE) || !$this->acl_is_removeable()){
542       $state= "disabled";
543     } else {
544       $state= "";
545     }
546     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
547     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
548       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
550     return($display);
551   }
554   /* Show header message for tab dialogs */
555   function show_header($button_text, $text, $disabled= FALSE)
556   {
557     echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
558     if ($disabled == TRUE){
559       $state= "disabled";
560     } else {
561       $state= "";
562     }
563     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
564     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
565       ($this->acl_is_createable()?'':'disabled')." ".$state.
566       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
568     return($display);
569   }
572   function postcreate($add_attrs= array())
573   {
574     /* Find postcreate entries for this class */
575     $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
576     if ($command == "" && isset($this->config->data['TABS'])){
577       $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
578     }
580     if ($command != ""){
581       /* Walk through attribute list */
582       foreach ($this->attributes as $attr){
583         if (!is_array($this->$attr)){
584           $command= preg_replace("/%$attr/", $this->$attr, $command);
585         }
586       }
587       $command= preg_replace("/%dn/", $this->dn, $command);
589       /* Additional attributes */
590       foreach ($add_attrs as $name => $value){
591         $command= preg_replace("/%$name/", $value, $command);
592       }
594       if (check_command($command)){
595         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
596             $command, "Execute");
598         exec($command);
599       } else {
600         $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
601         print_red ($message);
602       }
603     }
604   }
606   function postmodify($add_attrs= array())
607   {
608     /* Find postcreate entries for this class */
609     $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
610     if ($command == "" && isset($this->config->data['TABS'])){
611       $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
612     }
614     if ($command != ""){
615       /* Walk through attribute list */
616       foreach ($this->attributes as $attr){
617         if (!is_array($this->$attr)){
618           $command= preg_replace("/%$attr/", $this->$attr, $command);
619         }
620       }
621       $command= preg_replace("/%dn/", $this->dn, $command);
623       /* Additional attributes */
624       foreach ($add_attrs as $name => $value){
625         $command= preg_replace("/%$name/", $value, $command);
626       }
628       if (check_command($command)){
629         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
630             $command, "Execute");
632         exec($command);
633       } else {
634         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
635         print_red ($message);
636       }
637     }
638   }
640   function postremove($add_attrs= array())
641   {
642     /* Find postremove entries for this class */
643     $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
644     if ($command == "" && isset($this->config->data['TABS'])){
645       $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
646     }
648     if ($command != ""){
649       /* Walk through attribute list */
650       foreach ($this->attributes as $attr){
651         if (!is_array($this->$attr)){
652           $command= preg_replace("/%$attr/", $this->$attr, $command);
653         }
654       }
655       $command= preg_replace("/%dn/", $this->dn, $command);
657       /* Additional attributes */
658       foreach ($add_attrs as $name => $value){
659         $command= preg_replace("/%$name/", $value, $command);
660       }
662       if (check_command($command)){
663         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
664             $command, "Execute");
666         exec($command);
667       } else {
668         $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
669         print_red ($message);
670       }
671     }
672   }
674   /* Create unique DN */
675   function create_unique_dn($attribute, $base)
676   {
677     $ldap= $this->config->get_ldap_link();
678     $base= preg_replace("/^,*/", "", $base);
680     /* Try to use plain entry first */
681     $dn= "$attribute=".$this->$attribute.",$base";
682     $ldap->cat ($dn, array('dn'));
683     if (!$ldap->fetch()){
684       return ($dn);
685     }
687     /* Look for additional attributes */
688     foreach ($this->attributes as $attr){
689       if ($attr == $attribute || $this->$attr == ""){
690         continue;
691       }
693       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
694       $ldap->cat ($dn, array('dn'));
695       if (!$ldap->fetch()){
696         return ($dn);
697       }
698     }
700     /* None found */
701     return ("none");
702   }
704   function rebind($ldap, $referral)
705   {
706     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
707     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
708       $this->error = "Success";
709       $this->hascon=true;
710       $this->reconnect= true;
711       return (0);
712     } else {
713       $this->error = "Could not bind to " . $credentials['ADMIN'];
714       return NULL;
715     }
716   }
718   /* This is a workaround function. */
719   function copy($src_dn, $dst_dn)
720   {
721     /* Rename dn in possible object groups */
722     $ldap= $this->config->get_ldap_link();
723     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
724         array('cn'));
725     while ($attrs= $ldap->fetch()){
726       $og= new ogroup($this->config, $ldap->getDN());
727       unset($og->member[$src_dn]);
728       $og->member[$dst_dn]= $dst_dn;
729       $og->save ();
730     }
732     $ldap->cat($dst_dn);
733     $attrs= $ldap->fetch();
734     if (count($attrs)){
735       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
736           E_USER_WARNING);
737       return (FALSE);
738     }
740     $ldap->cat($src_dn);
741     $attrs= $ldap->fetch();
742     if (!count($attrs)){
743       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
744           E_USER_WARNING);
745       return (FALSE);
746     }
748     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
749     $ds= ldap_connect($this->config->current['SERVER']);
750     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
751     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
752       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
753     }
755     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
756     error_reporting (0);
757     $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
759     /* Fill data from LDAP */
760     $new= array();
761     if ($sr) {
762       $ei=ldap_first_entry($ds, $sr);
763       if ($ei) {
764         foreach($attrs as $attr => $val){
765           if ($info = ldap_get_values_len($ds, $ei, $attr)){
766             for ($i= 0; $i<$info['count']; $i++){
767               if ($info['count'] == 1){
768                 $new[$attr]= $info[$i];
769               } else {
770                 $new[$attr][]= $info[$i];
771               }
772             }
773           }
774         }
775       }
776     }
778     /* close conncetion */
779     error_reporting (E_ALL);
780     ldap_unbind($ds);
782     /* Adapt naming attribute */
783     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
784     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
785     $new[$dst_name]= @LDAP::fix($dst_val);
787     /* Check if this is a department.
788      * If it is a dep. && there is a , override in his ou 
789      *  change \2C to , again, else this entry can't be saved ...
790      */
791     if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
792       $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
793     }
795     /* Save copy */
796     $ldap->connect();
797     $ldap->cd($this->config->current['BASE']);
798     
799     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
801     /* FAIvariable=.../..., cn=.. 
802         could not be saved, because the attribute FAIvariable was different to 
803         the dn FAIvariable=..., cn=... */
804     if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
805       $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
806     }
807     $ldap->cd($dst_dn);
808     $ldap->add($new);
810     if ($ldap->error != "Success"){
811       trigger_error("Trying to save $dst_dn failed.",
812           E_USER_WARNING);
813       return(FALSE);
814     }
816     return (TRUE);
817   }
820   function move($src_dn, $dst_dn)
821   {
822     /* Copy source to destination */
823     if (!$this->copy($src_dn, $dst_dn)){
824       return (FALSE);
825     }
827     /* Delete source */
828     $ldap= $this->config->get_ldap_link();
829     $ldap->rmdir($src_dn);
830     if ($ldap->error != "Success"){
831       trigger_error("Trying to delete $src_dn failed.",
832           E_USER_WARNING);
833       return (FALSE);
834     }
836     return (TRUE);
837   }
840   /* Move/Rename complete trees */
841   function recursive_move($src_dn, $dst_dn)
842   {
843     /* Check if the destination entry exists */
844     $ldap= $this->config->get_ldap_link();
846     /* Check if destination exists - abort */
847     $ldap->cat($dst_dn, array('dn'));
848     if ($ldap->fetch()){
849       trigger_error("recursive_move $dst_dn already exists.",
850           E_USER_WARNING);
851       return (FALSE);
852     }
854     /* Perform a search for all objects to be moved */
855     $objects= array();
856     $ldap->cd($src_dn);
857     $ldap->search("(objectClass=*)", array("dn"));
858     while($attrs= $ldap->fetch()){
859       $dn= $attrs['dn'];
860       $objects[$dn]= strlen($dn);
861     }
863     /* Sort objects by indent level */
864     asort($objects);
865     reset($objects);
867     /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
868     foreach ($objects as $object => $len){
869       $src= $object;
870       $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
871       if (!$this->copy($src, $dst)){
872         return (FALSE);
873       }
874     }
876     /* Remove src_dn */
877     $ldap->cd($src_dn);
878     $ldap->recursive_remove();
879     return (TRUE);
880   }
883   function handle_post_events($mode, $add_attrs= array())
884   {
885     switch ($mode){
886       case "add":
887         $this->postcreate($add_attrs);
888       break;
890       case "modify":
891         $this->postmodify($add_attrs);
892       break;
894       case "remove":
895         $this->postremove($add_attrs);
896       break;
897     }
898   }
901   function saveCopyDialog(){
902   }
905   function getCopyDialog(){
906     return(array("string"=>"","status"=>""));
907   }
910   function PrepareForCopyPaste($source){
911     $todo = $this->attributes;
912     if(isset($this->CopyPasteVars)){
913       $todo = array_merge($todo,$this->CopyPasteVars);
914     }
915     $todo[] = "is_account";
916     foreach($todo as $var){
917       if (isset($source->$var)){
918         $this->$var= $source->$var;
919       }
920     }
921   }
924   function handle_object_tagging($dn= "", $tag= "", $show= false)
925   {
926     //FIXME: How to optimize this? We have at least two
927     //       LDAP accesses per object. It would be a good
928     //       idea to have it integrated.
930     /* No dn? Self-operation... */
931     if ($dn == ""){
932       $dn= $this->dn;
934       /* No tag? Find it yourself... */
935       if ($tag == ""){
936         $len= strlen($dn);
938         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
939         $relevant= array();
940         foreach ($this->config->adepartments as $key => $ntag){
942           /* This one is bigger than our dn, its not relevant... */
943           if ($len <= strlen($key)){
944             continue;
945           }
947           /* This one matches with the latter part. Break and don't fix this entry */
948           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
949             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
950             $relevant[strlen($key)]= $ntag;
951             continue;
952           }
954         }
956         /* If we've some relevant tags to set, just get the longest one */
957         if (count($relevant)){
958           ksort($relevant);
959           $tmp= array_keys($relevant);
960           $idx= end($tmp);
961           $tag= $relevant[$idx];
962           $this->gosaUnitTag= $tag;
963         }
964       }
965     }
968     /* Set tag? */
969     if ($tag != ""){
970       /* Set objectclass and attribute */
971       $ldap= $this->config->get_ldap_link();
972       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
973       $attrs= $ldap->fetch();
974       if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
975         if ($show) {
976           echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
977           flush();
978         }
979         return;
980       }
981       if (count($attrs)){
982         if ($show){
983           echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
984           flush();
985         }
986         $nattrs= array("gosaUnitTag" => $tag);
987         $nattrs['objectClass']= array();
988         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
989           $oc= $attrs['objectClass'][$i];
990           if ($oc != "gosaAdministrativeUnitTag"){
991             $nattrs['objectClass'][]= $oc;
992           }
993         }
994         $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
995         $ldap->cd($dn);
996         $ldap->modify($nattrs);
997         show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
998       } else {
999         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
1000       }
1002     } else {
1003       /* Remove objectclass and attribute */
1004       $ldap= $this->config->get_ldap_link();
1005       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
1006       $attrs= $ldap->fetch();
1007       if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
1008         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
1009         return;
1010       }
1011       if (count($attrs)){
1012         if ($show){
1013           echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
1014           flush();
1015         }
1016         $nattrs= array("gosaUnitTag" => array());
1017         $nattrs['objectClass']= array();
1018         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
1019           $oc= $attrs['objectClass'][$i];
1020           if ($oc != "gosaAdministrativeUnitTag"){
1021             $nattrs['objectClass'][]= $oc;
1022           }
1023         }
1024         $ldap->cd($dn);
1025         $ldap->modify($nattrs);
1026         show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
1027       } else {
1028         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
1029       }
1030     }
1032   }
1035   /* Add possibility to stop remove process */
1036   function allow_remove()
1037   {
1038     $reason= "";
1039     return $reason;
1040   }
1043   /* Create a snapshot of the current object */
1044   function create_snapshot($type= "snapshot", $description= array())
1045   {
1047     /* Check if snapshot functionality is enabled */
1048     if(!$this->snapshotEnabled()){
1049       return;
1050     }
1052     /* Get configuration from gosa.conf */
1053     $tmp = $this->config->current;
1055     /* Create lokal ldap connection */
1056     $ldap= $this->config->get_ldap_link();
1057     $ldap->cd($this->config->current['BASE']);
1059     /* check if there are special server configurations for snapshots */
1060     if(!isset($tmp['SNAPSHOT_SERVER'])){
1062       /* Source and destination server are both the same, just copy source to dest obj */
1063       $ldap_to      = $ldap;
1064       $snapldapbase = $this->config->current['BASE'];
1066     }else{
1067       $server         = $tmp['SNAPSHOT_SERVER'];
1068       $user           = $tmp['SNAPSHOT_USER'];
1069       $password       = $tmp['SNAPSHOT_PASSWORD'];
1070       $snapldapbase   = $tmp['SNAPSHOT_LDAP_BASE'];
1072       $ldap_to        = new LDAP($user,$password, $server);
1073       $ldap_to -> cd($snapldapbase);
1074       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$snapldapbase));
1075     }
1077     /* check if the dn exists */ 
1078     if ($ldap->dn_exists($this->dn)){
1080       /* Extract seconds & mysecs, they are used as entry index */
1081       list($usec, $sec)= explode(" ", microtime());
1083       /* Collect some infos */
1084       $base           = $this->config->current['BASE'];
1085       $snap_base      = $tmp['SNAPSHOT_BASE'];
1086       $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1087       $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1089       /* Create object */
1090 #$data             = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1091       $data             = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1092       $newName          = preg_replace("/\./", "", $sec."-".$usec);
1093       $target= array();
1094       $target['objectClass']            = array("top", "gosaSnapshotObject");
1095       $target['gosaSnapshotData']       = gzcompress($data, 6);
1096       $target['gosaSnapshotType']       = $type;
1097       $target['gosaSnapshotDN']         = $this->dn;
1098       $target['description']            = $description;
1099       $target['gosaSnapshotTimestamp']  = $newName;
1101       /* Insert the new snapshot 
1102          But we have to check first, if the given gosaSnapshotTimestamp
1103          is already used, in this case we should increment this value till there is 
1104          an unused value. */ 
1105       $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1106       $ldap_to->cat($new_dn);
1107       while($ldap_to->count()){
1108         $ldap_to->cat($new_dn);
1109         $newName = preg_replace("/\./", "", $sec."-".($usec++));
1110         $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1111         $target['gosaSnapshotTimestamp']  = $newName;
1112       } 
1114       /* Inset this new snapshot */
1115       $ldap_to->cd($snapldapbase);
1116       $ldap_to->create_missing_trees($new_base);
1117       $ldap_to->cd($new_dn);
1118       $ldap_to->add($target);
1120       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1121       show_ldap_error($ldap_to->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1122     }
1123   }
1125   function remove_snapshot($dn)
1126   {
1127     $ui       = get_userinfo();
1128     $old_dn   = $this->dn; 
1129     $this->dn = $dn;
1130     $ldap = $this->config->get_ldap_link();
1131     $ldap->cd($this->config->current['BASE']);
1132     $ldap->rmdir_recursive($dn);
1133     $this->dn = $old_dn;
1134   }
1137   /* returns true if snapshots are enabled, and false if it is disalbed
1138      There will also be some errors psoted, if the configuration failed */
1139   function snapshotEnabled()
1140   {
1141     $tmp = $this->config->current;
1142     if(isset($tmp['ENABLE_SNAPSHOT'])){
1143       if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1145         /* Check if the snapshot_base is defined */
1146         if(!isset($tmp['SNAPSHOT_BASE'])){
1147           print_red(sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not configured in your gosa.conf."),$missing));
1148           return(FALSE);
1149         }
1151         /* check if there are special server configurations for snapshots */
1152         if(isset($tmp['SNAPSHOT_SERVER'])){
1154           /* check if all required vars are available to create a new ldap connection */
1155           $missing = "";
1156           foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_LDAP_BASE") as $var){
1157             if(!isset($tmp[$var])){
1158               $missing .= $var." ";
1159               print_red(sprintf(_("The snapshot functionality is enabled, but the required variable(s) '%s' is not configured in your gosa.conf."),$missing));
1160               return(FALSE);
1161             }
1162           }
1163         }
1164         return(TRUE);
1165       }
1166     }
1167     return(FALSE);
1168   }
1171   /* Return available snapshots for the given base 
1172    */
1173   function Available_SnapsShots($dn,$raw = false)
1174   {
1175     if(!$this->snapshotEnabled()) return(array());
1177     /* Create an additional ldap object which
1178        points to our ldap snapshot server */
1179     $ldap= $this->config->get_ldap_link();
1180     $ldap->cd($this->config->current['BASE']);
1181     $tmp = $this->config->current;
1183     /* check if there are special server configurations for snapshots */
1184     if(isset($tmp['SNAPSHOT_SERVER'])){
1185       $server       = $tmp['SNAPSHOT_SERVER'];
1186       $user         = $tmp['SNAPSHOT_USER'];
1187       $password     = $tmp['SNAPSHOT_PASSWORD'];
1188       $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1189       $ldap_to      = new LDAP($user,$password, $server);
1190       $ldap_to -> cd ($snapldapbase);
1191       show_ldap_error($ldap->get_error(), sprintf(_("Method get available snapshots with dn '%s' failed."),$this->dn));
1192     }else{
1193       $ldap_to    = $ldap;
1194     }
1196     /* Prepare bases and some other infos */
1197     $base           = $this->config->current['BASE'];
1198     $snap_base      = $tmp['SNAPSHOT_BASE'];
1199     $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1200     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1201     $tmp            = array(); 
1203     /* Fetch all objects with  gosaSnapshotDN=$dn */
1204     $ldap_to->cd($new_base);
1205     $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1206         array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description")); 
1208     /* Put results into a list and add description if missing */
1209     while($entry = $ldap_to->fetch()){ 
1210       if(!isset($entry['description'][0])){
1211         $entry['description'][0]  = "";
1212       }
1213       $tmp[] = $entry; 
1214     }
1216     /* Return the raw array, or format the result */
1217     if($raw){
1218       return($tmp);
1219     }else{  
1220       $tmp2 = array();
1221       foreach($tmp as $entry){
1222         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1223       }
1224     }
1225     return($tmp2);
1226   }
1229   function getAllDeletedSnapshots($base_of_object,$raw = false)
1230   {
1231     if(!$this->snapshotEnabled()) return(array());
1233     /* Create an additional ldap object which
1234        points to our ldap snapshot server */
1235     $ldap= $this->config->get_ldap_link();
1236     $ldap->cd($this->config->current['BASE']);
1237     $tmp = $this->config->current;
1239     /* check if there are special server configurations for snapshots */
1240     if(isset($tmp['SNAPSHOT_SERVER'])){
1241       $server       = $tmp['SNAPSHOT_SERVER'];
1242       $user         = $tmp['SNAPSHOT_USER'];
1243       $password     = $tmp['SNAPSHOT_PASSWORD'];
1244       $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1245       $ldap_to      = new LDAP($user,$password, $server);
1246       $ldap_to->cd ($snapldapbase);
1247       show_ldap_error($ldap->get_error(), sprintf(_("Method get deleted snapshots with dn '%s' failed."),$this->dn));
1248     }else{
1249       $ldap_to    = $ldap;
1250     }
1252     /* Prepare bases */ 
1253     $base           = $this->config->current['BASE'];
1254     $snap_base      = $tmp['SNAPSHOT_BASE'];
1255     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1257     /* Fetch all objects and check if they do not exist anymore */
1258     $ui = get_userinfo();
1259     $tmp = array();
1260     $ldap_to->cd($new_base);
1261     $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1262     while($entry = $ldap_to->fetch()){
1264       $chk =  str_replace($new_base,"",$entry['dn']);
1265       if(preg_match("/,ou=/",$chk)) continue;
1267       if(!isset($entry['description'][0])){
1268         $entry['description'][0]  = "";
1269       }
1270       $tmp[] = $entry; 
1271     }
1273     /* Check if entry still exists */
1274     foreach($tmp as $key => $entry){
1275       $ldap->cat($entry['gosaSnapshotDN'][0]);
1276       if($ldap->count()){
1277         unset($tmp[$key]);
1278       }
1279     }
1281     /* Format result as requested */
1282     if($raw) {
1283       return($tmp);
1284     }else{
1285       $tmp2 = array();
1286       foreach($tmp as $key => $entry){
1287         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1288       }
1289     }
1290     return($tmp2);
1291   } 
1294   /* Restore selected snapshot */
1295   function restore_snapshot($dn)
1296   {
1297     if(!$this->snapshotEnabled()) return(array());
1299     $ldap= $this->config->get_ldap_link();
1300     $ldap->cd($this->config->current['BASE']);
1301     $tmp = $this->config->current;
1303     /* check if there are special server configurations for snapshots */
1304     if(isset($tmp['SNAPSHOT_SERVER'])){
1305       $server       = $tmp['SNAPSHOT_SERVER'];
1306       $user         = $tmp['SNAPSHOT_USER'];
1307       $password     = $tmp['SNAPSHOT_PASSWORD'];
1308       $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1309       $ldap_to      = new LDAP($user,$password, $server);
1310       $ldap_to->cd ($snapldapbase);
1311       show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$snapldapbase));
1312     }else{
1313       $ldap_to    = $ldap;
1314     }
1316     /* Get the snapshot */ 
1317     $ldap_to->cat($dn);
1318     $restoreObject = $ldap_to->fetch();
1320     /* Prepare import string */
1321     $data  = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1323     /* Import the given data */
1324     $ldap->import_complete_ldif($data,$err,false,false);
1325     show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$dn));
1326   }
1329   function showSnapshotDialog($base,$baseSuffixe)
1330   {
1331     $once = true;
1332     foreach($_POST as $name => $value){
1334       /* Create a new snapshot, display a dialog */
1335       if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1336         $once = false;
1337         $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1338         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1339         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1340       }
1342       /* Restore a snapshot, display a dialog with all snapshots of the current object */
1343       if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1344         $once = false;
1345         $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1346         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1347         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1348         $this->snapDialog->display_restore_dialog = true;
1349       }
1351       /* Restore one of the already deleted objects */
1352       if(preg_match("/^RestoreDeletedSnapShot_/",$name) && $once){
1353         $once = false;
1354         $this->snapDialog = new SnapShotDialog($this->config,"",$this);
1355         $this->snapDialog->set_snapshot_bases($baseSuffixe);
1356         $this->snapDialog->display_restore_dialog      = true;
1357         $this->snapDialog->display_all_removed_objects  = true;
1358       }
1360       /* Restore selected snapshot */
1361       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1362         $once = false;
1363         $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1364         $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1365         if(!empty($entry)){
1366           $this->restore_snapshot($entry);
1367           $this->snapDialog = NULL;
1368         }
1369       }
1370     }
1372     /* Create a new snapshot requested, check
1373        the given attributes and create the snapshot*/
1374     if(isset($_POST['CreateSnapshot'])){
1375       $this->snapDialog->save_object();
1376       $msgs = $this->snapDialog->check();
1377       if(count($msgs)){
1378         foreach($msgs as $msg){
1379           print_red($msg);
1380         }
1381       }else{
1382         $this->dn =  $this->snapDialog->dn;
1383         $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1384         $this->snapDialog = NULL;
1385       }
1386     }
1388     /* Restore is requested, restore the object with the posted dn .*/
1389     if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1390     }
1392     if(isset($_POST['CancelSnapshot'])){
1393       $this->snapDialog = NULL;
1394     }
1396     if($this->snapDialog){
1397       $this->snapDialog->save_object();
1398       return($this->snapDialog->execute());
1399     }
1400   }
1403   function plInfo()
1404   {
1405     return array();
1406   }
1409   function set_acl_base($base)
1410   {
1411     $this->acl_base= $base;
1412   }
1415   function set_acl_category($category)
1416   {
1417     $this->acl_category= "$category/";
1418   }
1421   function acl_is_writeable($attribute,$skip_write = FALSE)
1422   {
1423     $ui= get_userinfo();
1424     return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1425   }
1428   function acl_is_readable($attribute)
1429   {
1430     $ui= get_userinfo();
1431     return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1432   }
1435   function acl_is_createable()
1436   {
1437     $ui= get_userinfo();
1438     return preg_match('/c/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1439   }
1442   function acl_is_removeable()
1443   {
1444     $ui= get_userinfo();
1445     return preg_match('/d/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1446   }
1449   function acl_is_moveable()
1450   {
1451     $ui= get_userinfo();
1452     return preg_match('/m/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1453   }
1456   function acl_have_any_permissions()
1457   {
1458   }
1461   function getacl($attribute,$skip_write= FALSE)
1462   {
1463     $ui= get_userinfo();
1464     return  $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1465   }
1467   /* Get all allowed bases to move an object to or to create a new object.
1468      Idepartments also contains all base departments which lead to the allowed bases */
1469   function get_allowed_bases($category = "")
1470   {
1471     $ui = get_userinfo();
1472     $deps = array();
1474     /* Set category */ 
1475     if(empty($category)){
1476       $category = $this->acl_category.get_class($this);
1477     }
1479     /* Is this a new object ? Or just an edited existing object */
1480     if(!$this->initially_was_account && $this->is_account){
1481       $new = true;
1482     }else{
1483       $new = false;
1484     }
1486     $cat_bases = $ui->get_module_departments(preg_replace("/\/.*$/","",$category));
1487     foreach($this->config->idepartments as $dn => $name){
1488       
1489       if(!in_array_ics($dn,$cat_bases)){
1490         continue;
1491       }
1492       
1493       $acl = $ui->get_permissions($dn,$category);
1494       if($new && preg_match("/c/",$acl)){
1495         $deps[$dn] = $name;
1496       }elseif(!$new && preg_match("/m/",$acl)){
1497         $deps[$dn] = $name;
1498       }
1499     }
1501     /* Add current base */      
1502     if(isset($this->base) && isset($this->config->idepartments[$this->base])){
1503       $deps[$this->base] = $this->config->idepartments[$this->base];
1504     }else{
1505       echo "No default base found. ".$this->base."<br> ";
1506     }
1508     return($deps);
1509   }
1511   /* This function modifies object acls too, if an object is moved.
1512    *  $old_dn   specifies the actually used dn
1513    *  $new_dn   specifies the destiantion dn
1514    */
1515   function update_acls($old_dn,$new_dn,$output_changes = FALSE)
1516   {
1517     global $config;
1519     /* Check if old_dn is empty. This should never happen */
1520     if(empty($old_dn) || empty($new_dn)){
1521       trigger_error("Failed to check acl dependencies, wrong dn given.");
1522       return;
1523     }
1525     /* Update userinfo if necessary */
1526     if($_SESSION['ui']->dn == $old_dn){
1527       $_SESSION['ui']->dn = $new_dn;
1528       new log("view","acl/".get_class($this),$this->dn,array(),"Updated current user dn from '".$old_dn."' to '".$new_dn."'");
1529     }
1531     /* Object was moved, ensure that all acls will be moved too */
1532     if($new_dn != $old_dn && $old_dn != "new"){
1534       /* get_ldap configuration */
1535       $update = array();
1536       $ldap = $config->get_ldap_link();
1537       $ldap->cd ($config->current['BASE']);
1538       $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*))",array("cn","gosaAclEntry"));
1539       while($attrs = $ldap->fetch()){
1541         $acls = array();
1543         /* Walk through acls */
1544         for($i = 0 ; $i <  $attrs['gosaAclEntry']['count'] ; $i ++ ){
1546           /* Reset vars */
1547           $found = false;
1549           /* Get Acl parts */
1550           $acl_parts = split(":",$attrs['gosaAclEntry'][$i]);
1552           /* Get every single member for this acl */  
1553           $members = array();  
1554           if(preg_match("/,/",$acl_parts[2])){
1555             $members = split(",",$acl_parts[2]);
1556           }else{
1557             $members = array($acl_parts[2]);
1558           } 
1559       
1560           /* Check if member match current dn */
1561           foreach($members as $key => $member){
1562             $member = base64_decode($member);
1563             if($member == $old_dn){
1564               $found = true;
1565               $members[$key] = base64_encode($new_dn);
1566             }
1567           } 
1568          
1569           /* Create new member string */ 
1570           $new_members = "";
1571           foreach($members as $member){
1572             $new_members .= $member.",";
1573           }
1574           $new_members = preg_replace("/,$/","",$new_members);
1575           $acl_parts[2] = $new_members;
1576         
1577           /* Reconstruckt acl entry */
1578           $acl_str  ="";
1579           foreach($acl_parts as $t){
1580            $acl_str .= $t.":";
1581           }
1582           $acl_str = preg_replace("/:$/","",$acl_str);
1583        }
1585        /* Acls for this object must be adjusted */
1586        if($found){
1588           if($output_changes){
1589             echo "<font color='green'>".
1590                   _("Changing ACL dn")."&nbsp;:&nbsp;<br>&nbsp;-"._("from")."&nbsp;<b>&nbsp;".
1591                   $old_dn.
1592                   "</b><br>&nbsp;-"._("to")."&nbsp;<b>".
1593                   $new_dn.
1594                   "</b></font><br>";
1595           }
1596           $update[$attrs['dn']] =array();
1597           foreach($acls as $acl){
1598             $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1599           }
1600         }
1601       }
1603       /* Write updated acls */
1604       foreach($update as $dn => $attrs){
1605         $ldap->cd($dn);
1606         $ldap->modify($attrs);
1607       }
1608     }
1609   }
1611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1612 ?>