Code

Added logout tooltip
[gosa.git] / gosa-core / include / class_configRegistry.inc
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     function __construct($config)
24     {
25         $this->config = &$config;
27         // Detect classes that have a plInfo method 
28         global $class_mapping;
29         foreach ($class_mapping as $cname => $path){
30             $cmethods = get_class_methods($cname);
31             if (is_array($cmethods) && in_array_ics('plInfo',$cmethods)){
33                 // Get plugin definitions
34                 $def = call_user_func(array($cname, 'plInfo'));;
36                 // Register Post Events (postmodfiy,postcreate,postremove,checkhook)
37                 if(count($def)){
38                     $this->classesWithInfo[$cname] = $def;
39                 }
40             }
41         }
43         // (Re)Load properties
44         $this->reload();
45     }
48     function validateSchemata($disableIncompatiblePlugins = FALSE,$displayMessage = FALSE)
49     {
50         // We can check the schemata only with a valid LDAP connection
51         if(empty($this->config->current['CONFIG'])){
52             return(TRUE); 
53         }
55         // Read objectClasses from ldap
56         if(!count($this->objectClasses)){
57             $ldap = $this->config->get_ldap_link();
58             $ldap->cd($this->config->current['BASE']);
59             $this->objectClasses = $ldap->get_objectclasses();
60         }
62         // Collect required schema infos
63         $this->pluginRequirements = array('ldapSchema' => array());
64         $this->categoryToClass = array();
65         foreach($this->classesWithInfo as $cname => $defs){
66             if(isset($defs['plRequirements']['ldapSchema'])){
67                 $this->pluginRequirements['ldapSchema'][$cname] = $defs['plRequirements']['ldapSchema'];
68             }
69         }
71         // Check schema requirements now
72         foreach($this->pluginRequirements['ldapSchema'] as $cname => $requirements){
73             foreach($requirements as $oc => $version){
74                 if(!$this->ocAvailable($oc)){
75                     if($displayMessage){
76                         msg_dialog::display(_("Schema validation error"), 
77                                 sprintf(_("The objectClass '%s' which is required for plugin '%s' is not availabe!"),
78                                     bold($oc),bold($cname)), 
79                                 ERROR_DIALOG);
80                     }
81                 }elseif(!empty($version)){
83                     $currentVersion = $this->getObjectClassVersion($oc);
84                     if(!empty($currentVersion) && !$this->ocVersionMatch($version, $currentVersion)){
85                         if($displayMessage){
86                             msg_dialog::display(_("Schema validation error"), 
87                                     sprintf(_("The objectClass '%s' which is required for plugin '%s' has version %s but %s is required!"),bold($oc),bold($cname),bold($currentVersion),bold($version)), 
88                                     ERROR_DIALOG);
89                         }
90                     }
91                 }
92             }
93         }
94     }
96     function ocVersionMatch($required, $installed)
97     {
98         $operator = preg_replace('/^([=<>]*).*$/',"\\1",$required);
99         $required = preg_replace('/^[=<>]*(.*)$/',"\\1",$required);
100         return(version_compare($installed,$required, $operator)); 
101     }
103     
104     function getObjectClassVersion($oc)
105     {
106         if(!isset($this->objectClasses[$oc])){
107             return(NULL);
108         }else{
109             $version = -1; // unknown
110             if(preg_match("/(v[^)]*)/", $this->objectClasses[$oc]['DESC'])){
111                 $version = preg_replace('/^.*\(v([^)]*)\).*$/',"\\1", $this->objectClasses[$oc]['DESC']);
112             }
113         }
114         return($version);
115     }
116     
118     // check wheter an objectClass is installed or not.
119     function ocAvailable($name)
120     {
121         return(isset($this->objectClasses[$name]));
122     }
125     function reload($force = FALSE)
126     {
127         // Do not reload the properties everytime, once we have  
128         //  everything loaded and registrered skip the reload.
129         // Status is 'finished' once we had a ldap connection (logged in)
130         if(!$force && $this->status == 'finished') return;
132         // Reset everything
133         $this->ldapStoredProperties = array();
134         $this->fileStoredProperties = array();
135         $this->properties = array();
136         $this->mapByName = array();
138         // Search for config flags defined in the config file (TAB section)
139         foreach($this->config->data['TABS'] as $tabname => $tabdefs){
140             foreach($tabdefs as $info){
142                 // Check if the info is valid
143                 if(isset($info['NAME']) && isset($info['CLASS'])){
145                     // Check if there is nore than just the plugin definition
146                     if(count($info) > 2){
147                         foreach($info as $name => $value){
148                             
149                             if(!in_array($name, array('CLASS','NAME'))){
150                                 $class= $info['CLASS'];    
151                                 $this->fileStoredProperties[$class][strtolower($name)] = $value;
152                             }
153                         }
154                     }
155                 }
156             }
157         }
159         // Search for config flags defined in the config file (MENU section)
160         foreach($this->config->data['MENU'] as $section => $entries){
161             foreach($entries as $entry){
162                 if(count($entry) > 2 && isset($entry['CLASS'])){
163                     $class = $entry['CLASS'];
164                     foreach($entry as $name => $value){
165                         if(!in_array($name, array('CLASS','ACL'))){
166                             $this->fileStoredProperties[strtolower($class)][strtolower($name)] = $value;
167                         }
168                     }
169                 }
170             }
171         }
173         // Search for config flags defined in the config file (MAIN section)
174         foreach($this->config->data['MAIN'] as $name => $value){
175             $this->fileStoredProperties['core'][strtolower($name)] = $value;
176         }
178         // Search for config flags defined in the config file (Current LOCATION section)
179         if(isset($this->config->current)){
180             foreach($this->config->current as $name => $value){
181                 $this->fileStoredProperties['core'][strtolower($name)] = $value;
182             }
183         }
185         // Skip searching for LDAP defined properties if 'ignoreLdapProperties' is set to 'true'
186         //  in the config. 
187         $this->ignoreLdapProperties = FALSE;
188         if(isset($this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')]) && 
189             preg_match("/(true|on)/i", $this->fileStoredProperties['core'][strtolower('ignoreLdapProperties')])){
190             $this->ignoreLdapProperties = TRUE;
191         }
193         // Search for all config flags defined in the LDAP - BUT only if we ARE logged in. 
194         if(!empty($this->config->current['CONFIG'])){
195             $ldap = $this->config->get_ldap_link();
196             $ldap->cd($this->config->current['CONFIG']);
197             $ldap->search('(&(objectClass=gosaConfig)(gosaSetting=*))', array('cn','gosaSetting'));
198             while($attrs = $ldap->fetch()){
199                 $class = $attrs['cn'][0];
200                 for($i=0; $i<$attrs['gosaSetting']['count']; $i++){
201                     list($name,$value) = preg_split("/:/",$attrs['gosaSetting'][$i],2);
202                     $this->ldapStoredProperties[$class][$name] = $value;
203                 }
204             }
205             $this->status = 'finished';
206         }
208         // Register plugin properties.
209         foreach ($this->classesWithInfo as $cname => $def){
211             // Detect class name
212             $name = $cname;
213             $name = (isset($def['plShortName'])) ? $def['plShortName'] : $cname;
214             $name = (isset($def['plDescription'])) ? $def['plDescription'] : $cname;
216             // Register post events
217             $this->classToName[$cname] = $name;
218             $data = array('name' => 'postcreate','type' => 'command');
219             $this->register($cname, $data);    
220             $data = array('name' => 'postremove','type' => 'command');
221             $this->register($cname, $data);    
222             $data = array('name' => 'postmodify','type' => 'command');
223             $this->register($cname, $data);    
224             $data = array('name' => 'check', 'type' => 'command');
225             $this->register($cname, $data);    
227             // Register properties 
228             if(isset($def['plProperties'])){
229                 foreach($def['plProperties'] as $property){
230                     $this->register($cname, $property);
231                 }
232             }
233         }
234     }
236     function register($class,$data)
237     {
238         $id = count($this->properties);
239         $this->properties[$id] = new gosaProperty($this,$class,$data);
240         $p = strtolower("{$class}::{$data['name']}");
241         $this->mapByName[$p] = $id;
242     }
244     public function getAllProperties()
245     {
246         return($this->properties);
247     }
249     function propertyExists($class,$name)
250     {       
251         $p = strtolower("{$class}::{$name}");
252         return(isset($this->mapByName[$p]));
253     }
255     private function getId($class,$name)
256     {
257         $p = strtolower("{$class}::{$name}");
258         if(!isset($this->mapByName[$p])){
259             return(-1);
260         }       
261         return($this->mapByName[$p]);    
262     }
264     function getProperty($class,$name)
265     {
266         if($this->propertyExists($class,$name)){
267             return($this->properties[$this->getId($class,$name)]);
268         }
269         return(NULL); 
270     }
272     function getPropertyValue($class,$name)
273     {   
274         if($this->propertyExists($class,$name)){
275             $tmp = $this->getProperty($class,$name);
276             return($tmp->getValue());
277         }
278         return("");
279     }
281     function setPropertyValue($class,$name, $value)
282     {   
283         if($this->propertyExists($class,$name)){
284             $tmp = $this->getProperty($class,$name);
285             return($tmp->setValue($value));
286         }
287         return("");
288     }
290     function saveChanges()
291     {
292         $migrate = array();
293         foreach($this->properties as $prop){
295             // Is this property modified
296             if(in_array($prop->getStatus(),array('modified','removed'))){
298                 // Check if we've to migrate something before we can make the changes effective. 
299                 if($prop->migrationRequired()){
300                     $migrate[] = $prop;
301                 }else{
302                     $prop->save();
303                 }
304             }
305         }
306         return($migrate);
307     }
311 class gosaProperty
313     protected $name = "";
314     protected $class = "";
315     protected $value = "";
316     protected $tmp_value = "";  // Used when modified but not saved 
317     protected $type = "string";
318     protected $default = "";
319     protected $defaults = "";
320     protected $description = "";
321     protected $check = "";
322     protected $migrate = "";
323     protected $mandatory = FALSE;
324     protected $group = "default";
325     protected $parent = NULL;
326     protected $data = array();
328     protected $migrationClass = NULL;
330     /*!  The current property status
331      *     'ldap'       Property is stored in ldap 
332      *     'file'       Property is stored in the config file
333      *     'undefined'  Property is currently not stored anywhere
334      *     'modified'   Property has been modified (should be saved)
335      */
336     protected $status = 'undefined';
338     protected $attributes = array('name','type','default','description','check',
339             'migrate','mandatory','group','defaults');
344     function __construct($parent,$classname,$data)
345     {
346         // Set some basic infos 
347         $this->parent = &$parent;
348         $this->class = $classname;
349         $this->data  = $data;
351         // Get all relevant information from the data array (comes from plInfo)    
352         foreach($this->attributes as $aName){
353             if(isset($data[$aName])){
354                 $this->$aName = $data[$aName];
355             }
356         }      
358         // Initialize with the current value
359         $this->_restoreCurrentValue(); 
361     }
363     function migrationRequired()
364     {
365         // Instantiate migration class 
366         if(!empty($this->migrate) && $this->migrationClass == NULL){
367             if(!class_available($this->migrate)){
368                 trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' class not found ({$this->migrate})!");
369             }else{
370                 $class = $this->migrate;
371                 $tmp = new $class($this->parent->config,$this);
372                 if(! $tmp instanceof propertyMigration){ 
373                     trigger_error("Cannot start migration for gosaProperty::'{$this->getName()}' doesn't implement propertyMigration!");
374                 }else{
375                     $this->migrationClass = $tmp;
376                 }
377             }
378         }
379         if(empty($this->migrate) || $this->migrationClass == NULL){
380             return(FALSE);
381         }
382         return($this->migrationClass->checkForIssues());
383     }
385     function getMigrationClass()
386     {
387         return($this->migrationClass);
388     }
390     function check()
391     {
392         $val = $this->getValue(TRUE);
393         $return = TRUE;
394         if($this->mandatory && empty($val)){
395             $return = FALSE;
396         }
398         $check = $this->getCheck();
399         if(!empty($val) && !empty($check)){
400             $res = call_user_func(preg_split("/::/", $this->check),$messages=TRUE, $this->class,$this->name,$val, $this->type);
401             if(!$res){
402                 $return = FALSE;
403             }
404         }
405         return($return);
406     }
408     static function isBool($message,$class,$name,$value, $type)
409     {
410         $match = in_array($value,array('true','false',''));
412         // Display the reason for failing this check.         
413         if($message && ! $match){
414             msg_dialog::display(_("Warning"), 
415                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A bool value is required here!"), 
416                         bold($value),bold($class),bold($name)), 
417                     WARNING_DIALOG);
418         }
419     
420         return($match);
421     }
423     static function isString($message,$class,$name,$value, $type)
424     {
425         $match = TRUE;
426     
427         // Display the reason for failing this check.         
428         if($message && ! $match){
429             msg_dialog::display(_("Warning"), 
430                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A string value is required here!"), 
431                         bold($value),bold($class),bold($name)), 
432                     WARNING_DIALOG);
433         }
435         return($match);
436     }
438     static function isInteger($message,$class,$name,$value, $type)
439     {
440         $match = is_numeric($value) && !preg_match("/[^0-9]/", $value);
442         // Display the reason for failing this check.         
443         if($message && ! $match){
444             msg_dialog::display(_("Warning"), 
445                     sprintf(_("The value '%s' specified for '%s:%s' is invalid. A numeric value is required here!"), 
446                         bold($value),bold($class),bold($name)), 
447                     WARNING_DIALOG);
448         }
450         return($match);
451     }
453     static function isPath($message,$class,$name,$value, $type)
454     {
455         $match = preg_match("#^(/[^/]*/){1}#", $value);
456     
457         // Display the reason for failing this check.         
458         if($message && ! $match){
459             msg_dialog::display(_("Warning"), 
460                     sprintf(_("The path '%s' specified for '%s:%s' is invalid!"), 
461                         bold($value),bold($class),bold($name)), 
462                     WARNING_DIALOG);
463         }
465         return($match);
466     }
468     static function isReadablePath($message,$class,$name,$value, $type)
469     {
470         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
471    
472         // Display the reason for failing this check.         
473         if($message && ! $match){
474             if(!is_dir($value)){
475                 msg_dialog::display(_("Warning"), 
476                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
477                             bold($value),bold($class),bold($name)), 
478                         WARNING_DIALOG);
479             }elseif(!is_readable($value)){
480                 msg_dialog::display(_("Warning"), 
481                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for reading!"), 
482                             bold($value),bold($class),bold($name)), 
483                         WARNING_DIALOG);
484             }
485         }
487         return($match);
488     }
490     static function isWriteablePath($message,$class,$name,$value, $type)
491     {
492         $match = !empty($value)&&is_dir($value)&&is_writeable($value);
493    
494         // Display the reason for failing this check.         
495         if($message && ! $match){
496             if(!is_dir($value)){
497                 msg_dialog::display(_("Warning"), 
498                         sprintf(_("The folder '%s' specified for '%s:%s' does not exists!"), 
499                             bold($value),bold($class),bold($name)), 
500                         WARNING_DIALOG);
501             }elseif(!is_writeable($value)){
502                 msg_dialog::display(_("Warning"), 
503                         sprintf(_("The folder '%s' specified for '%s:%s' cannot be used for writing!"), 
504                             bold($value),bold($class),bold($name)), 
505                         WARNING_DIALOG);
506             }
507         }
509         return($match);
510     }
512     static function isReadableFile($message,$class,$name,$value, $type)
513     {
514         $match = !empty($value) && is_readable($value) && is_file($value);
516         // Display the reason for failing this check.         
517         if($message && ! $match){
518                 
519             if(!is_file($value)){
520                 msg_dialog::display(_("Warning"), 
521                         sprintf(_("The file '%s' specified for '%s:%s' does not exists!"), 
522                             bold($value),bold($class),bold($name)), 
523                         WARNING_DIALOG);
524             }elseif(!is_readable($value)){
525                 msg_dialog::display(_("Warning"), 
526                         sprintf(_("The file '%s' specified for '%s:%s' cannot be read!"), 
527                             bold($value),bold($class),bold($name)), 
528                         WARNING_DIALOG);
529             }
530         }
532         return($match);
533     }
535     static function isCommand($message,$class,$name,$value, $type)
536     {
537         $match = TRUE;
539         // Display the reason for failing this check.         
540         if($message && ! $match){
541             msg_dialog::display(_("Warning"), 
542                     sprintf(_("The command '%s' specified for '%s:%s' is invalid!"), 
543                         bold($value),bold($class),bold($name)), 
544                     WARNING_DIALOG);
545         }
546         
547         return($match);
548     }
550     static function isDn($message,$class,$name,$value, $type)
551     {
552         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=]*$/i", $value);
554         // Display the reason for failing this check.         
555         if($message && ! $match){
556             msg_dialog::display(_("Warning"), 
557                     sprintf(_("The dn '%s' specified for '%s:%s' is invalid!"), 
558                         bold($value),bold($class),bold($name)), 
559                     WARNING_DIALOG);
560         }
561         
562         return($match);
563     }
565     static function isRdn($message,$class,$name,$value, $type)
566     {
567         $match = preg_match("/^([a-z]*=[^=,]*,)*[^=]*=[^=,]*,?$/i", $value);
569         // Display the reason for failing this check.         
570         if($message && ! $match){
571             msg_dialog::display(_("Warning"), 
572                     sprintf(_("The rdn '%s' specified for '%s:%s' is invalid!"), 
573                         bold($value),bold($class),bold($name)), 
574                     WARNING_DIALOG);
575         }
576         
577         return($match);
578     }
580     private function _restoreCurrentValue()
581     {
582         // First check for values in the LDAP Database.
583         if(isset($this->parent->ldapStoredProperties[$this->class][$this->name])){
584             $this->setStatus('ldap');
585             $this->value = $this->parent->ldapStoredProperties[$this->class][$this->name];
586             return;
587         }
589         // Second check for values in the config file.
590         if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
591             $this->setStatus('file');
592             $this->value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
593             return;
594         }
596         // If there still wasn't found anything then fallback to the default.
597         if($this->getStatus() == 'undefined'){
598             $this->value = $this->getDefault();
599         }
600     }
602     function getMigrate() { return($this->migrate); }
603     function getCheck() { return($this->check); }
604     function getName() { return($this->name); }
605     function getClass() { return($this->class); }
606     function getGroup() { return($this->group); }
607     function getType() { return($this->type); }
608     function getDescription() { return($this->description); }
609     function getDefault() { return($this->default); }
610     function getDefaults() { return($this->defaults); }
611     function getStatus() { return($this->status); }
612     function isMandatory() { return($this->mandatory); }
614     function setValue($str) 
615     {
616         if(in_array($this->getStatus(), array('modified'))){
617             $this->tmp_value = $str; 
618         }elseif($this->value != $str){
619             $this->setStatus('modified'); 
620             $this->tmp_value = $str; 
621         }
622     }
624     function getValue($temporary = FALSE) 
625     { 
626         if($temporary){
627             if(in_array($this->getStatus(), array('modified','removed'))){
628                 return($this->tmp_value); 
629             }else{
630                 return($this->value); 
631             }
632         }else{ 
634             // Do not return ldap values if we've to ignore them.
635             if($this->parent->ignoreLdapProperties){
636                 if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
637                     return($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)]);
638                 }else{
639                     return($this->getDefault());
640                 }
641             }else{
642                 return($this->value); 
643             }
644         }
645     }
647     function restoreDefault() 
648     {
649         if(in_array($this->getStatus(),array('ldap'))){
650             $this->setStatus('removed'); 
652             // Second check for values in the config file.
653             if(isset($this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)])){
654                 $this->tmp_value = $this->parent->fileStoredProperties[strtolower($this->class)][strtolower($this->name)];
655             }else{
656                 $this->tmp_value = $this->getDefault();
657             }
658         }
659     }
661     function save()
662     {
663         if($this->getStatus() == 'modified'){
664             $ldap = $this->parent->config->get_ldap_link();
665             $ldap->cd($this->parent->config->current['BASE']);
666             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
667             $ldap->cat($dn);
668             if(!$ldap->count()){
669                 $ldap->cd($dn);
670                 $data = array(
671                         'cn' => $this->class, 
672                         'objectClass' => array('top','gosaConfig'),
673                         'gosaSetting' => $this->name.":".$this->tmp_value);
675                 $ldap->add($data);
676                 if(!$ldap->success()){
677                     echo $ldap->get_error();
678                 }
680             }else{
681                 $attrs = $ldap->fetch();
682                 $data = array();
683                 $found = false;
684                 if(isset($attrs['gosaSetting']['count'])){
685                     for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
686                         $set = $attrs['gosaSetting'][$i];
687                         if(preg_match("/^{$this->name}:/", $set)){
688                             $set = "{$this->name}:{$this->tmp_value}";
689                             $found = true;
690                         }
691                         $data['gosaSetting'][] = $set;
692                     }
693                 }
694                 if(!$found) $data['gosaSetting'][] = "{$this->name}:{$this->tmp_value}";
695                 $ldap->cd($dn);
696                 $ldap->modify($data);
697                 if(!$ldap->success()){
698                     echo $ldap->get_error();
699                 }
700             }
701             $this->value = $this->tmp_value;
702             $this->setStatus('ldap'); 
703         }elseif($this->getStatus() == 'removed'){
704             $ldap = $this->parent->config->get_ldap_link();
705             $ldap->cd($this->parent->config->current['BASE']);
706             $dn = "cn={$this->class},".$this->parent->config->current['CONFIG'];
707             $ldap->cat($dn);
708             $attrs = $ldap->fetch();
709             $data = array('gosaSetting' => array());
710             for($i = 0;$i<$attrs['gosaSetting']['count']; $i ++){
711                 $set = $attrs['gosaSetting'][$i];
712                 if(preg_match("/^{$this->name}:/", $set)){
713                     continue;
714                 }
715                 $data['gosaSetting'][] = $set;
716             }
717             $ldap->cd($dn);
718             $ldap->modify($data);
719             if(!$ldap->success()){
720                 echo $ldap->get_error();
721             }
722             $this->_restoreCurrentValue();
723         }
724     }
726     private function setStatus($state) 
727     {
728         if(!in_array($state, array('ldap','file','undefined','modified','removed'))) {
729             trigger_error("Unknown property status given '{$state}' for {$this->class}:{$this->name}!");
730         }else{
731             $this->status = $state; 
732         }
733     }
735     function isValid() 
736     { 
737         return(TRUE);    
738     }
743 interface propertyMigration
745     function __construct($config,$property);
749 ?>