Code

Prepared statistics to print password changes over time
[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' => 'precreate','type' => 'command');
419             $this->register($cname, $data);    
420             $data = array('name' => 'preremove','type' => 'command');
421             $this->register($cname, $data);    
422             $data = array('name' => 'premodify','type' => 'command');
423             $this->register($cname, $data);    
424             $data = array('name' => 'check', 'type' => 'command');
425             $this->register($cname, $data);    
427             // Register properties 
428             if(isset($def['plProperties'])){
429                 foreach($def['plProperties'] as $property){
430                     $this->register($cname, $property);
431                 }
432             }
433         }
435         // We are only finsihed once we are logged in.
436         if(!empty($this->config->current['CONFIG'])){
437             $this->status = 'finished';
438         }
439     }
441    
442     /*! \brief      Returns TRUE if the property registration has finished without any error.
443      */ 
444     function propertyInitializationComplete()
445     {
446         return($this->status == 'finished');
447     }
450     /*! \brief      Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
451      *  @param      String      $class  The name of the class/plugin that wants to register this property.
452      *  @return     Array       $data   An array containing all data set in plInfo['plProperty]
453      */
454     function register($class,$data)
455     {
456         $id = count($this->properties);
457         $this->properties[$id] = new gosaProperty($this,$class,$data);
458         $p = strtolower("{$class}::{$data['name']}");
459         $this->mapByName[$p] = $id;
460     }
463     /*! \brief      Returns all registered properties.
464      *  @return     Array   A list of all properties.
465      */
466     public function getAllProperties()
467     {
468         return($this->properties);
469     }
472     /*! \brief      Checks whether a property exists or not.
473      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
474      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
475      *  @return     Boolean     TRUE if it exists else FALSE.
476      */
477     function propertyExists($class,$name)
478     {       
479         $p = strtolower("{$class}::{$name}");
480         return(isset($this->mapByName[$p]));
481     }
484     /*! \brief      Returns the id of a registered property.
485      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
486      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
487      *  @return     Integer     The id for the given property.  
488      */
489     private function getId($class,$name)
490     {
491         $p = strtolower("{$class}::{$name}");
492         if(!isset($this->mapByName[$p])){
493             return(-1);
494         }       
495         return($this->mapByName[$p]);    
496     }
499     /*! \brief      Returns a given property, if it exists.
500      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
501      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
502      *  @return     GOsaPropery     The property or 'NULL' if it doesn't exists.
503      */
504     function getProperty($class,$name)
505     {
506         if($this->propertyExists($class,$name)){
507             return($this->properties[$this->getId($class,$name)]);
508         }
509         return(NULL); 
510     }
513     /*! \brief      Returns the value for a given property, if it exists.
514      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
515      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
516      *  @return     GOsaPropery     The property value or an empty string if it doesn't exists.
517      */
518     function getPropertyValue($class,$name)
519     {   
520         if($this->propertyExists($class,$name)){
521             $tmp = $this->getProperty($class,$name);
522             return($tmp->getValue());
523         }
524         return("");
525     }
528     /*! \brief      Set a new value for a given property, if it exists.
529      *  @param      String      $class  The class name (e.g. 'core' or 'mailAccount') 
530      *  @param      String      $name   The property name (e.g. 'sessionTimeout' or 'mailMethod')
531      *  @return     
532      */
533     function setPropertyValue($class,$name, $value)
534     {   
535         if($this->propertyExists($class,$name)){
536             $tmp = $this->getProperty($class,$name);
537             return($tmp->setValue($value));
538         }
539         return("");
540     }
543     /*! \brief      Save all temporary made property changes and thus make them useable/effective.
544      *  @return     Array       Returns a list of plugins that have to be migrated before they can be saved.
545      */
546     function saveChanges()
547     {
548         $migrate = array();
549         foreach($this->properties as $prop){
551             // Is this property modified
552             if(in_array($prop->getStatus(),array('modified','removed'))){
554                 // Check if we've to migrate something before we can make the changes effective. 
555                 if($prop->migrationRequired()){
556                     $migrate[] = $prop;
557                 }else{
558                     $prop->save();
559                 }
560             }
561         }
562         return($migrate);
563     }
567 class gosaProperty
569     protected $name = "";
570     protected $class = "";
571     protected $value = "";
572     protected $tmp_value = "";  // Used when modified but not saved 
573     protected $type = "string";
574     protected $default = "";
575     protected $defaults = "";
576     protected $description = "";
577     protected $check = "";
578     protected $migrate = "";
579     protected $mandatory = FALSE;
580     protected $group = "default";
581     protected $parent = NULL;
582     protected $data = array();
584     protected $migrationClass = NULL;
586     /*!  The current property status
587      *     'ldap'       Property is stored in ldap 
588      *     'file'       Property is stored in the config file
589      *     'undefined'  Property is currently not stored anywhere
590      *     'modified'   Property has been modified (should be saved)
591      */
592     protected $status = 'undefined';
594     protected $attributes = array('name','type','default','description','check',
595             'migrate','mandatory','group','defaults');
600     function __construct($parent,$classname,$data)
601     {
602         // Set some basic infos 
603         $this->parent = &$parent;
604         $this->class = $classname;
605         $this->data  = $data;
607         // Get all relevant information from the data array (comes from plInfo)    
608         foreach($this->attributes as $aName){
609             if(isset($data[$aName])){
610                 $this->$aName = $data[$aName];
611             }
612         }      
614         // Initialize with the current value
615         $this->_restoreCurrentValue(); 
617     }
619     function migrationRequired()
620     {
621         // Instantiate migration class 
622         if(!empty($this->migrate) && $this->migrationClass == NULL){
623             if(!class_available($this->migrate)){
624                 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
625             }else{
626                 $class = $this->migrate;
627                 $tmp = new $class($this->parent->config,$this);
628                 if(! $tmp instanceof propertyMigration){ 
629                     trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
630                 }else{
631                     $this->migrationClass = $tmp;
632                 }
633             }
634         }
635         if(empty($this->migrate) || $this->migrationClass == NULL){
636             return(FALSE);
637         }
638         return($this->migrationClass->checkForIssues());
639     }
641     function getMigrationClass()
642     {
643         return($this->migrationClass);
644     }
646     function check()
647     {
648         $val = $this->getValue(TRUE);
649         $return = TRUE;
650         if($this->mandatory && empty($val)){
651             $return = FALSE;
652         }
654         $check = $this->getCheck();
655         if(!empty($val) && !empty($check)){
656             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
657             if(!$res){
658                 $return = FALSE;
659             }
660         }
661         return($return);
662     }
664     static function isBool($message,$class,$name,$value, $type)
665     {
666         $match = in_array($value,array('true','false',''));
668         // Display the reason for failing this check.         
669         if($message && ! $match){
670             msg_dialog::display(_("Warning"), 
671                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"), 
672                         bold($value),bold($class),bold($name)), 
673                     WARNING_DIALOG);
674         }
675     
676         return($match);
677     }
679     static function isString($message,$class,$name,$value, $type)
680     {
681         $match = TRUE;
682     
683         // Display the reason for failing this check.         
684         if($message && ! $match){
685             msg_dialog::display(_("Warning"), 
686                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"), 
687                         bold($value),bold($class),bold($name)), 
688                     WARNING_DIALOG);
689         }
691         return($match);
692     }
694     static function isInteger($message,$class,$name,$value, $type)
695     {
696         $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
698         // Display the reason for failing this check.         
699         if($message && ! $match){
700             msg_dialog::display(_("Warning"), 
701                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"), 
702                         bold($value),bold($class),bold($name)), 
703                     WARNING_DIALOG);
704         }
706         return($match);
707     }
709     static function isPath($message,$class,$name,$value, $type)
710     {
711         $match = preg_match("#^(/[^/]*/){1}#", $value);
712     
713         // Display the reason for failing this check.         
714         if($message && ! $match){
715             msg_dialog::display(_("Warning"), 
716                     sprintf(_("The path '%s' specified for '%s:%s' is invalid!"), 
717                         bold($value),bold($class),bold($name)), 
718                     WARNING_DIALOG);
719         }
721         return($match);
722     }
724     static function isReadablePath($message,$class,$name,$value, $type)
725     {
726         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
727    
728         // Display the reason for failing this check.         
729         if($message && ! $match){
730             if(!is_dir($value)){
731                 msg_dialog::display(_("Warning"), 
732                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
733                             bold($value),bold($class),bold($name)), 
734                         WARNING_DIALOG);
735             }elseif(!is_readable($value)){
736                 msg_dialog::display(_("Warning"), 
737                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"), 
738                             bold($value),bold($class),bold($name)), 
739                         WARNING_DIALOG);
740             }
741         }
743         return($match);
744     }
746     static function isWriteableFile($message,$class,$name,$value, $type)
747     {
748         $match = (file_exists($value) && is_writeable($value)) || 
749                  (!file_exists($value) && is_writeable(dirname($value)));
750                 
751    
752         // Display the reason for failing this check.         
753         if($message && ! $match){
755             if(!file_exists($value) && !is_writeable(dirname($value))){
756                 msg_dialog::display(_("Warning"), 
757                         sprintf(_("The file '%s' specified for '%s:%s' cannot be created neither be used for writing!"), 
758                             bold($value),bold($class),bold($name)), 
759                         WARNING_DIALOG);
760             }elseif(file_exists($value) && !is_writeable($value)){
761                 msg_dialog::display(_("Warning"), 
762                         sprintf(_("The file '%s' specified for '%s:%s' cannot be used for writing!"), 
763                             bold($value),bold($class),bold($name)), 
764                         WARNING_DIALOG);
765             }
766         }
768         return($match);
769     }
771     static function isWriteablePath($message,$class,$name,$value, $type)
772     {
773         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
774    
775         // Display the reason for failing this check.         
776         if($message && ! $match){
777             if(!is_dir($value)){
778                 msg_dialog::display(_("Warning"), 
779                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
780                             bold($value),bold($class),bold($name)), 
781                         WARNING_DIALOG);
782             }elseif(!is_writeable($value)){
783                 msg_dialog::display(_("Warning"), 
784                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"), 
785                             bold($value),bold($class),bold($name)), 
786                         WARNING_DIALOG);
787             }
788         }
790         return($match);
791     }
793     static function isReadableFile($message,$class,$name,$value, $type)
794     {
795         $match = !empty($value) && is_readable($value) && is_file($value);
797         // Display the reason for failing this check.         
798         if($message && ! $match){
799                 
800             if(!is_file($value)){
801                 msg_dialog::display(_("Warning"), 
802                         sprintf(_("The file '%s' specified for '%s:%s' does not exists!"), 
803                             bold($value),bold($class),bold($name)), 
804                         WARNING_DIALOG);
805             }elseif(!is_readable($value)){
806                 msg_dialog::display(_("Warning"), 
807                         sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"), 
808                             bold($value),bold($class),bold($name)), 
809                         WARNING_DIALOG);
810             }
811         }
813         return($match);
814     }
816     static function isCommand($message,$class,$name,$value, $type)
817     {
818         $match = TRUE;
820         // Display the reason for failing this check.         
821         if($message && ! $match){
822             msg_dialog::display(_("Warning"), 
823                     sprintf(_("The command '%s' specified for '%s:%s' is invalid!"), 
824                         bold($value),bold($class),bold($name)), 
825                     WARNING_DIALOG);
826         }
827         
828         return($match);
829     }
831     static function isDn($message,$class,$name,$value, $type)
832     {
833         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
835         // Display the reason for failing this check.         
836         if($message && ! $match){
837             msg_dialog::display(_("Warning"), 
838                     sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"), 
839                         bold($value),bold($class),bold($name)), 
840                     WARNING_DIALOG);
841         }
842         
843         return($match);
844     }
846     static function isRdn($message,$class,$name,$value, $type)
847     {
848         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
850         // Display the reason for failing this check.         
851         if($message && ! $match){
852             msg_dialog::display(_("Warning"), 
853                     sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"), 
854                         bold($value),bold($class),bold($name)), 
855                     WARNING_DIALOG);
856         }
857         
858         return($match);
859     }
861     private function _restoreCurrentValue()
862     {
863         // First check for values in the LDAP Database.
864         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
865             $this->setStatus('ldap');
866             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
867             return;
868         }
870         // Second check for values in the config file.
871         if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
872             $this->setStatus('file');
873             $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
874             return;
875         }
877         // If there still wasn't found anything then fallback to the default.
878         if($this->getStatus() == 'undefined'){
879             $this->value = $this->getDefault();
880         }
881     }
883     function getMigrate() { return($this->migrate); }
884     function getCheck() { return($this->check); }
885     function getName() { return($this->name); }
886     function getClass() { return($this->class); }
887     function getGroup() { return($this->group); }
888     function getType() { return($this->type); }
889     function getDescription() { return($this->description); }
890     function getDefault() { return($this->default); }
891     function getDefaults() { return($this->defaults); }
892     function getStatus() { return($this->status); }
893     function isMandatory() { return($this->mandatory); }
895     function setValue($str) 
896     {
897         if(in_array($this->getStatus(), array('modified'))){
898             $this->tmp_value = $str; 
899         }elseif($this->value != $str){
900             $this->setStatus('modified'); 
901             $this->tmp_value = $str; 
902         }
903     }
905     function getValue($temporary = FALSE) 
906     { 
907         if($temporary){
908             if(in_array($this->getStatus(), array('modified','removed'))){
909                 return($this->tmp_value); 
910             }else{
911                 return($this->value); 
912             }
913         }else{ 
915             // Do not return ldap values if we've to ignore them.
916             if($this->parent->ignoreLdapProperties){
917                 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
918                     return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
919                 }else{
920                     return($this->getDefault());
921                 }
922             }else{
923                 return($this->value); 
924             }
925         }
926     }
928     function restoreDefault() 
929     {
930         if(in_array($this->getStatus(),array('ldap'))){
931             $this->setStatus('removed'); 
933             // Second check for values in the config file.
934             if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
935                 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
936             }else{
937                 $this->tmp_value = $this->getDefault();
938             }
939         }
940     }
942     function save()
943     {
944         if($this->getStatus() == 'modified'){
945             $ldap = $this->parent->config->get_ldap_link();
946             $ldap->cd($this->parent->config->current['BASE']);
947             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
948             $ldap->cat($dn);
949             if(!$ldap->count()){
950                 $ldap->cd($dn);
951                 $data = array(
952                         'cn' => $this->class, 
953                         'objectClass' => array('top','gosaConfig'),
954                         'gosaSetting' => $this->name.":".$this->tmp_value);
956                 $ldap->add($data);
957                 if(!$ldap->success()){
958                     echo $ldap->get_error();
959                 }
961             }else{
962                 $attrs = $ldap->fetch();
963                 $data = array();
964                 $found = false;
965                 if(isset($attrs['gosaSetting']['count'])){
966                     for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
967                         $set = $attrs['gosaSetting'][$i];
968                         if(preg_match("/^{$this->name}:/", $set)){
969                             $set = "{$this->name}:{$this->tmp_value}";
970                             $found = true;
971                         }
972                         $data['gosaSetting'][] = $set;
973                     }
974                 }
975                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
976                 $ldap->cd($dn);
977                 $ldap->modify($data);
978                 if(!$ldap->success()){
979                     echo $ldap->get_error();
980                 }
981             }
982             $this->value = $this->tmp_value;
983             $this->setStatus('ldap'); 
984         }elseif($this->getStatus() == 'removed'){
985             $ldap = $this->parent->config->get_ldap_link();
986             $ldap->cd($this->parent->config->current['BASE']);
987             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
988             $ldap->cat($dn);
989             $attrs = $ldap->fetch();
990             $data = array('gosaSetting' => array());
991             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
992                 $set = $attrs['gosaSetting'][$i];
993                 if(preg_match("/^{$this->name}:/", $set)){
994                     continue;
995                 }
996                 $data['gosaSetting'][] = $set;
997             }
998             $ldap->cd($dn);
999             $ldap->modify($data);
1000             if(!$ldap->success()){
1001                 echo $ldap->get_error();
1002             }
1003             $this->_restoreCurrentValue();
1004         }
1005     }
1007     private function setStatus($state) 
1008     {
1009         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
1010             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
1011         }else{
1012             $this->status = $state; 
1013         }
1014     }
1016     function isValid() 
1017     { 
1018         return(TRUE);    
1019     }
1024 interface propertyMigration
1026     function __construct($config,$property);
1030 ?>