Code

Fixed Problem with TABS post creation
[gosa.git] / include / class_plugin.inc
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>&nbsp;<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         }
245     
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 &nbsp; in between the ...option>&nbsp;</option.. because there is no value=".." specified in these option fields  
250          * This &nbsp; 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);
273     
274     $tmp= $ldap->fetch ();
275     
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\">&nbsp;</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['TABS'])){
381       $command= search_config($this->config->data['TABS'], get_class($this), "POSTCREATE");
382     }
384     if ($command != ""){
385       /* Walk through attribute list */
386       foreach ($this->attributes as $attr){
387         if (!is_array($this->$attr)){
388           $command= preg_replace("/%$attr/", $this->$attr, $command);
389         }
390       }
391       $command= preg_replace("/%dn/", $this->dn, $command);
392       if (check_command($command)){
393         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
394             $command, "Execute");
396         exec($command);
397       } else {
398         $message= sprintf(_("Command '%s', specified as POSTCREATE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
399         print_red ($message);
400       }
401     }
402   }
404   function postmodify()
405   {
406     /* Find postcreate entries for this class */
407     $command= search_config($this->config->data['MENU'], get_class($this), "POSTMODIFY");
408     if ($command == "" && isset($this->config->data['TABS'])){
409       $command= search_config($this->config->data['TABS'], get_class($this), "POSTMODIFY");
410     }
412     if ($command != ""){
413       /* Walk through attribute list */
414       foreach ($this->attributes as $attr){
415         if (!is_array($this->$attr)){
416           $command= preg_replace("/%$attr/", $this->$attr, $command);
417         }
418       }
419       $command= preg_replace("/%dn/", $this->dn, $command);
420       if (check_command($command)){
421         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
422             $command, "Execute");
424         exec($command);
425       } else {
426         $message= sprintf(_("Command '%s', specified as POSTMODIFY for plugin '%s' doesn't seem to exist."), $command, get_class($this));
427         print_red ($message);
428       }
429     }
430   }
432   function postremove()
433   {
434     /* Find postremove entries for this class */
435     $command= search_config($this->config->data['MENU'], get_class($this), "POSTREMOVE");
436     if ($command == "" && isset($this->config->data['TABS'])){
437       $command= search_config($this->config->data['TABS'], get_class($this), "POSTREMOVE");
438     }
440     if ($command != ""){
441       /* Walk through attribute list */
442       foreach ($this->attributes as $attr){
443         if (!is_array($this->$attr)){
444           $command= preg_replace("/%$attr/", $this->$attr, $command);
445         }
446       }
447       $command= preg_replace("/%dn/", $this->dn, $command);
448       if (check_command($command)){
449         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
450             $command, "Execute");
452         exec($command);
453       } else {
454         $message= sprintf(_("Command '%s', specified as POSTREMOVE for plugin '%s' doesn't seem to exist."), $command, get_class($this));
455         print_red ($message);
456       }
457     }
458   }
460   /* Create unique DN */
461   function create_unique_dn($attribute, $base)
462   {
463     $ldap= $this->config->get_ldap_link();
464     $base= preg_replace("/^,*/", "", $base);
466     /* Try to use plain entry first */
467     $dn= "$attribute=".$this->$attribute.",$base";
468     $ldap->cat ($dn);
469     if (!$ldap->fetch()){
470       return ($dn);
471     }
473     /* Look for additional attributes */
474     foreach ($this->attributes as $attr){
475       if ($attr == $attribute || $this->$attr == ""){
476         continue;
477       }
479       $dn= "$attribute=".$this->$attribute."+$attr=".$this->$attr.",$base";
480       $ldap->cat ($dn);
481       if (!$ldap->fetch()){
482         return ($dn);
483       }
484     }
486     /* None found */
487     return ("none");
488   }
490   function rebind($ldap, $referral)
491   {
492     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
493     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
494       $this->error = "Success";
495       $this->hascon=true;
496       $this->reconnect= true;
497       return (0);
498     } else {
499       $this->error = "Could not bind to " . $credentials['ADMIN'];
500       return NULL;
501     }
502   }
504   /* This is a workaround function. */
505   function copy($src_dn, $dst_dn)
506   {
507     /* Rename dn in possible object groups */
508     $ldap= $this->config->get_ldap_link();
509     $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.$src_dn.'))',
510         array('cn'));
511     while ($attrs= $ldap->fetch()){
512       $og= new ogroup($this->config, $ldap->getDN());
513       unset($og->member[$src_dn]);
514       $og->member[$dst_dn]= $dst_dn;
515       $og->save ();
516     }
518     $ldap->cat($dst_dn);
519     $attrs= $ldap->fetch();
520     if (count($attrs)){
521       trigger_error("Trying to overwrite $dst_dn, which already exists.",
522           E_USER_WARNING);
523       return (FALSE);
524     }
526     $ldap->cat($src_dn);
527     $attrs= array();
528     $attrs= $ldap->fetch();
529     if (!count($attrs)){
530       trigger_error("Trying to move $src_dn, which does not seem to exist.",
531           E_USER_WARNING);
532       return (FALSE);
533     }
535     /* Grummble. This really sucks. PHP ldap doesn't support rdn stuff. */
536     $ds= ldap_connect($this->config->current['SERVER']);
537     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
538     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['REFERRAL'])) {
539       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
540     }
542     $r=ldap_bind($ds,$this->config->current['ADMIN'], $this->config->current['PASSWORD']);
543     error_reporting (0);
544     $sr=ldap_read($ds, $src_dn, "objectClass=*");
546     /* Fill data from LDAP */
547     $new= array();
548     if ($sr) {
549       $ei=ldap_first_entry($ds, $sr);
550       if ($ei) {
551         foreach($attrs as $attr => $val){
552           if ($info = ldap_get_values_len($ds, $ei, $attr)){
553             for ($i= 0; $i<$info['count']; $i++){
554               if ($info['count'] == 1){
555                 $new[$attr]= $info[$i];
556               } else {
557                 $new[$attr][]= $info[$i];
558               }
559             }
560           }
561         }
562       }
563     }
565     /* close conncetion */
566     error_reporting (E_ALL);
567     ldap_unbind($ds);
569     /* Adapt naming attribute */
570     $dst_name= preg_replace("/^([^=]+)=.*$/", "\\1", $dst_dn);
571     $dst_val = preg_replace("/^[^=]+=([^,+]+).*,.*$/", "\\1", $dst_dn);
572     $new[$dst_name]= $dst_val;
574     /* Save copy */
575     $ldap->connect();
576     $ldap->cd($this->config->current['BASE']);
577     $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dst_dn));
578     $ldap->cd($dst_dn);
579     $ldap->add($new);
581     if ($ldap->error != "Success"){
582       trigger_error("Trying to save $dst_dn failed.",
583           E_USER_WARNING);
584       return(FALSE);
585     }
587     return (TRUE);
588   }
591   function move($src_dn, $dst_dn)
592   {
593     /* Copy source to destination */
594     if (!$this->copy($src_dn, $dst_dn)){
595       return (FALSE);
596     }
598     /* Delete source */
599     $ldap= $this->config->get_ldap_link();
600     $ldap->rmdir($src_dn);
601     if ($ldap->error != "Success"){
602       trigger_error("Trying to delete $src_dn failed.",
603           E_USER_WARNING);
604       return (FALSE);
605     }
607     return (TRUE);
608   }
611   /* Move/Rename complete trees */
612   function recursive_move($src_dn, $dst_dn)
613   {
614     /* Check if the destination entry exists */
615     $ldap= $this->config->get_ldap_link();
617     /* Check if destination exists - abort */
618     $ldap->cat($dst_dn);
619     if ($ldap->fetch()){
620       trigger_error("recursive_move $dst_dn already exists.",
621           E_USER_WARNING);
622       return (FALSE);
623     }
625     /* Perform a search for all objects to be moved */
626     $objects= array();
627     $ldap->cd($src_dn);
628     $ldap->search("(objectClass=*)", array("dn"));
629     while($attrs= $ldap->fetch()){
630       $dn= $attrs['dn'];
631       $objects[$dn]= strlen($dn);
632     }
634     /* Sort objects by indent level */
635     asort($objects);
636     reset($objects);
638     /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
639     foreach ($objects as $object => $len){
640       $src= $object;
641       $dst= preg_replace("/$src_dn$/", "$dst_dn", $object);
642       if (!$this->copy($src, $dst)){
643         return (FALSE);
644       }
645     }
647     /* Remove src_dn */
648     $ldap->cd($src_dn);
649     $ldap->recursive_remove();
650     return (TRUE);
651   }
654   function handle_post_events($mode)
655   {
656     switch ($mode){
657       case "add":
658         $this->postcreate();
659       break;
661       case "modify":
662         $this->postmodify();
663       break;
665       case "remove":
666         $this->postremove();
667       break;
668     }
669   }
674 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
675 ?>