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 {
33 private $validationResult;
35 public function __construct( $params ) {
36 parent::__construct( $params );
38 // For differentiating the type of form, mainly
39 if ( isset( $params['prefix'] ) ) {
40 $this->prefix = $params['prefix'];
45 * Get the captcha body. Don't include any of the surrounding table cells/rows
47 * @param $value String
50 public function getInputHTML( $value ) {
54 public function validate( $data, $alldata ) {
55 // We sent back the exists status of the captcha before. If it *doesn't* exist
56 // we actually want to validate this as true, because we don't want an angry red
57 // error message, just for the user to put the captcha in again
58 if ( $data === false ) {
64 * @param $request WebRequest
67 public function loadDataFromRequest( $request ) {
68 $this->captcha = Captcha::factory();
69 $this->captcha->loadFromRequest( $request, $this );
70 if ( !$this->captcha->exists() ) {
71 // The captcha doesn't exist; probably because it's already been used and
72 // then deleted for security. Load the field up with a new captcha which
73 // will be shown to the user when the validation of said new object fails
74 $this->captcha = Captcha::newRandom();
77 // This will be useful as the difference between "the captcha doesn't exist" and
78 // "you answered the captcha wrongly"
79 return $this->captcha->exists();