Code

Updated config flags
[gosa.git] / gosa-core / include / class_configRegistry.inc
1 <?php
3 class configRegistry{
5     public $config = NULL;
6     public $properties = array();
7     public $mapByClass = array();
8     public $mapPropertyToClass = array();
9     public $ldapStoredProperties = array(); 
10     public $fileStoredProperties = array(); 
11     public $classToName = array(); 
13     public $status = 'none';
15     function __construct($config)
16     {
17         restore_error_handler();
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->mapByClass = array();
34         $this->mapByName = array();
35         $this->mapPropertyToClass = array();
37         // Search for config flags defined in the config file (TAB section)
38         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
39             foreach($tabdefs as $info){
41                 // Check if the info is valid
42                 if(isset($info['NAME']) && isset($info['CLASS'])){
44                     // Check if there is nore than just the plugin definition
45                     if(count($info) > 2){
46                         foreach($info as $name => $value){
47                             
48                             if(!in_array($name, array('CLASS','NAME'))){
49                                 $class= $info['CLASS'];    
50                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
51                             }
52                         }
53                     }
54                 }
55             }
56         }
58         // Search for config flags defined in the config file (MAIN section)
59         foreach($this->config->data['MAIN'] as $name => $value){
60             $this->fileStoredProperties['core'][strtolower($name)] = $value;
61         }
63         // Search for config flags defined in the config file (Current LOCATION section)
64         if(isset($this->config->current)){
65             foreach($this->config->current as $name => $value){
66                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
67             }
68         }
70         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
71         if(!empty($this->config->current['CONFIG'])){
72             $ldap = $this->config->get_ldap_link();
73             $ldap->cd($this->config->current['CONFIG']);
74             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
75             while($attrs = $ldap->fetch()){
76                 $class = $attrs['cn'][0];
77                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
78                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
79                     $this->ldapStoredProperties[$class][$name] = $value;
80                 }
81             }
82             $this->status = 'finished';
83         }
85         global $class_mapping;
86         foreach ($class_mapping as $cname => $path){
87             $cmethods = get_class_methods($cname);
88             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
90                 // Get plugin definitions
91                 $def = call_user_func(array($cname, 'plInfo'));;
93                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
94                 if(isset($def['plShortName'])){
95                     $this->classToName[$cname] = $def['plShortName'];
96                     $data = array('name' => 'postcreate','type' => 'string');
97                     $this->register($cname, $data);    
98                     $data = array('name' => 'postremove','type' => 'string');
99                     $this->register($cname, $data);    
100                     $data = array('name' => 'postmodify','type' => 'string');
101                     $this->register($cname, $data);    
102                     $data = array('name' => 'checkhook', 'type' => 'string');
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         $this->mapByName[$class][$data['name']] = $id;
120         $this->mapByClass[$class][] = $id;
121         $this->mapPropertyToClass[$id] = $class;
122     }
124     public function getAllProperties()
125     {
126         return($this->properties);
127     }
129     function propertyExists($class,$name)
130     {
131         if(!isset($this->mapByName[$class][$name])){
132         print_a(array($class,$name));
134         }
135         return(isset($this->mapByName[$class][$name]));
136     }
138     private function getId($class,$name)
139     {
140         return($this->mapByName[$class][$name]);    
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 $type = "string";
185     protected $default = "";
186     protected $description = "";
187     protected $check = "";
188     protected $migrate = "";
189     protected $mandatory = FALSE;
190     protected $group = "default";
191     protected $parent = NULL;
192     protected $data = array();
194     /*!  The current property status
195      *     'ldap'       Property is stored in ldap 
196      *     'file'       Property is stored in the config file
197      *     'undefined'  Property is currently not stored anywhere
198      *     'modified'   Property has been modified (should be saved)
199      */
200     protected $status = 'undefined';
202     protected $attributes = array('name','type','default','description','check',
203             'migrate','mandatory','group');
205     function __construct($parent,$classname,$data)
206     {
207         // Set some basic infos 
208         $this->parent = &$parent;
209         $this->class = $classname;
210         $this->data  = $data;
212         // Get all relevant information from the data array (comes from plInfo)    
213         foreach($this->attributes as $aName){
214             if(isset($data[$aName])){
215                 $this->$aName = $data[$aName];
216             }
217         }      
219         // Initialize with the current value
220         $this->_restoreCurrentValue(); 
221     }
223     private function _restoreCurrentValue()
224     {
225     
226         // First check for values in the LDAP Database.
227         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
228             $this->setStatus('ldap');
229             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
230             return;
231         }
233         // Second check for values in the config file.
234         if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
235             $this->setStatus('file');
236             $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
237             return;
238         }
240         // If there still wasn't found anything then fallback to the default.
241         if($this->getStatus() == 'undefined'){
242             $this->value = $this->getDefault();
243         }
244     }
246     function getValue() { return($this->value); }
247     function getMigrate() { return($this->migrate); }
248     function getCheck() { return($this->check); }
249     function getName() { return($this->name); }
250     function getClass() { return($this->class); }
251     function getGroup() { return($this->group); }
252     function getType() { return($this->type); }
253     function getDescription() { return($this->description); }
254     function getDefault() { return($this->default); }
255     function getStatus() { return($this->status); }
256     function isMandatory() { return($this->mandatory); }
258     function setValue($str) 
259     {
260         if($this->value != $str){
261             $this->setStatus('modified'); 
262             $this->value = $str; 
263         }
264     }
266     function restoreDefault() 
267     {
268         if(in_array($this->getStatus(),array('ldap'))){
269             $this->setStatus('removed'); 
270         }elseif(in_array($this->getStatus(),array('modified'))){
271             $this->_restoreCurrentValue();
272         }
273     }
275     function save()
276     {
277         if($this->getStatus() == 'modified'){
279             $ldap = $this->parent->config->get_ldap_link();
280             $ldap->cd($this->parent->config->current['BASE']);
281             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
282             $ldap->cat($dn);
283             if(!$ldap->count()){
284                 $ldap->cd($dn);
285                 $data = array(
286                         'cn' => $this->class, 
287                         'objectClass' => array('top','gosaConfig'),
288                         'gosaSetting' => $this->name.":".$this->value);
290                 $ldap->add($data);
291                 if(!$ldap->success()){
292                     echo $ldap->get_error();
293                 }
294                 $this->_restoreCurrentValue();
296             }else{
297                 $attrs = $ldap->fetch();
298                 $data = array();
299                 $found = false;
300                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
301                     $set = $attrs['gosaSetting'][$i];
302                     if(preg_match("/^{$this->name}:/", $set)){
303                         $set = "{$this->name}:{$this->value}";
304                         $found = true;
305                     }
306                     $data['gosaSetting'][] = $set;
307                 }
308                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
309                 $ldap->cd($dn);
310                 $ldap->modify($data);
311                 if(!$ldap->success()){
312                     echo $ldap->get_error();
313                 }
314                 $this->_restoreCurrentValue();
315             } 
316         }elseif($this->getStatus() == 'removed'){
317             $ldap = $this->parent->config->get_ldap_link();
318             $ldap->cd($this->parent->config->current['BASE']);
319             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
320             $ldap->cat($dn);
321             $attrs = $ldap->fetch();
322             $data = array('gosaSetting' => array());
323             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
324                 $set = $attrs['gosaSetting'][$i];
325                 if(preg_match("/^{$this->name}:/", $set)){
326                     continue;
327                 }
328                 $data['gosaSetting'][] = $set;
329             }
330             $ldap->cd($dn);
331             $ldap->modify($data);
332             if(!$ldap->success()){
333                 echo $ldap->get_error();
334             }
335             $this->_restoreCurrentValue();
336         }
337     }
339     private function setStatus($state) 
340     {
341         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
342             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
343         }else{
344             $this->status = $state; 
345         }
346     }
348     function isValid() 
349     { 
350         return(TRUE);    
351     }
354 ?>