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 $migrate = array();
172 foreach($this->properties as $prop){
174 // Is this property modified
175 if(in_array($prop->getStatus(),array('modified','removed'))){
177 // Check if we've to migrate something before we can make the changes effective.
178 if($prop->migrationRequired()){
179 $migrate[] = $prop;
180 }else{
181 $prop->save();
182 }
183 }
184 }
185 return($migrate);
186 }
187 }
190 class gosaProperty
191 {
192 protected $name = "";
193 protected $class = "";
194 protected $value = "";
195 protected $tmp_value = ""; // Used when modified but not saved
196 protected $type = "string";
197 protected $default = "";
198 protected $defaults = "";
199 protected $description = "";
200 protected $check = "";
201 protected $migrate = "";
202 protected $mandatory = FALSE;
203 protected $group = "default";
204 protected $parent = NULL;
205 protected $data = array();
207 protected $migrationClass = NULL;
209 /*! The current property status
210 * 'ldap' Property is stored in ldap
211 * 'file' Property is stored in the config file
212 * 'undefined' Property is currently not stored anywhere
213 * 'modified' Property has been modified (should be saved)
214 */
215 protected $status = 'undefined';
217 protected $attributes = array('name','type','default','description','check',
218 'migrate','mandatory','group','defaults');
223 function __construct($parent,$classname,$data)
224 {
225 // Set some basic infos
226 $this->parent = &$parent;
227 $this->class = $classname;
228 $this->data = $data;
230 // Get all relevant information from the data array (comes from plInfo)
231 foreach($this->attributes as $aName){
232 if(isset($data[$aName])){
233 $this->$aName = $data[$aName];
234 }
235 }
237 // Initialize with the current value
238 $this->_restoreCurrentValue();
240 // Instantiate migration class
241 if(!empty($this->migrate)){
242 if(!class_available($this->migrate)){
243 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
244 }else{
245 $class = $this->migrate;
246 $tmp = new $class($this->parent->config,$this);
247 if(! $tmp instanceof propertyMigration){
248 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
249 }
250 $this->migrationClass = $tmp;
251 }
252 }
253 }
255 function migrationRequired()
256 {
257 if(empty($this->migrate) || $this->migrationClass == NULL){
258 return(FALSE);
259 }
260 return($this->migrationClass->checkForIssues());
261 }
263 function getMigrationClass()
264 {
265 return($this->migrationClass);
266 }
268 function check()
269 {
270 $val = $this->getValue(TRUE);
271 $return = TRUE;
272 if($this->mandatory && empty($val)){
273 $return = FALSE;
274 }
276 $check = $this->getCheck();
277 if(!empty($val) && !empty($check)){
278 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
279 if(!$res){
280 $return = FALSE;
281 }
282 }
283 return($return);
284 }
286 static function isBool($message,$class,$name,$value, $type)
287 {
288 $match = in_array($value,array('true','false',''));
290 // Display the reason for failing this check.
291 if($message && ! $match){
292 msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,"",_("Use 'true', 'false' or empty if allowed")), WARNING_DIALOG);
293 }
295 return($match);
296 }
298 static function isString($message,$class,$name,$value, $type)
299 {
300 $match = TRUE;
302 // Display the reason for failing this check.
303 if($message && ! $match){
304 msg_dialog::display(_("Warning"), msgPool::invalid($name), WARNING_DIALOG);
305 }
307 return($match);
308 }
310 static function isInteger($message,$class,$name,$value, $type)
311 {
312 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
314 // Display the reason for failing this check.
315 if($message && ! $match){
316 msg_dialog::display(_("Warning"), msgPool::invalid($name, $value,'/[0-9]/'), WARNING_DIALOG);
317 }
319 return($match);
320 }
322 static function isPath($message,$class,$name,$value, $type)
323 {
324 $match = TRUE;
326 // Display the reason for failing this check.
327 if($message && ! $match){
328 msg_dialog::display(_("Warning"), msgPool::invalid($name), WARNING_DIALOG);
329 }
331 return($match);
332 }
334 static function isWriteablePath($message,$class,$name,$value, $type)
335 {
336 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
338 // Display the reason for failing this check.
339 if($message && ! $match){
341 if(!is_dir($value)){
342 msg_dialog::display(_("Warning"), sprintf(_("The specified folder does not exists '%s'."), $value), WARNING_DIALOG);
343 }elseif(!is_writeable($value)){
344 msg_dialog::display(_("Warning"), sprintf(_("The specified folder cannot be used for writing '%s'."), $value), WARNING_DIALOG);
345 }
346 }
348 return($match);
349 }
351 static function isReadableFile($message,$class,$name,$value, $type)
352 {
353 $match = !empty($value) && is_readable($value) && is_file($value);
355 // Display the reason for failing this check.
356 if($message && ! $match){
358 if(!is_file($value)){
359 msg_dialog::display(_("Warning"), sprintf(_("The specified file does not exists '%s'."), $value), WARNING_DIALOG);
360 }elseif(!is_readable($value)){
361 msg_dialog::display(_("Warning"), sprintf(_("The specified file cannot be used for reading '%s'."), $value), WARNING_DIALOG);
362 }
363 }
365 return($match);
366 }
368 static function isCommand($message,$class,$name,$value, $type)
369 {
370 $match = TRUE;
372 // Display the reason for failing this check.
373 if($message && ! $match){
374 msg_dialog::display(_("Warning"), msgPool::cmdinvalid($name,$value), WARNING_DIALOG);
375 }
377 return($match);
378 }
380 static function isDn($message,$class,$name,$value, $type)
381 {
382 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
384 // Display the reason for failing this check.
385 if($message && ! $match){
386 msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,'','cn=user,ou=people,dc=example,dc=de'), WARNING_DIALOG);
387 }
389 return($match);
390 }
392 static function isRdn($message,$class,$name,$value, $type)
393 {
394 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*,$/i", $value);
396 // Display the reason for failing this check.
397 if($message && ! $match){
398 msg_dialog::display(_("Warning"), msgPool::invalid($name,$value,'','ou=people,'), WARNING_DIALOG);
399 }
401 return($match);
402 }
404 private function _restoreCurrentValue()
405 {
407 // First check for values in the LDAP Database.
408 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
409 $this->setStatus('ldap');
410 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
411 return;
412 }
414 // Second check for values in the config file.
415 if(isset($this->parent->fileStoredProperties[$this->class][strtolower($this->name)])){
416 $this->setStatus('file');
417 $this->value = $this->parent->fileStoredProperties[$this->class][strtolower($this->name)];
418 return;
419 }
421 // If there still wasn't found anything then fallback to the default.
422 if($this->getStatus() == 'undefined'){
423 $this->value = $this->getDefault();
424 }
425 }
427 function getMigrate() { return($this->migrate); }
428 function getCheck() { return($this->check); }
429 function getName() { return($this->name); }
430 function getClass() { return($this->class); }
431 function getGroup() { return($this->group); }
432 function getType() { return($this->type); }
433 function getDescription() { return($this->description); }
434 function getDefault() { return($this->default); }
435 function getDefaults() { return($this->defaults); }
436 function getStatus() { return($this->status); }
437 function isMandatory() { return($this->mandatory); }
439 function setValue($str)
440 {
441 if(in_array($this->getStatus(), array('modified'))){
442 $this->tmp_value = $str;
443 }elseif($this->value != $str){
444 $this->setStatus('modified');
445 $this->tmp_value = $str;
446 }
447 }
449 function getValue($temporary = FALSE)
450 {
451 if($temporary && in_array($this->getStatus(), array('modified'))){
452 return($this->tmp_value);
453 }else{
454 return($this->value);
455 }
456 }
458 function restoreDefault()
459 {
460 if(in_array($this->getStatus(),array('ldap'))){
461 $this->setStatus('removed');
462 }elseif(in_array($this->getStatus(),array('modified'))){
463 $this->_restoreCurrentValue();
464 }
465 }
467 function save()
468 {
469 if($this->getStatus() == 'modified'){
470 $ldap = $this->parent->config->get_ldap_link();
471 $ldap->cd($this->parent->config->current['BASE']);
472 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
473 $ldap->cat($dn);
474 if(!$ldap->count()){
475 $ldap->cd($dn);
476 $data = array(
477 'cn' => $this->class,
478 'objectClass' => array('top','gosaConfig'),
479 'gosaSetting' => $this->name.":".$this->tmp_value);
481 $ldap->add($data);
482 if(!$ldap->success()){
483 echo $ldap->get_error();
484 }
486 }else{
487 $attrs = $ldap->fetch();
488 $data = array();
489 $found = false;
490 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
491 $set = $attrs['gosaSetting'][$i];
492 if(preg_match("/^{$this->name}:/", $set)){
493 $set = "{$this->name}:{$this->tmp_value}";
494 $found = true;
495 }
496 $data['gosaSetting'][] = $set;
497 }
498 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
499 $ldap->cd($dn);
500 $ldap->modify($data);
501 if(!$ldap->success()){
502 echo $ldap->get_error();
503 }
504 }
505 $this->value = $this->tmp_value;
506 $this->setStatus('ldap');
507 }elseif($this->getStatus() == 'removed'){
508 $ldap = $this->parent->config->get_ldap_link();
509 $ldap->cd($this->parent->config->current['BASE']);
510 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
511 $ldap->cat($dn);
512 $attrs = $ldap->fetch();
513 $data = array('gosaSetting' => array());
514 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
515 $set = $attrs['gosaSetting'][$i];
516 if(preg_match("/^{$this->name}:/", $set)){
517 continue;
518 }
519 $data['gosaSetting'][] = $set;
520 }
521 $ldap->cd($dn);
522 $ldap->modify($data);
523 if(!$ldap->success()){
524 echo $ldap->get_error();
525 }
526 $this->_restoreCurrentValue();
527 }
528 }
530 private function setStatus($state)
531 {
532 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
533 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
534 }else{
535 $this->status = $state;
536 }
537 }
539 function isValid()
540 {
541 return(TRUE);
542 }
543 }
547 interface propertyMigration
548 {
549 function __construct($config,$property);
550 }
553 ?>