Code

b05953e3d0d8cf38c2b31b0792394cc8499612c8
[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     
28         $force = TRUE;
29         
30         if(!$force && $this->status == 'finished') return;
32         // Reset everything
33         $this->ldapStoredProperties = array();
34         $this->fileStoredProperties = array();
35         $this->properties = array();
36         $this->mapByClass = array();
37         $this->mapPropertyToClass = array();
38         
39         // Search for config flags defined in the config file
40         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
41             foreach($tabdefs as $info){
43                 // Check if the info is valid
44                 if(isset($info['NAME']) && isset($info['CLASS'])){
46                     // Check if there is nore than just the plugin definition
47                     if(count($info) > 2){
48                         foreach($info as $name => $value){
49                             
50                             if(!in_array($name, array('CLASS','NAME'))){
51                                 $class= $info['CLASS'];    
52                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
53                             }
54                         }
55                     }
56                 }
57             }
58         }
60         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
61         if(!empty($this->config->current['CONFIG'])){
62             $ldap = $this->config->get_ldap_link();
63             $ldap->cd($this->config->current['CONFIG']);
64             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
65             while($attrs = $ldap->fetch()){
66                 $class = $attrs['cn'][0];
67                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
68                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
69                     $this->ldapStoredProperties[$class][$name] = $value;
70                 }
71             }
72             $this->status = 'finished';
73         }
74  
75         global $class_mapping;
76         foreach ($class_mapping as $cname => $path){
77             $cmethods = get_class_methods($cname);
78             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
80                 // Get plugin definitions
81                 $def = call_user_func(array($cname, 'plInfo'));;
83                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
84                 if(isset($def['plShortName'])){
85                     $this->classToName[$cname] = $def['plShortName'];
86                     $data = array('name' => 'postcreate','type' => 'string');
87                     $this->register($cname, $data);    
88                     $data = array('name' => 'postremove','type' => 'string');
89                     $this->register($cname, $data);    
90                     $data = array('name' => 'postmodify','type' => 'string');
91                     $this->register($cname, $data);    
92                     $data = array('name' => 'checkhook', 'type' => 'string');
93                     $this->register($cname, $data);    
94                 }
96                 if(isset($def['plProperties'])){
97                     foreach($def['plProperties'] as $property){
98                         $this->register($cname, $property);
99                     }
100                 }
101             }
102         }
103     }
105     function register($class,$data)
106     {
107         $id = count($this->properties);
108         $this->properties[$id] = new gosaProperty($this,$class,$data);
109         $this->mapByName[$class][$data['name']] = $id;
110         $this->mapByClass[$class][] = $id;
111         $this->mapPropertyToClass[$id] = $class;
112     }
114     public function getAllProperties()
115     {
116         return($this->properties);
117     }
119     function propertyExists($class,$name)
120     {
121         return(isset($this->mapByName[$class][$name]));
122     }
124     private function getId($class,$name)
125     {
126         return($this->mapByName[$class][$name]);    
127     }
129     function getProperty($class,$name)
130     {
131         if($this->propertyExists($class,$name)){
132             return($this->properties[$this->getId($class,$name)]);
133         }
134         return(NULL); 
135     }
137     function getPropertyValue($class,$name)
138     {   
139         if($this->propertyExists($class,$name)){
140             $tmp = $this->getProperty($class,$name);
141             return($tmp->getValue());
142         }
143         return("");
144     }
146     function setPropertyValue($class,$name, $value)
147     {   
148         if($this->propertyExists($class,$name)){
149             $tmp = $this->getProperty($class,$name);
150             return($tmp->setValue($value));
151         }
152         return("");
153     }
157 class gosaProperty
159     protected $name = "";
160     protected $class = "";
161     protected $value = "";
162     protected $type = "string";
163     protected $default = "";
164     protected $description = "";
165     protected $check = "";
166     protected $migrate = "";
167     protected $mandatory = FALSE;
168     protected $group = "default";
169     protected $parent = NULL;
170     protected $data = array();
172     /*!  The current property status
173      *     'ldap'       Property is stored in ldap 
174      *     'file'       Property is stored in the config file
175      *     'undefined'  Property is currently not stored anywhere
176      *     'modified'   Property has been modified (should be saved)
177      */
178     protected $status = 'undefined';
180     protected $attributes = array('name','type','default','description','check',
181             'migrate','mandatory','group');
183     function __construct($parent,$classname,$data)
184     {
185         // Set some basic infos 
186         $this->parent = &$parent;
187         $this->class = $classname;
188         $this->data  = $data;
190         // Get all relevant information from the data array (comes from plInfo)    
191         foreach($this->attributes as $aName){
192             if(isset($data[$aName])){
193                 $this->$aName = $data[$aName];
194             }
195         }       
197         // First check for values in the LDAP Database.
198         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
199             $this->setStatus('ldap');
200             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
201         }
203         // Second check for values in the config file.
204         if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
205             $this->setStatus('file');
206             $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
207         }
209         // If there still wasn't found anything then fallback to the default.
210         if($this->getStatus() == 'undefined'){
211             $this->value = $this->getDefault();
212         }
213     }
215     function getValue() { return($this->value); }
216     function getName() { return($this->name); }
217     function getClass() { return($this->class); }
218     function getGroup() { return($this->group); }
219     function getType() { return($this->type); }
220     function getDescription() { return($this->description); }
221     function getDefault() { return($this->default); }
222     function getStatus() { return($this->status); }
224     function setValue($str) 
225     {
226         $this->setStatus('modified'); 
227         $this->value = $str; 
228     }
230     function save()
231     {
232         if($this->getStatus() == 'modified'){
234             $ldap = $this->parent->config->get_ldap_link();
235             $ldap->cd($this->parent->config->current['BASE']);
236             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
237             $ldap->cat($dn);
238             if(!$ldap->count()){
239                 $ldap->cd($dn);
240                 $data = array(
241                             'cn' => $this->class, 
242                             'objectClass' => array('top','gosaConfig'),
243                             'gosaSetting' => $this->name.":".$this->value);
245                 $ldap->add($data);
246                 if($ldap->success()){
247                     $this->status = 'ldap';
248                 }else{
249                     echo $ldap->get_error();
250                 }
251             }else{
252                 $attrs = $ldap->fetch();
253                 $data = array();
254                 $found = false;
255                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
256                     $set = $attrs['gosaSetting'][$i];
257                     if(preg_match("/^{$this->name}:/", $set)){
258                         $set = "{$this->name}:{$this->value}";
259                         $found = true;
260                     }
261                     $data['gosaSetting'][] = $set;
262                 }
263                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
264                 $ldap->cd($dn);
265                 $ldap->modify($data);
266                 if($ldap->success()){
267                     $this->status = 'ldap';
268                 }else{
269                     echo $ldap->get_error();
270                 }
271             } 
272         }
273     }
275     private function setStatus($state) 
276     {
277         if(!in_array($state, array('ldap','file','undefined','modified'))) {
278             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
279         }else{
280             $this->status = $state; 
281         }
282     }
284     function isValid() 
285     { 
286         return(TRUE);    
287     }
290 ?>