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