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 // Excludes property values defined in ldap
14 public $ignoreLdapProperties = FALSE;
16 // Contains all classes with plInfo
17 public $classesWithInfo = array();
18 public $pluginRequirements = array();
19 public $categoryToClass = array();
21 public $objectClasses = array();
23 public $schemaCheckFinished = FALSE;
25 function __construct($config)
26 {
27 $this->config = &$config;
29 // Detect classes that have a plInfo method
30 global $class_mapping;
31 foreach ($class_mapping as $cname => $path){
32 $cmethods = get_class_methods($cname);
33 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
35 // Get plugin definitions
36 $def = call_user_func(array($cname, 'plInfo'));;
38 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
39 if(count($def)){
40 $this->classesWithInfo[$cname] = $def;
41 }
42 }
43 }
45 // (Re)Load properties
46 $this->reload();
47 }
50 function validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE,$displayMessage = FALSE)
51 {
52 // We can check the schemata only with a valid LDAP connection
53 if(empty($this->config->current['CONFIG'])){
54 return(TRUE);
55 }
57 // Don't do things twice unless forced
58 if($this->schemaCheckFinished && !$force) return;
60 // Read objectClasses from ldap
61 if(!count($this->objectClasses)){
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd($this->config->current['BASE']);
64 $this->objectClasses = $ldap->get_objectclasses();
65 }
67 // Collect required schema infos
68 $this->pluginRequirements = array('ldapSchema' => array());
69 $this->categoryToClass = array();
70 foreach($this->classesWithInfo as $cname => $defs){
71 if(isset($defs['plRequirements']['ldapSchema'])){
72 $this->pluginRequirements['ldapSchema'][$cname] = $defs['plRequirements']['ldapSchema'];
73 }
74 }
76 // Check schema requirements now
77 $missing = $invalid = array();
78 foreach($this->pluginRequirements['ldapSchema'] as $cname => $requirements){
79 foreach($requirements as $oc => $version){
80 if(!$this->ocAvailable($oc)){
81 $missing[] = $oc;
82 }elseif(!empty($version)){
83 $currentVersion = $this->getObjectClassVersion($oc);
84 if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
85 if($currentVersion == -1){
86 $currentVersion = _("unknown");
87 }
88 $invalid[] = sprintf(_("%s has version %s but %s required!"), bold($oc),bold($currentVersion),bold($version));
89 }
90 }
91 }
92 }
94 if($displayMessage && count($missing)){
95 msg_dialog::display(_("Schema validation error"),
96 sprintf(_("The following objectClasses are missing! %s"), msgPool::buildList($missing)),
97 ERROR_DIALOG);
98 }
99 if($displayMessage && count($invalid)){
100 msg_dialog::display(_("Schema validation error"),
101 sprintf(_("The following objectClasses do not match the version requirements! %s"), msgPool::buildList($invalid)),
102 ERROR_DIALOG);
103 }
105 $this->schemaCheckFinished =TRUE;
106 }
108 function ocVersionMatch($required, $installed)
109 {
110 $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
111 $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
112 return(version_compare($installed,$required, $operator));
113 }
116 function getObjectClassVersion($oc)
117 {
118 if(!isset($this->objectClasses[$oc])){
119 return(NULL);
120 }else{
121 $version = -1; // unknown
122 if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
123 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
124 }
125 }
126 return($version);
127 }
130 // check wheter an objectClass is installed or not.
131 function ocAvailable($name)
132 {
133 return(isset($this->objectClasses[$name]));
134 }
137 function reload($force = FALSE)
138 {
139 // Do not reload the properties everytime, once we have
140 // everything loaded and registrered skip the reload.
141 // Status is 'finished' once we had a ldap connection (logged in)
142 if(!$force && $this->status == 'finished') return;
144 // Reset everything
145 $this->ldapStoredProperties = array();
146 $this->fileStoredProperties = array();
147 $this->properties = array();
148 $this->mapByName = array();
150 // Search for config flags defined in the config file (TAB section)
151 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
152 foreach($tabdefs as $info){
154 // Check if the info is valid
155 if(isset($info['NAME']) && isset($info['CLASS'])){
157 // Check if there is nore than just the plugin definition
158 if(count($info) > 2){
159 foreach($info as $name => $value){
161 if(!in_array($name, array('CLASS','NAME'))){
162 $class= $info['CLASS'];
163 $this->fileStoredProperties[$class][strtolower($name)] = $value;
164 }
165 }
166 }
167 }
168 }
169 }
171 // Search for config flags defined in the config file (MENU section)
172 foreach($this->config->data['MENU'] as $section => $entries){
173 foreach($entries as $entry){
174 if(count($entry) > 2 && isset($entry['CLASS'])){
175 $class = $entry['CLASS'];
176 foreach($entry as $name => $value){
177 if(!in_array($name, array('CLASS','ACL'))){
178 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
179 }
180 }
181 }
182 }
183 }
185 // Search for config flags defined in the config file (MAIN section)
186 foreach($this->config->data['MAIN'] as $name => $value){
187 $this->fileStoredProperties['core'][strtolower($name)] = $value;
188 }
190 // Search for config flags defined in the config file (Current LOCATION section)
191 if(isset($this->config->current)){
192 foreach($this->config->current as $name => $value){
193 $this->fileStoredProperties['core'][strtolower($name)] = $value;
194 }
195 }
197 // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
198 // in the config.
199 $this->ignoreLdapProperties = FALSE;
200 if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) &&
201 preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
202 $this->ignoreLdapProperties = TRUE;
203 }
205 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
206 if(!empty($this->config->current['CONFIG'])){
207 $ldap = $this->config->get_ldap_link();
208 $ldap->cd($this->config->current['CONFIG']);
209 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
210 while($attrs = $ldap->fetch()){
211 $class = $attrs['cn'][0];
212 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
213 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
214 $this->ldapStoredProperties[$class][$name] = $value;
215 }
216 }
217 $this->status = 'finished';
218 }
220 // Register plugin properties.
221 foreach ($this->classesWithInfo as $cname => $def){
223 // Detect class name
224 $name = $cname;
225 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
226 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
228 // Register post events
229 $this->classToName[$cname] = $name;
230 $data = array('name' => 'postcreate','type' => 'command');
231 $this->register($cname, $data);
232 $data = array('name' => 'postremove','type' => 'command');
233 $this->register($cname, $data);
234 $data = array('name' => 'postmodify','type' => 'command');
235 $this->register($cname, $data);
236 $data = array('name' => 'check', 'type' => 'command');
237 $this->register($cname, $data);
239 // Register properties
240 if(isset($def['plProperties'])){
241 foreach($def['plProperties'] as $property){
242 $this->register($cname, $property);
243 }
244 }
245 }
246 }
248 function register($class,$data)
249 {
250 $id = count($this->properties);
251 $this->properties[$id] = new gosaProperty($this,$class,$data);
252 $p = strtolower("{$class}::{$data['name']}");
253 $this->mapByName[$p] = $id;
254 }
256 public function getAllProperties()
257 {
258 return($this->properties);
259 }
261 function propertyExists($class,$name)
262 {
263 $p = strtolower("{$class}::{$name}");
264 return(isset($this->mapByName[$p]));
265 }
267 private function getId($class,$name)
268 {
269 $p = strtolower("{$class}::{$name}");
270 if(!isset($this->mapByName[$p])){
271 return(-1);
272 }
273 return($this->mapByName[$p]);
274 }
276 function getProperty($class,$name)
277 {
278 if($this->propertyExists($class,$name)){
279 return($this->properties[$this->getId($class,$name)]);
280 }
281 return(NULL);
282 }
284 function getPropertyValue($class,$name)
285 {
286 if($this->propertyExists($class,$name)){
287 $tmp = $this->getProperty($class,$name);
288 return($tmp->getValue());
289 }
290 return("");
291 }
293 function setPropertyValue($class,$name, $value)
294 {
295 if($this->propertyExists($class,$name)){
296 $tmp = $this->getProperty($class,$name);
297 return($tmp->setValue($value));
298 }
299 return("");
300 }
302 function saveChanges()
303 {
304 $migrate = array();
305 foreach($this->properties as $prop){
307 // Is this property modified
308 if(in_array($prop->getStatus(),array('modified','removed'))){
310 // Check if we've to migrate something before we can make the changes effective.
311 if($prop->migrationRequired()){
312 $migrate[] = $prop;
313 }else{
314 $prop->save();
315 }
316 }
317 }
318 return($migrate);
319 }
320 }
323 class gosaProperty
324 {
325 protected $name = "";
326 protected $class = "";
327 protected $value = "";
328 protected $tmp_value = ""; // Used when modified but not saved
329 protected $type = "string";
330 protected $default = "";
331 protected $defaults = "";
332 protected $description = "";
333 protected $check = "";
334 protected $migrate = "";
335 protected $mandatory = FALSE;
336 protected $group = "default";
337 protected $parent = NULL;
338 protected $data = array();
340 protected $migrationClass = NULL;
342 /*! The current property status
343 * 'ldap' Property is stored in ldap
344 * 'file' Property is stored in the config file
345 * 'undefined' Property is currently not stored anywhere
346 * 'modified' Property has been modified (should be saved)
347 */
348 protected $status = 'undefined';
350 protected $attributes = array('name','type','default','description','check',
351 'migrate','mandatory','group','defaults');
356 function __construct($parent,$classname,$data)
357 {
358 // Set some basic infos
359 $this->parent = &$parent;
360 $this->class = $classname;
361 $this->data = $data;
363 // Get all relevant information from the data array (comes from plInfo)
364 foreach($this->attributes as $aName){
365 if(isset($data[$aName])){
366 $this->$aName = $data[$aName];
367 }
368 }
370 // Initialize with the current value
371 $this->_restoreCurrentValue();
373 }
375 function migrationRequired()
376 {
377 // Instantiate migration class
378 if(!empty($this->migrate) && $this->migrationClass == NULL){
379 if(!class_available($this->migrate)){
380 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
381 }else{
382 $class = $this->migrate;
383 $tmp = new $class($this->parent->config,$this);
384 if(! $tmp instanceof propertyMigration){
385 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
386 }else{
387 $this->migrationClass = $tmp;
388 }
389 }
390 }
391 if(empty($this->migrate) || $this->migrationClass == NULL){
392 return(FALSE);
393 }
394 return($this->migrationClass->checkForIssues());
395 }
397 function getMigrationClass()
398 {
399 return($this->migrationClass);
400 }
402 function check()
403 {
404 $val = $this->getValue(TRUE);
405 $return = TRUE;
406 if($this->mandatory && empty($val)){
407 $return = FALSE;
408 }
410 $check = $this->getCheck();
411 if(!empty($val) && !empty($check)){
412 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
413 if(!$res){
414 $return = FALSE;
415 }
416 }
417 return($return);
418 }
420 static function isBool($message,$class,$name,$value, $type)
421 {
422 $match = in_array($value,array('true','false',''));
424 // Display the reason for failing this check.
425 if($message && ! $match){
426 msg_dialog::display(_("Warning"),
427 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"),
428 bold($value),bold($class),bold($name)),
429 WARNING_DIALOG);
430 }
432 return($match);
433 }
435 static function isString($message,$class,$name,$value, $type)
436 {
437 $match = TRUE;
439 // Display the reason for failing this check.
440 if($message && ! $match){
441 msg_dialog::display(_("Warning"),
442 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"),
443 bold($value),bold($class),bold($name)),
444 WARNING_DIALOG);
445 }
447 return($match);
448 }
450 static function isInteger($message,$class,$name,$value, $type)
451 {
452 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
454 // Display the reason for failing this check.
455 if($message && ! $match){
456 msg_dialog::display(_("Warning"),
457 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
458 bold($value),bold($class),bold($name)),
459 WARNING_DIALOG);
460 }
462 return($match);
463 }
465 static function isPath($message,$class,$name,$value, $type)
466 {
467 $match = preg_match("#^(/[^/]*/){1}#", $value);
469 // Display the reason for failing this check.
470 if($message && ! $match){
471 msg_dialog::display(_("Warning"),
472 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
473 bold($value),bold($class),bold($name)),
474 WARNING_DIALOG);
475 }
477 return($match);
478 }
480 static function isReadablePath($message,$class,$name,$value, $type)
481 {
482 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
484 // Display the reason for failing this check.
485 if($message && ! $match){
486 if(!is_dir($value)){
487 msg_dialog::display(_("Warning"),
488 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
489 bold($value),bold($class),bold($name)),
490 WARNING_DIALOG);
491 }elseif(!is_readable($value)){
492 msg_dialog::display(_("Warning"),
493 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
494 bold($value),bold($class),bold($name)),
495 WARNING_DIALOG);
496 }
497 }
499 return($match);
500 }
502 static function isWriteablePath($message,$class,$name,$value, $type)
503 {
504 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
506 // Display the reason for failing this check.
507 if($message && ! $match){
508 if(!is_dir($value)){
509 msg_dialog::display(_("Warning"),
510 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
511 bold($value),bold($class),bold($name)),
512 WARNING_DIALOG);
513 }elseif(!is_writeable($value)){
514 msg_dialog::display(_("Warning"),
515 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
516 bold($value),bold($class),bold($name)),
517 WARNING_DIALOG);
518 }
519 }
521 return($match);
522 }
524 static function isReadableFile($message,$class,$name,$value, $type)
525 {
526 $match = !empty($value) && is_readable($value) && is_file($value);
528 // Display the reason for failing this check.
529 if($message && ! $match){
531 if(!is_file($value)){
532 msg_dialog::display(_("Warning"),
533 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
534 bold($value),bold($class),bold($name)),
535 WARNING_DIALOG);
536 }elseif(!is_readable($value)){
537 msg_dialog::display(_("Warning"),
538 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
539 bold($value),bold($class),bold($name)),
540 WARNING_DIALOG);
541 }
542 }
544 return($match);
545 }
547 static function isCommand($message,$class,$name,$value, $type)
548 {
549 $match = TRUE;
551 // Display the reason for failing this check.
552 if($message && ! $match){
553 msg_dialog::display(_("Warning"),
554 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
555 bold($value),bold($class),bold($name)),
556 WARNING_DIALOG);
557 }
559 return($match);
560 }
562 static function isDn($message,$class,$name,$value, $type)
563 {
564 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
566 // Display the reason for failing this check.
567 if($message && ! $match){
568 msg_dialog::display(_("Warning"),
569 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
570 bold($value),bold($class),bold($name)),
571 WARNING_DIALOG);
572 }
574 return($match);
575 }
577 static function isRdn($message,$class,$name,$value, $type)
578 {
579 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
581 // Display the reason for failing this check.
582 if($message && ! $match){
583 msg_dialog::display(_("Warning"),
584 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
585 bold($value),bold($class),bold($name)),
586 WARNING_DIALOG);
587 }
589 return($match);
590 }
592 private function _restoreCurrentValue()
593 {
594 // First check for values in the LDAP Database.
595 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
596 $this->setStatus('ldap');
597 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
598 return;
599 }
601 // Second check for values in the config file.
602 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
603 $this->setStatus('file');
604 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
605 return;
606 }
608 // If there still wasn't found anything then fallback to the default.
609 if($this->getStatus() == 'undefined'){
610 $this->value = $this->getDefault();
611 }
612 }
614 function getMigrate() { return($this->migrate); }
615 function getCheck() { return($this->check); }
616 function getName() { return($this->name); }
617 function getClass() { return($this->class); }
618 function getGroup() { return($this->group); }
619 function getType() { return($this->type); }
620 function getDescription() { return($this->description); }
621 function getDefault() { return($this->default); }
622 function getDefaults() { return($this->defaults); }
623 function getStatus() { return($this->status); }
624 function isMandatory() { return($this->mandatory); }
626 function setValue($str)
627 {
628 if(in_array($this->getStatus(), array('modified'))){
629 $this->tmp_value = $str;
630 }elseif($this->value != $str){
631 $this->setStatus('modified');
632 $this->tmp_value = $str;
633 }
634 }
636 function getValue($temporary = FALSE)
637 {
638 if($temporary){
639 if(in_array($this->getStatus(), array('modified','removed'))){
640 return($this->tmp_value);
641 }else{
642 return($this->value);
643 }
644 }else{
646 // Do not return ldap values if we've to ignore them.
647 if($this->parent->ignoreLdapProperties){
648 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
649 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
650 }else{
651 return($this->getDefault());
652 }
653 }else{
654 return($this->value);
655 }
656 }
657 }
659 function restoreDefault()
660 {
661 if(in_array($this->getStatus(),array('ldap'))){
662 $this->setStatus('removed');
664 // Second check for values in the config file.
665 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
666 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
667 }else{
668 $this->tmp_value = $this->getDefault();
669 }
670 }
671 }
673 function save()
674 {
675 if($this->getStatus() == 'modified'){
676 $ldap = $this->parent->config->get_ldap_link();
677 $ldap->cd($this->parent->config->current['BASE']);
678 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
679 $ldap->cat($dn);
680 if(!$ldap->count()){
681 $ldap->cd($dn);
682 $data = array(
683 'cn' => $this->class,
684 'objectClass' => array('top','gosaConfig'),
685 'gosaSetting' => $this->name.":".$this->tmp_value);
687 $ldap->add($data);
688 if(!$ldap->success()){
689 echo $ldap->get_error();
690 }
692 }else{
693 $attrs = $ldap->fetch();
694 $data = array();
695 $found = false;
696 if(isset($attrs['gosaSetting']['count'])){
697 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
698 $set = $attrs['gosaSetting'][$i];
699 if(preg_match("/^{$this->name}:/", $set)){
700 $set = "{$this->name}:{$this->tmp_value}";
701 $found = true;
702 }
703 $data['gosaSetting'][] = $set;
704 }
705 }
706 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
707 $ldap->cd($dn);
708 $ldap->modify($data);
709 if(!$ldap->success()){
710 echo $ldap->get_error();
711 }
712 }
713 $this->value = $this->tmp_value;
714 $this->setStatus('ldap');
715 }elseif($this->getStatus() == 'removed'){
716 $ldap = $this->parent->config->get_ldap_link();
717 $ldap->cd($this->parent->config->current['BASE']);
718 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
719 $ldap->cat($dn);
720 $attrs = $ldap->fetch();
721 $data = array('gosaSetting' => array());
722 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
723 $set = $attrs['gosaSetting'][$i];
724 if(preg_match("/^{$this->name}:/", $set)){
725 continue;
726 }
727 $data['gosaSetting'][] = $set;
728 }
729 $ldap->cd($dn);
730 $ldap->modify($data);
731 if(!$ldap->success()){
732 echo $ldap->get_error();
733 }
734 $this->_restoreCurrentValue();
735 }
736 }
738 private function setStatus($state)
739 {
740 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
741 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
742 }else{
743 $this->status = $state;
744 }
745 }
747 function isValid()
748 {
749 return(TRUE);
750 }
751 }
755 interface propertyMigration
756 {
757 function __construct($config,$property);
758 }
761 ?>