Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / api / ApiCreateAccount.php
1 <?php
2 /**
3 * Created on August 7, 2012
4 *
5 * Copyright © 2012 Tyler Romeo <tylerromeo@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24 use MediaWiki\Logger\LoggerFactory;
25
26 /**
27 * Unit to authenticate account registration attempts to the current wiki.
28 *
29 * @ingroup API
30 */
31 class ApiCreateAccount extends ApiBase {
32 public function execute() {
33 // If we're in a mode that breaks the same-origin policy, no tokens can
34 // be obtained
35 if ( $this->lacksSameOriginSecurity() ) {
36 $this->dieUsage(
37 'Cannot create account when the same-origin policy is not applied', 'aborted'
38 );
39 }
40
41 // $loginForm->addNewaccountInternal will throw exceptions
42 // if wiki is read only (already handled by api), user is blocked or does not have rights.
43 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
44 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
45 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
46 $this->dieUsage(
47 'You do not have the right to create a new account',
48 'permdenied-createaccount'
49 );
50 }
51 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
52 $this->dieUsage(
53 'You cannot create a new account because you are blocked',
54 'blocked',
55 0,
56 array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
57 );
58 }
59
60 $params = $this->extractRequestParams();
61
62 // Init session if necessary
63 if ( session_id() == '' ) {
64 wfSetupSession();
65 }
66
67 if ( $params['mailpassword'] && !$params['email'] ) {
68 $this->dieUsageMsg( 'noemail' );
69 }
70
71 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
72 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
73 }
74
75 $context = new DerivativeContext( $this->getContext() );
76 $context->setRequest( new DerivativeRequest(
77 $this->getContext()->getRequest(),
78 array(
79 'type' => 'signup',
80 'uselang' => $params['language'],
81 'wpName' => $params['name'],
82 'wpPassword' => $params['password'],
83 'wpRetype' => $params['password'],
84 'wpDomain' => $params['domain'],
85 'wpEmail' => $params['email'],
86 'wpRealName' => $params['realname'],
87 'wpCreateaccountToken' => $params['token'],
88 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
89 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
90 )
91 ) );
92
93 $loginForm = new LoginForm();
94 $loginForm->setContext( $context );
95 Hooks::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
96 $loginForm->load();
97
98 $status = $loginForm->addNewaccountInternal();
99 LoggerFactory::getInstance( 'authmanager' )->info( 'Account creation attempt via API', array(
100 'event' => 'accountcreation',
101 'status' => $status,
102 ) );
103 $result = array();
104 if ( $status->isGood() ) {
105 // Success!
106 $user = $status->getValue();
107
108 if ( $params['language'] ) {
109 $user->setOption( 'language', $params['language'] );
110 }
111
112 if ( $params['mailpassword'] ) {
113 // If mailpassword was set, disable the password and send an email.
114 $user->setPassword( null );
115 $status->merge( $loginForm->mailPasswordInternal(
116 $user,
117 false,
118 'createaccount-title',
119 'createaccount-text'
120 ) );
121 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer::validateEmail( $user->getEmail() ) ) {
122 // Send out an email authentication message if needed
123 $status->merge( $user->sendConfirmationMail() );
124 }
125
126 // Save settings (including confirmation token)
127 $user->saveSettings();
128
129 Hooks::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
130
131 if ( $params['mailpassword'] ) {
132 $logAction = 'byemail';
133 } elseif ( $this->getUser()->isLoggedIn() ) {
134 $logAction = 'create2';
135 } else {
136 $logAction = 'create';
137 }
138 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
139
140 // Add username, id, and token to result.
141 $result['username'] = $user->getName();
142 $result['userid'] = $user->getId();
143 $result['token'] = $user->getToken();
144 }
145
146 $apiResult = $this->getResult();
147
148 if ( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
149 // Token was incorrect, so add it to result, but don't throw an exception
150 // since not having the correct token is part of the normal
151 // flow of events.
152 $result['token'] = LoginForm::getCreateaccountToken();
153 $result['result'] = 'NeedToken';
154 } elseif ( !$status->isOK() ) {
155 // There was an error. Die now.
156 $this->dieStatus( $status );
157 } elseif ( !$status->isGood() ) {
158 // Status is not good, but OK. This means warnings.
159 $result['result'] = 'Warning';
160
161 // Add any warnings to the result
162 $warnings = $status->getErrorsByType( 'warning' );
163 if ( $warnings ) {
164 foreach ( $warnings as &$warning ) {
165 ApiResult::setIndexedTagName( $warning['params'], 'param' );
166 }
167 ApiResult::setIndexedTagName( $warnings, 'warning' );
168 $result['warnings'] = $warnings;
169 }
170 } else {
171 // Everything was fine.
172 $result['result'] = 'Success';
173 }
174
175 // Give extensions a chance to modify the API result data
176 Hooks::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
177
178 $apiResult->addValue( null, 'createaccount', $result );
179 }
180
181 public function mustBePosted() {
182 return true;
183 }
184
185 public function isReadMode() {
186 return false;
187 }
188
189 public function isWriteMode() {
190 return true;
191 }
192
193 public function getAllowedParams() {
194 return array(
195 'name' => array(
196 ApiBase::PARAM_TYPE => 'user',
197 ApiBase::PARAM_REQUIRED => true
198 ),
199 'password' => array(
200 ApiBase::PARAM_TYPE => 'password',
201 ),
202 'domain' => null,
203 'token' => null,
204 'email' => array(
205 ApiBase::PARAM_TYPE => 'string',
206 ApiBase::PARAM_REQUIRED => $this->getConfig()->get( 'EmailConfirmToEdit' ),
207 ),
208 'realname' => null,
209 'mailpassword' => array(
210 ApiBase::PARAM_TYPE => 'boolean',
211 ApiBase::PARAM_DFLT => false
212 ),
213 'reason' => null,
214 'language' => null
215 );
216 }
217
218 protected function getExamplesMessages() {
219 return array(
220 'action=createaccount&name=testuser&password=test123'
221 => 'apihelp-createaccount-example-pass',
222 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
223 => 'apihelp-createaccount-example-mail',
224 );
225 }
226
227 public function getHelpUrls() {
228 return 'https://www.mediawiki.org/wiki/API:Account_creation';
229 }
230 }