Code

removed defaults from get_cfg_value and added some missing properties.
[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         $this->config = &$config;
16         $this->reload();
17     }
19     function reload($force = FALSE)
20     {
21         // Do not reload the properties everytime, once we have  
22         //  everything loaded and registrered skip the reload.
23         // Status is 'finished' once we had a ldap connection (logged in)
24         if(!$force && $this->status == 'finished') return;
26         // Reset everything
27         $this->ldapStoredProperties = array();
28         $this->fileStoredProperties = array();
29         $this->properties = array();
30         $this->mapByName = array();
32         // Search for config flags defined in the config file (TAB section)
33         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
34             foreach($tabdefs as $info){
36                 // Check if the info is valid
37                 if(isset($info['NAME']) && isset($info['CLASS'])){
39                     // Check if there is nore than just the plugin definition
40                     if(count($info) > 2){
41                         foreach($info as $name => $value){
42                             
43                             if(!in_array($name, array('CLASS','NAME'))){
44                                 $class= $info['CLASS'];    
45                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
46                             }
47                         }
48                     }
49                 }
50             }
51         }
53         // Search for config flags defined in the config file (MAIN section)
54         foreach($this->config->data['MAIN'] as $name => $value){
55             $this->fileStoredProperties['core'][strtolower($name)] = $value;
56         }
58         // Search for config flags defined in the config file (Current LOCATION section)
59         if(isset($this->config->current)){
60             foreach($this->config->current as $name => $value){
61                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
62             }
63         }
65         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
66         if(!empty($this->config->current['CONFIG'])){
67             $ldap = $this->config->get_ldap_link();
68             $ldap->cd($this->config->current['CONFIG']);
69             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
70             while($attrs = $ldap->fetch()){
71                 $class = $attrs['cn'][0];
72                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
73                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
74                     $this->ldapStoredProperties[$class][$name] = $value;
75                 }
76             }
77             $this->status = 'finished';
78         }
80         global $class_mapping;
81         foreach ($class_mapping as $cname => $path){
82             $cmethods = get_class_methods($cname);
83             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
85                 // Get plugin definitions
86                 $def = call_user_func(array($cname, 'plInfo'));;
88                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
89                 if(isset($def['plShortName'])){
90                     $this->classToName[$cname] = $def['plShortName'];
91                     $data = array('name' => 'postcreate','type' => 'command');
92                     $this->register($cname, $data);    
93                     $data = array('name' => 'postremove','type' => 'command');
94                     $this->register($cname, $data);    
95                     $data = array('name' => 'postmodify','type' => 'command');
96                     $this->register($cname, $data);    
97                     $data = array('name' => 'check', 'type' => 'command');
98                     $this->register($cname, $data);    
99                 }
101                 if(isset($def['plProperties'])){
102                     foreach($def['plProperties'] as $property){
103                         $this->register($cname, $property);
104                     }
105                 }
106             }
107         }
108     }
110     function register($class,$data)
111     {
112         $id = count($this->properties);
113         $this->properties[$id] = new gosaProperty($this,$class,$data);
114         $p = strtolower("{$class}::{$data['name']}");
115         $this->mapByName[$p] = $id;
116     }
118     public function getAllProperties()
119     {
120         return($this->properties);
121     }
123     function propertyExists($class,$name)
124     {       
125         $p = strtolower("{$class}::{$name}");
126         return(isset($this->mapByName[$p]));
127     }
129     private function getId($class,$name)
130     {
131         $p = strtolower("{$class}::{$name}");
132         if(!isset($this->mapByName[$p])){
133             return(-1);
134         }       
135         return($this->mapByName[$p]);    
136     }
138     function getProperty($class,$name)
139     {
140         if($this->propertyExists($class,$name)){
141             return($this->properties[$this->getId($class,$name)]);
142         }
143         return(NULL); 
144     }
146     function getPropertyValue($class,$name)
147     {   
148         if($this->propertyExists($class,$name)){
149             $tmp = $this->getProperty($class,$name);
150             return($tmp->getValue());
151         }
152         return("");
153     }
155     function setPropertyValue($class,$name, $value)
156     {   
157         if($this->propertyExists($class,$name)){
158             $tmp = $this->getProperty($class,$name);
159             return($tmp->setValue($value));
160         }
161         return("");
162     }
164     function saveChanges()
165     {
166         foreach($this->properties as $prop){
167             $prop->save();
168         }
169         $this->reload(TRUE);
170     }
174 class gosaProperty
176     protected $name = "";
177     protected $class = "";
178     protected $value = "";
179     protected $tmp_value = "";  // Used when modified but not saved 
180     protected $type = "string";
181     protected $default = "";
182     protected $defaults = "";
183     protected $description = "";
184     protected $check = "";
185     protected $migrate = "";
186     protected $mandatory = FALSE;
187     protected $group = "default";
188     protected $parent = NULL;
189     protected $data = array();
191     /*!  The current property status
192      *     'ldap'       Property is stored in ldap 
193      *     'file'       Property is stored in the config file
194      *     'undefined'  Property is currently not stored anywhere
195      *     'modified'   Property has been modified (should be saved)
196      */
197     protected $status = 'undefined';
199     protected $attributes = array('name','type','default','description','check',
200             'migrate','mandatory','group','defaults');
202     function __construct($parent,$classname,$data)
203     {
204         // Set some basic infos 
205         $this->parent = &$parent;
206         $this->class = $classname;
207         $this->data  = $data;
209         // Get all relevant information from the data array (comes from plInfo)    
210         foreach($this->attributes as $aName){
211             if(isset($data[$aName])){
212                 $this->$aName = $data[$aName];
213             }
214         }      
216         // Initialize with the current value
217         $this->_restoreCurrentValue(); 
218     }
220     function check()
221     {
222         $val = $this->getValue(TRUE);
223         $return = TRUE;
224         if($this->mandatory && empty($val)){
225             msg_dialog::display(_("Error"), msgPool::required(_($this->name)), ERROR_DIALOG);
226             $return = FALSE;
227         }
229         $check = $this->getCheck();
230         if(!empty($val) && !empty($check)){
231             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
232             if(!$res){
233                 msg_dialog::display(_("Error"), msgPool::invalid(_($this->name)), ERROR_DIALOG);
234                 $return = FALSE;
235             }
236         }
237         return($return);
238     }
240     static function isBool($message,$class,$name,$value, $type)
241     {
242         return(in_array($value,array('true','false','')));
243     }
245     static function isInteger($message,$class,$name,$value, $type)
246     {
247         return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
248     }
250     static function isPath($message,$class,$name,$value, $type)
251     {
252         return(TRUE);
253     }
255     static function isWriteablePath($message,$class,$name,$value, $type)
256     {
257         return(!empty($value)&&is_writeable($value));
258     }
260     static function isReadableFile($message,$class,$name,$value, $type)
261     {
262         return(!empty($value)&&is_readable($value)&&is_file($value));
263     }
265     static function isCommand($message,$class,$name,$value, $type)
266     {
267         return(TRUE);
268     }
270     static function isDn($message,$class,$name,$value, $type)
271     {
272         return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
273     }
275     static function isRdn($message,$class,$name,$value, $type)
276     {
277         return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
278     }
280     private function _restoreCurrentValue()
281     {
282     
283         // First check for values in the LDAP Database.
284         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
285             $this->setStatus('ldap');
286             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
287             return;
288         }
290         // Second check for values in the config file.
291         if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
292             $this->setStatus('file');
293             $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
294             return;
295         }
297         // If there still wasn't found anything then fallback to the default.
298         if($this->getStatus() == 'undefined'){
299             $this->value = $this->getDefault();
300         }
301     }
303     function getMigrate() { return($this->migrate); }
304     function getCheck() { return($this->check); }
305     function getName() { return($this->name); }
306     function getClass() { return($this->class); }
307     function getGroup() { return($this->group); }
308     function getType() { return($this->type); }
309     function getDescription() { return($this->description); }
310     function getDefault() { return($this->default); }
311     function getDefaults() { return($this->defaults); }
312     function getStatus() { return($this->status); }
313     function isMandatory() { return($this->mandatory); }
315     function setValue($str) 
316     {
317         if(in_array($this->getStatus(), array('modified'))){
318             $this->tmp_value = $str; 
319         }elseif($this->value != $str){
320             $this->setStatus('modified'); 
321             $this->tmp_value = $str; 
322         }
323     }
325     function getValue($temporary = FALSE) 
326     { 
327         if($temporary && in_array($this->getStatus(), array('modified'))){
328             return($this->tmp_value); 
329         }else{ 
330             return($this->value); 
331         }
332     }
334     function restoreDefault() 
335     {
336         if(in_array($this->getStatus(),array('ldap'))){
337             $this->setStatus('removed'); 
338         }elseif(in_array($this->getStatus(),array('modified'))){
339             $this->_restoreCurrentValue();
340         }
341     }
343     function save()
344     {
345         if($this->getStatus() == 'modified'){
346             $ldap = $this->parent->config->get_ldap_link();
347             $ldap->cd($this->parent->config->current['BASE']);
348             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
349             $ldap->cat($dn);
350             if(!$ldap->count()){
351                 $ldap->cd($dn);
352                 $data = array(
353                         'cn' => $this->class, 
354                         'objectClass' => array('top','gosaConfig'),
355                         'gosaSetting' => $this->name.":".$this->tmp_value);
357                 $ldap->add($data);
358                 if(!$ldap->success()){
359                     echo $ldap->get_error();
360                 }
362             }else{
363                 $attrs = $ldap->fetch();
364                 $data = array();
365                 $found = false;
366                 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
367                     $set = $attrs['gosaSetting'][$i];
368                     if(preg_match("/^{$this->name}:/", $set)){
369                         $set = "{$this->name}:{$this->tmp_value}";
370                         $found = true;
371                     }
372                     $data['gosaSetting'][] = $set;
373                 }
374                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
375                 $ldap->cd($dn);
376                 $ldap->modify($data);
377                 if(!$ldap->success()){
378                     echo $ldap->get_error();
379                 }
380             } 
381             $this->_restoreCurrentValue();
382         }elseif($this->getStatus() == 'removed'){
383             $ldap = $this->parent->config->get_ldap_link();
384             $ldap->cd($this->parent->config->current['BASE']);
385             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
386             $ldap->cat($dn);
387             $attrs = $ldap->fetch();
388             $data = array('gosaSetting' => array());
389             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
390                 $set = $attrs['gosaSetting'][$i];
391                 if(preg_match("/^{$this->name}:/", $set)){
392                     continue;
393                 }
394                 $data['gosaSetting'][] = $set;
395             }
396             $ldap->cd($dn);
397             $ldap->modify($data);
398             if(!$ldap->success()){
399                 echo $ldap->get_error();
400             }
401             $this->_restoreCurrentValue();
402         }
403     }
405     private function setStatus($state) 
406     {
407         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
408             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
409         }else{
410             $this->status = $state; 
411         }
412     }
414     function isValid() 
415     { 
416         return(TRUE);    
417     }
420 ?>