commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / HTML / QuickForm / Rule / Regex.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * Validates values using regular expressions
6 *
7 * PHP versions 4 and 5
8 *
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
14 *
15 * @category HTML
16 * @package HTML_QuickForm
17 * @author Bertrand Mansion <bmansion@mamasam.com>
18 * @copyright 2001-2009 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
20 * @version CVS: $Id: Regex.php,v 1.6 2009/04/04 21:34:04 avb Exp $
21 * @link http://pear.php.net/package/HTML_QuickForm
22 */
23
24 /**
25 * Abstract base class for QuickForm validation rules
26 */
27 require_once 'HTML/QuickForm/Rule.php';
28
29 /**
30 * Validates values using regular expressions
31 *
32 * @category HTML
33 * @package HTML_QuickForm
34 * @author Bertrand Mansion <bmansion@mamasam.com>
35 * @version Release: 3.2.11
36 * @since 3.2
37 */
38 class HTML_QuickForm_Rule_Regex extends HTML_QuickForm_Rule
39 {
40 /**
41 * Array of regular expressions
42 *
43 * Array is in the format:
44 * $_data['rulename'] = 'pattern';
45 *
46 * @var array
47 * @access private
48 */
49 var $_data = array(
50 'lettersonly' => '/^[a-zA-Z]+$/',
51 'alphanumeric' => '/^[a-zA-Z0-9]+$/',
52 'numeric' => '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/',
53 'nopunctuation' => '/^[^().\/\*\^\?#!@$%+=,\"\'><~\[\]{}]+$/',
54 'nonzero' => '/^-?[1-9][0-9]*/'
55 );
56
57 /**
58 * Validates a value using a regular expression
59 *
60 * @param string $value Value to be checked
61 * @param string $regex Regular expression
62 * @access public
63 * @return boolean true if value is valid
64 */
65 function validate($value, $regex = null)
66 {
67 // Fix for bug #10799: add 'D' modifier to regex
68 if (isset($this->_data[$this->name])) {
69 if (!preg_match($this->_data[$this->name] . 'D', $value)) {
70 return false;
71 }
72 } else {
73 if (!preg_match($regex . 'D', $value)) {
74 return false;
75 }
76 }
77 return true;
78 } // end func validate
79
80 /**
81 * Adds new regular expressions to the list
82 *
83 * @param string $name Name of rule
84 * @param string $pattern Regular expression pattern
85 * @access public
86 */
87 function addData($name, $pattern)
88 {
89 $this->_data[$name] = $pattern;
90 } // end func addData
91
92
93 function getValidationScript($options = null)
94 {
95 $regex = isset($this->_data[$this->name]) ? $this->_data[$this->name] : $options;
96
97 // bug #12376, converting unicode escapes and stripping 'u' modifier
98 if ($pos = strpos($regex, 'u', strrpos($regex, '/'))) {
99 $regex = substr($regex, 0, $pos) . substr($regex, $pos + 1);
100 $regex = preg_replace('/(?<!\\\\)(?>\\\\\\\\)*\\\\x{([a-fA-F0-9]+)}/', '\\u$1', $regex);
101 }
102
103 return array(" var regex = " . $regex . ";\n", "{jsVar} != '' && !regex.test({jsVar})");
104 } // end func getValidationScript
105
106 } // end class HTML_QuickForm_Rule_Regex
107 ?>