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;
104 /*! \brief plugin constructor
106 If 'dn' is set, the node loads the given 'dn' from LDAP
108 \param dn Distinguished name to initialize plugin from
109 \sa plugin()
110 */
111 function plugin ($config, $dn= NULL)
112 {
113 /* Configuration is fine, allways */
114 $this->config= $config;
115 $this->dn= $dn;
117 /* Handle new accounts, don't read information from LDAP */
118 if ($dn == "new"){
119 return;
120 }
122 /* Get LDAP descriptor */
123 $ldap= $this->config->get_ldap_link();
124 if ($dn != NULL){
126 /* Load data to 'attrs' and save 'dn' */
127 $ldap->cat ($dn);
128 $this->attrs= $ldap->fetch();
130 /* Copy needed attributes */
131 foreach ($this->attributes as $val){
132 if (isset($this->attrs["$val"][0])){
133 $this->$val= $this->attrs["$val"][0];
134 }
135 }
137 /* Set the template flag according to the existence of objectClass
138 gosaUserTemplate */
139 if (isset($this->attrs['objectClass'])){
140 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
141 $this->is_template= TRUE;
142 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
143 "found", "Template check");
144 }
145 }
147 /* Is Account? */
148 error_reporting(0);
149 $found= TRUE;
150 foreach ($this->objectclasses as $obj){
151 if (preg_match('/top/i', $obj)){
152 continue;
153 }
154 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
155 $found= FALSE;
156 break;
157 }
158 }
159 error_reporting(E_ALL);
160 if ($found){
161 $this->is_account= TRUE;
162 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
163 "found", "Object check");
164 }
165 }
167 /* Save initial account state */
168 $this->initially_was_account= $this->is_account;
169 }
171 /*! \brief execute plugin
173 Generates the html output for this node
174 */
175 function execute()
176 {
177 /* Do we represent a valid account? */
178 if (!$this->is_account){
179 echo "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
180 _("This 'dn' has no account extensions.")."</b>";
181 return;
182 }
184 /* Show dummy message */
185 echo _("This is an empty plugin.");
186 }
188 /* remove object from parent */
189 function remove_from_parent()
190 {
191 /* include global link_info */
192 $ldap= $this->config->get_ldap_link();
194 /* Get current objectClasses in order to add the required ones */
195 $ldap->cat($this->dn);
196 $tmp= $ldap->fetch ();
197 if (isset($tmp['objectClass'])){
198 $oc= $tmp['objectClass'];
199 } else {
200 $oc= array("count" => 0);
201 }
203 /* Remove objectClasses from entry */
204 $ldap->cd($this->dn);
205 $this->attrs= array();
206 $this->attrs['objectClass']= array();
207 for ($i= 0; $i<$oc["count"]; $i++){
208 if (!in_array_ics($oc[$i], $this->objectclasses)){
209 $this->attrs['objectClass'][]= $oc[$i];
210 }
211 }
213 /* Unset attributes from entry */
214 foreach ($this->attributes as $val){
215 $this->attrs["$val"]= array();
216 }
218 /* Unset account info */
219 $this->is_account= FALSE;
221 /* Do not write in plugin base class, this must be done by
222 children, since there are normally additional attribs,
223 lists, etc. */
224 /*
225 $ldap->modify($this->attrs);
226 */
227 }
230 /* Save data to object */
231 function save_object()
232 {
233 /* Save values to object */
234 foreach ($this->attributes as $val){
235 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
236 /* Check for modifications */
237 if (get_magic_quotes_gpc()) {
238 $data= stripcslashes($_POST["$val"]);
239 } else {
240 $data= $this->$val = $_POST["$val"];
241 }
242 if ($this->$val != $data){
243 $this->is_modified= TRUE;
244 }
246 /* Okay, how can I explain this fix ...
247 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
248 * So IE posts these 'unselectable' option, with value = chr(194)
249 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
250 * This was added for W3c compliance, but now causes these ... ldap errors ...
251 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
252 */
253 if(isset($data[0])){
254 if($data[0] == chr(194)) {
255 $data = "";
256 }
257 }
258 $this->$val= $data;
259 }
260 }
261 }
264 /* Save data to LDAP, depending on is_account we save or delete */
265 function save()
266 {
267 /* include global link_info */
268 $ldap= $this->config->get_ldap_link();
270 /* Start with empty array */
271 $this->attrs= array();
273 /* Get current objectClasses in order to add the required ones */
274 $ldap->cat($this->dn);
276 $tmp= $ldap->fetch ();
278 if (isset($tmp['objectClass'])){
279 $oc= $tmp["objectClass"];
280 $this->new= FALSE;
281 } else {
282 $oc= array("count" => 0);
283 $this->new= TRUE;
284 }
286 /* Load (minimum) attributes, add missing ones */
287 $this->attrs['objectClass']= $this->objectclasses;
288 for ($i= 0; $i<$oc["count"]; $i++){
289 if (!in_array_ics($oc[$i], $this->objectclasses)){
290 $this->attrs['objectClass'][]= $oc[$i];
291 }
292 }
294 /* Copy standard attributes */
295 foreach ($this->attributes as $val){
296 if ($this->$val != ""){
297 $this->attrs["$val"]= $this->$val;
298 } elseif (!$this->new) {
299 $this->attrs["$val"]= array();
300 }
301 }
303 }
305 /* Check formular input */
306 function check()
307 {
308 $message= array();
309 return ($message);
310 }
312 /* Adapt from template, using 'dn' */
313 function adapt_from_template($dn)
314 {
315 /* Include global link_info */
316 $ldap= $this->config->get_ldap_link();
318 /* Load requested 'dn' to 'attrs' */
319 $ldap->cat ($dn);
320 $this->attrs= $ldap->fetch();
322 /* Walk through attributes */
323 foreach ($this->attributes as $val){
325 if (isset($this->attrs["$val"][0])){
327 /* If attribute is set, replace dynamic parts:
328 %sn, %givenName and %uid. Fill these in our local variables. */
329 $value= $this->attrs["$val"][0];
331 foreach (array("sn", "givenName", "uid") as $repl){
332 if (preg_match("/%$repl/i", $value)){
333 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
334 }
335 }
336 $this->$val= $value;
337 }
338 }
340 /* Is Account? */
341 $found= TRUE;
342 foreach ($this->objectclasses as $obj){
343 if (preg_match('/top/i', $obj)){
344 continue;
345 }
346 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
347 $found= FALSE;
348 break;
349 }
350 }
351 if ($found){
352 $this->is_account= TRUE;
353 }
354 }
356 /* Indicate whether a password change is needed or not */
357 function password_change_needed()
358 {
359 return FALSE;
360 }
362 /* Show header message for tab dialogs */
363 function show_header($button_text, $text, $disabled= FALSE)
364 {
365 if ($disabled == TRUE){
366 $state= "disabled";
367 } else {
368 $state= "";
369 }
370 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
371 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
372 chkacl($this->acl, "all")." ".$state.
373 "><p class=\"seperator\"> </p></td></tr></table>";
375 return($display);
376 }
378 function postcreate()
379 {
380 /* Find postcreate entries for this class */
381 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
382 if ($command == "" && isset($this->config->data['TAB'])){
383 $command= search_config($this->config->data['TAB'], get_class($this), "POSTCREATE");
384 }
386 if ($command != ""){
387 /* Walk through attribute list */
388 foreach ($this->attributes as $attr){
389 $command= preg_replace("/%$attr/", $this->$attr, $command);
390 }
391 $command= preg_replace("/%dn/", $this->dn, $command);
392 if (check_command($command)){
393 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
394 $command, "Execute");
396 exec($command);
397 } else {
398 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
399 print_red ($message);
400 }
401 }
402 }
404 function postmodify()
405 {
406 /* Find postcreate entries for this class */
407 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
408 if ($command == "" && isset($this->config->data['TAB'])){
409 $command= search_config($this->config->data['TAB'], get_class($this), "POSTMODIFY");
410 }
412 if ($command != ""){
413 /* Walk through attribute list */
414 foreach ($this->attributes as $attr){
415 $command= preg_replace("/%$attr/", $this->$attr, $command);
416 }
417 $command= preg_replace("/%dn/", $this->dn, $command);
418 if (check_command($command)){
419 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
420 $command, "Execute");
422 exec($command);
423 } else {
424 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
425 print_red ($message);
426 }
427 }
428 }
430 function postremove()
431 {
432 /* Find postremove entries for this class */
433 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
434 if ($command == "" && isset($this->config->data['TAB'])){
435 $command= search_config($this->config->data['TAB'], get_class($this), "POSTREMOVE");
436 }
438 if ($command != ""){
439 /* Walk through attribute list */
440 foreach ($this->attributes as $attr){
441 $command= preg_replace("/%$attr/", $this->$attr, $command);
442 }
443 $command= preg_replace("/%dn/", $this->dn, $command);
444 if (check_command($command)){
445 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
446 $command, "Execute");
448 exec($command);
449 } else {
450 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
451 print_red ($message);
452 }
453 }
454 }
456 /* Create unique DN */
457 function create_unique_dn($attribute, $base)
458 {
459 $ldap= $this->config->get_ldap_link();
460 $base= preg_replace("/^,*/", "", $base);
462 /* Try to use plain entry first */
463 $dn= "$attribute=".$this->$attribute.",$base";
464 $ldap->cat ($dn);
465 if (!$ldap->fetch()){
466 return ($dn);
467 }
469 /* Look for additional attributes */
470 foreach ($this->attributes as $attr){
471 if ($attr == $attribute || $this->$attr == ""){
472 continue;
473 }
475 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
476 $ldap->cat ($dn);
477 if (!$ldap->fetch()){
478 return ($dn);
479 }
480 }
482 /* None found */
483 return ("none");
484 }
486 function rebind($ldap, $referral)
487 {
488 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
489 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
490 $this->error = "Success";
491 $this->hascon=true;
492 $this->reconnect= true;
493 return (0);
494 } else {
495 $this->error = "Could not bind to " . $credentials['ADMIN'];
496 return NULL;
497 }
498 }
500 /* This is a workaround function. */
501 function copy($src_dn, $dst_dn)
502 {
503 /* Rename dn in possible object groups */
504 $ldap= $this->config->get_ldap_link();
505 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
506 array('cn'));
507 while ($attrs= $ldap->fetch()){
508 $og= new ogroup($this->config, $ldap->getDN());
509 unset($og->member[$src_dn]);
510 $og->member[$dst_dn]= $dst_dn;
511 $og->save ();
512 }
514 $ldap->cat($dst_dn);
515 $attrs= $ldap->fetch();
516 if (count($attrs)){
517 trigger_error("Trying to overwrite $dst_dn, which already exists.",
518 E_USER_WARNING);
519 return (FALSE);
520 }
522 $ldap->cat($src_dn);
523 $attrs= array();
524 $attrs= $ldap->fetch();
525 if (!count($attrs)){
526 trigger_error("Trying to move $src_dn, which does not seem to exist.",
527 E_USER_WARNING);
528 return (FALSE);
529 }
531 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
532 $ds= ldap_connect($this->config->current['SERVER']);
533 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
534 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
535 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
536 }
538 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
539 error_reporting (0);
540 $sr=ldap_read($ds, $src_dn, "objectClass=*");
542 /* Fill data from LDAP */
543 $new= array();
544 if ($sr) {
545 $ei=ldap_first_entry($ds, $sr);
546 if ($ei) {
547 foreach($attrs as $attr => $val){
548 if ($info = ldap_get_values_len($ds, $ei, $attr)){
549 for ($i= 0; $i<$info['count']; $i++){
550 if ($info['count'] == 1){
551 $new[$attr]= $info[$i];
552 } else {
553 $new[$attr][]= $info[$i];
554 }
555 }
556 }
557 }
558 }
559 }
561 /* close conncetion */
562 error_reporting (E_ALL);
563 ldap_unbind($ds);
565 /* Adapt naming attribute */
566 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
567 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
568 $new[$dst_name]= $dst_val;
570 /* Save copy */
571 $ldap->connect();
572 $ldap->cd($this->config->current['BASE']);
573 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
574 $ldap->cd($dst_dn);
575 $ldap->add($new);
577 if ($ldap->error != "Success"){
578 trigger_error("Trying to save $dst_dn failed.",
579 E_USER_WARNING);
580 return(FALSE);
581 }
583 return (TRUE);
584 }
587 function move($src_dn, $dst_dn)
588 {
589 /* Copy source to destination */
590 if (!$this->copy($src_dn, $dst_dn)){
591 return (FALSE);
592 }
594 /* Delete source */
595 $ldap= $this->config->get_ldap_link();
596 $ldap->rmdir($src_dn);
597 if ($ldap->error != "Success"){
598 trigger_error("Trying to delete $src_dn failed.",
599 E_USER_WARNING);
600 return (FALSE);
601 }
603 return (TRUE);
604 }
607 /* Move/Rename complete trees */
608 function recursive_move($src_dn, $dst_dn)
609 {
610 /* Check if the destination entry exists */
611 $ldap= $this->config->get_ldap_link();
613 /* Check if destination exists - abort */
614 $ldap->cat($dst_dn);
615 if ($ldap->fetch()){
616 trigger_error("recursive_move $dst_dn already exists.",
617 E_USER_WARNING);
618 return (FALSE);
619 }
621 /* Perform a search for all objects to be moved */
622 $objects= array();
623 $ldap->cd($src_dn);
624 $ldap->search("(objectClass=*)", array("dn"));
625 while($attrs= $ldap->fetch()){
626 $dn= $attrs['dn'];
627 $objects[$dn]= strlen($dn);
628 }
630 /* Sort objects by indent level */
631 asort($objects);
632 reset($objects);
634 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
635 foreach ($objects as $object => $len){
636 $src= $object;
637 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
638 if (!$this->copy($src, $dst)){
639 return (FALSE);
640 }
641 }
643 /* Remove src_dn */
644 $ldap->cd($src_dn);
645 $ldap->recursive_remove();
646 return (TRUE);
647 }
650 function handle_post_events($mode)
651 {
652 switch ($mode){
653 case "add":
654 $this->postcreate();
655 break;
657 case "modify":
658 $this->postmodify();
659 break;
661 case "remove":
662 $this->postremove();
663 break;
664 }
665 }
668 }
670 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
671 ?>