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 foreach($this->pluginRequirements['ldapSchema'] as $cname => $requirements){
78 foreach($requirements as $oc => $version){
79 if(!$this->ocAvailable($oc)){
80 if($displayMessage){
81 msg_dialog::display(_("Schema validation error"),
82 sprintf(_("The objectClass '%s' which is required for plugin '%s' is not availabe!"),
83 bold($oc),bold($cname)),
84 ERROR_DIALOG);
85 }
86 }elseif(!empty($version)){
88 $currentVersion = $this->getObjectClassVersion($oc);
89 if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
90 if($displayMessage){
91 msg_dialog::display(_("Schema validation error"),
92 sprintf(_("The objectClass '%s' which is required for plugin '%s' has version %s but %s is required!"),bold($oc),bold($cname),bold($currentVersion),bold($version)),
93 ERROR_DIALOG);
94 }
95 }
96 }
97 }
98 }
99 $this->schemaCheckFinished =TRUE;
100 }
102 function ocVersionMatch($required, $installed)
103 {
104 $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
105 $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
106 return(version_compare($installed,$required, $operator));
107 }
110 function getObjectClassVersion($oc)
111 {
112 if(!isset($this->objectClasses[$oc])){
113 return(NULL);
114 }else{
115 $version = -1; // unknown
116 if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
117 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
118 }
119 }
120 return($version);
121 }
124 // check wheter an objectClass is installed or not.
125 function ocAvailable($name)
126 {
127 return(isset($this->objectClasses[$name]));
128 }
131 function reload($force = FALSE)
132 {
133 // Do not reload the properties everytime, once we have
134 // everything loaded and registrered skip the reload.
135 // Status is 'finished' once we had a ldap connection (logged in)
136 if(!$force && $this->status == 'finished') return;
138 // Reset everything
139 $this->ldapStoredProperties = array();
140 $this->fileStoredProperties = array();
141 $this->properties = array();
142 $this->mapByName = array();
144 // Search for config flags defined in the config file (TAB section)
145 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
146 foreach($tabdefs as $info){
148 // Check if the info is valid
149 if(isset($info['NAME']) && isset($info['CLASS'])){
151 // Check if there is nore than just the plugin definition
152 if(count($info) > 2){
153 foreach($info as $name => $value){
155 if(!in_array($name, array('CLASS','NAME'))){
156 $class= $info['CLASS'];
157 $this->fileStoredProperties[$class][strtolower($name)] = $value;
158 }
159 }
160 }
161 }
162 }
163 }
165 // Search for config flags defined in the config file (MENU section)
166 foreach($this->config->data['MENU'] as $section => $entries){
167 foreach($entries as $entry){
168 if(count($entry) > 2 && isset($entry['CLASS'])){
169 $class = $entry['CLASS'];
170 foreach($entry as $name => $value){
171 if(!in_array($name, array('CLASS','ACL'))){
172 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
173 }
174 }
175 }
176 }
177 }
179 // Search for config flags defined in the config file (MAIN section)
180 foreach($this->config->data['MAIN'] as $name => $value){
181 $this->fileStoredProperties['core'][strtolower($name)] = $value;
182 }
184 // Search for config flags defined in the config file (Current LOCATION section)
185 if(isset($this->config->current)){
186 foreach($this->config->current as $name => $value){
187 $this->fileStoredProperties['core'][strtolower($name)] = $value;
188 }
189 }
191 // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
192 // in the config.
193 $this->ignoreLdapProperties = FALSE;
194 if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) &&
195 preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
196 $this->ignoreLdapProperties = TRUE;
197 }
199 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
200 if(!empty($this->config->current['CONFIG'])){
201 $ldap = $this->config->get_ldap_link();
202 $ldap->cd($this->config->current['CONFIG']);
203 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
204 while($attrs = $ldap->fetch()){
205 $class = $attrs['cn'][0];
206 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
207 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
208 $this->ldapStoredProperties[$class][$name] = $value;
209 }
210 }
211 $this->status = 'finished';
212 }
214 // Register plugin properties.
215 foreach ($this->classesWithInfo as $cname => $def){
217 // Detect class name
218 $name = $cname;
219 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
220 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
222 // Register post events
223 $this->classToName[$cname] = $name;
224 $data = array('name' => 'postcreate','type' => 'command');
225 $this->register($cname, $data);
226 $data = array('name' => 'postremove','type' => 'command');
227 $this->register($cname, $data);
228 $data = array('name' => 'postmodify','type' => 'command');
229 $this->register($cname, $data);
230 $data = array('name' => 'check', 'type' => 'command');
231 $this->register($cname, $data);
233 // Register properties
234 if(isset($def['plProperties'])){
235 foreach($def['plProperties'] as $property){
236 $this->register($cname, $property);
237 }
238 }
239 }
240 }
242 function register($class,$data)
243 {
244 $id = count($this->properties);
245 $this->properties[$id] = new gosaProperty($this,$class,$data);
246 $p = strtolower("{$class}::{$data['name']}");
247 $this->mapByName[$p] = $id;
248 }
250 public function getAllProperties()
251 {
252 return($this->properties);
253 }
255 function propertyExists($class,$name)
256 {
257 $p = strtolower("{$class}::{$name}");
258 return(isset($this->mapByName[$p]));
259 }
261 private function getId($class,$name)
262 {
263 $p = strtolower("{$class}::{$name}");
264 if(!isset($this->mapByName[$p])){
265 return(-1);
266 }
267 return($this->mapByName[$p]);
268 }
270 function getProperty($class,$name)
271 {
272 if($this->propertyExists($class,$name)){
273 return($this->properties[$this->getId($class,$name)]);
274 }
275 return(NULL);
276 }
278 function getPropertyValue($class,$name)
279 {
280 if($this->propertyExists($class,$name)){
281 $tmp = $this->getProperty($class,$name);
282 return($tmp->getValue());
283 }
284 return("");
285 }
287 function setPropertyValue($class,$name, $value)
288 {
289 if($this->propertyExists($class,$name)){
290 $tmp = $this->getProperty($class,$name);
291 return($tmp->setValue($value));
292 }
293 return("");
294 }
296 function saveChanges()
297 {
298 $migrate = array();
299 foreach($this->properties as $prop){
301 // Is this property modified
302 if(in_array($prop->getStatus(),array('modified','removed'))){
304 // Check if we've to migrate something before we can make the changes effective.
305 if($prop->migrationRequired()){
306 $migrate[] = $prop;
307 }else{
308 $prop->save();
309 }
310 }
311 }
312 return($migrate);
313 }
314 }
317 class gosaProperty
318 {
319 protected $name = "";
320 protected $class = "";
321 protected $value = "";
322 protected $tmp_value = ""; // Used when modified but not saved
323 protected $type = "string";
324 protected $default = "";
325 protected $defaults = "";
326 protected $description = "";
327 protected $check = "";
328 protected $migrate = "";
329 protected $mandatory = FALSE;
330 protected $group = "default";
331 protected $parent = NULL;
332 protected $data = array();
334 protected $migrationClass = NULL;
336 /*! The current property status
337 * 'ldap' Property is stored in ldap
338 * 'file' Property is stored in the config file
339 * 'undefined' Property is currently not stored anywhere
340 * 'modified' Property has been modified (should be saved)
341 */
342 protected $status = 'undefined';
344 protected $attributes = array('name','type','default','description','check',
345 'migrate','mandatory','group','defaults');
350 function __construct($parent,$classname,$data)
351 {
352 // Set some basic infos
353 $this->parent = &$parent;
354 $this->class = $classname;
355 $this->data = $data;
357 // Get all relevant information from the data array (comes from plInfo)
358 foreach($this->attributes as $aName){
359 if(isset($data[$aName])){
360 $this->$aName = $data[$aName];
361 }
362 }
364 // Initialize with the current value
365 $this->_restoreCurrentValue();
367 }
369 function migrationRequired()
370 {
371 // Instantiate migration class
372 if(!empty($this->migrate) && $this->migrationClass == NULL){
373 if(!class_available($this->migrate)){
374 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
375 }else{
376 $class = $this->migrate;
377 $tmp = new $class($this->parent->config,$this);
378 if(! $tmp instanceof propertyMigration){
379 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
380 }else{
381 $this->migrationClass = $tmp;
382 }
383 }
384 }
385 if(empty($this->migrate) || $this->migrationClass == NULL){
386 return(FALSE);
387 }
388 return($this->migrationClass->checkForIssues());
389 }
391 function getMigrationClass()
392 {
393 return($this->migrationClass);
394 }
396 function check()
397 {
398 $val = $this->getValue(TRUE);
399 $return = TRUE;
400 if($this->mandatory && empty($val)){
401 $return = FALSE;
402 }
404 $check = $this->getCheck();
405 if(!empty($val) && !empty($check)){
406 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
407 if(!$res){
408 $return = FALSE;
409 }
410 }
411 return($return);
412 }
414 static function isBool($message,$class,$name,$value, $type)
415 {
416 $match = in_array($value,array('true','false',''));
418 // Display the reason for failing this check.
419 if($message && ! $match){
420 msg_dialog::display(_("Warning"),
421 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"),
422 bold($value),bold($class),bold($name)),
423 WARNING_DIALOG);
424 }
426 return($match);
427 }
429 static function isString($message,$class,$name,$value, $type)
430 {
431 $match = TRUE;
433 // Display the reason for failing this check.
434 if($message && ! $match){
435 msg_dialog::display(_("Warning"),
436 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"),
437 bold($value),bold($class),bold($name)),
438 WARNING_DIALOG);
439 }
441 return($match);
442 }
444 static function isInteger($message,$class,$name,$value, $type)
445 {
446 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
448 // Display the reason for failing this check.
449 if($message && ! $match){
450 msg_dialog::display(_("Warning"),
451 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
452 bold($value),bold($class),bold($name)),
453 WARNING_DIALOG);
454 }
456 return($match);
457 }
459 static function isPath($message,$class,$name,$value, $type)
460 {
461 $match = preg_match("#^(/[^/]*/){1}#", $value);
463 // Display the reason for failing this check.
464 if($message && ! $match){
465 msg_dialog::display(_("Warning"),
466 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
467 bold($value),bold($class),bold($name)),
468 WARNING_DIALOG);
469 }
471 return($match);
472 }
474 static function isReadablePath($message,$class,$name,$value, $type)
475 {
476 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
478 // Display the reason for failing this check.
479 if($message && ! $match){
480 if(!is_dir($value)){
481 msg_dialog::display(_("Warning"),
482 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
483 bold($value),bold($class),bold($name)),
484 WARNING_DIALOG);
485 }elseif(!is_readable($value)){
486 msg_dialog::display(_("Warning"),
487 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
488 bold($value),bold($class),bold($name)),
489 WARNING_DIALOG);
490 }
491 }
493 return($match);
494 }
496 static function isWriteablePath($message,$class,$name,$value, $type)
497 {
498 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
500 // Display the reason for failing this check.
501 if($message && ! $match){
502 if(!is_dir($value)){
503 msg_dialog::display(_("Warning"),
504 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
505 bold($value),bold($class),bold($name)),
506 WARNING_DIALOG);
507 }elseif(!is_writeable($value)){
508 msg_dialog::display(_("Warning"),
509 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
510 bold($value),bold($class),bold($name)),
511 WARNING_DIALOG);
512 }
513 }
515 return($match);
516 }
518 static function isReadableFile($message,$class,$name,$value, $type)
519 {
520 $match = !empty($value) && is_readable($value) && is_file($value);
522 // Display the reason for failing this check.
523 if($message && ! $match){
525 if(!is_file($value)){
526 msg_dialog::display(_("Warning"),
527 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
528 bold($value),bold($class),bold($name)),
529 WARNING_DIALOG);
530 }elseif(!is_readable($value)){
531 msg_dialog::display(_("Warning"),
532 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
533 bold($value),bold($class),bold($name)),
534 WARNING_DIALOG);
535 }
536 }
538 return($match);
539 }
541 static function isCommand($message,$class,$name,$value, $type)
542 {
543 $match = TRUE;
545 // Display the reason for failing this check.
546 if($message && ! $match){
547 msg_dialog::display(_("Warning"),
548 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
549 bold($value),bold($class),bold($name)),
550 WARNING_DIALOG);
551 }
553 return($match);
554 }
556 static function isDn($message,$class,$name,$value, $type)
557 {
558 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
560 // Display the reason for failing this check.
561 if($message && ! $match){
562 msg_dialog::display(_("Warning"),
563 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
564 bold($value),bold($class),bold($name)),
565 WARNING_DIALOG);
566 }
568 return($match);
569 }
571 static function isRdn($message,$class,$name,$value, $type)
572 {
573 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
575 // Display the reason for failing this check.
576 if($message && ! $match){
577 msg_dialog::display(_("Warning"),
578 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
579 bold($value),bold($class),bold($name)),
580 WARNING_DIALOG);
581 }
583 return($match);
584 }
586 private function _restoreCurrentValue()
587 {
588 // First check for values in the LDAP Database.
589 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
590 $this->setStatus('ldap');
591 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
592 return;
593 }
595 // Second check for values in the config file.
596 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
597 $this->setStatus('file');
598 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
599 return;
600 }
602 // If there still wasn't found anything then fallback to the default.
603 if($this->getStatus() == 'undefined'){
604 $this->value = $this->getDefault();
605 }
606 }
608 function getMigrate() { return($this->migrate); }
609 function getCheck() { return($this->check); }
610 function getName() { return($this->name); }
611 function getClass() { return($this->class); }
612 function getGroup() { return($this->group); }
613 function getType() { return($this->type); }
614 function getDescription() { return($this->description); }
615 function getDefault() { return($this->default); }
616 function getDefaults() { return($this->defaults); }
617 function getStatus() { return($this->status); }
618 function isMandatory() { return($this->mandatory); }
620 function setValue($str)
621 {
622 if(in_array($this->getStatus(), array('modified'))){
623 $this->tmp_value = $str;
624 }elseif($this->value != $str){
625 $this->setStatus('modified');
626 $this->tmp_value = $str;
627 }
628 }
630 function getValue($temporary = FALSE)
631 {
632 if($temporary){
633 if(in_array($this->getStatus(), array('modified','removed'))){
634 return($this->tmp_value);
635 }else{
636 return($this->value);
637 }
638 }else{
640 // Do not return ldap values if we've to ignore them.
641 if($this->parent->ignoreLdapProperties){
642 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
643 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
644 }else{
645 return($this->getDefault());
646 }
647 }else{
648 return($this->value);
649 }
650 }
651 }
653 function restoreDefault()
654 {
655 if(in_array($this->getStatus(),array('ldap'))){
656 $this->setStatus('removed');
658 // Second check for values in the config file.
659 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
660 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
661 }else{
662 $this->tmp_value = $this->getDefault();
663 }
664 }
665 }
667 function save()
668 {
669 if($this->getStatus() == 'modified'){
670 $ldap = $this->parent->config->get_ldap_link();
671 $ldap->cd($this->parent->config->current['BASE']);
672 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
673 $ldap->cat($dn);
674 if(!$ldap->count()){
675 $ldap->cd($dn);
676 $data = array(
677 'cn' => $this->class,
678 'objectClass' => array('top','gosaConfig'),
679 'gosaSetting' => $this->name.":".$this->tmp_value);
681 $ldap->add($data);
682 if(!$ldap->success()){
683 echo $ldap->get_error();
684 }
686 }else{
687 $attrs = $ldap->fetch();
688 $data = array();
689 $found = false;
690 if(isset($attrs['gosaSetting']['count'])){
691 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
692 $set = $attrs['gosaSetting'][$i];
693 if(preg_match("/^{$this->name}:/", $set)){
694 $set = "{$this->name}:{$this->tmp_value}";
695 $found = true;
696 }
697 $data['gosaSetting'][] = $set;
698 }
699 }
700 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
701 $ldap->cd($dn);
702 $ldap->modify($data);
703 if(!$ldap->success()){
704 echo $ldap->get_error();
705 }
706 }
707 $this->value = $this->tmp_value;
708 $this->setStatus('ldap');
709 }elseif($this->getStatus() == 'removed'){
710 $ldap = $this->parent->config->get_ldap_link();
711 $ldap->cd($this->parent->config->current['BASE']);
712 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
713 $ldap->cat($dn);
714 $attrs = $ldap->fetch();
715 $data = array('gosaSetting' => array());
716 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
717 $set = $attrs['gosaSetting'][$i];
718 if(preg_match("/^{$this->name}:/", $set)){
719 continue;
720 }
721 $data['gosaSetting'][] = $set;
722 }
723 $ldap->cd($dn);
724 $ldap->modify($data);
725 if(!$ldap->success()){
726 echo $ldap->get_error();
727 }
728 $this->_restoreCurrentValue();
729 }
730 }
732 private function setStatus($state)
733 {
734 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
735 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
736 }else{
737 $this->status = $state;
738 }
739 }
741 function isValid()
742 {
743 return(TRUE);
744 }
745 }
749 interface propertyMigration
750 {
751 function __construct($config,$property);
752 }
755 ?>