Code

efc85f942dccd14bd3119b7599817462dff74f72
[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     function propertyExists($class,$name)
115     {
116         return(isset($this->mapByName[$class][$name]));
117     }
119     private function getId($class,$name)
120     {
121         return($this->mapByName[$class][$name]);    
122     }
124     function getProperty($class,$name)
125     {
126         if($this->propertyExists($class,$name)){
127             return($this->properties[$this->getId($class,$name)]);
128         }
129         return(NULL); 
130     }
132     function getPropertyValue($class,$name)
133     {   
134         if($this->propertyExists($class,$name)){
135             $tmp = $this->getProperty($class,$name);
136             return($tmp->getValue());
137         }
138         return("");
139     }
141     function setPropertyValue($class,$name, $value)
142     {   
143         if($this->propertyExists($class,$name)){
144             $tmp = $this->getProperty($class,$name);
145             return($tmp->setValue($value));
146         }
147         return("");
148     }
152 class gosaProperty
154     protected $name = "";
155     protected $class = "";
156     protected $value = "";
157     protected $type = "string";
158     protected $default = "";
159     protected $description = "";
160     protected $check = "";
161     protected $migrate = "";
162     protected $mandatory = FALSE;
163     protected $parent = NULL;
164     protected $data = array();
166     /*!  The current property status
167      *     'ldap'       Property is stored in ldap 
168      *     'file'       Property is stored in the config file
169      *     'undefined'  Property is currently not stored anywhere
170      *     'modified'   Property has been modified (should be saved)
171      */
172     protected $status = 'undefined';
174     protected $attributes = array('name','type','default','description','check',
175             'migrate','mandatory');
177     function __construct($parent,$classname,$data)
178     {
179         // Set some basic infos 
180         $this->parent = &$parent;
181         $this->class = $classname;
182         $this->data  = $data;
184         // Get all relevant information from the data array (comes from plInfo)    
185         foreach($this->attributes as $aName){
186             if(isset($data[$aName])){
187                 $this->$aName = $data[$aName];
188             }
189         }       
191         // First check for values in the LDAP Database.
192         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
193             $this->setStatus('ldap');
194             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
195         }
197         // Second check for values in the config file.
198         if(isset($this->parent->fileStoredProperties[$this->class][$this->name])){
199             $this->setStatus('file');
200             $this->value = $this->parent->fileStoredProperties[$this->class][$this->name];
201         }
203         // If there still wasn't found anything then fallback to the default.
204         if($this->getStatus() == 'undefined'){
205             $this->value = $this->getDefault();
206         }
207     }
209     function getValue() { return($this->value); }
210     function getName() { return($this->name); }
211     function getType() { return($this->type); }
212     function getDescription() { return($this->description); }
213     function getDefault() { return($this->default); }
214     function getStatus() { return($this->status); }
216     function setValue($str) 
217     {
218         $this->setStatus('modified'); 
219         $this->value = $str; 
220     }
222     function save()
223     {
224         if($this->getStatus() == 'modified'){
226             $ldap = $this->parent->config->get_ldap_link();
227             $ldap->cd($this->parent->config->current['BASE']);
228             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
229             $ldap->cat($dn);
230             if(!$ldap->count()){
231                 $ldap->cd($dn);
232                 $data = array(
233                             'cn' => $this->class, 
234                             'objectClass' => array('top','gosaConfig'),
235                             'gosaSetting' => $this->name.":".$this->value);
237                 $ldap->add($data);
238                 if($ldap->success()){
239                     $this->status = 'ldap';
240                 }else{
241                     echo $ldap->get_error();
242                 }
243             }else{
244                 $attrs = $ldap->fetch();
245                 $data = array();
246                 $found = false;
247                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
248                     $set = $attrs['gosaSetting'][$i];
249                     if(preg_match("/^{$this->name}:/", $set)){
250                         $set = "{$this->name}:{$this->value}";
251                         $found = true;
252                     }
253                     $data['gosaSetting'][] = $set;
254                 }
255                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->value}";
256                 $ldap->cd($dn);
257                 $ldap->modify($data);
258                 if($ldap->success()){
259                     $this->status = 'ldap';
260                 }else{
261                     echo $ldap->get_error();
262                 }
263             } 
264         }
265     }
267     private function setStatus($state) 
268     {
269         if(!in_array($state, array('ldap','file','undefined','modified'))) {
270             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
271         }else{
272             $this->status = $state; 
273         }
274     }
276     function isValid() 
277     { 
278         return(TRUE);    
279     }
282 ?>