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 $detectedSchemaIssues = array();
24 public $schemaCheckFailed = FALSE;
25 public $schemaCheckFinished = FALSE;
26 public $pluginsDeactivated = array();
28 // Name of enabled plugins found in gosa.conf.
29 private $activePlugins = array();
32 /*! \brief Constructs the config registry
33 * @param config The configuration object
34 * @return
35 */
36 function __construct($config)
37 {
38 $this->config = &$config;
40 // Detect classes that have a plInfo method
41 global $class_mapping;
42 foreach ($class_mapping as $cname => $path){
43 $cmethods = get_class_methods($cname);
44 if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
46 // Get plugin definitions
47 $def = call_user_func(array($cname, 'plInfo'));;
49 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
50 if(count($def)){
51 $this->classesWithInfo[$cname] = $def;
52 }
53 }
54 }
56 // (Re)Load properties
57 $this->reload();
58 }
61 /*! \brief Checks whether the schema check was called in the current session or not.
62 * @return Boolean True if check was already called
63 */
64 function schemaCheckFinished()
65 {
66 return($this->schemaCheckFinished);
67 }
70 /*! \brief Starts the schema validation
71 * @param Boolean 'force' Force a re-check.
72 * @param Boolean 'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
73 * @return Boolean True on success else FALSE
74 */
75 function validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE, $objectClassesToUse = array())
76 {
77 // Read objectClasses from ldap
78 if(count($objectClassesToUse)){
79 $this->setObjectClasses($objectClassesToUse);
80 }elseif(!count($this->objectClasses)){
81 $ldap = $this->config->get_ldap_link();
82 $ldap->cd($this->config->current['BASE']);
83 $this->setObjectClasses($ldap->get_objectclasses());
84 }
86 return($this->_validateSchemata($force, $disableIncompatiblePlugins));
87 }
90 /*! \brief Sets the list object classes to use while validation the schema. (See 'validateSchemata')
91 * This is called from the GOsa-Setup
92 * @param Array The list of object classes (usually LDAP::get_objectlclasses()).
93 * @return void
94 */
95 function setObjectClasses($ocs)
96 {
97 $this->objectClasses = $ocs;
98 }
101 /*! \brief Returns an array which contains all unresolved schemata requirements.
102 * @return Array An array containing all errors/issues
103 */
104 function getSchemaResults()
105 {
106 return($this->detectedSchemaIssues);
107 }
110 /*! \brief This method checks if the installed ldap-schemata matches the plugin requirements.
111 * @param Boolean 'force' Force a re-check.
112 * @param Boolean 'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
113 * @return String
114 */
115 private function _validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE)
116 {
117 // We cannot check without readable schema info
118 if(!count($this->objectClasses)){
119 return(TRUE);
120 }
122 // Don't do things twice unless forced
123 if($this->schemaCheckFinished && !$force) return($this->schemaCheckFailed);
125 // Prepare result array
126 $this->detectedSchemaIssues = array();
127 $this->detectedSchemaIssues['missing'] = array();
128 $this->detectedSchemaIssues['versionMismatch'] = array();
130 // Clear last results
131 $this->pluginsDeactivated = array();
133 // Collect required schema infos
134 $this->pluginRequirements = array('ldapSchema' => array());
135 $this->categoryToClass = array();
137 // Walk through plugins with requirements, but only check for active plugins.
138 foreach($this->classesWithInfo as $cname => $defs){
139 if(isset($defs['plRequirements'])){
141 // Only queue checks for active plugins.
142 if(isset($this->activePlugins[strtolower($cname)])){
143 $this->pluginRequirements[$cname] = $defs['plRequirements'];
144 }else{
145 new log("debug","schemaValidation","Skipped schema check for '{$cname}' plugin is inactive!",
146 array(),'');
147 }
148 }
149 }
151 // Check schema requirements now $missing = $invalid = array();
152 foreach($this->pluginRequirements as $cname => $requirements){
154 // Check LDAP schema requirements for this plugins
155 $failure = FALSE;
156 if(isset($requirements['ldapSchema'])){
157 foreach($requirements['ldapSchema'] as $oc => $version){
158 if(!$this->ocAvailable($oc)){
159 $this->detectedSchemaIssues['missing'][$oc] = $oc;
161 $this->schemaCheckFailed = TRUE;
162 $failure = TRUE;
164 new log("debug","schemaValidation","LDAP objectClass missing '{$oc}'!",
165 array(),'');
167 }elseif(!empty($version)){
168 $currentVersion = $this->getObjectClassVersion($oc);
169 if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
170 if($currentVersion == -1){
171 $currentVersion = _("unknown");
172 }
173 $this->detectedSchemaIssues['versionMismatch'][$oc] =
174 sprintf(_("%s has version %s but %s is required!"), bold($oc),bold($currentVersion),bold($version));
175 $this->schemaCheckFailed = TRUE;
176 $failure = TRUE;
178 new log("debug","schemaValidation","LDAP objectClass version mismatch '{$oc}' ".
179 "has '{$currentVersion}' but {$version} required!",
180 array(),'');
181 }
182 }
183 }
184 }
186 // Display corresponding plugins now
187 if($disableIncompatiblePlugins && $failure && isset($requirements['onFailureDisablePlugin'])){
188 foreach($requirements['onFailureDisablePlugin'] as $name){
189 $this->pluginsDeactivated[$name] = $name;
190 }
191 }
192 }
193 $this->schemaCheckFinished =TRUE;
194 session::un_set('plist');
195 return(!$this->schemaCheckFailed);
196 }
199 /*! \brief The function 'validateSchemata' may has disabled some GOsa-Plugins,
200 * the list of disabled plugins will be returned here.
201 * @return Array The list of plugins disabled by 'validateSchemata'
202 */
203 function getDisabledPlugins()
204 {
205 return($this->pluginsDeactivated);
206 }
209 /*! \brief Displays an error message with all issues detect during the schema validation.
210 */
211 function displayRequirementErrors()
212 {
213 $message = "";
214 if(count($this->detectedSchemaIssues['missing'])){
215 $message.= "<br>".
216 _("The following object classes are missing:").
217 "<div class='scrollContainer' style='height:100px'>".
218 msgPool::buildList(array_values($this->detectedSchemaIssues['missing'])).
219 "</div>";
220 }
221 if(count($this->detectedSchemaIssues['versionMismatch'])){
222 $message.= "<br>".
223 _("The following object classes are outdated:").
224 "<div class='scrollContainer' style='height:100px'>".
225 msgPool::buildList(array_values($this->detectedSchemaIssues['versionMismatch'])).
226 "</div>";
227 }
228 if($message != ""){
229 $message.= "<br>"._("Plugins that require one or more of the object classes above will be disabled until the object classes get updated.");
231 msg_dialog::display(_("Schema validation error"),$message, ERROR_DIALOG);
232 }
233 }
236 /*! \brief Checks to version strings (e.g. '>=v2.8' and '2.9')
237 * @param String The required version with operators (e.g. '>=2.8')
238 * @param String The version to match for withOUT operators (e.g. '2.9')
239 * @return Boolean True if version matches else false.
240 */
241 private function ocVersionMatch($required, $installed)
242 {
243 $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
244 $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
245 return(version_compare($installed,$required, $operator));
246 }
249 /*! \brief Returns the currently installed version of a given object class.
250 * @param String The name of the objectClass to check for.
251 * @return String The version string of the objectClass (e.g. v2.7)
252 */
253 function getObjectClassVersion($oc)
254 {
255 if(!isset($this->objectClasses[$oc])){
256 return(NULL);
257 }else{
258 $version = -1; // unknown
259 if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
260 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
261 }
262 }
263 return($version);
264 }
267 /*! \brief Check whether the given object class is available or not.
268 * @param String The name of the objectClass to check for (e.g. 'mailAccount')
269 * @return Boolean Returns TRUE if the class exists else FALSE.
270 */
271 function ocAvailable($name)
272 {
273 return(isset($this->objectClasses[$name]));
274 }
277 /*! \brief Re-loads the list of installed GOsa-Properties.
278 * @param Boolean $force If force is TRUE, the complete properties list is rebuild..
279 */
280 function reload($force = FALSE)
281 {
282 // Do not reload the properties everytime, once we have
283 // everything loaded and registrered skip the reload.
284 // Status is 'finished' once we had a ldap connection (logged in)
285 if(!$force && $this->status == 'finished') return;
287 // Reset everything
288 $this->ldapStoredProperties = array();
289 $this->fileStoredProperties = array();
290 $this->properties = array();
291 $this->mapByName = array();
292 $this->activePlugins = array('core'=>'core');
294 if(!$this->config) return;
296 // Search for config flags defined in the config file (TAB section)
297 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
298 foreach($tabdefs as $info){
300 // Put plugin in list of active plugins
301 if(isset($info['CLASS'])){
302 $class = strtolower($info['CLASS']);
303 $this->activePlugins[$class] = $class;
304 }
306 // Check if the info is valid
307 if(isset($info['NAME']) && isset($info['CLASS'])){
310 // Check if there is nore than just the plugin definition
311 if(count($info) > 2){
312 foreach($info as $name => $value){
314 if(!in_array($name, array('CLASS','NAME'))){
315 $class= $info['CLASS'];
316 $this->fileStoredProperties[$class][strtolower($name)] = $value;
317 }
318 }
319 }
320 }
321 }
322 }
324 // Search for config flags defined in the config file (MENU section)
325 foreach($this->config->data['MENU'] as $section => $entries){
326 foreach($entries as $entry){
328 if(isset($entry['CLASS'])){
330 // Put plugin to active plugins list.
331 $class = strtolower($entry['CLASS']);
332 $this->activePlugins[$class] = $class;
334 if(count($entry) > 2 ){
335 foreach($entry as $name => $value){
336 if(!in_array($name, array('CLASS','ACL'))){
337 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
338 }
339 }
340 }
341 }
342 }
343 }
345 // Search for config flags defined in the config file (MAIN section)
346 foreach($this->config->data['MAIN'] as $name => $value){
347 $this->fileStoredProperties['core'][strtolower($name)] = $value;
348 }
350 // Search for config flags defined in the config file (Current LOCATION section)
351 if(isset($this->config->current)){
352 foreach($this->config->current as $name => $value){
353 $this->fileStoredProperties['core'][strtolower($name)] = $value;
354 }
355 }
357 // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
358 // in the config.
359 $this->ignoreLdapProperties = FALSE;
360 if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) &&
361 preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
362 $this->ignoreLdapProperties = TRUE;
363 }
365 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
366 if(!empty($this->config->current['CONFIG'])){
367 $ldap = $this->config->get_ldap_link();
368 $ldap->cd($this->config->current['CONFIG']);
369 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
370 while($attrs = $ldap->fetch()){
371 $class = $attrs['cn'][0];
372 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
373 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
374 $this->ldapStoredProperties[$class][$name] = $value;
375 }
376 }
377 $this->status = 'finished';
378 }
380 // Register plugin properties.
381 foreach ($this->classesWithInfo as $cname => $def){
383 // Detect class name
384 $name = $cname;
385 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
386 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
388 // Register post events
389 $this->classToName[$cname] = $name;
390 $data = array('name' => 'postcreate','type' => 'command');
391 $this->register($cname, $data);
392 $data = array('name' => 'postremove','type' => 'command');
393 $this->register($cname, $data);
394 $data = array('name' => 'postmodify','type' => 'command');
395 $this->register($cname, $data);
396 $data = array('name' => 'check', 'type' => 'command');
397 $this->register($cname, $data);
399 // Register properties
400 if(isset($def['plProperties'])){
401 foreach($def['plProperties'] as $property){
402 $this->register($cname, $property);
403 }
404 }
405 }
406 }
409 /*! \brief Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
410 * @param String $class The name of the class/plugin that wants to register this property.
411 * @return Array $data An array containing all data set in plInfo['plProperty]
412 */
413 function register($class,$data)
414 {
415 $id = count($this->properties);
416 $this->properties[$id] = new gosaProperty($this,$class,$data);
417 $p = strtolower("{$class}::{$data['name']}");
418 $this->mapByName[$p] = $id;
419 }
422 /*! \brief Returns all registered properties.
423 * @return Array A list of all properties.
424 */
425 public function getAllProperties()
426 {
427 return($this->properties);
428 }
431 /*! \brief Checks whether a property exists or not.
432 * @param String $class The class name (e.g. 'core' or 'mailAccount')
433 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
434 * @return Boolean TRUE if it exists else FALSE.
435 */
436 function propertyExists($class,$name)
437 {
438 $p = strtolower("{$class}::{$name}");
439 return(isset($this->mapByName[$p]));
440 }
443 /*! \brief Returns the id of a registered property.
444 * @param String $class The class name (e.g. 'core' or 'mailAccount')
445 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
446 * @return Integer The id for the given property.
447 */
448 private function getId($class,$name)
449 {
450 $p = strtolower("{$class}::{$name}");
451 if(!isset($this->mapByName[$p])){
452 return(-1);
453 }
454 return($this->mapByName[$p]);
455 }
458 /*! \brief Returns a given property, if it exists.
459 * @param String $class The class name (e.g. 'core' or 'mailAccount')
460 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
461 * @return GOsaPropery The property or 'NULL' if it doesn't exists.
462 */
463 function getProperty($class,$name)
464 {
465 if($this->propertyExists($class,$name)){
466 return($this->properties[$this->getId($class,$name)]);
467 }
468 return(NULL);
469 }
472 /*! \brief Returns the value for a given property, if it exists.
473 * @param String $class The class name (e.g. 'core' or 'mailAccount')
474 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
475 * @return GOsaPropery The property value or an empty string if it doesn't exists.
476 */
477 function getPropertyValue($class,$name)
478 {
479 if($this->propertyExists($class,$name)){
480 $tmp = $this->getProperty($class,$name);
481 return($tmp->getValue());
482 }
483 return("");
484 }
487 /*! \brief Set a new value for a given property, if it exists.
488 * @param String $class The class name (e.g. 'core' or 'mailAccount')
489 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
490 * @return
491 */
492 function setPropertyValue($class,$name, $value)
493 {
494 if($this->propertyExists($class,$name)){
495 $tmp = $this->getProperty($class,$name);
496 return($tmp->setValue($value));
497 }
498 return("");
499 }
502 /*! \brief Save all temporary made property changes and thus make them useable/effective.
503 * @return Array Returns a list of plugins that have to be migrated before they can be saved.
504 */
505 function saveChanges()
506 {
507 $migrate = array();
508 foreach($this->properties as $prop){
510 // Is this property modified
511 if(in_array($prop->getStatus(),array('modified','removed'))){
513 // Check if we've to migrate something before we can make the changes effective.
514 if($prop->migrationRequired()){
515 $migrate[] = $prop;
516 }else{
517 $prop->save();
518 }
519 }
520 }
521 return($migrate);
522 }
523 }
526 class gosaProperty
527 {
528 protected $name = "";
529 protected $class = "";
530 protected $value = "";
531 protected $tmp_value = ""; // Used when modified but not saved
532 protected $type = "string";
533 protected $default = "";
534 protected $defaults = "";
535 protected $description = "";
536 protected $check = "";
537 protected $migrate = "";
538 protected $mandatory = FALSE;
539 protected $group = "default";
540 protected $parent = NULL;
541 protected $data = array();
543 protected $migrationClass = NULL;
545 /*! The current property status
546 * 'ldap' Property is stored in ldap
547 * 'file' Property is stored in the config file
548 * 'undefined' Property is currently not stored anywhere
549 * 'modified' Property has been modified (should be saved)
550 */
551 protected $status = 'undefined';
553 protected $attributes = array('name','type','default','description','check',
554 'migrate','mandatory','group','defaults');
559 function __construct($parent,$classname,$data)
560 {
561 // Set some basic infos
562 $this->parent = &$parent;
563 $this->class = $classname;
564 $this->data = $data;
566 // Get all relevant information from the data array (comes from plInfo)
567 foreach($this->attributes as $aName){
568 if(isset($data[$aName])){
569 $this->$aName = $data[$aName];
570 }
571 }
573 // Initialize with the current value
574 $this->_restoreCurrentValue();
576 }
578 function migrationRequired()
579 {
580 // Instantiate migration class
581 if(!empty($this->migrate) && $this->migrationClass == NULL){
582 if(!class_available($this->migrate)){
583 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
584 }else{
585 $class = $this->migrate;
586 $tmp = new $class($this->parent->config,$this);
587 if(! $tmp instanceof propertyMigration){
588 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
589 }else{
590 $this->migrationClass = $tmp;
591 }
592 }
593 }
594 if(empty($this->migrate) || $this->migrationClass == NULL){
595 return(FALSE);
596 }
597 return($this->migrationClass->checkForIssues());
598 }
600 function getMigrationClass()
601 {
602 return($this->migrationClass);
603 }
605 function check()
606 {
607 $val = $this->getValue(TRUE);
608 $return = TRUE;
609 if($this->mandatory && empty($val)){
610 $return = FALSE;
611 }
613 $check = $this->getCheck();
614 if(!empty($val) && !empty($check)){
615 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
616 if(!$res){
617 $return = FALSE;
618 }
619 }
620 return($return);
621 }
623 static function isBool($message,$class,$name,$value, $type)
624 {
625 $match = in_array($value,array('true','false',''));
627 // Display the reason for failing this check.
628 if($message && ! $match){
629 msg_dialog::display(_("Warning"),
630 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"),
631 bold($value),bold($class),bold($name)),
632 WARNING_DIALOG);
633 }
635 return($match);
636 }
638 static function isString($message,$class,$name,$value, $type)
639 {
640 $match = TRUE;
642 // Display the reason for failing this check.
643 if($message && ! $match){
644 msg_dialog::display(_("Warning"),
645 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"),
646 bold($value),bold($class),bold($name)),
647 WARNING_DIALOG);
648 }
650 return($match);
651 }
653 static function isInteger($message,$class,$name,$value, $type)
654 {
655 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
657 // Display the reason for failing this check.
658 if($message && ! $match){
659 msg_dialog::display(_("Warning"),
660 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
661 bold($value),bold($class),bold($name)),
662 WARNING_DIALOG);
663 }
665 return($match);
666 }
668 static function isPath($message,$class,$name,$value, $type)
669 {
670 $match = preg_match("#^(/[^/]*/){1}#", $value);
672 // Display the reason for failing this check.
673 if($message && ! $match){
674 msg_dialog::display(_("Warning"),
675 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
676 bold($value),bold($class),bold($name)),
677 WARNING_DIALOG);
678 }
680 return($match);
681 }
683 static function isReadablePath($message,$class,$name,$value, $type)
684 {
685 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
687 // Display the reason for failing this check.
688 if($message && ! $match){
689 if(!is_dir($value)){
690 msg_dialog::display(_("Warning"),
691 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
692 bold($value),bold($class),bold($name)),
693 WARNING_DIALOG);
694 }elseif(!is_readable($value)){
695 msg_dialog::display(_("Warning"),
696 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
697 bold($value),bold($class),bold($name)),
698 WARNING_DIALOG);
699 }
700 }
702 return($match);
703 }
705 static function isWriteablePath($message,$class,$name,$value, $type)
706 {
707 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
709 // Display the reason for failing this check.
710 if($message && ! $match){
711 if(!is_dir($value)){
712 msg_dialog::display(_("Warning"),
713 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
714 bold($value),bold($class),bold($name)),
715 WARNING_DIALOG);
716 }elseif(!is_writeable($value)){
717 msg_dialog::display(_("Warning"),
718 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
719 bold($value),bold($class),bold($name)),
720 WARNING_DIALOG);
721 }
722 }
724 return($match);
725 }
727 static function isReadableFile($message,$class,$name,$value, $type)
728 {
729 $match = !empty($value) && is_readable($value) && is_file($value);
731 // Display the reason for failing this check.
732 if($message && ! $match){
734 if(!is_file($value)){
735 msg_dialog::display(_("Warning"),
736 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
737 bold($value),bold($class),bold($name)),
738 WARNING_DIALOG);
739 }elseif(!is_readable($value)){
740 msg_dialog::display(_("Warning"),
741 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
742 bold($value),bold($class),bold($name)),
743 WARNING_DIALOG);
744 }
745 }
747 return($match);
748 }
750 static function isCommand($message,$class,$name,$value, $type)
751 {
752 $match = TRUE;
754 // Display the reason for failing this check.
755 if($message && ! $match){
756 msg_dialog::display(_("Warning"),
757 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
758 bold($value),bold($class),bold($name)),
759 WARNING_DIALOG);
760 }
762 return($match);
763 }
765 static function isDn($message,$class,$name,$value, $type)
766 {
767 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
769 // Display the reason for failing this check.
770 if($message && ! $match){
771 msg_dialog::display(_("Warning"),
772 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
773 bold($value),bold($class),bold($name)),
774 WARNING_DIALOG);
775 }
777 return($match);
778 }
780 static function isRdn($message,$class,$name,$value, $type)
781 {
782 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
784 // Display the reason for failing this check.
785 if($message && ! $match){
786 msg_dialog::display(_("Warning"),
787 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
788 bold($value),bold($class),bold($name)),
789 WARNING_DIALOG);
790 }
792 return($match);
793 }
795 private function _restoreCurrentValue()
796 {
797 // First check for values in the LDAP Database.
798 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
799 $this->setStatus('ldap');
800 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
801 return;
802 }
804 // Second check for values in the config file.
805 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
806 $this->setStatus('file');
807 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
808 return;
809 }
811 // If there still wasn't found anything then fallback to the default.
812 if($this->getStatus() == 'undefined'){
813 $this->value = $this->getDefault();
814 }
815 }
817 function getMigrate() { return($this->migrate); }
818 function getCheck() { return($this->check); }
819 function getName() { return($this->name); }
820 function getClass() { return($this->class); }
821 function getGroup() { return($this->group); }
822 function getType() { return($this->type); }
823 function getDescription() { return($this->description); }
824 function getDefault() { return($this->default); }
825 function getDefaults() { return($this->defaults); }
826 function getStatus() { return($this->status); }
827 function isMandatory() { return($this->mandatory); }
829 function setValue($str)
830 {
831 if(in_array($this->getStatus(), array('modified'))){
832 $this->tmp_value = $str;
833 }elseif($this->value != $str){
834 $this->setStatus('modified');
835 $this->tmp_value = $str;
836 }
837 }
839 function getValue($temporary = FALSE)
840 {
841 if($temporary){
842 if(in_array($this->getStatus(), array('modified','removed'))){
843 return($this->tmp_value);
844 }else{
845 return($this->value);
846 }
847 }else{
849 // Do not return ldap values if we've to ignore them.
850 if($this->parent->ignoreLdapProperties){
851 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
852 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
853 }else{
854 return($this->getDefault());
855 }
856 }else{
857 return($this->value);
858 }
859 }
860 }
862 function restoreDefault()
863 {
864 if(in_array($this->getStatus(),array('ldap'))){
865 $this->setStatus('removed');
867 // Second check for values in the config file.
868 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
869 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
870 }else{
871 $this->tmp_value = $this->getDefault();
872 }
873 }
874 }
876 function save()
877 {
878 if($this->getStatus() == 'modified'){
879 $ldap = $this->parent->config->get_ldap_link();
880 $ldap->cd($this->parent->config->current['BASE']);
881 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
882 $ldap->cat($dn);
883 if(!$ldap->count()){
884 $ldap->cd($dn);
885 $data = array(
886 'cn' => $this->class,
887 'objectClass' => array('top','gosaConfig'),
888 'gosaSetting' => $this->name.":".$this->tmp_value);
890 $ldap->add($data);
891 if(!$ldap->success()){
892 echo $ldap->get_error();
893 }
895 }else{
896 $attrs = $ldap->fetch();
897 $data = array();
898 $found = false;
899 if(isset($attrs['gosaSetting']['count'])){
900 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
901 $set = $attrs['gosaSetting'][$i];
902 if(preg_match("/^{$this->name}:/", $set)){
903 $set = "{$this->name}:{$this->tmp_value}";
904 $found = true;
905 }
906 $data['gosaSetting'][] = $set;
907 }
908 }
909 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
910 $ldap->cd($dn);
911 $ldap->modify($data);
912 if(!$ldap->success()){
913 echo $ldap->get_error();
914 }
915 }
916 $this->value = $this->tmp_value;
917 $this->setStatus('ldap');
918 }elseif($this->getStatus() == 'removed'){
919 $ldap = $this->parent->config->get_ldap_link();
920 $ldap->cd($this->parent->config->current['BASE']);
921 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
922 $ldap->cat($dn);
923 $attrs = $ldap->fetch();
924 $data = array('gosaSetting' => array());
925 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
926 $set = $attrs['gosaSetting'][$i];
927 if(preg_match("/^{$this->name}:/", $set)){
928 continue;
929 }
930 $data['gosaSetting'][] = $set;
931 }
932 $ldap->cd($dn);
933 $ldap->modify($data);
934 if(!$ldap->success()){
935 echo $ldap->get_error();
936 }
937 $this->_restoreCurrentValue();
938 }
939 }
941 private function setStatus($state)
942 {
943 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
944 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
945 }else{
946 $this->status = $state;
947 }
948 }
950 function isValid()
951 {
952 return(TRUE);
953 }
954 }
958 interface propertyMigration
959 {
960 function __construct($config,$property);
961 }
964 ?>