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

Last change on this file since 722 was 722, checked in by anonymous, 4 years ago

Refactor URLSlug() and cleanFileName(). Add simplifyAccents().

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