summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ConfirmEdit/includes/auth/CaptchaAuthenticationRequest.php
blob: d5d6e5a1119d5349926924abee6752df5589e386 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

use MediaWiki\Auth\AuthenticationRequest;
use MediaWiki\Auth\AuthManager;

/**
 * Generic captcha authentication request class. A captcha consist some data stored in the session
 * (e.g. a question and its answer), an ID that references the data, and a solution.
 */
class CaptchaAuthenticationRequest extends AuthenticationRequest {
	/** @var string Identifier of the captcha. Used internally to remember which captcha was used. */
	public $captchaId;

	/** @var array Information about the captcha (e.g. question text; solution). Exact semantics
	 *    differ between types. */
	public $captchaData;

	/** @var string Captcha solution submitted by the user. */
	public $captchaWord;

	public function __construct( $id, $data ) {
		$this->captchaId = $id;
		$this->captchaData = $data;
	}

	public function loadFromSubmission( array $data ) {
		$success = parent::loadFromSubmission( $data );
		if ( $success ) {
			// captchaId and captchaWord was set from the submission but captchaData was not.
			$captcha = ConfirmEditHooks::getInstance();
			$this->captchaData = $captcha->retrieveCaptcha( $this->captchaId );
			if ( !$this->captchaData ) {
				return false;
			}
		}
		return $success;
	}

	public function getFieldInfo() {
		$captcha = ConfirmEditHooks::getInstance();

		$action = 'generic'; // doesn't actually exist but *Captcha::getMessage will handle that
		switch ( $this->action ) {
			case AuthManager::ACTION_LOGIN:
				$action = 'badlogin';
				break;
			case AuthManager::ACTION_CREATE:
				$action = 'createaccount';
				break;
		}

		$fields = [
			'captchaId' => [
				'type' => 'hidden',
				'value' => $this->captchaId,
				'label' => wfMessage( 'captcha-id-label' ),
				'help' => wfMessage( 'captcha-id-help' ),
			],
			'captchaInfo' => [
				'type' => 'null',
				'label' => $captcha->getMessage( $action ),
				'value' => $captcha->getCaptchaInfo( $this->captchaData, $this->captchaId ),
				'help' => wfMessage( 'captcha-info-help' ),
			],
			'captchaWord' => [
				'type' => 'string',
				'label' => wfMessage( 'captcha-label' ),
				'help' => wfMessage( 'captcha-help' ),
			],
		];

		return $fields;
	}

	public function getMetadata() {
		$captcha = ConfirmEditHooks::getInstance();
		return $captcha->describeCaptchaType();
	}

	public static function __set_state( $data ) {
		$ret = new static( null, null );
		foreach ( $data as $k => $v ) {
			$ret->$k = $v;
		}
		return $ret;
	}
}