Code

192330579c8915469f48a813078f0749ae9b72fb
[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     function __construct($config)
17     {
18         $this->config = &$config;
19         $this->reload();
20     }
22     function reload($force = FALSE)
23     {
24         // Do not reload the properties everytime, once we have  
25         //  everything loaded and registrered skip the reload.
26         // Status is 'finished' once we had a ldap connection (logged in)
27         if(!$force && $this->status == 'finished') return;
29         // Reset everything
30         $this->ldapStoredProperties = array();
31         $this->fileStoredProperties = array();
32         $this->properties = array();
33         $this->mapByName = array();
35         // Search for config flags defined in the config file (TAB section)
36         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
37             foreach($tabdefs as $info){
39                 // Check if the info is valid
40                 if(isset($info['NAME']) && isset($info['CLASS'])){
42                     // Check if there is nore than just the plugin definition
43                     if(count($info) > 2){
44                         foreach($info as $name => $value){
45                             
46                             if(!in_array($name, array('CLASS','NAME'))){
47                                 $class= $info['CLASS'];    
48                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
49                             }
50                         }
51                     }
52                 }
53             }
54         }
56         // Search for config flags defined in the config file (MENU section)
57         foreach($this->config->data['MENU'] as $section => $entries){
58             foreach($entries as $entry){
59                 if(count($entry) > 2 && isset($entry['CLASS'])){
60                     $class = $entry['CLASS'];
61                     foreach($entry as $name => $value){
62                         if(!in_array($name, array('CLASS','ACL'))){
63                             $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
64                         }
65                     }
66                 }
67             }
68         }
70         // Search for config flags defined in the config file (MAIN section)
71         foreach($this->config->data['MAIN'] as $name => $value){
72             $this->fileStoredProperties['core'][strtolower($name)] = $value;
73         }
75         // Search for config flags defined in the config file (Current LOCATION section)
76         if(isset($this->config->current)){
77             foreach($this->config->current as $name => $value){
78                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
79             }
80         }
82         // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
83         //  in the config. 
84         if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) && 
85             preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
86             $this->ignoreLdapProperties = TRUE;
87         }
89         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
90         if(!$this->ignoreLdapProperties && !empty($this->config->current['CONFIG'])){
91             $ldap = $this->config->get_ldap_link();
92             $ldap->cd($this->config->current['CONFIG']);
93             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
94             while($attrs = $ldap->fetch()){
95                 $class = $attrs['cn'][0];
96                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
97                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
98                     $this->ldapStoredProperties[$class][$name] = $value;
99                 }
100             }
101             $this->status = 'finished';
102         }
104         global $class_mapping;
105         foreach ($class_mapping as $cname => $path){
106             $cmethods = get_class_methods($cname);
107             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
109                 // Get plugin definitions
110                 $def = call_user_func(array($cname, 'plInfo'));;
112                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
113                 if(count($def)){
114                     
115                     $name = $cname;
116                     $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
117                     $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
119                     $this->classToName[$cname] = $name;
120                     $data = array('name' => 'postcreate','type' => 'command');
121                     $this->register($cname, $data);    
122                     $data = array('name' => 'postremove','type' => 'command');
123                     $this->register($cname, $data);    
124                     $data = array('name' => 'postmodify','type' => 'command');
125                     $this->register($cname, $data);    
126                     $data = array('name' => 'check', 'type' => 'command');
127                     $this->register($cname, $data);    
128                 }
130                 if(isset($def['plProperties'])){
131                     foreach($def['plProperties'] as $property){
132                         $this->register($cname, $property);
133                     }
134                 }
135             }
136         }
137     }
139     function register($class,$data)
140     {
141         $id = count($this->properties);
142         $this->properties[$id] = new gosaProperty($this,$class,$data);
143         $p = strtolower("{$class}::{$data['name']}");
144         $this->mapByName[$p] = $id;
145     }
147     public function getAllProperties()
148     {
149         return($this->properties);
150     }
152     function propertyExists($class,$name)
153     {       
154         $p = strtolower("{$class}::{$name}");
155         return(isset($this->mapByName[$p]));
156     }
158     private function getId($class,$name)
159     {
160         $p = strtolower("{$class}::{$name}");
161         if(!isset($this->mapByName[$p])){
162             return(-1);
163         }       
164         return($this->mapByName[$p]);    
165     }
167     function getProperty($class,$name)
168     {
169         if($this->propertyExists($class,$name)){
170             return($this->properties[$this->getId($class,$name)]);
171         }
172         return(NULL); 
173     }
175     function getPropertyValue($class,$name)
176     {   
177         if($this->propertyExists($class,$name)){
178             $tmp = $this->getProperty($class,$name);
179             return($tmp->getValue());
180         }
181         return("");
182     }
184     function setPropertyValue($class,$name, $value)
185     {   
186         if($this->propertyExists($class,$name)){
187             $tmp = $this->getProperty($class,$name);
188             return($tmp->setValue($value));
189         }
190         return("");
191     }
193     function saveChanges()
194     {
195         $migrate = array();
196         foreach($this->properties as $prop){
198             // Is this property modified
199             if(in_array($prop->getStatus(),array('modified','removed'))){
201                 // Check if we've to migrate something before we can make the changes effective. 
202                 if($prop->migrationRequired()){
203                     $migrate[] = $prop;
204                 }else{
205                     $prop->save();
206                 }
207             }
208         }
209         return($migrate);
210     }
214 class gosaProperty
216     protected $name = "";
217     protected $class = "";
218     protected $value = "";
219     protected $tmp_value = "";  // Used when modified but not saved 
220     protected $type = "string";
221     protected $default = "";
222     protected $defaults = "";
223     protected $description = "";
224     protected $check = "";
225     protected $migrate = "";
226     protected $mandatory = FALSE;
227     protected $group = "default";
228     protected $parent = NULL;
229     protected $data = array();
231     protected $migrationClass = NULL;
233     /*!  The current property status
234      *     'ldap'       Property is stored in ldap 
235      *     'file'       Property is stored in the config file
236      *     'undefined'  Property is currently not stored anywhere
237      *     'modified'   Property has been modified (should be saved)
238      */
239     protected $status = 'undefined';
241     protected $attributes = array('name','type','default','description','check',
242             'migrate','mandatory','group','defaults');
247     function __construct($parent,$classname,$data)
248     {
249         // Set some basic infos 
250         $this->parent = &$parent;
251         $this->class = $classname;
252         $this->data  = $data;
254         // Get all relevant information from the data array (comes from plInfo)    
255         foreach($this->attributes as $aName){
256             if(isset($data[$aName])){
257                 $this->$aName = $data[$aName];
258             }
259         }      
261         // Initialize with the current value
262         $this->_restoreCurrentValue(); 
264     }
266     function migrationRequired()
267     {
268         // Instantiate migration class 
269         if(!empty($this->migrate) && $this->migrationClass == NULL){
270             if(!class_available($this->migrate)){
271                 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
272             }else{
273                 $class = $this->migrate;
274                 $tmp = new $class($this->parent->config,$this);
275                 if(! $tmp instanceof propertyMigration){ 
276                     trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
277                 }else{
278                     $this->migrationClass = $tmp;
279                 }
280             }
281         }
282         if(empty($this->migrate) || $this->migrationClass == NULL){
283             return(FALSE);
284         }
285         return($this->migrationClass->checkForIssues());
286     }
288     function getMigrationClass()
289     {
290         return($this->migrationClass);
291     }
293     function check()
294     {
295         $val = $this->getValue(TRUE);
296         $return = TRUE;
297         if($this->mandatory && empty($val)){
298             $return = FALSE;
299         }
301         $check = $this->getCheck();
302         if(!empty($val) && !empty($check)){
303             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
304             if(!$res){
305                 $return = FALSE;
306             }
307         }
308         return($return);
309     }
311     static function isBool($message,$class,$name,$value, $type)
312     {
313         $match = in_array($value,array('true','false',''));
315         // Display the reason for failing this check.         
316         if($message && ! $match){
317             msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,"",_("Use 'true', 'false' or empty if allowed")), WARNING_DIALOG);
318         }
319     
320         return($match);
321     }
323     static function isString($message,$class,$name,$value, $type)
324     {
325         $match = TRUE;
326     
327         // Display the reason for failing this check.         
328         if($message && ! $match){
329             msg_dialog::display(_("Warning"), msgPool::invalid($name), WARNING_DIALOG);
330         }
332         return($match);
333     }
335     static function isInteger($message,$class,$name,$value, $type)
336     {
337         $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
339         // Display the reason for failing this check.         
340         if($message && ! $match){
341             msg_dialog::display(_("Warning"), msgPool::invalid($name, $value,'/[0-9]/'), WARNING_DIALOG);
342         }
344         return($match);
345     }
347     static function isPath($message,$class,$name,$value, $type)
348     {
349         $match = TRUE;
350     
351         // Display the reason for failing this check.         
352         if($message && ! $match){
353             msg_dialog::display(_("Warning"), msgPool::invalid($name), WARNING_DIALOG);
354         }
356         return($match);
357     }
359     static function isWriteablePath($message,$class,$name,$value, $type)
360     {
361         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
362     
363         // Display the reason for failing this check.         
364         if($message && ! $match){
366             if(!is_dir($value)){
367                 msg_dialog::display(_("Warning"), sprintf(_("The specified folder does not exists '%s'."), $value), WARNING_DIALOG);
368             }elseif(!is_writeable($value)){
369                 msg_dialog::display(_("Warning"), sprintf(_("The specified folder cannot be used for writing '%s'."), $value), WARNING_DIALOG);
370             }
371         }
373         return($match);
374     }
376     static function isReadableFile($message,$class,$name,$value, $type)
377     {
378         $match = !empty($value) && is_readable($value) && is_file($value);
380         // Display the reason for failing this check.         
381         if($message && ! $match){
382                 
383             if(!is_file($value)){
384                 msg_dialog::display(_("Warning"), sprintf(_("The specified file does not exists '%s'."), $value), WARNING_DIALOG);
385             }elseif(!is_readable($value)){
386                 msg_dialog::display(_("Warning"), sprintf(_("The specified file cannot be used for reading '%s'."), $value), WARNING_DIALOG);
387             }
388         }
390         return($match);
391     }
393     static function isCommand($message,$class,$name,$value, $type)
394     {
395         $match = TRUE;
397         // Display the reason for failing this check.         
398         if($message && ! $match){
399             msg_dialog::display(_("Warning"), msgPool::cmdinvalid($name,$value),  WARNING_DIALOG);
400         }
401         
402         return($match);
403     }
405     static function isDn($message,$class,$name,$value, $type)
406     {
407         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
409         // Display the reason for failing this check.         
410         if($message && ! $match){
411             msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,'','cn=user,ou=people,dc=example,dc=de'),  WARNING_DIALOG);
412         }
413         
414         return($match);
415     }
417     static function isRdn($message,$class,$name,$value, $type)
418     {
419         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*,$/i", $value);
421         // Display the reason for failing this check.         
422         if($message && ! $match){
423             msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,'','ou=people,'),  WARNING_DIALOG);
424         }
425         
426         return($match);
427     }
429     private function _restoreCurrentValue()
430     {
431         // First check for values in the LDAP Database.
432         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
433             $this->setStatus('ldap');
434             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
435             return;
436         }
438         // Second check for values in the config file.
439         if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
440             $this->setStatus('file');
441             $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
442             return;
443         }
445         // If there still wasn't found anything then fallback to the default.
446         if($this->getStatus() == 'undefined'){
447             $this->value = $this->getDefault();
448         }
449     }
451     function getMigrate() { return($this->migrate); }
452     function getCheck() { return($this->check); }
453     function getName() { return($this->name); }
454     function getClass() { return($this->class); }
455     function getGroup() { return($this->group); }
456     function getType() { return($this->type); }
457     function getDescription() { return($this->description); }
458     function getDefault() { return($this->default); }
459     function getDefaults() { return($this->defaults); }
460     function getStatus() { return($this->status); }
461     function isMandatory() { return($this->mandatory); }
463     function setValue($str) 
464     {
465         if(in_array($this->getStatus(), array('modified'))){
466             $this->tmp_value = $str; 
467         }elseif($this->value != $str){
468             $this->setStatus('modified'); 
469             $this->tmp_value = $str; 
470         }
471     }
473     function getValue($temporary = FALSE) 
474     { 
475         if($temporary && in_array($this->getStatus(), array('modified','removed'))){
476             return($this->tmp_value); 
477         }else{ 
478             return($this->value); 
479         }
480     }
482     function restoreDefault() 
483     {
484         if(in_array($this->getStatus(),array('ldap'))){
485             $this->setStatus('removed'); 
487             // Second check for values in the config file.
488             if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
489                 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
490             }else{
491                 $this->tmp_value = $this->getDefault();
492             }
493         }
494     }
496     function save()
497     {
498         if($this->getStatus() == 'modified'){
499             $ldap = $this->parent->config->get_ldap_link();
500             $ldap->cd($this->parent->config->current['BASE']);
501             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
502             $ldap->cat($dn);
503             if(!$ldap->count()){
504                 $ldap->cd($dn);
505                 $data = array(
506                         'cn' => $this->class, 
507                         'objectClass' => array('top','gosaConfig'),
508                         'gosaSetting' => $this->name.":".$this->tmp_value);
510                 $ldap->add($data);
511                 if(!$ldap->success()){
512                     echo $ldap->get_error();
513                 }
515             }else{
516                 $attrs = $ldap->fetch();
517                 $data = array();
518                 $found = false;
519                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
520                     $set = $attrs['gosaSetting'][$i];
521                     if(preg_match("/^{$this->name}:/", $set)){
522                         $set = "{$this->name}:{$this->tmp_value}";
523                         $found = true;
524                     }
525                     $data['gosaSetting'][] = $set;
526                 }
527                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
528                 $ldap->cd($dn);
529                 $ldap->modify($data);
530                 if(!$ldap->success()){
531                     echo $ldap->get_error();
532                 }
533             }
534             $this->value = $this->tmp_value;
535             $this->setStatus('ldap'); 
536         }elseif($this->getStatus() == 'removed'){
537             $ldap = $this->parent->config->get_ldap_link();
538             $ldap->cd($this->parent->config->current['BASE']);
539             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
540             $ldap->cat($dn);
541             $attrs = $ldap->fetch();
542             $data = array('gosaSetting' => array());
543             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
544                 $set = $attrs['gosaSetting'][$i];
545                 if(preg_match("/^{$this->name}:/", $set)){
546                     continue;
547                 }
548                 $data['gosaSetting'][] = $set;
549             }
550             $ldap->cd($dn);
551             $ldap->modify($data);
552             if(!$ldap->success()){
553                 echo $ldap->get_error();
554             }
555             $this->_restoreCurrentValue();
556         }
557     }
559     private function setStatus($state) 
560     {
561         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
562             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
563         }else{
564             $this->status = $state; 
565         }
566     }
568     function isValid() 
569     { 
570         return(TRUE);    
571     }
576 interface propertyMigration
578     function __construct($config,$property);
582 ?>