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 # This one is empty currently. Fabian - please fill in the docu code
178 $_SESSION['current_class_for_help'] = get_class($this);
179 }
181 /* remove object from parent */
182 function remove_from_parent()
183 {
184 /* include global link_info */
185 $ldap= $this->config->get_ldap_link();
187 /* Get current objectClasses in order to add the required ones */
188 $ldap->cat($this->dn);
189 $tmp= $ldap->fetch ();
190 if (isset($tmp['objectClass'])){
191 $oc= $tmp['objectClass'];
192 } else {
193 $oc= array("count" => 0);
194 }
196 /* Remove objectClasses from entry */
197 $ldap->cd($this->dn);
198 $this->attrs= array();
199 $this->attrs['objectClass']= array();
200 for ($i= 0; $i<$oc["count"]; $i++){
201 if (!in_array_ics($oc[$i], $this->objectclasses)){
202 $this->attrs['objectClass'][]= $oc[$i];
203 }
204 }
206 /* Unset attributes from entry */
207 foreach ($this->attributes as $val){
208 $this->attrs["$val"]= array();
209 }
211 /* Unset account info */
212 $this->is_account= FALSE;
214 /* Do not write in plugin base class, this must be done by
215 children, since there are normally additional attribs,
216 lists, etc. */
217 /*
218 $ldap->modify($this->attrs);
219 */
220 }
223 /* Save data to object */
224 function save_object()
225 {
226 /* Save values to object */
227 foreach ($this->attributes as $val){
228 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
229 /* Check for modifications */
230 if (get_magic_quotes_gpc()) {
231 $data= stripcslashes($_POST["$val"]);
232 } else {
233 $data= $this->$val = $_POST["$val"];
234 }
235 if ($this->$val != $data){
236 $this->is_modified= TRUE;
237 }
239 /* Okay, how can I explain this fix ...
240 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
241 * So IE posts these 'unselectable' option, with value = chr(194)
242 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
243 * This was added for W3c compliance, but now causes these ... ldap errors ...
244 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
245 */
246 if(isset($data[0]) && $data[0] == chr(194)) {
247 $data = "";
248 }
249 $this->$val= $data;
250 }
251 }
252 }
255 /* Save data to LDAP, depending on is_account we save or delete */
256 function save()
257 {
258 /* include global link_info */
259 $ldap= $this->config->get_ldap_link();
261 /* Start with empty array */
262 $this->attrs= array();
264 /* Get current objectClasses in order to add the required ones */
265 $ldap->cat($this->dn);
267 $tmp= $ldap->fetch ();
269 if (isset($tmp['objectClass'])){
270 $oc= $tmp["objectClass"];
271 $this->new= FALSE;
272 } else {
273 $oc= array("count" => 0);
274 $this->new= TRUE;
275 }
277 /* Load (minimum) attributes, add missing ones */
278 $this->attrs['objectClass']= $this->objectclasses;
279 for ($i= 0; $i<$oc["count"]; $i++){
280 if (!in_array_ics($oc[$i], $this->objectclasses)){
281 $this->attrs['objectClass'][]= $oc[$i];
282 }
283 }
285 /* Copy standard attributes */
286 foreach ($this->attributes as $val){
287 if ($this->$val != ""){
288 $this->attrs["$val"]= $this->$val;
289 } elseif (!$this->new) {
290 $this->attrs["$val"]= array();
291 }
292 }
294 }
296 /* Check formular input */
297 function check()
298 {
299 $message= array();
300 return ($message);
301 }
303 /* Adapt from template, using 'dn' */
304 function adapt_from_template($dn)
305 {
306 /* Include global link_info */
307 $ldap= $this->config->get_ldap_link();
309 /* Load requested 'dn' to 'attrs' */
310 $ldap->cat ($dn);
311 $this->attrs= $ldap->fetch();
313 /* Walk through attributes */
314 foreach ($this->attributes as $val){
316 if (isset($this->attrs["$val"][0])){
318 /* If attribute is set, replace dynamic parts:
319 %sn, %givenName and %uid. Fill these in our local variables. */
320 $value= $this->attrs["$val"][0];
322 foreach (array("sn", "givenName", "uid") as $repl){
323 if (preg_match("/%$repl/i", $value)){
324 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
325 }
326 }
327 $this->$val= $value;
328 }
329 }
331 /* Is Account? */
332 $found= TRUE;
333 foreach ($this->objectclasses as $obj){
334 if (preg_match('/top/i', $obj)){
335 continue;
336 }
337 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
338 $found= FALSE;
339 break;
340 }
341 }
342 if ($found){
343 $this->is_account= TRUE;
344 }
345 }
347 /* Indicate whether a password change is needed or not */
348 function password_change_needed()
349 {
350 return FALSE;
351 }
353 /* Show header message for tab dialogs */
354 function show_header($button_text, $text, $disabled= FALSE)
355 {
356 if ($disabled == TRUE){
357 $state= "disabled";
358 } else {
359 $state= "";
360 }
361 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
362 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
363 chkacl($this->acl, "all")." ".$state.
364 "><p class=\"seperator\"> </p></td></tr></table>";
366 return($display);
367 }
369 function postcreate()
370 {
371 /* Find postcreate entries for this class */
372 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
373 if ($command == "" && isset($this->config->data['TABS'])){
374 $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
375 }
377 if ($command != ""){
378 /* Walk through attribute list */
379 foreach ($this->attributes as $attr){
380 if (!is_array($this->$attr)){
381 $command= preg_replace("/%$attr/", $this->$attr, $command);
382 }
383 }
384 $command= preg_replace("/%dn/", $this->dn, $command);
385 if (check_command($command)){
386 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
387 $command, "Execute");
389 exec($command);
390 } else {
391 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
392 print_red ($message);
393 }
394 }
395 }
397 function postmodify()
398 {
399 /* Find postcreate entries for this class */
400 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
401 if ($command == "" && isset($this->config->data['TABS'])){
402 $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
403 }
405 if ($command != ""){
406 /* Walk through attribute list */
407 foreach ($this->attributes as $attr){
408 if (!is_array($this->$attr)){
409 $command= preg_replace("/%$attr/", $this->$attr, $command);
410 }
411 }
412 $command= preg_replace("/%dn/", $this->dn, $command);
413 if (check_command($command)){
414 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
415 $command, "Execute");
417 exec($command);
418 } else {
419 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
420 print_red ($message);
421 }
422 }
423 }
425 function postremove()
426 {
427 /* Find postremove entries for this class */
428 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
429 if ($command == "" && isset($this->config->data['TABS'])){
430 $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
431 }
433 if ($command != ""){
434 /* Walk through attribute list */
435 foreach ($this->attributes as $attr){
436 if (!is_array($this->$attr)){
437 $command= preg_replace("/%$attr/", $this->$attr, $command);
438 }
439 }
440 $command= preg_replace("/%dn/", $this->dn, $command);
441 if (check_command($command)){
442 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
443 $command, "Execute");
445 exec($command);
446 } else {
447 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
448 print_red ($message);
449 }
450 }
451 }
453 /* Create unique DN */
454 function create_unique_dn($attribute, $base)
455 {
456 $ldap= $this->config->get_ldap_link();
457 $base= preg_replace("/^,*/", "", $base);
459 /* Try to use plain entry first */
460 $dn= "$attribute=".$this->$attribute.",$base";
461 $ldap->cat ($dn);
462 if (!$ldap->fetch()){
463 return ($dn);
464 }
466 /* Look for additional attributes */
467 foreach ($this->attributes as $attr){
468 if ($attr == $attribute || $this->$attr == ""){
469 continue;
470 }
472 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
473 $ldap->cat ($dn);
474 if (!$ldap->fetch()){
475 return ($dn);
476 }
477 }
479 /* None found */
480 return ("none");
481 }
483 function rebind($ldap, $referral)
484 {
485 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
486 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
487 $this->error = "Success";
488 $this->hascon=true;
489 $this->reconnect= true;
490 return (0);
491 } else {
492 $this->error = "Could not bind to " . $credentials['ADMIN'];
493 return NULL;
494 }
495 }
497 /* This is a workaround function. */
498 function copy($src_dn, $dst_dn)
499 {
500 /* Rename dn in possible object groups */
501 $ldap= $this->config->get_ldap_link();
502 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
503 array('cn'));
504 while ($attrs= $ldap->fetch()){
505 $og= new ogroup($this->config, $ldap->getDN());
506 unset($og->member[$src_dn]);
507 $og->member[$dst_dn]= $dst_dn;
508 $og->save ();
509 }
511 $ldap->cat($dst_dn);
512 $attrs= $ldap->fetch();
513 if (count($attrs)){
514 trigger_error("Trying to overwrite $dst_dn, which already exists.",
515 E_USER_WARNING);
516 return (FALSE);
517 }
519 $ldap->cat($src_dn);
520 $attrs= array();
521 $attrs= $ldap->fetch();
522 if (!count($attrs)){
523 trigger_error("Trying to move $src_dn, which does not seem to exist.",
524 E_USER_WARNING);
525 return (FALSE);
526 }
528 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
529 $ds= ldap_connect($this->config->current['SERVER']);
530 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
531 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
532 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
533 }
535 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
536 error_reporting (0);
537 $sr=ldap_read($ds, $src_dn, "objectClass=*");
539 /* Fill data from LDAP */
540 $new= array();
541 if ($sr) {
542 $ei=ldap_first_entry($ds, $sr);
543 if ($ei) {
544 foreach($attrs as $attr => $val){
545 if ($info = ldap_get_values_len($ds, $ei, $attr)){
546 for ($i= 0; $i<$info['count']; $i++){
547 if ($info['count'] == 1){
548 $new[$attr]= $info[$i];
549 } else {
550 $new[$attr][]= $info[$i];
551 }
552 }
553 }
554 }
555 }
556 }
558 /* close conncetion */
559 error_reporting (E_ALL);
560 ldap_unbind($ds);
562 /* Adapt naming attribute */
563 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
564 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
565 $new[$dst_name]= $dst_val;
567 /* Save copy */
568 $ldap->connect();
569 $ldap->cd($this->config->current['BASE']);
570 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
571 $ldap->cd($dst_dn);
572 $ldap->add($new);
574 if ($ldap->error != "Success"){
575 trigger_error("Trying to save $dst_dn failed.",
576 E_USER_WARNING);
577 return(FALSE);
578 }
580 return (TRUE);
581 }
584 function move($src_dn, $dst_dn)
585 {
586 /* Copy source to destination */
587 if (!$this->copy($src_dn, $dst_dn)){
588 return (FALSE);
589 }
591 /* Delete source */
592 $ldap= $this->config->get_ldap_link();
593 $ldap->rmdir($src_dn);
594 if ($ldap->error != "Success"){
595 trigger_error("Trying to delete $src_dn failed.",
596 E_USER_WARNING);
597 return (FALSE);
598 }
600 return (TRUE);
601 }
604 /* Move/Rename complete trees */
605 function recursive_move($src_dn, $dst_dn)
606 {
607 /* Check if the destination entry exists */
608 $ldap= $this->config->get_ldap_link();
610 /* Check if destination exists - abort */
611 $ldap->cat($dst_dn);
612 if ($ldap->fetch()){
613 trigger_error("recursive_move $dst_dn already exists.",
614 E_USER_WARNING);
615 return (FALSE);
616 }
618 /* Perform a search for all objects to be moved */
619 $objects= array();
620 $ldap->cd($src_dn);
621 $ldap->search("(objectClass=*)", array("dn"));
622 while($attrs= $ldap->fetch()){
623 $dn= $attrs['dn'];
624 $objects[$dn]= strlen($dn);
625 }
627 /* Sort objects by indent level */
628 asort($objects);
629 reset($objects);
631 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
632 foreach ($objects as $object => $len){
633 $src= $object;
634 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
635 if (!$this->copy($src, $dst)){
636 return (FALSE);
637 }
638 }
640 /* Remove src_dn */
641 $ldap->cd($src_dn);
642 $ldap->recursive_remove();
643 return (TRUE);
644 }
647 function handle_post_events($mode)
648 {
649 switch ($mode){
650 case "add":
651 $this->postcreate();
652 break;
654 case "modify":
655 $this->postmodify();
656 break;
658 case "remove":
659 $this->postremove();
660 break;
661 }
662 }
665 }
667 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
668 ?>