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 }
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 in between the ...option> </option.. because there is no value=".." specified in these option fields
287 * This 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);
311 $tmp= $ldap->fetch ();
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])){
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])){
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])){
380 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
381 unset ($this->attrs[$index]);
382 continue;
383 }
384 }
385 }
386 }
388 /* Check formular input */
389 function check()
390 {
391 $message= array();
393 /* Skip if we've no config object */
394 if (!isset($this->config)){
395 return $message;
396 }
398 /* Find hooks entries for this class */
399 $command= search_config($this->config->data['MENU'], get_class($this), "CHECK");
400 if ($command == "" && isset($this->config->data['TABS'])){
401 $command= search_config($this->config->data['TABS'], get_class($this), "CHECK");
402 }
404 if ($command != ""){
406 if (!check_command($command)){
407 $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
408 get_class($this));
409 } else {
411 /* Generate "ldif" for check hook */
412 $ldif= "dn: $this->dn\n";
414 /* ... objectClasses */
415 foreach ($this->objectclasses as $oc){
416 $ldif.= "objectClass: $oc\n";
417 }
419 /* ... attributes */
420 foreach ($this->attributes as $attr){
421 if ($this->$attr == ""){
422 continue;
423 }
424 if (is_array($this->$attr)){
425 foreach ($this->$attr as $val){
426 $ldif.= "$attr: $val\n";
427 }
428 } else {
429 $ldif.= "$attr: ".$this->$attr."\n";
430 }
431 }
433 /* Append empty line */
434 $ldif.= "\n";
436 /* Feed "ldif" into hook and retrieve result*/
437 $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
438 $fh= proc_open($command, $descriptorspec, $pipes);
439 if (is_resource($fh)) {
440 fwrite ($pipes[0], $ldif);
441 fclose($pipes[0]);
443 $result= stream_get_contents($pipes[1]);
444 if ($result != ""){
445 $message[]= $result;
446 }
448 fclose($pipes[1]);
449 fclose($pipes[2]);
450 proc_close($fh);
451 }
452 }
454 }
456 return ($message);
457 }
459 /* Adapt from template, using 'dn' */
460 function adapt_from_template($dn)
461 {
462 /* Include global link_info */
463 $ldap= $this->config->get_ldap_link();
465 /* Load requested 'dn' to 'attrs' */
466 $ldap->cat ($dn);
467 $this->attrs= $ldap->fetch();
469 /* Walk through attributes */
470 foreach ($this->attributes as $val){
472 if (isset($this->attrs["$val"][0])){
474 /* If attribute is set, replace dynamic parts:
475 %sn, %givenName and %uid. Fill these in our local variables. */
476 $value= $this->attrs["$val"][0];
478 foreach (array("sn", "givenName", "uid") as $repl){
479 if (preg_match("/%$repl/i", $value)){
480 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
481 }
482 }
483 $this->$val= $value;
484 }
485 }
487 /* Is Account? */
488 $found= TRUE;
489 foreach ($this->objectclasses as $obj){
490 if (preg_match('/top/i', $obj)){
491 continue;
492 }
493 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
494 $found= FALSE;
495 break;
496 }
497 }
498 if ($found){
499 $this->is_account= TRUE;
500 }
501 }
503 /* Indicate whether a password change is needed or not */
504 function password_change_needed()
505 {
506 return FALSE;
507 }
509 /* Show header message for tab dialogs */
510 function show_header($button_text, $text, $disabled= FALSE)
511 {
512 $state = "disabled";
513 if($this->is_account && $this->acl == "#all#"){
514 $state= "";
515 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
516 $state= "";
517 }
519 if ($disabled == TRUE){
520 $state= "disabled";
521 }
523 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
524 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".$state.">".
525 "<p class=\"seperator\"> </p></td></tr></table>";
527 return($display);
528 }
530 function postcreate($add_attrs= array())
531 {
532 /* Find postcreate entries for this class */
533 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
534 if ($command == "" && isset($this->config->data['TABS'])){
535 $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
536 }
538 if ($command != ""){
539 /* Walk through attribute list */
540 foreach ($this->attributes as $attr){
541 if (!is_array($this->$attr)){
542 $command= preg_replace("/%$attr/", $this->$attr, $command);
543 }
544 }
545 $command= preg_replace("/%dn/", $this->dn, $command);
547 /* Additional attributes */
548 foreach ($add_attrs as $name => $value){
549 $command= preg_replace("/%$name/", $value, $command);
550 }
552 if (check_command($command)){
553 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
554 $command, "Execute");
556 exec($command);
557 } else {
558 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
559 print_red ($message);
560 }
561 }
562 }
564 function postmodify($add_attrs= array())
565 {
566 /* Find postcreate entries for this class */
567 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
568 if ($command == "" && isset($this->config->data['TABS'])){
569 $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
570 }
572 if ($command != ""){
573 /* Walk through attribute list */
574 foreach ($this->attributes as $attr){
575 if (!is_array($this->$attr)){
576 $command= preg_replace("/%$attr/", $this->$attr, $command);
577 }
578 }
579 $command= preg_replace("/%dn/", $this->dn, $command);
581 /* Additional attributes */
582 foreach ($add_attrs as $name => $value){
583 $command= preg_replace("/%$name/", $value, $command);
584 }
586 if (check_command($command)){
587 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
588 $command, "Execute");
590 exec($command);
591 } else {
592 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
593 print_red ($message);
594 }
595 }
596 }
598 function postremove($add_attrs= array())
599 {
600 /* Find postremove entries for this class */
601 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
602 if ($command == "" && isset($this->config->data['TABS'])){
603 $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
604 }
606 if ($command != ""){
607 /* Walk through attribute list */
608 foreach ($this->attributes as $attr){
609 if (!is_array($this->$attr)){
610 $command= preg_replace("/%$attr/", $this->$attr, $command);
611 }
612 }
613 $command= preg_replace("/%dn/", $this->dn, $command);
615 /* Additional attributes */
616 foreach ($add_attrs as $name => $value){
617 $command= preg_replace("/%$name/", $value, $command);
618 }
620 if (check_command($command)){
621 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
622 $command, "Execute");
624 exec($command);
625 } else {
626 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
627 print_red ($message);
628 }
629 }
630 }
632 /* Create unique DN */
633 function create_unique_dn($attribute, $base)
634 {
635 $ldap= $this->config->get_ldap_link();
636 $base= preg_replace("/^,*/", "", $base);
638 /* Try to use plain entry first */
639 $dn= "$attribute=".$this->$attribute.",$base";
640 $ldap->cat ($dn, array('dn'));
641 if (!$ldap->fetch()){
642 return ($dn);
643 }
645 /* Look for additional attributes */
646 foreach ($this->attributes as $attr){
647 if ($attr == $attribute || $this->$attr == ""){
648 continue;
649 }
651 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
652 $ldap->cat ($dn, array('dn'));
653 if (!$ldap->fetch()){
654 return ($dn);
655 }
656 }
658 /* None found */
659 return ("none");
660 }
662 function rebind($ldap, $referral)
663 {
664 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
665 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
666 $this->error = "Success";
667 $this->hascon=true;
668 $this->reconnect= true;
669 return (0);
670 } else {
671 $this->error = "Could not bind to " . $credentials['ADMIN'];
672 return NULL;
673 }
674 }
676 /* This is a workaround function. */
677 function copy($src_dn, $dst_dn)
678 {
679 /* Rename dn in possible object groups */
680 $ldap= $this->config->get_ldap_link();
681 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
682 array('cn'));
683 while ($attrs= $ldap->fetch()){
684 $og= new ogroup($this->config, $ldap->getDN());
685 unset($og->member[$src_dn]);
686 $og->member[$dst_dn]= $dst_dn;
687 $og->save ();
688 }
690 $ldap->cat($dst_dn);
691 $attrs= $ldap->fetch();
692 if (count($attrs)){
693 trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
694 E_USER_WARNING);
695 return (FALSE);
696 }
698 $ldap->cat($src_dn);
699 $attrs= $ldap->fetch();
700 if (!count($attrs)){
701 trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
702 E_USER_WARNING);
703 return (FALSE);
704 }
706 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
707 $ds= ldap_connect($this->config->current['SERVER']);
708 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
709 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
710 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
711 }
713 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
714 error_reporting (0);
715 $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
717 /* Fill data from LDAP */
718 $new= array();
719 if ($sr) {
720 $ei=ldap_first_entry($ds, $sr);
721 if ($ei) {
722 foreach($attrs as $attr => $val){
723 if ($info = ldap_get_values_len($ds, $ei, $attr)){
724 for ($i= 0; $i<$info['count']; $i++){
725 if ($info['count'] == 1){
726 $new[$attr]= $info[$i];
727 } else {
728 $new[$attr][]= $info[$i];
729 }
730 }
731 }
732 }
733 }
734 }
736 /* close conncetion */
737 error_reporting (E_ALL);
738 ldap_unbind($ds);
740 /* Adapt naming attribute */
741 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
742 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
743 $new[$dst_name]= @LDAP::fix($dst_val);
745 /* Check if this is a department.
746 * If it is a dep. && there is a , override in his ou
747 * change \2C to , again, else this entry can't be saved ...
748 */
749 if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
750 $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
751 }
753 /* Save copy */
754 $ldap->connect();
755 $ldap->cd($this->config->current['BASE']);
757 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
759 /* FAIvariable=.../..., cn=..
760 could not be saved, because the attribute FAIvariable was different to
761 the dn FAIvariable=..., cn=... */
762 if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
763 $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
764 }
765 $ldap->cd($dst_dn);
766 $ldap->add($new);
768 if ($ldap->error != "Success"){
769 trigger_error("Trying to save $dst_dn failed.",
770 E_USER_WARNING);
771 return(FALSE);
772 }
774 return (TRUE);
775 }
778 function move($src_dn, $dst_dn)
779 {
780 /* Copy source to destination */
781 if (!$this->copy($src_dn, $dst_dn)){
782 return (FALSE);
783 }
785 /* Delete source */
786 $ldap= $this->config->get_ldap_link();
787 $ldap->rmdir($src_dn);
788 if ($ldap->error != "Success"){
789 trigger_error("Trying to delete $src_dn failed.",
790 E_USER_WARNING);
791 return (FALSE);
792 }
794 return (TRUE);
795 }
798 /* Move/Rename complete trees */
799 function recursive_move($src_dn, $dst_dn)
800 {
801 /* Check if the destination entry exists */
802 $ldap= $this->config->get_ldap_link();
804 /* Check if destination exists - abort */
805 $ldap->cat($dst_dn, array('dn'));
806 if ($ldap->fetch()){
807 trigger_error("recursive_move $dst_dn already exists.",
808 E_USER_WARNING);
809 return (FALSE);
810 }
812 /* Perform a search for all objects to be moved */
813 $objects= array();
814 $ldap->cd($src_dn);
815 $ldap->search("(objectClass=*)", array("dn"));
816 while($attrs= $ldap->fetch()){
817 $dn= $attrs['dn'];
818 $objects[$dn]= strlen($dn);
819 }
821 /* Sort objects by indent level */
822 asort($objects);
823 reset($objects);
825 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
826 foreach ($objects as $object => $len){
827 $src= $object;
828 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
829 if (!$this->copy($src, $dst)){
830 return (FALSE);
831 }
832 }
834 /* Remove src_dn */
835 $ldap->cd($src_dn);
836 $ldap->recursive_remove();
837 return (TRUE);
838 }
841 function handle_post_events($mode, $add_attrs= array())
842 {
843 switch ($mode){
844 case "add":
845 $this->postcreate($add_attrs);
846 break;
848 case "modify":
849 $this->postmodify($add_attrs);
850 break;
852 case "remove":
853 $this->postremove($add_attrs);
854 break;
855 }
856 }
859 function saveCopyDialog(){
860 }
863 function getCopyDialog(){
864 return(array("string"=>"","status"=>""));
865 }
868 function PrepareForCopyPaste($source){
869 $todo = $this->attributes;
870 if(isset($this->CopyPasteVars)){
871 $todo = array_merge($todo,$this->CopyPasteVars);
872 }
873 $todo[] = "is_account";
874 foreach($todo as $var){
875 if (isset($source->$var)){
876 $this->$var= $source->$var;
877 }
878 }
879 }
882 function handle_object_tagging($dn= "", $tag= "", $show= false)
883 {
884 //FIXME: How to optimize this? We have at least two
885 // LDAP accesses per object. It would be a good
886 // idea to have it integrated.
888 /* No dn? Self-operation... */
889 if ($dn == ""){
890 $dn= $this->dn;
892 /* No tag? Find it yourself... */
893 if ($tag == ""){
894 $len= strlen($dn);
896 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
897 $relevant= array();
898 foreach ($this->config->adepartments as $key => $ntag){
900 /* This one is bigger than our dn, its not relevant... */
901 if ($len <= strlen($key)){
902 continue;
903 }
905 /* This one matches with the latter part. Break and don't fix this entry */
906 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
907 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
908 $relevant[strlen($key)]= $ntag;
909 continue;
910 }
912 }
914 /* If we've some relevant tags to set, just get the longest one */
915 if (count($relevant)){
916 ksort($relevant);
917 $tmp= array_keys($relevant);
918 $idx= end($tmp);
919 $tag= $relevant[$idx];
920 $this->gosaUnitTag= $tag;
921 }
922 }
923 }
926 /* Set tag? */
927 if ($tag != ""){
928 /* Set objectclass and attribute */
929 $ldap= $this->config->get_ldap_link();
930 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
931 $attrs= $ldap->fetch();
932 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
933 if ($show) {
934 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
935 flush();
936 }
937 return;
938 }
939 if (count($attrs)){
940 if ($show){
941 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
942 flush();
943 }
944 $nattrs= array("gosaUnitTag" => $tag);
945 $nattrs['objectClass']= array();
946 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
947 $oc= $attrs['objectClass'][$i];
948 if ($oc != "gosaAdministrativeUnitTag"){
949 $nattrs['objectClass'][]= $oc;
950 }
951 }
952 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
953 $ldap->cd($dn);
954 $ldap->modify($nattrs);
955 show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
956 } else {
957 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
958 }
960 } else {
961 /* Remove objectclass and attribute */
962 $ldap= $this->config->get_ldap_link();
963 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
964 $attrs= $ldap->fetch();
965 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
966 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
967 return;
968 }
969 if (count($attrs)){
970 if ($show){
971 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
972 flush();
973 }
974 $nattrs= array("gosaUnitTag" => array());
975 $nattrs['objectClass']= array();
976 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
977 $oc= $attrs['objectClass'][$i];
978 if ($oc != "gosaAdministrativeUnitTag"){
979 $nattrs['objectClass'][]= $oc;
980 }
981 }
982 $ldap->cd($dn);
983 $ldap->modify($nattrs);
984 show_ldap_error($ldap->get_error(), _("Handle object tagging failed"));
985 } else {
986 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
987 }
988 }
990 }
993 /* Add possibility to stop remove process */
994 function allow_remove()
995 {
996 $reason= "";
997 return $reason;
998 }
1000 }
1001 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1002 ?>