Debian Codegraph @

extensions/ConfirmEdit/includes/Auth/CaptchaAuthenticationRequest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\ConfirmEdit\Auth;
4
5 use MediaWiki\Auth\AuthenticationRequest;
6 use MediaWiki\Auth\AuthManager;
7 use MediaWiki\Extension\ConfirmEdit\Hooks;
8
9 /**
10 * Generic captcha authentication request class. A captcha consist some data stored in the session
11 * (e.g. a question and its answer), an ID that references the data, and a solution.
12 */
13 class CaptchaAuthenticationRequest extends AuthenticationRequest {
14 /** @var string Identifier of the captcha. Used internally to remember which captcha was used. */
15 public $captchaId;
16
17 /** @var array Information about the captcha (e.g. question text; solution). Exact semantics
18 * differ between types.
19 */
20 public $captchaData;
21
22 /** @var string Captcha solution submitted by the user. */
23 public $captchaWord;
24
25 /**
26 * @param string $id
27 * @param array $data
28 */
29 public function __construct( $id, $data ) {
30 $this->captchaId = $id;
31 $this->captchaData = $data;
32 }
33
34 /** @inheritDoc */
35 public function getUniqueId() {
36 return 'CaptchaAuthenticationRequest';
37 }
38
39 /**
40 * @inheritDoc
41 */
42 public function loadFromSubmission( array $data ) {
43 $success = parent::loadFromSubmission( $data );
44 if ( $success ) {
45 // captchaId and captchaWord was set from the submission but captchaData was not.
46 $captcha = Hooks::getInstance();
47 $this->captchaData = $captcha->retrieveCaptcha( $this->captchaId );
48 if ( !$this->captchaData ) {
49 return false;
50 }
51 }
52 return $success;
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function getFieldInfo() {
59 $captcha = Hooks::getInstance();
60
61 // doesn't actually exist but *Captcha::getMessage will handle that
62 $action = 'generic';
63 switch ( $this->action ) {
64 case AuthManager::ACTION_LOGIN:
65 $action = 'badlogin';
66 break;
67 case AuthManager::ACTION_CREATE:
68 $action = 'createaccount';
69 break;
70 }
71
72 $fields = [
73 'captchaId' => [
74 'type' => 'hidden',
75 'value' => $this->captchaId,
76 'label' => wfMessage( 'captcha-id-label' ),
77 'help' => wfMessage( 'captcha-id-help' ),
78 ],
79 'captchaInfo' => [
80 'type' => 'null',
81 'label' => $captcha->getMessage( $action ),
82 'value' => $captcha->getCaptchaInfo( $this->captchaData, $this->captchaId ),
83 'help' => wfMessage( 'captcha-info-help' ),
84 ],
85 'captchaWord' => [
86 'type' => 'string',
87 'label' => wfMessage( 'captcha-label' ),
88 'help' => wfMessage( 'captcha-help' ),
89 ],
90 ];
91
92 return $fields;
93 }
94
95 /**
96 * @inheritDoc
97 */
98 public function getMetadata() {
99 return ( Hooks::getInstance() )->describeCaptchaType();
100 }
101
102 /**
103 * @param array $data
104 * @return CaptchaAuthenticationRequest
105 */
106 public static function __set_state( $data ) {
107 $ret = new static( '', [] );
108 foreach ( $data as $k => $v ) {
109 $ret->$k = $v;
110 }
111 return $ret;
112 }
113 }