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]) && $data[0] == chr(194)) {
254 $data = "";
255 }
256 $this->$val= $data;
257 }
258 }
259 }
262 /* Save data to LDAP, depending on is_account we save or delete */
263 function save()
264 {
265 /* include global link_info */
266 $ldap= $this->config->get_ldap_link();
268 /* Start with empty array */
269 $this->attrs= array();
271 /* Get current objectClasses in order to add the required ones */
272 $ldap->cat($this->dn);
274 $tmp= $ldap->fetch ();
276 if (isset($tmp['objectClass'])){
277 $oc= $tmp["objectClass"];
278 $this->new= FALSE;
279 } else {
280 $oc= array("count" => 0);
281 $this->new= TRUE;
282 }
284 /* Load (minimum) attributes, add missing ones */
285 $this->attrs['objectClass']= $this->objectclasses;
286 for ($i= 0; $i<$oc["count"]; $i++){
287 if (!in_array_ics($oc[$i], $this->objectclasses)){
288 $this->attrs['objectClass'][]= $oc[$i];
289 }
290 }
292 /* Copy standard attributes */
293 foreach ($this->attributes as $val){
294 if ($this->$val != ""){
295 $this->attrs["$val"]= $this->$val;
296 } elseif (!$this->new) {
297 $this->attrs["$val"]= array();
298 }
299 }
301 }
303 /* Check formular input */
304 function check()
305 {
306 $message= array();
307 return ($message);
308 }
310 /* Adapt from template, using 'dn' */
311 function adapt_from_template($dn)
312 {
313 /* Include global link_info */
314 $ldap= $this->config->get_ldap_link();
316 /* Load requested 'dn' to 'attrs' */
317 $ldap->cat ($dn);
318 $this->attrs= $ldap->fetch();
320 /* Walk through attributes */
321 foreach ($this->attributes as $val){
323 if (isset($this->attrs["$val"][0])){
325 /* If attribute is set, replace dynamic parts:
326 %sn, %givenName and %uid. Fill these in our local variables. */
327 $value= $this->attrs["$val"][0];
329 foreach (array("sn", "givenName", "uid") as $repl){
330 if (preg_match("/%$repl/i", $value)){
331 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
332 }
333 }
334 $this->$val= $value;
335 }
336 }
338 /* Is Account? */
339 $found= TRUE;
340 foreach ($this->objectclasses as $obj){
341 if (preg_match('/top/i', $obj)){
342 continue;
343 }
344 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
345 $found= FALSE;
346 break;
347 }
348 }
349 if ($found){
350 $this->is_account= TRUE;
351 }
352 }
354 /* Indicate whether a password change is needed or not */
355 function password_change_needed()
356 {
357 return FALSE;
358 }
360 /* Show header message for tab dialogs */
361 function show_header($button_text, $text, $disabled= FALSE)
362 {
363 if ($disabled == TRUE){
364 $state= "disabled";
365 } else {
366 $state= "";
367 }
368 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
369 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
370 chkacl($this->acl, "all")." ".$state.
371 "><p class=\"seperator\"> </p></td></tr></table>";
373 return($display);
374 }
376 function postcreate()
377 {
378 /* Find postcreate entries for this class */
379 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
380 if ($command == "" && isset($this->config->data['TAB'])){
381 $command= search_config($this->config->data['TAB'], get_class($this), "POSTCREATE");
382 }
384 if ($command != ""){
385 /* Walk through attribute list */
386 foreach ($this->attributes as $attr){
387 $command= preg_replace("/%$attr/", $this->$attr, $command);
388 }
389 $command= preg_replace("/%dn/", $this->dn, $command);
390 if (check_command($command)){
391 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
392 $command, "Execute");
394 exec($command);
395 } else {
396 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
397 print_red ($message);
398 }
399 }
400 }
402 function postmodify()
403 {
404 /* Find postcreate entries for this class */
405 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
406 if ($command == "" && isset($this->config->data['TAB'])){
407 $command= search_config($this->config->data['TAB'], get_class($this), "POSTMODIFY");
408 }
410 if ($command != ""){
411 /* Walk through attribute list */
412 foreach ($this->attributes as $attr){
413 $command= preg_replace("/%$attr/", $this->$attr, $command);
414 }
415 $command= preg_replace("/%dn/", $this->dn, $command);
416 if (check_command($command)){
417 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
418 $command, "Execute");
420 exec($command);
421 } else {
422 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
423 print_red ($message);
424 }
425 }
426 }
428 function postremove()
429 {
430 /* Find postremove entries for this class */
431 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
432 if ($command == "" && isset($this->config->data['TAB'])){
433 $command= search_config($this->config->data['TAB'], get_class($this), "POSTREMOVE");
434 }
436 if ($command != ""){
437 /* Walk through attribute list */
438 foreach ($this->attributes as $attr){
439 $command= preg_replace("/%$attr/", $this->$attr, $command);
440 }
441 $command= preg_replace("/%dn/", $this->dn, $command);
442 if (check_command($command)){
443 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
444 $command, "Execute");
446 exec($command);
447 } else {
448 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
449 print_red ($message);
450 }
451 }
452 }
454 /* Create unique DN */
455 function create_unique_dn($attribute, $base)
456 {
457 $ldap= $this->config->get_ldap_link();
458 $base= preg_replace("/^,*/", "", $base);
460 /* Try to use plain entry first */
461 $dn= "$attribute=".$this->$attribute.",$base";
462 $ldap->cat ($dn);
463 if (!$ldap->fetch()){
464 return ($dn);
465 }
467 /* Look for additional attributes */
468 foreach ($this->attributes as $attr){
469 if ($attr == $attribute || $this->$attr == ""){
470 continue;
471 }
473 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
474 $ldap->cat ($dn);
475 if (!$ldap->fetch()){
476 return ($dn);
477 }
478 }
480 /* None found */
481 return ("none");
482 }
484 function rebind($ldap, $referral)
485 {
486 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
487 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
488 $this->error = "Success";
489 $this->hascon=true;
490 $this->reconnect= true;
491 return (0);
492 } else {
493 $this->error = "Could not bind to " . $credentials['ADMIN'];
494 return NULL;
495 }
496 }
498 /* This is a workaround function. */
499 function copy($src_dn, $dst_dn)
500 {
501 /* Rename dn in possible object groups */
502 $ldap= $this->config->get_ldap_link();
503 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
504 array('cn'));
505 while ($attrs= $ldap->fetch()){
506 $og= new ogroup($this->config, $ldap->getDN());
507 unset($og->member[$src_dn]);
508 $og->member[$dst_dn]= $dst_dn;
509 $og->save ();
510 }
512 $ldap->cat($dst_dn);
513 $attrs= $ldap->fetch();
514 if (count($attrs)){
515 trigger_error("Trying to overwrite $dst_dn, which already exists.",
516 E_USER_WARNING);
517 return (FALSE);
518 }
520 $ldap->cat($src_dn);
521 $attrs= array();
522 $attrs= $ldap->fetch();
523 if (!count($attrs)){
524 trigger_error("Trying to move $src_dn, which does not seem to exist.",
525 E_USER_WARNING);
526 return (FALSE);
527 }
529 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
530 $ds= ldap_connect($this->config->current['SERVER']);
531 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
532 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
533 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
534 }
536 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
537 error_reporting (0);
538 $sr=ldap_read($ds, $src_dn, "objectClass=*");
540 /* Fill data from LDAP */
541 $new= array();
542 if ($sr) {
543 $ei=ldap_first_entry($ds, $sr);
544 if ($ei) {
545 foreach($attrs as $attr => $val){
546 if ($info = ldap_get_values_len($ds, $ei, $attr)){
547 for ($i= 0; $i<$info['count']; $i++){
548 if ($info['count'] == 1){
549 $new[$attr]= $info[$i];
550 } else {
551 $new[$attr][]= $info[$i];
552 }
553 }
554 }
555 }
556 }
557 }
559 /* close conncetion */
560 error_reporting (E_ALL);
561 ldap_unbind($ds);
563 /* Adapt naming attribute */
564 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
565 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
566 $new[$dst_name]= $dst_val;
568 /* Save copy */
569 $ldap->connect();
570 $ldap->cd($this->config->current['BASE']);
571 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
572 $ldap->cd($dst_dn);
573 $ldap->add($new);
575 if ($ldap->error != "Success"){
576 trigger_error("Trying to save $dst_dn failed.",
577 E_USER_WARNING);
578 return(FALSE);
579 }
581 return (TRUE);
582 }
585 function move($src_dn, $dst_dn)
586 {
587 /* Copy source to destination */
588 if (!$this->copy($src_dn, $dst_dn)){
589 return (FALSE);
590 }
592 /* Delete source */
593 $ldap= $this->config->get_ldap_link();
594 $ldap->rmdir($src_dn);
595 if ($ldap->error != "Success"){
596 trigger_error("Trying to delete $src_dn failed.",
597 E_USER_WARNING);
598 return (FALSE);
599 }
601 return (TRUE);
602 }
605 /* Move/Rename complete trees */
606 function recursive_move($src_dn, $dst_dn)
607 {
608 /* Check if the destination entry exists */
609 $ldap= $this->config->get_ldap_link();
611 /* Check if destination exists - abort */
612 $ldap->cat($dst_dn);
613 if ($ldap->fetch()){
614 trigger_error("recursive_move $dst_dn already exists.",
615 E_USER_WARNING);
616 return (FALSE);
617 }
619 /* Perform a search for all objects to be moved */
620 $objects= array();
621 $ldap->cd($src_dn);
622 $ldap->search("(objectClass=*)", array("dn"));
623 while($attrs= $ldap->fetch()){
624 $dn= $attrs['dn'];
625 $objects[$dn]= strlen($dn);
626 }
628 /* Sort objects by indent level */
629 asort($objects);
630 reset($objects);
632 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
633 foreach ($objects as $object => $len){
634 $src= $object;
635 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
636 if (!$this->copy($src, $dst)){
637 return (FALSE);
638 }
639 }
641 /* Remove src_dn */
642 $ldap->cd($src_dn);
643 $ldap->recursive_remove();
644 return (TRUE);
645 }
648 function handle_post_events($mode)
649 {
650 switch ($mode){
651 case "add":
652 $this->postcreate();
653 break;
655 case "modify":
656 $this->postmodify();
657 break;
659 case "remove":
660 $this->postremove();
661 break;
662 }
663 }
666 }
668 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
669 ?>