3 * HTMLFormField for inserting Captchas into a form.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 class HTMLCaptchaField extends HTMLFormField {
34 private $validationResult;
36 public function __construct( $params ){
37 parent::__construct( $params );
39 // For differentiating the type of form, mainly
40 if( isset( $params['prefix'] ) ){
41 $this->prefix = $params['prefix'];
46 * Get the captcha body. Don't include any of the surrounding table cells/rows
48 * @param $value String
51 public function getInputHTML( $value ){
55 public function validate( $data, $alldata ){
56 // We sent back the exists status of the captcha before. If it *doesn't* exist
57 // we actually want to validate this as true, because we don't want an angry red
58 // error message, just for the user to put the captcha in again
59 if( $data === false ){
67 * @param $request WebRequest
70 public function loadDataFromRequest( $request ){
71 $this->captcha = Captcha::factory();
72 $this->captcha->loadFromRequest( $request, $this );
73 if( !$this->captcha->exists() ){
74 // The captcha doesn't exist; probably because it's already been used and
75 // then deleted for security. Load the field up with a new captcha which
76 // will be shown to the user when the validation of said new object fails
77 $this->captcha = Captcha::newRandom();
80 // This will be useful as the difference between "the captcha doesn't exist" and
81 // "you answered the captcha wrongly"
82 return $this->captcha->exists();