Code

Updated property editor.
[gosa.git] / gosa-core / include / class_configRegistry.inc
1 <?php
3 class configRegistry{
5     public $config = NULL;
6     public $properties = array();
7     public $ldapStoredProperties = array(); 
8     public $fileStoredProperties = array(); 
9     public $classToName = array(); 
11     public $status = 'none';
13     function __construct($config)
14     {
15         restore_error_handler();
16         $this->config = &$config;
17         $this->reload();
18     }
20     function reload($force = FALSE)
21     {
22         // Do not reload the properties everytime, once we have  
23         //  everything loaded and registrered skip the reload.
24         // Status is 'finished' once we had a ldap connection (logged in)
25         if(!$force && $this->status == 'finished') return;
27         // Reset everything
28         $this->ldapStoredProperties = array();
29         $this->fileStoredProperties = array();
30         $this->properties = array();
31         $this->mapByName = array();
33         // Search for config flags defined in the config file (TAB section)
34         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
35             foreach($tabdefs as $info){
37                 // Check if the info is valid
38                 if(isset($info['NAME']) && isset($info['CLASS'])){
40                     // Check if there is nore than just the plugin definition
41                     if(count($info) > 2){
42                         foreach($info as $name => $value){
43                             
44                             if(!in_array($name, array('CLASS','NAME'))){
45                                 $class= $info['CLASS'];    
46                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
47                             }
48                         }
49                     }
50                 }
51             }
52         }
54         // Search for config flags defined in the config file (MAIN section)
55         foreach($this->config->data['MAIN'] as $name => $value){
56             $this->fileStoredProperties['core'][strtolower($name)] = $value;
57         }
59         // Search for config flags defined in the config file (Current LOCATION section)
60         if(isset($this->config->current)){
61             foreach($this->config->current as $name => $value){
62                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
63             }
64         }
66         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
67         if(!empty($this->config->current['CONFIG'])){
68             $ldap = $this->config->get_ldap_link();
69             $ldap->cd($this->config->current['CONFIG']);
70             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
71             while($attrs = $ldap->fetch()){
72                 $class = $attrs['cn'][0];
73                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
74                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
75                     $this->ldapStoredProperties[$class][$name] = $value;
76                 }
77             }
78             $this->status = 'finished';
79         }
81         global $class_mapping;
82         foreach ($class_mapping as $cname => $path){
83             $cmethods = get_class_methods($cname);
84             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
86                 // Get plugin definitions
87                 $def = call_user_func(array($cname, 'plInfo'));;
89                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
90                 if(isset($def['plShortName'])){
91                     $this->classToName[$cname] = $def['plShortName'];
92                     $data = array('name' => 'postcreate','type' => 'string');
93                     $this->register($cname, $data);    
94                     $data = array('name' => 'postremove','type' => 'string');
95                     $this->register($cname, $data);    
96                     $data = array('name' => 'postmodify','type' => 'string');
97                     $this->register($cname, $data);    
98                     $data = array('name' => 'checkhook', 'type' => 'string');
99                     $this->register($cname, $data);    
100                 }
102                 if(isset($def['plProperties'])){
103                     foreach($def['plProperties'] as $property){
104                         $this->register($cname, $property);
105                     }
106                 }
107             }
108         }
109     }
111     function register($class,$data)
112     {
113         $id = count($this->properties);
114         $this->properties[$id] = new gosaProperty($this,$class,$data);
115         $p = strtolower("{$class}::{$data['name']}");
116         $this->mapByName[$p] = $id;
117     }
119     public function getAllProperties()
120     {
121         return($this->properties);
122     }
124     function propertyExists($class,$name)
125     {       
126         $p = strtolower("{$class}::{$name}");
127         return(isset($this->mapByName[$p]));
128     }
130     private function getId($class,$name)
131     {
132         $p = strtolower("{$class}::{$name}");
133         if(!isset($this->mapByName[$p])){
134             return(-1);
135         }       
136         return($this->mapByName[$p]);    
137     }
139     function getProperty($class,$name)
140     {
141         if($this->propertyExists($class,$name)){
142             return($this->properties[$this->getId($class,$name)]);
143         }
144         return(NULL); 
145     }
147     function getPropertyValue($class,$name)
148     {   
149         if($this->propertyExists($class,$name)){
150             $tmp = $this->getProperty($class,$name);
151             return($tmp->getValue());
152         }
153         return("");
154     }
156     function setPropertyValue($class,$name, $value)
157     {   
158         if($this->propertyExists($class,$name)){
159             $tmp = $this->getProperty($class,$name);
160             return($tmp->setValue($value));
161         }
162         return("");
163     }
165     function saveChanges()
166     {
167         foreach($this->properties as $prop){
168             $prop->save();
169         }
170         $this->reload(TRUE);
171     }
175 class gosaProperty
177     protected $name = "";
178     protected $class = "";
179     protected $value = "";
180     protected $tmp_value = "";  // Used when modified but not saved 
181     protected $type = "string";
182     protected $default = "";
183     protected $defaults = "";
184     protected $description = "";
185     protected $check = "";
186     protected $migrate = "";
187     protected $mandatory = FALSE;
188     protected $group = "default";
189     protected $parent = NULL;
190     protected $data = array();
192     /*!  The current property status
193      *     'ldap'       Property is stored in ldap 
194      *     'file'       Property is stored in the config file
195      *     'undefined'  Property is currently not stored anywhere
196      *     'modified'   Property has been modified (should be saved)
197      */
198     protected $status = 'undefined';
200     protected $attributes = array('name','type','default','description','check',
201             'migrate','mandatory','group','defaults');
203     function __construct($parent,$classname,$data)
204     {
205         // Set some basic infos 
206         $this->parent = &$parent;
207         $this->class = $classname;
208         $this->data  = $data;
210         // Get all relevant information from the data array (comes from plInfo)    
211         foreach($this->attributes as $aName){
212             if(isset($data[$aName])){
213                 $this->$aName = $data[$aName];
214             }
215         }      
217         // Initialize with the current value
218         $this->_restoreCurrentValue(); 
219     }
221     static function isBool($message,$class,$name,$value, $type)
222     {
223         return(in_array($value,array('true','false','')));
224     }
226     static function isInteger($message,$class,$name,$value, $type)
227     {
228         return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
229     }
231     static function isPath($message,$class,$name,$value, $type)
232     {
233         return(TRUE);
234     }
236     static function isWriteablePath($message,$class,$name,$value, $type)
237     {
238         return(!empty($value)&&is_writeable($value));
239     }
241     static function isCommand($message,$class,$name,$value, $type)
242     {
243         return(TRUE);
244     }
246     static function isDn($message,$class,$name,$value, $type)
247     {
248         return(TRUE);
249     }
251     static function isRdn($message,$class,$name,$value, $type)
252     {
253         return(TRUE);
254     }
256     private function _restoreCurrentValue()
257     {
258     
259         // First check for values in the LDAP Database.
260         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
261             $this->setStatus('ldap');
262             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
263             return;
264         }
266         // Second check for values in the config file.
267         if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
268             $this->setStatus('file');
269             $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
270             return;
271         }
273         // If there still wasn't found anything then fallback to the default.
274         if($this->getStatus() == 'undefined'){
275             $this->value = $this->getDefault();
276         }
277     }
279     function getMigrate() { return($this->migrate); }
280     function getCheck() { return($this->check); }
281     function getName() { return($this->name); }
282     function getClass() { return($this->class); }
283     function getGroup() { return($this->group); }
284     function getType() { return($this->type); }
285     function getDescription() { return($this->description); }
286     function getDefault() { return($this->default); }
287     function getDefaults() { return($this->defaults); }
288     function getStatus() { return($this->status); }
289     function isMandatory() { return($this->mandatory); }
291     function setValue($str) 
292     {
293         if($this->value != $str){
294             $this->setStatus('modified'); 
295             $this->tmp_value = $str; 
296         }
297     }
299     function getValue($temporary = FALSE) 
300     { 
301         if($temporary && in_array($this->getStatus(), array('modified'))){
302             return($this->tmp_value); 
303         }else{ 
304             return($this->value); 
305         }
306     }
308     function restoreDefault() 
309     {
310         if(in_array($this->getStatus(),array('ldap'))){
311             $this->setStatus('removed'); 
312         }elseif(in_array($this->getStatus(),array('modified'))){
313             $this->_restoreCurrentValue();
314         }
315     }
317     function save()
318     {
319         if($this->getStatus() == 'modified'){
320             $ldap = $this->parent->config->get_ldap_link();
321             $ldap->cd($this->parent->config->current['BASE']);
322             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
323             $ldap->cat($dn);
324             if(!$ldap->count()){
325                 $ldap->cd($dn);
326                 $data = array(
327                         'cn' => $this->class, 
328                         'objectClass' => array('top','gosaConfig'),
329                         'gosaSetting' => $this->name.":".$this->tmp_value);
331                 $ldap->add($data);
332                 if(!$ldap->success()){
333                     echo $ldap->get_error();
334                 }
336             }else{
337                 $attrs = $ldap->fetch();
338                 $data = array();
339                 $found = false;
340                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
341                     $set = $attrs['gosaSetting'][$i];
342                     if(preg_match("/^{$this->name}:/", $set)){
343                         $set = "{$this->name}:{$this->tmp_value}";
344                         $found = true;
345                     }
346                     $data['gosaSetting'][] = $set;
347                 }
348                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
349                 $ldap->cd($dn);
350                 $ldap->modify($data);
351                 if(!$ldap->success()){
352                     echo $ldap->get_error();
353                 }
354             } 
355             $this->_restoreCurrentValue();
356         }elseif($this->getStatus() == 'removed'){
357             $ldap = $this->parent->config->get_ldap_link();
358             $ldap->cd($this->parent->config->current['BASE']);
359             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
360             $ldap->cat($dn);
361             $attrs = $ldap->fetch();
362             $data = array('gosaSetting' => array());
363             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
364                 $set = $attrs['gosaSetting'][$i];
365                 if(preg_match("/^{$this->name}:/", $set)){
366                     continue;
367                 }
368                 $data['gosaSetting'][] = $set;
369             }
370             $ldap->cd($dn);
371             $ldap->modify($data);
372             if(!$ldap->success()){
373                 echo $ldap->get_error();
374             }
375             $this->_restoreCurrentValue();
376         }
377     }
379     private function setStatus($state) 
380     {
381         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
382             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
383         }else{
384             $this->status = $state; 
385         }
386     }
388     function isValid() 
389     { 
390         return(TRUE);    
391     }
394 ?>