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
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
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.
13 *
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.
18 *
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/**
24 * FormValidator.inc.php
25 *
26 * The FormValidator class provides a method for validating input from
27 * http requests and displaying errors.
28 *
29 * @requires  codebase/lib/Validator.inc.php
30 * @author    Quinn Comendant <quinn@strangecode.com>
31 * @version   1.8
32 *
33 * Example of use:
34---------------------------------------------------------------------
35// The object that validates form input.
36require_once 'codebase/lib/FormValidator.inc.php';
37$fv = new FormValidator();
38
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__);
47if (is_null($var)) {
48    $fv->addError('field_name', sprintf(_("%s is invalid."), _("Field name")), MSG_ERR, __FILE__, __LINE__);
49}
50if ($fv->anyErrors()) {
51    // Errors!
52}
53---------------------------------------------------------------------
54 */
55
56// Credit card types are defined in class Validator.
57
58require_once 'codebase/lib/Validator.inc.php';
59
60class FormValidator
61{
62
63    // Class parameters.
64    protected $_params = array(
65        'error' => ' sc-msg-error ',
66        'warning' => ' sc-msg-warning ',
67        'notice' => ' sc-msg-notice ',
68        'success' => ' sc-msg-success ',
69    );
70
71    // Array filling with error messages.
72    public $errors = array();
73
74    /**
75    * FormValidator constructor.
76    *
77    * @access public
78    * @param array $params Configuration parameters for this object.
79    */
80    public function __construct($params=array())
81    {
82        // Set custom parameters.
83        $this->setParam($params);
84    }
85
86    /**
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    */
92    public function setParam($params)
93    {
94        $app =& App::getInstance();
95
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    /**
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    */
114    public function getParam($param)
115    {
116        $app =& App::getInstance();
117
118        if (array_key_exists($param, $this->_params)) {
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    }
125
126    /**
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    */
136    public function getErrorList()
137    {
138        return $this->errors;
139    }
140
141    /**
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    */
151    public function addError($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
152    {
153        $this->errors[] = array(
154            'name' => $form_name,
155            'message' => $msg,
156            'type' => $type,
157            'file' => $file,
158            'line' => $line
159        );
160    }
161
162    /**
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    */
172    public function anyErrors($form_name=null)
173    {
174        if (isset($form_name)) {
175            foreach ($this->errors as $err) {
176                if ($err['name'] == $form_name) {
177                    return $err['type'];
178                }
179            }
180            return false;
181        } else {
182            return (sizeof($this->errors) > 0);
183        }
184    }
185
186    /**
187    * Reset the error list.
188    */
189    public function resetErrorList()
190    {
191        $this->errors = array();
192    }
193
194    /**
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    */
205    public function printErrorMessages($above='', $below='', $print_gotohash_js=false, $hash='sc-msg-formvalidator')
206    {
207        $app =& App::getInstance();
208        if ($this->anyErrors()) {
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
211            if ('' != $above) {
212                ?><div class="sc-above"><?php echo oTxt($above); ?></div><?php
213            }
214            foreach ($this->getErrorList() as $e) {
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'] . '] -->';
218                    }
219                    switch ($e['type']) {
220                    case MSG_ERR:
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>';
222                        break;
223
224                    case MSG_WARNING:
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>';
226                        break;
227
228                    case MSG_SUCCESS:
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>';
230                        break;
231
232                    case MSG_NOTICE:
233                    default:
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>';
235                        break;
236                    }
237                }
238            }
239            if ('' != $below) {
240                ?><div class="sc-below"><?php echo oTxt($below); ?></div><?php
241            }
242            ?></div><?php
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            }
252        }
253    }
254
255    /**
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    */
262    public function err($form_name, $marker=null)
263    {
264        if (false !== ($type = $this->anyErrors($form_name))) {
265            if (isset($marker)) {
266                echo $marker;
267            } else {
268                switch ($type) {
269                case MSG_ERR:
270                default:
271                    echo $this->getParam('error');
272                    break;
273
274                case MSG_WARNING:
275                    echo $this->getParam('warning');
276                    break;
277
278                case MSG_NOTICE:
279                    echo $this->getParam('notice');
280                    break;
281
282                case MSG_SUCCESS:
283                    echo $this->getParam('success');
284                    break;
285                }
286            }
287        }
288    }
289
290    /**
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)
301    {
302        if (Validator::notEmpty(getFormData($form_name), LOG_NOTICE, $file, $line)) {
303            return true;
304        } else {
305            $this->addError($form_name, $msg, $type, $file, $line);
306            return false;
307        }
308    }
309
310    /*
311    * We were using the isEmpty method *wrong* for years and should have been using notEmpty because 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().
313    * @since    03 Jun 2006 22:56:46
314    */
315    public function isEmpty($form_name, $msg='', $type=MSG_ERR, $file=null, $line=null)
316    {
317        return $this->notEmpty($form_name, $msg, $type, $file, $line);
318    }
319
320    /**
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)
331    {
332        if (Validator::isString(getFormData($form_name), LOG_NOTICE, $file, $line)) {
333            return true;
334        } else {
335            $this->addError($form_name, $msg, $type, $file, $line);
336            return false;
337        }
338    }
339
340    /**
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)
351    {
352        if (Validator::isNumber(getFormData($form_name), LOG_NOTICE, $file, $line)) {
353            return true;
354        } else {
355            $this->addError($form_name, $msg, $type, $file, $line);
356            return false;
357        }
358    }
359
360    /**
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  bool   $negative_ok   Set to true if negative numbers will be allowed.
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)
373    {
374        if (Validator::isInteger(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
375            return true;
376        } else {
377            $this->addError($form_name, $msg, $type, $file, $line);
378            return false;
379        }
380    }
381
382    /**
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)
395    {
396        if (Validator::isFloat(getFormData($form_name), $negative_ok, LOG_NOTICE, $file, $line)) {
397            return true;
398        } else {
399            $this->addError($form_name, $msg, $type, $file, $line);
400            return false;
401        }
402    }
403
404    /**
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)
421    {
422        if (Validator::isDecimal(getFormData($form_name), $max, $dec, $negative_ok, LOG_NOTICE, $file, $line)) {
423            return true;
424        } else {
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);
428            return false;
429        }
430    }
431
432    /**
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)
443    {
444        if (Validator::isArray(getFormData($form_name), LOG_NOTICE, $file, $line)) {
445            return true;
446        } else {
447            $this->addError($form_name, $msg, $type, $file, $line);
448            return false;
449        }
450    }
451
452    /**
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)
466    {
467        if (Validator::checkRegex(getFormData($form_name), $regex, $valid_on_match, LOG_NOTICE, $file, $line)) {
468            return true;
469        } else {
470            $this->addError($form_name, $msg, $type, $file, $line);
471            return false;
472        }
473    }
474
475    /**
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)
488    {
489        if (Validator::stringLength(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
490            return true;
491        } else {
492            $this->addError($form_name, $msg, $type, $file, $line);
493            return false;
494        }
495    }
496
497    /**
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)
510    {
511        if (Validator::numericRange(getFormData($form_name), $min, $max, LOG_NOTICE, $file, $line)) {
512            return true;
513        } else {
514            $this->addError($form_name, $msg, $type, $file, $line);
515            return false;
516        }
517    }
518
519    /**
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)
538    {
539        $app =& App::getInstance();
540
541        $email = getFormData($form_name);
542
543        if ('' == trim($email)) {
544            // No email address provided, and that's okay.
545            return true;
546        }
547
548        // Validator::validateEmail() returns a value that relates to the Validate::EMAIL_* constants (defined in Validator.inc.php).
549        switch (Validator::validateEmail($email, $strict, LOG_NOTICE, $file, $line)) {
550        case Validator::EMAIL_REGEX_FAIL:
551            // Failed regex match.
552            $this->addError($form_name, sprintf(_("The email address <em>%s</em> is formatted incorrectly."), oTxt($email)), $type, $file, $line);
553            $app->logMsg(sprintf('The email address %s is not valid.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
554            return false;
555
556        case Validator::EMAIL_LENGTH_FAIL:
557            // Failed length requirements.
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);
559            $app->logMsg(sprintf('The email address %s must contain less than 256 characters.', oTxt($email)), LOG_DEBUG, __FILE__, __LINE__);
560            return false;
561
562        case Validator::EMAIL_MX_FAIL:
563            // Failed MX record test.
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__);
566            return false;
567
568        case Validator::EMAIL_SUCCESS:
569        default :
570            return true;
571        }
572    }
573
574    /**
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)
586    {
587        $app =& App::getInstance();
588
589        $phone = getFormData($form_name);
590
591        // Validator::validateEmail() returns a value that relates to the Validate::PHONE_* constants (defined in Validator.inc.php).
592        switch (Validator::validatePhone($phone, LOG_NOTICE, $file, $line)) {
593        case Validator::PHONE_REGEX_FAIL:
594            // Failed regex match.
595            $this->addError($form_name, sprintf(_("The phone number <em>%s</em> is not valid."), oTxt($phone)), $type, $file, $line);
596            $app->logMsg(sprintf('The phone number %s is not valid.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
597            return false;
598
599        case Validator::PHONE_LENGTH_FAIL:
600            // Failed length requirements.
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);
602            $app->logMsg(sprintf('The phone number %s must contain less than 25 characters.', oTxt($phone)), LOG_DEBUG, __FILE__, __LINE__);
603            return false;
604
605        case Validator::PHONE_SUCCESS:
606        default :
607            return true;
608        }
609    }
610
611    /**
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)
622    {
623        $app =& App::getInstance();
624
625        if (Validator::validateStrDate(getFormData($form_name, ''), LOG_NOTICE, $file, $line)) {
626            return true;
627        } else {
628            $this->addError($form_name, $msg, $type, $file, $line);
629            $app->logMsg(sprintf('The string date %s is not valid.', getFormData($form_name)), LOG_DEBUG, __FILE__, __LINE__);
630            return false;
631        }
632    }
633
634
635    /**
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)
647    {
648        $cc_num = getFormData($form_name);
649
650        if (Validator::validateCCNumber($cc_num, $cc_type, LOG_NOTICE, $file, $line)) {
651            return true;
652        } else {
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);
654            return false;
655        }
656    }
657
658    /**
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)
669    {
670        if (Validator::fileUploaded($form_name, LOG_NOTICE, $file, $line)) {
671            return true;
672        } else {
673            $this->addError($form_name, $msg, $type, $file, $line);
674            return false;
675        }
676    }
677    /**
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)
688    {
689        if (Validator::fileUploadSize($form_name, LOG_NOTICE, $file, $line)) {
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;
693            $this->addError($form_name, $msg, $type, $file, $line);
694            return false;
695        }
696    }
697
698} // THE END
699
Note: See TracBrowser for help on using the repository browser.