Code

Updated plugin
[gosa.git] / include / class_plugin.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 /*! \brief   The plugin base class
22   \author  Cajus Pollmeier <pollmeier@gonicus.de>
23   \version 2.00
24   \date    24.07.2003
26   This is the base class for all plugins. It can be used standalone or
27   can be included by the tabs class. All management should be done 
28   within this class. Extend your plugins from this class.
29  */
31 class plugin
32 {
33   /*!
34     \brief Reference to parent object
36     This variable is used when the plugin is included in tabs
37     and keeps reference to the tab class. Communication to other
38     tabs is possible by 'name'. So the 'fax' plugin can ask the
39     'userinfo' plugin for the fax number.
41     \sa tab
42    */
43   var $parent= NULL;
45   /*!
46     \brief Configuration container
48     Access to global configuration
49    */
50   var $config= NULL;
52   /*!
53     \brief Mark plugin as account
55     Defines whether this plugin is defined as an account or not.
56     This has consequences for the plugin to be saved from tab
57     mode. If it is set to 'FALSE' the tab will call the delete
58     function, else the save function. Should be set to 'TRUE' if
59     the construtor detects a valid LDAP object.
61     \sa plugin::plugin()
62    */
63   var $is_account= FALSE;
64   var $initially_was_account= FALSE;
66   /*!
67     \brief Mark plugin as template
69     Defines whether we are creating a template or a normal object.
70     Has conseqences on the way execute() shows the formular and how
71     save() puts the data to LDAP.
73     \sa plugin::save() plugin::execute()
74    */
75   var $is_template= FALSE;
76   var $ignore_account= FALSE;
77   var $is_modified= FALSE;
79   /*!
80     \brief Represent temporary LDAP data
82     This is only used internally.
83    */
84   var $attrs= array();
86   /* Keep set of conflicting plugins */
87   var $conflicts= array();
89   /* Save unit tags */
90   var $gosaUnitTag= "";
92   /*!
93     \brief Used standard values
95     dn
96    */
97   var $dn= "";
98   var $uid= "";
99   var $sn= "";
100   var $givenName= "";
101   var $acl= "*none*";
102   var $dialog= FALSE;
104   /* attribute list for save action */
105   var $attributes= array();
106   var $objectclasses= array();
107   var $new= TRUE;
108   var $saved_attributes= array();
110   /* This can be set to render the tabulators in another stylesheet */
111   var $pl_notify= FALSE;
113   /*! \brief plugin constructor
115     If 'dn' is set, the node loads the given 'dn' from LDAP
117     \param dn Distinguished name to initialize plugin from
118     \sa plugin()
119    */
120   function plugin ($config, $dn= NULL, $parent= NULL)
121   {
122     /* Configuration is fine, allways */
123     $this->config= $config;     
124     $this->dn= $dn;
126     /* Handle new accounts, don't read information from LDAP */
127     if ($dn == "new"){
128       return;
129     }
131     /* Get LDAP descriptor */
132     $ldap= $this->config->get_ldap_link();
133     if ($dn != NULL){
135       /* Load data to 'attrs' and save 'dn' */
136       if ($parent != NULL){
137         $this->attrs= $parent->attrs;
138       } else {
139         $ldap->cat ($dn);
140         $this->attrs= $ldap->fetch();
141       }
143       /* Copy needed attributes */
144       foreach ($this->attributes as $val){
145         $found= array_key_ics($val, $this->attrs);
146         if ($found != ""){
147           $this->$val= $this->attrs["$found"][0];
148         }
149       }
151       /* gosaUnitTag loading... */
152       if (isset($this->attrs['gosaUnitTag'][0])){
153         $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
154       }
156       /* Set the template flag according to the existence of objectClass
157          gosaUserTemplate */
158       if (isset($this->attrs['objectClass'])){
159         if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
160           $this->is_template= TRUE;
161           @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
162               "found", "Template check");
163         }
164       }
166       /* Is Account? */
167       error_reporting(0);
168       $found= TRUE;
169       foreach ($this->objectclasses as $obj){
170         if (preg_match('/top/i', $obj)){
171           continue;
172         }
173         if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
174           $found= FALSE;
175           break;
176         }
177       }
178       error_reporting(E_ALL);
179       if ($found){
180         $this->is_account= TRUE;
181         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
182             "found", "Object check");
183       }
185       /* Prepare saved attributes */
186       $this->saved_attributes= $this->attrs;
187       foreach ($this->saved_attributes as $index => $value){
188         if (preg_match('/^[0-9]+$/', $index)){
189           unset($this->saved_attributes[$index]);
190           continue;
191         }
192         if (!in_array($index, $this->attributes) && $index != "objectClass"){
193           unset($this->saved_attributes[$index]);
194           continue;
195         }
196         if ($this->saved_attributes[$index]["count"] == 1){
197           $tmp= $this->saved_attributes[$index][0];
198           unset($this->saved_attributes[$index]);
199           $this->saved_attributes[$index]= $tmp;
200           continue;
201         }
203         unset($this->saved_attributes["$index"]["count"]);
204       }
205     }
207     /* Save initial account state */
208     $this->initially_was_account= $this->is_account;
209   }
211   /*! \brief execute plugin
213     Generates the html output for this node
214    */
215   function execute()
216   {
217     # This one is empty currently. Fabian - please fill in the docu code
218     $_SESSION['current_class_for_help'] = get_class($this);
219     /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
220     $_SESSION['LOCK_VARS_TO_USE'] =array();
221   }
223   /*! \brief execute plugin
224      Removes object from parent
225    */
226   function remove_from_parent()
227   {
228     /* include global link_info */
229     $ldap= $this->config->get_ldap_link();
231     /* Get current objectClasses in order to add the required ones */
232     $ldap->cat($this->dn);
233     $tmp= $ldap->fetch ();
234     if (isset($tmp['objectClass'])){
235       $oc= $tmp['objectClass'];
236     } else {
237       $oc= array("count" => 0);
238     }
240     /* Remove objectClasses from entry */
241     $ldap->cd($this->dn);
242     $this->attrs= array();
243     $this->attrs['objectClass']= array();
244     for ($i= 0; $i<$oc["count"]; $i++){
245       if (!in_array_ics($oc[$i], $this->objectclasses)){
246         $this->attrs['objectClass'][]= $oc[$i];
247       }
248     }
250     /* Unset attributes from entry */
251     foreach ($this->attributes as $val){
252       $this->attrs["$val"]= array();
253     }
255     /* Unset account info */
256     $this->is_account= FALSE;
258     /* Do not write in plugin base class, this must be done by
259        children, since there are normally additional attribs,
260        lists, etc. */
261     /*
262        $ldap->modify($this->attrs);
263      */
264   }
267   /* Save data to object */
268   function save_object()
269   {
270     /* Save values to object */
271     foreach ($this->attributes as $val){
272       if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
273         /* Check for modifications */
274         if ((get_magic_quotes_gpc()) && !is_array($_POST["$val"])) {
275           $data= stripcslashes($_POST["$val"]);
276         } else {
277           $data= $this->$val = $_POST["$val"];
278         }
279         if ($this->$val != $data){
280           $this->is_modified= TRUE;
281         }
282     
283         /* Okay, how can I explain this fix ... 
284          * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds. 
285          * So IE posts these 'unselectable' option, with value = chr(194) 
286          * chr(194) seems to be the &nbsp; in between the ...option>&nbsp;</option.. because there is no value=".." specified in these option fields  
287          * This &nbsp; was added for W3c compliance, but now causes these ... ldap errors ... 
288          * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
289          */
290         if(isset($data[0]) && $data[0] == chr(194)) {
291           $data = "";  
292         }
293         $this->$val= $data;
294       }
295     }
296   }
299   /* Save data to LDAP, depending on is_account we save or delete */
300   function save()
301   {
302     /* include global link_info */
303     $ldap= $this->config->get_ldap_link();
305     /* Start with empty array */
306     $this->attrs= array();
308     /* Get current objectClasses in order to add the required ones */
309     $ldap->cat($this->dn);
310     
311     $tmp= $ldap->fetch ();
312     
313     if (isset($tmp['objectClass'])){
314       $oc= $tmp["objectClass"];
315       $this->new= FALSE;
316     } else {
317       $oc= array("count" => 0);
318       $this->new= TRUE;
319     }
321     /* Load (minimum) attributes, add missing ones */
322     $this->attrs['objectClass']= $this->objectclasses;
323     for ($i= 0; $i<$oc["count"]; $i++){
324       if (!in_array_ics($oc[$i], $this->objectclasses)){
325         $this->attrs['objectClass'][]= $oc[$i];
326       }
327     }
329     /* Copy standard attributes */
330     foreach ($this->attributes as $val){
331       if ($this->$val != ""){
332         $this->attrs["$val"]= $this->$val;
333       } elseif (!$this->new) {
334         $this->attrs["$val"]= array();
335       }
336     }
338   }
341   function cleanup()
342   {
343     foreach ($this->attrs as $index => $value){
345       /* Convert arrays with one element to non arrays, if the saved
346          attributes are no array, too */
347       if (is_array($this->attrs[$index]) && 
348           count ($this->attrs[$index]) == 1 &&
349           isset($this->saved_attributes[$index]) &&
350           !is_array($this->saved_attributes[$index])){
351           
352         $tmp= $this->attrs[$index][0];
353         $this->attrs[$index]= $tmp;
354       }
356       /* Remove emtpy arrays if they do not differ */
357       if (is_array($this->attrs[$index]) &&
358           count($this->attrs[$index]) == 0 &&
359           !isset($this->saved_attributes[$index])){
360           
361         unset ($this->attrs[$index]);
362         continue;
363       }
365       /* Remove single attributes that do not differ */
366       if (!is_array($this->attrs[$index]) &&
367           isset($this->saved_attributes[$index]) &&
368           !is_array($this->saved_attributes[$index]) &&
369           $this->attrs[$index] == $this->saved_attributes[$index]){
371         unset ($this->attrs[$index]);
372         continue;
373       }
375       /* Remove arrays that do not differ */
376       if (is_array($this->attrs[$index]) && 
377           isset($this->saved_attributes[$index]) &&
378           is_array($this->saved_attributes[$index])){
379           
380         if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
381           unset ($this->attrs[$index]);
382           continue;
383         }
384       }
385     }
387     /* Update saved attributes and ensure that next cleanups will be successful too */
388     foreach($this->attrs as $name => $value){
389       $this->saved_attributes[$name] = $value;
390     }
391   }
393   /* Check formular input */
394   function check()
395   {
396     $message= array();
398     /* Skip if we've no config object */
399     if (!isset($this->config)){
400       return $message;
401     }
403     /* Find hooks entries for this class */
404     $command= search_config($this->config->data['MENU'], get_class($this), "CHECK");
405     if ($command == "" && isset($this->config->data['TABS'])){
406       $command= search_config($this->config->data['TABS'], get_class($this), "CHECK");
407     }
409     if ($command != ""){
411       if (!check_command($command)){
412         $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
413                             get_class($this));
414       } else {
416         /* Generate "ldif" for check hook */
417         $ldif= "dn: $this->dn\n";
418         
419         /* ... objectClasses */
420         foreach ($this->objectclasses as $oc){
421           $ldif.= "objectClass: $oc\n";
422         }
423         
424         /* ... attributes */
425         foreach ($this->attributes as $attr){
426           if ($this->$attr == ""){
427             continue;
428           }
429           if (is_array($this->$attr)){
430             foreach ($this->$attr as $val){
431               $ldif.= "$attr: $val\n";
432             }
433           } else {
434               $ldif.= "$attr: ".$this->$attr."\n";
435           }
436         }
438         /* Append empty line */
439         $ldif.= "\n";
441         /* Feed "ldif" into hook and retrieve result*/
442         $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
443         $fh= proc_open($command, $descriptorspec, $pipes);
444         if (is_resource($fh)) {
445           fwrite ($pipes[0], $ldif);
446           fclose($pipes[0]);
447           
448           $result= stream_get_contents($pipes[1]);
449           if ($result != ""){
450             $message[]= $result;
451           }
452           
453           fclose($pipes[1]);
454           fclose($pipes[2]);
455           proc_close($fh);
456         }
457       }
459     }
461     return ($message);
462   }
464   /* Adapt from template, using 'dn' */
465   function adapt_from_template($dn)
466   {
467     /* Include global link_info */
468     $ldap= $this->config->get_ldap_link();
470     /* Load requested 'dn' to 'attrs' */
471     $ldap->cat ($dn);
472     $this->attrs= $ldap->fetch();
474     /* Walk through attributes */
475     foreach ($this->attributes as $val){
477       if (isset($this->attrs["$val"][0])){
479         /* If attribute is set, replace dynamic parts: 
480            %sn, %givenName and %uid. Fill these in our local variables. */
481         $value= $this->attrs["$val"][0];
483         foreach (array("sn", "givenName", "uid") as $repl){
484           if (preg_match("/%$repl/i", $value)){
485             $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
486           }
487         }
488         $this->$val= $value;
489       }
490     }
492     /* Is Account? */
493     $found= TRUE;
494     foreach ($this->objectclasses as $obj){
495       if (preg_match('/top/i', $obj)){
496         continue;
497       }
498       if (!in_array_ics ($obj, $this->attrs['objectClass'])){
499         $found= FALSE;
500         break;
501       }
502     }
503     if ($found){
504       $this->is_account= TRUE;
505     }
506   }
508   /* Indicate whether a password change is needed or not */
509   function password_change_needed()
510   {
511     return FALSE;
512   }
514   /* Show header message for tab dialogs */
515   function show_header($button_text, $text, $disabled= FALSE)
516   {
517     $state = "disabled";
518     if($this->is_account && $this->acl == "#all#"){
519       $state= "";
520     }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
521       $state= "";
522     }
524     if ($disabled == TRUE){
525       $state= "disabled";
526     }
528     $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
529     $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.">".
530                 "<p class=\"seperator\">&nbsp;</p></td></tr></table>";
532     return($display);
533   }
535   function postcreate($add_attrs= array())
536   {
537     /* Find postcreate entries for this class */
538     $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
539     if ($command == "" && isset($this->config->data['TABS'])){
540       $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
541     }
543     if ($command != ""){
544       /* Walk through attribute list */
545       foreach ($this->attributes as $attr){
546         if (!is_array($this->$attr)){
547           $command= preg_replace("/%$attr/", $this->$attr, $command);
548         }
549       }
550       $command= preg_replace("/%dn/", $this->dn, $command);
552       /* Additional attributes */
553       foreach ($add_attrs as $name => $value){
554         $command= preg_replace("/%$name/", $value, $command);
555       }
557       if (check_command($command)){
558         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
559             $command, "Execute");
561         exec($command);
562       } else {
563         $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
564         print_red ($message);
565       }
566     }
567   }
569   function postmodify($add_attrs= array())
570   {
571     /* Find postcreate entries for this class */
572     $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
573     if ($command == "" && isset($this->config->data['TABS'])){
574       $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
575     }
577     if ($command != ""){
578       /* Walk through attribute list */
579       foreach ($this->attributes as $attr){
580         if (!is_array($this->$attr)){
581           $command= preg_replace("/%$attr/", $this->$attr, $command);
582         }
583       }
584       $command= preg_replace("/%dn/", $this->dn, $command);
586       /* Additional attributes */
587       foreach ($add_attrs as $name => $value){
588         $command= preg_replace("/%$name/", $value, $command);
589       }
591       if (check_command($command)){
592         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
593             $command, "Execute");
595         exec($command);
596       } else {
597         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
598         print_red ($message);
599       }
600     }
601   }
603   function postremove($add_attrs= array())
604   {
605     /* Find postremove entries for this class */
606     $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
607     if ($command == "" && isset($this->config->data['TABS'])){
608       $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
609     }
611     if ($command != ""){
612       /* Walk through attribute list */
613       foreach ($this->attributes as $attr){
614         if (!is_array($this->$attr)){
615           $command= preg_replace("/%$attr/", $this->$attr, $command);
616         }
617       }
618       $command= preg_replace("/%dn/", $this->dn, $command);
620       /* Additional attributes */
621       foreach ($add_attrs as $name => $value){
622         $command= preg_replace("/%$name/", $value, $command);
623       }
625       if (check_command($command)){
626         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
627             $command, "Execute");
629         exec($command);
630       } else {
631         $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
632         print_red ($message);
633       }
634     }
635   }
637   /* Create unique DN */
638   function create_unique_dn($attribute, $base)
639   {
640     $ldap= $this->config->get_ldap_link();
641     $base= preg_replace("/^,*/", "", $base);
643     /* Try to use plain entry first */
644     $dn= "$attribute=".$this->$attribute.",$base";
645     $ldap->cat ($dn, array('dn'));
646     if (!$ldap->fetch()){
647       return ($dn);
648     }
650     /* Look for additional attributes */
651     foreach ($this->attributes as $attr){
652       if ($attr == $attribute || $this->$attr == ""){
653         continue;
654       }
656       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
657       $ldap->cat ($dn, array('dn'));
658       if (!$ldap->fetch()){
659         return ($dn);
660       }
661     }
663     /* None found */
664     return ("none");
665   }
667   function rebind($ldap, $referral)
668   {
669     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
670     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
671       $this->error = "Success";
672       $this->hascon=true;
673       $this->reconnect= true;
674       return (0);
675     } else {
676       $this->error = "Could not bind to " . $credentials['ADMIN'];
677       return NULL;
678     }
679   }
681   /* This is a workaround function. */
682   function copy($src_dn, $dst_dn)
683   {
684     /* Rename dn in possible object groups */
685     $ldap= $this->config->get_ldap_link();
686     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
687         array('cn'));
688     while ($attrs= $ldap->fetch()){
689       $og= new ogroup($this->config, $ldap->getDN());
690       unset($og->member[$src_dn]);
691       $og->member[$dst_dn]= $dst_dn;
692       $og->save ();
693     }
695     $ldap->cat($dst_dn);
696     $attrs= $ldap->fetch();
697     if (count($attrs)){
698       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
699           E_USER_WARNING);
700       return (FALSE);
701     }
703     $ldap->cat($src_dn);
704     $attrs= $ldap->fetch();
705     if (!count($attrs)){
706       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
707           E_USER_WARNING);
708       return (FALSE);
709     }
711     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
712     $ds= ldap_connect($this->config->current['SERVER']);
713     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
714     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
715       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
716     }
718     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
719     error_reporting (0);
720     $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
722     /* Fill data from LDAP */
723     $new= array();
724     if ($sr) {
725       $ei=ldap_first_entry($ds, $sr);
726       if ($ei) {
727         foreach($attrs as $attr => $val){
728           if ($info = ldap_get_values_len($ds, $ei, $attr)){
729             for ($i= 0; $i<$info['count']; $i++){
730               if ($info['count'] == 1){
731                 $new[$attr]= $info[$i];
732               } else {
733                 $new[$attr][]= $info[$i];
734               }
735             }
736           }
737         }
738       }
739     }
741     /* close conncetion */
742     error_reporting (E_ALL);
743     ldap_unbind($ds);
745     /* Adapt naming attribute */
746     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
747     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
748     $new[$dst_name]= @LDAP::fix($dst_val);
750     /* Check if this is a department.
751      * If it is a dep. && there is a , override in his ou 
752      *  change \2C to , again, else this entry can't be saved ...
753      */
754     if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
755       $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
756     }
758     /* Save copy */
759     $ldap->connect();
760     $ldap->cd($this->config->current['BASE']);
761     
762     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
764     /* FAIvariable=.../..., cn=.. 
765         could not be saved, because the attribute FAIvariable was different to 
766         the dn FAIvariable=..., cn=... */
767     if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
768       $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
769     }
770     $ldap->cd($dst_dn);
771     $ldap->add($new);
773     if ($ldap->error != "Success"){
774       trigger_error("Trying to save $dst_dn failed.",
775           E_USER_WARNING);
776       return(FALSE);
777     }
779     return (TRUE);
780   }
783   function move($src_dn, $dst_dn)
784   {
785     /* Copy source to destination */
786     if (!$this->copy($src_dn, $dst_dn)){
787       return (FALSE);
788     }
790     /* Delete source */
791     $ldap= $this->config->get_ldap_link();
792     $ldap->rmdir($src_dn);
793     if ($ldap->error != "Success"){
794       trigger_error("Trying to delete $src_dn failed.",
795           E_USER_WARNING);
796       return (FALSE);
797     }
799     return (TRUE);
800   }
803   /* Move/Rename complete trees */
804   function recursive_move($src_dn, $dst_dn)
805   {
806     /* Check if the destination entry exists */
807     $ldap= $this->config->get_ldap_link();
809     /* Check if destination exists - abort */
810     $ldap->cat($dst_dn, array('dn'));
811     if ($ldap->fetch()){
812       trigger_error("recursive_move $dst_dn already exists.",
813           E_USER_WARNING);
814       return (FALSE);
815     }
817     /* Perform a search for all objects to be moved */
818     $objects= array();
819     $ldap->cd($src_dn);
820     $ldap->search("(objectClass=*)", array("dn"));
821     while($attrs= $ldap->fetch()){
822       $dn= $attrs['dn'];
823       $objects[$dn]= strlen($dn);
824     }
826     /* Sort objects by indent level */
827     asort($objects);
828     reset($objects);
830     /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
831     foreach ($objects as $object => $len){
832       $src= $object;
833       $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
834       if (!$this->copy($src, $dst)){
835         return (FALSE);
836       }
837     }
839     /* Remove src_dn */
840     $ldap->cd($src_dn);
841     $ldap->recursive_remove();
842     return (TRUE);
843   }
846   function handle_post_events($mode, $add_attrs= array())
847   {
848     switch ($mode){
849       case "add":
850         $this->postcreate($add_attrs);
851       break;
853       case "modify":
854         $this->postmodify($add_attrs);
855       break;
857       case "remove":
858         $this->postremove($add_attrs);
859       break;
860     }
861   }
864   function saveCopyDialog(){
865   }
868   function getCopyDialog(){
869     return(array("string"=>"","status"=>""));
870   }
873   function PrepareForCopyPaste($source){
874     $todo = $this->attributes;
875     if(isset($this->CopyPasteVars)){
876       $todo = array_merge($todo,$this->CopyPasteVars);
877     }
878     $todo[] = "is_account";
879     foreach($todo as $var){
880       if (isset($source->$var)){
881         $this->$var= $source->$var;
882       }
883     }
884   }
887   function handle_object_tagging($dn= "", $tag= "", $show= false)
888   {
889     //FIXME: How to optimize this? We have at least two
890     //       LDAP accesses per object. It would be a good
891     //       idea to have it integrated.
892   
893     /* No dn? Self-operation... */
894     if ($dn == ""){
895       $dn= $this->dn;
896     
897       /* No tag? Find it yourself... */
898       if ($tag == ""){
899         $len= strlen($dn);
901         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
902         $relevant= array();
903         foreach ($this->config->adepartments as $key => $ntag){
905           /* This one is bigger than our dn, its not relevant... */
906           if ($len <= strlen($key)){
907             continue;
908           }
910           /* This one matches with the latter part. Break and don't fix this entry */
911           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
912             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
913             $relevant[strlen($key)]= $ntag;
914             continue;
915           }
917         }
919         /* If we've some relevant tags to set, just get the longest one */
920         if (count($relevant)){
921           ksort($relevant);
922           $tmp= array_keys($relevant);
923           $idx= end($tmp);
924           $tag= $relevant[$idx];
925           $this->gosaUnitTag= $tag;
926         }
927       }
928     }
929  
931     /* Set tag? */
932     if ($tag != ""){
933       /* Set objectclass and attribute */
934       $ldap= $this->config->get_ldap_link();
935       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
936       $attrs= $ldap->fetch();
937       if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
938         if ($show) {
939           echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
940           flush();
941         }
942         return;
943       }
944       if (count($attrs)){
945         if ($show){
946           echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
947           flush();
948         }
949         $nattrs= array("gosaUnitTag" => $tag);
950         $nattrs['objectClass']= array();
951         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
952           $oc= $attrs['objectClass'][$i];
953           if ($oc != "gosaAdministrativeUnitTag"){
954             $nattrs['objectClass'][]= $oc;
955           }
956         }
957         $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
958         $ldap->cd($dn);
959         $ldap->modify($nattrs);
960         show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
961       } else {
962         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
963       }
964       
965     } else {
966       /* Remove objectclass and attribute */
967       $ldap= $this->config->get_ldap_link();
968       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
969       $attrs= $ldap->fetch();
970       if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
971         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
972         return;
973       }
974       if (count($attrs)){
975         if ($show){
976           echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
977           flush();
978         }
979         $nattrs= array("gosaUnitTag" => array());
980         $nattrs['objectClass']= array();
981         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
982           $oc= $attrs['objectClass'][$i];
983           if ($oc != "gosaAdministrativeUnitTag"){
984             $nattrs['objectClass'][]= $oc;
985           }
986         }
987         $ldap->cd($dn);
988         $ldap->modify($nattrs);
989         show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
990       } else {
991         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
992       }
993     }
994     
995   }
998   /* Add possibility to stop remove process */
999   function allow_remove()
1000   {
1001     $reason= "";
1002     return $reason;
1003   }
1006 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1007 ?>