Code

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