Code

Updated post handling
[gosa.git] / gosa-core / include / class_configRegistry.inc
1 <?php
3 class configRegistry{
5     public $config = NULL;
6     public $properties = array();
7     public $ldapStoredProperties = array(); 
8     public $fileStoredProperties = array(); 
9     public $classToName = array(); 
11     public $status = 'none';
13     // Excludes property values defined in ldap 
14     public $ignoreLdapProperties = FALSE;
16     // Contains all classes with plInfo
17     public $classesWithInfo = array();
18     public $pluginRequirements  = array();
19     public $categoryToClass  = array();
21     public $objectClasses = array();
23     public $detectedSchemaIssues = array();
24     public $schemaCheckFailed = FALSE;
25     public $schemaCheckFinished = FALSE;
26     public $pluginsDeactivated = array();
28     // Name of enabled plugins found in gosa.conf.
29     private $activePlugins = array();
32     /*! \brief      Constructs the config registry 
33      *  @param      config  The configuration object
34      *  @return     
35      */
36     function __construct($config)
37     {
38         $this->config = &$config;
40         // Detect classes that have a plInfo method 
41         global $class_mapping;
42         foreach ($class_mapping as $cname => $path){
43             $cmethods = get_class_methods($cname);
44             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
46                 // Get plugin definitions
47                 $def = call_user_func(array($cname, 'plInfo'));;
49                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
50                 if(count($def)){
51                     $this->classesWithInfo[$cname] = $def;
52                 }
53             }
54         }
56         // (Re)Load properties
57         $this->reload();
58     }
60     
61     /*! \brief      Returns a list of plugins used by GOsa.
62         @return     Array       An array containing all plugins with theis plInfo data.
63      */
64     function getListOfPlugins()
65     {
66         return($this->classesWithInfo);
67     }
70     /*! \brief      Checks whether the schema check was called in the current session or not.
71      *  @return     Boolean     True if check was already called
72      */
73     function schemaCheckFinished()
74     {
75         return($this->schemaCheckFinished);
76     }
79     /*! \brief      Starts the schema validation
80      *  @param      Boolean     'force' Force a re-check.
81      *  @param      Boolean     'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
82      *  @return     Boolean     True on success else FALSE
83      */
84     function validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE, $objectClassesToUse = array())
85     {
86         // Read objectClasses from ldap
87         if(count($objectClassesToUse)){
88             $this->setObjectClasses($objectClassesToUse);
89         }elseif(!count($this->objectClasses)){
90             $ldap = $this->config->get_ldap_link();
91             $ldap->cd($this->config->current['BASE']);
92             $this->setObjectClasses($ldap->get_objectclasses());
93         }
95         return($this->_validateSchemata($force, $disableIncompatiblePlugins));
96     }
99     /*! \brief      Sets the list object classes to use while validation the schema. (See 'validateSchemata')
100      *              This is called from the GOsa-Setup
101      *  @param      Array       The list of object classes (usually LDAP::get_objectlclasses()).
102      *  @return     void  
103      */
104     function setObjectClasses($ocs)
105     {
106         $this->objectClasses = $ocs;
107     }
110     /*! \brief      Returns an array which contains all unresolved schemata requirements.
111      *  @return     Array       An array containing all errors/issues  
112      */
113     function getSchemaResults()
114     {
115         return($this->detectedSchemaIssues);
116     }
119     /*! \brief      This method checks if the installed ldap-schemata matches the plugin requirements.
120      *  @param      Boolean     'force' Force a re-check.
121      *  @param      Boolean     'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
122      *  @return     String  
123      */
124     private function _validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE)
125     {
126         // We cannot check without readable schema info
127         if(!count($this->objectClasses)){
128             return(TRUE); 
129         }
131         // Don't do things twice unless forced
132         if($this->schemaCheckFinished && !$force) return($this->schemaCheckFailed); 
134         // Prepare result array
135         $this->detectedSchemaIssues = array();
136         $this->detectedSchemaIssues['missing'] = array();
137         $this->detectedSchemaIssues['versionMismatch'] = array();
139         // Clear last results 
140         $this->pluginsDeactivated = array();
142         // Collect required schema infos
143         $this->pluginRequirements = array('ldapSchema' => array());
144         $this->categoryToClass = array();
146         // Walk through plugins with requirements, but only check for active plugins.
147         foreach($this->classesWithInfo as $cname => $defs){
148             if(isset($defs['plRequirements'])){
150                 // Check only if required plugin is enabled in gosa.conf
151                 // Normally this is the class name itself, but may be overridden
152                 //  in plInfo using the plRequirements::activePlugin statement.
153                 $requiresActivePlugin = $cname;
154                 if(isset($defs['plRequirements']['activePlugin'])){
155                     $requiresActivePlugin = $defs['plRequirements']['activePlugin'];
156                 }
158                 // Only queue checks for active plugins. 
159                 if(isset($this->activePlugins[strtolower($requiresActivePlugin)])){
160                     $this->pluginRequirements[$cname] = $defs['plRequirements'];
161                 }else{
162                     if($cname == $requiresActivePlugin){
163                         new log("debug","","Skipped schema check for '{$cname}' plugin is inactive!",
164                                 array(),'');
165                     }else{
166                         new log("debug","","Skipped schema check for class '{$cname}' skipped,".
167                                     " required plugin '{$requiresActivePlugin}' is inactive!",
168                                 array(),'');
169                     }
170                 }
171             }
172         }
174         // Check schema requirements now        $missing = $invalid = array();
175         foreach($this->pluginRequirements as $cname => $requirements){
177             // Check LDAP schema requirements for this plugins
178             $failure = FALSE;
179             if(isset($requirements['ldapSchema'])){
180                 foreach($requirements['ldapSchema'] as $oc => $version){
181                     if(!$this->ocAvailable($oc)){
182                         $this->detectedSchemaIssues['missing'][$oc] = $oc;
183                     
184                         $this->schemaCheckFailed = TRUE;
185                         $failure = TRUE;
187                         new log("debug","","LDAP objectClass missing '{$oc}'!",
188                                 array(),'');
190                     }elseif(!empty($version)){
191                         $currentVersion = $this->getObjectClassVersion($oc);
192                         if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
193                             if($currentVersion == -1){
194                                 $currentVersion = _("unknown");
195                             }
196                             $this->detectedSchemaIssues['versionMismatch'][$oc] = 
197                                 sprintf(_("%s has version %s but %s is required!"), bold($oc),bold($currentVersion),bold($version));
198                             $this->schemaCheckFailed = TRUE;
199                             $failure = TRUE;
201                             new log("debug","","LDAP objectClass version mismatch '{$oc}' ".
202                                     "has '{$currentVersion}' but {$version} required!",
203                                     array(),'');
204                         }
205                     }
206                 }
207             }
209             // Display corresponding plugins now 
210             if($disableIncompatiblePlugins && $failure && isset($requirements['onFailureDisablePlugin'])){
211                 foreach($requirements['onFailureDisablePlugin'] as $name){
212                     $this->pluginsDeactivated[$name] = $name;
213                 } 
214             }
215         }
216         $this->schemaCheckFinished =TRUE;
217         session::un_set('plist');
218         return(!$this->schemaCheckFailed);
219     }
221     
222     /*! \brief      The function 'validateSchemata' may has disabled some GOsa-Plugins, 
223      *               the list of disabled plugins will be returned here.
224      *  @return     Array       The list of plugins disabled by 'validateSchemata'
225      */
226     function getDisabledPlugins()
227     {
228         return($this->pluginsDeactivated);
229     }
231         
232     /*! \brief      Displays an error message with all issues detect during the schema validation.
233      */
234     function displayRequirementErrors()
235     {
236         $message = "";
237         if(count($this->detectedSchemaIssues['missing'])){
238             $message.= "<br>".
239                 _("The following object classes are missing:").
240                 "<div class='scrollContainer' style='height:100px'>".
241                 msgPool::buildList(array_values($this->detectedSchemaIssues['missing'])).
242                 "</div>";
243         }    
244         if(count($this->detectedSchemaIssues['versionMismatch'])){
245             $message.= "<br>".
246                 _("The following object classes are outdated:").
247                 "<div class='scrollContainer' style='height:100px'>".
248                 msgPool::buildList(array_values($this->detectedSchemaIssues['versionMismatch'])).
249                 "</div>";
250         }    
251         if($message != ""){
252             $message.= "<br>"._("Plugins that require one or more of the object classes above will be disabled until the object classes get updated.");
253  
254             msg_dialog::display(_("Schema validation error"),$message, ERROR_DIALOG);
255         }
256     }
259     /*! \brief      Checks to version strings (e.g. '>=v2.8' and '2.9')
260      *  @param      String      The required version with operators (e.g. '>=2.8') 
261      *  @param      String      The version to match for withOUT operators (e.g. '2.9') 
262      *  @return     Boolean     True if version matches else false.  
263      */
264     private function ocVersionMatch($required, $installed)
265     {
266         $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
267         $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
268         return(version_compare($installed,$required, $operator)); 
269     }
271     
272     /*! \brief      Returns the currently installed version of a given object class.
273      *  @param      String      The name of the objectClass to check for. 
274      *  @return     String      The version string of the objectClass (e.g. v2.7) 
275      */
276     function getObjectClassVersion($oc)
277     {
278         if(!isset($this->objectClasses[$oc])){
279             return(NULL);
280         }else{
281             $version = -1; // unknown
282             if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
283                 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
284             }
285         }
286         return($version);
287     }
288     
290     /*! \brief      Check whether the given object class is available or not. 
291      *  @param      String      The name of the objectClass to check for (e.g. 'mailAccount') 
292      *  @return     Boolean     Returns TRUE if the class exists else FALSE.
293      */
294     function ocAvailable($name)
295     {
296         return(isset($this->objectClasses[$name]));
297     }
300     /*! \brief      Re-loads the list of installed GOsa-Properties. 
301      *  @param      Boolean     $force   If force is TRUE, the complete properties list is rebuild..
302      */
303     function reload($force = FALSE)
304     {
305         // Do not reload the properties everytime, once we have  
306         //  everything loaded and registrered skip the reload.
307         // Status is 'finished' once we had a ldap connection (logged in)
308         if(!$force && $this->status == 'finished') return;
310         // Reset everything
311         $this->ldapStoredProperties = array();
312         $this->fileStoredProperties = array();
313         $this->properties = array();
314         $this->mapByName = array();
315         $this->activePlugins = array('core'=>'core');
317         if(!$this->config) return;
319         // Search for config flags defined in the config file (TAB section)
320         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
321             foreach($tabdefs as $info){
323                 // Put plugin in list of active plugins
324                 if(isset($info['CLASS'])){
325                     $class = strtolower($info['CLASS']);
326                     $this->activePlugins[$class] = $class;
327                 }
329                 // Check if the info is valid
330                 if(isset($info['NAME']) && isset($info['CLASS'])){
331                     
333                     // Check if there is nore than just the plugin definition
334                     if(count($info) > 2){
335                         foreach($info as $name => $value){
336                             
337                             if(!in_array($name, array('CLASS','NAME'))){
338                                 $class= $info['CLASS'];    
339                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
340                             }
341                         }
342                     }
343                 }
344             }
345         }
347         // Search for config flags defined in the config file (MENU section)
348         foreach($this->config->data['MENU'] as $section => $entries){
349             foreach($entries as $entry){
351                 if(isset($entry['CLASS'])){
353                     // Put plugin to active plugins list.
354                     $class = strtolower($entry['CLASS']);
355                     $this->activePlugins[$class] = $class;
356                 
357                     if(count($entry) > 2 ){
358                         foreach($entry as $name => $value){
359                             if(!in_array($name, array('CLASS','ACL'))){
360                                 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
361                             }
362                         }
363                     }
364                 }
365             }
366         }
368         // Search for config flags defined in the config file (MAIN section)
369         foreach($this->config->data['MAIN'] as $name => $value){
370             $this->fileStoredProperties['core'][strtolower($name)] = $value;
371         }
373         // Search for config flags defined in the config file (Current LOCATION section)
374         if(isset($this->config->current)){
375             foreach($this->config->current as $name => $value){
376                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
377             }
378         }
380         // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
381         //  in the config. 
382         $this->ignoreLdapProperties = FALSE;
383         if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) && 
384             preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
385             $this->ignoreLdapProperties = TRUE;
386         }
388         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
389         if(!empty($this->config->current['CONFIG'])){
390             $ldap = $this->config->get_ldap_link();
391             $ldap->cd($this->config->current['CONFIG']);
392             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
393             while($attrs = $ldap->fetch()){
394                 $class = $attrs['cn'][0];
395                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
396                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
397                     $this->ldapStoredProperties[$class][$name] = $value;
398                 }
399             }
400         }
402         // Register plugin properties.
403         foreach ($this->classesWithInfo as $cname => $def){
405             // Detect class name
406             $name = $cname;
407             $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
408             $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
410             // Register post events
411             $this->classToName[$cname] = $name;
412             $data = array('name' => 'postcreate','type' => 'command');
413             $this->register($cname, $data);    
414             $data = array('name' => 'postremove','type' => 'command');
415             $this->register($cname, $data);    
416             $data = array('name' => 'postmodify','type' => 'command');
417             $this->register($cname, $data);    
418             $data = array('name' => 'check', 'type' => 'command');
419             $this->register($cname, $data);    
421             // Register properties 
422             if(isset($def['plProperties'])){
423                 foreach($def['plProperties'] as $property){
424                     $this->register($cname, $property);
425                 }
426             }
427         }
429         // We are only finsihed once we are logged in.
430         if(!empty($this->config->current['CONFIG'])){
431             $this->status = 'finished';
432         }
433     }
435    
436     /*! \brief      Returns TRUE if the property registration has finished without any error.
437      */ 
438     function propertyInitializationComplete()
439     {
440         return($this->status == 'finished');
441     }
444     /*! \brief      Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
445      *  @param      String      $class  The name of the class/plugin that wants to register this property.
446      *  @return     Array       $data   An array containing all data set in plInfo['plProperty]
447      */
448     function register($class,$data)
449     {
450         $id = count($this->properties);
451         $this->properties[$id] = new gosaProperty($this,$class,$data);
452         $p = strtolower("{$class}::{$data['name']}");
453         $this->mapByName[$p] = $id;
454     }
457     /*! \brief      Returns all registered properties.
458      *  @return     Array   A list of all properties.
459      */
460     public function getAllProperties()
461     {
462         return($this->properties);
463     }
466     /*! \brief      Checks whether a property exists or not.
467      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
468      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
469      *  @return     Boolean     TRUE if it exists else FALSE.
470      */
471     function propertyExists($class,$name)
472     {       
473         $p = strtolower("{$class}::{$name}");
474         return(isset($this->mapByName[$p]));
475     }
478     /*! \brief      Returns the id of a registered property.
479      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
480      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
481      *  @return     Integer     The id for the given property.  
482      */
483     private function getId($class,$name)
484     {
485         $p = strtolower("{$class}::{$name}");
486         if(!isset($this->mapByName[$p])){
487             return(-1);
488         }       
489         return($this->mapByName[$p]);    
490     }
493     /*! \brief      Returns a given property, if it exists.
494      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
495      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
496      *  @return     GOsaPropery     The property or 'NULL' if it doesn't exists.
497      */
498     function getProperty($class,$name)
499     {
500         if($this->propertyExists($class,$name)){
501             return($this->properties[$this->getId($class,$name)]);
502         }
503         return(NULL); 
504     }
507     /*! \brief      Returns the value for a given property, if it exists.
508      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
509      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
510      *  @return     GOsaPropery     The property value or an empty string if it doesn't exists.
511      */
512     function getPropertyValue($class,$name)
513     {   
514         if($this->propertyExists($class,$name)){
515             $tmp = $this->getProperty($class,$name);
516             return($tmp->getValue());
517         }
518         return("");
519     }
522     /*! \brief      Set a new value for a given property, if it exists.
523      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
524      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
525      *  @return     
526      */
527     function setPropertyValue($class,$name, $value)
528     {   
529         if($this->propertyExists($class,$name)){
530             $tmp = $this->getProperty($class,$name);
531             return($tmp->setValue($value));
532         }
533         return("");
534     }
537     /*! \brief      Save all temporary made property changes and thus make them useable/effective.
538      *  @return     Array       Returns a list of plugins that have to be migrated before they can be saved.
539      */
540     function saveChanges()
541     {
542         $migrate = array();
543         foreach($this->properties as $prop){
545             // Is this property modified
546             if(in_array($prop->getStatus(),array('modified','removed'))){
548                 // Check if we've to migrate something before we can make the changes effective. 
549                 if($prop->migrationRequired()){
550                     $migrate[] = $prop;
551                 }else{
552                     $prop->save();
553                 }
554             }
555         }
556         return($migrate);
557     }
561 class gosaProperty
563     protected $name = "";
564     protected $class = "";
565     protected $value = "";
566     protected $tmp_value = "";  // Used when modified but not saved 
567     protected $type = "string";
568     protected $default = "";
569     protected $defaults = "";
570     protected $description = "";
571     protected $check = "";
572     protected $migrate = "";
573     protected $mandatory = FALSE;
574     protected $group = "default";
575     protected $parent = NULL;
576     protected $data = array();
578     protected $migrationClass = NULL;
580     /*!  The current property status
581      *     'ldap'       Property is stored in ldap 
582      *     'file'       Property is stored in the config file
583      *     'undefined'  Property is currently not stored anywhere
584      *     'modified'   Property has been modified (should be saved)
585      */
586     protected $status = 'undefined';
588     protected $attributes = array('name','type','default','description','check',
589             'migrate','mandatory','group','defaults');
594     function __construct($parent,$classname,$data)
595     {
596         // Set some basic infos 
597         $this->parent = &$parent;
598         $this->class = $classname;
599         $this->data  = $data;
601         // Get all relevant information from the data array (comes from plInfo)    
602         foreach($this->attributes as $aName){
603             if(isset($data[$aName])){
604                 $this->$aName = $data[$aName];
605             }
606         }      
608         // Initialize with the current value
609         $this->_restoreCurrentValue(); 
611     }
613     function migrationRequired()
614     {
615         // Instantiate migration class 
616         if(!empty($this->migrate) && $this->migrationClass == NULL){
617             if(!class_available($this->migrate)){
618                 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
619             }else{
620                 $class = $this->migrate;
621                 $tmp = new $class($this->parent->config,$this);
622                 if(! $tmp instanceof propertyMigration){ 
623                     trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
624                 }else{
625                     $this->migrationClass = $tmp;
626                 }
627             }
628         }
629         if(empty($this->migrate) || $this->migrationClass == NULL){
630             return(FALSE);
631         }
632         return($this->migrationClass->checkForIssues());
633     }
635     function getMigrationClass()
636     {
637         return($this->migrationClass);
638     }
640     function check()
641     {
642         $val = $this->getValue(TRUE);
643         $return = TRUE;
644         if($this->mandatory && empty($val)){
645             $return = FALSE;
646         }
648         $check = $this->getCheck();
649         if(!empty($val) && !empty($check)){
650             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
651             if(!$res){
652                 $return = FALSE;
653             }
654         }
655         return($return);
656     }
658     static function isBool($message,$class,$name,$value, $type)
659     {
660         $match = in_array($value,array('true','false',''));
662         // Display the reason for failing this check.         
663         if($message && ! $match){
664             msg_dialog::display(_("Warning"), 
665                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"), 
666                         bold($value),bold($class),bold($name)), 
667                     WARNING_DIALOG);
668         }
669     
670         return($match);
671     }
673     static function isString($message,$class,$name,$value, $type)
674     {
675         $match = TRUE;
676     
677         // Display the reason for failing this check.         
678         if($message && ! $match){
679             msg_dialog::display(_("Warning"), 
680                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"), 
681                         bold($value),bold($class),bold($name)), 
682                     WARNING_DIALOG);
683         }
685         return($match);
686     }
688     static function isInteger($message,$class,$name,$value, $type)
689     {
690         $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
692         // Display the reason for failing this check.         
693         if($message && ! $match){
694             msg_dialog::display(_("Warning"), 
695                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"), 
696                         bold($value),bold($class),bold($name)), 
697                     WARNING_DIALOG);
698         }
700         return($match);
701     }
703     static function isPath($message,$class,$name,$value, $type)
704     {
705         $match = preg_match("#^(/[^/]*/){1}#", $value);
706     
707         // Display the reason for failing this check.         
708         if($message && ! $match){
709             msg_dialog::display(_("Warning"), 
710                     sprintf(_("The path '%s' specified for '%s:%s' is invalid!"), 
711                         bold($value),bold($class),bold($name)), 
712                     WARNING_DIALOG);
713         }
715         return($match);
716     }
718     static function isReadablePath($message,$class,$name,$value, $type)
719     {
720         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
721    
722         // Display the reason for failing this check.         
723         if($message && ! $match){
724             if(!is_dir($value)){
725                 msg_dialog::display(_("Warning"), 
726                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
727                             bold($value),bold($class),bold($name)), 
728                         WARNING_DIALOG);
729             }elseif(!is_readable($value)){
730                 msg_dialog::display(_("Warning"), 
731                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"), 
732                             bold($value),bold($class),bold($name)), 
733                         WARNING_DIALOG);
734             }
735         }
737         return($match);
738     }
740     static function isWriteablePath($message,$class,$name,$value, $type)
741     {
742         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
743    
744         // Display the reason for failing this check.         
745         if($message && ! $match){
746             if(!is_dir($value)){
747                 msg_dialog::display(_("Warning"), 
748                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
749                             bold($value),bold($class),bold($name)), 
750                         WARNING_DIALOG);
751             }elseif(!is_writeable($value)){
752                 msg_dialog::display(_("Warning"), 
753                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"), 
754                             bold($value),bold($class),bold($name)), 
755                         WARNING_DIALOG);
756             }
757         }
759         return($match);
760     }
762     static function isReadableFile($message,$class,$name,$value, $type)
763     {
764         $match = !empty($value) && is_readable($value) && is_file($value);
766         // Display the reason for failing this check.         
767         if($message && ! $match){
768                 
769             if(!is_file($value)){
770                 msg_dialog::display(_("Warning"), 
771                         sprintf(_("The file '%s' specified for '%s:%s' does not exists!"), 
772                             bold($value),bold($class),bold($name)), 
773                         WARNING_DIALOG);
774             }elseif(!is_readable($value)){
775                 msg_dialog::display(_("Warning"), 
776                         sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"), 
777                             bold($value),bold($class),bold($name)), 
778                         WARNING_DIALOG);
779             }
780         }
782         return($match);
783     }
785     static function isCommand($message,$class,$name,$value, $type)
786     {
787         $match = TRUE;
789         // Display the reason for failing this check.         
790         if($message && ! $match){
791             msg_dialog::display(_("Warning"), 
792                     sprintf(_("The command '%s' specified for '%s:%s' is invalid!"), 
793                         bold($value),bold($class),bold($name)), 
794                     WARNING_DIALOG);
795         }
796         
797         return($match);
798     }
800     static function isDn($message,$class,$name,$value, $type)
801     {
802         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
804         // Display the reason for failing this check.         
805         if($message && ! $match){
806             msg_dialog::display(_("Warning"), 
807                     sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"), 
808                         bold($value),bold($class),bold($name)), 
809                     WARNING_DIALOG);
810         }
811         
812         return($match);
813     }
815     static function isRdn($message,$class,$name,$value, $type)
816     {
817         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
819         // Display the reason for failing this check.         
820         if($message && ! $match){
821             msg_dialog::display(_("Warning"), 
822                     sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"), 
823                         bold($value),bold($class),bold($name)), 
824                     WARNING_DIALOG);
825         }
826         
827         return($match);
828     }
830     private function _restoreCurrentValue()
831     {
832         // First check for values in the LDAP Database.
833         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
834             $this->setStatus('ldap');
835             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
836             return;
837         }
839         // Second check for values in the config file.
840         if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
841             $this->setStatus('file');
842             $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
843             return;
844         }
846         // If there still wasn't found anything then fallback to the default.
847         if($this->getStatus() == 'undefined'){
848             $this->value = $this->getDefault();
849         }
850     }
852     function getMigrate() { return($this->migrate); }
853     function getCheck() { return($this->check); }
854     function getName() { return($this->name); }
855     function getClass() { return($this->class); }
856     function getGroup() { return($this->group); }
857     function getType() { return($this->type); }
858     function getDescription() { return($this->description); }
859     function getDefault() { return($this->default); }
860     function getDefaults() { return($this->defaults); }
861     function getStatus() { return($this->status); }
862     function isMandatory() { return($this->mandatory); }
864     function setValue($str) 
865     {
866         if(in_array($this->getStatus(), array('modified'))){
867             $this->tmp_value = $str; 
868         }elseif($this->value != $str){
869             $this->setStatus('modified'); 
870             $this->tmp_value = $str; 
871         }
872     }
874     function getValue($temporary = FALSE) 
875     { 
876         if($temporary){
877             if(in_array($this->getStatus(), array('modified','removed'))){
878                 return($this->tmp_value); 
879             }else{
880                 return($this->value); 
881             }
882         }else{ 
884             // Do not return ldap values if we've to ignore them.
885             if($this->parent->ignoreLdapProperties){
886                 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
887                     return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
888                 }else{
889                     return($this->getDefault());
890                 }
891             }else{
892                 return($this->value); 
893             }
894         }
895     }
897     function restoreDefault() 
898     {
899         if(in_array($this->getStatus(),array('ldap'))){
900             $this->setStatus('removed'); 
902             // Second check for values in the config file.
903             if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
904                 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
905             }else{
906                 $this->tmp_value = $this->getDefault();
907             }
908         }
909     }
911     function save()
912     {
913         if($this->getStatus() == 'modified'){
914             $ldap = $this->parent->config->get_ldap_link();
915             $ldap->cd($this->parent->config->current['BASE']);
916             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
917             $ldap->cat($dn);
918             if(!$ldap->count()){
919                 $ldap->cd($dn);
920                 $data = array(
921                         'cn' => $this->class, 
922                         'objectClass' => array('top','gosaConfig'),
923                         'gosaSetting' => $this->name.":".$this->tmp_value);
925                 $ldap->add($data);
926                 if(!$ldap->success()){
927                     echo $ldap->get_error();
928                 }
930             }else{
931                 $attrs = $ldap->fetch();
932                 $data = array();
933                 $found = false;
934                 if(isset($attrs['gosaSetting']['count'])){
935                     for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
936                         $set = $attrs['gosaSetting'][$i];
937                         if(preg_match("/^{$this->name}:/", $set)){
938                             $set = "{$this->name}:{$this->tmp_value}";
939                             $found = true;
940                         }
941                         $data['gosaSetting'][] = $set;
942                     }
943                 }
944                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
945                 $ldap->cd($dn);
946                 $ldap->modify($data);
947                 if(!$ldap->success()){
948                     echo $ldap->get_error();
949                 }
950             }
951             $this->value = $this->tmp_value;
952             $this->setStatus('ldap'); 
953         }elseif($this->getStatus() == 'removed'){
954             $ldap = $this->parent->config->get_ldap_link();
955             $ldap->cd($this->parent->config->current['BASE']);
956             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
957             $ldap->cat($dn);
958             $attrs = $ldap->fetch();
959             $data = array('gosaSetting' => array());
960             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
961                 $set = $attrs['gosaSetting'][$i];
962                 if(preg_match("/^{$this->name}:/", $set)){
963                     continue;
964                 }
965                 $data['gosaSetting'][] = $set;
966             }
967             $ldap->cd($dn);
968             $ldap->modify($data);
969             if(!$ldap->success()){
970                 echo $ldap->get_error();
971             }
972             $this->_restoreCurrentValue();
973         }
974     }
976     private function setStatus($state) 
977     {
978         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
979             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
980         }else{
981             $this->status = $state; 
982         }
983     }
985     function isValid() 
986     { 
987         return(TRUE);    
988     }
993 interface propertyMigration
995     function __construct($config,$property);
999 ?>