Code

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