source: trunk/lib/FormValidator.inc.php @ 685

Last change on this file since 685 was 685, checked in by anonymous, 5 years ago

Various minor changes:

  • Allow $app->ohref() with no value to return a link to the current page (REQUEST_URI but without the QUERY_STRING).
  • Add some classes to HTML output to support Zurb Foundation 6.
  • Stop prepending scheme://host:port to URLs because we can't guess hte port at a proxy.
  • Include Auth_Simple.inc.php (copied from control.strangecode.com). $auth->login() now requires a callback funtion to verify credentials.
File size: 29.5 KB
RevLine 
[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.
36require_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]47if (is_null($var)) {
[550]48    $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")), MSG_ERR, __FILE__, __LINE__);
[136]49}
50if ($fv->anyErrors()) {
51    // Errors!
52}
53---------------------------------------------------------------------
[1]54 */
[42]55
[144]56// Credit card types are defined in class Validator.
57
58require_once 'codebase/lib/Validator.inc.php';
59
[502]60class 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:
[685]221                        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">&times;</span></a></div>';
[1]222                        break;
[42]223
[1]224                    case MSG_WARNING:
[685]225                        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">&times;</span></a></div>';
[1]226                        break;
[42]227
[1]228                    case MSG_SUCCESS:
[685]229                        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">&times;</span></a></div>';
[1]230                        break;
[42]231
[1]232                    case MSG_NOTICE:
233                    default:
[685]234                        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">&times;</span></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    /*
[597]311    * We were using the isEmpty method *wrong* for years and should have been using notEmpty because it is more grammatically correct.
[550]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
[665]366    * @param  bool   $negative_ok   Set to true if negative numbers will be allowed.
[550]367    * @param  const  $type  A LOG_* constant (see App->logMsg())
368    * @param  const  $file  Filename to log (usually __FILE__)
369    * @param  const  $line  Line number to log (usually __LINE__)
370    * @return bool   true if value is an integer
371    */
372    public function isInteger($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null)
[1]373    {
[550]374        if (Validator::isInteger(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
[144]375            return true;
376        } else {
[550]377            $this->addError($form_name, $msg, $type, $file, $line);
[1]378            return false;
379        }
380    }
381
382    /**
[550]383    * Check whether input is a float. Don't just use is_float() because the
384    * data coming from the user is *really* a string. Integers will also
385    * pass this test.
386    *
387    * @param  string $form_name the name of the incoming form variable
388    * @param  string $msg       the message to display on error
389    * @param  const  $type  A LOG_* constant (see App->logMsg())
390    * @param  const  $file  Filename to log (usually __FILE__)
391    * @param  const  $line  Line number to log (usually __LINE__)
392    * @return bool   true if value is a float
393    */
394    public function isFloat($form_name, $msg='', $negative_ok=false, $type=MSG_ERR, $file=null, $line=null)
[1]395    {
[550]396        if (Validator::isFloat(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
[144]397            return true;
398        } else {
[550]399            $this->addError($form_name, $msg, $type, $file, $line);
[1]400            return false;
401        }
402    }
403
404    /**
[550]405    * Check whether input is a Decimal or Fixed type. Use to check values to be stored in mysql decimal, numeric, num, or fixed types.
406    * The arguments $max and $dec should match M and D of the column definition "DECIMAL(M,D)".
407    * Note: some integers and floats will also pass this test.
408    * https://dev.mysql.com/doc/refman/5.5/en/fixed-point-types.html
409    *
410    * @param  string    $form_name      The name of the incoming form variable
411    * @param  int       $max            Total max number of digits.
412    * @param  int       $dec            Total max number of digits after the decimal place.
413    * @param  string    $msg            The message to display on error
414    * @param  bool      $negative_ok    If the value can be unsigned.
415    * @param  const  $type  A LOG_* constant (see App->logMsg())
416    * @param  const  $file  Filename to log (usually __FILE__)
417    * @param  const  $line  Line number to log (usually __LINE__)
418    * @return bool                      True if value is a decimal, false otherwise.
419    */
420    public function isDecimal($form_name, $max=10, $dec=2, $negative_ok=false, $msg='', $type=MSG_ERR, $file=null, $line=null)
[534]421    {
[550]422        if (Validator::isDecimal(getFormData($form_name), $max, $dec, $negative_ok, LOG_NOTICE, $file, $line)) {
[534]423            return true;
424        } else {
[550]425            // Set the example to a sequence of Ns with $max number of digits and a faction part length of $dec.
426            $msg = str_replace('{EX}', sprintf('%s.%s', str_repeat('N', $max - $dec), str_repeat('N', $dec)), $msg);
427            $this->addError($form_name, $msg, $type, $file, $line);
[534]428            return false;
429        }
430    }
431
432    /**
[550]433    * Check whether input is an array.
434    *
435    * @param  string $form_name the name of the incoming form variable
436    * @param  string $msg       the message to display on error
437    * @param  const  $type  A LOG_* constant (see App->logMsg())
438    * @param  const  $file  Filename to log (usually __FILE__)
439    * @param  const  $line  Line number to log (usually __LINE__)
440    * @return bool   true if value is an array
441    */
442    public function isArray($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]443    {
[550]444        if (Validator::isArray(getFormData($form_name), LOG_NOTICE, $file, $line)) {
[144]445            return true;
446        } else {
[550]447            $this->addError($form_name, $msg, $type, $file, $line);
[1]448            return false;
449        }
450    }
[42]451
[1]452    /**
[550]453    * Check whether input matches the specified perl regular expression
454    * pattern.
455    *
456    * @param  string $form_name        The name of the incoming form variable
457    * @param  int    $regex            Perl regex that the string must match
458    * @param  bool   $valid_on_match   Set to true to be valid if match, or false to be valid if the match fails.
459    * @param  string $msg              The message to display on error
460    * @param  const  $type  A LOG_* constant (see App->logMsg())
461    * @param  const  $file  Filename to log (usually __FILE__)
462    * @param  const  $line  Line number to log (usually __LINE__)
463    * @return bool   true if value passes regex test (or false if $valid_on_match=false)
464    */
465    public function checkRegex($form_name, $regex, $valid_on_match=true, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]466    {
[550]467        if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match, LOG_NOTICE, $file, $line)) {
[144]468            return true;
[1]469        } else {
[550]470            $this->addError($form_name, $msg, $type, $file, $line);
[144]471            return false;
[1]472        }
473    }
[42]474
[1]475    /**
[550]476    * Tests if the string length is between specified values. Whitespace excluded for min.
477    *
478    * @param  string $form_name the name of the incoming form variable
479    * @param  int    $min       minimum length of string, inclusive
480    * @param  int    $max       maximum length of string, inclusive
481    * @param  string $msg       the message to display on error
482    * @param  const  $type  A LOG_* constant (see App->logMsg())
483    * @param  const  $file  Filename to log (usually __FILE__)
484    * @param  const  $line  Line number to log (usually __LINE__)
485    * @return bool   true if string length is within given boundaries
486    */
487    public function stringLength($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]488    {
[550]489        if (Validator::stringLength(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
[144]490            return true;
491        } else {
[550]492            $this->addError($form_name, $msg, $type, $file, $line);
[1]493            return false;
494        }
495    }
496
497    /**
[550]498    * Check whether input is within a valid numeric range.
499    *
500    * @param  string $form_name the name of the incoming form variable
501    * @param  int    $min       minimum value of number, inclusive
502    * @param  int    $max       maximum value of number, inclusive
503    * @param  string $msg       the message to display on error
504    * @param  const  $type  A LOG_* constant (see App->logMsg())
505    * @param  const  $file  Filename to log (usually __FILE__)
506    * @param  const  $line  Line number to log (usually __LINE__)
507    * @return bool   true if no errors found, false otherwise
508    */
509    public function numericRange($form_name, $min, $max, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]510    {
[550]511        if (Validator::numericRange(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
[1]512            return true;
513        } else {
[550]514            $this->addError($form_name, $msg, $type, $file, $line);
[1]515            return false;
516        }
517    }
518
519    /**
[550]520    * Validates an email address based on the recommendations in RFC 3696.
521    * Is more loose than restrictive, to allow the many valid variants of
522    * email addresses while catching the most common mistakes.
523    * http://www.faqs.org/rfcs/rfc822.html
524    * http://www.faqs.org/rfcs/rfc2822.html
525    * http://www.faqs.org/rfcs/rfc3696.html
526    * http://www.faqs.org/rfcs/rfc1035.html
527    *
528    * @access  public
529    * @param   string  $form_name   The name of the incoming form variable.
530    * @param   bool    $strict      Run strict tests (check if the domain exists and has an MX record assigned)
531    * @param  const  $type  A LOG_* constant (see App->logMsg())
532    * @param  const  $file  Filename to log (usually __FILE__)
533    * @param  const  $line  Line number to log (usually __LINE__)
534    * @return  bool                 Validity of address.
535    * @author  Quinn Comendant <quinn@strangecode.com>
536    */
537    public function validateEmail($form_name, $strict=false, $type=MSG_ERR, $file=null, $line=null)
[1]538    {
[479]539        $app =& App::getInstance();
[144]540
[1]541        $email = getFormData($form_name);
[144]542
[1]543        if ('' == trim($email)) {
[305]544            // No email address provided, and that's okay.
[144]545            return true;
[1]546        }
[23]547
[468]548        // Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php).
[550]549        switch (Validator::validateEmail($email, $strict, LOG_NOTICE, $file, $line)) {
[468]550        case Validator::EMAIL_REGEX_FAIL:
[144]551            // Failed regex match.
[550]552            $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email)), $type, $file, $line);
[305]553            $app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
[23]554            return false;
[459]555
[550]556        case Validator::EMAIL_LENGTH_FAIL:
[144]557            // Failed length requirements.
[550]558            $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]559            $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
[1]560            return false;
[459]561
[550]562        case Validator::EMAIL_MX_FAIL:
[144]563            // Failed MX record test.
[550]564            $this->addError($form_name, sprintf(_("The email address <em>%s</em> does not have a valid domain name"), oTxt($email)), $type, $file, $line);
565            $app->logMsg(sprintf('The email address %s does not have a valid domain name.', oTxt($email)), LOG_NOTICE, __FILE__, __LINE__);
[23]566            return false;
[459]567
[550]568        case Validator::EMAIL_SUCCESS:
[144]569        default :
570            return true;
[1]571        }
572    }
573
574    /**
[550]575    * Check whether input is a valid phone number. Notice: it is now set
576    * to allow characters like - or () or + so people can type in a phone
577    * number that looks like: +1 (530) 555-1212
578    *
579    * @param  string  $form_name the name of the incoming form variable
580    * @param  const  $type  A LOG_* constant (see App->logMsg())
581    * @param  const  $file  Filename to log (usually __FILE__)
582    * @param  const  $line  Line number to log (usually __LINE__)
583    * @return bool    true if no errors found, false otherwise
584    */
585    public function validatePhone($form_name, $type=MSG_ERR, $file=null, $line=null)
[1]586    {
[468]587        $app =& App::getInstance();
588
[1]589        $phone = getFormData($form_name);
[42]590
[468]591        // Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php).
[550]592        switch (Validator::validatePhone($phone, LOG_NOTICE, $file, $line)) {
[468]593        case Validator::PHONE_REGEX_FAIL:
594            // Failed regex match.
[550]595            $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is not valid."), oTxt($phone)), $type, $file, $line);
[468]596            $app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
597            return false;
598
[550]599        case Validator::PHONE_LENGTH_FAIL:
[468]600            // Failed length requirements.
[550]601            $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]602            $app->logMsg(sprintf('The phone number %s must contain less than 25 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
[468]603            return false;
604
[550]605        case Validator::PHONE_SUCCESS:
[468]606        default :
607            return true;
608        }
[1]609    }
610
611    /**
[550]612    * Verifies that date can be processed by the strtotime function.
613    *
614    * @param  string  $form_name the name of the incoming form variable
615    * @param  string  $msg       the message to display on error
616    * @param  const  $type  A LOG_* constant (see App->logMsg())
617    * @param  const  $file  Filename to log (usually __FILE__)
618    * @param  const  $line  Line number to log (usually __LINE__)
619    * @return bool    true if no errors found, false otherwise
620    */
621    public function validateStrDate($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]622    {
[144]623        $app =& App::getInstance();
624
[550]625        if (Validator::validateStrDate(getFormData($form_name, ''), LOG_NOTICE, $file, $line)) {
[144]626            return true;
627        } else {
[550]628            $this->addError($form_name, $msg, $type, $file, $line);
[136]629            $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
[1]630            return false;
631        }
632    }
[42]633
634
[1]635    /**
[550]636    * Verifies credit card number using the Luhn (mod 10) algorithm.
637    * http://en.wikipedia.org/wiki/Luhn_algorithm
638    *
639    * @param  string  $form_name   The name of the incoming form variable.
640    * @param  string  $cc_type     Optional, card type to do specific checks. One of the Validator::CC_TYPE_* constants.
641    * @param  const  $type  A LOG_* constant (see App->logMsg())
642    * @param  const  $file  Filename to log (usually __FILE__)
643    * @param  const  $line  Line number to log (usually __LINE__)
644    * @return bool    true if no errors found, false otherwise
645    */
646    public function validateCCNumber($form_name, $cc_type=null, $type=MSG_ERR, $file=null, $line=null)
[1]647    {
[144]648        $cc_num = getFormData($form_name);
[459]649
[550]650        if (Validator::validateCCNumber($cc_num, $cc_type, LOG_NOTICE, $file, $line)) {
[1]651            return true;
652        } else {
[550]653            $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]654            return false;
655        }
656    }
657
658    /**
[550]659    * Check whether a file was selected for uploading. If file is missing, it's an error.
660    *
661    * @param  string $form_name the name of the incoming form variable
662    * @param  string $msg       the message to display on error
663    * @param  const  $type  A LOG_* constant (see App->logMsg())
664    * @param  const  $file  Filename to log (usually __FILE__)
665    * @param  const  $line  Line number to log (usually __LINE__)
666    * @return bool   true if no errors found, false otherwise
667    */
668    public function fileUploaded($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
[1]669    {
[550]670        if (Validator::fileUploaded($form_name, LOG_NOTICE, $file, $line)) {
[144]671            return true;
672        } else {
[550]673            $this->addError($form_name, $msg, $type, $file, $line);
[1]674            return false;
675        }
676    }
[487]677    /**
[550]678    * Check whether a file was selected for uploading. If file is missing, it's an error.
679    *
680    * @param  string $form_name the name of the incoming form variable
681    * @param  string $msg       the message to display on error
682    * @param  const  $type  A LOG_* constant (see App->logMsg())
683    * @param  const  $file  Filename to log (usually __FILE__)
684    * @param  const  $line  Line number to log (usually __LINE__)
685    * @return bool   true if no errors found, false otherwise
686    */
687    public function fileUploadSize($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
[487]688    {
[550]689        if (Validator::fileUploadSize($form_name, LOG_NOTICE, $file, $line)) {
[487]690            return true;
691        } else {
692            $msg = '' == $msg ? sprintf(_("Maximum filesize exceeded. Got %s, but limit is %s."), humanFileSize($_SERVER['CONTENT_LENGTH']), humanFileSize(phpIniGetBytes('upload_max_filesize'))) : $msg;
[550]693            $this->addError($form_name, $msg, $type, $file, $line);
[487]694            return false;
695        }
696    }
[42]697
[1]698} // THE END
699
Note: See TracBrowser for help on using the repository browser.