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 Returns a list of plugins used by GOsa.
62 @return Array An array containing all plugins with theis plInfo data.
63 */
64 function getListOfPlugins()
65 {
66 return($this->classesWithInfo);
67 }
70 /*! \brief Checks whether the schema check was called in the current session or not.
71 * @return Boolean True if check was already called
72 */
73 function schemaCheckFinished()
74 {
75 return($this->schemaCheckFinished);
76 }
79 /*! \brief Starts the schema validation
80 * @param Boolean 'force' Force a re-check.
81 * @param Boolean 'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
82 * @return Boolean True on success else FALSE
83 */
84 function validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE, $objectClassesToUse = array())
85 {
86 // Read objectClasses from ldap
87 if(count($objectClassesToUse)){
88 $this->setObjectClasses($objectClassesToUse);
89 }elseif(!count($this->objectClasses)){
90 $ldap = $this->config->get_ldap_link();
91 $ldap->cd($this->config->current['BASE']);
92 $this->setObjectClasses($ldap->get_objectclasses());
93 }
95 return($this->_validateSchemata($force, $disableIncompatiblePlugins));
96 }
99 /*! \brief Sets the list object classes to use while validation the schema. (See 'validateSchemata')
100 * This is called from the GOsa-Setup
101 * @param Array The list of object classes (usually LDAP::get_objectlclasses()).
102 * @return void
103 */
104 function setObjectClasses($ocs)
105 {
106 $this->objectClasses = $ocs;
107 }
110 /*! \brief Returns an array which contains all unresolved schemata requirements.
111 * @return Array An array containing all errors/issues
112 */
113 function getSchemaResults()
114 {
115 return($this->detectedSchemaIssues);
116 }
119 /*! \brief This method checks if the installed ldap-schemata matches the plugin requirements.
120 * @param Boolean 'force' Force a re-check.
121 * @param Boolean 'disableIncompatiblePlugins' Disables of incompatible GOsa-plugins.
122 * @return String
123 */
124 private function _validateSchemata($force = FALSE, $disableIncompatiblePlugins = FALSE)
125 {
126 // We cannot check without readable schema info
127 if(!count($this->objectClasses)){
128 return(TRUE);
129 }
131 // Don't do things twice unless forced
132 if($this->schemaCheckFinished && !$force) return($this->schemaCheckFailed);
134 // Prepare result array
135 $this->detectedSchemaIssues = array();
136 $this->detectedSchemaIssues['missing'] = array();
137 $this->detectedSchemaIssues['versionMismatch'] = array();
139 // Clear last results
140 $this->pluginsDeactivated = array();
142 // Collect required schema infos
143 $this->pluginRequirements = array('ldapSchema' => array());
144 $this->categoryToClass = array();
146 // Walk through plugins with requirements, but only check for active plugins.
147 foreach($this->classesWithInfo as $cname => $defs){
148 if(isset($defs['plRequirements'])){
150 // Check only if required plugin is enabled in gosa.conf
151 // Normally this is the class name itself, but may be overridden
152 // in plInfo using the plRequirements::activePlugin statement.
153 $requiresActivePlugin = $cname;
154 if(isset($defs['plRequirements']['activePlugin'])){
155 $requiresActivePlugin = $defs['plRequirements']['activePlugin'];
156 }
158 // Only queue checks for active plugins.
159 if(isset($this->activePlugins[strtolower($requiresActivePlugin)])){
160 $this->pluginRequirements[$cname] = $defs['plRequirements'];
161 }else{
162 if($cname == $requiresActivePlugin){
163 new log("debug","","Skipped schema check for '{$cname}' plugin is inactive!",
164 array(),'');
165 }else{
166 new log("debug","","Skipped schema check for class '{$cname}' skipped,".
167 " required plugin '{$requiresActivePlugin}' is inactive!",
168 array(),'');
169 }
170 }
171 }
172 }
174 // Check schema requirements now $missing = $invalid = array();
175 foreach($this->pluginRequirements as $cname => $requirements){
177 // Check LDAP schema requirements for this plugins
178 $failure = FALSE;
179 if(isset($requirements['ldapSchema'])){
180 foreach($requirements['ldapSchema'] as $oc => $version){
181 if(!$this->ocAvailable($oc)){
182 $this->detectedSchemaIssues['missing'][$oc] = $oc;
184 $this->schemaCheckFailed = TRUE;
185 $failure = TRUE;
187 new log("debug","","LDAP objectClass missing '{$oc}'!",
188 array(),'');
190 }elseif(!empty($version)){
191 $currentVersion = $this->getObjectClassVersion($oc);
192 if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
193 if($currentVersion == -1){
194 $currentVersion = _("unknown");
195 }
196 $this->detectedSchemaIssues['versionMismatch'][$oc] =
197 sprintf(_("%s has version %s but %s is required!"), bold($oc),bold($currentVersion),bold($version));
198 $this->schemaCheckFailed = TRUE;
199 $failure = TRUE;
201 new log("debug","","LDAP objectClass version mismatch '{$oc}' ".
202 "has '{$currentVersion}' but {$version} required!",
203 array(),'');
204 }
205 }
206 }
207 }
209 // Display corresponding plugins now
210 if($disableIncompatiblePlugins && $failure && isset($requirements['onFailureDisablePlugin'])){
211 foreach($requirements['onFailureDisablePlugin'] as $name){
212 $this->pluginsDeactivated[$name] = $name;
213 }
214 }
215 }
216 $this->schemaCheckFinished =TRUE;
217 session::un_set('plist');
218 return(!$this->schemaCheckFailed);
219 }
222 /*! \brief The function 'validateSchemata' may has disabled some GOsa-Plugins,
223 * the list of disabled plugins will be returned here.
224 * @return Array The list of plugins disabled by 'validateSchemata'
225 */
226 function getDisabledPlugins()
227 {
228 return($this->pluginsDeactivated);
229 }
232 /*! \brief Displays an error message with all issues detect during the schema validation.
233 */
234 function displayRequirementErrors()
235 {
236 $message = "";
237 if(count($this->detectedSchemaIssues['missing'])){
238 $message.= "<br>".
239 _("The following object classes are missing:").
240 "<div class='scrollContainer' style='height:100px'>".
241 msgPool::buildList(array_values($this->detectedSchemaIssues['missing'])).
242 "</div>";
243 }
244 if(count($this->detectedSchemaIssues['versionMismatch'])){
245 $message.= "<br>".
246 _("The following object classes are outdated:").
247 "<div class='scrollContainer' style='height:100px'>".
248 msgPool::buildList(array_values($this->detectedSchemaIssues['versionMismatch'])).
249 "</div>";
250 }
251 if($message != ""){
252 $message.= "<br>"._("Plugins that require one or more of the object classes above will be disabled until the object classes get updated.");
254 msg_dialog::display(_("Schema validation error"),$message, ERROR_DIALOG);
255 }
256 }
259 /*! \brief Checks to version strings (e.g. '>=v2.8' and '2.9')
260 * @param String The required version with operators (e.g. '>=2.8')
261 * @param String The version to match for withOUT operators (e.g. '2.9')
262 * @return Boolean True if version matches else false.
263 */
264 private function ocVersionMatch($required, $installed)
265 {
266 $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
267 $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
268 return(version_compare($installed,$required, $operator));
269 }
272 /*! \brief Returns the currently installed version of a given object class.
273 * @param String The name of the objectClass to check for.
274 * @return String The version string of the objectClass (e.g. v2.7)
275 */
276 function getObjectClassVersion($oc)
277 {
278 if(!isset($this->objectClasses[$oc])){
279 return(NULL);
280 }else{
281 $version = -1; // unknown
282 if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
283 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
284 }
285 }
286 return($version);
287 }
290 /*! \brief Check whether the given object class is available or not.
291 * @param String The name of the objectClass to check for (e.g. 'mailAccount')
292 * @return Boolean Returns TRUE if the class exists else FALSE.
293 */
294 function ocAvailable($name)
295 {
296 return(isset($this->objectClasses[$name]));
297 }
300 /*! \brief Re-loads the list of installed GOsa-Properties.
301 * @param Boolean $force If force is TRUE, the complete properties list is rebuild..
302 */
303 function reload($force = FALSE)
304 {
305 // Do not reload the properties everytime, once we have
306 // everything loaded and registrered skip the reload.
307 // Status is 'finished' once we had a ldap connection (logged in)
308 if(!$force && $this->status == 'finished') return;
310 // Reset everything
311 $this->ldapStoredProperties = array();
312 $this->fileStoredProperties = array();
313 $this->properties = array();
314 $this->mapByName = array();
315 $this->activePlugins = array('core'=>'core');
317 if(!$this->config) return;
319 // Search for config flags defined in the config file (TAB section)
320 foreach($this->config->data['TABS'] as $tabname => $tabdefs){
321 foreach($tabdefs as $info){
323 // Put plugin in list of active plugins
324 if(isset($info['CLASS'])){
325 $class = strtolower($info['CLASS']);
326 $this->activePlugins[$class] = $class;
327 }
329 // Check if the info is valid
330 if(isset($info['NAME']) && isset($info['CLASS'])){
333 // Check if there is nore than just the plugin definition
334 if(count($info) > 2){
335 foreach($info as $name => $value){
337 if(!in_array($name, array('CLASS','NAME'))){
338 $class= $info['CLASS'];
339 $this->fileStoredProperties[$class][strtolower($name)] = $value;
340 }
341 }
342 }
343 }
344 }
345 }
347 // Search for config flags defined in the config file (MENU section)
348 foreach($this->config->data['MENU'] as $section => $entries){
349 foreach($entries as $entry){
351 if(isset($entry['CLASS'])){
353 // Put plugin to active plugins list.
354 $class = strtolower($entry['CLASS']);
355 $this->activePlugins[$class] = $class;
357 if(count($entry) > 2 ){
358 foreach($entry as $name => $value){
359 if(!in_array($name, array('CLASS','ACL'))){
360 $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
361 }
362 }
363 }
364 }
365 }
366 }
368 // Search for config flags defined in the config file (MAIN section)
369 foreach($this->config->data['MAIN'] as $name => $value){
370 $this->fileStoredProperties['core'][strtolower($name)] = $value;
371 }
373 // Search for config flags defined in the config file (Current LOCATION section)
374 if(isset($this->config->current)){
375 foreach($this->config->current as $name => $value){
376 $this->fileStoredProperties['core'][strtolower($name)] = $value;
377 }
378 }
380 // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
381 // in the config.
382 $this->ignoreLdapProperties = FALSE;
383 if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) &&
384 preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
385 $this->ignoreLdapProperties = TRUE;
386 }
388 // Search for all config flags defined in the LDAP - BUT only if we ARE logged in.
389 if(!empty($this->config->current['CONFIG'])){
390 $ldap = $this->config->get_ldap_link();
391 $ldap->cd($this->config->current['CONFIG']);
392 $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
393 while($attrs = $ldap->fetch()){
394 $class = $attrs['cn'][0];
395 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
396 list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
397 $this->ldapStoredProperties[$class][$name] = $value;
398 }
399 }
400 }
402 // Register plugin properties.
403 foreach ($this->classesWithInfo as $cname => $def){
405 // Detect class name
406 $name = $cname;
407 $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
408 $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
410 // Register post events
411 $this->classToName[$cname] = $name;
412 $data = array('name' => 'postcreate','type' => 'command');
413 $this->register($cname, $data);
414 $data = array('name' => 'postremove','type' => 'command');
415 $this->register($cname, $data);
416 $data = array('name' => 'postmodify','type' => 'command');
417 $this->register($cname, $data);
418 $data = array('name' => 'precreate','type' => 'command');
419 $this->register($cname, $data);
420 $data = array('name' => 'preremove','type' => 'command');
421 $this->register($cname, $data);
422 $data = array('name' => 'premodify','type' => 'command');
423 $this->register($cname, $data);
424 $data = array('name' => 'check', 'type' => 'command');
425 $this->register($cname, $data);
427 // Register properties
428 if(isset($def['plProperties'])){
429 foreach($def['plProperties'] as $property){
430 $this->register($cname, $property);
431 }
432 }
433 }
435 // We are only finsihed once we are logged in.
436 if(!empty($this->config->current['CONFIG'])){
437 $this->status = 'finished';
438 }
439 }
442 /*! \brief Returns TRUE if the property registration has finished without any error.
443 */
444 function propertyInitializationComplete()
445 {
446 return($this->status == 'finished');
447 }
450 /*! \brief Registers a GOsa-Property and thus makes it useable by GOsa and its plugins.
451 * @param String $class The name of the class/plugin that wants to register this property.
452 * @return Array $data An array containing all data set in plInfo['plProperty]
453 */
454 function register($class,$data)
455 {
456 $id = count($this->properties);
457 $this->properties[$id] = new gosaProperty($this,$class,$data);
458 $p = strtolower("{$class}::{$data['name']}");
459 $this->mapByName[$p] = $id;
460 }
463 /*! \brief Returns all registered properties.
464 * @return Array A list of all properties.
465 */
466 public function getAllProperties()
467 {
468 return($this->properties);
469 }
472 /*! \brief Checks whether a property exists or not.
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 Boolean TRUE if it exists else FALSE.
476 */
477 function propertyExists($class,$name)
478 {
479 $p = strtolower("{$class}::{$name}");
480 return(isset($this->mapByName[$p]));
481 }
484 /*! \brief Returns the id of a registered property.
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 Integer The id for the given property.
488 */
489 private function getId($class,$name)
490 {
491 $p = strtolower("{$class}::{$name}");
492 if(!isset($this->mapByName[$p])){
493 return(-1);
494 }
495 return($this->mapByName[$p]);
496 }
499 /*! \brief Returns a given property, if it exists.
500 * @param String $class The class name (e.g. 'core' or 'mailAccount')
501 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
502 * @return GOsaPropery The property or 'NULL' if it doesn't exists.
503 */
504 function getProperty($class,$name)
505 {
506 if($this->propertyExists($class,$name)){
507 return($this->properties[$this->getId($class,$name)]);
508 }
509 return(NULL);
510 }
513 /*! \brief Returns the 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 GOsaPropery The property value or an empty string if it doesn't exists.
517 */
518 function getPropertyValue($class,$name)
519 {
520 if($this->propertyExists($class,$name)){
521 $tmp = $this->getProperty($class,$name);
522 return($tmp->getValue());
523 }
524 return("");
525 }
528 /*! \brief Set a new value for a given property, if it exists.
529 * @param String $class The class name (e.g. 'core' or 'mailAccount')
530 * @param String $name The property name (e.g. 'sessionTimeout' or 'mailMethod')
531 * @return
532 */
533 function setPropertyValue($class,$name, $value)
534 {
535 if($this->propertyExists($class,$name)){
536 $tmp = $this->getProperty($class,$name);
537 return($tmp->setValue($value));
538 }
539 return("");
540 }
543 /*! \brief Save all temporary made property changes and thus make them useable/effective.
544 * @return Array Returns a list of plugins that have to be migrated before they can be saved.
545 */
546 function saveChanges()
547 {
548 $migrate = array();
549 foreach($this->properties as $prop){
551 // Is this property modified
552 if(in_array($prop->getStatus(),array('modified','removed'))){
554 // Check if we've to migrate something before we can make the changes effective.
555 if($prop->migrationRequired()){
556 $migrate[] = $prop;
557 }else{
558 $prop->save();
559 }
560 }
561 }
562 return($migrate);
563 }
564 }
567 class gosaProperty
568 {
569 protected $name = "";
570 protected $class = "";
571 protected $value = "";
572 protected $tmp_value = ""; // Used when modified but not saved
573 protected $type = "string";
574 protected $default = "";
575 protected $defaults = "";
576 protected $description = "";
577 protected $check = "";
578 protected $migrate = "";
579 protected $mandatory = FALSE;
580 protected $group = "default";
581 protected $parent = NULL;
582 protected $data = array();
584 protected $migrationClass = NULL;
586 /*! The current property status
587 * 'ldap' Property is stored in ldap
588 * 'file' Property is stored in the config file
589 * 'undefined' Property is currently not stored anywhere
590 * 'modified' Property has been modified (should be saved)
591 */
592 protected $status = 'undefined';
594 protected $attributes = array('name','type','default','description','check',
595 'migrate','mandatory','group','defaults');
600 function __construct($parent,$classname,$data)
601 {
602 // Set some basic infos
603 $this->parent = &$parent;
604 $this->class = $classname;
605 $this->data = $data;
607 // Get all relevant information from the data array (comes from plInfo)
608 foreach($this->attributes as $aName){
609 if(isset($data[$aName])){
610 $this->$aName = $data[$aName];
611 }
612 }
614 // Initialize with the current value
615 $this->_restoreCurrentValue();
617 }
619 function migrationRequired()
620 {
621 // Instantiate migration class
622 if(!empty($this->migrate) && $this->migrationClass == NULL){
623 if(!class_available($this->migrate)){
624 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
625 }else{
626 $class = $this->migrate;
627 $tmp = new $class($this->parent->config,$this);
628 if(! $tmp instanceof propertyMigration){
629 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
630 }else{
631 $this->migrationClass = $tmp;
632 }
633 }
634 }
635 if(empty($this->migrate) || $this->migrationClass == NULL){
636 return(FALSE);
637 }
638 return($this->migrationClass->checkForIssues());
639 }
641 function getMigrationClass()
642 {
643 return($this->migrationClass);
644 }
646 function check()
647 {
648 $val = $this->getValue(TRUE);
649 $return = TRUE;
650 if($this->mandatory && empty($val)){
651 $return = FALSE;
652 }
654 $check = $this->getCheck();
655 if(!empty($val) && !empty($check)){
656 $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
657 if(!$res){
658 $return = FALSE;
659 }
660 }
661 return($return);
662 }
664 static function isBool($message,$class,$name,$value, $type)
665 {
666 $match = in_array($value,array('true','false',''));
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 bool value is required here!"),
672 bold($value),bold($class),bold($name)),
673 WARNING_DIALOG);
674 }
676 return($match);
677 }
679 static function isString($message,$class,$name,$value, $type)
680 {
681 $match = TRUE;
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 string value is required here!"),
687 bold($value),bold($class),bold($name)),
688 WARNING_DIALOG);
689 }
691 return($match);
692 }
694 static function isInteger($message,$class,$name,$value, $type)
695 {
696 $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
698 // Display the reason for failing this check.
699 if($message && ! $match){
700 msg_dialog::display(_("Warning"),
701 sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"),
702 bold($value),bold($class),bold($name)),
703 WARNING_DIALOG);
704 }
706 return($match);
707 }
709 static function isPath($message,$class,$name,$value, $type)
710 {
711 $match = preg_match("#^(/[^/]*/){1}#", $value);
713 // Display the reason for failing this check.
714 if($message && ! $match){
715 msg_dialog::display(_("Warning"),
716 sprintf(_("The path '%s' specified for '%s:%s' is invalid!"),
717 bold($value),bold($class),bold($name)),
718 WARNING_DIALOG);
719 }
721 return($match);
722 }
724 static function isReadablePath($message,$class,$name,$value, $type)
725 {
726 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
728 // Display the reason for failing this check.
729 if($message && ! $match){
730 if(!is_dir($value)){
731 msg_dialog::display(_("Warning"),
732 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
733 bold($value),bold($class),bold($name)),
734 WARNING_DIALOG);
735 }elseif(!is_readable($value)){
736 msg_dialog::display(_("Warning"),
737 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"),
738 bold($value),bold($class),bold($name)),
739 WARNING_DIALOG);
740 }
741 }
743 return($match);
744 }
746 static function isWriteableFile($message,$class,$name,$value, $type)
747 {
748 $match = !empty($value) && (is_file($value) || is_writeable($value));
750 // Display the reason for failing this check.
751 if($message && ! $match){
752 if(!is_writeable($value) || !is_file($value)){
753 msg_dialog::display(_("Warning"),
754 sprintf(_("The file '%s' specified for '%s:%s' cannot be used for writing!"),
755 bold($value),bold($class),bold($name)),
756 WARNING_DIALOG);
757 }
758 }
760 return($match);
761 }
763 static function isWriteablePath($message,$class,$name,$value, $type)
764 {
765 $match = !empty($value)&&is_dir($value)&&is_writeable($value);
767 // Display the reason for failing this check.
768 if($message && ! $match){
769 if(!is_dir($value)){
770 msg_dialog::display(_("Warning"),
771 sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"),
772 bold($value),bold($class),bold($name)),
773 WARNING_DIALOG);
774 }elseif(!is_writeable($value)){
775 msg_dialog::display(_("Warning"),
776 sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"),
777 bold($value),bold($class),bold($name)),
778 WARNING_DIALOG);
779 }
780 }
782 return($match);
783 }
785 static function isReadableFile($message,$class,$name,$value, $type)
786 {
787 $match = !empty($value) && is_readable($value) && is_file($value);
789 // Display the reason for failing this check.
790 if($message && ! $match){
792 if(!is_file($value)){
793 msg_dialog::display(_("Warning"),
794 sprintf(_("The file '%s' specified for '%s:%s' does not exists!"),
795 bold($value),bold($class),bold($name)),
796 WARNING_DIALOG);
797 }elseif(!is_readable($value)){
798 msg_dialog::display(_("Warning"),
799 sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"),
800 bold($value),bold($class),bold($name)),
801 WARNING_DIALOG);
802 }
803 }
805 return($match);
806 }
808 static function isCommand($message,$class,$name,$value, $type)
809 {
810 $match = TRUE;
812 // Display the reason for failing this check.
813 if($message && ! $match){
814 msg_dialog::display(_("Warning"),
815 sprintf(_("The command '%s' specified for '%s:%s' is invalid!"),
816 bold($value),bold($class),bold($name)),
817 WARNING_DIALOG);
818 }
820 return($match);
821 }
823 static function isDn($message,$class,$name,$value, $type)
824 {
825 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
827 // Display the reason for failing this check.
828 if($message && ! $match){
829 msg_dialog::display(_("Warning"),
830 sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"),
831 bold($value),bold($class),bold($name)),
832 WARNING_DIALOG);
833 }
835 return($match);
836 }
838 static function isRdn($message,$class,$name,$value, $type)
839 {
840 $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
842 // Display the reason for failing this check.
843 if($message && ! $match){
844 msg_dialog::display(_("Warning"),
845 sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"),
846 bold($value),bold($class),bold($name)),
847 WARNING_DIALOG);
848 }
850 return($match);
851 }
853 private function _restoreCurrentValue()
854 {
855 // First check for values in the LDAP Database.
856 if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
857 $this->setStatus('ldap');
858 $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
859 return;
860 }
862 // Second check for values in the config file.
863 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
864 $this->setStatus('file');
865 $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
866 return;
867 }
869 // If there still wasn't found anything then fallback to the default.
870 if($this->getStatus() == 'undefined'){
871 $this->value = $this->getDefault();
872 }
873 }
875 function getMigrate() { return($this->migrate); }
876 function getCheck() { return($this->check); }
877 function getName() { return($this->name); }
878 function getClass() { return($this->class); }
879 function getGroup() { return($this->group); }
880 function getType() { return($this->type); }
881 function getDescription() { return($this->description); }
882 function getDefault() { return($this->default); }
883 function getDefaults() { return($this->defaults); }
884 function getStatus() { return($this->status); }
885 function isMandatory() { return($this->mandatory); }
887 function setValue($str)
888 {
889 if(in_array($this->getStatus(), array('modified'))){
890 $this->tmp_value = $str;
891 }elseif($this->value != $str){
892 $this->setStatus('modified');
893 $this->tmp_value = $str;
894 }
895 }
897 function getValue($temporary = FALSE)
898 {
899 if($temporary){
900 if(in_array($this->getStatus(), array('modified','removed'))){
901 return($this->tmp_value);
902 }else{
903 return($this->value);
904 }
905 }else{
907 // Do not return ldap values if we've to ignore them.
908 if($this->parent->ignoreLdapProperties){
909 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
910 return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
911 }else{
912 return($this->getDefault());
913 }
914 }else{
915 return($this->value);
916 }
917 }
918 }
920 function restoreDefault()
921 {
922 if(in_array($this->getStatus(),array('ldap'))){
923 $this->setStatus('removed');
925 // Second check for values in the config file.
926 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
927 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
928 }else{
929 $this->tmp_value = $this->getDefault();
930 }
931 }
932 }
934 function save()
935 {
936 if($this->getStatus() == 'modified'){
937 $ldap = $this->parent->config->get_ldap_link();
938 $ldap->cd($this->parent->config->current['BASE']);
939 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
940 $ldap->cat($dn);
941 if(!$ldap->count()){
942 $ldap->cd($dn);
943 $data = array(
944 'cn' => $this->class,
945 'objectClass' => array('top','gosaConfig'),
946 'gosaSetting' => $this->name.":".$this->tmp_value);
948 $ldap->add($data);
949 if(!$ldap->success()){
950 echo $ldap->get_error();
951 }
953 }else{
954 $attrs = $ldap->fetch();
955 $data = array();
956 $found = false;
957 if(isset($attrs['gosaSetting']['count'])){
958 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
959 $set = $attrs['gosaSetting'][$i];
960 if(preg_match("/^{$this->name}:/", $set)){
961 $set = "{$this->name}:{$this->tmp_value}";
962 $found = true;
963 }
964 $data['gosaSetting'][] = $set;
965 }
966 }
967 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
968 $ldap->cd($dn);
969 $ldap->modify($data);
970 if(!$ldap->success()){
971 echo $ldap->get_error();
972 }
973 }
974 $this->value = $this->tmp_value;
975 $this->setStatus('ldap');
976 }elseif($this->getStatus() == 'removed'){
977 $ldap = $this->parent->config->get_ldap_link();
978 $ldap->cd($this->parent->config->current['BASE']);
979 $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
980 $ldap->cat($dn);
981 $attrs = $ldap->fetch();
982 $data = array('gosaSetting' => array());
983 for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
984 $set = $attrs['gosaSetting'][$i];
985 if(preg_match("/^{$this->name}:/", $set)){
986 continue;
987 }
988 $data['gosaSetting'][] = $set;
989 }
990 $ldap->cd($dn);
991 $ldap->modify($data);
992 if(!$ldap->success()){
993 echo $ldap->get_error();
994 }
995 $this->_restoreCurrentValue();
996 }
997 }
999 private function setStatus($state)
1000 {
1001 if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
1002 trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
1003 }else{
1004 $this->status = $state;
1005 }
1006 }
1008 function isValid()
1009 {
1010 return(TRUE);
1011 }
1012 }
1016 interface propertyMigration
1017 {
1018 function __construct($config,$property);
1019 }
1022 ?>