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