2 rcs_id('$Id: Tools.php,v 1.7 2004-06-03 10:18:40 rurban Exp $');
4 Copyright 2002,2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
24 * Class for Configuration properties
26 * Class provides the functions to get and set
27 * valid values for configuration properties.
28 * @author Joby Walker<zorloc@imperium.org>
51 * Array with a short and full description.
57 * Validator object to validate a new value.
66 * Initializes instance variables from parameter array.
67 * @param array $params Array with properties of the config value.
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();
79 * Static method to get the proper subclass.
81 * @param array $params Config Values properties.
82 * @return object A subclass of ConfigValue.
85 function getConfig($params){
86 if (isset($params['validator'])) {
87 $params['validator'] = &Validator::getValidator($params['validator']);
89 return new ConfigValue($params);
93 * Determines if the value is valid.
95 * If the parameter is a valid value for this config value returns
97 * @param mixed $value Value to be checked for validity.
98 * @return boolean True if valid, false else.
100 function valid($value){
101 if ($this->validator->validate($value)) {
104 trigger_error("Value for \'" . $this->name . "\' is invalid.",
110 * Determines the value currently being used.
112 * Just returns the default value.
113 * @return mixed The currently used value (the default).
115 function getStarting(){
116 return $this->defaultValue;
120 * Get the currently selected value.
122 * @return mixed The currently selected value.
124 function getCurrent(){
125 return $this->currentValue;
129 * Set the current value to this.
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.
135 function setCurrent($value){
136 if ($this->valid($value)) {
137 $this->currentValue = $value;
142 * Get the Name of the Value
143 * @return mixed Name of the value.
150 * Get the default value of the Value
151 * @return mixed Default value of the value.
153 function getDefaultValue(){
154 return $this->defaultValue;
158 * Get the Short Description of the Value
159 * @return mixed Short Description of the value.
161 function getShortDescription(){
162 return $this->description['short'];
166 * Get the Full Description of the Value
167 * @return mixed Full Description of the value.
169 function getFullDescription(){
170 return $this->description['full'];
178 * Abstract base Validator Class
179 * @author Joby Walker<zorloc@imperium.org>
186 * Dummy constructor that does nothing.
188 function Validator(){
193 * Dummy valitate method -- always returns true.
194 * @param mixed $value Value to check.
195 * @return boolean Always returns true.
197 function validate($value){
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.
207 function getValidator($params){
208 extract($params, EXTR_OVERWRITE);
209 $class = 'Validator' . $type;
212 return new $class ($list);
213 } elseif (isset($range)) {
215 return new $class ($range);
216 } elseif (isset($pcre)){
218 return new $class ($pcre);
220 return new $class ();
227 * Validator subclass for use with boolean values
228 * @author Joby Walker<zorloc@imperium.org>
230 class ValidatorBoolean extends Validator {
233 * Checks the parameter to see if it is a boolean, returns true if
235 * @param boolean $boolean Value to check to ensure it is a boolean.
236 * @return boolean True if parameter is boolean.
238 function validate ($boolean){
239 if (is_bool($boolean)) {
247 * Validator subclass for use with integer values with no bounds.
248 * @author Joby Walker<zorloc@imperium.org>
250 class ValidatorInteger extends Validator {
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.
257 function validate ($integer){
258 if (is_int($integer)) {
266 * Validator subclass for use with integer values to be bound within a range.
267 * @author Joby Walker<zorloc@imperium.org>
269 class ValidatorIntegerRange extends ValidatorInteger {
272 * Minimum valid value
279 * Maximum valid value
288 * Sets the minimum and maximum values from the parameter array.
289 * @param array $range Minimum and maximum valid values.
291 function ValidatorIntegerRange($range){
292 $this->minimum = $range['minimum'];
293 $this->maximum = $range['maximum'];
298 * Checks to ensure that the parameter is an integer and within the desired
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.
304 function validate ($integer){
305 if (is_int($integer)) {
306 if (($integer >= $this->minimum) && ($integer <= $this->maximum)) {
316 * Validator subclass for use with integer values to be selected from a list.
317 * @author Joby Walker<zorloc@imperium.org>
319 class ValidatorIntegerList extends ValidatorInteger {
322 * Array of potential valid values
331 * Saves parameter as the instance variable $intList.
332 * @param array List of valid values.
334 function ValidatorIntegerList($intList){
335 $this->intList = $intList;
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.
345 function validate ($integer){
346 if (is_int($integer)) {
347 if (in_array($integer, $this->intList, true)) {
357 * Validator subclass for string values with no bounds
358 * @author Joby Walker<zorloc@imperium.org>
360 class ValidatorString extends Validator {
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.
367 function validate ($string){
368 if (is_string($string)) {
377 * Validator subclass for string values to be selected from a list.
378 * @author Joby Walker<zorloc@imperium.org>
380 class ValidatorStringList extends ValidatorString {
383 * Array of potential valid values
392 * Saves parameter as the instance variable $stringList.
393 * @param array List of valid values.
395 function ValidatorStringList($stringList){
396 $this->stringList = $stringList;
401 * Checks the parameter to ensure that is is a string, and within
403 * @param string $string Value to check.
404 * @return boolean True if parameter is a string and in the list, false else.
406 function validate($string){
407 if (is_string($string)) {
408 if (in_array($string, $this->stringList, true)) {
418 * Validator subclass for string values that must meet a PCRE.
419 * @author Joby Walker<zorloc@imperium.org>
421 class ValidatorStringPcre extends ValidatorString {
424 * PCRE to validate value
433 * Saves parameter as the instance variable $pattern.
434 * @param array PCRE pattern to determin validity.
436 function ValidatorStringPcre($pattern){
437 $this->pattern = $pattern;
442 * Checks the parameter to ensure that is is a string, and matches the
444 * @param string $string Value to check.
445 * @return boolean True if parameter is a string and matches the pattern,
448 function validate ($string){
449 if (is_string($string)) {
450 if (preg_match($this->pattern, $string)) {
459 * Validator subclass for constant values.
460 * @author Joby Walker<zorloc@imperium.org>
462 class ValidatorConstant extends Validator {
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.
469 function validate ($constant){
470 if (defined($constant)) {
478 * Validator subclass for constant values to be selected from a list.
479 * @author Joby Walker<zorloc@imperium.org>
481 class ValidatorConstantList extends Validator {
484 * Array of potential valid values
493 * Saves parameter as the instance variable $constantList.
494 * @param array List of valid values.
496 function ValidatorConstantList($constantList){
497 $this->constantList = $constantList;
502 * Checks the parameter to ensure that is is a constant, and within
504 * @param string $constant Value to check.
505 * @return boolean True if parameter is a constant and in the list, false else.
507 function validate ($constant){
508 if (defined($constant)) {
509 if (in_array($constant, $this->constantList, true)) {
518 * Validator subclass for an array.
519 * @author Joby Walker<zorloc@imperium.org>
521 class ValidatorArray extends Validator {
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
527 * @param array $array Value to check.
528 * @return boolean True if the value is and array and members are valid, false else.
530 function validate($array){
531 if(is_array($array)){
532 return $this->validMembers($array);
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.
542 function validMembers($array){
548 * Validator subclass for an array of strings.
549 * @author Joby Walker<zorloc@imperium.org>
551 class ValidatorArrayString extends Validator {
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.
558 function validMembers($array){
559 foreach ($array as $member){
560 if (!is_string($member)) {
569 * Validator subclass for an array of strings that must be in a list of
571 * @author Joby Walker<zorloc@imperium.org>
573 class ValidatorArrayStringList extends Validator {
576 * Array of potential valid values
585 * Saves parameter as the instance variable $stringList.
586 * @param array List of valid values.
588 function ValidatorArrayStringList($stringList){
589 $this->stringList = $stringList;
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,
600 function validMembers($array){
601 foreach ($array as $member){
602 if(!in_array($member, $stringList, true)){
612 * Convert old-style index.php configuration to config/config.ini
614 function ConvertToIni ($index) {
615 $config = "config/config.ini";
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.
622 //Revision 1.5 2003/12/07 19:25:41 carstenklapp
623 //Code Housecleaning: fixed syntax errors. (php -l *.php)
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.
629 //Revision 1.2 2003/01/28 06:31:00 zorloc
630 //Mostly done but ConfigArray will probably need some more helper methods.
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.
641 // c-hanging-comment-ender-p: nil
642 // indent-tabs-mode: nil