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 // Check only if required plugin is enabled in gosa.conf
142 // Normally this is the class name itself, but may be overridden
143 // in plInfo using the plRequirements::activePlugin statement.
144 $requiresActivePlugin = $cname;
145 if(isset($defs['plRequirements']['activePlugin'])){
146 $requiresActivePlugin = $defs['plRequirements']['activePlugin'];
147 }
149 // Only queue checks for active plugins.
150 if(isset($this->activePlugins[strtolower($requiresActivePlugin)])){
151 $this->pluginRequirements[$cname] = $defs['plRequirements'];
152 }else{
153 if($cname == $requiresActivePlugin){
154 new log("debug","","Skipped schema check for '{$cname}' plugin is inactive!",
155 array(),'');
156 }else{
157 new log("debug","","Skipped schema check for class '{$cname}' skipped,".
158 " required plugin '{$requiresActivePlugin}' is inactive!",
159 array(),'');
160 }
161 }
162 }
163 }
165 // Check schema requirements now $missing = $invalid = array();
166 foreach($this->pluginRequirements as $cname => $requirements){
168 // Check LDAP schema requirements for this plugins
169 $failure = FALSE;
170 if(isset($requirements['ldapSchema'])){
171 foreach($requirements['ldapSchema'] as $oc => $version){
172 if(!$this->ocAvailable($oc)){
173 $this->detectedSchemaIssues['missing'][$oc] = $oc;
175 $this->schemaCheckFailed = TRUE;
176 $failure = TRUE;
178 new log("debug","","LDAP objectClass missing '{$oc}'!",
179 array(),'');
181 }elseif(!empty($version)){
182 $currentVersion = $this->getObjectClassVersion($oc);
183 if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
184 if($currentVersion == -1){
185 $currentVersion = _("unknown");
186 }
187 $this->detectedSchemaIssues['versionMismatch'][$oc] =
188 sprintf(_("%s has version %s but %s is required!"), bold($oc),bold($currentVersion),bold($version));
189 $this->schemaCheckFailed = TRUE;
190 $failure = TRUE;
192 new log("debug","","LDAP objectClass version mismatch '{$oc}' ".
193 "has '{$currentVersion}' but {$version} required!",
194 array(),'');
195 }
196 }
197 }
198 }
200 // Display corresponding plugins now
201 if($disableIncompatiblePlugins && $failure && isset($requirements['onFailureDisablePlugin'])){
202 foreach($requirements['onFailureDisablePlugin'] as $name){
203 $this->pluginsDeactivated[$name] = $name;
204 }
205 }
206 }
207 $this->schemaCheckFinished =TRUE;
208 session::un_set('plist');
209 return(!$this->schemaCheckFailed);
210 }
213 /*! \brief The function 'validateSchemata' may has disabled some GOsa-Plugins,
214 * the list of disabled plugins will be returned here.
215 * @return Array The list of plugins disabled by 'validateSchemata'
216 */
217 function getDisabledPlugins()
218 {
219 return($this->pluginsDeactivated);
220 }
223 /*! \brief Displays an error message with all issues detect during the schema validation.
224 */
225 function displayRequirementErrors()
226 {
227 $message = "";
228 if(count($this->detectedSchemaIssues['missing'])){
229 $message.= "<br>".
230 _("The following object classes are missing:").
231 "<div class='scrollContainer' style='height:100px'>".
232 msgPool::buildList(array_values($this->detectedSchemaIssues['missing'])).
233 "</div>";
234 }
235 if(count($this->detectedSchemaIssues['versionMismatch'])){
236 $message.= "<br>".
237 _("The following object classes are outdated:").
238 "<div class='scrollContainer' style='height:100px'>".
239 msgPool::buildList(array_values($this->detectedSchemaIssues['versionMismatch'])).
240 "</div>";
241 }
242 if($message != ""){
243 $message.= "<br>"._("Plugins that require one or more of the object classes above will be disabled until the object classes get updated.");
245 msg_dialog::display(_("Schema validation error"),$message, ERROR_DIALOG);
246 }
247 }
250 /*! \brief Checks to version strings (e.g. '>=v2.8' and '2.9')
251 * @param String The required version with operators (e.g. '>=2.8')
252 * @param String The version to match for withOUT operators (e.g. '2.9')
253 * @return Boolean True if version matches else false.
254 */
255 private function ocVersionMatch($required, $installed)
256 {
257 $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
258 $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
259 return(version_compare($installed,$required, $operator));
260 }
263 /*! \brief Returns the currently installed version of a given object class.
264 * @param String The name of the objectClass to check for.
265 * @return String The version string of the objectClass (e.g. v2.7)
266 */
267 function getObjectClassVersion($oc)
268 {
269 if(!isset($this->objectClasses[$oc])){
270 return(NULL);
271 }else{
272 $version = -1; // unknown
273 if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
274 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
275 }
276 }
277 return($version);
278 }
281 /*! \brief Check whether the given object class is available or not.
282 * @param String The name of the objectClass to check for (e.g. 'mailAccount')
283 * @return Boolean Returns TRUE if the class exists else FALSE.
284 */
285 function ocAvailable($name)
286 {
287 return(isset($this->objectClasses[$name]));
288 }
291 /*! \brief Re-loads the list of installed GOsa-Properties.
292 * @param Boolean $force If force is TRUE, the complete properties list is rebuild..
293 */
294 function reload($force = FALSE)
295 {
296 // Do not reload the properties everytime, once we have
297 // everything loaded and registrered skip the reload.
298 // Status is 'finished' once we had a ldap connection (logged in)
299 if(!$force && $this->status == 'finished') return;
301 // Reset everything
302 $this->ldapStoredProperties = array();
303 $this->fileStoredProperties = array();
304 $this->properties = array();
305 $this->mapByName = array();
306 $this->activePlugins = array('core'=>'core');
308 if(!$this->config) return;
310 // Search for config flags defined in the config file (TAB section)
311 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
312 foreach($tabdefs as $info){
314 // Put plugin in list of active plugins
315 if(isset($info['CLASS'])){
316 $class = strtolower($info['CLASS']);
317 $this->activePlugins[$class] = $class;
318 }
320 // Check if the info is valid
321 if(isset($info['NAME']) && isset($info['CLASS'])){
324 // Check if there is nore than just the plugin definition
325 if(count($info) > 2){
326 foreach($info as $name => $value){
328 if(!in_array($name, array('CLASS','NAME'))){
329 $class= $info['CLASS'];
330 $this->fileStoredProperties[$class][strtolower($name)] = $value;
331 }
332 }
333 }
334 }
335 }
336 }
338 // Search for config flags defined in the config file (MENU section)
339 foreach($this->config->data['MENU'] as $section => $entries){
340 foreach($entries as $entry){
342 if(isset($entry['CLASS'])){
344 // Put plugin to active plugins list.
345 $class = strtolower($entry['CLASS']);
346 $this->activePlugins[$class] = $class;
348 if(count($entry) > 2 ){
349 foreach($entry as $name => $value){
350 if(!in_array($name, array('CLASS','ACL'))){
351 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
352 }
353 }
354 }
355 }
356 }
357 }
359 // Search for config flags defined in the config file (MAIN section)
360 foreach($this->config->data['MAIN'] as $name => $value){
361 $this->fileStoredProperties['core'][strtolower($name)] = $value;
362 }
364 // Search for config flags defined in the config file (Current LOCATION section)
365 if(isset($this->config->current)){
366 foreach($this->config->current as $name => $value){
367 $this->fileStoredProperties['core'][strtolower($name)] = $value;
368 }
369 }
371 // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
372 // in the config.
373 $this->ignoreLdapProperties = FALSE;
374 if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) &&
375 preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
376 $this->ignoreLdapProperties = TRUE;
377 }
379 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
380 if(!empty($this->config->current['CONFIG'])){
381 $ldap = $this->config->get_ldap_link();
382 $ldap->cd($this->config->current['CONFIG']);
383 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
384 while($attrs = $ldap->fetch()){
385 $class = $attrs['cn'][0];
386 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
387 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
388 $this->ldapStoredProperties[$class][$name] = $value;
389 }
390 }
391 }
393 // Register plugin properties.
394 foreach ($this->classesWithInfo as $cname => $def){
396 // Detect class name
397 $name = $cname;
398 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
399 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
401 // Register post events
402 $this->classToName[$cname] = $name;
403 $data = array('name' => 'postcreate','type' => 'command');
404 $this->register($cname, $data);
405 $data = array('name' => 'postremove','type' => 'command');
406 $this->register($cname, $data);
407 $data = array('name' => 'postmodify','type' => 'command');
408 $this->register($cname, $data);
409 $data = array('name' => 'check', 'type' => 'command');
410 $this->register($cname, $data);
412 // Register properties
413 if(isset($def['plProperties'])){
414 foreach($def['plProperties'] as $property){
415 $this->register($cname, $property);
416 }
417 }
418 }
420 // We are only finsihed once we are logged in.
421 if(!empty($this->config->current['CONFIG'])){
422 $this->status = 'finished';
423 }
424 }
427 /*! \brief Returns TRUE if the property registration has finished without any error.
428 */
429 function propertyInitializationComplete()
430 {
431 return($this->status == 'finished');
432 }
435 /*! \brief Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
436 * @param String $class The name of the class/plugin that wants to register this property.
437 * @return Array $data An array containing all data set in plInfo['plProperty]
438 */
439 function register($class,$data)
440 {
441 $id = count($this->properties);
442 $this->properties[$id] = new gosaProperty($this,$class,$data);
443 $p = strtolower("{$class}::{$data['name']}");
444 $this->mapByName[$p] = $id;
445 }
448 /*! \brief Returns all registered properties.
449 * @return Array A list of all properties.
450 */
451 public function getAllProperties()
452 {
453 return($this->properties);
454 }
457 /*! \brief Checks whether a property exists or not.
458 * @param String $class The class name (e.g. 'core' or 'mailAccount')
459 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
460 * @return Boolean TRUE if it exists else FALSE.
461 */
462 function propertyExists($class,$name)
463 {
464 $p = strtolower("{$class}::{$name}");
465 return(isset($this->mapByName[$p]));
466 }
469 /*! \brief Returns the id of a registered property.
470 * @param String $class The class name (e.g. 'core' or 'mailAccount')
471 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
472 * @return Integer The id for the given property.
473 */
474 private function getId($class,$name)
475 {
476 $p = strtolower("{$class}::{$name}");
477 if(!isset($this->mapByName[$p])){
478 return(-1);
479 }
480 return($this->mapByName[$p]);
481 }
484 /*! \brief Returns a given property, if it exists.
485 * @param String $class The class name (e.g. 'core' or 'mailAccount')
486 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
487 * @return GOsaPropery The property or 'NULL' if it doesn't exists.
488 */
489 function getProperty($class,$name)
490 {
491 if($this->propertyExists($class,$name)){
492 return($this->properties[$this->getId($class,$name)]);
493 }
494 return(NULL);
495 }
498 /*! \brief Returns the value for a given property, if it exists.
499 * @param String $class The class name (e.g. 'core' or 'mailAccount')
500 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
501 * @return GOsaPropery The property value or an empty string if it doesn't exists.
502 */
503 function getPropertyValue($class,$name)
504 {
505 if($this->propertyExists($class,$name)){
506 $tmp = $this->getProperty($class,$name);
507 return($tmp->getValue());
508 }
509 return("");
510 }
513 /*! \brief Set a new value for a given property, if it exists.
514 * @param String $class The class name (e.g. 'core' or 'mailAccount')
515 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
516 * @return
517 */
518 function setPropertyValue($class,$name, $value)
519 {
520 if($this->propertyExists($class,$name)){
521 $tmp = $this->getProperty($class,$name);
522 return($tmp->setValue($value));
523 }
524 return("");
525 }
528 /*! \brief Save all temporary made property changes and thus make them useable/effective.
529 * @return Array Returns a list of plugins that have to be migrated before they can be saved.
530 */
531 function saveChanges()
532 {
533 $migrate = array();
534 foreach($this->properties as $prop){
536 // Is this property modified
537 if(in_array($prop->getStatus(),array('modified','removed'))){
539 // Check if we've to migrate something before we can make the changes effective.
540 if($prop->migrationRequired()){
541 $migrate[] = $prop;
542 }else{
543 $prop->save();
544 }
545 }
546 }
547 return($migrate);
548 }
549 }
552 class gosaProperty
553 {
554 protected $name = "";
555 protected $class = "";
556 protected $value = "";
557 protected $tmp_value = ""; // Used when modified but not saved
558 protected $type = "string";
559 protected $default = "";
560 protected $defaults = "";
561 protected $description = "";
562 protected $check = "";
563 protected $migrate = "";
564 protected $mandatory = FALSE;
565 protected $group = "default";
566 protected $parent = NULL;
567 protected $data = array();
569 protected $migrationClass = NULL;
571 /*! The current property status
572 * 'ldap' Property is stored in ldap
573 * 'file' Property is stored in the config file
574 * 'undefined' Property is currently not stored anywhere
575 * 'modified' Property has been modified (should be saved)
576 */
577 protected $status = 'undefined';
579 protected $attributes = array('name','type','default','description','check',
580 'migrate','mandatory','group','defaults');
585 function __construct($parent,$classname,$data)
586 {
587 // Set some basic infos
588 $this->parent = &$parent;
589 $this->class = $classname;
590 $this->data = $data;
592 // Get all relevant information from the data array (comes from plInfo)
593 foreach($this->attributes as $aName){
594 if(isset($data[$aName])){
595 $this->$aName = $data[$aName];
596 }
597 }
599 // Initialize with the current value
600 $this->_restoreCurrentValue();
602 }
604 function migrationRequired()
605 {
606 // Instantiate migration class
607 if(!empty($this->migrate) && $this->migrationClass == NULL){
608 if(!class_available($this->migrate)){
609 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
610 }else{
611 $class = $this->migrate;
612 $tmp = new $class($this->parent->config,$this);
613 if(! $tmp instanceof propertyMigration){
614 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
615 }else{
616 $this->migrationClass = $tmp;
617 }
618 }
619 }
620 if(empty($this->migrate) || $this->migrationClass == NULL){
621 return(FALSE);
622 }
623 return($this->migrationClass->checkForIssues());
624 }
626 function getMigrationClass()
627 {
628 return($this->migrationClass);
629 }
631 function check()
632 {
633 $val = $this->getValue(TRUE);
634 $return = TRUE;
635 if($this->mandatory && empty($val)){
636 $return = FALSE;
637 }
639 $check = $this->getCheck();
640 if(!empty($val) && !empty($check)){
641 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
642 if(!$res){
643 $return = FALSE;
644 }
645 }
646 return($return);
647 }
649 static function isBool($message,$class,$name,$value, $type)
650 {
651 $match = in_array($value,array('true','false',''));
653 // Display the reason for failing this check.
654 if($message && ! $match){
655 msg_dialog::display(_("Warning"),
656 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"),
657 bold($value),bold($class),bold($name)),
658 WARNING_DIALOG);
659 }
661 return($match);
662 }
664 static function isString($message,$class,$name,$value, $type)
665 {
666 $match = TRUE;
668 // Display the reason for failing this check.
669 if($message && ! $match){
670 msg_dialog::display(_("Warning"),
671 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"),
672 bold($value),bold($class),bold($name)),
673 WARNING_DIALOG);
674 }
676 return($match);
677 }
679 static function isInteger($message,$class,$name,$value, $type)
680 {
681 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
683 // Display the reason for failing this check.
684 if($message && ! $match){
685 msg_dialog::display(_("Warning"),
686 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
687 bold($value),bold($class),bold($name)),
688 WARNING_DIALOG);
689 }
691 return($match);
692 }
694 static function isPath($message,$class,$name,$value, $type)
695 {
696 $match = preg_match("#^(/[^/]*/){1}#", $value);
698 // Display the reason for failing this check.
699 if($message && ! $match){
700 msg_dialog::display(_("Warning"),
701 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
702 bold($value),bold($class),bold($name)),
703 WARNING_DIALOG);
704 }
706 return($match);
707 }
709 static function isReadablePath($message,$class,$name,$value, $type)
710 {
711 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
713 // Display the reason for failing this check.
714 if($message && ! $match){
715 if(!is_dir($value)){
716 msg_dialog::display(_("Warning"),
717 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
718 bold($value),bold($class),bold($name)),
719 WARNING_DIALOG);
720 }elseif(!is_readable($value)){
721 msg_dialog::display(_("Warning"),
722 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
723 bold($value),bold($class),bold($name)),
724 WARNING_DIALOG);
725 }
726 }
728 return($match);
729 }
731 static function isWriteablePath($message,$class,$name,$value, $type)
732 {
733 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
735 // Display the reason for failing this check.
736 if($message && ! $match){
737 if(!is_dir($value)){
738 msg_dialog::display(_("Warning"),
739 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
740 bold($value),bold($class),bold($name)),
741 WARNING_DIALOG);
742 }elseif(!is_writeable($value)){
743 msg_dialog::display(_("Warning"),
744 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
745 bold($value),bold($class),bold($name)),
746 WARNING_DIALOG);
747 }
748 }
750 return($match);
751 }
753 static function isReadableFile($message,$class,$name,$value, $type)
754 {
755 $match = !empty($value) && is_readable($value) && is_file($value);
757 // Display the reason for failing this check.
758 if($message && ! $match){
760 if(!is_file($value)){
761 msg_dialog::display(_("Warning"),
762 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
763 bold($value),bold($class),bold($name)),
764 WARNING_DIALOG);
765 }elseif(!is_readable($value)){
766 msg_dialog::display(_("Warning"),
767 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
768 bold($value),bold($class),bold($name)),
769 WARNING_DIALOG);
770 }
771 }
773 return($match);
774 }
776 static function isCommand($message,$class,$name,$value, $type)
777 {
778 $match = TRUE;
780 // Display the reason for failing this check.
781 if($message && ! $match){
782 msg_dialog::display(_("Warning"),
783 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
784 bold($value),bold($class),bold($name)),
785 WARNING_DIALOG);
786 }
788 return($match);
789 }
791 static function isDn($message,$class,$name,$value, $type)
792 {
793 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
795 // Display the reason for failing this check.
796 if($message && ! $match){
797 msg_dialog::display(_("Warning"),
798 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
799 bold($value),bold($class),bold($name)),
800 WARNING_DIALOG);
801 }
803 return($match);
804 }
806 static function isRdn($message,$class,$name,$value, $type)
807 {
808 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
810 // Display the reason for failing this check.
811 if($message && ! $match){
812 msg_dialog::display(_("Warning"),
813 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
814 bold($value),bold($class),bold($name)),
815 WARNING_DIALOG);
816 }
818 return($match);
819 }
821 private function _restoreCurrentValue()
822 {
823 // First check for values in the LDAP Database.
824 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
825 $this->setStatus('ldap');
826 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
827 return;
828 }
830 // Second check for values in the config file.
831 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
832 $this->setStatus('file');
833 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
834 return;
835 }
837 // If there still wasn't found anything then fallback to the default.
838 if($this->getStatus() == 'undefined'){
839 $this->value = $this->getDefault();
840 }
841 }
843 function getMigrate() { return($this->migrate); }
844 function getCheck() { return($this->check); }
845 function getName() { return($this->name); }
846 function getClass() { return($this->class); }
847 function getGroup() { return($this->group); }
848 function getType() { return($this->type); }
849 function getDescription() { return($this->description); }
850 function getDefault() { return($this->default); }
851 function getDefaults() { return($this->defaults); }
852 function getStatus() { return($this->status); }
853 function isMandatory() { return($this->mandatory); }
855 function setValue($str)
856 {
857 if(in_array($this->getStatus(), array('modified'))){
858 $this->tmp_value = $str;
859 }elseif($this->value != $str){
860 $this->setStatus('modified');
861 $this->tmp_value = $str;
862 }
863 }
865 function getValue($temporary = FALSE)
866 {
867 if($temporary){
868 if(in_array($this->getStatus(), array('modified','removed'))){
869 return($this->tmp_value);
870 }else{
871 return($this->value);
872 }
873 }else{
875 // Do not return ldap values if we've to ignore them.
876 if($this->parent->ignoreLdapProperties){
877 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
878 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
879 }else{
880 return($this->getDefault());
881 }
882 }else{
883 return($this->value);
884 }
885 }
886 }
888 function restoreDefault()
889 {
890 if(in_array($this->getStatus(),array('ldap'))){
891 $this->setStatus('removed');
893 // Second check for values in the config file.
894 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
895 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
896 }else{
897 $this->tmp_value = $this->getDefault();
898 }
899 }
900 }
902 function save()
903 {
904 if($this->getStatus() == 'modified'){
905 $ldap = $this->parent->config->get_ldap_link();
906 $ldap->cd($this->parent->config->current['BASE']);
907 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
908 $ldap->cat($dn);
909 if(!$ldap->count()){
910 $ldap->cd($dn);
911 $data = array(
912 'cn' => $this->class,
913 'objectClass' => array('top','gosaConfig'),
914 'gosaSetting' => $this->name.":".$this->tmp_value);
916 $ldap->add($data);
917 if(!$ldap->success()){
918 echo $ldap->get_error();
919 }
921 }else{
922 $attrs = $ldap->fetch();
923 $data = array();
924 $found = false;
925 if(isset($attrs['gosaSetting']['count'])){
926 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
927 $set = $attrs['gosaSetting'][$i];
928 if(preg_match("/^{$this->name}:/", $set)){
929 $set = "{$this->name}:{$this->tmp_value}";
930 $found = true;
931 }
932 $data['gosaSetting'][] = $set;
933 }
934 }
935 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
936 $ldap->cd($dn);
937 $ldap->modify($data);
938 if(!$ldap->success()){
939 echo $ldap->get_error();
940 }
941 }
942 $this->value = $this->tmp_value;
943 $this->setStatus('ldap');
944 }elseif($this->getStatus() == 'removed'){
945 $ldap = $this->parent->config->get_ldap_link();
946 $ldap->cd($this->parent->config->current['BASE']);
947 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
948 $ldap->cat($dn);
949 $attrs = $ldap->fetch();
950 $data = array('gosaSetting' => array());
951 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
952 $set = $attrs['gosaSetting'][$i];
953 if(preg_match("/^{$this->name}:/", $set)){
954 continue;
955 }
956 $data['gosaSetting'][] = $set;
957 }
958 $ldap->cd($dn);
959 $ldap->modify($data);
960 if(!$ldap->success()){
961 echo $ldap->get_error();
962 }
963 $this->_restoreCurrentValue();
964 }
965 }
967 private function setStatus($state)
968 {
969 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
970 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
971 }else{
972 $this->status = $state;
973 }
974 }
976 function isValid()
977 {
978 return(TRUE);
979 }
980 }
984 interface propertyMigration
985 {
986 function __construct($config,$property);
987 }
990 ?>