Code

8e992dccc33c03b894848d7fe53767a16b168040
[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 != ""){
545       /* Additional attributes */
546       foreach ($add_attrs as $name => $value){
547         $command= preg_replace("/%$name/", $value, $command);
548       }
550       /* Walk through attribute list */
551       foreach ($this->attributes as $attr){
552         if (!is_array($this->$attr)){
553           $command= preg_replace("/%$attr/", $this->$attr, $command);
554         }
555       }
556       $command= preg_replace("/%dn/", $this->dn, $command);
558       if (check_command($command)){
559         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
560             $command, "Execute");
562         exec($command);
563       } else {
564         $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
565         print_red ($message);
566       }
567     }
568   }
570   function postmodify($add_attrs= array())
571   {
572     /* Find postcreate entries for this class */
573     $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
574     if ($command == "" && isset($this->config->data['TABS'])){
575       $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
576     }
578     if ($command != ""){
580       /* Additional attributes */
581       foreach ($add_attrs as $name => $value){
582         $command= preg_replace("/%$name/", $value, $command);
583       }
585       /* Walk through attribute list */
586       foreach ($this->attributes as $attr){
587         if (!is_array($this->$attr)){
588           $command= preg_replace("/%$attr/", $this->$attr, $command);
589         }
590       }
591       $command= preg_replace("/%dn/", $this->dn, $command);
593       if (check_command($command)){
594         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
595             $command, "Execute");
597         exec($command);
598       } else {
599         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
600         print_red ($message);
601       }
602     }
603   }
605   function postremove($add_attrs= array())
606   {
607     /* Find postremove entries for this class */
608     $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
609     if ($command == "" && isset($this->config->data['TABS'])){
610       $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
611     }
613     if ($command != ""){
615       /* Additional attributes */
616       foreach ($add_attrs as $name => $value){
617         $command= preg_replace("/%$name/", $value, $command);
618       }
620       /* Walk through attribute list */
621       foreach ($this->attributes as $attr){
622         if (!is_array($this->$attr)){
623           $command= preg_replace("/%$attr/", $this->$attr, $command);
624         }
625       }
626       $command= preg_replace("/%dn/", $this->dn, $command);
628       /* Additional attributes */
629       foreach ($add_attrs as $name => $value){
630         $command= preg_replace("/%$name/", $value, $command);
631       }
633       if (check_command($command)){
634         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
635             $command, "Execute");
637         exec($command);
638       } else {
639         $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
640         print_red ($message);
641       }
642     }
643   }
645   /* Create unique DN */
646   function create_unique_dn($attribute, $base)
647   {
648     $ldap= $this->config->get_ldap_link();
649     $base= preg_replace("/^,*/", "", $base);
651     /* Try to use plain entry first */
652     $dn= "$attribute=".$this->$attribute.",$base";
653     $ldap->cat ($dn, array('dn'));
654     if (!$ldap->fetch()){
655       return ($dn);
656     }
658     /* Look for additional attributes */
659     foreach ($this->attributes as $attr){
660       if ($attr == $attribute || $this->$attr == ""){
661         continue;
662       }
664       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
665       $ldap->cat ($dn, array('dn'));
666       if (!$ldap->fetch()){
667         return ($dn);
668       }
669     }
671     /* None found */
672     return ("none");
673   }
675   function rebind($ldap, $referral)
676   {
677     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
678     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
679       $this->error = "Success";
680       $this->hascon=true;
681       $this->reconnect= true;
682       return (0);
683     } else {
684       $this->error = "Could not bind to " . $credentials['ADMIN'];
685       return NULL;
686     }
687   }
689   /* This is a workaround function. */
690   function copy($src_dn, $dst_dn)
691   {
692     /* Rename dn in possible object groups */
693     $ldap= $this->config->get_ldap_link();
694     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
695         array('cn'));
696     while ($attrs= $ldap->fetch()){
697       $og= new ogroup($this->config, $ldap->getDN());
698       unset($og->member[$src_dn]);
699       $og->member[$dst_dn]= $dst_dn;
700       $og->save ();
701     }
703     $ldap->cat($dst_dn);
704     $attrs= $ldap->fetch();
705     if (count($attrs)){
706       trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
707           E_USER_WARNING);
708       return (FALSE);
709     }
711     $ldap->cat($src_dn);
712     $attrs= $ldap->fetch();
713     if (!count($attrs)){
714       trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
715           E_USER_WARNING);
716       return (FALSE);
717     }
719     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
720     $ds= ldap_connect($this->config->current['SERVER']);
721     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
722     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
723       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
724     }
726     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
727     error_reporting (0);
728     $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
730     /* Fill data from LDAP */
731     $new= array();
732     if ($sr) {
733       $ei=ldap_first_entry($ds, $sr);
734       if ($ei) {
735         foreach($attrs as $attr => $val){
736           if ($info = ldap_get_values_len($ds, $ei, $attr)){
737             for ($i= 0; $i<$info['count']; $i++){
738               if ($info['count'] == 1){
739                 $new[$attr]= $info[$i];
740               } else {
741                 $new[$attr][]= $info[$i];
742               }
743             }
744           }
745         }
746       }
747     }
749     /* close conncetion */
750     error_reporting (E_ALL);
751     ldap_unbind($ds);
753     /* Adapt naming attribute */
754     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
755     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
756     $new[$dst_name]= @LDAP::fix($dst_val);
758     /* Check if this is a department.
759      * If it is a dep. && there is a , override in his ou 
760      *  change \2C to , again, else this entry can't be saved ...
761      */
762     if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
763       $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
764     }
766     /* Save copy */
767     $ldap->connect();
768     $ldap->cd($this->config->current['BASE']);
769     
770     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
772     /* FAIvariable=.../..., cn=.. 
773         could not be saved, because the attribute FAIvariable was different to 
774         the dn FAIvariable=..., cn=... */
775     if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
776       $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
777     }
778     $ldap->cd($dst_dn);
779     $ldap->add($new);
781     if ($ldap->error != "Success"){
782       trigger_error("Trying to save $dst_dn failed.",
783           E_USER_WARNING);
784       return(FALSE);
785     }
787     return (TRUE);
788   }
791   function move($src_dn, $dst_dn)
792   {
793     /* Copy source to destination */
794     if (!$this->copy($src_dn, $dst_dn)){
795       return (FALSE);
796     }
798     /* Delete source */
799     $ldap= $this->config->get_ldap_link();
800     $ldap->rmdir($src_dn);
801     if ($ldap->error != "Success"){
802       trigger_error("Trying to delete $src_dn failed.",
803           E_USER_WARNING);
804       return (FALSE);
805     }
807     return (TRUE);
808   }
811   /* Move/Rename complete trees */
812   function recursive_move($src_dn, $dst_dn)
813   {
814     /* Check if the destination entry exists */
815     $ldap= $this->config->get_ldap_link();
817     /* Check if destination exists - abort */
818     $ldap->cat($dst_dn, array('dn'));
819     if ($ldap->fetch()){
820       trigger_error("recursive_move $dst_dn already exists.",
821           E_USER_WARNING);
822       return (FALSE);
823     }
825     /* Perform a search for all objects to be moved */
826     $objects= array();
827     $ldap->cd($src_dn);
828     $ldap->search("(objectClass=*)", array("dn"));
829     while($attrs= $ldap->fetch()){
830       $dn= $attrs['dn'];
831       $objects[$dn]= strlen($dn);
832     }
834     /* Sort objects by indent level */
835     asort($objects);
836     reset($objects);
838     /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
839     foreach ($objects as $object => $len){
840       $src= $object;
841       $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
842       if (!$this->copy($src, $dst)){
843         return (FALSE);
844       }
845     }
847     /* Remove src_dn */
848     $ldap->cd($src_dn);
849     $ldap->recursive_remove();
850     return (TRUE);
851   }
854   function handle_post_events($mode, $add_attrs= array())
855   {
856     switch ($mode){
857       case "add":
858         $this->postcreate($add_attrs);
859       break;
861       case "modify":
862         $this->postmodify($add_attrs);
863       break;
865       case "remove":
866         $this->postremove($add_attrs);
867       break;
868     }
869   }
872   function saveCopyDialog(){
873   }
876   function getCopyDialog(){
877     return(array("string"=>"","status"=>""));
878   }
881   function PrepareForCopyPaste($source){
882     $todo = $this->attributes;
883     if(isset($this->CopyPasteVars)){
884       $todo = array_merge($todo,$this->CopyPasteVars);
885     }
886     $todo[] = "is_account";
887     foreach($todo as $var){
888       if (isset($source->$var)){
889         $this->$var= $source->$var;
890       }
891     }
892   }
895   function handle_object_tagging($dn= "", $tag= "", $show= false)
896   {
897     //FIXME: How to optimize this? We have at least two
898     //       LDAP accesses per object. It would be a good
899     //       idea to have it integrated.
900   
901     /* No dn? Self-operation... */
902     if ($dn == ""){
903       $dn= $this->dn;
904     
905       /* No tag? Find it yourself... */
906       if ($tag == ""){
907         $len= strlen($dn);
909         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
910         $relevant= array();
911         foreach ($this->config->adepartments as $key => $ntag){
913           /* This one is bigger than our dn, its not relevant... */
914           if ($len <= strlen($key)){
915             continue;
916           }
918           /* This one matches with the latter part. Break and don't fix this entry */
919           if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
920             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
921             $relevant[strlen($key)]= $ntag;
922             continue;
923           }
925         }
927         /* If we've some relevant tags to set, just get the longest one */
928         if (count($relevant)){
929           ksort($relevant);
930           $tmp= array_keys($relevant);
931           $idx= end($tmp);
932           $tag= $relevant[$idx];
933           $this->gosaUnitTag= $tag;
934         }
935       }
936     }
937  
939     /* Set tag? */
940     if ($tag != ""){
941       /* Set objectclass and attribute */
942       $ldap= $this->config->get_ldap_link();
943       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
944       $attrs= $ldap->fetch();
945       if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
946         if ($show) {
947           echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
948           flush();
949         }
950         return;
951       }
952       if (count($attrs)){
953         if ($show){
954           echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
955           flush();
956         }
957         $nattrs= array("gosaUnitTag" => $tag);
958         $nattrs['objectClass']= array();
959         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
960           $oc= $attrs['objectClass'][$i];
961           if ($oc != "gosaAdministrativeUnitTag"){
962             $nattrs['objectClass'][]= $oc;
963           }
964         }
965         $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
966         $ldap->cd($dn);
967         $ldap->modify($nattrs);
968         show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
969       } else {
970         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
971       }
972       
973     } else {
974       /* Remove objectclass and attribute */
975       $ldap= $this->config->get_ldap_link();
976       $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
977       $attrs= $ldap->fetch();
978       if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
979         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
980         return;
981       }
982       if (count($attrs)){
983         if ($show){
984           echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
985           flush();
986         }
987         $nattrs= array("gosaUnitTag" => array());
988         $nattrs['objectClass']= array();
989         for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
990           $oc= $attrs['objectClass'][$i];
991           if ($oc != "gosaAdministrativeUnitTag"){
992             $nattrs['objectClass'][]= $oc;
993           }
994         }
995         $ldap->cd($dn);
996         $ldap->modify($nattrs);
997         show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
998       } else {
999         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
1000       }
1001     }
1002     
1003   }
1006   /* Add possibility to stop remove process */
1007   function allow_remove()
1008   {
1009     $reason= "";
1010     return $reason;
1011   }
1014 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1015 ?>