[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[459] | 6 | * |
---|
[362] | 7 | * This file is part of The Strangecode Codebase. |
---|
| 8 | * |
---|
| 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
[459] | 13 | * |
---|
[362] | 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
[459] | 18 | * |
---|
[362] | 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
[42] | 24 | * FormValidator.inc.php |
---|
[1] | 25 | * |
---|
[136] | 26 | * The FormValidator class provides a method for validating input from |
---|
[1] | 27 | * http requests and displaying errors. |
---|
| 28 | * |
---|
[144] | 29 | * @requires codebase/lib/Validator.inc.php |
---|
[1] | 30 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[106] | 31 | * @version 1.8 |
---|
[1] | 32 | * |
---|
[136] | 33 | * Example of use: |
---|
| 34 | --------------------------------------------------------------------- |
---|
| 35 | // The object that validates form input. |
---|
| 36 | require_once 'codebase/lib/FormValidator.inc.php'; |
---|
| 37 | $fv = new FormValidator(); |
---|
| 38 | |
---|
[550] | 39 | $fv->empty('field_name', sprintf(_("%s cannot be blank."), _("Field name")), MSG_ERR, __FILE__, __LINE__); |
---|
| 40 | $fv->stringLength('field_name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Field name"), 0, 255), MSG_ERR, __FILE__, __LINE__); |
---|
| 41 | $fv->isInteger('field_name', sprintf(_("%s must be an integer."), _("Field name")), MSG_ERR, __FILE__, __LINE__); |
---|
| 42 | $fv->checkRegex('field_name', '/^\d{4}$|^$/', true, sprintf(_("%s must be in MMYY format."), _("Field name")), MSG_ERR, __FILE__, __LINE__); |
---|
| 43 | $fv->numericRange('field_name', 0, 65535, sprintf(_("%s must be a number between %d and %d."), _("Field name"), 0, 65535), MSG_ERR, __FILE__, __LINE__); |
---|
| 44 | $fv->validatePhone('field_name', MSG_ERR, __FILE__, __LINE__); |
---|
| 45 | $fv->validateEmail('field_name', MSG_ERR, __FILE__, __LINE__); |
---|
| 46 | $fv->validateStrDate('field_name', sprintf(_("%s must be a valid date in YYYY-MM-DD format."), _("Field name")), MSG_ERR, __FILE__, __LINE__); |
---|
[136] | 47 | if (is_null($var)) { |
---|
[550] | 48 | $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")), MSG_ERR, __FILE__, __LINE__); |
---|
[136] | 49 | } |
---|
| 50 | if ($fv->anyErrors()) { |
---|
| 51 | // Errors! |
---|
| 52 | } |
---|
| 53 | --------------------------------------------------------------------- |
---|
[1] | 54 | */ |
---|
[42] | 55 | |
---|
[144] | 56 | // Credit card types are defined in class Validator. |
---|
| 57 | |
---|
| 58 | require_once 'codebase/lib/Validator.inc.php'; |
---|
| 59 | |
---|
[502] | 60 | class FormValidator |
---|
| 61 | { |
---|
[144] | 62 | |
---|
[266] | 63 | // Class parameters. |
---|
[484] | 64 | protected $_params = array( |
---|
[266] | 65 | 'error' => ' sc-msg-error ', |
---|
| 66 | 'warning' => ' sc-msg-warning ', |
---|
| 67 | 'notice' => ' sc-msg-notice ', |
---|
| 68 | 'success' => ' sc-msg-success ', |
---|
[722] | 69 | 'use_raise_msg' => false, |
---|
[266] | 70 | ); |
---|
| 71 | |
---|
[100] | 72 | // Array filling with error messages. |
---|
[468] | 73 | public $errors = array(); |
---|
[459] | 74 | |
---|
[1] | 75 | /** |
---|
[550] | 76 | * FormValidator constructor. |
---|
| 77 | * |
---|
| 78 | * @access public |
---|
| 79 | * @param array $params Configuration parameters for this object. |
---|
| 80 | */ |
---|
[494] | 81 | public function __construct($params=array()) |
---|
[493] | 82 | { |
---|
| 83 | // Set custom parameters. |
---|
| 84 | $this->setParam($params); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
[550] | 88 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 89 | * |
---|
| 90 | * @access public |
---|
| 91 | * @param array $params Array of parameters (key => val pairs). |
---|
| 92 | */ |
---|
[468] | 93 | public function setParam($params) |
---|
[266] | 94 | { |
---|
[479] | 95 | $app =& App::getInstance(); |
---|
[459] | 96 | |
---|
[266] | 97 | if (isset($params) && is_array($params)) { |
---|
| 98 | // Merge new parameters with old overriding only those passed. |
---|
| 99 | $this->_params = array_merge($this->_params, $params); |
---|
| 100 | } else { |
---|
| 101 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
[550] | 106 | * Return the value of a parameter, if it exists. |
---|
| 107 | * |
---|
| 108 | * @access public |
---|
| 109 | * @param string $param Which parameter to return. |
---|
| 110 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 111 | * @param const $file Filename to log (usually __FILE__) |
---|
| 112 | * @param const $line Line number to log (usually __LINE__) |
---|
| 113 | * @return mixed Configured parameter value. |
---|
| 114 | */ |
---|
[468] | 115 | public function getParam($param) |
---|
[266] | 116 | { |
---|
[479] | 117 | $app =& App::getInstance(); |
---|
[459] | 118 | |
---|
[478] | 119 | if (array_key_exists($param, $this->_params)) { |
---|
[266] | 120 | return $this->_params[$param]; |
---|
| 121 | } else { |
---|
| 122 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 123 | return null; |
---|
| 124 | } |
---|
| 125 | } |
---|
[459] | 126 | |
---|
[266] | 127 | /** |
---|
[550] | 128 | * Return the current list of errors. |
---|
| 129 | * |
---|
| 130 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 131 | * @param const $file Filename to log (usually __FILE__) |
---|
| 132 | * @param const $line Line number to log (usually __LINE__) |
---|
| 133 | * @return array an array of errors in the following arrangement: |
---|
| 134 | * keys: the name of the variable with an error |
---|
| 135 | * vals: the message to display for that error |
---|
| 136 | */ |
---|
[468] | 137 | public function getErrorList() |
---|
[1] | 138 | { |
---|
| 139 | return $this->errors; |
---|
| 140 | } |
---|
[42] | 141 | |
---|
[1] | 142 | /** |
---|
[550] | 143 | * Add an error to the errors stack. |
---|
| 144 | * |
---|
| 145 | * @param string $form_name The name of the incoming form variable. |
---|
| 146 | * @param string $msg The error message for that form. |
---|
| 147 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 148 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 149 | * @param string $file __FILE__. |
---|
| 150 | * @param string $line __LINE__. |
---|
| 151 | */ |
---|
[468] | 152 | public function addError($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 153 | { |
---|
[722] | 154 | $app =& App::getInstance(); |
---|
| 155 | |
---|
| 156 | if (true === $this->getParam('use_raise_msg')) { |
---|
| 157 | $app->raiseMsg($msg, $type, $file, $line); |
---|
| 158 | } |
---|
| 159 | |
---|
[1] | 160 | $this->errors[] = array( |
---|
| 161 | 'name' => $form_name, |
---|
| 162 | 'message' => $msg, |
---|
| 163 | 'type' => $type, |
---|
| 164 | 'file' => $file, |
---|
| 165 | 'line' => $line |
---|
| 166 | ); |
---|
| 167 | } |
---|
[42] | 168 | |
---|
[1] | 169 | /** |
---|
[550] | 170 | * Check whether any errors have been triggered. |
---|
| 171 | * |
---|
| 172 | * @param string $form_name the name of the incoming form variable |
---|
| 173 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 174 | * @param const $file Filename to log (usually __FILE__) |
---|
| 175 | * @param const $line Line number to log (usually __LINE__) |
---|
| 176 | * @return bool true if any errors were found, or if found for |
---|
| 177 | * a variable of $form_name, false otherwise |
---|
| 178 | */ |
---|
[468] | 179 | public function anyErrors($form_name=null) |
---|
[1] | 180 | { |
---|
| 181 | if (isset($form_name)) { |
---|
| 182 | foreach ($this->errors as $err) { |
---|
| 183 | if ($err['name'] == $form_name) { |
---|
[178] | 184 | return $err['type']; |
---|
[1] | 185 | } |
---|
| 186 | } |
---|
| 187 | return false; |
---|
[144] | 188 | } else { |
---|
[459] | 189 | return (sizeof($this->errors) > 0); |
---|
[1] | 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
[550] | 194 | * Reset the error list. |
---|
| 195 | */ |
---|
[468] | 196 | public function resetErrorList() |
---|
[1] | 197 | { |
---|
| 198 | $this->errors = array(); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
[550] | 202 | * Prints the HTML for displaying error messages. |
---|
| 203 | * |
---|
| 204 | * @param string $above Additional message to print above error messages (e.g. "Oops!"). |
---|
| 205 | * @param string $below Additional message to print below error messages (e.g. "Please fix and resubmit"). |
---|
| 206 | * @param string $print_gotohash_js Print a line of javascript that scrolls the browser window down to view any error messages. |
---|
| 207 | * @param string $hash The #hashtag to scroll to. |
---|
| 208 | * @access public |
---|
| 209 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 210 | * @since 15 Jul 2005 01:39:14 |
---|
| 211 | */ |
---|
[468] | 212 | public function printErrorMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg-formvalidator') |
---|
[1] | 213 | { |
---|
[136] | 214 | $app =& App::getInstance(); |
---|
[1] | 215 | if ($this->anyErrors()) { |
---|
[550] | 216 | // data-gotohash="âŠ" is to be used by sites that refuse inline JS (via CSP) but needs to be referenced from an external JS. |
---|
| 217 | ?><div class="sc-msg" id="sc-msg-formvalidator" data-gotohash="<?php echo oTxt($hash); ?>"><?php |
---|
[393] | 218 | if ('' != $above) { |
---|
| 219 | ?><div class="sc-above"><?php echo oTxt($above); ?></div><?php |
---|
| 220 | } |
---|
[333] | 221 | foreach ($this->getErrorList() as $e) { |
---|
[136] | 222 | if ('' != $e['message'] && is_string($e['message'])) { |
---|
| 223 | if (error_reporting() > 0 && $app->getParam('display_errors') && isset($e['file']) && isset($e['line'])) { |
---|
| 224 | echo "\n<!-- [" . $e['file'] . ' : ' . $e['line'] . '] -->'; |
---|
[1] | 225 | } |
---|
[136] | 226 | switch ($e['type']) { |
---|
[1] | 227 | case MSG_ERR: |
---|
[685] | 228 | echo '<div data-alert data-closable class="sc-msg-error alert-box callout alert">' . $e['message'] . '<a class="close close-button" aria-label="Dismiss alert" data-close><span aria-hidden="true">×</span></a></div>'; |
---|
[1] | 229 | break; |
---|
[42] | 230 | |
---|
[1] | 231 | case MSG_WARNING: |
---|
[685] | 232 | echo '<div data-alert data-closable class="sc-msg-warning alert-box callout warning">' . $e['message'] . '<a class="close close-button" aria-label="Dismiss alert" data-close><span aria-hidden="true">×</span></a></div>'; |
---|
[1] | 233 | break; |
---|
[42] | 234 | |
---|
[1] | 235 | case MSG_SUCCESS: |
---|
[685] | 236 | echo '<div data-alert data-closable class="sc-msg-success alert-box callout success">' . $e['message'] . '<a class="close close-button" aria-label="Dismiss alert" data-close><span aria-hidden="true">×</span></a></div>'; |
---|
[1] | 237 | break; |
---|
[42] | 238 | |
---|
[1] | 239 | case MSG_NOTICE: |
---|
| 240 | default: |
---|
[685] | 241 | echo '<div data-alert data-closable class="sc-msg-notice alert-box callout primary info">' . $e['message'] . '<a class="close close-button" aria-label="Dismiss alert" data-close><span aria-hidden="true">×</span></a></div>'; |
---|
[1] | 242 | break; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | } |
---|
[393] | 246 | if ('' != $below) { |
---|
| 247 | ?><div class="sc-below"><?php echo oTxt($below); ?></div><?php |
---|
| 248 | } |
---|
[1] | 249 | ?></div><?php |
---|
[413] | 250 | if ($print_gotohash_js) { |
---|
| 251 | ?> |
---|
| 252 | <script type="text/javascript"> |
---|
| 253 | /* <![CDATA[ */ |
---|
| 254 | window.location.hash = '#<?php echo urlencode($hash); ?>'; |
---|
| 255 | /* ]]> */ |
---|
| 256 | </script> |
---|
| 257 | <?php |
---|
| 258 | } |
---|
[1] | 259 | } |
---|
| 260 | } |
---|
[42] | 261 | |
---|
[1] | 262 | /** |
---|
[550] | 263 | * If this form has an error, print an error marker like "<<". |
---|
| 264 | * |
---|
| 265 | * @param string $form_name the name of the incoming form variable |
---|
| 266 | * @param string $marker A string to print if there is an error. if |
---|
| 267 | * not provided, use default. |
---|
| 268 | */ |
---|
[468] | 269 | public function err($form_name, $marker=null) |
---|
[1] | 270 | { |
---|
[178] | 271 | if (false !== ($type = $this->anyErrors($form_name))) { |
---|
[1] | 272 | if (isset($marker)) { |
---|
| 273 | echo $marker; |
---|
| 274 | } else { |
---|
[178] | 275 | switch ($type) { |
---|
| 276 | case MSG_ERR: |
---|
| 277 | default: |
---|
[266] | 278 | echo $this->getParam('error'); |
---|
[178] | 279 | break; |
---|
| 280 | |
---|
| 281 | case MSG_WARNING: |
---|
[266] | 282 | echo $this->getParam('warning'); |
---|
[178] | 283 | break; |
---|
| 284 | |
---|
[266] | 285 | case MSG_NOTICE: |
---|
| 286 | echo $this->getParam('notice'); |
---|
[178] | 287 | break; |
---|
| 288 | |
---|
[266] | 289 | case MSG_SUCCESS: |
---|
| 290 | echo $this->getParam('success'); |
---|
[178] | 291 | break; |
---|
| 292 | } |
---|
[1] | 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
[550] | 298 | * Ensure the length of string is non-zero. |
---|
| 299 | * |
---|
| 300 | * @param string $form_name the name of the incoming form variable |
---|
| 301 | * @param string $msg the message to display on error |
---|
| 302 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 303 | * @param const $file Filename to log (usually __FILE__) |
---|
| 304 | * @param const $line Line number to log (usually __LINE__) |
---|
| 305 | * @return bool true if form is not empty, false otherwise. |
---|
| 306 | */ |
---|
| 307 | public function notEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 308 | { |
---|
[550] | 309 | if (Validator::notEmpty(getFormData($form_name), LOG_NOTICE, $file, $line)) { |
---|
[1] | 310 | return true; |
---|
| 311 | } else { |
---|
[550] | 312 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 313 | return false; |
---|
| 314 | } |
---|
| 315 | } |
---|
[459] | 316 | |
---|
[144] | 317 | /* |
---|
[597] | 318 | * We were using the isEmpty method *wrong* for years and should have been using notEmpty because it is more grammatically correct. |
---|
[550] | 319 | * Because the only use is to ensure a value is not empty, we're simply going to alias this method to notEmpty(). |
---|
[144] | 320 | * @since 03 Jun 2006 22:56:46 |
---|
| 321 | */ |
---|
[550] | 322 | public function isEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 323 | { |
---|
[550] | 324 | return $this->notEmpty($form_name, $msg, $type, $file, $line); |
---|
[1] | 325 | } |
---|
| 326 | |
---|
| 327 | /** |
---|
[550] | 328 | * Check whether input is a string. |
---|
| 329 | * |
---|
| 330 | * @param string $form_name the name of the incoming form variable |
---|
| 331 | * @param string $msg the message to display on error |
---|
| 332 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 333 | * @param const $file Filename to log (usually __FILE__) |
---|
| 334 | * @param const $line Line number to log (usually __LINE__) |
---|
| 335 | * @return bool true if form is a string, false otherwise. |
---|
| 336 | */ |
---|
| 337 | public function isString($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 338 | { |
---|
[550] | 339 | if (Validator::isString(getFormData($form_name), LOG_NOTICE, $file, $line)) { |
---|
[144] | 340 | return true; |
---|
| 341 | } else { |
---|
[550] | 342 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 343 | return false; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
[550] | 348 | * Check whether input is a number. Allows negative numbers. |
---|
| 349 | * |
---|
| 350 | * @param string $form_name the name of the incoming form variable |
---|
| 351 | * @param string $msg the message to display on error |
---|
| 352 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 353 | * @param const $file Filename to log (usually __FILE__) |
---|
| 354 | * @param const $line Line number to log (usually __LINE__) |
---|
| 355 | * @return bool true if no errors found, false otherwise |
---|
| 356 | */ |
---|
| 357 | public function isNumber($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 358 | { |
---|
[550] | 359 | if (Validator::isNumber(getFormData($form_name), LOG_NOTICE, $file, $line)) { |
---|
[144] | 360 | return true; |
---|
| 361 | } else { |
---|
[550] | 362 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 363 | return false; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | /** |
---|
[550] | 368 | * addError if input is NOT an integer. Don't just use is_int() because the |
---|
| 369 | * data coming from the user is *really* a string. |
---|
| 370 | * |
---|
| 371 | * @param string $form_name the name of the incoming form variable |
---|
| 372 | * @param string $msg the message to display on error |
---|
[665] | 373 | * @param bool $negative_ok Set to true if negative numbers will be allowed. |
---|
[550] | 374 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 375 | * @param const $file Filename to log (usually __FILE__) |
---|
| 376 | * @param const $line Line number to log (usually __LINE__) |
---|
| 377 | * @return bool true if value is an integer |
---|
| 378 | */ |
---|
| 379 | public function isInteger($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 380 | { |
---|
[550] | 381 | if (Validator::isInteger(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) { |
---|
[144] | 382 | return true; |
---|
| 383 | } else { |
---|
[550] | 384 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 385 | return false; |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /** |
---|
[550] | 390 | * Check whether input is a float. Don't just use is_float() because the |
---|
| 391 | * data coming from the user is *really* a string. Integers will also |
---|
| 392 | * pass this test. |
---|
| 393 | * |
---|
| 394 | * @param string $form_name the name of the incoming form variable |
---|
| 395 | * @param string $msg the message to display on error |
---|
| 396 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 397 | * @param const $file Filename to log (usually __FILE__) |
---|
| 398 | * @param const $line Line number to log (usually __LINE__) |
---|
| 399 | * @return bool true if value is a float |
---|
| 400 | */ |
---|
| 401 | public function isFloat($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 402 | { |
---|
[550] | 403 | if (Validator::isFloat(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) { |
---|
[144] | 404 | return true; |
---|
| 405 | } else { |
---|
[550] | 406 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 407 | return false; |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | /** |
---|
[550] | 412 | * Check whether input is a Decimal or Fixed type. Use to check values to be stored in mysql decimal, numeric, num, or fixed types. |
---|
| 413 | * The arguments $max and $dec should match M and D of the column definition "DECIMAL(M,D)". |
---|
| 414 | * Note: some integers and floats will also pass this test. |
---|
| 415 | * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html |
---|
| 416 | * |
---|
| 417 | * @param string $form_name The name of the incoming form variable |
---|
| 418 | * @param int $max Total max number of digits. |
---|
| 419 | * @param int $dec Total max number of digits after the decimal place. |
---|
| 420 | * @param string $msg The message to display on error |
---|
| 421 | * @param bool $negative_ok If the value can be unsigned. |
---|
| 422 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 423 | * @param const $file Filename to log (usually __FILE__) |
---|
| 424 | * @param const $line Line number to log (usually __LINE__) |
---|
| 425 | * @return bool True if value is a decimal, false otherwise. |
---|
| 426 | */ |
---|
| 427 | public function isDecimal($form_name, $max=10, $dec=2, $negative_ok=false, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[534] | 428 | { |
---|
[550] | 429 | if (Validator::isDecimal(getFormData($form_name), $max, $dec, $negative_ok, LOG_NOTICE, $file, $line)) { |
---|
[534] | 430 | return true; |
---|
| 431 | } else { |
---|
[550] | 432 | // Set the example to a sequence of Ns with $max number of digits and a faction part length of $dec. |
---|
| 433 | $msg = str_replace('{EX}', sprintf('%s.%s', str_repeat('N', $max - $dec), str_repeat('N', $dec)), $msg); |
---|
| 434 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[534] | 435 | return false; |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | /** |
---|
[550] | 440 | * Check whether input is an array. |
---|
| 441 | * |
---|
| 442 | * @param string $form_name the name of the incoming form variable |
---|
| 443 | * @param string $msg the message to display on error |
---|
| 444 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 445 | * @param const $file Filename to log (usually __FILE__) |
---|
| 446 | * @param const $line Line number to log (usually __LINE__) |
---|
| 447 | * @return bool true if value is an array |
---|
| 448 | */ |
---|
| 449 | public function isArray($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 450 | { |
---|
[550] | 451 | if (Validator::isArray(getFormData($form_name), LOG_NOTICE, $file, $line)) { |
---|
[144] | 452 | return true; |
---|
| 453 | } else { |
---|
[550] | 454 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 455 | return false; |
---|
| 456 | } |
---|
| 457 | } |
---|
[42] | 458 | |
---|
[1] | 459 | /** |
---|
[550] | 460 | * Check whether input matches the specified perl regular expression |
---|
| 461 | * pattern. |
---|
| 462 | * |
---|
| 463 | * @param string $form_name The name of the incoming form variable |
---|
| 464 | * @param int $regex Perl regex that the string must match |
---|
| 465 | * @param bool $valid_on_match Set to true to be valid if match, or false to be valid if the match fails. |
---|
| 466 | * @param string $msg The message to display on error |
---|
| 467 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 468 | * @param const $file Filename to log (usually __FILE__) |
---|
| 469 | * @param const $line Line number to log (usually __LINE__) |
---|
| 470 | * @return bool true if value passes regex test (or false if $valid_on_match=false) |
---|
| 471 | */ |
---|
| 472 | public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 473 | { |
---|
[550] | 474 | if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match, LOG_NOTICE, $file, $line)) { |
---|
[144] | 475 | return true; |
---|
[1] | 476 | } else { |
---|
[550] | 477 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[144] | 478 | return false; |
---|
[1] | 479 | } |
---|
| 480 | } |
---|
[42] | 481 | |
---|
[1] | 482 | /** |
---|
[550] | 483 | * Tests if the string length is between specified values. Whitespace excluded for min. |
---|
| 484 | * |
---|
| 485 | * @param string $form_name the name of the incoming form variable |
---|
| 486 | * @param int $min minimum length of string, inclusive |
---|
| 487 | * @param int $max maximum length of string, inclusive |
---|
| 488 | * @param string $msg the message to display on error |
---|
| 489 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 490 | * @param const $file Filename to log (usually __FILE__) |
---|
| 491 | * @param const $line Line number to log (usually __LINE__) |
---|
| 492 | * @return bool true if string length is within given boundaries |
---|
| 493 | */ |
---|
| 494 | public function stringLength($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 495 | { |
---|
[550] | 496 | if (Validator::stringLength(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) { |
---|
[144] | 497 | return true; |
---|
| 498 | } else { |
---|
[550] | 499 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 500 | return false; |
---|
| 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | /** |
---|
[550] | 505 | * Check whether input is within a valid numeric range. |
---|
| 506 | * |
---|
| 507 | * @param string $form_name the name of the incoming form variable |
---|
| 508 | * @param int $min minimum value of number, inclusive |
---|
| 509 | * @param int $max maximum value of number, inclusive |
---|
| 510 | * @param string $msg the message to display on error |
---|
| 511 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 512 | * @param const $file Filename to log (usually __FILE__) |
---|
| 513 | * @param const $line Line number to log (usually __LINE__) |
---|
| 514 | * @return bool true if no errors found, false otherwise |
---|
| 515 | */ |
---|
| 516 | public function numericRange($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 517 | { |
---|
[550] | 518 | if (Validator::numericRange(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) { |
---|
[1] | 519 | return true; |
---|
| 520 | } else { |
---|
[550] | 521 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 522 | return false; |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** |
---|
[550] | 527 | * Validates an email address based on the recommendations in RFC 3696. |
---|
| 528 | * Is more loose than restrictive, to allow the many valid variants of |
---|
| 529 | * email addresses while catching the most common mistakes. |
---|
| 530 | * http://www.faqs.org/rfcs/rfc822.html |
---|
| 531 | * http://www.faqs.org/rfcs/rfc2822.html |
---|
| 532 | * http://www.faqs.org/rfcs/rfc3696.html |
---|
| 533 | * http://www.faqs.org/rfcs/rfc1035.html |
---|
| 534 | * |
---|
| 535 | * @access public |
---|
| 536 | * @param string $form_name The name of the incoming form variable. |
---|
| 537 | * @param bool $strict Run strict tests (check if the domain exists and has an MX record assigned) |
---|
| 538 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 539 | * @param const $file Filename to log (usually __FILE__) |
---|
| 540 | * @param const $line Line number to log (usually __LINE__) |
---|
| 541 | * @return bool Validity of address. |
---|
| 542 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 543 | */ |
---|
| 544 | public function validateEmail($form_name, $strict=false, $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 545 | { |
---|
[479] | 546 | $app =& App::getInstance(); |
---|
[144] | 547 | |
---|
[1] | 548 | $email = getFormData($form_name); |
---|
[144] | 549 | |
---|
[1] | 550 | if ('' == trim($email)) { |
---|
[305] | 551 | // No email address provided, and that's okay. |
---|
[144] | 552 | return true; |
---|
[1] | 553 | } |
---|
[23] | 554 | |
---|
[468] | 555 | // Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php). |
---|
[550] | 556 | switch (Validator::validateEmail($email, $strict, LOG_NOTICE, $file, $line)) { |
---|
[468] | 557 | case Validator::EMAIL_REGEX_FAIL: |
---|
[144] | 558 | // Failed regex match. |
---|
[550] | 559 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email)), $type, $file, $line); |
---|
[305] | 560 | $app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[23] | 561 | return false; |
---|
[459] | 562 | |
---|
[550] | 563 | case Validator::EMAIL_LENGTH_FAIL: |
---|
[144] | 564 | // Failed length requirements. |
---|
[550] | 565 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> is too long (email addresses must have fewer than 256 characters)."), oTxt($email)), $type, $file, $line); |
---|
[305] | 566 | $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 567 | return false; |
---|
[459] | 568 | |
---|
[550] | 569 | case Validator::EMAIL_MX_FAIL: |
---|
[144] | 570 | // Failed MX record test. |
---|
[550] | 571 | $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name"), oTxt($email)), $type, $file, $line); |
---|
| 572 | $app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_NOTICE, __FILE__, __LINE__); |
---|
[23] | 573 | return false; |
---|
[459] | 574 | |
---|
[550] | 575 | case Validator::EMAIL_SUCCESS: |
---|
[144] | 576 | default : |
---|
| 577 | return true; |
---|
[1] | 578 | } |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /** |
---|
[550] | 582 | * Check whether input is a valid phone number. Notice: it is now set |
---|
| 583 | * to allow characters like - or () or + so people can type in a phone |
---|
| 584 | * number that looks like: +1 (530) 555-1212 |
---|
| 585 | * |
---|
| 586 | * @param string $form_name the name of the incoming form variable |
---|
| 587 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 588 | * @param const $file Filename to log (usually __FILE__) |
---|
| 589 | * @param const $line Line number to log (usually __LINE__) |
---|
| 590 | * @return bool true if no errors found, false otherwise |
---|
| 591 | */ |
---|
| 592 | public function validatePhone($form_name, $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 593 | { |
---|
[468] | 594 | $app =& App::getInstance(); |
---|
| 595 | |
---|
[1] | 596 | $phone = getFormData($form_name); |
---|
[42] | 597 | |
---|
[468] | 598 | // Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php). |
---|
[550] | 599 | switch (Validator::validatePhone($phone, LOG_NOTICE, $file, $line)) { |
---|
[468] | 600 | case Validator::PHONE_REGEX_FAIL: |
---|
| 601 | // Failed regex match. |
---|
[550] | 602 | $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is not valid."), oTxt($phone)), $type, $file, $line); |
---|
[468] | 603 | $app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 604 | return false; |
---|
| 605 | |
---|
[550] | 606 | case Validator::PHONE_LENGTH_FAIL: |
---|
[468] | 607 | // Failed length requirements. |
---|
[550] | 608 | $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is too long (phone number must have fewer than 25 characters)."), oTxt($phone)), $type, $file, $line); |
---|
[561] | 609 | $app->logMsg(sprintf('The phone number %s must contain less than 25 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[468] | 610 | return false; |
---|
| 611 | |
---|
[550] | 612 | case Validator::PHONE_SUCCESS: |
---|
[468] | 613 | default : |
---|
| 614 | return true; |
---|
| 615 | } |
---|
[1] | 616 | } |
---|
| 617 | |
---|
| 618 | /** |
---|
[550] | 619 | * Verifies that date can be processed by the strtotime function. |
---|
| 620 | * |
---|
| 621 | * @param string $form_name the name of the incoming form variable |
---|
| 622 | * @param string $msg the message to display on error |
---|
| 623 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 624 | * @param const $file Filename to log (usually __FILE__) |
---|
| 625 | * @param const $line Line number to log (usually __LINE__) |
---|
| 626 | * @return bool true if no errors found, false otherwise |
---|
| 627 | */ |
---|
| 628 | public function validateStrDate($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 629 | { |
---|
[144] | 630 | $app =& App::getInstance(); |
---|
| 631 | |
---|
[550] | 632 | if (Validator::validateStrDate(getFormData($form_name, ''), LOG_NOTICE, $file, $line)) { |
---|
[144] | 633 | return true; |
---|
| 634 | } else { |
---|
[550] | 635 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[136] | 636 | $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 637 | return false; |
---|
| 638 | } |
---|
| 639 | } |
---|
[42] | 640 | |
---|
| 641 | |
---|
[1] | 642 | /** |
---|
[550] | 643 | * Verifies credit card number using the Luhn (mod 10) algorithm. |
---|
| 644 | * http://en.wikipedia.org/wiki/Luhn_algorithm |
---|
| 645 | * |
---|
| 646 | * @param string $form_name The name of the incoming form variable. |
---|
| 647 | * @param string $cc_type Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants. |
---|
| 648 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 649 | * @param const $file Filename to log (usually __FILE__) |
---|
| 650 | * @param const $line Line number to log (usually __LINE__) |
---|
| 651 | * @return bool true if no errors found, false otherwise |
---|
| 652 | */ |
---|
| 653 | public function validateCCNumber($form_name, $cc_type=null, $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 654 | { |
---|
[144] | 655 | $cc_num = getFormData($form_name); |
---|
[459] | 656 | |
---|
[550] | 657 | if (Validator::validateCCNumber($cc_num, $cc_type, LOG_NOTICE, $file, $line)) { |
---|
[1] | 658 | return true; |
---|
| 659 | } else { |
---|
[550] | 660 | $this->addError($form_name, sprintf(_("The credit card number you entered is not valid. Please check the number and try again."), $cc_num), $type, $file, $line); |
---|
[1] | 661 | return false; |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | /** |
---|
[550] | 666 | * Check whether a file was selected for uploading. If file is missing, it's an error. |
---|
| 667 | * |
---|
| 668 | * @param string $form_name the name of the incoming form variable |
---|
| 669 | * @param string $msg the message to display on error |
---|
| 670 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 671 | * @param const $file Filename to log (usually __FILE__) |
---|
| 672 | * @param const $line Line number to log (usually __LINE__) |
---|
| 673 | * @return bool true if no errors found, false otherwise |
---|
| 674 | */ |
---|
| 675 | public function fileUploaded($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[1] | 676 | { |
---|
[550] | 677 | if (Validator::fileUploaded($form_name, LOG_NOTICE, $file, $line)) { |
---|
[144] | 678 | return true; |
---|
| 679 | } else { |
---|
[550] | 680 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[1] | 681 | return false; |
---|
| 682 | } |
---|
| 683 | } |
---|
[487] | 684 | /** |
---|
[550] | 685 | * Check whether a file was selected for uploading. If file is missing, it's an error. |
---|
| 686 | * |
---|
| 687 | * @param string $form_name the name of the incoming form variable |
---|
| 688 | * @param string $msg the message to display on error |
---|
| 689 | * @param const $type A LOG_* constant (see App->logMsg()) |
---|
| 690 | * @param const $file Filename to log (usually __FILE__) |
---|
| 691 | * @param const $line Line number to log (usually __LINE__) |
---|
| 692 | * @return bool true if no errors found, false otherwise |
---|
| 693 | */ |
---|
| 694 | public function fileUploadSize($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null) |
---|
[487] | 695 | { |
---|
[807] | 696 | if (Validator::fileUploadSize(LOG_NOTICE, $file, $line)) { |
---|
[487] | 697 | return true; |
---|
| 698 | } else { |
---|
| 699 | $msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg; |
---|
[550] | 700 | $this->addError($form_name, $msg, $type, $file, $line); |
---|
[487] | 701 | return false; |
---|
| 702 | } |
---|
| 703 | } |
---|
[42] | 704 | |
---|
[1] | 705 | } // THE END |
---|
| 706 | |
---|