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 (!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 src=\"images/stop.png\" align=center> <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 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= $this->search($this->config->data['MENU'], get_class($this), "POSTCREATE");
370 if ($command == ""){
371 $command= $this->search($this->config->data['SERVICE'], 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= $this->search($this->config->data['MENU'], get_class($this), "POSTMODIFY");
396 if ($command == ""){
397 $command= $this->search($this->config->data['SERVICE'], 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= $this->search($this->config->data['MENU'], get_class($this), "POSTREMOVE");
422 if ($command == ""){
423 $command= $this->search($this->config->data['SERVICE'], 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 function search($arr, $name, $return)
445 {
446 if (is_array($arr)){
447 foreach ($arr as $a){
448 if (isset($a['CLASS']) &&
449 strtolower($a['CLASS']) == strtolower($name)){
451 if (isset($a[$return])){
452 return ($a[$return]);
453 } else {
454 return ("");
455 }
456 } else {
457 $res= $this->search ($a, $name, $return);
458 if ($res != ""){
459 return $res;
460 }
461 }
462 }
463 }
464 return ("");
465 }
467 /* Create unique DN */
468 function create_unique_dn($attribute, $base)
469 {
470 $ldap= $this->config->get_ldap_link();
471 $base= preg_replace("/^,*/", "", $base);
473 /* Try to use plain entry first */
474 $dn= "$attribute=".$this->$attribute.",$base";
475 $ldap->cat ($dn);
476 if (!$ldap->fetch()){
477 return ($dn);
478 }
480 /* Look for additional attributes */
481 foreach ($this->attributes as $attr){
482 if ($attr == $attribute || $this->$attr == ""){
483 continue;
484 }
486 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
487 $ldap->cat ($dn);
488 if (!$ldap->fetch()){
489 return ($dn);
490 }
491 }
493 /* None found */
494 return ("none");
495 }
497 function rebind($ldap, $referral)
498 {
499 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
500 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
501 $this->error = "Success";
502 $this->hascon=true;
503 $this->reconnect= true;
504 return (0);
505 } else {
506 $this->error = "Could not bind to " . $binddn;
507 return NULL;
508 }
509 }
511 /* This is a workaround function. */
512 function copy($src_dn, $dst_dn)
513 {
514 /* Rename dn in possible object groups */
515 $ldap= $this->config->get_ldap_link();
516 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
517 array('cn'));
518 while ($attrs= $ldap->fetch()){
519 $og= new ogroup($this->config, $ldap->getDN());
520 unset($og->member[$src_dn]);
521 $og->member[$dst_dn]= $dst_dn;
522 $og->save ();
523 }
525 $ldap->cat($dst_dn);
526 $attrs= $ldap->fetch();
527 if (count($attrs)){
528 trigger_error("Trying to overwrite $dst_dn, which already exists.",
529 E_USER_WARNING);
530 return (FALSE);
531 }
533 $ldap->cat($src_dn);
534 $attrs= array();
535 $attrs= $ldap->fetch();
536 if (!count($attrs)){
537 trigger_error("Trying to move $src_dn, which does not seem to exist.",
538 E_USER_WARNING);
539 return (FALSE);
540 }
542 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
543 $ds= ldap_connect($this->config->current['SERVER']);
544 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
545 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
546 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
547 }
549 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
550 error_reporting (0);
551 $sr=ldap_read($ds, $src_dn, "objectClass=*");
553 /* Fill data from LDAP */
554 $new= array();
555 if ($sr) {
556 $ei=ldap_first_entry($ds, $sr);
557 if ($ei) {
558 foreach($attrs as $attr => $val){
559 if ($info = ldap_get_values_len($ds, $ei, $attr)){
560 for ($i= 0; $i<$info['count']; $i++){
561 if ($info['count'] == 1){
562 $new[$attr]= $info[$i];
563 } else {
564 $new[$attr][]= $info[$i];
565 }
566 }
567 }
568 }
569 }
570 }
572 /* close conncetion */
573 error_reporting (E_ALL);
574 ldap_unbind($ds);
576 /* Adapt naming attribute */
577 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
578 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
579 $new[$dst_name]= $dst_val;
581 /* Save copy */
582 $ldap->connect();
583 $ldap->cd($this->config->current['BASE']);
584 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
585 $ldap->cd($dst_dn);
586 $ldap->add($new);
588 if ($ldap->error != "Success"){
589 trigger_error("Trying to save $dst_dn failed.",
590 E_USER_WARNING);
591 return(FALSE);
592 }
594 return (TRUE);
595 }
598 function move($src_dn, $dst_dn)
599 {
600 /* Copy source to destination */
601 if (!$this->copy($src_dn, $dst_dn)){
602 return (FALSE);
603 }
605 /* Delete source */
606 $ldap= $this->config->get_ldap_link();
607 $ldap->rmdir($src_dn);
608 if ($ldap->error != "Success"){
609 trigger_error("Trying to delete $src_dn failed.",
610 E_USER_WARNING);
611 return (FALSE);
612 }
614 return (TRUE);
615 }
618 /* Move/Rename complete trees */
619 function recursive_move($src_dn, $dst_dn)
620 {
621 /* Check if the destination entry exists */
622 $ldap= $this->config->get_ldap_link();
624 /* Check if destination exists - abort */
625 $ldap->cat($dst_dn);
626 if ($ldap->fetch()){
627 trigger_error("recursive_move $dst_dn already exists.",
628 E_USER_WARNING);
629 return (FALSE);
630 }
632 /* Perform a search for all objects to be moved */
633 $objects= array();
634 $ldap->cd($src_dn);
635 $ldap->search("(objectClass=*)", array("dn"));
636 while($attrs= $ldap->fetch()){
637 $dn= $attrs['dn'];
638 $objects[$dn]= strlen($dn);
639 }
641 /* Sort objects by indent level */
642 asort($objects);
643 reset($objects);
645 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
646 foreach ($objects as $object => $len){
647 $src= $object;
648 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
649 if (!$this->copy($src, $dst)){
650 return (FALSE);
651 }
652 }
654 /* Remove src_dn */
655 $ldap->cd($src_dn);
656 $ldap->recursive_remove();
657 return (TRUE);
658 }
661 function handle_post_events($mode)
662 {
663 switch ($mode){
664 case "add":
665 $this->postcreate();
666 break;
668 case "modify":
669 $this->postmodify();
670 break;
672 case "remove":
673 $this->postremove();
674 break;
675 }
676 }
679 }
681 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
682 ?>