ef02566336e6f5c66844a272930d59e734bdd75b
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();
87 /*!
88 \brief Used standard values
90 dn
91 */
92 var $dn= "";
93 var $uid= "";
94 var $sn= "";
95 var $givenName= "";
96 var $acl= "*none*";
97 var $dialog= FALSE;
99 /* attribute list for save action */
100 var $attributes= array();
101 var $objectclasses= array();
102 var $new= TRUE;
103 var $saved_attributes= array();
105 /*! \brief plugin constructor
107 If 'dn' is set, the node loads the given 'dn' from LDAP
109 \param dn Distinguished name to initialize plugin from
110 \sa plugin()
111 */
112 function plugin ($config, $dn= NULL)
113 {
114 /* Configuration is fine, allways */
115 $this->config= $config;
116 $this->dn= $dn;
118 /* Handle new accounts, don't read information from LDAP */
119 if ($dn == "new"){
120 return;
121 }
123 /* Get LDAP descriptor */
124 $ldap= $this->config->get_ldap_link();
125 if ($dn != NULL){
127 /* Load data to 'attrs' and save 'dn' */
128 $ldap->cat ($dn);
129 $this->attrs= $ldap->fetch();
131 /* Copy needed attributes */
132 foreach ($this->attributes as $val){
133 #if (isset($this->attrs["$val"][0])){
134 $found= array_key_ics($val, $this->attrs);
135 if ($found != ""){
136 $this->$val= $this->attrs["$found"][0];
137 }
138 }
140 /* Set the template flag according to the existence of objectClass
141 gosaUserTemplate */
142 if (isset($this->attrs['objectClass'])){
143 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
144 $this->is_template= TRUE;
145 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
146 "found", "Template check");
147 }
148 }
150 /* Is Account? */
151 error_reporting(0);
152 $found= TRUE;
153 foreach ($this->objectclasses as $obj){
154 if (preg_match('/top/i', $obj)){
155 continue;
156 }
157 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
158 $found= FALSE;
159 break;
160 }
161 }
162 error_reporting(E_ALL);
163 if ($found){
164 $this->is_account= TRUE;
165 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
166 "found", "Object check");
167 }
169 /* Prepare saved attributes */
170 $this->saved_attributes= $this->attrs;
171 foreach ($this->saved_attributes as $index => $value){
172 if (preg_match('/^[0-9]+$/', $index)){
173 unset($this->saved_attributes[$index]);
174 continue;
175 }
176 if (!in_array($index, $this->attributes) && $index != "objectClass"){
177 unset($this->saved_attributes[$index]);
178 continue;
179 }
180 if ($this->saved_attributes[$index]["count"] == 1){
181 $tmp= $this->saved_attributes[$index][0];
182 unset($this->saved_attributes[$index]);
183 $this->saved_attributes[$index]= $tmp;
184 continue;
185 }
187 unset($this->saved_attributes["$index"]["count"]);
188 }
189 }
191 /* Save initial account state */
192 $this->initially_was_account= $this->is_account;
193 }
195 /*! \brief execute plugin
197 Generates the html output for this node
198 */
199 function execute()
200 {
201 # This one is empty currently. Fabian - please fill in the docu code
202 $_SESSION['current_class_for_help'] = get_class($this);
203 }
205 /* remove object from parent */
206 function remove_from_parent()
207 {
208 /* include global link_info */
209 $ldap= $this->config->get_ldap_link();
211 /* Get current objectClasses in order to add the required ones */
212 $ldap->cat($this->dn);
213 $tmp= $ldap->fetch ();
214 if (isset($tmp['objectClass'])){
215 $oc= $tmp['objectClass'];
216 } else {
217 $oc= array("count" => 0);
218 }
220 /* Remove objectClasses from entry */
221 $ldap->cd($this->dn);
222 $this->attrs= array();
223 $this->attrs['objectClass']= array();
224 for ($i= 0; $i<$oc["count"]; $i++){
225 if (!in_array_ics($oc[$i], $this->objectclasses)){
226 $this->attrs['objectClass'][]= $oc[$i];
227 }
228 }
230 /* Unset attributes from entry */
231 foreach ($this->attributes as $val){
232 $this->attrs["$val"]= array();
233 }
235 /* Unset account info */
236 $this->is_account= FALSE;
238 /* Do not write in plugin base class, this must be done by
239 children, since there are normally additional attribs,
240 lists, etc. */
241 /*
242 $ldap->modify($this->attrs);
243 */
244 }
247 /* Save data to object */
248 function save_object()
249 {
250 /* Save values to object */
251 foreach ($this->attributes as $val){
252 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
253 /* Check for modifications */
254 if (get_magic_quotes_gpc()) {
255 $data= stripcslashes($_POST["$val"]);
256 } else {
257 $data= $this->$val = $_POST["$val"];
258 }
259 if ($this->$val != $data){
260 $this->is_modified= TRUE;
261 }
263 /* Okay, how can I explain this fix ...
264 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
265 * So IE posts these 'unselectable' option, with value = chr(194)
266 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
267 * This was added for W3c compliance, but now causes these ... ldap errors ...
268 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
269 */
270 if(isset($data[0]) && $data[0] == chr(194)) {
271 $data = "";
272 }
273 $this->$val= $data;
274 }
275 }
276 }
279 /* Save data to LDAP, depending on is_account we save or delete */
280 function save()
281 {
282 /* include global link_info */
283 $ldap= $this->config->get_ldap_link();
285 /* Start with empty array */
286 $this->attrs= array();
288 /* Get current objectClasses in order to add the required ones */
289 $ldap->cat($this->dn);
291 $tmp= $ldap->fetch ();
293 if (isset($tmp['objectClass'])){
294 $oc= $tmp["objectClass"];
295 $this->new= FALSE;
296 } else {
297 $oc= array("count" => 0);
298 $this->new= TRUE;
299 }
301 /* Load (minimum) attributes, add missing ones */
302 $this->attrs['objectClass']= $this->objectclasses;
303 for ($i= 0; $i<$oc["count"]; $i++){
304 if (!in_array_ics($oc[$i], $this->objectclasses)){
305 $this->attrs['objectClass'][]= $oc[$i];
306 }
307 }
309 /* Copy standard attributes */
310 foreach ($this->attributes as $val){
311 if ($this->$val != ""){
312 $this->attrs["$val"]= $this->$val;
313 } elseif (!$this->new) {
314 $this->attrs["$val"]= array();
315 }
316 }
318 }
321 function cleanup()
322 {
323 foreach ($this->attrs as $index => $value){
325 /* Convert arrays with one element to non arrays, if the saved
326 attributes are no array, too */
327 if (is_array($this->attrs[$index]) &&
328 count ($this->attrs[$index]) == 1 &&
329 isset($this->saved_attributes[$index]) &&
330 !is_array($this->saved_attributes[$index])){
332 $tmp= $this->attrs[$index][0];
333 $this->attrs[$index]= $tmp;
334 }
336 /* Remove emtpy arrays if they do not differ */
337 if (is_array($this->attrs[$index]) &&
338 count($this->attrs[$index]) == 0 &&
339 !isset($this->saved_attributes[$index])){
341 unset ($this->attrs[$index]);
342 continue;
343 }
345 /* Remove single attributes that do not differ */
346 if (!is_array($this->attrs[$index]) &&
347 isset($this->saved_attributes[$index]) &&
348 !is_array($this->saved_attributes[$index]) &&
349 $this->attrs[$index] == $this->saved_attributes[$index]){
351 unset ($this->attrs[$index]);
352 continue;
353 }
355 /* Remove arrays that do not differ */
356 if (is_array($this->attrs[$index]) &&
357 isset($this->saved_attributes[$index]) &&
358 is_array($this->saved_attributes[$index])){
360 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
361 unset ($this->attrs[$index]);
362 continue;
363 }
364 }
365 }
367 #######################################
368 ; if (count($this->attrs)!=0){ ;
369 ; echo "--- DEBUG: changes ---<br>" ;
370 ; print_a ($this->attrs) ;
371 ; } ;
372 #######################################
373 }
375 /* Check formular input */
376 function check()
377 {
378 $message= array();
379 return ($message);
380 }
382 /* Adapt from template, using 'dn' */
383 function adapt_from_template($dn)
384 {
385 /* Include global link_info */
386 $ldap= $this->config->get_ldap_link();
388 /* Load requested 'dn' to 'attrs' */
389 $ldap->cat ($dn);
390 $this->attrs= $ldap->fetch();
392 /* Walk through attributes */
393 foreach ($this->attributes as $val){
395 if (isset($this->attrs["$val"][0])){
397 /* If attribute is set, replace dynamic parts:
398 %sn, %givenName and %uid. Fill these in our local variables. */
399 $value= $this->attrs["$val"][0];
401 foreach (array("sn", "givenName", "uid") as $repl){
402 if (preg_match("/%$repl/i", $value)){
403 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
404 }
405 }
406 $this->$val= $value;
407 }
408 }
410 /* Is Account? */
411 $found= TRUE;
412 foreach ($this->objectclasses as $obj){
413 if (preg_match('/top/i', $obj)){
414 continue;
415 }
416 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
417 $found= FALSE;
418 break;
419 }
420 }
421 if ($found){
422 $this->is_account= TRUE;
423 }
424 }
426 /* Indicate whether a password change is needed or not */
427 function password_change_needed()
428 {
429 return FALSE;
430 }
432 /* Show header message for tab dialogs */
433 function show_header($button_text, $text, $disabled= FALSE)
434 {
435 if ($disabled == TRUE){
436 $state= "disabled";
437 } else {
438 $state= "";
439 }
440 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
441 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
442 chkacl($this->acl, "all")." ".$state.
443 "><p class=\"seperator\"> </p></td></tr></table>";
445 return($display);
446 }
448 function postcreate()
449 {
450 /* Find postcreate entries for this class */
451 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
452 if ($command == "" && isset($this->config->data['TABS'])){
453 $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
454 }
456 if ($command != ""){
457 /* Walk through attribute list */
458 foreach ($this->attributes as $attr){
459 if (!is_array($this->$attr)){
460 $command= preg_replace("/%$attr/", $this->$attr, $command);
461 }
462 }
463 $command= preg_replace("/%dn/", $this->dn, $command);
464 if (check_command($command)){
465 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
466 $command, "Execute");
468 exec($command);
469 } else {
470 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
471 print_red ($message);
472 }
473 }
474 }
476 function postmodify()
477 {
478 /* Find postcreate entries for this class */
479 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
480 if ($command == "" && isset($this->config->data['TABS'])){
481 $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
482 }
484 if ($command != ""){
485 /* Walk through attribute list */
486 foreach ($this->attributes as $attr){
487 if (!is_array($this->$attr)){
488 $command= preg_replace("/%$attr/", $this->$attr, $command);
489 }
490 }
491 $command= preg_replace("/%dn/", $this->dn, $command);
492 if (check_command($command)){
493 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
494 $command, "Execute");
496 exec($command);
497 } else {
498 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
499 print_red ($message);
500 }
501 }
502 }
504 function postremove()
505 {
506 /* Find postremove entries for this class */
507 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
508 if ($command == "" && isset($this->config->data['TABS'])){
509 $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
510 }
512 if ($command != ""){
513 /* Walk through attribute list */
514 foreach ($this->attributes as $attr){
515 if (!is_array($this->$attr)){
516 $command= preg_replace("/%$attr/", $this->$attr, $command);
517 }
518 }
519 $command= preg_replace("/%dn/", $this->dn, $command);
520 if (check_command($command)){
521 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
522 $command, "Execute");
524 exec($command);
525 } else {
526 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
527 print_red ($message);
528 }
529 }
530 }
532 /* Create unique DN */
533 function create_unique_dn($attribute, $base)
534 {
535 $ldap= $this->config->get_ldap_link();
536 $base= preg_replace("/^,*/", "", $base);
538 /* Try to use plain entry first */
539 $dn= "$attribute=".$this->$attribute.",$base";
540 $ldap->cat ($dn);
541 if (!$ldap->fetch()){
542 return ($dn);
543 }
545 /* Look for additional attributes */
546 foreach ($this->attributes as $attr){
547 if ($attr == $attribute || $this->$attr == ""){
548 continue;
549 }
551 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
552 $ldap->cat ($dn);
553 if (!$ldap->fetch()){
554 return ($dn);
555 }
556 }
558 /* None found */
559 return ("none");
560 }
562 function rebind($ldap, $referral)
563 {
564 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
565 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
566 $this->error = "Success";
567 $this->hascon=true;
568 $this->reconnect= true;
569 return (0);
570 } else {
571 $this->error = "Could not bind to " . $credentials['ADMIN'];
572 return NULL;
573 }
574 }
576 /* This is a workaround function. */
577 function copy($src_dn, $dst_dn)
578 {
579 /* Rename dn in possible object groups */
580 $ldap= $this->config->get_ldap_link();
581 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
582 array('cn'));
583 while ($attrs= $ldap->fetch()){
584 $og= new ogroup($this->config, $ldap->getDN());
585 unset($og->member[$src_dn]);
586 $og->member[$dst_dn]= $dst_dn;
587 $og->save ();
588 }
590 $ldap->cat($dst_dn);
591 $attrs= $ldap->fetch();
592 if (count($attrs)){
593 trigger_error("Trying to overwrite $dst_dn, which already exists.",
594 E_USER_WARNING);
595 return (FALSE);
596 }
598 $ldap->cat($src_dn);
599 $attrs= array();
600 $attrs= $ldap->fetch();
601 if (!count($attrs)){
602 trigger_error("Trying to move $src_dn, which does not seem to exist.",
603 E_USER_WARNING);
604 return (FALSE);
605 }
607 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
608 $ds= ldap_connect($this->config->current['SERVER']);
609 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
610 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
611 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
612 }
614 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
615 error_reporting (0);
616 $sr=ldap_read($ds, $src_dn, "objectClass=*");
618 /* Fill data from LDAP */
619 $new= array();
620 if ($sr) {
621 $ei=ldap_first_entry($ds, $sr);
622 if ($ei) {
623 foreach($attrs as $attr => $val){
624 if ($info = ldap_get_values_len($ds, $ei, $attr)){
625 for ($i= 0; $i<$info['count']; $i++){
626 if ($info['count'] == 1){
627 $new[$attr]= $info[$i];
628 } else {
629 $new[$attr][]= $info[$i];
630 }
631 }
632 }
633 }
634 }
635 }
637 /* close conncetion */
638 error_reporting (E_ALL);
639 ldap_unbind($ds);
641 /* Adapt naming attribute */
642 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
643 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
644 $new[$dst_name]= $dst_val;
646 /* Save copy */
647 $ldap->connect();
648 $ldap->cd($this->config->current['BASE']);
649 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
650 $ldap->cd($dst_dn);
651 $ldap->add($new);
653 if ($ldap->error != "Success"){
654 trigger_error("Trying to save $dst_dn failed.",
655 E_USER_WARNING);
656 return(FALSE);
657 }
659 return (TRUE);
660 }
663 function move($src_dn, $dst_dn)
664 {
665 /* Copy source to destination */
666 if (!$this->copy($src_dn, $dst_dn)){
667 return (FALSE);
668 }
670 /* Delete source */
671 $ldap= $this->config->get_ldap_link();
672 $ldap->rmdir($src_dn);
673 if ($ldap->error != "Success"){
674 trigger_error("Trying to delete $src_dn failed.",
675 E_USER_WARNING);
676 return (FALSE);
677 }
679 return (TRUE);
680 }
683 /* Move/Rename complete trees */
684 function recursive_move($src_dn, $dst_dn)
685 {
686 /* Check if the destination entry exists */
687 $ldap= $this->config->get_ldap_link();
689 /* Check if destination exists - abort */
690 $ldap->cat($dst_dn);
691 if ($ldap->fetch()){
692 trigger_error("recursive_move $dst_dn already exists.",
693 E_USER_WARNING);
694 return (FALSE);
695 }
697 /* Perform a search for all objects to be moved */
698 $objects= array();
699 $ldap->cd($src_dn);
700 $ldap->search("(objectClass=*)", array("dn"));
701 while($attrs= $ldap->fetch()){
702 $dn= $attrs['dn'];
703 $objects[$dn]= strlen($dn);
704 }
706 /* Sort objects by indent level */
707 asort($objects);
708 reset($objects);
710 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
711 foreach ($objects as $object => $len){
712 $src= $object;
713 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
714 if (!$this->copy($src, $dst)){
715 return (FALSE);
716 }
717 }
719 /* Remove src_dn */
720 $ldap->cd($src_dn);
721 $ldap->recursive_remove();
722 return (TRUE);
723 }
726 function handle_post_events($mode)
727 {
728 switch ($mode){
729 case "add":
730 $this->postcreate();
731 break;
733 case "modify":
734 $this->postmodify();
735 break;
737 case "remove":
738 $this->postremove();
739 break;
740 }
741 }
743 }
745 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
746 ?>