Code

Updated check for boolean config values
[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         restore_error_handler();
16         $this->config = &$config;
17         $this->reload();
18     }
20     function reload($force = FALSE)
21     {
22         // Do not reload the properties everytime, once we have  
23         //  everything loaded and registrered skip the reload.
24         // Status is 'finished' once we had a ldap connection (logged in)
25         if(!$force && $this->status == 'finished') return;
27         // Reset everything
28         $this->ldapStoredProperties = array();
29         $this->fileStoredProperties = array();
30         $this->properties = array();
31         $this->mapByName = array();
33         // Search for config flags defined in the config file (TAB section)
34         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
35             foreach($tabdefs as $info){
37                 // Check if the info is valid
38                 if(isset($info['NAME']) && isset($info['CLASS'])){
40                     // Check if there is nore than just the plugin definition
41                     if(count($info) > 2){
42                         foreach($info as $name => $value){
43                             
44                             if(!in_array($name, array('CLASS','NAME'))){
45                                 $class= $info['CLASS'];    
46                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
47                             }
48                         }
49                     }
50                 }
51             }
52         }
54         // Search for config flags defined in the config file (MAIN section)
55         foreach($this->config->data['MAIN'] as $name => $value){
56             $this->fileStoredProperties['core'][strtolower($name)] = $value;
57         }
59         // Search for config flags defined in the config file (Current LOCATION section)
60         if(isset($this->config->current)){
61             foreach($this->config->current as $name => $value){
62                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
63             }
64         }
66         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
67         if(!empty($this->config->current['CONFIG'])){
68             $ldap = $this->config->get_ldap_link();
69             $ldap->cd($this->config->current['CONFIG']);
70             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
71             while($attrs = $ldap->fetch()){
72                 $class = $attrs['cn'][0];
73                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
74                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
75                     $this->ldapStoredProperties[$class][$name] = $value;
76                 }
77             }
78             $this->status = 'finished';
79         }
81         global $class_mapping;
82         foreach ($class_mapping as $cname => $path){
83             $cmethods = get_class_methods($cname);
84             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
86                 // Get plugin definitions
87                 $def = call_user_func(array($cname, 'plInfo'));;
89                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
90                 if(isset($def['plShortName'])){
91                     $this->classToName[$cname] = $def['plShortName'];
92                     $data = array('name' => 'postcreate','type' => 'string');
93                     $this->register($cname, $data);    
94                     $data = array('name' => 'postremove','type' => 'string');
95                     $this->register($cname, $data);    
96                     $data = array('name' => 'postmodify','type' => 'string');
97                     $this->register($cname, $data);    
98                     $data = array('name' => 'checkhook', 'type' => 'string');
99                     $this->register($cname, $data);    
100                 }
102                 if(isset($def['plProperties'])){
103                     foreach($def['plProperties'] as $property){
104                         $this->register($cname, $property);
105                     }
106                 }
107             }
108         }
109     }
111     function register($class,$data)
112     {
113         $id = count($this->properties);
114         $this->properties[$id] = new gosaProperty($this,$class,$data);
115         $p = strtolower("{$class}::{$data['name']}");
116         $this->mapByName[$p] = $id;
117     }
119     public function getAllProperties()
120     {
121         return($this->properties);
122     }
124     function propertyExists($class,$name)
125     {       
126         $p = strtolower("{$class}::{$name}");
127         return(isset($this->mapByName[$p]));
128     }
130     private function getId($class,$name)
131     {
132         $p = strtolower("{$class}::{$name}");
133         if(!isset($this->mapByName[$p])){
134             return(-1);
135         }       
136         return($this->mapByName[$p]);    
137     }
139     function getProperty($class,$name)
140     {
141         if($this->propertyExists($class,$name)){
142             return($this->properties[$this->getId($class,$name)]);
143         }
144         return(NULL); 
145     }
147     function getPropertyValue($class,$name)
148     {   
149         if($this->propertyExists($class,$name)){
150             $tmp = $this->getProperty($class,$name);
151             return($tmp->getValue());
152         }
153         return("");
154     }
156     function setPropertyValue($class,$name, $value)
157     {   
158         if($this->propertyExists($class,$name)){
159             $tmp = $this->getProperty($class,$name);
160             return($tmp->setValue($value));
161         }
162         return("");
163     }
165     function saveChanges()
166     {
167         foreach($this->properties as $prop){
168             $prop->save();
169         }
170         $this->reload(TRUE);
171     }
175 class gosaProperty
177     protected $name = "";
178     protected $class = "";
179     protected $value = "";
180     protected $type = "string";
181     protected $default = "";
182     protected $defaults = "";
183     protected $description = "";
184     protected $check = "";
185     protected $migrate = "";
186     protected $mandatory = FALSE;
187     protected $group = "default";
188     protected $parent = NULL;
189     protected $data = array();
191     /*!  The current property status
192      *     'ldap'       Property is stored in ldap 
193      *     'file'       Property is stored in the config file
194      *     'undefined'  Property is currently not stored anywhere
195      *     'modified'   Property has been modified (should be saved)
196      */
197     protected $status = 'undefined';
199     protected $attributes = array('name','type','default','description','check',
200             'migrate','mandatory','group','defaults');
202     function __construct($parent,$classname,$data)
203     {
204         // Set some basic infos 
205         $this->parent = &$parent;
206         $this->class = $classname;
207         $this->data  = $data;
209         // Get all relevant information from the data array (comes from plInfo)    
210         foreach($this->attributes as $aName){
211             if(isset($data[$aName])){
212                 $this->$aName = $data[$aName];
213             }
214         }      
216         // Initialize with the current value
217         $this->_restoreCurrentValue(); 
218     }
220     private function _restoreCurrentValue()
221     {
222     
223         // First check for values in the LDAP Database.
224         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
225             $this->setStatus('ldap');
226             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
227             return;
228         }
230         // Second check for values in the config file.
231         if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
232             $this->setStatus('file');
233             $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
234             return;
235         }
237         // If there still wasn't found anything then fallback to the default.
238         if($this->getStatus() == 'undefined'){
239             $this->value = $this->getDefault();
240         }
241     }
243     function getValue() { return($this->value); }
244     function getMigrate() { return($this->migrate); }
245     function getCheck() { return($this->check); }
246     function getName() { return($this->name); }
247     function getClass() { return($this->class); }
248     function getGroup() { return($this->group); }
249     function getType() { return($this->type); }
250     function getDescription() { return($this->description); }
251     function getDefault() { return($this->default); }
252     function getDefaults() { return($this->defaults); }
253     function getStatus() { return($this->status); }
254     function isMandatory() { return($this->mandatory); }
256     function setValue($str) 
257     {
258         if($this->value != $str){
259             $this->setStatus('modified'); 
260             $this->value = $str; 
261         }
262     }
264     function restoreDefault() 
265     {
266         if(in_array($this->getStatus(),array('ldap'))){
267             $this->setStatus('removed'); 
268         }elseif(in_array($this->getStatus(),array('modified'))){
269             $this->_restoreCurrentValue();
270         }
271     }
273     function save()
274     {
275         if($this->getStatus() == 'modified'){
277             $ldap = $this->parent->config->get_ldap_link();
278             $ldap->cd($this->parent->config->current['BASE']);
279             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
280             $ldap->cat($dn);
281             if(!$ldap->count()){
282                 $ldap->cd($dn);
283                 $data = array(
284                         'cn' => $this->class, 
285                         'objectClass' => array('top','gosaConfig'),
286                         'gosaSetting' => $this->name.":".$this->value);
288                 $ldap->add($data);
289                 if(!$ldap->success()){
290                     echo $ldap->get_error();
291                 }
292                 $this->_restoreCurrentValue();
294             }else{
295                 $attrs = $ldap->fetch();
296                 $data = array();
297                 $found = false;
298                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
299                     $set = $attrs['gosaSetting'][$i];
300                     if(preg_match("/^{$this->name}:/", $set)){
301                         $set = "{$this->name}:{$this->value}";
302                         $found = true;
303                     }
304                     $data['gosaSetting'][] = $set;
305                 }
306                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
307                 $ldap->cd($dn);
308                 $ldap->modify($data);
309                 if(!$ldap->success()){
310                     echo $ldap->get_error();
311                 }
312                 $this->_restoreCurrentValue();
313             } 
314         }elseif($this->getStatus() == 'removed'){
315             $ldap = $this->parent->config->get_ldap_link();
316             $ldap->cd($this->parent->config->current['BASE']);
317             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
318             $ldap->cat($dn);
319             $attrs = $ldap->fetch();
320             $data = array('gosaSetting' => array());
321             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
322                 $set = $attrs['gosaSetting'][$i];
323                 if(preg_match("/^{$this->name}:/", $set)){
324                     continue;
325                 }
326                 $data['gosaSetting'][] = $set;
327             }
328             $ldap->cd($dn);
329             $ldap->modify($data);
330             if(!$ldap->success()){
331                 echo $ldap->get_error();
332             }
333             $this->_restoreCurrentValue();
334         }
335     }
337     private function setStatus($state) 
338     {
339         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
340             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
341         }else{
342             $this->status = $state; 
343         }
344     }
346     function isValid() 
347     { 
348         return(TRUE);    
349     }
352 ?>