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){
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(count($def)){
91 $name = $cname;
92 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
93 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
95 $this->classToName[$cname] = $name;
96 $data = array('name' => 'postcreate','type' => 'command');
97 $this->register($cname, $data);
98 $data = array('name' => 'postremove','type' => 'command');
99 $this->register($cname, $data);
100 $data = array('name' => 'postmodify','type' => 'command');
101 $this->register($cname, $data);
102 $data = array('name' => 'check', 'type' => 'command');
103 $this->register($cname, $data);
104 }
106 if(isset($def['plProperties'])){
107 foreach($def['plProperties'] as $property){
108 $this->register($cname, $property);
109 }
110 }
111 }
112 }
113 }
115 function register($class,$data)
116 {
117 $id = count($this->properties);
118 $this->properties[$id] = new gosaProperty($this,$class,$data);
119 $p = strtolower("{$class}::{$data['name']}");
120 $this->mapByName[$p] = $id;
121 }
123 public function getAllProperties()
124 {
125 return($this->properties);
126 }
128 function propertyExists($class,$name)
129 {
130 $p = strtolower("{$class}::{$name}");
131 return(isset($this->mapByName[$p]));
132 }
134 private function getId($class,$name)
135 {
136 $p = strtolower("{$class}::{$name}");
137 if(!isset($this->mapByName[$p])){
138 return(-1);
139 }
140 return($this->mapByName[$p]);
141 }
143 function getProperty($class,$name)
144 {
145 if($this->propertyExists($class,$name)){
146 return($this->properties[$this->getId($class,$name)]);
147 }
148 return(NULL);
149 }
151 function getPropertyValue($class,$name)
152 {
153 if($this->propertyExists($class,$name)){
154 $tmp = $this->getProperty($class,$name);
155 return($tmp->getValue());
156 }
157 return("");
158 }
160 function setPropertyValue($class,$name, $value)
161 {
162 if($this->propertyExists($class,$name)){
163 $tmp = $this->getProperty($class,$name);
164 return($tmp->setValue($value));
165 }
166 return("");
167 }
169 function saveChanges()
170 {
171 foreach($this->properties as $prop){
172 $prop->save();
173 }
174 $this->reload(TRUE);
175 }
176 }
179 class gosaProperty
180 {
181 protected $name = "";
182 protected $class = "";
183 protected $value = "";
184 protected $tmp_value = ""; // Used when modified but not saved
185 protected $type = "string";
186 protected $default = "";
187 protected $defaults = "";
188 protected $description = "";
189 protected $check = "";
190 protected $migrate = "";
191 protected $mandatory = FALSE;
192 protected $group = "default";
193 protected $parent = NULL;
194 protected $data = array();
196 /*! The current property status
197 * 'ldap' Property is stored in ldap
198 * 'file' Property is stored in the config file
199 * 'undefined' Property is currently not stored anywhere
200 * 'modified' Property has been modified (should be saved)
201 */
202 protected $status = 'undefined';
204 protected $attributes = array('name','type','default','description','check',
205 'migrate','mandatory','group','defaults');
207 function __construct($parent,$classname,$data)
208 {
209 // Set some basic infos
210 $this->parent = &$parent;
211 $this->class = $classname;
212 $this->data = $data;
214 // Get all relevant information from the data array (comes from plInfo)
215 foreach($this->attributes as $aName){
216 if(isset($data[$aName])){
217 $this->$aName = $data[$aName];
218 }
219 }
221 // Initialize with the current value
222 $this->_restoreCurrentValue();
223 }
225 function check()
226 {
227 $val = $this->getValue(TRUE);
228 $return = TRUE;
229 if($this->mandatory && empty($val)){
230 msg_dialog::display(_("Error"), msgPool::required(_($this->name)), ERROR_DIALOG);
231 $return = FALSE;
232 }
234 $check = $this->getCheck();
235 if(!empty($val) && !empty($check)){
236 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
237 if(!$res){
238 msg_dialog::display(_("Error"), msgPool::invalid(_($this->name)), ERROR_DIALOG);
239 $return = FALSE;
240 }
241 }
242 return($return);
243 }
245 static function isBool($message,$class,$name,$value, $type)
246 {
247 return(in_array($value,array('true','false','')));
248 }
250 static function isInteger($message,$class,$name,$value, $type)
251 {
252 return(is_numeric($value) && !preg_match("/[^0-9]/", $value));
253 }
255 static function isPath($message,$class,$name,$value, $type)
256 {
257 return(TRUE);
258 }
260 static function isWriteablePath($message,$class,$name,$value, $type)
261 {
262 return(!empty($value)&&is_writeable($value));
263 }
265 static function isReadableFile($message,$class,$name,$value, $type)
266 {
267 return(!empty($value)&&is_readable($value)&&is_file($value));
268 }
270 static function isCommand($message,$class,$name,$value, $type)
271 {
272 return(TRUE);
273 }
275 static function isDn($message,$class,$name,$value, $type)
276 {
277 return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
278 }
280 static function isRdn($message,$class,$name,$value, $type)
281 {
282 return(preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value));
283 }
285 private function _restoreCurrentValue()
286 {
288 // First check for values in the LDAP Database.
289 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
290 $this->setStatus('ldap');
291 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
292 return;
293 }
295 // Second check for values in the config file.
296 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
297 $this->setStatus('file');
298 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
299 return;
300 }
302 // If there still wasn't found anything then fallback to the default.
303 if($this->getStatus() == 'undefined'){
304 $this->value = $this->getDefault();
305 }
306 }
308 function getMigrate() { return($this->migrate); }
309 function getCheck() { return($this->check); }
310 function getName() { return($this->name); }
311 function getClass() { return($this->class); }
312 function getGroup() { return($this->group); }
313 function getType() { return($this->type); }
314 function getDescription() { return($this->description); }
315 function getDefault() { return($this->default); }
316 function getDefaults() { return($this->defaults); }
317 function getStatus() { return($this->status); }
318 function isMandatory() { return($this->mandatory); }
320 function setValue($str)
321 {
322 if(in_array($this->getStatus(), array('modified'))){
323 $this->tmp_value = $str;
324 }elseif($this->value != $str){
325 $this->setStatus('modified');
326 $this->tmp_value = $str;
327 }
328 }
330 function getValue($temporary = FALSE)
331 {
332 if($temporary && in_array($this->getStatus(), array('modified'))){
333 return($this->tmp_value);
334 }else{
335 return($this->value);
336 }
337 }
339 function restoreDefault()
340 {
341 if(in_array($this->getStatus(),array('ldap'))){
342 $this->setStatus('removed');
343 }elseif(in_array($this->getStatus(),array('modified'))){
344 $this->_restoreCurrentValue();
345 }
346 }
348 function save()
349 {
350 if($this->getStatus() == 'modified'){
351 $ldap = $this->parent->config->get_ldap_link();
352 $ldap->cd($this->parent->config->current['BASE']);
353 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
354 $ldap->cat($dn);
355 if(!$ldap->count()){
356 $ldap->cd($dn);
357 $data = array(
358 'cn' => $this->class,
359 'objectClass' => array('top','gosaConfig'),
360 'gosaSetting' => $this->name.":".$this->tmp_value);
362 $ldap->add($data);
363 if(!$ldap->success()){
364 echo $ldap->get_error();
365 }
367 }else{
368 $attrs = $ldap->fetch();
369 $data = array();
370 $found = false;
371 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
372 $set = $attrs['gosaSetting'][$i];
373 if(preg_match("/^{$this->name}:/", $set)){
374 $set = "{$this->name}:{$this->tmp_value}";
375 $found = true;
376 }
377 $data['gosaSetting'][] = $set;
378 }
379 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
380 $ldap->cd($dn);
381 $ldap->modify($data);
382 if(!$ldap->success()){
383 echo $ldap->get_error();
384 }
385 }
386 $this->_restoreCurrentValue();
387 }elseif($this->getStatus() == 'removed'){
388 $ldap = $this->parent->config->get_ldap_link();
389 $ldap->cd($this->parent->config->current['BASE']);
390 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
391 $ldap->cat($dn);
392 $attrs = $ldap->fetch();
393 $data = array('gosaSetting' => array());
394 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
395 $set = $attrs['gosaSetting'][$i];
396 if(preg_match("/^{$this->name}:/", $set)){
397 continue;
398 }
399 $data['gosaSetting'][] = $set;
400 }
401 $ldap->cd($dn);
402 $ldap->modify($data);
403 if(!$ldap->success()){
404 echo $ldap->get_error();
405 }
406 $this->_restoreCurrentValue();
407 }
408 }
410 private function setStatus($state)
411 {
412 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
413 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
414 }else{
415 $this->status = $state;
416 }
417 }
419 function isValid()
420 {
421 return(TRUE);
422 }
423 }
425 ?>