Code

Updated gosaSupportDaemon.
[gosa.git] / gosa-core / 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= "";
91   var $skipTagging= FALSE;
93   /*!
94     \brief Used standard values
96     dn
97    */
98   var $dn= "";
99   var $uid= "";
100   var $sn= "";
101   var $givenName= "";
102   var $acl= "*none*";
103   var $dialog= FALSE;
104   var $snapDialog = NULL;
106   /* attribute list for save action */
107   var $attributes= array();
108   var $objectclasses= array();
109   var $is_new= TRUE;
110   var $saved_attributes= array();
112   var $acl_base= "";
113   var $acl_category= "";
115   /* This can be set to render the tabulators in another stylesheet */
116   var $pl_notify= FALSE;
118   /* Object entry CSN */
119   var $entryCSN         = "";
120   var $CSN_check_active = FALSE;
122   /* This variable indicates that this class can handle multiple dns at once. */
123   var $multiple_support = FALSE;
124   var $multi_attrs      = array();
125   var $multi_attrs_all  = array(); 
127   /* This aviable indicates, that we are currently in multiple edit handle */
128   var $multiple_support_active = FALSE; 
129   var $selected_edit_values = array();
130   var $multi_boxes = array();
132   /*! \brief plugin constructor
134     If 'dn' is set, the node loads the given 'dn' from LDAP
136     \param dn Distinguished name to initialize plugin from
137     \sa plugin()
138    */
139   function plugin (&$config, $dn= NULL, $parent= NULL)
140   {
141     /* Configuration is fine, allways */
142     $this->config= &$config;    
143     $this->dn= $dn;
145     /* Handle new accounts, don't read information from LDAP */
146     if ($dn == "new"){
147       return;
148     }
150     /* Save current dn as acl_base */
151     $this->acl_base= $dn;
153     /* Get LDAP descriptor */
154     $ldap= $this->config->get_ldap_link();
155     if ($dn !== NULL){
157       /* Load data to 'attrs' and save 'dn' */
158       if ($parent !== NULL){
159         $this->attrs= $parent->attrs;
160       } else {
161         $ldap->cat ($dn);
162         $this->attrs= $ldap->fetch();
163       }
165       /* Copy needed attributes */
166       foreach ($this->attributes as $val){
167         $found= array_key_ics($val, $this->attrs);
168         if ($found != ""){
169           $this->$val= $this->attrs["$found"][0];
170         }
171       }
173       /* gosaUnitTag loading... */
174       if (isset($this->attrs['gosaUnitTag'][0])){
175         $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
176       }
178       /* Set the template flag according to the existence of objectClass
179          gosaUserTemplate */
180       if (isset($this->attrs['objectClass'])){
181         if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
182           $this->is_template= TRUE;
183           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
184               "found", "Template check");
185         }
186       }
188       /* Is Account? */
189       $found= TRUE;
190       foreach ($this->objectclasses as $obj){
191         if (preg_match('/top/i', $obj)){
192           continue;
193         }
194         if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
195           $found= FALSE;
196           break;
197         }
198       }
199       if ($found){
200         $this->is_account= TRUE;
201         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
202             "found", "Object check");
203       }
205       /* Prepare saved attributes */
206       $this->saved_attributes= $this->attrs;
207       foreach ($this->saved_attributes as $index => $value){
208         if (preg_match('/^[0-9]+$/', $index)){
209           unset($this->saved_attributes[$index]);
210           continue;
211         }
212         if (!in_array($index, $this->attributes) && $index != "objectClass"){
213           unset($this->saved_attributes[$index]);
214           continue;
215         }
216         if (isset($this->saved_attributes[$index][0]) || $this->saved_attributes[$index]["count"] == 1){
217           $tmp= $this->saved_attributes[$index][0];
218           unset($this->saved_attributes[$index]);
219           $this->saved_attributes[$index]= $tmp;
220           continue;
221         }
223         unset($this->saved_attributes["$index"]["count"]);
224       }
225     }
227     /* Save initial account state */
228     $this->initially_was_account= $this->is_account;
229   }
232   /*! \brief execute plugin
234     Generates the html output for this node
235    */
236   function execute()
237   {
238     /* This one is empty currently. Fabian - please fill in the docu code */
239     session::set('current_class_for_help',get_class($this));
241     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
242     session::set('LOCK_VARS_TO_USE',array());
243     session::set('LOCK_VARS_USED',array());
244   }
246   /*! \brief execute plugin
247      Removes object from parent
248    */
249   function remove_from_parent()
250   {
251     /* include global link_info */
252     $ldap= $this->config->get_ldap_link();
254     /* Get current objectClasses in order to add the required ones */
255     $ldap->cat($this->dn);
256     $tmp= $ldap->fetch ();
257     $oc= array();
258     if (isset($tmp['objectClass'])){
259       $oc= $tmp['objectClass'];
260       unset($oc['count']);
261     }
263     /* Remove objectClasses from entry */
264     $ldap->cd($this->dn);
265     $this->attrs= array();
266     $this->attrs['objectClass']= array_remove_entries($this->objectclasses,$oc);
268     /* Unset attributes from entry */
269     foreach ($this->attributes as $val){
270       $this->attrs["$val"]= array();
271     }
273     /* Unset account info */
274     $this->is_account= FALSE;
276     /* Do not write in plugin base class, this must be done by
277        children, since there are normally additional attribs,
278        lists, etc. */
279     /*
280        $ldap->modify($this->attrs);
281      */
282   }
285   /*! \brief   Save HTML posted data to object 
286    */
287   function save_object()
288   {
289     /* Update entry CSN if it is empty. */
290     if(empty($this->entryCSN) && $this->CSN_check_active){
291       $this->entryCSN = getEntryCSN($this->dn);
292     }
294     /* Save values to object */
295     foreach ($this->attributes as $val){
296       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
297         /* Check for modifications */
298         if (get_magic_quotes_gpc()) {
299           $data= stripcslashes($_POST["$val"]);
300         } else {
301           $data= $this->$val = $_POST["$val"];
302         }
303         if ($this->$val != $data){
304           $this->is_modified= TRUE;
305         }
306     
307         /* Okay, how can I explain this fix ... 
308          * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds. 
309          * So IE posts these 'unselectable' option, with value = chr(194) 
310          * chr(194) seems to be the &nbsp; in between the ...option>&nbsp;</option.. because there is no value=".." specified in these option fields  
311          * This &nbsp; was added for W3c compliance, but now causes these ... ldap errors ... 
312          * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
313          */
314         if(isset($data[0]) && $data[0] == chr(194)) {
315           $data = "";  
316         }
317         $this->$val= $data;
318       }
319     }
320   }
323   /* Save data to LDAP, depending on is_account we save or delete */
324   function save()
325   {
326     /* include global link_info */
327     $ldap= $this->config->get_ldap_link();
329     /* Save all plugins */
330     $this->entryCSN = "";
332     /* Start with empty array */
333     $this->attrs= array();
335     /* Get current objectClasses in order to add the required ones */
336     $ldap->cat($this->dn);
337     
338     $tmp= $ldap->fetch ();
340     $oc= array();
341     if (isset($tmp['objectClass'])){
342       $oc= $tmp["objectClass"];
343       $this->is_new= FALSE;
344       unset($oc['count']);
345     } else {
346       $this->is_new= TRUE;
347     }
349     /* Load (minimum) attributes, add missing ones */
350     $this->attrs['objectClass']= gosa_array_merge($oc,$this->objectclasses);
352     /* Copy standard attributes */
353     foreach ($this->attributes as $val){
354       if ($this->$val != ""){
355         $this->attrs["$val"]= $this->$val;
356       } elseif (!$this->is_new) {
357         $this->attrs["$val"]= array();
358       }
359     }
361     /* Handle tagging */
362     $this->tag_attrs($this->attrs);
363   }
366   function cleanup()
367   {
368     foreach ($this->attrs as $index => $value){
370       /* Convert arrays with one element to non arrays, if the saved
371          attributes are no array, too */
372       if (is_array($this->attrs[$index]) && 
373           count ($this->attrs[$index]) == 1 &&
374           isset($this->saved_attributes[$index]) &&
375           !is_array($this->saved_attributes[$index])){
376           
377         $tmp= $this->attrs[$index][0];
378         $this->attrs[$index]= $tmp;
379       }
381       /* Remove emtpy arrays if they do not differ */
382       if (is_array($this->attrs[$index]) &&
383           count($this->attrs[$index]) == 0 &&
384           !isset($this->saved_attributes[$index])){
385           
386         unset ($this->attrs[$index]);
387         continue;
388       }
390       /* Remove single attributes that do not differ */
391       if (!is_array($this->attrs[$index]) &&
392           isset($this->saved_attributes[$index]) &&
393           !is_array($this->saved_attributes[$index]) &&
394           $this->attrs[$index] == $this->saved_attributes[$index]){
396         unset ($this->attrs[$index]);
397         continue;
398       }
400       /* Remove arrays that do not differ */
401       if (is_array($this->attrs[$index]) && 
402           isset($this->saved_attributes[$index]) &&
403           is_array($this->saved_attributes[$index])){
404           
405         if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
406           unset ($this->attrs[$index]);
407           continue;
408         }
409       }
410     }
412     /* Update saved attributes and ensure that next cleanups will be successful too */
413     foreach($this->attrs as $name => $value){
414       $this->saved_attributes[$name] = $value;
415     }
416   }
418   /* Check formular input */
419   function check()
420   {
421     $message= array();
423     /* Skip if we've no config object */
424     if (!isset($this->config) || !is_object($this->config)){
425       return $message;
426     }
428     /* Find hooks entries for this class */
429     $command= $this->config->search(get_class($this), "CHECK", array('menu', 'tabs'));
431     if ($command != ""){
433       if (!check_command($command)){
434         $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
435                             get_class($this));
436       } else {
438         /* Generate "ldif" for check hook */
439         $ldif= "dn: $this->dn\n";
440         
441         /* ... objectClasses */
442         foreach ($this->objectclasses as $oc){
443           $ldif.= "objectClass: $oc\n";
444         }
445         
446         /* ... attributes */
447         foreach ($this->attributes as $attr){
448           if ($this->$attr == ""){
449             continue;
450           }
451           if (is_array($this->$attr)){
452             foreach ($this->$attr as $val){
453               $ldif.= "$attr: $val\n";
454             }
455           } else {
456               $ldif.= "$attr: ".$this->$attr."\n";
457           }
458         }
460         /* Append empty line */
461         $ldif.= "\n";
463         /* Feed "ldif" into hook and retrieve result*/
464         $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
465         $fh= proc_open($command, $descriptorspec, $pipes);
466         if (is_resource($fh)) {
467           fwrite ($pipes[0], $ldif);
468           fclose($pipes[0]);
469           
470           $result= stream_get_contents($pipes[1]);
471           if ($result != ""){
472             $message[]= $result;
473           }
474           
475           fclose($pipes[1]);
476           fclose($pipes[2]);
477           proc_close($fh);
478         }
479       }
481     }
483     /* Check entryCSN */
484     if($this->CSN_check_active){
485       $current_csn = getEntryCSN($this->dn);
486       if($current_csn != $this->entryCSN && !empty($this->entryCSN) && !empty($current_csn)){
487         $this->entryCSN = $current_csn;
488         $message[] = _("The object has changed since opened in GOsa. Please ensure that nobody has done serious changes that may get lost   if you save this entry.");
489       }
490     }
491     return ($message);
492   }
494   /* Adapt from template, using 'dn' */
495   function adapt_from_template($dn)
496   {
497     /* Include global link_info */
498     $ldap= $this->config->get_ldap_link();
500     /* Load requested 'dn' to 'attrs' */
501     $ldap->cat ($dn);
502     $this->attrs= $ldap->fetch();
504     /* Walk through attributes */
505     foreach ($this->attributes as $val){
507       if (isset($this->attrs["$val"][0])){
509         /* If attribute is set, replace dynamic parts: 
510            %sn, %givenName and %uid. Fill these in our local variables. */
511         $value= $this->attrs["$val"][0];
513         foreach (array("sn", "givenName", "uid") as $repl){
514           if (preg_match("/%$repl/i", $value)){
515             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
516           }
517         }
518         $this->$val= $value;
519       }
520     }
522     /* Is Account? */
523     $found= TRUE;
524     foreach ($this->objectclasses as $obj){
525       if (preg_match('/top/i', $obj)){
526         continue;
527       }
528       if (!in_array_ics ($obj, $this->attrs['objectClass'])){
529         $found= FALSE;
530         break;
531       }
532     }
533     if ($found){
534       $this->is_account= TRUE;
535     }
536   }
538   /* Indicate whether a password change is needed or not */
539   function password_change_needed()
540   {
541     return FALSE;
542   }
545   /* Show header message for tab dialogs */
546   function show_enable_header($button_text, $text, $disabled= FALSE)
547   {
548     if (($disabled == TRUE) || (!$this->acl_is_createable())){
549       $state= "disabled";
550     } else {
551       $state= "";
552     }
553     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
554     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
555       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
557     return($display);
558   }
561   /* Show header message for tab dialogs */
562   function show_disable_header($button_text, $text, $disabled= FALSE)
563   {
564     if (($disabled == TRUE) || !$this->acl_is_removeable()){
565       $state= "disabled";
566     } else {
567       $state= "";
568     }
569     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
570     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
571       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
573     return($display);
574   }
577   /* Show header message for tab dialogs */
578   function show_header($button_text, $text, $disabled= FALSE)
579   {
580     echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
581     if ($disabled == TRUE){
582       $state= "disabled";
583     } else {
584       $state= "";
585     }
586     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
587     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
588       ($this->acl_is_createable()?'':'disabled')." ".$state.
589       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
591     return($display);
592   }
595   function postcreate($add_attrs= array())
596   {
597     /* Find postcreate entries for this class */
598     $command= $this->config->search(get_class($this), "POSTCREATE",array('menu', 'tabs'));
600     if ($command != ""){
602       /* Additional attributes */
603       foreach ($add_attrs as $name => $value){
604         $command= preg_replace("/%$name/", $value, $command);
605       }
607       /* Walk through attribute list */
608       foreach ($this->attributes as $attr){
609         if (!is_array($this->$attr)){
610           $command= preg_replace("/%$attr/", $this->$attr, $command);
611         }
612       }
613       $command= preg_replace("/%dn/", $this->dn, $command);
615       if (check_command($command)){
616         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
617             $command, "Execute");
619         exec($command);
620       } else {
621         $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
622         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
623       }
624     }
625   }
627   function postmodify($add_attrs= array())
628   {
629     /* Find postcreate entries for this class */
630     $command= $this->config->search(get_class($this), "POSTMODIFY",array('menu','tabs'));
632     if ($command != ""){
634       /* Additional attributes */
635       foreach ($add_attrs as $name => $value){
636         $command= preg_replace("/%$name/", $value, $command);
637       }
639       /* Walk through attribute list */
640       foreach ($this->attributes as $attr){
641         if (!is_array($this->$attr)){
642           $command= preg_replace("/%$attr/", $this->$attr, $command);
643         }
644       }
645       $command= preg_replace("/%dn/", $this->dn, $command);
647       if (check_command($command)){
648         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
649             $command, "Execute");
651         exec($command);
652       } else {
653         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
654         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
655       }
656     }
657   }
659   function postremove($add_attrs= array())
660   {
661     /* Find postremove entries for this class */
662     $command= $this->config->search(get_class($this), "POSTREMOVE",array('menu','tabs'));
663     if ($command != ""){
665       /* Additional attributes */
666       foreach ($add_attrs as $name => $value){
667         $command= preg_replace("/%$name/", $value, $command);
668       }
670       /* Walk through attribute list */
671       foreach ($this->attributes as $attr){
672         if (!is_array($this->$attr)){
673           $command= preg_replace("/%$attr/", $this->$attr, $command);
674         }
675       }
676       $command= preg_replace("/%dn/", $this->dn, $command);
678       /* Additional attributes */
679       foreach ($add_attrs as $name => $value){
680         $command= preg_replace("/%$name/", $value, $command);
681       }
683       if (check_command($command)){
684         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
685             $command, "Execute");
687         exec($command);
688       } else {
689         $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
690         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
691       }
692     }
693   }
695   /* Create unique DN */
696   function create_unique_dn($attribute, $base)
697   {
698     $ldap= $this->config->get_ldap_link();
699     $base= preg_replace("/^,*/", "", $base);
701     /* Try to use plain entry first */
702     $dn= "$attribute=".$this->$attribute.",$base";
703     $ldap->cat ($dn, array('dn'));
704     if (!$ldap->fetch()){
705       return ($dn);
706     }
708     /* Look for additional attributes */
709     foreach ($this->attributes as $attr){
710       if ($attr == $attribute || $this->$attr == ""){
711         continue;
712       }
714       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
715       $ldap->cat ($dn, array('dn'));
716       if (!$ldap->fetch()){
717         return ($dn);
718       }
719     }
721     /* None found */
722     return ("none");
723   }
725   function rebind($ldap, $referral)
726   {
727     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
728     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
729       $this->error = "Success";
730       $this->hascon=true;
731       $this->reconnect= true;
732       return (0);
733     } else {
734       $this->error = "Could not bind to " . $credentials['ADMIN'];
735       return NULL;
736     }
737   }
740   /* Recursively copy ldap object */
741   function _copy($src_dn,$dst_dn)
742   {
743     $ldap=$this->config->get_ldap_link();
744     $ldap->cat($src_dn);
745     $attrs= $ldap->fetch();
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']);
795     
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     }
812     return(TRUE);
813   }
816   /* This is a workaround function. */
817   function copy($src_dn, $dst_dn)
818   {
819     /* Rename dn in possible object groups */
820     $ldap= $this->config->get_ldap_link();
821     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::prepare4filter($src_dn).'))',
822         array('cn'));
823     while ($attrs= $ldap->fetch()){
824       $og= new ogroup($this->config, $ldap->getDN());
825       unset($og->member[$src_dn]);
826       $og->member[$dst_dn]= $dst_dn;
827       $og->save ();
828     }
830     $ldap->cat($dst_dn);
831     $attrs= $ldap->fetch();
832     if (count($attrs)){
833       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
834           E_USER_WARNING);
835       return (FALSE);
836     }
838     $ldap->cat($src_dn);
839     $attrs= $ldap->fetch();
840     if (!count($attrs)){
841       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
842           E_USER_WARNING);
843       return (FALSE);
844     }
846     $ldap->cd($src_dn);
847     $ldap->search("objectClass=*",array("dn"));
848     while($attrs = $ldap->fetch()){
849       $src = $attrs['dn'];
850       $dst = preg_replace("/".normalizePreg($src_dn)."$/",$dst_dn,$attrs['dn']);
851       $this->_copy($src,$dst);
852     }
853     return (TRUE);
854   }
857   function move($src_dn, $dst_dn)
858   {
859     /* Do not copy if only upper- lowercase has changed */
860     if(strtolower($src_dn) == strtolower($dst_dn)){
861       return(TRUE);
862     }
864     /* Copy source to destination */
865     if (!$this->copy($src_dn, $dst_dn)){
866       return (FALSE);
867     }
869     /* Delete source */
870     $ldap= $this->config->get_ldap_link();
871     $ldap->rmdir_recursive($src_dn);
872     if ($ldap->error != "Success"){
873       trigger_error("Trying to delete $src_dn failed.",
874           E_USER_WARNING);
875       return (FALSE);
876     }
878     return (TRUE);
879   }
882   /* Move/Rename complete trees */
883   function recursive_move($src_dn, $dst_dn)
884   {
885     /* Check if the destination entry exists */
886     $ldap= $this->config->get_ldap_link();
888     /* Check if destination exists - abort */
889     $ldap->cat($dst_dn, array('dn'));
890     if ($ldap->fetch()){
891       trigger_error("recursive_move $dst_dn already exists.",
892           E_USER_WARNING);
893       return (FALSE);
894     }
896     $this->copy($src_dn, $dst_dn);
898     /* Remove src_dn */
899     $ldap->cd($src_dn);
900     $ldap->recursive_remove($src_dn);
901     return (TRUE);
902   }
905   function handle_post_events($mode, $add_attrs= array())
906   {
907     switch ($mode){
908       case "add":
909         $this->postcreate($add_attrs);
910       break;
912       case "modify":
913         $this->postmodify($add_attrs);
914       break;
916       case "remove":
917         $this->postremove($add_attrs);
918       break;
919     }
920   }
923   function saveCopyDialog(){
924   }
927   function getCopyDialog(){
928     return(array("string"=>"","status"=>""));
929   }
932   function PrepareForCopyPaste($source)
933   {
934     $todo = $this->attributes;
935     if(isset($this->CopyPasteVars)){
936       $todo = array_merge($todo,$this->CopyPasteVars);
937     }
939     if(count($this->objectclasses)){
940       $this->is_account = TRUE;
941       foreach($this->objectclasses as $class){
942         if(!in_array($class,$source['objectClass'])){
943           $this->is_account = FALSE;
944         }
945       }
946     }
948     foreach($todo as $var){
949       if (isset($source[$var])){
950         if(isset($source[$var]['count'])){
951           if($source[$var]['count'] > 1){
952             $this->$var = array();
953             $tmp = array();
954             for($i = 0 ; $i < $source[$var]['count']; $i++){
955               $tmp = $source[$var][$i];
956             }
957             $this->$var = $tmp;
958           }else{
959             $this->$var = $source[$var][0];
960           }
961         }else{
962           $this->$var= $source[$var];
963         }
964       }
965     }
966   }
968   function tag_attrs(&$at, $dn= "", $tag= "", $show= false)
969   {
970     /* Skip tagging? 
971        If this is called from departmentGeneric, we have to skip this
972         tagging procedure. 
973      */
974     if($this->skipTagging){
975       return;
976     }
978     /* No dn? Self-operation... */
979     if ($dn == ""){
980       $dn= $this->dn;
982       /* No tag? Find it yourself... */
983       if ($tag == ""){
984         $len= strlen($dn);
986         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
987         $relevant= array();
988         foreach ($this->config->adepartments as $key => $ntag){
990           /* This one is bigger than our dn, its not relevant... */
991           if ($len <= strlen($key)){
992             continue;
993           }
995           /* This one matches with the latter part. Break and don't fix this entry */
996           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
997             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
998             $relevant[strlen($key)]= $ntag;
999             continue;
1000           }
1002         }
1004         /* If we've some relevant tags to set, just get the longest one */
1005         if (count($relevant)){
1006           ksort($relevant);
1007           $tmp= array_keys($relevant);
1008           $idx= end($tmp);
1009           $tag= $relevant[$idx];
1010           $this->gosaUnitTag= $tag;
1011         }
1012       }
1013     }
1015     /* Remove tags that may already be here... */
1016     remove_objectClass("gosaAdministrativeUnitTag", $at);
1017     if (isset($at['gosaUnitTag'])){
1018         unset($at['gosaUnitTag']);
1019     }
1021     /* Set tag? */
1022     if ($tag != ""){
1023       add_objectClass("gosaAdministrativeUnitTag", $at);
1024       $at['gosaUnitTag']= $tag;
1025     }
1026   }
1029   /* Add possibility to stop remove process */
1030   function allow_remove()
1031   {
1032     $reason= "";
1033     return $reason;
1034   }
1037   /* Create a snapshot of the current object */
1038   function create_snapshot($type= "snapshot", $description= array())
1039   {
1041     /* Check if snapshot functionality is enabled */
1042     if(!$this->snapshotEnabled()){
1043       return;
1044     }
1046     /* Get configuration from gosa.conf */
1047     $tmp = $this->config->current;
1049     /* Create lokal ldap connection */
1050     $ldap= $this->config->get_ldap_link();
1051     $ldap->cd($this->config->current['BASE']);
1053     /* check if there are special server configurations for snapshots */
1054     if(!isset($tmp['SNAPSHOT_SERVER'])){
1056       /* Source and destination server are both the same, just copy source to dest obj */
1057       $ldap_to      = $ldap;
1058       $snapldapbase = $this->config->current['BASE'];
1060     }else{
1061       $server         = $tmp['SNAPSHOT_SERVER'];
1062       $user           = $tmp['SNAPSHOT_USER'];
1063       $password       = $tmp['SNAPSHOT_PASSWORD'];
1064       $snapldapbase   = $tmp['SNAPSHOT_BASE'];
1066       $ldap_to        = new LDAP($user,$password, $server);
1067       $ldap_to -> cd($snapldapbase);
1068       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$snapldapbase));
1069     }
1071     /* check if the dn exists */ 
1072     if ($ldap->dn_exists($this->dn)){
1074       /* Extract seconds & mysecs, they are used as entry index */
1075       list($usec, $sec)= explode(" ", microtime());
1077       /* Collect some infos */
1078       $base           = $this->config->current['BASE'];
1079       $snap_base      = $tmp['SNAPSHOT_BASE'];
1080       $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1081       $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1083       /* Create object */
1084 #$data             = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1085       $data             = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1086       $newName          = preg_replace("/\./", "", $sec."-".$usec);
1087       $target= array();
1088       $target['objectClass']            = array("top", "gosaSnapshotObject");
1089       $target['gosaSnapshotData']       = gzcompress($data, 6);
1090       $target['gosaSnapshotType']       = $type;
1091       $target['gosaSnapshotDN']         = $this->dn;
1092       $target['description']            = $description;
1093       $target['gosaSnapshotTimestamp']  = $newName;
1095       /* Insert the new snapshot 
1096          But we have to check first, if the given gosaSnapshotTimestamp
1097          is already used, in this case we should increment this value till there is 
1098          an unused value. */ 
1099       $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1100       $ldap_to->cat($new_dn);
1101       while($ldap_to->count()){
1102         $ldap_to->cat($new_dn);
1103         $newName = preg_replace("/\./", "", $sec."-".($usec++));
1104         $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1105         $target['gosaSnapshotTimestamp']  = $newName;
1106       } 
1108       /* Inset this new snapshot */
1109       $ldap_to->cd($snapldapbase);
1110       $ldap_to->create_missing_trees($snapldapbase);
1111       $ldap_to->create_missing_trees($new_base);
1112       $ldap_to->cd($new_dn);
1113       $ldap_to->add($target);
1114     
1115       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1116       show_ldap_error($ldap_to->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1117     }
1118   }
1120   function remove_snapshot($dn)
1121   {
1122     $ui       = get_userinfo();
1123     $old_dn   = $this->dn; 
1124     $this->dn = $dn;
1125     $ldap = $this->config->get_ldap_link();
1126     $ldap->cd($this->config->current['BASE']);
1127     $ldap->rmdir_recursive($dn);
1128     $this->dn = $old_dn;
1129   }
1132   /* returns true if snapshots are enabled, and false if it is disalbed
1133      There will also be some errors psoted, if the configuration failed */
1134   function snapshotEnabled()
1135   {
1136     $tmp = $this->config->current;
1137     if(isset($tmp['ENABLE_SNAPSHOT'])){
1138       if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1140         /* Check if the snapshot_base is defined */
1141         if(!isset($tmp['SNAPSHOT_BASE'])){
1142           msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),"SNAPSHOT_BASE"), ERROR_DIALOG);
1143           return(FALSE);
1144         }
1146         /* check if there are special server configurations for snapshots */
1147         if(isset($tmp['SNAPSHOT_SERVER'])){
1149           /* check if all required vars are available to create a new ldap connection */
1150           $missing = "";
1151           foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_BASE") as $var){
1152             if(!isset($tmp[$var])){
1153               $missing .= $var." ";
1154               msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."), $missing), ERROR_DIALOG);
1155               return(FALSE);
1156             }
1157           }
1158         }
1159         return(TRUE);
1160       }
1161     }
1162     return(FALSE);
1163   }
1166   /* Return available snapshots for the given base 
1167    */
1168   function Available_SnapsShots($dn,$raw = false)
1169   {
1170     if(!$this->snapshotEnabled()) return(array());
1172     /* Create an additional ldap object which
1173        points to our ldap snapshot server */
1174     $ldap= $this->config->get_ldap_link();
1175     $ldap->cd($this->config->current['BASE']);
1176     $cfg= &$this->config->current;
1178     /* check if there are special server configurations for snapshots */
1180     if(isset($cfg['SERVER']) && isset($cfg['SNAPSHOT_SERVER']) && $cfg['SERVER'] == $cfg['SNAPSHOT_SERVER']){
1181       $ldap_to    = $ldap;
1182     }elseif(isset($cfg['SNAPSHOT_SERVER'])){
1183       $server       = $cfg['SNAPSHOT_SERVER'];
1184       $user         = $cfg['SNAPSHOT_USER'];
1185       $password     = $cfg['SNAPSHOT_PASSWORD'];
1186       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1188       $ldap_to      = new LDAP($user,$password, $server);
1189       $ldap_to -> cd ($snapldapbase);
1190       show_ldap_error($ldap->get_error(), sprintf(_("Method get available snapshots with dn '%s' failed."),$this->dn));
1191     }else{
1192       $ldap_to    = $ldap;
1193     }
1195     /* Prepare bases and some other infos */
1196     $base           = $this->config->current['BASE'];
1197     $snap_base      = $cfg['SNAPSHOT_BASE'];
1198     $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1199     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1200     $tmp            = array(); 
1202     /* Fetch all objects with  gosaSnapshotDN=$dn */
1203     $ldap_to->cd($new_base);
1204     $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1205         array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description")); 
1207     /* Put results into a list and add description if missing */
1208     while($entry = $ldap_to->fetch()){ 
1209       if(!isset($entry['description'][0])){
1210         $entry['description'][0]  = "";
1211       }
1212       $tmp[] = $entry; 
1213     }
1215     /* Return the raw array, or format the result */
1216     if($raw){
1217       return($tmp);
1218     }else{  
1219       $tmp2 = array();
1220       foreach($tmp as $entry){
1221         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1222       }
1223     }
1224     return($tmp2);
1225   }
1228   function getAllDeletedSnapshots($base_of_object,$raw = false)
1229   {
1230     if(!$this->snapshotEnabled()) return(array());
1232     /* Create an additional ldap object which
1233        points to our ldap snapshot server */
1234     $ldap= $this->config->get_ldap_link();
1235     $ldap->cd($this->config->current['BASE']);
1236     $cfg= &$this->config->current;
1238     /* check if there are special server configurations for snapshots */
1239     if(isset($cfg['SNAPSHOT_SERVER'])){
1240       $server       = $cfg['SNAPSHOT_SERVER'];
1241       $user         = $cfg['SNAPSHOT_USER'];
1242       $password     = $cfg['SNAPSHOT_PASSWORD'];
1243       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1244       $ldap_to      = new LDAP($user,$password, $server);
1245       $ldap_to->cd ($snapldapbase);
1246       show_ldap_error($ldap_to->get_error(), sprintf(_("Method get deleted snapshots with dn '%s' failed."),$this->dn));
1247     }else{
1248       $ldap_to    = $ldap;
1249     }
1251     /* Prepare bases */ 
1252     $base           = $this->config->current['BASE'];
1253     $snap_base      = $cfg['SNAPSHOT_BASE'];
1254     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1256     /* Fetch all objects and check if they do not exist anymore */
1257     $ui = get_userinfo();
1258     $tmp = array();
1259     $ldap_to->cd($new_base);
1260     $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1261     while($entry = $ldap_to->fetch()){
1263       $chk =  str_replace($new_base,"",$entry['dn']);
1264       if(preg_match("/,ou=/",$chk)) continue;
1266       if(!isset($entry['description'][0])){
1267         $entry['description'][0]  = "";
1268       }
1269       $tmp[] = $entry; 
1270     }
1272     /* Check if entry still exists */
1273     foreach($tmp as $key => $entry){
1274       $ldap->cat($entry['gosaSnapshotDN'][0]);
1275       if($ldap->count()){
1276         unset($tmp[$key]);
1277       }
1278     }
1280     /* Format result as requested */
1281     if($raw) {
1282       return($tmp);
1283     }else{
1284       $tmp2 = array();
1285       foreach($tmp as $key => $entry){
1286         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1287       }
1288     }
1289     return($tmp2);
1290   } 
1293   /* Restore selected snapshot */
1294   function restore_snapshot($dn)
1295   {
1296     if(!$this->snapshotEnabled()) return(array());
1298     $ldap= $this->config->get_ldap_link();
1299     $ldap->cd($this->config->current['BASE']);
1300     $cfg= &$this->config->current;
1302     /* check if there are special server configurations for snapshots */
1303     if(isset($cfg['SNAPSHOT_SERVER'])){
1304       $server       = $cfg['SNAPSHOT_SERVER'];
1305       $user         = $cfg['SNAPSHOT_USER'];
1306       $password     = $cfg['SNAPSHOT_PASSWORD'];
1307       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1308       $ldap_to      = new LDAP($user,$password, $server);
1309       $ldap_to->cd ($snapldapbase);
1310       show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$snapldapbase));
1311     }else{
1312       $ldap_to    = $ldap;
1313     }
1315     /* Get the snapshot */ 
1316     $ldap_to->cat($dn);
1317     $restoreObject = $ldap_to->fetch();
1319     /* Prepare import string */
1320     $data  = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1322     /* Import the given data */
1323     $ldap->import_complete_ldif($data,$err,false,false);
1324     show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$dn));
1325   }
1328   function showSnapshotDialog($base,$baseSuffixe)
1329   {
1330     $once = true;
1331     foreach($_POST as $name => $value){
1333       /* Create a new snapshot, display a dialog */
1334       if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1335         $once = false;
1336         $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1337         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1338         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1339       }
1341       /* Restore a snapshot, display a dialog with all snapshots of the current object */
1342       if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1343         $once = false;
1344         $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1345         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1346         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1347         $this->snapDialog->display_restore_dialog = true;
1348       }
1350       /* Restore one of the already deleted objects */
1351       if(((isset($_POST['menu_action']) && $_POST['menu_action'] == "RestoreDeletedSnapShot") 
1352           || 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']) && is_object($this->snapDialog)){
1375       $this->snapDialog->save_object();
1376       $msgs = $this->snapDialog->check();
1377       if(count($msgs)){
1378         foreach($msgs as $msg){
1379           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
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(is_object($this->snapDialog )){
1397       $this->snapDialog->save_object();
1398       return($this->snapDialog->execute());
1399     }
1400   }
1403   static 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       trigger_error("No default base found in class ".get_class($this).". ".$this->base);
1506     }
1507     return($deps);
1508   }
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     /* Check if old_dn is empty. This should never happen */
1518     if(empty($old_dn) || empty($new_dn)){
1519       trigger_error("Failed to check acl dependencies, wrong dn given.");
1520       return;
1521     }
1523     /* Update userinfo if necessary */
1524     $ui = session::get('ui');
1525     if($ui->dn == $old_dn){
1526       $ui->dn = $new_dn;
1527       session::set('ui',$ui);
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 = $this->config->get_ldap_link();
1537       $ldap->cd ($this->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           $debug_info=  _("Changing ACL dn")."&nbsp;:&nbsp;<br>&nbsp;-"._("from")."&nbsp;<b>&nbsp;".
1589                   $old_dn."</b><br>&nbsp;-"._("to")."&nbsp;<b>".$new_dn."</b><br>";
1590           @DEBUG (DEBUG_ACL, __LINE__, __FUNCTION__, __FILE__,$debug_info,"ACL");
1592           $update[$attrs['dn']] =array();
1593           foreach($acls as $acl){
1594             $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1595           }
1596         }
1597       }
1599       /* Write updated acls */
1600       foreach($update as $dn => $attrs){
1601         $ldap->cd($dn);
1602         $ldap->modify($attrs);
1603       }
1604     }
1605   }
1607   
1609   /* This function enables the entry Serial ID check.
1610    * If an entry was edited while we have edited the entry too,
1611    *  an error message will be shown. 
1612    * To configure this check correctly read the FAQ.
1613    */    
1614   function enable_CSN_check()
1615   {
1616     $this->CSN_check_active =TRUE;
1617     $this->entryCSN = getEntryCSN($this->dn);
1618   }
1621   /*! \brief  Prepares the plugin to be used for multiple edit
1622    *          Update plugin attributes with given array of attribtues.
1623    *  @param  array   Array with attributes that must be updated.
1624    */
1625   function init_multiple_support($attrs,$all)
1626   {
1627     $ldap= $this->config->get_ldap_link();
1628     $this->multi_attrs    = $attrs;
1629     $this->multi_attrs_all= $all;
1631     /* Copy needed attributes */
1632     foreach ($this->attributes as $val){
1633       $found= array_key_ics($val, $this->multi_attrs);
1634       if ($found != ""){
1635         if(isset($this->multi_attrs["$found"][0])){
1636           $this->$val= $this->multi_attrs["$found"][0];
1637         }
1638       }
1639     }
1640   }
1642  
1643   /*! \brief  Enables multiple support for this plugin
1644    */
1645   function enable_multiple_support()
1646   {
1647     $this->ignore_account = TRUE;
1648     $this->multiple_support_active = TRUE;
1649   }
1652   /*! \brief  Returns all values that have been modfied in multiple edit mode.
1653       @return array Cotaining all mdofied values. 
1654    */
1655   function get_multi_edit_values()
1656   {
1657     $ret = array();
1658     foreach($this->attributes as $attr){
1659       if(in_array($attr,$this->multi_boxes)){
1660         $ret[$attr] = $this->$attr;
1661       }
1662     }
1663     return($ret);
1664   }
1666   
1667   /*! \brief  Update class variables with values collected by multiple edit.
1668    */
1669   function set_multi_edit_values($attrs)
1670   {
1671     foreach($attrs as $name => $value){
1672       $this->$name = $value;
1673     }
1674   }
1677   /*! \brief execute plugin
1679     Generates the html output for this node
1680    */
1681   function multiple_execute()
1682   {
1683     /* This one is empty currently. Fabian - please fill in the docu code */
1684     session::set('current_class_for_help',get_class($this));
1686     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
1687     session::set('LOCK_VARS_TO_USE',array());
1688     session::set('LOCK_VARS_USED',array());
1689     
1690     return("Multiple edit is currently not implemented for this plugin.");
1691   }
1694   /*! \brief   Save HTML posted data to object for multiple edit
1695    */
1696   function multiple_save_object()
1697   {
1698     if(empty($this->entryCSN) && $this->CSN_check_active){
1699       $this->entryCSN = getEntryCSN($this->dn);
1700     }
1702     /* Save values to object */
1703     $this->multi_boxes = array();
1704     foreach ($this->attributes as $val){
1705   
1706       /* Get selected checkboxes from multiple edit */
1707       if(isset($_POST["use_".$val])){
1708         $this->multi_boxes[] = $val;
1709       }
1711       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
1713         /* Check for modifications */
1714         if (get_magic_quotes_gpc()) {
1715           $data= stripcslashes($_POST["$val"]);
1716         } else {
1717           $data= $this->$val = $_POST["$val"];
1718         }
1719         if ($this->$val != $data){
1720           $this->is_modified= TRUE;
1721         }
1722     
1723         /* IE post fix */
1724         if(isset($data[0]) && $data[0] == chr(194)) {
1725           $data = "";  
1726         }
1727         $this->$val= $data;
1728       }
1729     }
1730   }
1733   /*! \brief  Returns all attributes of this plugin, 
1734                to be able to detect multiple used attributes 
1735                in multi_plugg::detect_multiple_used_attributes().
1736       @return array Attributes required for intialization of multi_plug
1737    */
1738   public function get_multi_init_values()
1739   {
1740     $attrs = $this->attrs;
1741     return($attrs);
1742   }
1745   /*! \brief  Check given values in multiple edit
1746       @return array Error messages
1747    */
1748   function multiple_check()
1749   {
1750     $message = plugin::check();
1751     return($message);
1752   }
1755 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1756 ?>