1e6b5ba215374a72be520cf78f83e245dbc945ff
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;
103 var $saved_attributes= array();
105 /*! \brief plugin constructor
107 If 'dn' is set, the node loads the given 'dn' from LDAP
109 \param dn Distinguished name to initialize plugin from
110 \sa plugin()
111 */
112 function plugin ($config, $dn= NULL)
113 {
114 /* Configuration is fine, allways */
115 $this->config= $config;
116 $this->dn= $dn;
118 /* Handle new accounts, don't read information from LDAP */
119 if ($dn == "new"){
120 return;
121 }
123 /* Get LDAP descriptor */
124 $ldap= $this->config->get_ldap_link();
125 if ($dn != NULL){
127 /* Load data to 'attrs' and save 'dn' */
128 $ldap->cat ($dn);
129 $this->attrs= $ldap->fetch();
131 /* Copy needed attributes */
132 foreach ($this->attributes as $val){
133 #if (isset($this->attrs["$val"][0])){
134 $found= array_key_ics($val, $this->attrs);
135 if ($found != ""){
136 $this->$val= $this->attrs["$found"][0];
137 }
138 }
140 /* Set the template flag according to the existence of objectClass
141 gosaUserTemplate */
142 if (isset($this->attrs['objectClass'])){
143 if (in_array ("gosaUserTemplate", $this->attrs['objectClass'])){
144 $this->is_template= TRUE;
145 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
146 "found", "Template check");
147 }
148 }
150 /* Is Account? */
151 error_reporting(0);
152 $found= TRUE;
153 foreach ($this->objectclasses as $obj){
154 if (preg_match('/top/i', $obj)){
155 continue;
156 }
157 if (!isset($this->attrs['objectClass']) || !in_array_ics ($obj, $this->attrs['objectClass'])){
158 $found= FALSE;
159 break;
160 }
161 }
162 error_reporting(E_ALL);
163 if ($found){
164 $this->is_account= TRUE;
165 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
166 "found", "Object check");
167 }
169 /* Prepare saved attributes */
170 $this->saved_attributes= $this->attrs;
171 foreach ($this->saved_attributes as $index => $value){
172 if (preg_match('/^[0-9]+$/', $index)){
173 unset($this->saved_attributes[$index]);
174 continue;
175 }
176 if (!in_array($index, $this->attributes) && $index != "objectClass"){
177 unset($this->saved_attributes[$index]);
178 continue;
179 }
180 if ($this->saved_attributes[$index]["count"] == 1){
181 $tmp= $this->saved_attributes[$index][0];
182 unset($this->saved_attributes[$index]);
183 $this->saved_attributes[$index]= $tmp;
184 continue;
185 }
187 unset($this->saved_attributes["$index"]["count"]);
188 }
189 }
191 /* Save initial account state */
192 $this->initially_was_account= $this->is_account;
193 }
195 /*! \brief execute plugin
197 Generates the html output for this node
198 */
199 function execute()
200 {
201 # This one is empty currently. Fabian - please fill in the docu code
202 $_SESSION['current_class_for_help'] = get_class($this);
203 /* Reset Lock message POST/GET check array, to prevent perg_match errors*/
204 $_SESSION['LOCK_VARS_TO_USE'] =array();
205 }
207 /* remove object from parent */
208 function remove_from_parent()
209 {
210 /* include global link_info */
211 $ldap= $this->config->get_ldap_link();
213 /* Get current objectClasses in order to add the required ones */
214 $ldap->cat($this->dn);
215 $tmp= $ldap->fetch ();
216 if (isset($tmp['objectClass'])){
217 $oc= $tmp['objectClass'];
218 } else {
219 $oc= array("count" => 0);
220 }
222 /* Remove objectClasses from entry */
223 $ldap->cd($this->dn);
224 $this->attrs= array();
225 $this->attrs['objectClass']= array();
226 for ($i= 0; $i<$oc["count"]; $i++){
227 if (!in_array_ics($oc[$i], $this->objectclasses)){
228 $this->attrs['objectClass'][]= $oc[$i];
229 }
230 }
232 /* Unset attributes from entry */
233 foreach ($this->attributes as $val){
234 $this->attrs["$val"]= array();
235 }
237 /* Unset account info */
238 $this->is_account= FALSE;
240 /* Do not write in plugin base class, this must be done by
241 children, since there are normally additional attribs,
242 lists, etc. */
243 /*
244 $ldap->modify($this->attrs);
245 */
246 }
249 /* Save data to object */
250 function save_object()
251 {
252 /* Save values to object */
253 foreach ($this->attributes as $val){
254 if (chkacl ($this->acl, "$val") == "" && isset ($_POST["$val"])){
255 /* Check for modifications */
256 if (get_magic_quotes_gpc()) {
257 $data= stripcslashes($_POST["$val"]);
258 } else {
259 $data= $this->$val = $_POST["$val"];
260 }
261 if ($this->$val != $data){
262 $this->is_modified= TRUE;
263 }
265 /* Okay, how can I explain this fix ...
266 * In firefox, disabled option fields aren't selectable ... but in IE you can select these fileds.
267 * So IE posts these 'unselectable' option, with value = chr(194)
268 * chr(194) seems to be the in between the ...option> </option.. because there is no value=".." specified in these option fields
269 * This was added for W3c compliance, but now causes these ... ldap errors ...
270 * So we set these Fields to ""; a normal empty string, and we can check these values in plugin::check() again ...
271 */
272 if(isset($data[0]) && $data[0] == chr(194)) {
273 $data = "";
274 }
275 $this->$val= $data;
276 }
277 }
278 }
281 /* Save data to LDAP, depending on is_account we save or delete */
282 function save()
283 {
284 /* include global link_info */
285 $ldap= $this->config->get_ldap_link();
287 /* Start with empty array */
288 $this->attrs= array();
290 /* Get current objectClasses in order to add the required ones */
291 $ldap->cat($this->dn);
293 $tmp= $ldap->fetch ();
295 if (isset($tmp['objectClass'])){
296 $oc= $tmp["objectClass"];
297 $this->new= FALSE;
298 } else {
299 $oc= array("count" => 0);
300 $this->new= TRUE;
301 }
303 /* Load (minimum) attributes, add missing ones */
304 $this->attrs['objectClass']= $this->objectclasses;
305 for ($i= 0; $i<$oc["count"]; $i++){
306 if (!in_array_ics($oc[$i], $this->objectclasses)){
307 $this->attrs['objectClass'][]= $oc[$i];
308 }
309 }
311 /* Copy standard attributes */
312 foreach ($this->attributes as $val){
313 if ($this->$val != ""){
314 $this->attrs["$val"]= $this->$val;
315 } elseif (!$this->new) {
316 $this->attrs["$val"]= array();
317 }
318 }
320 }
323 function cleanup()
324 {
325 foreach ($this->attrs as $index => $value){
327 /* Convert arrays with one element to non arrays, if the saved
328 attributes are no array, too */
329 if (is_array($this->attrs[$index]) &&
330 count ($this->attrs[$index]) == 1 &&
331 isset($this->saved_attributes[$index]) &&
332 !is_array($this->saved_attributes[$index])){
334 $tmp= $this->attrs[$index][0];
335 $this->attrs[$index]= $tmp;
336 }
338 /* Remove emtpy arrays if they do not differ */
339 if (is_array($this->attrs[$index]) &&
340 count($this->attrs[$index]) == 0 &&
341 !isset($this->saved_attributes[$index])){
343 unset ($this->attrs[$index]);
344 continue;
345 }
347 /* Remove single attributes that do not differ */
348 if (!is_array($this->attrs[$index]) &&
349 isset($this->saved_attributes[$index]) &&
350 !is_array($this->saved_attributes[$index]) &&
351 $this->attrs[$index] == $this->saved_attributes[$index]){
353 unset ($this->attrs[$index]);
354 continue;
355 }
357 /* Remove arrays that do not differ */
358 if (is_array($this->attrs[$index]) &&
359 isset($this->saved_attributes[$index]) &&
360 is_array($this->saved_attributes[$index])){
362 if (!array_differs($this->attrs[$index],$this->saved_attributes[$index])){
363 unset ($this->attrs[$index]);
364 continue;
365 }
366 }
367 }
368 }
370 /* Check formular input */
371 function check()
372 {
373 $message= array();
374 return ($message);
375 }
377 /* Adapt from template, using 'dn' */
378 function adapt_from_template($dn)
379 {
380 /* Include global link_info */
381 $ldap= $this->config->get_ldap_link();
383 /* Load requested 'dn' to 'attrs' */
384 $ldap->cat ($dn);
385 $this->attrs= $ldap->fetch();
387 /* Walk through attributes */
388 foreach ($this->attributes as $val){
390 if (isset($this->attrs["$val"][0])){
392 /* If attribute is set, replace dynamic parts:
393 %sn, %givenName and %uid. Fill these in our local variables. */
394 $value= $this->attrs["$val"][0];
396 foreach (array("sn", "givenName", "uid") as $repl){
397 if (preg_match("/%$repl/i", $value)){
398 $value= preg_replace ("/%$repl/i", $this->parent->$repl, $value);
399 }
400 }
401 $this->$val= $value;
402 }
403 }
405 /* Is Account? */
406 $found= TRUE;
407 foreach ($this->objectclasses as $obj){
408 if (preg_match('/top/i', $obj)){
409 continue;
410 }
411 if (!in_array_ics ($obj, $this->attrs['objectClass'])){
412 $found= FALSE;
413 break;
414 }
415 }
416 if ($found){
417 $this->is_account= TRUE;
418 }
419 }
421 /* Indicate whether a password change is needed or not */
422 function password_change_needed()
423 {
424 return FALSE;
425 }
427 /* Show header message for tab dialogs */
428 function show_header($button_text, $text, $disabled= FALSE)
429 {
430 if ($disabled == TRUE){
431 $state= "disabled";
432 } else {
433 $state= "";
434 }
435 $display= "<table summary=\"\" width=\"100%\"><tr>\n<td colspan=2><p><b>$text</b></p>\n";
436 $display.= "<input type=submit value=\"$button_text\" name=\"modify_state\" ".
437 chkacl($this->acl, "all")." ".$state.
438 "><p class=\"seperator\"> </p></td></tr></table>";
440 return($display);
441 }
443 function postcreate()
444 {
445 /* Find postcreate entries for this class */
446 $command= search_config($this->config->data['MENU'], get_class($this), "POSTCREATE");
447 if ($command == "" && isset($this->config->data['TABS'])){
448 $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
449 }
451 if ($command != ""){
452 /* Walk through attribute list */
453 foreach ($this->attributes as $attr){
454 if (!is_array($this->$attr)){
455 $command= preg_replace("/%$attr/", $this->$attr, $command);
456 }
457 }
458 $command= preg_replace("/%dn/", $this->dn, $command);
459 if (check_command($command)){
460 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
461 $command, "Execute");
463 exec($command);
464 } else {
465 $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
466 print_red ($message);
467 }
468 }
469 }
471 function postmodify()
472 {
473 /* Find postcreate entries for this class */
474 $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
475 if ($command == "" && isset($this->config->data['TABS'])){
476 $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
477 }
479 if ($command != ""){
480 /* Walk through attribute list */
481 foreach ($this->attributes as $attr){
482 if (!is_array($this->$attr)){
483 $command= preg_replace("/%$attr/", $this->$attr, $command);
484 }
485 }
486 $command= preg_replace("/%dn/", $this->dn, $command);
487 if (check_command($command)){
488 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
489 $command, "Execute");
491 exec($command);
492 } else {
493 $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
494 print_red ($message);
495 }
496 }
497 }
499 function postremove()
500 {
501 /* Find postremove entries for this class */
502 $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
503 if ($command == "" && isset($this->config->data['TABS'])){
504 $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
505 }
507 if ($command != ""){
508 /* Walk through attribute list */
509 foreach ($this->attributes as $attr){
510 if (!is_array($this->$attr)){
511 $command= preg_replace("/%$attr/", $this->$attr, $command);
512 }
513 }
514 $command= preg_replace("/%dn/", $this->dn, $command);
515 if (check_command($command)){
516 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
517 $command, "Execute");
519 exec($command);
520 } else {
521 $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
522 print_red ($message);
523 }
524 }
525 }
527 /* Create unique DN */
528 function create_unique_dn($attribute, $base)
529 {
530 $ldap= $this->config->get_ldap_link();
531 $base= preg_replace("/^,*/", "", $base);
533 /* Try to use plain entry first */
534 $dn= "$attribute=".$this->$attribute.",$base";
535 $ldap->cat ($dn);
536 if (!$ldap->fetch()){
537 return ($dn);
538 }
540 /* Look for additional attributes */
541 foreach ($this->attributes as $attr){
542 if ($attr == $attribute || $this->$attr == ""){
543 continue;
544 }
546 $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
547 $ldap->cat ($dn);
548 if (!$ldap->fetch()){
549 return ($dn);
550 }
551 }
553 /* None found */
554 return ("none");
555 }
557 function rebind($ldap, $referral)
558 {
559 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
560 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
561 $this->error = "Success";
562 $this->hascon=true;
563 $this->reconnect= true;
564 return (0);
565 } else {
566 $this->error = "Could not bind to " . $credentials['ADMIN'];
567 return NULL;
568 }
569 }
571 /* This is a workaround function. */
572 function copy($src_dn, $dst_dn)
573 {
574 /* Rename dn in possible object groups */
575 $ldap= $this->config->get_ldap_link();
576 $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
577 array('cn'));
578 while ($attrs= $ldap->fetch()){
579 $og= new ogroup($this->config, $ldap->getDN());
580 unset($og->member[$src_dn]);
581 $og->member[$dst_dn]= $dst_dn;
582 $og->save ();
583 }
585 $ldap->cat($dst_dn);
586 $attrs= $ldap->fetch();
587 if (count($attrs)){
588 trigger_error("Trying to overwrite $dst_dn, which already exists.",
589 E_USER_WARNING);
590 return (FALSE);
591 }
593 $ldap->cat($src_dn);
594 $attrs= array();
595 $attrs= $ldap->fetch();
596 if (!count($attrs)){
597 trigger_error("Trying to move $src_dn, which does not seem to exist.",
598 E_USER_WARNING);
599 return (FALSE);
600 }
602 /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
603 $ds= ldap_connect($this->config->current['SERVER']);
604 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
605 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
606 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
607 }
609 $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
610 error_reporting (0);
611 $sr=ldap_read($ds, $ldap->fix($src_dn), "objectClass=*");
613 /* Fill data from LDAP */
614 $new= array();
615 if ($sr) {
616 $ei=ldap_first_entry($ds, $sr);
617 if ($ei) {
618 foreach($attrs as $attr => $val){
619 if ($info = ldap_get_values_len($ds, $ei, $attr)){
620 for ($i= 0; $i<$info['count']; $i++){
621 if ($info['count'] == 1){
622 $new[$attr]= $info[$i];
623 } else {
624 $new[$attr][]= $info[$i];
625 }
626 }
627 }
628 }
629 }
630 }
632 /* close conncetion */
633 error_reporting (E_ALL);
634 ldap_unbind($ds);
636 /* Adapt naming attribute */
637 $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
638 $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
639 $new[$dst_name]= $dst_val;
641 /* Check if this is a department.
642 * If it is a dep. && there is a , override in his ou
643 * change \2C to , again, else this entry can't be saved ...
644 */
645 if((isset($new['ou'])) &&( preg_match("/\\\\2C/",$new['ou']))){
646 $new['ou'] = preg_replace("/\\\\2C/",",",$new['ou']);
647 }
649 /* Save copy */
650 $ldap->connect();
651 $ldap->cd($this->config->current['BASE']);
652 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
653 $ldap->cd($dst_dn);
654 $ldap->add($new);
656 if ($ldap->error != "Success"){
657 trigger_error("Trying to save $dst_dn failed.",
658 E_USER_WARNING);
659 return(FALSE);
660 }
662 return (TRUE);
663 }
666 function move($src_dn, $dst_dn)
667 {
668 /* Copy source to destination */
669 if (!$this->copy($src_dn, $dst_dn)){
670 return (FALSE);
671 }
673 /* Delete source */
674 $ldap= $this->config->get_ldap_link();
675 $ldap->rmdir($src_dn);
676 if ($ldap->error != "Success"){
677 trigger_error("Trying to delete $src_dn failed.",
678 E_USER_WARNING);
679 return (FALSE);
680 }
682 return (TRUE);
683 }
686 /* Move/Rename complete trees */
687 function recursive_move($src_dn, $dst_dn)
688 {
689 /* Check if the destination entry exists */
690 $ldap= $this->config->get_ldap_link();
692 /* Check if destination exists - abort */
693 $ldap->cat($dst_dn);
694 if ($ldap->fetch()){
695 trigger_error("recursive_move $dst_dn already exists.",
696 E_USER_WARNING);
697 return (FALSE);
698 }
700 /* Perform a search for all objects to be moved */
701 $objects= array();
702 $ldap->cd($src_dn);
703 $ldap->search("(objectClass=*)", array("dn"));
704 while($attrs= $ldap->fetch()){
705 $dn= $attrs['dn'];
706 $objects[$dn]= strlen($dn);
707 }
709 /* Sort objects by indent level */
710 asort($objects);
711 reset($objects);
713 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
714 foreach ($objects as $object => $len){
715 $src= $object;
716 $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
717 if (!$this->copy($src, $dst)){
718 return (FALSE);
719 }
720 }
722 /* Remove src_dn */
723 $ldap->cd($src_dn);
724 $ldap->recursive_remove();
725 return (TRUE);
726 }
729 function handle_post_events($mode)
730 {
731 switch ($mode){
732 case "add":
733 $this->postcreate();
734 break;
736 case "modify":
737 $this->postmodify();
738 break;
740 case "remove":
741 $this->postremove();
742 break;
743 }
744 }
746 function saveCopyDialog(){
747 }
749 function getCopyDialog(){
750 return(array("string"=>"","status"=>""));
751 }
753 }
754 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
755 ?>