]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - config/Tools.php
add TOOLBAR_TEMPLATE_PULLDOWN (AddTemplate icon)
[SourceForge/phpwiki.git] / config / Tools.php
1 <?php
2 rcs_id('$Id: Tools.php,v 1.7 2004-06-03 10:18:40 rurban Exp $');
3 /*
4  Copyright 2002,2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Class for Configuration properties
25  * 
26  * Class provides the functions to get and set 
27  * valid values for configuration properties.
28  * @author Joby Walker<zorloc@imperium.org>
29  */
30 class ConfigValue {
31     
32     /** 
33     * Name of the Value.
34     * @var string
35     * @access protected
36     */
37     var $name;
38     /** 
39     * The current value.
40     * @var mixed
41     * @access protected
42     */
43     var $currentValue;
44     /** 
45     * The default value.
46     * @var mixed
47     * @access protected
48     */
49     var $defaultValue;
50     /** 
51     * Array with a short and full description.
52     * @var array
53     * @access protected
54     */
55     var $description;
56     /** 
57     * Validator object to validate a new value.
58     * @var object
59     * @access protected
60     */
61     var $validator;
62     
63     /**
64     * Constructor
65     * 
66     * Initializes instance variables from parameter array.
67     * @param array $params Array with properties of the config value.
68     */
69     function ConfigValue($params){
70         $this->name = $params['name'];
71         $this->section = $params['section'];
72         $this->defaultValue = $params['defaultValue'];
73         $this->description = $params['description'];
74         $this->validator = &$params['validator'];
75         $this->currentValue = $this->getStarting();
76     }
77     
78     /**
79     * Static method to get the proper subclass.
80     * 
81     * @param array $params Config Values properties.
82     * @return object A subclass of ConfigValue.
83     * @static
84     */
85     function getConfig($params){
86         if (isset($params['validator'])) {
87             $params['validator'] = &Validator::getValidator($params['validator']);
88         }
89         return new ConfigValue($params);
90     }
91
92     /**
93     * Determines if the value is valid.
94     * 
95     * If the parameter is a valid value for this config value returns
96     * true, false else.
97     * @param mixed $value Value to be checked for validity.
98     * @return boolean True if valid, false else.
99     */
100     function valid($value){
101         if ($this->validator->validate($value)) {
102             return true;
103         }
104         trigger_error("Value for \'" . $this->name . "\' is invalid.",
105                       E_USER_WARNING);
106         return false;
107     }
108
109     /**
110     * Determines the value currently being used.
111     * 
112     * Just returns the default value.
113     * @return mixed The currently used value (the default).
114     */
115     function getStarting(){
116         return $this->defaultValue;
117     }
118     
119     /**
120     * Get the currently selected value.
121     * 
122     * @return mixed The currently selected value.
123     */
124     function getCurrent(){
125         return $this->currentValue;
126     }
127
128     /**
129     * Set the current value to this.
130     * 
131     * Checks to see if the parameter is a valid value, if so it
132     * sets the parameter to currentValue.
133     * @param mixed $value The value to set.
134     */    
135     function setCurrent($value){
136         if ($this->valid($value)) {
137             $this->currentValue = $value;
138         }
139     }
140     
141     /**
142     * Get the Name of the Value
143     * @return mixed Name of the value.
144     */
145     function getName(){
146         return $this->name;
147     }
148     
149     /**
150     * Get the default value of the Value
151     * @return mixed Default value of the value.
152     */
153     function getDefaultValue(){
154         return $this->defaultValue;
155     }
156     
157     /**
158     * Get the Short Description of the Value
159     * @return mixed Short Description of the value.
160     */
161     function getShortDescription(){
162         return $this->description['short'];
163     }
164
165     /**
166     * Get the Full Description of the Value
167     * @return mixed Full Description of the value.
168     */
169     function getFullDescription(){
170         return $this->description['full'];
171     }
172 }
173
174
175
176
177 /**
178 * Abstract base Validator Class
179 * @author Joby Walker<zorloc@imperium.org>
180 */
181 class Validator {
182
183     /**
184     * Constructor
185     * 
186     * Dummy constructor that does nothing.
187     */
188     function Validator(){
189         return;
190     }
191
192     /**
193     * Dummy valitate method -- always returns true.
194     * @param mixed $value Value to check.
195     * @return boolean Always returns true.
196     */
197     function validate($value){
198         return true;
199     }
200     
201     /**
202     * Get the proper Valitator subclass for the provided parameters
203     * @param array $params Initialization values for Validator.
204     * @return object Validator subclass for use with the parameters.
205     * @static
206     */
207     function getValidator($params){
208         extract($params, EXTR_OVERWRITE);
209         $class = 'Validator' . $type;
210         if (isset($list)){
211             $class .= 'List';
212             return new $class ($list);
213         } elseif (isset($range)) {
214             $class .= 'Range';
215             return new $class ($range);
216         } elseif (isset($pcre)){
217             $class .= 'Pcre';
218             return new $class ($pcre);
219         }
220         return new $class ();
221     
222     }
223
224 }
225
226 /**
227 * Validator subclass for use with boolean values
228 * @author Joby Walker<zorloc@imperium.org>
229 */
230 class ValidatorBoolean extends Validator {
231
232     /**
233     * Checks the parameter to see if it is a boolean, returns true if
234     * it is, else false.
235     * @param boolean $boolean Value to check to ensure it is a boolean.
236     * @return boolean True if parameter is boolean.
237     */
238     function validate ($boolean){
239         if (is_bool($boolean)) {
240             return true;
241         }
242         return false;
243     }
244 }
245
246 /**
247 * Validator subclass for use with integer values with no bounds.
248 * @author Joby Walker<zorloc@imperium.org>
249 */
250 class ValidatorInteger extends Validator {
251
252     /**
253     * Checks the parameter to ensure that it is an integer.
254     * @param integer $integer Value to check.
255     * @return boolean True if parameter is an integer, false else.
256     */
257     function validate ($integer){
258         if (is_int($integer)) {
259             return true;
260         }
261         return false;
262     }
263 }
264
265 /**
266 * Validator subclass for use with integer values to be bound within a range.
267 * @author Joby Walker<zorloc@imperium.org>
268 */
269 class ValidatorIntegerRange extends ValidatorInteger {
270
271     /** 
272     * Minimum valid value
273     * @var integer
274     * @access protected
275     */
276     var $minimum;
277     
278     /** 
279     * Maximum valid value
280     * @var integer
281     * @access protected
282     */
283     var $maximum;
284
285     /**
286     * Constructor
287     * 
288     * Sets the minimum and maximum values from the parameter array.
289     * @param array $range Minimum and maximum valid values.
290     */
291     function ValidatorIntegerRange($range){
292         $this->minimum = $range['minimum'];
293         $this->maximum = $range['maximum'];
294         return;
295     }
296     
297     /**
298     * Checks to ensure that the parameter is an integer and within the desired 
299     * range.
300     * @param integer $integer Value to check. 
301     * @return boolean True if the parameter is an integer and within the 
302     * desired range, false else.
303     */
304     function validate ($integer){
305         if (is_int($integer)) {
306             if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
307                 return true;
308             }
309         }
310         return false;
311     }
312
313 }
314
315 /**
316 * Validator subclass for use with integer values to be selected from a list.
317 * @author Joby Walker<zorloc@imperium.org>
318 */
319 class ValidatorIntegerList extends ValidatorInteger {
320
321     /** 
322     * Array of potential valid values
323     * @var array
324     * @access protected
325     */
326     var $intList;
327     
328     /**
329     * Constructor
330     * 
331     * Saves parameter as the instance variable $intList.
332     * @param array List of valid values.
333     */
334     function ValidatorIntegerList($intList){
335         $this->intList = $intList;
336         return;
337     }
338
339     /**
340     * Checks the parameter to ensure that it is an integer, and 
341     * within the defined list.
342     * @param integer $integer Value to check.
343     * @return boolean True if parameter is an integer and in list, false else.
344     */
345     function validate ($integer){
346         if (is_int($integer)) {
347             if (in_array($integer, $this->intList, true)) {
348                 return true;
349             }
350         }
351         return false;
352     }
353
354 }
355
356 /**
357 * Validator subclass for string values with no bounds
358 * @author Joby Walker<zorloc@imperium.org>
359 */
360 class ValidatorString extends Validator {
361
362     /**
363     * Checks the parameter to ensure that is is a string.
364     * @param string $string Value to check.
365     * @return boolean True if parameter is a string, false else.
366     */
367     function validate ($string){
368         if (is_string($string)) {
369             return true;
370         }
371         return false;
372     }
373
374 }
375
376 /**
377 * Validator subclass for string values to be selected from a list.
378 * @author Joby Walker<zorloc@imperium.org>
379 */
380 class ValidatorStringList extends ValidatorString {
381
382     /** 
383     * Array of potential valid values
384     * @var array
385     * @access protected
386     */
387     var $stringList;
388     
389     /**
390     * Constructor
391     * 
392     * Saves parameter as the instance variable $stringList.
393     * @param array List of valid values.
394     */
395     function ValidatorStringList($stringList){
396         $this->stringList = $stringList;
397         return;
398     }
399
400     /**
401     * Checks the parameter to ensure that is is a string, and within 
402     * the defined list.
403     * @param string $string Value to check.
404     * @return boolean True if parameter is a string and in the list, false else.
405     */
406     function validate($string){
407         if (is_string($string)) {
408             if (in_array($string, $this->stringList, true)) {
409                 return true;
410             }
411         }
412         return false;
413     }
414
415 }
416
417 /**
418 * Validator subclass for string values that must meet a PCRE.
419 * @author Joby Walker<zorloc@imperium.org>
420 */
421 class ValidatorStringPcre extends ValidatorString {
422
423     /** 
424     * PCRE to validate value
425     * @var array
426     * @access protected
427     */
428     var $pattern;
429
430     /**
431     * Constructor
432     * 
433     * Saves parameter as the instance variable $pattern.
434     * @param array PCRE pattern to determin validity.
435     */
436     function ValidatorStringPcre($pattern){
437         $this->pattern = $pattern;
438         return;
439     }
440
441     /**
442     * Checks the parameter to ensure that is is a string, and matches the 
443     * defined pattern.
444     * @param string $string Value to check.
445     * @return boolean True if parameter is a string and matches the pattern,
446     * false else.
447     */
448     function validate ($string){
449         if (is_string($string)) {
450             if (preg_match($this->pattern, $string)) {
451                 return true;
452             }
453         }
454         return false;
455     }
456 }
457
458 /**
459 * Validator subclass for constant values.
460 * @author Joby Walker<zorloc@imperium.org>
461 */
462 class ValidatorConstant extends Validator {
463
464     /**
465     * Checks the parameter to ensure that is is a constant.
466     * @param string $constant Value to check.
467     * @return boolean True if parameter is a constant, false else.
468     */
469     function validate ($constant){
470         if (defined($constant)) {
471             return true;
472         }
473         return false;
474     }
475 }
476
477 /**
478 * Validator subclass for constant values to be selected from a list.
479 * @author Joby Walker<zorloc@imperium.org>
480 */
481 class ValidatorConstantList extends Validator {
482
483     /** 
484     * Array of potential valid values
485     * @var array
486     * @access protected
487     */
488     var $constantList;
489
490     /**
491     * Constructor
492     * 
493     * Saves parameter as the instance variable $constantList.
494     * @param array List of valid values.
495     */
496     function ValidatorConstantList($constantList){
497         $this->constantList = $constantList;
498         return;
499     }
500
501     /**
502     * Checks the parameter to ensure that is is a constant, and within 
503     * the defined list.
504     * @param string $constant Value to check.
505     * @return boolean True if parameter is a constant and in the list, false else.
506     */
507     function validate ($constant){
508         if (defined($constant)) {
509             if (in_array($constant, $this->constantList, true)) {
510                 return true;
511             }
512         }
513         return false;
514     }
515 }
516
517 /**
518 * Validator subclass for an array.
519 * @author Joby Walker<zorloc@imperium.org>
520 */
521 class ValidatorArray extends Validator {
522
523     /*
524     * Checks to ensure that the parameter is an array then passes the
525     * array on to validMembers() to ensure that each member of the 
526     * array is valid.
527     * @param array $array Value to check.
528     * @return boolean True if the value is and array and members are valid, false else.
529     */
530     function validate($array){
531         if(is_array($array)){
532             return $this->validMembers($array);
533         }
534         return false;
535     }
536     
537     /**
538     * Checks to ensure that the members of the array are valid.  Always true here.
539     * @param array $array Array of members to check
540     * @return boolean Always true since there are no restrictions on the members.
541     */
542     function validMembers($array){
543         return true;
544     }
545 }
546
547 /**
548 * Validator subclass for an array of strings.
549 * @author Joby Walker<zorloc@imperium.org>
550 */
551 class ValidatorArrayString extends Validator {
552
553     /**
554     * Checks to ensure that the members of the array are valid strings.
555     * @param array $array Array of members to check
556     * @return boolean True if the members are valid strings, false else.
557     */
558     function validMembers($array){
559         foreach ($array as $member){
560             if (!is_string($member)) {
561                 return false;
562             }
563         }
564         return true;
565     }
566 }
567
568 /**
569 * Validator subclass for an array of strings that must be in a list of 
570 * defined values.
571 * @author Joby Walker<zorloc@imperium.org>
572 */
573 class ValidatorArrayStringList extends Validator {
574
575     /** 
576     * Array of potential valid values
577     * @var array
578     * @access protected
579     */
580     var $stringList;
581
582     /**
583     * Constructor
584     * 
585     * Saves parameter as the instance variable $stringList.
586     * @param array List of valid values.
587     */
588     function ValidatorArrayStringList($stringList){
589         $this->stringList = $stringList;
590         return;
591     }
592
593     /**
594     * Checks to ensure that the members of the array are valid strings and 
595     * within the defined list.
596     * @param array $array Array of members to check
597     * @return boolean True if the members are valid strings are in the defined list, 
598     * false else.
599     */
600     function validMembers($array){
601         foreach ($array as $member){
602             if(!in_array($member, $stringList, true)){
603                 return false;
604             }
605         }
606         return true;
607     }
608
609 }
610
611 /**
612  * Convert old-style index.php configuration to config/config.ini
613  */
614 function ConvertToIni ($index) {
615     $config = "config/config.ini";
616 }
617
618 //$Log: not supported by cvs2svn $
619 //Revision 1.6  2004/04/16 23:30:41  zorloc
620 //More work for new ini config system.  Tools has array type removed and first implimentations of config-dist.ini and IniConfig.php.  Will update config-dist.ini again soon.
621 //
622 //Revision 1.5  2003/12/07 19:25:41  carstenklapp
623 //Code Housecleaning: fixed syntax errors. (php -l *.php)
624 //
625 //Revision 1.3  2003/01/28 18:53:25  zorloc
626 //Added some more Validator subclasses to handle arrays of for which the
627 //validation criteria should be the same for all members.
628 //
629 //Revision 1.2  2003/01/28 06:31:00  zorloc
630 //Mostly done but ConfigArray will probably need some more helper methods.
631 //
632 //Revision 1.1  2003/01/23 00:32:04  zorloc
633 //Initial work for classes to hold configuration constants/variables. Base
634 //ConfigValue class and subclasses for constants and variables.
635 //
636
637 // Local Variables:
638 // mode: php
639 // tab-width: 8
640 // c-basic-offset: 4
641 // c-hanging-comment-ender-p: nil
642 // indent-tabs-mode: nil
643 // End:
644 ?>