419446592c24640539633e7b269c4e550da13e99
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;
103 var $snapDialog = NULL;
105 /* attribute list for save action */
106 var $attributes= array();
107 var $objectclasses= array();
108 var $new= TRUE;
109 var $saved_attributes= array();
111 /* Plugin identifier */
112 var $plHeadline= "";
113 var $plDescription= "";
115 /*! \brief plugin constructor
117 If 'dn' is set, the node loads the given 'dn' from LDAP
119 \param dn Distinguished name to initialize plugin from
120 \sa plugin()
121 */
122 function plugin ($config, $dn= NULL)
123 {
124 /* Configuration is fine, allways */
125 $this->config= $config;
126 $this->dn= $dn;
128 /* Handle new accounts, don't read information from LDAP */
129 if ($dn == "new"){
130 return;
131 }
133 /* Get LDAP descriptor */
134 $ldap= $this->config->get_ldap_link();
135 if ($dn != NULL){
137 /* Load data to 'attrs' and save 'dn' */
138 $ldap->cat ($dn);
139 $this->attrs= $ldap->fetch();
141 /* Copy needed attributes */
142 foreach ($this->attributes as $val){
143 $found= array_key_ics($val, $this->attrs);
144 if ($found != ""){
145 $this->$val= $this->attrs["$found"][0];
146 }
147 }
149 /* gosaUnitTag loading... */
150 if (isset($this->attrs['gosaUnitTag'][0])){
151 $this->gosaUnitTag= $this->attrs['gosaUnitTag'][0];
152 }
154 /* Set the template flag according to the existence of objectClass
155 gosaUserTemplate */
156 if (isset($this->attrs['objectClass'])){
157 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
158 $this->is_template= TRUE;
159 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
160 "found", "Template check");
161 }
162 }
164 /* Is Account? */
165 error_reporting(0);
166 $found= TRUE;
167 foreach ($this->objectclasses as $obj){
168 if (preg_match('/top/i', $obj)){
169 continue;
170 }
171 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
172 $found= FALSE;
173 break;
174 }
175 }
176 error_reporting(E_ALL);
177 if ($found){
178 $this->is_account= TRUE;
179 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
180 "found", "Object check");
181 }
183 /* Prepare saved attributes */
184 $this->saved_attributes= $this->attrs;
185 foreach ($this->saved_attributes as $index => $value){
186 if (preg_match('/^[0-9]+$/', $index)){
187 unset($this->saved_attributes[$index]);
188 continue;
189 }
190 if (!in_array($index, $this->attributes) && $index != "objectClass"){
191 unset($this->saved_attributes[$index]);
192 continue;
193 }
194 if ($this->saved_attributes[$index]["count"] == 1){
195 $tmp= $this->saved_attributes[$index][0];
196 unset($this->saved_attributes[$index]);
197 $this->saved_attributes[$index]= $tmp;
198 continue;
199 }
201 unset($this->saved_attributes["$index"]["count"]);
202 }
203 }
205 /* Save initial account state */
206 $this->initially_was_account= $this->is_account;
207 }
209 /*! \brief execute plugin
211 Generates the html output for this node
212 */
213 function execute()
214 {
215 # This one is empty currently. Fabian - please fill in the docu code
216 $_SESSION['current_class_for_help'] = get_class($this);
217 /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
218 $_SESSION['LOCK_VARS_TO_USE'] =array();
219 }
221 /*! \brief execute plugin
222 Removes object from parent
223 */
224 function remove_from_parent()
225 {
226 /* include global link_info */
227 $ldap= $this->config->get_ldap_link();
229 /* Get current objectClasses in order to add the required ones */
230 $ldap->cat($this->dn);
231 $tmp= $ldap->fetch ();
232 if (isset($tmp['objectClass'])){
233 $oc= $tmp['objectClass'];
234 } else {
235 $oc= array("count" => 0);
236 }
238 /* Remove objectClasses from entry */
239 $ldap->cd($this->dn);
240 $this->attrs= array();
241 $this->attrs['objectClass']= array();
242 for ($i= 0; $i<$oc["count"]; $i++){
243 if (!in_array_ics($oc[$i], $this->objectclasses)){
244 $this->attrs['objectClass'][]= $oc[$i];
245 }
246 }
248 /* Unset attributes from entry */
249 foreach ($this->attributes as $val){
250 $this->attrs["$val"]= array();
251 }
253 /* Unset account info */
254 $this->is_account= FALSE;
256 /* Do not write in plugin base class, this must be done by
257 children, since there are normally additional attribs,
258 lists, etc. */
259 /*
260 $ldap->modify($this->attrs);
261 */
262 }
265 /* Save data to object */
266 function save_object()
267 {
268 /* Save values to object */
269 foreach ($this->attributes as $val){
270 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
271 /* Check for modifications */
272 if (get_magic_quotes_gpc()) {
273 $data= stripcslashes($_POST["$val"]);
274 } else {
275 $data= $this->$val = $_POST["$val"];
276 }
277 if ($this->$val != $data){
278 $this->is_modified= TRUE;
279 }
281 /* Okay, how can I explain this fix ...
282 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
283 * So IE posts these 'unselectable' option, with value = chr(194)
284 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
285 * This was added for W3c compliance, but now causes these ... ldap errors ...
286 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
287 */
288 if(isset($data[0]) && $data[0] == chr(194)) {
289 $data = "";
290 }
291 $this->$val= $data;
292 }
293 }
294 }
297 /* Save data to LDAP, depending on is_account we save or delete */
298 function save()
299 {
300 /* include global link_info */
301 $ldap= $this->config->get_ldap_link();
303 /* Start with empty array */
304 $this->attrs= array();
306 /* Get current objectClasses in order to add the required ones */
307 $ldap->cat($this->dn);
309 $tmp= $ldap->fetch ();
311 if (isset($tmp['objectClass'])){
312 $oc= $tmp["objectClass"];
313 $this->new= FALSE;
314 } else {
315 $oc= array("count" => 0);
316 $this->new= TRUE;
317 }
319 /* Load (minimum) attributes, add missing ones */
320 $this->attrs['objectClass']= $this->objectclasses;
321 for ($i= 0; $i<$oc["count"]; $i++){
322 if (!in_array_ics($oc[$i], $this->objectclasses)){
323 $this->attrs['objectClass'][]= $oc[$i];
324 }
325 }
327 /* Copy standard attributes */
328 foreach ($this->attributes as $val){
329 if ($this->$val != ""){
330 $this->attrs["$val"]= $this->$val;
331 } elseif (!$this->new) {
332 $this->attrs["$val"]= array();
333 }
334 }
336 }
339 function cleanup()
340 {
341 foreach ($this->attrs as $index => $value){
343 /* Convert arrays with one element to non arrays, if the saved
344 attributes are no array, too */
345 if (is_array($this->attrs[$index]) &&
346 count ($this->attrs[$index]) == 1 &&
347 isset($this->saved_attributes[$index]) &&
348 !is_array($this->saved_attributes[$index])){
350 $tmp= $this->attrs[$index][0];
351 $this->attrs[$index]= $tmp;
352 }
354 /* Remove emtpy arrays if they do not differ */
355 if (is_array($this->attrs[$index]) &&
356 count($this->attrs[$index]) == 0 &&
357 !isset($this->saved_attributes[$index])){
359 unset ($this->attrs[$index]);
360 continue;
361 }
363 /* Remove single attributes that do not differ */
364 if (!is_array($this->attrs[$index]) &&
365 isset($this->saved_attributes[$index]) &&
366 !is_array($this->saved_attributes[$index]) &&
367 $this->attrs[$index] == $this->saved_attributes[$index]){
369 unset ($this->attrs[$index]);
370 continue;
371 }
373 /* Remove arrays that do not differ */
374 if (is_array($this->attrs[$index]) &&
375 isset($this->saved_attributes[$index]) &&
376 is_array($this->saved_attributes[$index])){
378 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
379 unset ($this->attrs[$index]);
380 continue;
381 }
382 }
383 }
384 }
386 /* Check formular input */
387 function check()
388 {
389 $message= array();
391 /* Skip if we've no config object */
392 if (!isset($this->config)){
393 return $message;
394 }
396 /* Find hooks entries for this class */
397 $command= search_config($this->config->data['MENU'], get_class($this), "CHECK");
398 if ($command == "" && isset($this->config->data['TABS'])){
399 $command= search_config($this->config->data['TABS'], get_class($this), "CHECK");
400 }
402 if ($command != ""){
404 if (!check_command($command)){
405 $message[]= sprintf(_("Command '%s', specified as CHECK hook for plugin '%s' doesn't seem to exist."), $command,
406 get_class($this));
407 } else {
409 /* Generate "ldif" for check hook */
410 $ldif= "dn: $this->dn\n";
412 /* ... objectClasses */
413 foreach ($this->objectclasses as $oc){
414 $ldif.= "objectClass: $oc\n";
415 }
417 /* ... attributes */
418 foreach ($this->attributes as $attr){
419 if ($this->$attr == ""){
420 continue;
421 }
422 if (is_array($this->$attr)){
423 foreach ($this->$attr as $val){
424 $ldif.= "$attr: $val\n";
425 }
426 } else {
427 $ldif.= "$attr: ".$this->$attr."\n";
428 }
429 }
431 /* Append empty line */
432 $ldif.= "\n";
434 /* Feed "ldif" into hook and retrieve result*/
435 $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
436 $fh= proc_open($command, $descriptorspec, $pipes);
437 if (is_resource($fh)) {
438 fwrite ($pipes[0], $ldif);
439 fclose($pipes[0]);
441 $result= stream_get_contents($pipes[1]);
442 if ($result != ""){
443 $message[]= $result;
444 }
446 fclose($pipes[1]);
447 fclose($pipes[2]);
448 proc_close($fh);
449 }
450 }
452 }
454 return ($message);
455 }
457 /* Adapt from template, using 'dn' */
458 function adapt_from_template($dn)
459 {
460 /* Include global link_info */
461 $ldap= $this->config->get_ldap_link();
463 /* Load requested 'dn' to 'attrs' */
464 $ldap->cat ($dn);
465 $this->attrs= $ldap->fetch();
467 /* Walk through attributes */
468 foreach ($this->attributes as $val){
470 if (isset($this->attrs["$val"][0])){
472 /* If attribute is set, replace dynamic parts:
473 %sn, %givenName and %uid. Fill these in our local variables. */
474 $value= $this->attrs["$val"][0];
476 foreach (array("sn", "givenName", "uid") as $repl){
477 if (preg_match("/%$repl/i", $value)){
478 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
479 }
480 }
481 $this->$val= $value;
482 }
483 }
485 /* Is Account? */
486 $found= TRUE;
487 foreach ($this->objectclasses as $obj){
488 if (preg_match('/top/i', $obj)){
489 continue;
490 }
491 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
492 $found= FALSE;
493 break;
494 }
495 }
496 if ($found){
497 $this->is_account= TRUE;
498 }
499 }
501 /* Indicate whether a password change is needed or not */
502 function password_change_needed()
503 {
504 return FALSE;
505 }
507 /* Show header message for tab dialogs */
508 function show_header($button_text, $text, $disabled= FALSE)
509 {
510 if ($disabled == TRUE){
511 $state= "disabled";
512 } else {
513 $state= "";
514 }
515 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
516 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
517 chkacl($this->acl, "all")." ".$state.
518 "><p class=\"seperator\"> </p></td></tr></table>";
520 return($display);
521 }
523 function postcreate($add_attrs= array())
524 {
525 /* Find postcreate entries for this class */
526 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
527 if ($command == "" && isset($this->config->data['TABS'])){
528 $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
529 }
531 if ($command != ""){
532 /* Walk through attribute list */
533 foreach ($this->attributes as $attr){
534 if (!is_array($this->$attr)){
535 $command= preg_replace("/%$attr/", $this->$attr, $command);
536 }
537 }
538 $command= preg_replace("/%dn/", $this->dn, $command);
540 /* Additional attributes */
541 foreach ($add_attrs as $name => $value){
542 $command= preg_replace("/%$name/", $value, $command);
543 }
545 if (check_command($command)){
546 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
547 $command, "Execute");
549 exec($command);
550 } else {
551 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
552 print_red ($message);
553 }
554 }
555 }
557 function postmodify($add_attrs= array())
558 {
559 /* Find postcreate entries for this class */
560 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
561 if ($command == "" && isset($this->config->data['TABS'])){
562 $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
563 }
565 if ($command != ""){
566 /* Walk through attribute list */
567 foreach ($this->attributes as $attr){
568 if (!is_array($this->$attr)){
569 $command= preg_replace("/%$attr/", $this->$attr, $command);
570 }
571 }
572 $command= preg_replace("/%dn/", $this->dn, $command);
574 /* Additional attributes */
575 foreach ($add_attrs as $name => $value){
576 $command= preg_replace("/%$name/", $value, $command);
577 }
579 if (check_command($command)){
580 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
581 $command, "Execute");
583 exec($command);
584 } else {
585 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
586 print_red ($message);
587 }
588 }
589 }
591 function postremove($add_attrs= array())
592 {
593 /* Find postremove entries for this class */
594 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
595 if ($command == "" && isset($this->config->data['TABS'])){
596 $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
597 }
599 if ($command != ""){
600 /* Walk through attribute list */
601 foreach ($this->attributes as $attr){
602 if (!is_array($this->$attr)){
603 $command= preg_replace("/%$attr/", $this->$attr, $command);
604 }
605 }
606 $command= preg_replace("/%dn/", $this->dn, $command);
608 /* Additional attributes */
609 foreach ($add_attrs as $name => $value){
610 $command= preg_replace("/%$name/", $value, $command);
611 }
613 if (check_command($command)){
614 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
615 $command, "Execute");
617 exec($command);
618 } else {
619 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
620 print_red ($message);
621 }
622 }
623 }
625 /* Create unique DN */
626 function create_unique_dn($attribute, $base)
627 {
628 $ldap= $this->config->get_ldap_link();
629 $base= preg_replace("/^,*/", "", $base);
631 /* Try to use plain entry first */
632 $dn= "$attribute=".$this->$attribute.",$base";
633 $ldap->cat ($dn, array('dn'));
634 if (!$ldap->fetch()){
635 return ($dn);
636 }
638 /* Look for additional attributes */
639 foreach ($this->attributes as $attr){
640 if ($attr == $attribute || $this->$attr == ""){
641 continue;
642 }
644 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
645 $ldap->cat ($dn, array('dn'));
646 if (!$ldap->fetch()){
647 return ($dn);
648 }
649 }
651 /* None found */
652 return ("none");
653 }
655 function rebind($ldap, $referral)
656 {
657 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
658 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
659 $this->error = "Success";
660 $this->hascon=true;
661 $this->reconnect= true;
662 return (0);
663 } else {
664 $this->error = "Could not bind to " . $credentials['ADMIN'];
665 return NULL;
666 }
667 }
669 /* This is a workaround function. */
670 function copy($src_dn, $dst_dn)
671 {
672 /* Rename dn in possible object groups */
673 $ldap= $this->config->get_ldap_link();
674 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::fix($src_dn).'))',
675 array('cn'));
676 while ($attrs= $ldap->fetch()){
677 $og= new ogroup($this->config, $ldap->getDN());
678 unset($og->member[$src_dn]);
679 $og->member[$dst_dn]= $dst_dn;
680 $og->save ();
681 }
683 $ldap->cat($dst_dn);
684 $attrs= $ldap->fetch();
685 if (count($attrs)){
686 trigger_error("Trying to overwrite ".@LDAP::fix($dst_dn).", which already exists.",
687 E_USER_WARNING);
688 return (FALSE);
689 }
691 $ldap->cat($src_dn);
692 $attrs= $ldap->fetch();
693 if (!count($attrs)){
694 trigger_error("Trying to move ".@LDAP::fix($src_dn).", which does not seem to exist.",
695 E_USER_WARNING);
696 return (FALSE);
697 }
699 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
700 $ds= ldap_connect($this->config->current['SERVER']);
701 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
702 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
703 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
704 }
706 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
707 error_reporting (0);
708 $sr=ldap_read($ds, @LDAP::fix($src_dn), "objectClass=*");
710 /* Fill data from LDAP */
711 $new= array();
712 if ($sr) {
713 $ei=ldap_first_entry($ds, $sr);
714 if ($ei) {
715 foreach($attrs as $attr => $val){
716 if ($info = ldap_get_values_len($ds, $ei, $attr)){
717 for ($i= 0; $i<$info['count']; $i++){
718 if ($info['count'] == 1){
719 $new[$attr]= $info[$i];
720 } else {
721 $new[$attr][]= $info[$i];
722 }
723 }
724 }
725 }
726 }
727 }
729 /* close conncetion */
730 error_reporting (E_ALL);
731 ldap_unbind($ds);
733 /* Adapt naming attribute */
734 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
735 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
736 $new[$dst_name]= @LDAP::fix($dst_val);
738 /* Check if this is a department.
739 * If it is a dep. && there is a , override in his ou
740 * change \2C to , again, else this entry can't be saved ...
741 */
742 if((isset($new['ou'])) &&( preg_match("/\\,/",$new['ou']))){
743 $new['ou'] = preg_replace("/\\\\,/",",",$new['ou']);
744 }
746 /* Save copy */
747 $ldap->connect();
748 $ldap->cd($this->config->current['BASE']);
750 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
752 /* FAIvariable=.../..., cn=..
753 could not be saved, because the attribute FAIvariable was different to
754 the dn FAIvariable=..., cn=... */
755 if(in_array_ics("FAIdebconfInfo",$new['objectClass'])){
756 $new['FAIvariable'] = $ldap->fix($new['FAIvariable']);
757 }
758 $ldap->cd($dst_dn);
759 $ldap->add($new);
761 if ($ldap->error != "Success"){
762 trigger_error("Trying to save $dst_dn failed.",
763 E_USER_WARNING);
764 return(FALSE);
765 }
767 return (TRUE);
768 }
771 function move($src_dn, $dst_dn)
772 {
773 /* Copy source to destination */
774 if (!$this->copy($src_dn, $dst_dn)){
775 return (FALSE);
776 }
778 /* Delete source */
779 $ldap= $this->config->get_ldap_link();
780 $ldap->rmdir($src_dn);
781 if ($ldap->error != "Success"){
782 trigger_error("Trying to delete $src_dn failed.",
783 E_USER_WARNING);
784 return (FALSE);
785 }
787 return (TRUE);
788 }
791 /* Move/Rename complete trees */
792 function recursive_move($src_dn, $dst_dn)
793 {
794 /* Check if the destination entry exists */
795 $ldap= $this->config->get_ldap_link();
797 /* Check if destination exists - abort */
798 $ldap->cat($dst_dn, array('dn'));
799 if ($ldap->fetch()){
800 trigger_error("recursive_move $dst_dn already exists.",
801 E_USER_WARNING);
802 return (FALSE);
803 }
805 /* Perform a search for all objects to be moved */
806 $objects= array();
807 $ldap->cd($src_dn);
808 $ldap->search("(objectClass=*)", array("dn"));
809 while($attrs= $ldap->fetch()){
810 $dn= $attrs['dn'];
811 $objects[$dn]= strlen($dn);
812 }
814 /* Sort objects by indent level */
815 asort($objects);
816 reset($objects);
818 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
819 foreach ($objects as $object => $len){
820 $src= $object;
821 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
822 if (!$this->copy($src, $dst)){
823 return (FALSE);
824 }
825 }
827 /* Remove src_dn */
828 $ldap->cd($src_dn);
829 $ldap->recursive_remove();
830 return (TRUE);
831 }
834 function handle_post_events($mode, $add_attrs= array())
835 {
836 switch ($mode){
837 case "add":
838 $this->postcreate($add_attrs);
839 break;
841 case "modify":
842 $this->postmodify($add_attrs);
843 break;
845 case "remove":
846 $this->postremove($add_attrs);
847 break;
848 }
849 }
852 function saveCopyDialog(){
853 }
856 function getCopyDialog(){
857 return(array("string"=>"","status"=>""));
858 }
861 function PrepareForCopyPaste($source){
862 $todo = $this->attributes;
863 if(isset($this->CopyPasteVars)){
864 $todo = array_merge($todo,$this->CopyPasteVars);
865 }
866 $todo[] = "is_account";
867 foreach($todo as $var){
868 $this->$var = $source->$var;
869 }
870 }
873 function handle_object_tagging($dn= "", $tag= "", $show= false)
874 {
875 //FIXME: How to optimize this? We have at least two
876 // LDAP accesses per object. It would be a good
877 // idea to have it integrated.
879 /* No dn? Self-operation... */
880 if ($dn == ""){
881 $dn= $this->dn;
883 /* No tag? Find it yourself... */
884 if ($tag == ""){
885 $len= strlen($dn);
887 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
888 $relevant= array();
889 foreach ($this->config->adepartments as $key => $ntag){
891 /* This one is bigger than our dn, its not relevant... */
892 if ($len <= strlen($key)){
893 continue;
894 }
896 /* This one matches with the latter part. Break and don't fix this entry */
897 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
898 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
899 $relevant[strlen($key)]= $ntag;
900 continue;
901 }
903 }
905 /* If we've some relevant tags to set, just get the longest one */
906 if (count($relevant)){
907 ksort($relevant);
908 $tmp= array_keys($relevant);
909 $idx= end($tmp);
910 $tag= $relevant[$idx];
911 $this->gosaUnitTag= $tag;
912 }
913 }
914 }
917 /* Set tag? */
918 if ($tag != ""){
919 /* Set objectclass and attribute */
920 $ldap= $this->config->get_ldap_link();
921 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
922 $attrs= $ldap->fetch();
923 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
924 if ($show) {
925 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
926 flush();
927 }
928 return;
929 }
930 if (count($attrs)){
931 if ($show){
932 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
933 flush();
934 }
935 $nattrs= array("gosaUnitTag" => $this->gosaUnitTag);
936 $nattrs['objectClass']= array();
937 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
938 $oc= $attrs['objectClass'][$i];
939 if ($oc != "gosaAdministrativeUnitTag"){
940 $nattrs['objectClass'][]= $oc;
941 }
942 }
943 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
944 $ldap->cd($dn);
945 $ldap->modify($nattrs);
946 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
947 } else {
948 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
949 }
951 } else {
952 /* Remove objectclass and attribute */
953 $ldap= $this->config->get_ldap_link();
954 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
955 $attrs= $ldap->fetch();
956 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
957 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
958 return;
959 }
960 if (count($attrs)){
961 if ($show){
962 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
963 flush();
964 }
965 $nattrs= array("gosaUnitTag" => array());
966 $nattrs['objectClass']= array();
967 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
968 $oc= $attrs['objectClass'][$i];
969 if ($oc != "gosaAdministrativeUnitTag"){
970 $nattrs['objectClass'][]= $oc;
971 }
972 }
973 $ldap->cd($dn);
974 $ldap->modify($nattrs);
975 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
976 } else {
977 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
978 }
979 }
981 }
984 /* Add possibility to stop remove process */
985 function allow_remove()
986 {
987 $reason= "";
988 return $reason;
989 }
992 /* Create a snapshot of the current object */
993 function create_snapshot($type= "snapshot", $description= array())
994 {
996 /* Check if snapshot functionality is enabled */
997 if(!$this->snapshotEnabled()){
998 return;
999 }
1001 /* Get configuration from gosa.conf */
1002 $tmp = $this->config->current;
1004 /* Create lokal ldap connection */
1005 $ldap= $this->config->get_ldap_link();
1006 $ldap->cd($this->config->current['BASE']);
1008 /* check if there are special server configurations for snapshots */
1009 if(!isset($tmp['SNAPSHOT_SERVER'])){
1011 /* Source and destination server are both the same, just copy source to dest obj */
1012 $ldap_to = $ldap;
1013 $snapldapbase = $this->config->current['BASE'];
1015 }else{
1016 $server = $tmp['SNAPSHOT_SERVER'];
1017 $user = $tmp['SNAPSHOT_USER'];
1018 $password = $tmp['SNAPSHOT_PASSWORD'];
1019 $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1021 $ldap_to = new LDAP($user,$password, $server);
1022 $ldap_to -> cd($snapldapbase);
1023 show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$snapldapbase));
1024 }
1026 /* check if the dn exists */
1027 if ($ldap->dn_exists($this->dn)){
1029 /* Extract seconds & mysecs, they are used as entry index */
1030 list($usec, $sec)= explode(" ", microtime());
1032 /* Collect some infos */
1033 $base = $this->config->current['BASE'];
1034 $snap_base = $tmp['SNAPSHOT_BASE'];
1035 $base_of_object = preg_replace ('/^[^,]+,/i', '', $this->dn);
1036 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1038 /* Create object */
1039 #$data = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($this->dn,"(!(objectClass=gosaDepartment))"));
1040 $data = $ldap->gen_ldif($this->dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
1041 $newName = preg_replace("/\./", "", $sec."-".$usec);
1042 $target= array();
1043 $target['objectClass'] = array("top", "gosaSnapshotObject");
1044 $target['gosaSnapshotData'] = gzcompress($data, 6);
1045 $target['gosaSnapshotType'] = $type;
1046 $target['gosaSnapshotDN'] = $this->dn;
1047 $target['description'] = $description;
1048 $target['gosaSnapshotTimestamp'] = $newName;
1050 /* Insert the new snapshot
1051 But we have to check first, if the given gosaSnapshotTimestamp
1052 is already used, in this case we should increment this value till there is
1053 an unused value. */
1054 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1055 $ldap_to->cat($new_dn);
1056 while($ldap_to->count()){
1057 $ldap_to->cat($new_dn);
1058 $newName = preg_replace("/\./", "", $sec."-".($usec++));
1059 $new_dn = "gosaSnapshotTimestamp=".$newName.",".$new_base;
1060 $target['gosaSnapshotTimestamp'] = $newName;
1061 }
1063 /* Inset this new snapshot */
1064 $ldap_to->cd($snapldapbase);
1065 $ldap_to->create_missing_trees($new_base);
1066 $ldap_to->cd($new_dn);
1067 $ldap_to->add($target);
1069 show_ldap_error($ldap->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1070 show_ldap_error($ldap_to->get_error(), sprintf(_("Saving object snapshot with dn '%s' failed."),$new_base));
1071 }
1072 }
1074 function remove_snapshot($dn)
1075 {
1076 $ui = get_userinfo();
1077 $acl = get_permissions ($dn, $ui->subtreeACL);
1078 $acl = get_module_permission($acl, "snapshot", $dn);
1080 if (chkacl($this->acl, "delete") == ""){
1081 $ldap = $this->config->get_ldap_link();
1082 $ldap->cd($this->config->current['BASE']);
1083 $ldap->rmdir_recursive($dn);
1084 }else{
1085 print_red (_("You are not allowed to delete this snap shot!"));
1086 }
1087 }
1090 /* returns true if snapshots are enabled, and false if it is disalbed
1091 There will also be some errors psoted, if the configuration failed */
1092 function snapshotEnabled()
1093 {
1094 $tmp = $this->config->current;
1095 if(isset($tmp['ENABLE_SNAPSHOT'])){
1096 if (preg_match("/^true$/i", $tmp['ENABLE_SNAPSHOT']) || preg_match("/yes/i", $tmp['ENABLE_SNAPSHOT'])){
1098 /* Check if the snapshot_base is defined */
1099 if(!isset($tmp['SNAPSHOT_BASE'])){
1100 print_red(sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not configured in your gosa.conf."),$missing));
1101 return(FALSE);
1102 }
1104 /* check if there are special server configurations for snapshots */
1105 if(isset($tmp['SNAPSHOT_SERVER'])){
1107 /* check if all required vars are available to create a new ldap connection */
1108 $missing = "";
1109 foreach(array("SNAPSHOT_SERVER","SNAPSHOT_USER","SNAPSHOT_PASSWORD","SNAPSHOT_LDAP_BASE") as $var){
1110 if(!isset($tmp[$var])){
1111 $missing .= $var." ";
1112 print_red(sprintf(_("The snapshot functionality is enabled, but the required variable(s) '%s' is not configured in your gosa.conf."),$missing));
1113 return(FALSE);
1114 }
1115 }
1116 }
1117 return(TRUE);
1118 }
1119 }
1120 return(FALSE);
1121 }
1124 /* Return available snapshots for the given base
1125 */
1126 function Available_SnapsShots($dn,$raw = false)
1127 {
1128 if(!$this->snapshotEnabled()) return(array());
1130 /* Create an additional ldap object which
1131 points to our ldap snapshot server */
1132 $ldap= $this->config->get_ldap_link();
1133 $ldap->cd($this->config->current['BASE']);
1134 $tmp = $this->config->current;
1136 /* check if there are special server configurations for snapshots */
1137 if(isset($tmp['SNAPSHOT_SERVER'])){
1138 $server = $tmp['SNAPSHOT_SERVER'];
1139 $user = $tmp['SNAPSHOT_USER'];
1140 $password = $tmp['SNAPSHOT_PASSWORD'];
1141 $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1142 $ldap_to = new LDAP($user,$password, $server);
1143 $ldap_to -> cd ($snapldapbase);
1144 show_ldap_error($ldap->get_error(), sprintf(_("Method get available snapshots with dn '%s' failed."),$this->dn));
1145 }else{
1146 $ldap_to = $ldap;
1147 }
1149 /* Prepare bases and some other infos */
1150 $base = $this->config->current['BASE'];
1151 $snap_base = $tmp['SNAPSHOT_BASE'];
1152 $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
1153 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1154 $tmp = array();
1156 /* Fetch all objects with gosaSnapshotDN=$dn */
1157 $ldap_to->cd($new_base);
1158 $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
1159 array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1161 /* Put results into a list and add description if missing */
1162 while($entry = $ldap_to->fetch()){
1163 if(!isset($entry['description'][0])){
1164 $entry['description'][0] = "";
1165 }
1166 $tmp[] = $entry;
1167 }
1169 /* Return the raw array, or format the result */
1170 if($raw){
1171 return($tmp);
1172 }else{
1173 $tmp2 = array();
1174 foreach($tmp as $entry){
1175 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1176 }
1177 }
1178 return($tmp2);
1179 }
1182 function getAllDeletedSnapshots($base_of_object,$raw = false)
1183 {
1184 if(!$this->snapshotEnabled()) return(array());
1186 /* Create an additional ldap object which
1187 points to our ldap snapshot server */
1188 $ldap= $this->config->get_ldap_link();
1189 $ldap->cd($this->config->current['BASE']);
1190 $tmp = $this->config->current;
1192 /* check if there are special server configurations for snapshots */
1193 if(isset($tmp['SNAPSHOT_SERVER'])){
1194 $server = $tmp['SNAPSHOT_SERVER'];
1195 $user = $tmp['SNAPSHOT_USER'];
1196 $password = $tmp['SNAPSHOT_PASSWORD'];
1197 $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1198 $ldap_to = new LDAP($user,$password, $server);
1199 $ldap_to->cd ($snapldapbase);
1200 show_ldap_error($ldap->get_error(), sprintf(_("Method get deleted snapshots with dn '%s' failed."),$this->dn));
1201 }else{
1202 $ldap_to = $ldap;
1203 }
1205 /* Prepare bases */
1206 $base = $this->config->current['BASE'];
1207 $snap_base = $tmp['SNAPSHOT_BASE'];
1208 $new_base = preg_replace("/".normalizePreg($base)."$/","",$base_of_object).$snap_base;
1210 /* Fetch all objects and check if they do not exist anymore */
1211 $ui = get_userinfo();
1212 $tmp = array();
1213 $ldap_to->cd($new_base);
1214 $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
1215 while($entry = $ldap_to->fetch()){
1217 $chk = str_replace($new_base,"",$entry['dn']);
1218 if(preg_match("/,ou=/",$chk)) continue;
1220 if(!isset($entry['description'][0])){
1221 $entry['description'][0] = "";
1222 }
1223 $tmp[] = $entry;
1224 }
1226 /* Check if entry still exists */
1227 foreach($tmp as $key => $entry){
1228 $ldap->cat($entry['gosaSnapshotDN'][0]);
1229 if($ldap->count()){
1230 unset($tmp[$key]);
1231 }
1232 }
1234 /* Format result as requested */
1235 if($raw) {
1236 return($tmp);
1237 }else{
1238 $tmp2 = array();
1239 foreach($tmp as $key => $entry){
1240 $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
1241 }
1242 }
1243 return($tmp2);
1244 }
1247 /* Restore selected snapshot */
1248 function restore_snapshot($dn)
1249 {
1250 if(!$this->snapshotEnabled()) return(array());
1252 $ldap= $this->config->get_ldap_link();
1253 $ldap->cd($this->config->current['BASE']);
1254 $tmp = $this->config->current;
1256 /* check if there are special server configurations for snapshots */
1257 if(isset($tmp['SNAPSHOT_SERVER'])){
1258 $server = $tmp['SNAPSHOT_SERVER'];
1259 $user = $tmp['SNAPSHOT_USER'];
1260 $password = $tmp['SNAPSHOT_PASSWORD'];
1261 $snapldapbase = $tmp['SNAPSHOT_LDAP_BASE'];
1262 $ldap_to = new LDAP($user,$password, $server);
1263 $ldap_to->cd ($snapldapbase);
1264 show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$snapldapbase));
1265 }else{
1266 $ldap_to = $ldap;
1267 }
1269 /* Get the snapshot */
1270 $ldap_to->cat($dn);
1271 $restoreObject = $ldap_to->fetch();
1273 /* Prepare import string */
1274 $data = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
1276 /* Import the given data */
1277 $ldap->import_complete_ldif($data,$err,false,false);
1278 show_ldap_error($ldap->get_error(), sprintf(_("Restore snapshot with dn '%s' failed."),$dn));
1279 }
1282 function showSnapshotDialog($base,$baseSuffixe)
1283 {
1284 $once = true;
1285 foreach($_POST as $name => $value){
1287 /* Create a new snapshot, display a dialog */
1288 if(preg_match("/^CreateSnapShotDialog_/",$name) && $once){
1289 $once = false;
1290 $entry = preg_replace("/^CreateSnapShotDialog_/","",$name);
1291 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1292 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1293 }
1295 /* Restore a snapshot, display a dialog with all snapshots of the current object */
1296 if(preg_match("/^RestoreSnapShotDialog_/",$name) && $once){
1297 $once = false;
1298 $entry = preg_replace("/^RestoreSnapShotDialog_/","",$name);
1299 $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
1300 $this->snapDialog = new SnapShotDialog($this->config,$entry,$this);
1301 $this->snapDialog->display_restore_dialog = true;
1302 }
1304 /* Restore one of the already deleted objects */
1305 if(preg_match("/^RestoreDeletedSnapShot_/",$name) && $once){
1306 $once = false;
1307 $this->snapDialog = new SnapShotDialog($this->config,$baseSuffixe,$this);
1308 $this->snapDialog->display_restore_dialog = true;
1309 $this->snapDialog->display_all_removed_objects = true;
1310 }
1312 /* Restore selected snapshot */
1313 if(preg_match("/^RestoreSnapShot_/",$name) && $once){
1314 $once = false;
1315 $entry = preg_replace("/^RestoreSnapShot_/","",$name);
1316 $entry = base64_decode(trim(preg_replace("/_[xy]$/","",$entry)));
1317 if(!empty($entry)){
1318 $this->restore_snapshot($entry);
1319 $this->snapDialog = NULL;
1320 }
1321 }
1322 }
1324 /* Create a new snapshot requested, check
1325 the given attributes and create the snapshot*/
1326 if(isset($_POST['CreateSnapshot'])){
1327 $this->snapDialog->save_object();
1328 $msgs = $this->snapDialog->check();
1329 if(count($msgs)){
1330 foreach($msgs as $msg){
1331 print_red($msg);
1332 }
1333 }else{
1334 $this->dn = $this->snapDialog->dn;
1335 $this->create_snapshot("snapshot",$this->snapDialog->CurrentDescription);
1336 $this->snapDialog = NULL;
1337 }
1338 }
1340 /* Restore is requested, restore the object with the posted dn .*/
1341 if((isset($_POST['RestoreSnapshot'])) && (isset($_POST['SnapShot']))){
1342 }
1344 if(isset($_POST['CancelSnapshot'])){
1345 $this->snapDialog = NULL;
1346 }
1348 if($this->snapDialog){
1349 $this->snapDialog->save_object();
1350 return($this->snapDialog->execute());
1351 }
1352 }
1355 function plInfo()
1356 {
1357 #var $plObject_name= "";
1358 #var $plProvidedAcls= array();
1359 #var $plSelfModify= FALSE;
1360 #var $plOptions= array();
1361 #var $plSection= "";
1362 #var $plTask= array();
1363 #var $plPriority= 0;
1364 #var $plDepends= array();
1365 #var $plConflicts= array();
1366 return array();
1367 }
1369 }
1370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1371 ?>