Code

Added list footer
[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)
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       if (isset($this->attrs["$val"][0])){
513         /* If attribute is set, replace dynamic parts: 
514            %sn, %givenName and %uid. Fill these in our local variables. */
515         $value= $this->attrs["$val"][0];
517         foreach (array("sn", "givenName", "uid") as $repl){
518           if (preg_match("/%$repl/i", $value)){
519             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
520           }
521         }
522         $this->$val= $value;
523       }
524     }
526     /* Is Account? */
527     $found= TRUE;
528     foreach ($this->objectclasses as $obj){
529       if (preg_match('/top/i', $obj)){
530         continue;
531       }
532       if (!in_array_ics ($obj, $this->attrs['objectClass'])){
533         $found= FALSE;
534         break;
535       }
536     }
537     if ($found){
538       $this->is_account= TRUE;
539     }
540   }
542   /* Indicate whether a password change is needed or not */
543   function password_change_needed()
544   {
545     return FALSE;
546   }
549   /* Show header message for tab dialogs */
550   function show_enable_header($button_text, $text, $disabled= FALSE)
551   {
552     if (($disabled == TRUE) || (!$this->acl_is_createable())){
553       $state= "disabled";
554     } else {
555       $state= "";
556     }
557     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
558     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
559       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
561     return($display);
562   }
565   /* Show header message for tab dialogs */
566   function show_disable_header($button_text, $text, $disabled= FALSE)
567   {
568     if (($disabled == TRUE) || !$this->acl_is_removeable()){
569       $state= "disabled";
570     } else {
571       $state= "";
572     }
573     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
574     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.
575       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
577     return($display);
578   }
581   /* Show header message for tab dialogs */
582   function show_header($button_text, $text, $disabled= FALSE)
583   {
584     echo "FIXME: show_header should be replaced by show_disable_header and show_enable_header<br>";
585     if ($disabled == TRUE){
586       $state= "disabled";
587     } else {
588       $state= "";
589     }
590     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
591     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
592       ($this->acl_is_createable()?'':'disabled')." ".$state.
593       "><p class=\"seperator\">&nbsp;</p></td></tr></table>";
595     return($display);
596   }
599   function postcreate($add_attrs= array())
600   {
601     /* Find postcreate entries for this class */
602     $command= $this->config->search(get_class($this), "POSTCREATE",array('menu', 'tabs'));
604     if ($command != ""){
606       /* Additional attributes */
607       foreach ($add_attrs as $name => $value){
608         $command= preg_replace("/%$name( |$)/", "$value ", $command);
609       }
611       /* Walk through attribute list */
612       foreach ($this->attributes as $attr){
613         if (!is_array($this->$attr)){
614           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
615         }
616       }
617       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
619       if (check_command($command)){
620         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
621             $command, "Execute");
623         exec($command);
624       } else {
625         $message[]= msgPool::cmdnotfound("POSTCREATE", get_class($this));
626         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
627       }
628     }
629   }
631   function postmodify($add_attrs= array())
632   {
633     /* Find postcreate entries for this class */
634     $command= $this->config->search(get_class($this), "POSTMODIFY",array('menu','tabs'));
636     if ($command != ""){
638       /* Additional attributes */
639       foreach ($add_attrs as $name => $value){
640         $command= preg_replace("/%$name( |$)/", "$value ", $command);
641       }
643       /* Walk through attribute list */
644       foreach ($this->attributes as $attr){
645         if (!is_array($this->$attr)){
646           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
647         }
648       }
649       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
651       if (check_command($command)){
652         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
653             $command, "Execute");
655         exec($command);
656       } else {
657         $message[]= msgPool::cmdnotfound("POSTMODIFY", get_class($this));
658         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
659       }
660     }
661   }
663   function postremove($add_attrs= array())
664   {
665     /* Find postremove entries for this class */
666     $command= $this->config->search(get_class($this), "POSTREMOVE",array('menu','tabs'));
667     if ($command != ""){
669       /* Additional attributes */
670       foreach ($add_attrs as $name => $value){
671         $command= preg_replace("/%$name( |$)/", "$value ", $command);
672       }
674       /* Walk through attribute list */
675       foreach ($this->attributes as $attr){
676         if (!is_array($this->$attr)){
677           $command= preg_replace("/%$attr( |$)/", $this->$attr." ", $command);
678         }
679       }
680       $command= preg_replace("/%dn( |$)/", $this->dn." ", $command);
682       if (check_command($command)){
683         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
684             $command, "Execute");
686         exec($command);
687       } else {
688         $message[]= msgPool::cmdnotfound("POSTREMOVE", get_class($this));
689         msg_dialog::display(_("Error"), $message, ERROR_DIALOG);
690       }
691     }
692   }
694   /* Create unique DN */
695   function create_unique_dn($attribute, $base)
696   {
697     $ldap= $this->config->get_ldap_link();
698     $base= preg_replace("/^,*/", "", $base);
700     /* Try to use plain entry first */
701     $dn= "$attribute=".$this->$attribute.",$base";
702     $ldap->cat ($dn, array('dn'));
703     if (!$ldap->fetch()){
704       return ($dn);
705     }
707     /* Look for additional attributes */
708     foreach ($this->attributes as $attr){
709       if ($attr == $attribute || $this->$attr == ""){
710         continue;
711       }
713       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
714       $ldap->cat ($dn, array('dn'));
715       if (!$ldap->fetch()){
716         return ($dn);
717       }
718     }
720     /* None found */
721     return ("none");
722   }
724   function rebind($ldap, $referral)
725   {
726     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
727     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
728       $this->error = "Success";
729       $this->hascon=true;
730       $this->reconnect= true;
731       return (0);
732     } else {
733       $this->error = "Could not bind to " . $credentials['ADMIN'];
734       return NULL;
735     }
736   }
739   /* Recursively copy ldap object */
740   function _copy($src_dn,$dst_dn)
741   {
742     $ldap=$this->config->get_ldap_link();
743     $ldap->cat($src_dn);
744     $attrs= $ldap->fetch();
746     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
747     $ds= ldap_connect($this->config->current['SERVER']);
748     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
749     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
750       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
751     }
753     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
754     $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
756     /* Fill data from LDAP */
757     $new= array();
758     if ($sr) {
759       $ei=ldap_first_entry($ds, $sr);
760       if ($ei) {
761         foreach($attrs as $attr => $val){
762           if ($info = @ldap_get_values_len($ds, $ei, $attr)){
763             for ($i= 0; $i<$info['count']; $i++){
764               if ($info['count'] == 1){
765                 $new[$attr]= $info[$i];
766               } else {
767                 $new[$attr][]= $info[$i];
768               }
769             }
770           }
771         }
772       }
773     }
775     /* close conncetion */
776     ldap_unbind($ds);
778     /* Adapt naming attribute */
779     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
780     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
781     $new[$dst_name]= @LDAP::fix($dst_val);
783     /* Check if this is a department.
784      * If it is a dep. && there is a , override in his ou 
785      *  change \2C to , again, else this entry can't be saved ...
786      */
787     if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
788       $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
789     }
791     /* Save copy */
792     $ldap->connect();
793     $ldap->cd($this->config->current['BASE']);
794     
795     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
797     /* FAIvariable=.../..., cn=.. 
798         could not be saved, because the attribute FAIvariable was different to 
799         the dn FAIvariable=..., cn=... */
800     if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
801       $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
802     }
803     $ldap->cd($dst_dn);
804     $ldap->add($new);
806     if ($ldap->error != "Success"){
807       trigger_error("Trying to save $dst_dn failed.",
808           E_USER_WARNING);
809       return(FALSE);
810     }
811     return(TRUE);
812   }
815   /* This is a workaround function. */
816   function copy($src_dn, $dst_dn)
817   {
818     /* Rename dn in possible object groups */
819     $ldap= $this->config->get_ldap_link();
820     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::prepare4filter($src_dn).'))',
821         array('cn'));
822     while ($attrs= $ldap->fetch()){
823       $og= new ogroup($this->config, $ldap->getDN());
824       unset($og->member[$src_dn]);
825       $og->member[$dst_dn]= $dst_dn;
826       $og->save ();
827     }
829     $ldap->cat($dst_dn);
830     $attrs= $ldap->fetch();
831     if (count($attrs)){
832       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
833           E_USER_WARNING);
834       return (FALSE);
835     }
837     $ldap->cat($src_dn);
838     $attrs= $ldap->fetch();
839     if (!count($attrs)){
840       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
841           E_USER_WARNING);
842       return (FALSE);
843     }
845     $ldap->cd($src_dn);
846     $ldap->search("objectClass=*",array("dn"));
847     while($attrs = $ldap->fetch()){
848       $src = $attrs['dn'];
849       $dst = preg_replace("/".normalizePreg($src_dn)."$/",$dst_dn,$attrs['dn']);
850       $this->_copy($src,$dst);
851     }
852     return (TRUE);
853   }
856   function move($src_dn, $dst_dn)
857   {
858     /* Do not copy if only upper- lowercase has changed */
859     if(strtolower($src_dn) == strtolower($dst_dn)){
860       return(TRUE);
861     }
863     /* Copy source to destination */
864     if (!$this->copy($src_dn, $dst_dn)){
865       return (FALSE);
866     }
868     /* Delete source */
869     $ldap= $this->config->get_ldap_link();
870     $ldap->rmdir_recursive($src_dn);
871     if ($ldap->error != "Success"){
872       trigger_error("Trying to delete $src_dn failed.",
873           E_USER_WARNING);
874       return (FALSE);
875     }
877     return (TRUE);
878   }
881   /* Move/Rename complete trees */
882   function recursive_move($src_dn, $dst_dn)
883   {
884     /* Check if the destination entry exists */
885     $ldap= $this->config->get_ldap_link();
887     /* Check if destination exists - abort */
888     $ldap->cat($dst_dn, array('dn'));
889     if ($ldap->fetch()){
890       trigger_error("recursive_move $dst_dn already exists.",
891           E_USER_WARNING);
892       return (FALSE);
893     }
895     $this->copy($src_dn, $dst_dn);
897     /* Remove src_dn */
898     $ldap->cd($src_dn);
899     $ldap->recursive_remove($src_dn);
900     return (TRUE);
901   }
904   function handle_post_events($mode, $add_attrs= array())
905   {
906     switch ($mode){
907       case "add":
908         $this->postcreate($add_attrs);
909       break;
911       case "modify":
912         $this->postmodify($add_attrs);
913       break;
915       case "remove":
916         $this->postremove($add_attrs);
917       break;
918     }
919   }
922   function saveCopyDialog(){
923   }
926   function getCopyDialog(){
927     return(array("string"=>"","status"=>""));
928   }
931   function PrepareForCopyPaste($source)
932   {
933     $todo = $this->attributes;
934     if(isset($this->CopyPasteVars)){
935       $todo = array_merge($todo,$this->CopyPasteVars);
936     }
938     if(count($this->objectclasses)){
939       $this->is_account = TRUE;
940       foreach($this->objectclasses as $class){
941         if(!in_array($class,$source['objectClass'])){
942           $this->is_account = FALSE;
943         }
944       }
945     }
947     foreach($todo as $var){
948       if (isset($source[$var])){
949         if(isset($source[$var]['count'])){
950           if($source[$var]['count'] > 1){
951             $this->$var = array();
952             $tmp = array();
953             for($i = 0 ; $i < $source[$var]['count']; $i++){
954               $tmp = $source[$var][$i];
955             }
956             $this->$var = $tmp;
957           }else{
958             $this->$var = $source[$var][0];
959           }
960         }else{
961           $this->$var= $source[$var];
962         }
963       }
964     }
965   }
967   function tag_attrs(&$at, $dn= "", $tag= "", $show= false)
968   {
969     /* Skip tagging? 
970        If this is called from departmentGeneric, we have to skip this
971         tagging procedure. 
972      */
973     if($this->skipTagging){
974       return;
975     }
977     /* No dn? Self-operation... */
978     if ($dn == ""){
979       $dn= $this->dn;
981       /* No tag? Find it yourself... */
982       if ($tag == ""){
983         $len= strlen($dn);
985         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
986         $relevant= array();
987         foreach ($this->config->adepartments as $key => $ntag){
989           /* This one is bigger than our dn, its not relevant... */
990           if ($len <= strlen($key)){
991             continue;
992           }
994           /* This one matches with the latter part. Break and don't fix this entry */
995           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
996             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
997             $relevant[strlen($key)]= $ntag;
998             continue;
999           }
1001         }
1003         /* If we've some relevant tags to set, just get the longest one */
1004         if (count($relevant)){
1005           ksort($relevant);
1006           $tmp= array_keys($relevant);
1007           $idx= end($tmp);
1008           $tag= $relevant[$idx];
1009           $this->gosaUnitTag= $tag;
1010         }
1011       }
1012     }
1013   
1014     /* Remove tags that may already be here... */
1015     remove_objectClass("gosaAdministrativeUnitTag", $at);
1016     if (isset($at['gosaUnitTag'])){
1017         unset($at['gosaUnitTag']);
1018     }
1020     /* Set tag? */
1021     if ($tag != ""){
1022       add_objectClass("gosaAdministrativeUnitTag", $at);
1023       $at['gosaUnitTag']= $tag;
1024     }
1026     /* Initially this object was tagged. 
1027        - But now, it is no longer inside a tagged department. 
1028        So force the remove of the tag.
1029        (objectClass was already removed obove)
1030      */
1031     if($tag == "" && $this->gosaUnitTag){
1032       $at['gosaUnitTag'] = array();
1033     }
1034   }
1037   /* Add possibility to stop remove process */
1038   function allow_remove()
1039   {
1040     $reason= "";
1041     return $reason;
1042   }
1045   /* Create a snapshot of the current object */
1046   function create_snapshot($type= "snapshot", $description= array())
1047   {
1049     /* Check if snapshot functionality is enabled */
1050     if(!$this->snapshotEnabled()){
1051       return;
1052     }
1054     /* Get configuration from gosa.conf */
1055     $tmp = $this->config->current;
1057     /* Create lokal ldap connection */
1058     $ldap= $this->config->get_ldap_link();
1059     $ldap->cd($this->config->current['BASE']);
1061     /* check if there are special server configurations for snapshots */
1062     if(!isset($tmp['SNAPSHOT_SERVER'])){
1064       /* Source and destination server are both the same, just copy source to dest obj */
1065       $ldap_to      = $ldap;
1066       $snapldapbase = $this->config->current['BASE'];
1068     }else{
1069       $server         = $tmp['SNAPSHOT_SERVER'];
1070       $user           = $tmp['SNAPSHOT_USER'];
1071       $password       = $tmp['SNAPSHOT_PASSWORD'];
1072       $snapldapbase   = $tmp['SNAPSHOT_BASE'];
1074       $ldap_to        = new LDAP($user,$password, $server);
1075       $ldap_to -> cd($snapldapbase);
1076       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$snapldapbase));
1077     }
1079     /* check if the dn exists */ 
1080     if ($ldap->dn_exists($this->dn)){
1082       /* Extract seconds & mysecs, they are used as entry index */
1083       list($usec, $sec)= explode(" ", microtime());
1085       /* Collect some infos */
1086       $base           = $this->config->current['BASE'];
1087       $snap_base      = $tmp['SNAPSHOT_BASE'];
1088       $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1089       $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1091       /* Create object */
1092 #$data             = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1093       $data             = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1094       $newName          = preg_replace("/\./", "", $sec."-".$usec);
1095       $target= array();
1096       $target['objectClass']            = array("top", "gosaSnapshotObject");
1097       $target['gosaSnapshotData']       = gzcompress($data, 6);
1098       $target['gosaSnapshotType']       = $type;
1099       $target['gosaSnapshotDN']         = $this->dn;
1100       $target['description']            = $description;
1101       $target['gosaSnapshotTimestamp']  = $newName;
1103       /* Insert the new snapshot 
1104          But we have to check first, if the given gosaSnapshotTimestamp
1105          is already used, in this case we should increment this value till there is 
1106          an unused value. */ 
1107       $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1108       $ldap_to->cat($new_dn);
1109       while($ldap_to->count()){
1110         $ldap_to->cat($new_dn);
1111         $newName = preg_replace("/\./", "", $sec."-".($usec++));
1112         $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1113         $target['gosaSnapshotTimestamp']  = $newName;
1114       } 
1116       /* Inset this new snapshot */
1117       $ldap_to->cd($snapldapbase);
1118       $ldap_to->create_missing_trees($snapldapbase);
1119       $ldap_to->create_missing_trees($new_base);
1120       $ldap_to->cd($new_dn);
1121       $ldap_to->add($target);
1122     
1123       show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1124       show_ldap_error($ldap_to->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1125     }
1126   }
1128   function remove_snapshot($dn)
1129   {
1130     $ui       = get_userinfo();
1131     $old_dn   = $this->dn; 
1132     $this->dn = $dn;
1133     $ldap = $this->config->get_ldap_link();
1134     $ldap->cd($this->config->current['BASE']);
1135     $ldap->rmdir_recursive($dn);
1136     $this->dn = $old_dn;
1137   }
1140   /* returns true if snapshots are enabled, and false if it is disalbed
1141      There will also be some errors psoted, if the configuration failed */
1142   function snapshotEnabled()
1143   {
1144     $tmp = $this->config->current;
1145     if(isset($tmp['ENABLE_SNAPSHOT'])){
1146       if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1148         /* Check if the snapshot_base is defined */
1149         if(!isset($tmp['SNAPSHOT_BASE'])){
1150           msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),"SNAPSHOT_BASE"), ERROR_DIALOG);
1151           return(FALSE);
1152         }
1154         /* check if there are special server configurations for snapshots */
1155         if(isset($tmp['SNAPSHOT_SERVER'])){
1157           /* check if all required vars are available to create a new ldap connection */
1158           $missing = "";
1159           foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_BASE") as $var){
1160             if(!isset($tmp[$var])){
1161               $missing .= $var." ";
1162               msg_dialog::display(_("Configuration error"), sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."), $missing), ERROR_DIALOG);
1163               return(FALSE);
1164             }
1165           }
1166         }
1167         return(TRUE);
1168       }
1169     }
1170     return(FALSE);
1171   }
1174   /* Return available snapshots for the given base 
1175    */
1176   function Available_SnapsShots($dn,$raw = false)
1177   {
1178     if(!$this->snapshotEnabled()) return(array());
1180     /* Create an additional ldap object which
1181        points to our ldap snapshot server */
1182     $ldap= $this->config->get_ldap_link();
1183     $ldap->cd($this->config->current['BASE']);
1184     $cfg= &$this->config->current;
1186     /* check if there are special server configurations for snapshots */
1188     if(isset($cfg['SERVER']) && isset($cfg['SNAPSHOT_SERVER']) && $cfg['SERVER'] == $cfg['SNAPSHOT_SERVER']){
1189       $ldap_to    = $ldap;
1190     }elseif(isset($cfg['SNAPSHOT_SERVER'])){
1191       $server       = $cfg['SNAPSHOT_SERVER'];
1192       $user         = $cfg['SNAPSHOT_USER'];
1193       $password     = $cfg['SNAPSHOT_PASSWORD'];
1194       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1196       $ldap_to      = new LDAP($user,$password, $server);
1197       $ldap_to -> cd ($snapldapbase);
1198       show_ldap_error($ldap->get_error(), sprintf(_("Method get available snapshots with dn '%s' failed."),$this->dn));
1199     }else{
1200       $ldap_to    = $ldap;
1201     }
1203     /* Prepare bases and some other infos */
1204     $base           = $this->config->current['BASE'];
1205     $snap_base      = $cfg['SNAPSHOT_BASE'];
1206     $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1207     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1208     $tmp            = array(); 
1210     /* Fetch all objects with  gosaSnapshotDN=$dn */
1211     $ldap_to->cd($new_base);
1212     $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1213         array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description")); 
1215     /* Put results into a list and add description if missing */
1216     while($entry = $ldap_to->fetch()){ 
1217       if(!isset($entry['description'][0])){
1218         $entry['description'][0]  = "";
1219       }
1220       $tmp[] = $entry; 
1221     }
1223     /* Return the raw array, or format the result */
1224     if($raw){
1225       return($tmp);
1226     }else{  
1227       $tmp2 = array();
1228       foreach($tmp as $entry){
1229         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1230       }
1231     }
1232     return($tmp2);
1233   }
1236   function getAllDeletedSnapshots($base_of_object,$raw = false)
1237   {
1238     if(!$this->snapshotEnabled()) return(array());
1240     /* Create an additional ldap object which
1241        points to our ldap snapshot server */
1242     $ldap= $this->config->get_ldap_link();
1243     $ldap->cd($this->config->current['BASE']);
1244     $cfg= &$this->config->current;
1246     /* check if there are special server configurations for snapshots */
1247     if(isset($cfg['SNAPSHOT_SERVER'])){
1248       $server       = $cfg['SNAPSHOT_SERVER'];
1249       $user         = $cfg['SNAPSHOT_USER'];
1250       $password     = $cfg['SNAPSHOT_PASSWORD'];
1251       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1252       $ldap_to      = new LDAP($user,$password, $server);
1253       $ldap_to->cd ($snapldapbase);
1254       show_ldap_error($ldap_to->get_error(), sprintf(_("Method get deleted snapshots with dn '%s' failed."),$this->dn));
1255     }else{
1256       $ldap_to    = $ldap;
1257     }
1259     /* Prepare bases */ 
1260     $base           = $this->config->current['BASE'];
1261     $snap_base      = $cfg['SNAPSHOT_BASE'];
1262     $new_base       = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1264     /* Fetch all objects and check if they do not exist anymore */
1265     $ui = get_userinfo();
1266     $tmp = array();
1267     $ldap_to->cd($new_base);
1268     $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1269     while($entry = $ldap_to->fetch()){
1271       $chk =  str_replace($new_base,"",$entry['dn']);
1272       if(preg_match("/,ou=/",$chk)) continue;
1274       if(!isset($entry['description'][0])){
1275         $entry['description'][0]  = "";
1276       }
1277       $tmp[] = $entry; 
1278     }
1280     /* Check if entry still exists */
1281     foreach($tmp as $key => $entry){
1282       $ldap->cat($entry['gosaSnapshotDN'][0]);
1283       if($ldap->count()){
1284         unset($tmp[$key]);
1285       }
1286     }
1288     /* Format result as requested */
1289     if($raw) {
1290       return($tmp);
1291     }else{
1292       $tmp2 = array();
1293       foreach($tmp as $key => $entry){
1294         $tmp2[base64_encode($entry['dn'])] = $entry['description'][0]; 
1295       }
1296     }
1297     return($tmp2);
1298   } 
1301   /* Restore selected snapshot */
1302   function restore_snapshot($dn)
1303   {
1304     if(!$this->snapshotEnabled()) return(array());
1306     $ldap= $this->config->get_ldap_link();
1307     $ldap->cd($this->config->current['BASE']);
1308     $cfg= &$this->config->current;
1310     /* check if there are special server configurations for snapshots */
1311     if(isset($cfg['SNAPSHOT_SERVER'])){
1312       $server       = $cfg['SNAPSHOT_SERVER'];
1313       $user         = $cfg['SNAPSHOT_USER'];
1314       $password     = $cfg['SNAPSHOT_PASSWORD'];
1315       $snapldapbase = $cfg['SNAPSHOT_BASE'];
1316       $ldap_to      = new LDAP($user,$password, $server);
1317       $ldap_to->cd ($snapldapbase);
1318       show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$snapldapbase));
1319     }else{
1320       $ldap_to    = $ldap;
1321     }
1323     /* Get the snapshot */ 
1324     $ldap_to->cat($dn);
1325     $restoreObject = $ldap_to->fetch();
1327     /* Prepare import string */
1328     $data  = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1330     /* Import the given data */
1331     $ldap->import_complete_ldif($data,$err,false,false);
1332     show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$dn));
1333   }
1336   function showSnapshotDialog($base,$baseSuffixe)
1337   {
1338     $once = true;
1339     foreach($_POST as $name => $value){
1341       /* Create a new snapshot, display a dialog */
1342       if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1343         $once = false;
1344         $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1345         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1346         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1347       }
1349       /* Restore a snapshot, display a dialog with all snapshots of the current object */
1350       if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1351         $once = false;
1352         $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1353         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1354         $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1355         $this->snapDialog->display_restore_dialog = true;
1356       }
1358       /* Restore one of the already deleted objects */
1359       if(((isset($_POST['menu_action']) && $_POST['menu_action'] == "RestoreDeletedSnapShot") 
1360           || preg_match("/^RestoreDeletedSnapShot_/",$name)) && $once){
1361         $once = false;
1362         $this->snapDialog = new SnapShotDialog($this->config,"",$this);
1363         $this->snapDialog->set_snapshot_bases($baseSuffixe);
1364         $this->snapDialog->display_restore_dialog      = true;
1365         $this->snapDialog->display_all_removed_objects  = true;
1366       }
1368       /* Restore selected snapshot */
1369       if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1370         $once = false;
1371         $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1372         $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1373         if(!empty($entry)){
1374           $this->restore_snapshot($entry);
1375           $this->snapDialog = NULL;
1376         }
1377       }
1378     }
1380     /* Create a new snapshot requested, check
1381        the given attributes and create the snapshot*/
1382     if(isset($_POST['CreateSnapshot']) && is_object($this->snapDialog)){
1383       $this->snapDialog->save_object();
1384       $msgs = $this->snapDialog->check();
1385       if(count($msgs)){
1386         foreach($msgs as $msg){
1387           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
1388         }
1389       }else{
1390         $this->dn =  $this->snapDialog->dn;
1391         $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1392         $this->snapDialog = NULL;
1393       }
1394     }
1396     /* Restore is requested, restore the object with the posted dn .*/
1397     if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1398     }
1400     if(isset($_POST['CancelSnapshot'])){
1401       $this->snapDialog = NULL;
1402     }
1404     if(is_object($this->snapDialog )){
1405       $this->snapDialog->save_object();
1406       return($this->snapDialog->execute());
1407     }
1408   }
1411   static function plInfo()
1412   {
1413     return array();
1414   }
1417   function set_acl_base($base)
1418   {
1419     $this->acl_base= $base;
1420   }
1423   function set_acl_category($category)
1424   {
1425     $this->acl_category= "$category/";
1426   }
1429   function acl_is_writeable($attribute,$skip_write = FALSE)
1430   {
1431     $ui= get_userinfo();
1432     return preg_match('/w/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute, $skip_write));
1433   }
1436   function acl_is_readable($attribute)
1437   {
1438     $ui= get_userinfo();
1439     return preg_match('/r/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute));
1440   }
1443   function acl_is_createable()
1444   {
1445     $ui= get_userinfo();
1446     return preg_match('/c/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1447   }
1450   function acl_is_removeable()
1451   {
1452     $ui= get_userinfo();
1453     return preg_match('/d/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1454   }
1457   function acl_is_moveable()
1458   {
1459     $ui= get_userinfo();
1460     return preg_match('/m/', $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), '0'));
1461   }
1464   function acl_have_any_permissions()
1465   {
1466   }
1469   function getacl($attribute,$skip_write= FALSE)
1470   {
1471     $ui= get_userinfo();
1472     return  $ui->get_permissions($this->acl_base, $this->acl_category.get_class($this), $attribute,$skip_write);
1473   }
1475   /* Get all allowed bases to move an object to or to create a new object.
1476      Idepartments also contains all base departments which lead to the allowed bases */
1477   function get_allowed_bases($category = "")
1478   {
1479     $ui = get_userinfo();
1480     $deps = array();
1482     /* Set category */ 
1483     if(empty($category)){
1484       $category = $this->acl_category.get_class($this);
1485     }
1487     /* Is this a new object ? Or just an edited existing object */
1488     if(!$this->initially_was_account && $this->is_account){
1489       $new = true;
1490     }else{
1491       $new = false;
1492     }
1494     $cat_bases = $ui->get_module_departments(preg_replace("/\/.*$/","",$category));
1495     foreach($this->config->idepartments as $dn => $name){
1496       
1497       if(!in_array_ics($dn,$cat_bases)){
1498         continue;
1499       }
1500       
1501       $acl = $ui->get_permissions($dn,$category);
1502       if($new && preg_match("/c/",$acl)){
1503         $deps[$dn] = $name;
1504       }elseif(!$new && preg_match("/m/",$acl)){
1505         $deps[$dn] = $name;
1506       }
1507     }
1509     /* Add current base */      
1510     if(isset($this->base) && isset($this->config->idepartments[$this->base])){
1511       $deps[$this->base] = $this->config->idepartments[$this->base];
1512     }else{
1513       trigger_error("No default base found in class ".get_class($this).". ".$this->base);
1514     }
1515     return($deps);
1516   }
1519   /* This function modifies object acls too, if an object is moved.
1520    *  $old_dn   specifies the actually used dn
1521    *  $new_dn   specifies the destiantion dn
1522    */
1523   function update_acls($old_dn,$new_dn,$output_changes = FALSE)
1524   {
1525     /* Check if old_dn is empty. This should never happen */
1526     if(empty($old_dn) || empty($new_dn)){
1527       trigger_error("Failed to check acl dependencies, wrong dn given.");
1528       return;
1529     }
1531     /* Update userinfo if necessary */
1532     $ui = session::get('ui');
1533     if($ui->dn == $old_dn){
1534       $ui->dn = $new_dn;
1535       session::set('ui',$ui);
1536       new log("view","acl/".get_class($this),$this->dn,array(),"Updated current user dn from '".$old_dn."' to '".$new_dn."'");
1537     }
1539     /* Object was moved, ensure that all acls will be moved too */
1540     if($new_dn != $old_dn && $old_dn != "new"){
1542       /* get_ldap configuration */
1543       $update = array();
1544       $ldap = $this->config->get_ldap_link();
1545       $ldap->cd ($this->config->current['BASE']);
1546       $ldap->search("(&(objectClass=gosaAcl)(gosaAclEntry=*))",array("cn","gosaAclEntry"));
1547       while($attrs = $ldap->fetch()){
1549         $acls = array();
1551         /* Walk through acls */
1552         for($i = 0 ; $i <  $attrs['gosaAclEntry']['count'] ; $i ++ ){
1554           /* Reset vars */
1555           $found = false;
1557           /* Get Acl parts */
1558           $acl_parts = split(":",$attrs['gosaAclEntry'][$i]);
1560           /* Get every single member for this acl */  
1561           $members = array();  
1562           if(preg_match("/,/",$acl_parts[2])){
1563             $members = split(",",$acl_parts[2]);
1564           }else{
1565             $members = array($acl_parts[2]);
1566           } 
1567       
1568           /* Check if member match current dn */
1569           foreach($members as $key => $member){
1570             $member = base64_decode($member);
1571             if($member == $old_dn){
1572               $found = true;
1573               $members[$key] = base64_encode($new_dn);
1574             }
1575           } 
1576          
1577           /* Create new member string */ 
1578           $new_members = "";
1579           foreach($members as $member){
1580             $new_members .= $member.",";
1581           }
1582           $new_members = preg_replace("/,$/","",$new_members);
1583           $acl_parts[2] = $new_members;
1584         
1585           /* Reconstruckt acl entry */
1586           $acl_str  ="";
1587           foreach($acl_parts as $t){
1588            $acl_str .= $t.":";
1589           }
1590           $acl_str = preg_replace("/:$/","",$acl_str);
1591        }
1593        /* Acls for this object must be adjusted */
1594        if($found){
1596           $debug_info=  _("Changing ACL dn")."&nbsp;:&nbsp;<br>&nbsp;-"._("from")."&nbsp;<b>&nbsp;".
1597                   $old_dn."</b><br>&nbsp;-"._("to")."&nbsp;<b>".$new_dn."</b><br>";
1598           @DEBUG (DEBUG_ACL, __LINE__, __FUNCTION__, __FILE__,$debug_info,"ACL");
1600           $update[$attrs['dn']] =array();
1601           foreach($acls as $acl){
1602             $update[$attrs['dn']]['gosaAclEntry'][] = $acl;
1603           }
1604         }
1605       }
1607       /* Write updated acls */
1608       foreach($update as $dn => $attrs){
1609         $ldap->cd($dn);
1610         $ldap->modify($attrs);
1611       }
1612     }
1613   }
1615   
1617   /* This function enables the entry Serial ID check.
1618    * If an entry was edited while we have edited the entry too,
1619    *  an error message will be shown. 
1620    * To configure this check correctly read the FAQ.
1621    */    
1622   function enable_CSN_check()
1623   {
1624     $this->CSN_check_active =TRUE;
1625     $this->entryCSN = getEntryCSN($this->dn);
1626   }
1629   /*! \brief  Prepares the plugin to be used for multiple edit
1630    *          Update plugin attributes with given array of attribtues.
1631    *  @param  array   Array with attributes that must be updated.
1632    */
1633   function init_multiple_support($attrs,$all)
1634   {
1635     $ldap= $this->config->get_ldap_link();
1636     $this->multi_attrs    = $attrs;
1637     $this->multi_attrs_all= $all;
1639     /* Copy needed attributes */
1640     foreach ($this->attributes as $val){
1641       $found= array_key_ics($val, $this->multi_attrs);
1642       if ($found != ""){
1643         if(isset($this->multi_attrs["$found"][0])){
1644           $this->$val= $this->multi_attrs["$found"][0];
1645         }
1646       }
1647     }
1648   }
1650  
1651   /*! \brief  Enables multiple support for this plugin
1652    */
1653   function enable_multiple_support()
1654   {
1655     $this->ignore_account = TRUE;
1656     $this->multiple_support_active = TRUE;
1657   }
1660   /*! \brief  Returns all values that have been modfied in multiple edit mode.
1661       @return array Cotaining all mdofied values. 
1662    */
1663   function get_multi_edit_values()
1664   {
1665     $ret = array();
1666     foreach($this->attributes as $attr){
1667       if(in_array($attr,$this->multi_boxes)){
1668         $ret[$attr] = $this->$attr;
1669       }
1670     }
1671     return($ret);
1672   }
1674   
1675   /*! \brief  Update class variables with values collected by multiple edit.
1676    */
1677   function set_multi_edit_values($attrs)
1678   {
1679     foreach($attrs as $name => $value){
1680       $this->$name = $value;
1681     }
1682   }
1685   /*! \brief execute plugin
1687     Generates the html output for this node
1688    */
1689   function multiple_execute()
1690   {
1691     /* This one is empty currently. Fabian - please fill in the docu code */
1692     session::set('current_class_for_help',get_class($this));
1694     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
1695     session::set('LOCK_VARS_TO_USE',array());
1696     session::set('LOCK_VARS_USED',array());
1697     
1698     return("Multiple edit is currently not implemented for this plugin.");
1699   }
1702   /*! \brief   Save HTML posted data to object for multiple edit
1703    */
1704   function multiple_save_object()
1705   {
1706     if(empty($this->entryCSN) && $this->CSN_check_active){
1707       $this->entryCSN = getEntryCSN($this->dn);
1708     }
1710     /* Save values to object */
1711     $this->multi_boxes = array();
1712     foreach ($this->attributes as $val){
1713   
1714       /* Get selected checkboxes from multiple edit */
1715       if(isset($_POST["use_".$val])){
1716         $this->multi_boxes[] = $val;
1717       }
1719       if ($this->acl_is_writeable($val) && isset ($_POST["$val"])){
1721         /* Check for modifications */
1722         if (get_magic_quotes_gpc()) {
1723           $data= stripcslashes($_POST["$val"]);
1724         } else {
1725           $data= $this->$val = $_POST["$val"];
1726         }
1727         if ($this->$val != $data){
1728           $this->is_modified= TRUE;
1729         }
1730     
1731         /* IE post fix */
1732         if(isset($data[0]) && $data[0] == chr(194)) {
1733           $data = "";  
1734         }
1735         $this->$val= $data;
1736       }
1737     }
1738   }
1741   /*! \brief  Returns all attributes of this plugin, 
1742                to be able to detect multiple used attributes 
1743                in multi_plugg::detect_multiple_used_attributes().
1744       @return array Attributes required for intialization of multi_plug
1745    */
1746   public function get_multi_init_values()
1747   {
1748     $attrs = $this->attrs;
1749     return($attrs);
1750   }
1753   /*! \brief  Check given values in multiple edit
1754       @return array Error messages
1755    */
1756   function multiple_check()
1757   {
1758     $message = plugin::check();
1759     return($message);
1760   }
1763 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1764 ?>