1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 
	<?php
/**
 ***********************************************************************************************
 * @copyright 2004-2016 The Admidio Team
 * @see http://www.admidio.org/
 * @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
 ***********************************************************************************************
 */
require_once(SERVER_PATH.'/adm_program/libs/securimage/securimage.php');
/**
 * @class FormValidation
 * @brief Validate various content of form elements
 *
 * This class can be used to validate form input. Therefore the methods can be called and get the
 * form input as parameter. The method will return @b true if validation was succesfull. Otherwise
 * an AdmException will be thrown. To catch this exception all method calls of this class should
 * be within a try and catch structure. Also all method are declared static.
 *
 * @par Examples
 * @code // validate the captcha code
 * try
 * {
 *     FormValidation::checkCaptcha($_POST['captcha_code']);
 * }
 * catch(AdmException $e)
 * {
 *     $e->showHtml();
 * } @endcode
 */
class FormValidation
{
    /**
     * Checks if the value of the captcha input matches with the captcha image.
     * @param string $value Value of the captcha input field.
     * @throws AdmException SYS_CAPTCHA_CALC_CODE_INVALID, SYS_CAPTCHA_CODE_INVALID
     * @return true Returns @b true if the value matches the captcha image.
     *              Otherwise throw an exception SYS_CAPTCHA_CODE_INVALID.
     */
    public static function checkCaptcha($value)
    {
        global $gPreferences;
        $securimage = new Securimage();
        if ($securimage->check($value) === false)
        {
            if($gPreferences['captcha_type'] === 'calc')
            {
                throw new AdmException('SYS_CAPTCHA_CALC_CODE_INVALID');
            }
            throw new AdmException('SYS_CAPTCHA_CODE_INVALID');
        }
        return true;
    }
}