Code

Updated accountRDN property 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     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 (MAIN section)
54         foreach($this->config->data['MAIN'] as $name => $value){
55             $this->fileStoredProperties['core'][strtolower($name)] = $value;
56         }
58         // Search for config flags defined in the config file (Current LOCATION section)
59         if(isset($this->config->current)){
60             foreach($this->config->current as $name => $value){
61                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
62             }
63         }
65         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
66         if(!empty($this->config->current['CONFIG'])){
67             $ldap = $this->config->get_ldap_link();
68             $ldap->cd($this->config->current['CONFIG']);
69             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
70             while($attrs = $ldap->fetch()){
71                 $class = $attrs['cn'][0];
72                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
73                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
74                     $this->ldapStoredProperties[$class][$name] = $value;
75                 }
76             }
77             $this->status = 'finished';
78         }
80         global $class_mapping;
81         foreach ($class_mapping as $cname => $path){
82             $cmethods = get_class_methods($cname);
83             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
85                 // Get plugin definitions
86                 $def = call_user_func(array($cname, 'plInfo'));;
88                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
89                 if(count($def)){
90                     
91                     $name = $cname;
92                     $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
93                     $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
95                     $this->classToName[$cname] = $name;
96                     $data = array('name' => 'postcreate','type' => 'command');
97                     $this->register($cname, $data);    
98                     $data = array('name' => 'postremove','type' => 'command');
99                     $this->register($cname, $data);    
100                     $data = array('name' => 'postmodify','type' => 'command');
101                     $this->register($cname, $data);    
102                     $data = array('name' => 'check', 'type' => 'command');
103                     $this->register($cname, $data);    
104                 }
106                 if(isset($def['plProperties'])){
107                     foreach($def['plProperties'] as $property){
108                         $this->register($cname, $property);
109                     }
110                 }
111             }
112         }
113     }
115     function register($class,$data)
116     {
117         $id = count($this->properties);
118         $this->properties[$id] = new gosaProperty($this,$class,$data);
119         $p = strtolower("{$class}::{$data['name']}");
120         $this->mapByName[$p] = $id;
121     }
123     public function getAllProperties()
124     {
125         return($this->properties);
126     }
128     function propertyExists($class,$name)
129     {       
130         $p = strtolower("{$class}::{$name}");
131         return(isset($this->mapByName[$p]));
132     }
134     private function getId($class,$name)
135     {
136         $p = strtolower("{$class}::{$name}");
137         if(!isset($this->mapByName[$p])){
138             return(-1);
139         }       
140         return($this->mapByName[$p]);    
141     }
143     function getProperty($class,$name)
144     {
145         if($this->propertyExists($class,$name)){
146             return($this->properties[$this->getId($class,$name)]);
147         }
148         return(NULL); 
149     }
151     function getPropertyValue($class,$name)
152     {   
153         if($this->propertyExists($class,$name)){
154             $tmp = $this->getProperty($class,$name);
155             return($tmp->getValue());
156         }
157         return("");
158     }
160     function setPropertyValue($class,$name, $value)
161     {   
162         if($this->propertyExists($class,$name)){
163             $tmp = $this->getProperty($class,$name);
164             return($tmp->setValue($value));
165         }
166         return("");
167     }
169     function saveChanges()
170     {
171         foreach($this->properties as $prop){
172             $prop->save();
173         }
174         $this->reload(TRUE);
175     }
179 class gosaProperty
181     protected $name = "";
182     protected $class = "";
183     protected $value = "";
184     protected $tmp_value = "";  // Used when modified but not saved 
185     protected $type = "string";
186     protected $default = "";
187     protected $defaults = "";
188     protected $description = "";
189     protected $check = "";
190     protected $migrate = "";
191     protected $mandatory = FALSE;
192     protected $group = "default";
193     protected $parent = NULL;
194     protected $data = array();
196     /*!  The current property status
197      *     'ldap'       Property is stored in ldap 
198      *     'file'       Property is stored in the config file
199      *     'undefined'  Property is currently not stored anywhere
200      *     'modified'   Property has been modified (should be saved)
201      */
202     protected $status = 'undefined';
204     protected $attributes = array('name','type','default','description','check',
205             'migrate','mandatory','group','defaults');
207     function __construct($parent,$classname,$data)
208     {
209         // Set some basic infos 
210         $this->parent = &$parent;
211         $this->class = $classname;
212         $this->data  = $data;
214         // Get all relevant information from the data array (comes from plInfo)    
215         foreach($this->attributes as $aName){
216             if(isset($data[$aName])){
217                 $this->$aName = $data[$aName];
218             }
219         }      
221         // Initialize with the current value
222         $this->_restoreCurrentValue(); 
223     }
225     function check()
226     {
227         $val = $this->getValue(TRUE);
228         $return = TRUE;
229         if($this->mandatory && empty($val)){
230             msg_dialog::display(_("Error"), msgPool::required(_($this->name)), ERROR_DIALOG);
231             $return = FALSE;
232         }
234         $check = $this->getCheck();
235         if(!empty($val) && !empty($check)){
236             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
237             if(!$res){
238                 msg_dialog::display(_("Error"), msgPool::invalid(_($this->name)), ERROR_DIALOG);
239                 $return = FALSE;
240             }
241         }
242         return($return);
243     }
245     static function isBool($message,$class,$name,$value, $type)
246     {
247         return(in_array($value,array('true','false','')));
248     }
250     static function isString($message,$class,$name,$value, $type)
251     {
252         return(TRUE);
253     }
255     static function isInteger($message,$class,$name,$value, $type)
256     {
257         return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
258     }
260     static function isPath($message,$class,$name,$value, $type)
261     {
262         return(TRUE);
263     }
265     static function isWriteablePath($message,$class,$name,$value, $type)
266     {
267         return(!empty($value)&&is_writeable($value));
268     }
270     static function isReadableFile($message,$class,$name,$value, $type)
271     {
272         return(!empty($value)&&is_readable($value)&&is_file($value));
273     }
275     static function isCommand($message,$class,$name,$value, $type)
276     {
277         return(TRUE);
278     }
280     static function isDn($message,$class,$name,$value, $type)
281     {
282         return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
283     }
285     static function isRdn($message,$class,$name,$value, $type)
286     {
287         return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
288     }
290     private function _restoreCurrentValue()
291     {
292     
293         // First check for values in the LDAP Database.
294         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
295             $this->setStatus('ldap');
296             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
297             return;
298         }
300         // Second check for values in the config file.
301         if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
302             $this->setStatus('file');
303             $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
304             return;
305         }
307         // If there still wasn't found anything then fallback to the default.
308         if($this->getStatus() == 'undefined'){
309             $this->value = $this->getDefault();
310         }
311     }
313     function getMigrate() { return($this->migrate); }
314     function getCheck() { return($this->check); }
315     function getName() { return($this->name); }
316     function getClass() { return($this->class); }
317     function getGroup() { return($this->group); }
318     function getType() { return($this->type); }
319     function getDescription() { return($this->description); }
320     function getDefault() { return($this->default); }
321     function getDefaults() { return($this->defaults); }
322     function getStatus() { return($this->status); }
323     function isMandatory() { return($this->mandatory); }
325     function setValue($str) 
326     {
327         if(in_array($this->getStatus(), array('modified'))){
328             $this->tmp_value = $str; 
329         }elseif($this->value != $str){
330             $this->setStatus('modified'); 
331             $this->tmp_value = $str; 
332         }
333     }
335     function getValue($temporary = FALSE) 
336     { 
337         if($temporary && in_array($this->getStatus(), array('modified'))){
338             return($this->tmp_value); 
339         }else{ 
340             return($this->value); 
341         }
342     }
344     function restoreDefault() 
345     {
346         if(in_array($this->getStatus(),array('ldap'))){
347             $this->setStatus('removed'); 
348         }elseif(in_array($this->getStatus(),array('modified'))){
349             $this->_restoreCurrentValue();
350         }
351     }
353     function save()
354     {
355         if($this->getStatus() == 'modified'){
356             $ldap = $this->parent->config->get_ldap_link();
357             $ldap->cd($this->parent->config->current['BASE']);
358             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
359             $ldap->cat($dn);
360             if(!$ldap->count()){
361                 $ldap->cd($dn);
362                 $data = array(
363                         'cn' => $this->class, 
364                         'objectClass' => array('top','gosaConfig'),
365                         'gosaSetting' => $this->name.":".$this->tmp_value);
367                 $ldap->add($data);
368                 if(!$ldap->success()){
369                     echo $ldap->get_error();
370                 }
372             }else{
373                 $attrs = $ldap->fetch();
374                 $data = array();
375                 $found = false;
376                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
377                     $set = $attrs['gosaSetting'][$i];
378                     if(preg_match("/^{$this->name}:/", $set)){
379                         $set = "{$this->name}:{$this->tmp_value}";
380                         $found = true;
381                     }
382                     $data['gosaSetting'][] = $set;
383                 }
384                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
385                 $ldap->cd($dn);
386                 $ldap->modify($data);
387                 if(!$ldap->success()){
388                     echo $ldap->get_error();
389                 }
390             } 
391             $this->_restoreCurrentValue();
392         }elseif($this->getStatus() == 'removed'){
393             $ldap = $this->parent->config->get_ldap_link();
394             $ldap->cd($this->parent->config->current['BASE']);
395             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
396             $ldap->cat($dn);
397             $attrs = $ldap->fetch();
398             $data = array('gosaSetting' => array());
399             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
400                 $set = $attrs['gosaSetting'][$i];
401                 if(preg_match("/^{$this->name}:/", $set)){
402                     continue;
403                 }
404                 $data['gosaSetting'][] = $set;
405             }
406             $ldap->cd($dn);
407             $ldap->modify($data);
408             if(!$ldap->success()){
409                 echo $ldap->get_error();
410             }
411             $this->_restoreCurrentValue();
412         }
413     }
415     private function setStatus($state) 
416     {
417         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
418             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
419         }else{
420             $this->status = $state; 
421         }
422     }
424     function isValid() 
425     { 
426         return(TRUE);    
427     }
430 ?>