Code

Removed debug reload
[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     }
154 class gosaProperty
156     protected $name = "";
157     protected $class = "";
158     protected $value = "";
159     protected $type = "string";
160     protected $default = "";
161     protected $description = "";
162     protected $check = "";
163     protected $migrate = "";
164     protected $mandatory = FALSE;
165     protected $group = "default";
166     protected $parent = NULL;
167     protected $data = array();
169     /*!  The current property status
170      *     'ldap'       Property is stored in ldap 
171      *     'file'       Property is stored in the config file
172      *     'undefined'  Property is currently not stored anywhere
173      *     'modified'   Property has been modified (should be saved)
174      */
175     protected $status = 'undefined';
177     protected $attributes = array('name','type','default','description','check',
178             'migrate','mandatory','group');
180     function __construct($parent,$classname,$data)
181     {
182         // Set some basic infos 
183         $this->parent = &$parent;
184         $this->class = $classname;
185         $this->data  = $data;
187         // Get all relevant information from the data array (comes from plInfo)    
188         foreach($this->attributes as $aName){
189             if(isset($data[$aName])){
190                 $this->$aName = $data[$aName];
191             }
192         }       
194         // First check for values in the LDAP Database.
195         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
196             $this->setStatus('ldap');
197             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
198         }
200         // Second check for values in the config file.
201         if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
202             $this->setStatus('file');
203             $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
204         }
206         // If there still wasn't found anything then fallback to the default.
207         if($this->getStatus() == 'undefined'){
208             $this->value = $this->getDefault();
209         }
210     }
212     function getValue() { return($this->value); }
213     function getName() { return($this->name); }
214     function getClass() { return($this->class); }
215     function getGroup() { return($this->group); }
216     function getType() { return($this->type); }
217     function getDescription() { return($this->description); }
218     function getDefault() { return($this->default); }
219     function getStatus() { return($this->status); }
221     function setValue($str) 
222     {
223         $this->setStatus('modified'); 
224         $this->value = $str; 
225     }
227     function save()
228     {
229         if($this->getStatus() == 'modified'){
231             $ldap = $this->parent->config->get_ldap_link();
232             $ldap->cd($this->parent->config->current['BASE']);
233             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
234             $ldap->cat($dn);
235             if(!$ldap->count()){
236                 $ldap->cd($dn);
237                 $data = array(
238                             'cn' => $this->class, 
239                             'objectClass' => array('top','gosaConfig'),
240                             'gosaSetting' => $this->name.":".$this->value);
242                 $ldap->add($data);
243                 if($ldap->success()){
244                     $this->status = 'ldap';
245                 }else{
246                     echo $ldap->get_error();
247                 }
248             }else{
249                 $attrs = $ldap->fetch();
250                 $data = array();
251                 $found = false;
252                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
253                     $set = $attrs['gosaSetting'][$i];
254                     if(preg_match("/^{$this->name}:/", $set)){
255                         $set = "{$this->name}:{$this->value}";
256                         $found = true;
257                     }
258                     $data['gosaSetting'][] = $set;
259                 }
260                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
261                 $ldap->cd($dn);
262                 $ldap->modify($data);
263                 if($ldap->success()){
264                     $this->status = 'ldap';
265                 }else{
266                     echo $ldap->get_error();
267                 }
268             } 
269         }
270     }
272     private function setStatus($state) 
273     {
274         if(!in_array($state, array('ldap','file','undefined','modified'))) {
275             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
276         }else{
277             $this->status = $state; 
278         }
279     }
281     function isValid() 
282     { 
283         return(TRUE);    
284     }
287 ?>