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

Last change on this file since 652 was 597, checked in by anonymous, 7 years ago

Remove useless logging

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