Code

3e9cb3b275bbc7873ac2148d4252c1c5eb3634c0
[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
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 all config flags defined in the LDAP - BUT only if we ARE logged in. 
58         if(!empty($this->config->current['CONFIG'])){
59             $ldap = $this->config->get_ldap_link();
60             $ldap->cd($this->config->current['CONFIG']);
61             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
62             while($attrs = $ldap->fetch()){
63                 $class = $attrs['cn'][0];
64                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
65                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
66                     $this->ldapStoredProperties[$class][$name] = $value;
67                 }
68             }
69             $this->status = 'finished';
70         }
71  
72         global $class_mapping;
73         foreach ($class_mapping as $cname => $path){
74             $cmethods = get_class_methods($cname);
75             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
77                 // Get plugin definitions
78                 $def = call_user_func(array($cname, 'plInfo'));;
80                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
81                 if(isset($def['plShortName'])){
82                     $this->classToName[$cname] = $def['plShortName'];
83                     $data = array('name' => 'postcreate','type' => 'string');
84                     $this->register($cname, $data);    
85                     $data = array('name' => 'postremove','type' => 'string');
86                     $this->register($cname, $data);    
87                     $data = array('name' => 'postmodify','type' => 'string');
88                     $this->register($cname, $data);    
89                     $data = array('name' => 'checkhook', 'type' => 'string');
90                     $this->register($cname, $data);    
91                 }
93                 if(isset($def['plProperties'])){
94                     foreach($def['plProperties'] as $property){
95                         $this->register($cname, $property);
96                     }
97                 }
98             }
99         }
100     }
102     function register($class,$data)
103     {
104         $id = count($this->properties);
105         $this->properties[$id] = new gosaProperty($this,$class,$data);
106         $this->mapByName[$class][$data['name']] = $id;
107         $this->mapByClass[$class][] = $id;
108         $this->mapPropertyToClass[$id] = $class;
109     }
111     public function getAllProperties()
112     {
113         return($this->properties);
114     }
116     function propertyExists($class,$name)
117     {
118         return(isset($this->mapByName[$class][$name]));
119     }
121     private function getId($class,$name)
122     {
123         return($this->mapByName[$class][$name]);    
124     }
126     function getProperty($class,$name)
127     {
128         if($this->propertyExists($class,$name)){
129             return($this->properties[$this->getId($class,$name)]);
130         }
131         return(NULL); 
132     }
134     function getPropertyValue($class,$name)
135     {   
136         if($this->propertyExists($class,$name)){
137             $tmp = $this->getProperty($class,$name);
138             return($tmp->getValue());
139         }
140         return("");
141     }
143     function setPropertyValue($class,$name, $value)
144     {   
145         if($this->propertyExists($class,$name)){
146             $tmp = $this->getProperty($class,$name);
147             return($tmp->setValue($value));
148         }
149         return("");
150     }
152     function saveChanges()
153     {
154         foreach($this->properties as $prop){
155             $prop->save();
156         }
157     }
161 class gosaProperty
163     protected $name = "";
164     protected $class = "";
165     protected $value = "";
166     protected $type = "string";
167     protected $default = "";
168     protected $description = "";
169     protected $check = "";
170     protected $migrate = "";
171     protected $mandatory = FALSE;
172     protected $group = "default";
173     protected $parent = NULL;
174     protected $data = array();
176     /*!  The current property status
177      *     'ldap'       Property is stored in ldap 
178      *     'file'       Property is stored in the config file
179      *     'undefined'  Property is currently not stored anywhere
180      *     'modified'   Property has been modified (should be saved)
181      */
182     protected $status = 'undefined';
184     protected $attributes = array('name','type','default','description','check',
185             'migrate','mandatory','group');
187     function __construct($parent,$classname,$data)
188     {
189         // Set some basic infos 
190         $this->parent = &$parent;
191         $this->class = $classname;
192         $this->data  = $data;
194         // Get all relevant information from the data array (comes from plInfo)    
195         foreach($this->attributes as $aName){
196             if(isset($data[$aName])){
197                 $this->$aName = $data[$aName];
198             }
199         }       
201         // First check for values in the LDAP Database.
202         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
203             $this->setStatus('ldap');
204             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
205         }
207         // Second check for values in the config file.
208         if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
209             $this->setStatus('file');
210             $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
211         }
213         // If there still wasn't found anything then fallback to the default.
214         if($this->getStatus() == 'undefined'){
215             $this->value = $this->getDefault();
216         }
217     }
219     function getValue() { return($this->value); }
220     function getMigrate() { return($this->migrate); }
221     function getCheck() { return($this->check); }
222     function getName() { return($this->name); }
223     function getClass() { return($this->class); }
224     function getGroup() { return($this->group); }
225     function getType() { return($this->type); }
226     function getDescription() { return($this->description); }
227     function getDefault() { return($this->default); }
228     function getStatus() { return($this->status); }
229     function isMandatory() { return($this->mandatory); }
231     function setValue($str) 
232     {
233         $this->setStatus('modified'); 
234         $this->value = $str; 
235     }
237     function restoreDefault() 
238     {
239         $this->setStatus('removed'); 
240     }
242     function save()
243     {
244         if($this->getStatus() == 'modified'){
246             $ldap = $this->parent->config->get_ldap_link();
247             $ldap->cd($this->parent->config->current['BASE']);
248             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
249             $ldap->cat($dn);
250             if(!$ldap->count()){
251                 $ldap->cd($dn);
252                 $data = array(
253                             'cn' => $this->class, 
254                             'objectClass' => array('top','gosaConfig'),
255                             'gosaSetting' => $this->name.":".$this->value);
257                 $ldap->add($data);
258                 if($ldap->success()){
259                     $this->status = 'ldap';
260                 }else{
261                     echo $ldap->get_error();
262                 }
263             }else{
264                 $attrs = $ldap->fetch();
265                 $data = array();
266                 $found = false;
267                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
268                     $set = $attrs['gosaSetting'][$i];
269                     if(preg_match("/^{$this->name}:/", $set)){
270                         $set = "{$this->name}:{$this->value}";
271                         $found = true;
272                     }
273                     $data['gosaSetting'][] = $set;
274                 }
275                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
276                 $ldap->cd($dn);
277                 $ldap->modify($data);
278                 if($ldap->success()){
279                     $this->status = 'ldap';
280                 }else{
281                     echo $ldap->get_error();
282                 }
283             } 
284         }elseif($this->getStatus() == 'removed'){
285             $ldap = $this->parent->config->get_ldap_link();
286             $ldap->cd($this->parent->config->current['BASE']);
287             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
288             $ldap->cat($dn);
289             $attrs = $ldap->fetch();
290             $data = array();
291             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
292                 $set = $attrs['gosaSetting'][$i];
293                 if(preg_match("/^{$this->name}:/", $set)){
294                     continue;
295                 }
296                 $data['gosaSetting'][] = $set;
297             }
298             $ldap->cd($dn);
299             $ldap->modify($data);
300             if($ldap->success()){
301                 $this->status = 'undefined';
303                 // Second check for values in the config file.
304                 if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
305                     $this->setStatus('file');
306                     $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
307                 }
309                 // If there still wasn't found anything then fallback to the default.
310                 if($this->getStatus() == 'undefined'){
311                     $this->value = $this->getDefault();
312                 }
314             }else{
315                 echo $ldap->get_error();
316             }
317         }
318     }
320     private function setStatus($state) 
321     {
322         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
323             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
324         }else{
325             $this->status = $state; 
326         }
327     }
329     function isValid() 
330     { 
331         return(TRUE);    
332     }
335 ?>