Code

Updated msgPool
[gosa.git] / gosa-core / include / class_plugin.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 /*! \brief   The plugin base class
24   \author  Cajus Pollmeier <pollmeier@gonicus.de>
25   \version 2.00
26   \date    24.07.2003
28   This is the base class for all plugins. It can be used standalone or
29   can be included by the tabs class. All management should be done 
30   within this class. Extend your plugins from this class.
31  */
33 class plugin
34 {
35   /*!
36     \brief Reference to parent object
38     This variable is used when the plugin is included in tabs
39     and keeps reference to the tab class. Communication to other
40     tabs is possible by 'name'. So the 'fax' plugin can ask the
41     'userinfo' plugin for the fax number.
43     \sa tab
44    */
45   var $parent= NULL;
47   /*!
48     \brief Configuration container
50     Access to global configuration
51    */
52   var $config= NULL;
54   /*!
55     \brief Mark plugin as account
57     Defines whether this plugin is defined as an account or not.
58     This has consequences for the plugin to be saved from tab
59     mode. If it is set to 'FALSE' the tab will call the delete
60     function, else the save function. Should be set to 'TRUE' if
61     the construtor detects a valid LDAP object.
63     \sa plugin::plugin()
64    */
65   var $is_account= FALSE;
66   var $initially_was_account= FALSE;
68   /*!
69     \brief Mark plugin as template
71     Defines whether we are creating a template or a normal object.
72     Has conseqences on the way execute() shows the formular and how
73     save() puts the data to LDAP.
75     \sa plugin::save() plugin::execute()
76    */
77   var $is_template= FALSE;
78   var $ignore_account= FALSE;
79   var $is_modified= FALSE;
81   /*!
82     \brief Represent temporary LDAP data
84     This is only used internally.
85    */
86   var $attrs= array();
88   /* Keep set of conflicting plugins */
89   var $conflicts= array();
91   /* Save unit tags */
92   var $gosaUnitTag= "";
93   var $skipTagging= FALSE;
95   /*!
96     \brief Used standard values
98     dn
99    */
100   var $dn= "";
101   var $uid= "";
102   var $sn= "";
103   var $givenName= "";
104   var $acl= "*none*";
105   var $dialog= FALSE;
106   var $snapDialog = NULL;
108   /* attribute list for save action */
109   var $attributes= array();
110   var $objectclasses= array();
111   var $is_new= TRUE;
112   var $saved_attributes= array();
114   var $acl_base= "";
115   var $acl_category= "";
117   /* This can be set to render the tabulators in another stylesheet */
118   var $pl_notify= FALSE;
120   /* Object entry CSN */
121   var $entryCSN         = "";
122   var $CSN_check_active = FALSE;
124   /* This variable indicates that this class can handle multiple dns at once. */
125   var $multiple_support = FALSE;
126   var $multi_attrs      = array();
127   var $multi_attrs_all  = array(); 
129   /* This aviable indicates, that we are currently in multiple edit handle */
130   var $multiple_support_active = FALSE; 
131   var $selected_edit_values = array();
132   var $multi_boxes = array();
134   /*! \brief plugin constructor
136     If 'dn' is set, the node loads the given 'dn' from LDAP
138     \param dn Distinguished name to initialize plugin from
139     \sa plugin()
140    */
141   function plugin (&$config, $dn= NULL, $parent= NULL)
142   {
143     /* Configuration is fine, allways */
144     $this->config= &$config;    
145     $this->dn= $dn;
147     /* Handle new accounts, don't read information from LDAP */
148     if ($dn == "new"){
149       return;
150     }
152     /* Save current dn as acl_base */
153     $this->acl_base= $dn;
155     /* Get LDAP descriptor */
156     if ($dn !== NULL){
158       /* Load data to 'attrs' and save 'dn' */
159       if ($parent !== NULL){
160         $this->attrs= $parent->attrs;
161       } else {
162         $ldap= $this->config->get_ldap_link();
163         $ldap->cat ($dn);
164         $this->attrs= $ldap->fetch();
165       }
167       /* Copy needed attributes */
168       foreach ($this->attributes as $val){
169         $found= array_key_ics($val, $this->attrs);
170         if ($found != ""){
171           $this->$val= $this->attrs["$found"][0];
172         }
173       }
175       /* gosaUnitTag loading... */
176       if (isset($this->attrs['gosaUnitTag'][0])){
177         $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
178       }
180       /* Set the template flag according to the existence of objectClass
181          gosaUserTemplate */
182       if (isset($this->attrs['objectClass'])){
183         if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
184           $this->is_template= TRUE;
185           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
186               "found", "Template check");
187         }
188       }
190       /* Is Account? */
191       $found= TRUE;
192       foreach ($this->objectclasses as $obj){
193         if (preg_match('/top/i', $obj)){
194           continue;
195         }
196         if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
197           $found= FALSE;
198           break;
199         }
200       }
201       if ($found){
202         $this->is_account= TRUE;
203         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
204             "found", "Object check");
205       }
207       /* Prepare saved attributes */
208       $this->saved_attributes= $this->attrs;
209       foreach ($this->saved_attributes as $index => $value){
210         if (preg_match('/^[0-9]+$/', $index)){
211           unset($this->saved_attributes[$index]);
212           continue;
213         }
214         if (!in_array($index, $this->attributes) && $index != "objectClass"){
215           unset($this->saved_attributes[$index]);
216           continue;
217         }
218         if (isset($this->saved_attributes[$index][0]) && $this->saved_attributes[$index]["count"] == 1){
219           $tmp= $this->saved_attributes[$index][0];
220           unset($this->saved_attributes[$index]);
221           $this->saved_attributes[$index]= $tmp;
222           continue;
223         }
225         unset($this->saved_attributes["$index"]["count"]);
226       }
227       if(isset($this->attrs['gosaUnitTag'])){
228         $this->saved_attributes['gosaUnitTag'] = $this->attrs['gosaUnitTag'][0];
229       }
230     }
232     /* Save initial account state */
233     $this->initially_was_account= $this->is_account;
234   }
237   /*! \brief execute plugin
239     Generates the html output for this node
240    */
241   function execute()
242   {
243     /* This one is empty currently. Fabian - please fill in the docu code */
244     session::set('current_class_for_help',get_class($this));
246     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
247     session::set('LOCK_VARS_TO_USE',array());
248     session::set('LOCK_VARS_USED',array());
249   }
251   /*! \brief execute plugin
252      Removes object from parent
253    */
254   function remove_from_parent()
255   {
256     /* include global link_info */
257     $ldap= $this->config->get_ldap_link();
259     /* Get current objectClasses in order to add the required ones */
260     $ldap->cat($this->dn);
261     $tmp= $ldap->fetch ();
262     $oc= array();
263     if (isset($tmp['objectClass'])){
264       $oc= $tmp['objectClass'];
265       unset($oc['count']);
266     }
268     /* Remove objectClasses from entry */
269     $ldap->cd($this->dn);
270     $this->attrs= array();
271     $this->attrs['objectClass']= array_remove_entries($this->objectclasses,$oc);
273     /* Unset attributes from entry */
274     foreach ($this->attributes as $val){
275       $this->attrs["$val"]= array();
276     }
278     /* Unset account info */
279     $this->is_account= FALSE;
281     /* Do not write in plugin base class, this must be done by
282        children, since there are normally additional attribs,
283        lists, etc. */
284     /*
285        $ldap->modify($this->attrs);
286      */
287   }
290   /*! \brief   Save HTML posted data to object 
291    */
292   function save_object()
293   {
294     /* Update entry CSN if it is empty. */
295     if(empty($this->entryCSN) && $this->CSN_check_active){
296       $this->entryCSN = getEntryCSN($this->dn);
297     }
299     /* Save values to object */
300     foreach ($this->attributes as $val){
301       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
302         /* Check for modifications */
303         if (get_magic_quotes_gpc()) {
304           $data= stripcslashes($_POST["$val"]);
305         } else {
306           $data= $this->$val = $_POST["$val"];
307         }
308         if ($this->$val != $data){
309           $this->is_modified= TRUE;
310         }
311     
312         /* Okay, how can I explain this fix ... 
313          * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds. 
314          * So IE posts these 'unselectable' option, with value = chr(194) 
315          * chr(194) seems to be the &nbsp; in between the ...option>&nbsp;</option.. because there is no value=".." specified in these option fields  
316          * This &nbsp; was added for W3c compliance, but now causes these ... ldap errors ... 
317          * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
318          */
319         if(isset($data[0]) && $data[0] == chr(194)) {
320           $data = "";  
321         }
322         $this->$val= $data;
323       }
324     }
325   }
328   /* Save data to LDAP, depending on is_account we save or delete */
329   function save()
330   {
331     /* include global link_info */
332     $ldap= $this->config->get_ldap_link();
334     /* Save all plugins */
335     $this->entryCSN = "";
337     /* Start with empty array */
338     $this->attrs= array();
340     /* Get current objectClasses in order to add the required ones */
341     $ldap->cat($this->dn);
342     
343     $tmp= $ldap->fetch ();
345     $oc= array();
346     if (isset($tmp['objectClass'])){
347       $oc= $tmp["objectClass"];
348       $this->is_new= FALSE;
349       unset($oc['count']);
350     } else {
351       $this->is_new= TRUE;
352     }
354     /* Load (minimum) attributes, add missing ones */
355     $this->attrs['objectClass']= gosa_array_merge($oc,$this->objectclasses);
357     /* Copy standard attributes */
358     foreach ($this->attributes as $val){
359       if ($this->$val != ""){
360         $this->attrs["$val"]= $this->$val;
361       } elseif (!$this->is_new) {
362         $this->attrs["$val"]= array();
363       }
364     }
366     /* Handle tagging */
367     $this->tag_attrs($this->attrs);
368   }
371   function cleanup()
372   {
373     foreach ($this->attrs as $index => $value){
374       
375       /* Convert arrays with one element to non arrays, if the saved
376          attributes are no array, too */
377       if (is_array($this->attrs[$index]) && 
378           count ($this->attrs[$index]) == 1 &&
379           isset($this->saved_attributes[$index]) &&
380           !is_array($this->saved_attributes[$index])){
381           
382         $tmp= $this->attrs[$index][0];
383         $this->attrs[$index]= $tmp;
384       }
386       /* Remove emtpy arrays if they do not differ */
387       if (is_array($this->attrs[$index]) &&
388           count($this->attrs[$index]) == 0 &&
389           !isset($this->saved_attributes[$index])){
390           
391         unset ($this->attrs[$index]);
392         continue;
393       }
395       /* Remove single attributes that do not differ */
396       if (!is_array($this->attrs[$index]) &&
397           isset($this->saved_attributes[$index]) &&
398           !is_array($this->saved_attributes[$index]) &&
399           $this->attrs[$index] == $this->saved_attributes[$index]){
401         unset ($this->attrs[$index]);
402         continue;
403       }
405       /* Remove arrays that do not differ */
406       if (is_array($this->attrs[$index]) && 
407           isset($this->saved_attributes[$index]) &&
408           is_array($this->saved_attributes[$index])){
409           
410         if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
411           unset ($this->attrs[$index]);
412           continue;
413         }
414       }
415     }
417     /* Update saved attributes and ensure that next cleanups will be successful too */
418     foreach($this->attrs as $name => $value){
419       $this->saved_attributes[$name] = $value;
420     }
421   }
423   /* Check formular input */
424   function check()
425   {
426     $message= array();
428     /* Skip if we've no config object */
429     if (!isset($this->config) || !is_object($this->config)){
430       return $message;
431     }
433     /* Find hooks entries for this class */
434     $command= $this->config->search(get_class($this), "CHECK", array('menu', 'tabs'));
436     if ($command != ""){
438       if (!check_command($command)){
439         $message[]= msgPool::cmdnotfound("CHECK", get_class($this));
440       } else {
442         /* Generate "ldif" for check hook */
443         $ldif= "dn: $this->dn\n";
444         
445         /* ... objectClasses */
446         foreach ($this->objectclasses as $oc){
447           $ldif.= "objectClass: $oc\n";
448         }
449         
450         /* ... attributes */
451         foreach ($this->attributes as $attr){
452           if ($this->$attr == ""){
453             continue;
454           }
455           if (is_array($this->$attr)){
456             foreach ($this->$attr as $val){
457               $ldif.= "$attr: $val\n";
458             }
459           } else {
460               $ldif.= "$attr: ".$this->$attr."\n";
461           }
462         }
464         /* Append empty line */
465         $ldif.= "\n";
467         /* Feed "ldif" into hook and retrieve result*/
468         $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
469         $fh= proc_open($command, $descriptorspec, $pipes);
470         if (is_resource($fh)) {
471           fwrite ($pipes[0], $ldif);
472           fclose($pipes[0]);
473           
474           $result= stream_get_contents($pipes[1]);
475           if ($result != ""){
476             $message[]= $result;
477           }
478           
479           fclose($pipes[1]);
480           fclose($pipes[2]);
481           proc_close($fh);
482         }
483       }
485     }
487     /* Check entryCSN */
488     if($this->CSN_check_active){
489       $current_csn = getEntryCSN($this->dn);
490       if($current_csn != $this->entryCSN && !empty($this->entryCSN) && !empty($current_csn)){
491         $this->entryCSN = $current_csn;
492         $message[] = _("The object has changed since opened in GOsa. All changes that may be done by others get lost if you save this entry!");
493       }
494     }
495     return ($message);
496   }
498   /* Adapt from template, using 'dn' */
499   function adapt_from_template($dn, $skip= array())
500   {
501     /* Include global link_info */
502     $ldap= $this->config->get_ldap_link();
504     /* Load requested 'dn' to 'attrs' */
505     $ldap->cat ($dn);
506     $this->attrs= $ldap->fetch();
508     /* Walk through attributes */
509     foreach ($this->attributes as $val){
511       /* Skip the ones in skip list */
512       if (in_array($val, $skip)){
513         continue;
514       }
516       if (isset($this->attrs["$val"][0])){
518         /* If attribute is set, replace dynamic parts: 
519            %sn, %givenName and %uid. Fill these in our local variables. */
520         $value= $this->attrs["$val"][0];
522         foreach (array("sn", "givenName", "uid") as $repl){
523           if (preg_match("/%$repl/i", $value)){
524             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
525           }
526         }
527         $this->$val= $value;
528       }
529     }
531     /* Is Account? */
532     $found= TRUE;
533     foreach ($this->objectclasses as $obj){
534       if (preg_match('/top/i', $obj)){
535         continue;
536       }
537       if (!in_array_ics ($obj, $this->attrs['objectClass'])){
538         $found= FALSE;
539         break;
540       }
541     }
542     if ($found){
543       $this->is_account= TRUE;
544     }
545   }
547   /* Indicate whether a password change is needed or not */
548   function password_change_needed()
549   {
550     return FALSE;
551   }
554   /* Show header message for tab dialogs */
555   function show_enable_header($button_text, $text, $disabled= FALSE)
556   {
557     if (($disabled == TRUE) || (!$this->acl_is_createable())){
558       $state= "disabled";
559     } else {
560       $state= "";
561     }
562     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
563     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
564       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
566     return($display);
567   }
570   /* Show header message for tab dialogs */
571   function show_disable_header($button_text, $text, $disabled= FALSE)
572   {
573     if (($disabled == TRUE) || !$this->acl_is_removeable()){
574       $state= "disabled";
575     } else {
576       $state= "";
577     }
578     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
579     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
580       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
582     return($display);
583   }
586   /* Show header message for tab dialogs */
587   function show_header($button_text, $text, $disabled= FALSE)
588   {
589     echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
590     if ($disabled == TRUE){
591       $state= "disabled";
592     } else {
593       $state= "";
594     }
595     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
596     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
597       ($this->acl_is_createable()?'':'disabled')." ".$state.
598       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
600     return($display);
601   }
604   function postcreate($add_attrs= array())
605   {
606     /* Find postcreate entries for this class */
607     $command= $this->config->search(get_class($this), "POSTCREATE",array('menu', 'tabs'));
609     if ($command != ""){
611       /* Additional attributes */
612       foreach ($add_attrs as $name => $value){
613         $command= preg_replace("/%$name( |$)/", "$value ", $command);
614       }
616       /* Walk through attribute list */
617       foreach ($this->attributes as $attr){
618         if (!is_array($this->$attr)){
619           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
620         }
621       }
622       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
624       if (check_command($command)){
625         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
626             $command, "Execute");
628         exec($command);
629       } else {
630         $message[]= msgPool::cmdnotfound("POSTCREATE", get_class($this));
631         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
632       }
633     }
634   }
636   function postmodify($add_attrs= array())
637   {
638     /* Find postcreate entries for this class */
639     $command= $this->config->search(get_class($this), "POSTMODIFY",array('menu','tabs'));
641     if ($command != ""){
643       /* Additional attributes */
644       foreach ($add_attrs as $name => $value){
645         $command= preg_replace("/%$name( |$)/", "$value ", $command);
646       }
648       /* Walk through attribute list */
649       foreach ($this->attributes as $attr){
650         if (!is_array($this->$attr)){
651           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
652         }
653       }
654       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
656       if (check_command($command)){
657         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
658             $command, "Execute");
660         exec($command);
661       } else {
662         $message[]= msgPool::cmdnotfound("POSTMODIFY", get_class($this));
663         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
664       }
665     }
666   }
668   function postremove($add_attrs= array())
669   {
670     /* Find postremove entries for this class */
671     $command= $this->config->search(get_class($this), "POSTREMOVE",array('menu','tabs'));
672     if ($command != ""){
674       /* Additional attributes */
675       foreach ($add_attrs as $name => $value){
676         $command= preg_replace("/%$name( |$)/", "$value ", $command);
677       }
679       /* Walk through attribute list */
680       foreach ($this->attributes as $attr){
681         if (!is_array($this->$attr)){
682           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
683         }
684       }
685       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
687       if (check_command($command)){
688         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
689             $command, "Execute");
691         exec($command);
692       } else {
693         $message[]= msgPool::cmdnotfound("POSTREMOVE", get_class($this));
694         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
695       }
696     }
697   }
699   /* Create unique DN */
700   function create_unique_dn($attribute, $base)
701   {
702     $ldap= $this->config->get_ldap_link();
703     $base= preg_replace("/^,*/", "", $base);
705     /* Try to use plain entry first */
706     $dn= "$attribute=".$this->$attribute.",$base";
707     $ldap->cat ($dn, array('dn'));
708     if (!$ldap->fetch()){
709       return ($dn);
710     }
712     /* Look for additional attributes */
713     foreach ($this->attributes as $attr){
714       if ($attr == $attribute || $this->$attr == ""){
715         continue;
716       }
718       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
719       $ldap->cat ($dn, array('dn'));
720       if (!$ldap->fetch()){
721         return ($dn);
722       }
723     }
725     /* None found */
726     return ("none");
727   }
729   function rebind($ldap, $referral)
730   {
731     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
732     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
733       $this->error = "Success";
734       $this->hascon=true;
735       $this->reconnect= true;
736       return (0);
737     } else {
738       $this->error = "Could not bind to " . $credentials['ADMIN'];
739       return NULL;
740     }
741   }
744   /* Recursively copy ldap object */
745   function _copy($src_dn,$dst_dn)
746   {
747     $ldap=$this->config->get_ldap_link();
748     $ldap->cat($src_dn);
749     $attrs= $ldap->fetch();
751     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
752     $ds= ldap_connect($this->config->current['SERVER']);
753     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
754     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
755       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
756     }
758     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
759     $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
761     /* Fill data from LDAP */
762     $new= array();
763     if ($sr) {
764       $ei=ldap_first_entry($ds, $sr);
765       if ($ei) {
766         foreach($attrs as $attr => $val){
767           if ($info = @ldap_get_values_len($ds, $ei, $attr)){
768             for ($i= 0; $i<$info['count']; $i++){
769               if ($info['count'] == 1){
770                 $new[$attr]= $info[$i];
771               } else {
772                 $new[$attr][]= $info[$i];
773               }
774             }
775           }
776         }
777       }
778     }
780     /* close conncetion */
781     ldap_unbind($ds);
783     /* Adapt naming attribute */
784     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
785     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
786     $new[$dst_name]= @LDAP::fix($dst_val);
788     /* Check if this is a department.
789      * If it is a dep. && there is a , override in his ou 
790      *  change \2C to , again, else this entry can't be saved ...
791      */
792     if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
793       $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
794     }
796     /* Save copy */
797     $ldap->connect();
798     $ldap->cd($this->config->current['BASE']);
799     
800     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
802     /* FAIvariable=.../..., cn=.. 
803         could not be saved, because the attribute FAIvariable was different to 
804         the dn FAIvariable=..., cn=... */
805     if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
806       $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
807     }
808     $ldap->cd($dst_dn);
809     $ldap->add($new);
811     if (!$ldap->success()){
812       trigger_error("Trying to save $dst_dn failed.",
813           E_USER_WARNING);
814       return(FALSE);
815     }
816     return(TRUE);
817   }
820   /* This is a workaround function. */
821   function copy($src_dn, $dst_dn)
822   {
823     /* Rename dn in possible object groups */
824     $ldap= $this->config->get_ldap_link();
825     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::prepare4filter($src_dn).'))',
826         array('cn'));
827     while ($attrs= $ldap->fetch()){
828       $og= new ogroup($this->config, $ldap->getDN());
829       unset($og->member[$src_dn]);
830       $og->member[$dst_dn]= $dst_dn;
831       $og->save ();
832     }
834     $ldap->cat($dst_dn);
835     $attrs= $ldap->fetch();
836     if (count($attrs)){
837       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
838           E_USER_WARNING);
839       return (FALSE);
840     }
842     $ldap->cat($src_dn);
843     $attrs= $ldap->fetch();
844     if (!count($attrs)){
845       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
846           E_USER_WARNING);
847       return (FALSE);
848     }
850     $ldap->cd($src_dn);
851     $ldap->search("objectClass=*",array("dn"));
852     while($attrs = $ldap->fetch()){
853       $src = $attrs['dn'];
854       $dst = preg_replace("/".normalizePreg($src_dn)."$/",$dst_dn,$attrs['dn']);
855       $this->_copy($src,$dst);
856     }
857     return (TRUE);
858   }
861   function move($src_dn, $dst_dn)
862   {
863     /* Do not copy if only upper- lowercase has changed */
864     if(strtolower($src_dn) == strtolower($dst_dn)){
865       return(TRUE);
866     }
868     /* Copy source to destination */
869     if (!$this->copy($src_dn, $dst_dn)){
870       return (FALSE);
871     }
873     /* Delete source */
874     $ldap= $this->config->get_ldap_link();
875     $ldap->rmdir_recursive($src_dn);
876     if (!$ldap->success()){
877       trigger_error("Trying to delete $src_dn failed.",
878           E_USER_WARNING);
879       return (FALSE);
880     }
882     return (TRUE);
883   }
886   /* Move/Rename complete trees */
887   function recursive_move($src_dn, $dst_dn)
888   {
889     /* Check if the destination entry exists */
890     $ldap= $this->config->get_ldap_link();
892     /* Check if destination exists - abort */
893     $ldap->cat($dst_dn, array('dn'));
894     if ($ldap->fetch()){
895       trigger_error("recursive_move $dst_dn already exists.",
896           E_USER_WARNING);
897       return (FALSE);
898     }
900     $this->copy($src_dn, $dst_dn);
902     /* Remove src_dn */
903     $ldap->cd($src_dn);
904     $ldap->recursive_remove($src_dn);
905     return (TRUE);
906   }
909   function handle_post_events($mode, $add_attrs= array())
910   {
911     switch ($mode){
912       case "add":
913         $this->postcreate($add_attrs);
914       break;
916       case "modify":
917         $this->postmodify($add_attrs);
918       break;
920       case "remove":
921         $this->postremove($add_attrs);
922       break;
923     }
924   }
927   function saveCopyDialog(){
928   }
931   function getCopyDialog(){
932     return(array("string"=>"","status"=>""));
933   }
936   function PrepareForCopyPaste($source)
937   {
938     $todo = $this->attributes;
939     if(isset($this->CopyPasteVars)){
940       $todo = array_merge($todo,$this->CopyPasteVars);
941     }
943     if(count($this->objectclasses)){
944       $this->is_account = TRUE;
945       foreach($this->objectclasses as $class){
946         if(!in_array($class,$source['objectClass'])){
947           $this->is_account = FALSE;
948         }
949       }
950     }
952     foreach($todo as $var){
953       if (isset($source[$var])){
954         if(isset($source[$var]['count'])){
955           if($source[$var]['count'] > 1){
956             $this->$var = array();
957             $tmp = array();
958             for($i = 0 ; $i < $source[$var]['count']; $i++){
959               $tmp = $source[$var][$i];
960             }
961             $this->$var = $tmp;
962           }else{
963             $this->$var = $source[$var][0];
964           }
965         }else{
966           $this->$var= $source[$var];
967         }
968       }
969     }
970   }
972   function tag_attrs(&$at, $dn= "", $tag= "", $show= false)
973   {
974     /* Skip tagging? 
975        If this is called from departmentGeneric, we have to skip this
976         tagging procedure. 
977      */
978     if($this->skipTagging){
979       return;
980     }
982     /* No dn? Self-operation... */
983     if ($dn == ""){
984       $dn= $this->dn;
986       /* No tag? Find it yourself... */
987       if ($tag == ""){
988         $len= strlen($dn);
990         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
991         $relevant= array();
992         foreach ($this->config->adepartments as $key => $ntag){
994           /* This one is bigger than our dn, its not relevant... */
995           if ($len <= strlen($key)){
996             continue;
997           }
999           /* This one matches with the latter part. Break and don't fix this entry */
1000           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
1001             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
1002             $relevant[strlen($key)]= $ntag;
1003             continue;
1004           }
1006         }
1008         /* If we've some relevant tags to set, just get the longest one */
1009         if (count($relevant)){
1010           ksort($relevant);
1011           $tmp= array_keys($relevant);
1012           $idx= end($tmp);
1013           $tag= $relevant[$idx];
1014           $this->gosaUnitTag= $tag;
1015         }
1016       }
1017     }
1018   
1019     /* Remove tags that may already be here... */
1020     remove_objectClass("gosaAdministrativeUnitTag", $at);
1021     if (isset($at['gosaUnitTag'])){
1022         unset($at['gosaUnitTag']);
1023     }
1025     /* Set tag? */
1026     if ($tag != ""){
1027       add_objectClass("gosaAdministrativeUnitTag", $at);
1028       $at['gosaUnitTag']= $tag;
1029     }
1031     /* Initially this object was tagged. 
1032        - But now, it is no longer inside a tagged department. 
1033        So force the remove of the tag.
1034        (objectClass was already removed obove)
1035      */
1036     if($tag == "" && $this->gosaUnitTag){
1037       $at['gosaUnitTag'] = array();
1038     }
1039   }
1042   /* Add possibility to stop remove process */
1043   function allow_remove()
1044   {
1045     $reason= "";
1046     return $reason;
1047   }
1050   /* Create a snapshot of the current object */
1051   function create_snapshot($type= "snapshot", $description= array())
1052   {
1054     /* Check if snapshot functionality is enabled */
1055     if(!$this->snapshotEnabled()){
1056       return;
1057     }
1059     /* Get configuration from gosa.conf */
1060     $tmp = $this->config->current;
1062     /* Create lokal ldap connection */
1063     $ldap= $this->config->get_ldap_link();
1064     $ldap->cd($this->config->current['BASE']);
1066     /* check if there are special server configurations for snapshots */
1067     if(!isset($tmp['SNAPSHOT_SERVER'])){
1069       /* Source and destination server are both the same, just copy source to dest obj */
1070       $ldap_to      = $ldap;
1071       $snapldapbase = $this->config->current['BASE'];
1073     }else{
1074       $server         = $tmp['SNAPSHOT_SERVER'];
1075       $user           = $tmp['SNAPSHOT_USER'];
1076       $password       = $tmp['SNAPSHOT_PASSWORD'];
1077       $snapldapbase   = $tmp['SNAPSHOT_BASE'];
1079       $ldap_to        = new ldapMultipelxer(new LDAP($user,$password, $server));
1080       $ldap_to -> cd($snapldapbase);
1082       if (!$ldap_to->success()){
1083         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1084       }
1086     }
1088     /* check if the dn exists */ 
1089     if ($ldap->dn_exists($this->dn)){
1091       /* Extract seconds & mysecs, they are used as entry index */
1092       list($usec, $sec)= explode(" ", microtime());
1094       /* Collect some infos */
1095       $base           = $this->config->current['BASE'];
1096       $snap_base      = $tmp['SNAPSHOT_BASE'];
1097       $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1098       $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1100       /* Create object */
1101 #$data             = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1102       $data             = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1103       $newName          = preg_replace("/\./", "", $sec."-".$usec);
1104       $target= array();
1105       $target['objectClass']            = array("top", "gosaSnapshotObject");
1106       $target['gosaSnapshotData']       = gzcompress($data, 6);
1107       $target['gosaSnapshotType']       = $type;
1108       $target['gosaSnapshotDN']         = $this->dn;
1109       $target['description']            = $description;
1110       $target['gosaSnapshotTimestamp']  = $newName;
1112       /* Insert the new snapshot 
1113          But we have to check first, if the given gosaSnapshotTimestamp
1114          is already used, in this case we should increment this value till there is 
1115          an unused value. */ 
1116       $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1117       $ldap_to->cat($new_dn);
1118       while($ldap_to->count()){
1119         $ldap_to->cat($new_dn);
1120         $newName = preg_replace("/\./", "", $sec."-".($usec++));
1121         $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1122         $target['gosaSnapshotTimestamp']  = $newName;
1123       } 
1125       /* Inset this new snapshot */
1126       $ldap_to->cd($snapldapbase);
1127       $ldap_to->create_missing_trees($snapldapbase);
1128       $ldap_to->create_missing_trees($new_base);
1129       $ldap_to->cd($new_dn);
1130       $ldap_to->add($target);
1131       if (!$ldap_to->success()){
1132         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $new_dn, LDAP_ADD, get_class()));
1133       }
1135       if (!$ldap->success()){
1136         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $new_base, "", get_class()));
1137       }
1139     }
1140   }
1142   function remove_snapshot($dn)
1143   {
1144     $ui       = get_userinfo();
1145     $old_dn   = $this->dn; 
1146     $this->dn = $dn;
1147     $ldap = $this->config->get_ldap_link();
1148     $ldap->cd($this->config->current['BASE']);
1149     $ldap->rmdir_recursive($dn);
1150     $this->dn = $old_dn;
1151   }
1154   /* returns true if snapshots are enabled, and false if it is disalbed
1155      There will also be some errors psoted, if the configuration failed */
1156   function snapshotEnabled()
1157   {
1158     $tmp = $this->config->current;
1159     if(isset($tmp['ENABLE_SNAPSHOT'])){
1160       if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1162         /* Check if the snapshot_base is defined */
1163         if(!isset($tmp['SNAPSHOT_BASE'])){
1164           msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),"SNAPSHOT_BASE"), ERROR_DIALOG);
1165           return(FALSE);
1166         }
1168         /* check if there are special server configurations for snapshots */
1169         if(isset($tmp['SNAPSHOT_SERVER'])){
1171           /* check if all required vars are available to create a new ldap connection */
1172           $missing = "";
1173           foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_BASE") as $var){
1174             if(!isset($tmp[$var])){
1175               $missing .= $var." ";
1176               msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."), $missing), ERROR_DIALOG);
1177               return(FALSE);
1178             }
1179           }
1180         }
1181         return(TRUE);
1182       }
1183     }
1184     return(FALSE);
1185   }
1188   /* Return available snapshots for the given base 
1189    */
1190   function Available_SnapsShots($dn,$raw = false)
1191   {
1192     if(!$this->snapshotEnabled()) return(array());
1194     /* Create an additional ldap object which
1195        points to our ldap snapshot server */
1196     $ldap= $this->config->get_ldap_link();
1197     $ldap->cd($this->config->current['BASE']);
1198     $cfg= &$this->config->current;
1200     /* check if there are special server configurations for snapshots */
1202     if(isset($cfg['SERVER']) && isset($cfg['SNAPSHOT_SERVER']) && $cfg['SERVER'] == $cfg['SNAPSHOT_SERVER']){
1203       $ldap_to    = $ldap;
1204     }elseif(isset($cfg['SNAPSHOT_SERVER'])){
1205       $server       = $cfg['SNAPSHOT_SERVER'];
1206       $user         = $cfg['SNAPSHOT_USER'];
1207       $password     = $cfg['SNAPSHOT_PASSWORD'];
1208       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1210       $ldap_to      = new ldapMultiplexer(new LDAP($user,$password, $server));
1211       $ldap_to -> cd ($snapldapbase);
1212       if (!$ldap_to->success()){
1213         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1214       }
1215     }else{
1216       $ldap_to    = $ldap;
1217     }
1219     /* Prepare bases and some other infos */
1220     $base           = $this->config->current['BASE'];
1221     $snap_base      = $cfg['SNAPSHOT_BASE'];
1222     $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1223     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1224     $tmp            = array(); 
1226     /* Fetch all objects with  gosaSnapshotDN=$dn */
1227     $ldap_to->cd($new_base);
1228     $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1229         array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description")); 
1231     /* Put results into a list and add description if missing */
1232     while($entry = $ldap_to->fetch()){ 
1233       if(!isset($entry['description'][0])){
1234         $entry['description'][0]  = "";
1235       }
1236       $tmp[] = $entry; 
1237     }
1239     /* Return the raw array, or format the result */
1240     if($raw){
1241       return($tmp);
1242     }else{  
1243       $tmp2 = array();
1244       foreach($tmp as $entry){
1245         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1246       }
1247     }
1248     return($tmp2);
1249   }
1252   function getAllDeletedSnapshots($base_of_object,$raw = false)
1253   {
1254     if(!$this->snapshotEnabled()) return(array());
1256     /* Create an additional ldap object which
1257        points to our ldap snapshot server */
1258     $ldap= $this->config->get_ldap_link();
1259     $ldap->cd($this->config->current['BASE']);
1260     $cfg= &$this->config->current;
1262     /* check if there are special server configurations for snapshots */
1263     if(isset($cfg['SNAPSHOT_SERVER'])){
1264       $server       = $cfg['SNAPSHOT_SERVER'];
1265       $user         = $cfg['SNAPSHOT_USER'];
1266       $password     = $cfg['SNAPSHOT_PASSWORD'];
1267       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1268       $ldap_to      = new ldapMultiplexer(new LDAP($user,$password, $server));
1269       $ldap_to->cd ($snapldapbase);
1270       if (!$ldap_to->success()){
1271         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1272       }
1273     }else{
1274       $ldap_to    = $ldap;
1275     }
1277     /* Prepare bases */ 
1278     $base           = $this->config->current['BASE'];
1279     $snap_base      = $cfg['SNAPSHOT_BASE'];
1280     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1282     /* Fetch all objects and check if they do not exist anymore */
1283     $ui = get_userinfo();
1284     $tmp = array();
1285     $ldap_to->cd($new_base);
1286     $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1287     while($entry = $ldap_to->fetch()){
1289       $chk =  str_replace($new_base,"",$entry['dn']);
1290       if(preg_match("/,ou=/",$chk)) continue;
1292       if(!isset($entry['description'][0])){
1293         $entry['description'][0]  = "";
1294       }
1295       $tmp[] = $entry; 
1296     }
1298     /* Check if entry still exists */
1299     foreach($tmp as $key => $entry){
1300       $ldap->cat($entry['gosaSnapshotDN'][0]);
1301       if($ldap->count()){
1302         unset($tmp[$key]);
1303       }
1304     }
1306     /* Format result as requested */
1307     if($raw) {
1308       return($tmp);
1309     }else{
1310       $tmp2 = array();
1311       foreach($tmp as $key => $entry){
1312         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1313       }
1314     }
1315     return($tmp2);
1316   } 
1319   /* Restore selected snapshot */
1320   function restore_snapshot($dn)
1321   {
1322     if(!$this->snapshotEnabled()) return(array());
1324     $ldap= $this->config->get_ldap_link();
1325     $ldap->cd($this->config->current['BASE']);
1326     $cfg= &$this->config->current;
1328     /* check if there are special server configurations for snapshots */
1329     if(isset($cfg['SNAPSHOT_SERVER'])){
1330       $server       = $cfg['SNAPSHOT_SERVER'];
1331       $user         = $cfg['SNAPSHOT_USER'];
1332       $password     = $cfg['SNAPSHOT_PASSWORD'];
1333       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1334       $ldap_to      = new ldapMultiplexer(new LDAP($user,$password, $server));
1335       $ldap_to->cd ($snapldapbase);
1336       if (!$ldap_to->success()){
1337         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
1338       }
1339     }else{
1340       $ldap_to    = $ldap;
1341     }
1343     /* Get the snapshot */ 
1344     $ldap_to->cat($dn);
1345     $restoreObject = $ldap_to->fetch();
1347     /* Prepare import string */
1348     $data  = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1350     /* Import the given data */
1351     $ldap->import_complete_ldif($data,$err,false,false);
1352     if (!$ldap->success()){
1353       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, "", get_class()));
1354     }
1355   }
1358   function showSnapshotDialog($base,$baseSuffixe)
1359   {
1360     $once = true;
1361     foreach($_POST as $name => $value){
1363       /* Create a new snapshot, display a dialog */
1364       if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1365         $once = false;
1366         $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1367         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1368         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1369       }
1371       /* Restore a snapshot, display a dialog with all snapshots of the current object */
1372       if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1373         $once = false;
1374         $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1375         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1376         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1377         $this->snapDialog->display_restore_dialog = true;
1378       }
1380       /* Restore one of the already deleted objects */
1381       if(((isset($_POST['menu_action']) && $_POST['menu_action'] == "RestoreDeletedSnapShot") 
1382           || preg_match("/^RestoreDeletedSnapShot_/",$name)) && $once){
1383         $once = false;
1384         $this->snapDialog = new SnapShotDialog($this->config,"",$this);
1385         $this->snapDialog->set_snapshot_bases($baseSuffixe);
1386         $this->snapDialog->display_restore_dialog      = true;
1387         $this->snapDialog->display_all_removed_objects  = true;
1388       }
1390       /* Restore selected snapshot */
1391       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1392         $once = false;
1393         $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1394         $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1395         if(!empty($entry)){
1396           $this->restore_snapshot($entry);
1397           $this->snapDialog = NULL;
1398         }
1399       }
1400     }
1402     /* Create a new snapshot requested, check
1403        the given attributes and create the snapshot*/
1404     if(isset($_POST['CreateSnapshot']) && is_object($this->snapDialog)){
1405       $this->snapDialog->save_object();
1406       $msgs = $this->snapDialog->check();
1407       if(count($msgs)){
1408         foreach($msgs as $msg){
1409           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
1410         }
1411       }else{
1412         $this->dn =  $this->snapDialog->dn;
1413         $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1414         $this->snapDialog = NULL;
1415       }
1416     }
1418     /* Restore is requested, restore the object with the posted dn .*/
1419     if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1420     }
1422     if(isset($_POST['CancelSnapshot'])){
1423       $this->snapDialog = NULL;
1424     }
1426     if(is_object($this->snapDialog )){
1427       $this->snapDialog->save_object();
1428       return($this->snapDialog->execute());
1429     }
1430   }
1433   static function plInfo()
1434   {
1435     return array();
1436   }
1439   function set_acl_base($base)
1440   {
1441     $this->acl_base= $base;
1442   }
1445   function set_acl_category($category)
1446   {
1447     $this->acl_category= "$category/";
1448   }
1451   function acl_is_writeable($attribute,$skip_write = FALSE)
1452   {
1453     $ui= get_userinfo();
1454     return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1455   }
1458   function acl_is_readable($attribute)
1459   {
1460     $ui= get_userinfo();
1461     return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1462   }
1465   function acl_is_createable()
1466   {
1467     $ui= get_userinfo();
1468     return preg_match('/c/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1469   }
1472   function acl_is_removeable()
1473   {
1474     $ui= get_userinfo();
1475     return preg_match('/d/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1476   }
1479   function acl_is_moveable()
1480   {
1481     $ui= get_userinfo();
1482     return preg_match('/m/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1483   }
1486   function acl_have_any_permissions()
1487   {
1488   }
1491   function getacl($attribute,$skip_write= FALSE)
1492   {
1493     $ui= get_userinfo();
1494     return  $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1495   }
1497   /* Get all allowed bases to move an object to or to create a new object.
1498      Idepartments also contains all base departments which lead to the allowed bases */
1499   function get_allowed_bases($category = "")
1500   {
1501     $ui = get_userinfo();
1502     $deps = array();
1504     /* Set category */ 
1505     if(empty($category)){
1506       $category = $this->acl_category.get_class($this);
1507     }
1509     /* Is this a new object ? Or just an edited existing object */
1510     if(!$this->initially_was_account && $this->is_account){
1511       $new = true;
1512     }else{
1513       $new = false;
1514     }
1516     $cat_bases = $ui->get_module_departments(preg_replace("/\/.*$/","",$category));
1517     foreach($this->config->idepartments as $dn => $name){
1518       
1519       if(!in_array_ics($dn,$cat_bases)){
1520         continue;
1521       }
1522       
1523       $acl = $ui->get_permissions($dn,$category);
1524       if($new && preg_match("/c/",$acl)){
1525         $deps[$dn] = $name;
1526       }elseif(!$new && preg_match("/m/",$acl)){
1527         $deps[$dn] = $name;
1528       }
1529     }
1531     /* Add current base */      
1532     if(isset($this->base) && isset($this->config->idepartments[$this->base])){
1533       $deps[$this->base] = $this->config->idepartments[$this->base];
1534     }else{
1535       trigger_error("No default base found in class ".get_class($this).". ".$this->base);
1536     }
1537     return($deps);
1538   }
1541   /* This function modifies object acls too, if an object is moved.
1542    *  $old_dn   specifies the actually used dn
1543    *  $new_dn   specifies the destiantion dn
1544    */
1545   function update_acls($old_dn,$new_dn,$output_changes = FALSE)
1546   {
1547     /* Check if old_dn is empty. This should never happen */
1548     if(empty($old_dn) || empty($new_dn)){
1549       trigger_error("Failed to check acl dependencies, wrong dn given.");
1550       return;
1551     }
1553     /* Update userinfo if necessary */
1554     $ui = session::get('ui');
1555     if($ui->dn == $old_dn){
1556       $ui->dn = $new_dn;
1557       session::set('ui',$ui);
1558       new log("view","acl/".get_class($this),$this->dn,array(),"Updated current user dn from '".$old_dn."' to '".$new_dn."'");
1559     }
1561     /* Object was moved, ensure that all acls will be moved too */
1562     if($new_dn != $old_dn && $old_dn != "new"){
1564       /* get_ldap configuration */
1565       $update = array();
1566       $ldap = $this->config->get_ldap_link();
1567       $ldap->cd ($this->config->current['BASE']);
1568       $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*))",array("cn","gosaAclEntry"));
1569       while($attrs = $ldap->fetch()){
1571         $acls = array();
1573         /* Walk through acls */
1574         for($i = 0 ; $i <  $attrs['gosaAclEntry']['count'] ; $i ++ ){
1576           /* Reset vars */
1577           $found = false;
1579           /* Get Acl parts */
1580           $acl_parts = split(":",$attrs['gosaAclEntry'][$i]);
1582           /* Get every single member for this acl */  
1583           $members = array();  
1584           if(preg_match("/,/",$acl_parts[2])){
1585             $members = split(",",$acl_parts[2]);
1586           }else{
1587             $members = array($acl_parts[2]);
1588           } 
1589       
1590           /* Check if member match current dn */
1591           foreach($members as $key => $member){
1592             $member = base64_decode($member);
1593             if($member == $old_dn){
1594               $found = true;
1595               $members[$key] = base64_encode($new_dn);
1596             }
1597           } 
1598          
1599           /* Create new member string */ 
1600           $new_members = "";
1601           foreach($members as $member){
1602             $new_members .= $member.",";
1603           }
1604           $new_members = preg_replace("/,$/","",$new_members);
1605           $acl_parts[2] = $new_members;
1606         
1607           /* Reconstruckt acl entry */
1608           $acl_str  ="";
1609           foreach($acl_parts as $t){
1610            $acl_str .= $t.":";
1611           }
1612           $acl_str = preg_replace("/:$/","",$acl_str);
1613        }
1615        /* Acls for this object must be adjusted */
1616        if($found){
1618           $debug_info=  _("Changing ACL dn")."&nbsp;:&nbsp;<br>&nbsp;-"._("from")."&nbsp;<b>&nbsp;".
1619                   $old_dn."</b><br>&nbsp;-"._("to")."&nbsp;<b>".$new_dn."</b><br>";
1620           @DEBUG (DEBUG_ACL, __LINE__, __FUNCTION__, __FILE__,$debug_info,"ACL");
1622           $update[$attrs['dn']] =array();
1623           foreach($acls as $acl){
1624             $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1625           }
1626         }
1627       }
1629       /* Write updated acls */
1630       foreach($update as $dn => $attrs){
1631         $ldap->cd($dn);
1632         $ldap->modify($attrs);
1633       }
1634     }
1635   }
1637   
1639   /* This function enables the entry Serial ID check.
1640    * If an entry was edited while we have edited the entry too,
1641    *  an error message will be shown. 
1642    * To configure this check correctly read the FAQ.
1643    */    
1644   function enable_CSN_check()
1645   {
1646     $this->CSN_check_active =TRUE;
1647     $this->entryCSN = getEntryCSN($this->dn);
1648   }
1651   /*! \brief  Prepares the plugin to be used for multiple edit
1652    *          Update plugin attributes with given array of attribtues.
1653    *  @param  array   Array with attributes that must be updated.
1654    */
1655   function init_multiple_support($attrs,$all)
1656   {
1657     $ldap= $this->config->get_ldap_link();
1658     $this->multi_attrs    = $attrs;
1659     $this->multi_attrs_all= $all;
1661     /* Copy needed attributes */
1662     foreach ($this->attributes as $val){
1663       $found= array_key_ics($val, $this->multi_attrs);
1664       if ($found != ""){
1665         if(isset($this->multi_attrs["$found"][0])){
1666           $this->$val= $this->multi_attrs["$found"][0];
1667         }
1668       }
1669     }
1670   }
1672  
1673   /*! \brief  Enables multiple support for this plugin
1674    */
1675   function enable_multiple_support()
1676   {
1677     $this->ignore_account = TRUE;
1678     $this->multiple_support_active = TRUE;
1679   }
1682   /*! \brief  Returns all values that have been modfied in multiple edit mode.
1683       @return array Cotaining all mdofied values. 
1684    */
1685   function get_multi_edit_values()
1686   {
1687     $ret = array();
1688     foreach($this->attributes as $attr){
1689       if(in_array($attr,$this->multi_boxes)){
1690         $ret[$attr] = $this->$attr;
1691       }
1692     }
1693     return($ret);
1694   }
1696   
1697   /*! \brief  Update class variables with values collected by multiple edit.
1698    */
1699   function set_multi_edit_values($attrs)
1700   {
1701     foreach($attrs as $name => $value){
1702       $this->$name = $value;
1703     }
1704   }
1707   /*! \brief execute plugin
1709     Generates the html output for this node
1710    */
1711   function multiple_execute()
1712   {
1713     /* This one is empty currently. Fabian - please fill in the docu code */
1714     session::set('current_class_for_help',get_class($this));
1716     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
1717     session::set('LOCK_VARS_TO_USE',array());
1718     session::set('LOCK_VARS_USED',array());
1719     
1720     return("Multiple edit is currently not implemented for this plugin.");
1721   }
1724   /*! \brief   Save HTML posted data to object for multiple edit
1725    */
1726   function multiple_save_object()
1727   {
1728     if(empty($this->entryCSN) && $this->CSN_check_active){
1729       $this->entryCSN = getEntryCSN($this->dn);
1730     }
1732     /* Save values to object */
1733     $this->multi_boxes = array();
1734     foreach ($this->attributes as $val){
1735   
1736       /* Get selected checkboxes from multiple edit */
1737       if(isset($_POST["use_".$val])){
1738         $this->multi_boxes[] = $val;
1739       }
1741       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
1743         /* Check for modifications */
1744         if (get_magic_quotes_gpc()) {
1745           $data= stripcslashes($_POST["$val"]);
1746         } else {
1747           $data= $this->$val = $_POST["$val"];
1748         }
1749         if ($this->$val != $data){
1750           $this->is_modified= TRUE;
1751         }
1752     
1753         /* IE post fix */
1754         if(isset($data[0]) && $data[0] == chr(194)) {
1755           $data = "";  
1756         }
1757         $this->$val= $data;
1758       }
1759     }
1760   }
1763   /*! \brief  Returns all attributes of this plugin, 
1764                to be able to detect multiple used attributes 
1765                in multi_plugg::detect_multiple_used_attributes().
1766       @return array Attributes required for intialization of multi_plug
1767    */
1768   public function get_multi_init_values()
1769   {
1770     $attrs = $this->attrs;
1771     return($attrs);
1772   }
1775   /*! \brief  Check given values in multiple edit
1776       @return array Error messages
1777    */
1778   function multiple_check()
1779   {
1780     $message = plugin::check();
1781     return($message);
1782   }
1785 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1786 ?>