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 $this->status = 'finished';
392 }
394 // Register plugin properties.
395 foreach ($this->classesWithInfo as $cname => $def){
397 // Detect class name
398 $name = $cname;
399 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
400 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
402 // Register post events
403 $this->classToName[$cname] = $name;
404 $data = array('name' => 'postcreate','type' => 'command');
405 $this->register($cname, $data);
406 $data = array('name' => 'postremove','type' => 'command');
407 $this->register($cname, $data);
408 $data = array('name' => 'postmodify','type' => 'command');
409 $this->register($cname, $data);
410 $data = array('name' => 'check', 'type' => 'command');
411 $this->register($cname, $data);
413 // Register properties
414 if(isset($def['plProperties'])){
415 foreach($def['plProperties'] as $property){
416 $this->register($cname, $property);
417 }
418 }
419 }
420 }
423 /*! \brief Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
424 * @param String $class The name of the class/plugin that wants to register this property.
425 * @return Array $data An array containing all data set in plInfo['plProperty]
426 */
427 function register($class,$data)
428 {
429 $id = count($this->properties);
430 $this->properties[$id] = new gosaProperty($this,$class,$data);
431 $p = strtolower("{$class}::{$data['name']}");
432 $this->mapByName[$p] = $id;
433 }
436 /*! \brief Returns all registered properties.
437 * @return Array A list of all properties.
438 */
439 public function getAllProperties()
440 {
441 return($this->properties);
442 }
445 /*! \brief Checks whether a property exists or not.
446 * @param String $class The class name (e.g. 'core' or 'mailAccount')
447 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
448 * @return Boolean TRUE if it exists else FALSE.
449 */
450 function propertyExists($class,$name)
451 {
452 $p = strtolower("{$class}::{$name}");
453 return(isset($this->mapByName[$p]));
454 }
457 /*! \brief Returns the id of a registered property.
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 Integer The id for the given property.
461 */
462 private function getId($class,$name)
463 {
464 $p = strtolower("{$class}::{$name}");
465 if(!isset($this->mapByName[$p])){
466 return(-1);
467 }
468 return($this->mapByName[$p]);
469 }
472 /*! \brief Returns 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 or 'NULL' if it doesn't exists.
476 */
477 function getProperty($class,$name)
478 {
479 if($this->propertyExists($class,$name)){
480 return($this->properties[$this->getId($class,$name)]);
481 }
482 return(NULL);
483 }
486 /*! \brief Returns the value for a given property, if it exists.
487 * @param String $class The class name (e.g. 'core' or 'mailAccount')
488 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
489 * @return GOsaPropery The property value or an empty string if it doesn't exists.
490 */
491 function getPropertyValue($class,$name)
492 {
493 if($this->propertyExists($class,$name)){
494 $tmp = $this->getProperty($class,$name);
495 return($tmp->getValue());
496 }
497 return("");
498 }
501 /*! \brief Set a new value for a given property, if it exists.
502 * @param String $class The class name (e.g. 'core' or 'mailAccount')
503 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
504 * @return
505 */
506 function setPropertyValue($class,$name, $value)
507 {
508 if($this->propertyExists($class,$name)){
509 $tmp = $this->getProperty($class,$name);
510 return($tmp->setValue($value));
511 }
512 return("");
513 }
516 /*! \brief Save all temporary made property changes and thus make them useable/effective.
517 * @return Array Returns a list of plugins that have to be migrated before they can be saved.
518 */
519 function saveChanges()
520 {
521 $migrate = array();
522 foreach($this->properties as $prop){
524 // Is this property modified
525 if(in_array($prop->getStatus(),array('modified','removed'))){
527 // Check if we've to migrate something before we can make the changes effective.
528 if($prop->migrationRequired()){
529 $migrate[] = $prop;
530 }else{
531 $prop->save();
532 }
533 }
534 }
535 return($migrate);
536 }
537 }
540 class gosaProperty
541 {
542 protected $name = "";
543 protected $class = "";
544 protected $value = "";
545 protected $tmp_value = ""; // Used when modified but not saved
546 protected $type = "string";
547 protected $default = "";
548 protected $defaults = "";
549 protected $description = "";
550 protected $check = "";
551 protected $migrate = "";
552 protected $mandatory = FALSE;
553 protected $group = "default";
554 protected $parent = NULL;
555 protected $data = array();
557 protected $migrationClass = NULL;
559 /*! The current property status
560 * 'ldap' Property is stored in ldap
561 * 'file' Property is stored in the config file
562 * 'undefined' Property is currently not stored anywhere
563 * 'modified' Property has been modified (should be saved)
564 */
565 protected $status = 'undefined';
567 protected $attributes = array('name','type','default','description','check',
568 'migrate','mandatory','group','defaults');
573 function __construct($parent,$classname,$data)
574 {
575 // Set some basic infos
576 $this->parent = &$parent;
577 $this->class = $classname;
578 $this->data = $data;
580 // Get all relevant information from the data array (comes from plInfo)
581 foreach($this->attributes as $aName){
582 if(isset($data[$aName])){
583 $this->$aName = $data[$aName];
584 }
585 }
587 // Initialize with the current value
588 $this->_restoreCurrentValue();
590 }
592 function migrationRequired()
593 {
594 // Instantiate migration class
595 if(!empty($this->migrate) && $this->migrationClass == NULL){
596 if(!class_available($this->migrate)){
597 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
598 }else{
599 $class = $this->migrate;
600 $tmp = new $class($this->parent->config,$this);
601 if(! $tmp instanceof propertyMigration){
602 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
603 }else{
604 $this->migrationClass = $tmp;
605 }
606 }
607 }
608 if(empty($this->migrate) || $this->migrationClass == NULL){
609 return(FALSE);
610 }
611 return($this->migrationClass->checkForIssues());
612 }
614 function getMigrationClass()
615 {
616 return($this->migrationClass);
617 }
619 function check()
620 {
621 $val = $this->getValue(TRUE);
622 $return = TRUE;
623 if($this->mandatory && empty($val)){
624 $return = FALSE;
625 }
627 $check = $this->getCheck();
628 if(!empty($val) && !empty($check)){
629 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
630 if(!$res){
631 $return = FALSE;
632 }
633 }
634 return($return);
635 }
637 static function isBool($message,$class,$name,$value, $type)
638 {
639 $match = in_array($value,array('true','false',''));
641 // Display the reason for failing this check.
642 if($message && ! $match){
643 msg_dialog::display(_("Warning"),
644 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"),
645 bold($value),bold($class),bold($name)),
646 WARNING_DIALOG);
647 }
649 return($match);
650 }
652 static function isString($message,$class,$name,$value, $type)
653 {
654 $match = TRUE;
656 // Display the reason for failing this check.
657 if($message && ! $match){
658 msg_dialog::display(_("Warning"),
659 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"),
660 bold($value),bold($class),bold($name)),
661 WARNING_DIALOG);
662 }
664 return($match);
665 }
667 static function isInteger($message,$class,$name,$value, $type)
668 {
669 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
671 // Display the reason for failing this check.
672 if($message && ! $match){
673 msg_dialog::display(_("Warning"),
674 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
675 bold($value),bold($class),bold($name)),
676 WARNING_DIALOG);
677 }
679 return($match);
680 }
682 static function isPath($message,$class,$name,$value, $type)
683 {
684 $match = preg_match("#^(/[^/]*/){1}#", $value);
686 // Display the reason for failing this check.
687 if($message && ! $match){
688 msg_dialog::display(_("Warning"),
689 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
690 bold($value),bold($class),bold($name)),
691 WARNING_DIALOG);
692 }
694 return($match);
695 }
697 static function isReadablePath($message,$class,$name,$value, $type)
698 {
699 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
701 // Display the reason for failing this check.
702 if($message && ! $match){
703 if(!is_dir($value)){
704 msg_dialog::display(_("Warning"),
705 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
706 bold($value),bold($class),bold($name)),
707 WARNING_DIALOG);
708 }elseif(!is_readable($value)){
709 msg_dialog::display(_("Warning"),
710 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
711 bold($value),bold($class),bold($name)),
712 WARNING_DIALOG);
713 }
714 }
716 return($match);
717 }
719 static function isWriteablePath($message,$class,$name,$value, $type)
720 {
721 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
723 // Display the reason for failing this check.
724 if($message && ! $match){
725 if(!is_dir($value)){
726 msg_dialog::display(_("Warning"),
727 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
728 bold($value),bold($class),bold($name)),
729 WARNING_DIALOG);
730 }elseif(!is_writeable($value)){
731 msg_dialog::display(_("Warning"),
732 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
733 bold($value),bold($class),bold($name)),
734 WARNING_DIALOG);
735 }
736 }
738 return($match);
739 }
741 static function isReadableFile($message,$class,$name,$value, $type)
742 {
743 $match = !empty($value) && is_readable($value) && is_file($value);
745 // Display the reason for failing this check.
746 if($message && ! $match){
748 if(!is_file($value)){
749 msg_dialog::display(_("Warning"),
750 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
751 bold($value),bold($class),bold($name)),
752 WARNING_DIALOG);
753 }elseif(!is_readable($value)){
754 msg_dialog::display(_("Warning"),
755 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
756 bold($value),bold($class),bold($name)),
757 WARNING_DIALOG);
758 }
759 }
761 return($match);
762 }
764 static function isCommand($message,$class,$name,$value, $type)
765 {
766 $match = TRUE;
768 // Display the reason for failing this check.
769 if($message && ! $match){
770 msg_dialog::display(_("Warning"),
771 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
772 bold($value),bold($class),bold($name)),
773 WARNING_DIALOG);
774 }
776 return($match);
777 }
779 static function isDn($message,$class,$name,$value, $type)
780 {
781 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
783 // Display the reason for failing this check.
784 if($message && ! $match){
785 msg_dialog::display(_("Warning"),
786 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
787 bold($value),bold($class),bold($name)),
788 WARNING_DIALOG);
789 }
791 return($match);
792 }
794 static function isRdn($message,$class,$name,$value, $type)
795 {
796 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
798 // Display the reason for failing this check.
799 if($message && ! $match){
800 msg_dialog::display(_("Warning"),
801 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
802 bold($value),bold($class),bold($name)),
803 WARNING_DIALOG);
804 }
806 return($match);
807 }
809 private function _restoreCurrentValue()
810 {
811 // First check for values in the LDAP Database.
812 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
813 $this->setStatus('ldap');
814 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
815 return;
816 }
818 // Second check for values in the config file.
819 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
820 $this->setStatus('file');
821 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
822 return;
823 }
825 // If there still wasn't found anything then fallback to the default.
826 if($this->getStatus() == 'undefined'){
827 $this->value = $this->getDefault();
828 }
829 }
831 function getMigrate() { return($this->migrate); }
832 function getCheck() { return($this->check); }
833 function getName() { return($this->name); }
834 function getClass() { return($this->class); }
835 function getGroup() { return($this->group); }
836 function getType() { return($this->type); }
837 function getDescription() { return($this->description); }
838 function getDefault() { return($this->default); }
839 function getDefaults() { return($this->defaults); }
840 function getStatus() { return($this->status); }
841 function isMandatory() { return($this->mandatory); }
843 function setValue($str)
844 {
845 if(in_array($this->getStatus(), array('modified'))){
846 $this->tmp_value = $str;
847 }elseif($this->value != $str){
848 $this->setStatus('modified');
849 $this->tmp_value = $str;
850 }
851 }
853 function getValue($temporary = FALSE)
854 {
855 if($temporary){
856 if(in_array($this->getStatus(), array('modified','removed'))){
857 return($this->tmp_value);
858 }else{
859 return($this->value);
860 }
861 }else{
863 // Do not return ldap values if we've to ignore them.
864 if($this->parent->ignoreLdapProperties){
865 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
866 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
867 }else{
868 return($this->getDefault());
869 }
870 }else{
871 return($this->value);
872 }
873 }
874 }
876 function restoreDefault()
877 {
878 if(in_array($this->getStatus(),array('ldap'))){
879 $this->setStatus('removed');
881 // Second check for values in the config file.
882 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
883 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
884 }else{
885 $this->tmp_value = $this->getDefault();
886 }
887 }
888 }
890 function save()
891 {
892 if($this->getStatus() == 'modified'){
893 $ldap = $this->parent->config->get_ldap_link();
894 $ldap->cd($this->parent->config->current['BASE']);
895 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
896 $ldap->cat($dn);
897 if(!$ldap->count()){
898 $ldap->cd($dn);
899 $data = array(
900 'cn' => $this->class,
901 'objectClass' => array('top','gosaConfig'),
902 'gosaSetting' => $this->name.":".$this->tmp_value);
904 $ldap->add($data);
905 if(!$ldap->success()){
906 echo $ldap->get_error();
907 }
909 }else{
910 $attrs = $ldap->fetch();
911 $data = array();
912 $found = false;
913 if(isset($attrs['gosaSetting']['count'])){
914 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
915 $set = $attrs['gosaSetting'][$i];
916 if(preg_match("/^{$this->name}:/", $set)){
917 $set = "{$this->name}:{$this->tmp_value}";
918 $found = true;
919 }
920 $data['gosaSetting'][] = $set;
921 }
922 }
923 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
924 $ldap->cd($dn);
925 $ldap->modify($data);
926 if(!$ldap->success()){
927 echo $ldap->get_error();
928 }
929 }
930 $this->value = $this->tmp_value;
931 $this->setStatus('ldap');
932 }elseif($this->getStatus() == 'removed'){
933 $ldap = $this->parent->config->get_ldap_link();
934 $ldap->cd($this->parent->config->current['BASE']);
935 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
936 $ldap->cat($dn);
937 $attrs = $ldap->fetch();
938 $data = array('gosaSetting' => array());
939 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
940 $set = $attrs['gosaSetting'][$i];
941 if(preg_match("/^{$this->name}:/", $set)){
942 continue;
943 }
944 $data['gosaSetting'][] = $set;
945 }
946 $ldap->cd($dn);
947 $ldap->modify($data);
948 if(!$ldap->success()){
949 echo $ldap->get_error();
950 }
951 $this->_restoreCurrentValue();
952 }
953 }
955 private function setStatus($state)
956 {
957 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
958 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
959 }else{
960 $this->status = $state;
961 }
962 }
964 function isValid()
965 {
966 return(TRUE);
967 }
968 }
972 interface propertyMigration
973 {
974 function __construct($config,$property);
975 }
978 ?>