Added spaces before and removed spaces after 'array'
[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
25 /**
26 * Unit to authenticate account registration attempts to the current wiki.
27 *
28 * @ingroup API
29 */
30 class ApiCreateAccount extends ApiBase {
31 public function execute() {
32
33 // $loginForm->addNewaccountInternal will throw exceptions
34 // if wiki is read only (already handled by api), user is blocked or does not have rights.
35 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
36 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
37 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
38 $this->dieUsage( 'You do not have the right to create a new account', 'permdenied-createaccount' );
39 }
40 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
41 $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
42 }
43
44 $params = $this->extractRequestParams();
45
46 // Init session if necessary
47 if ( session_id() == '' ) {
48 wfSetupSession();
49 }
50
51 if( $params['mailpassword'] && !$params['email'] ) {
52 $this->dieUsageMsg( 'noemail' );
53 }
54
55 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
56 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
57 }
58
59 $context = new DerivativeContext( $this->getContext() );
60 $context->setRequest( new DerivativeRequest(
61 $this->getContext()->getRequest(),
62 array(
63 'type' => 'signup',
64 'uselang' => $params['language'],
65 'wpName' => $params['name'],
66 'wpPassword' => $params['password'],
67 'wpRetype' => $params['password'],
68 'wpDomain' => $params['domain'],
69 'wpEmail' => $params['email'],
70 'wpRealName' => $params['realname'],
71 'wpCreateaccountToken' => $params['token'],
72 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
73 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
74 )
75 ) );
76
77 $loginForm = new LoginForm();
78 $loginForm->setContext( $context );
79 $loginForm->load();
80
81 $status = $loginForm->addNewaccountInternal();
82 $result = array();
83 if( $status->isGood() ) {
84 // Success!
85 global $wgEmailAuthentication;
86 $user = $status->getValue();
87
88 if( $params['language'] ) {
89 $user->setOption( 'language', $params['language'] );
90 }
91
92 if( $params['mailpassword'] ) {
93 // If mailpassword was set, disable the password and send an email.
94 $user->setPassword( null );
95 $status->merge( $loginForm->mailPasswordInternal( $user, false, 'createaccount-title', 'createaccount-text' ) );
96 } elseif( $wgEmailAuthentication && Sanitizer::validateEmail( $user->getEmail() ) ) {
97 // Send out an email authentication message if needed
98 $status->merge( $user->sendConfirmationMail() );
99 }
100
101 // Save settings (including confirmation token)
102 $user->saveSettings();
103
104 wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
105
106 if ( $params['mailpassword'] ) {
107 $logAction = 'byemail';
108 } elseif ( $this->getUser()->isLoggedIn() ) {
109 $logAction = 'create2';
110 } else {
111 $logAction = 'create';
112 }
113 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
114
115 // Add username, id, and token to result.
116 $result['username'] = $user->getName();
117 $result['userid'] = $user->getId();
118 $result['token'] = $user->getToken();
119 }
120
121 $apiResult = $this->getResult();
122
123 if( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
124 // Token was incorrect, so add it to result, but don't throw an exception
125 // since not having the correct token is part of the normal
126 // flow of events.
127 $result['token'] = LoginForm::getCreateaccountToken();
128 $result['result'] = 'needtoken';
129 } elseif( !$status->isOK() ) {
130 // There was an error. Die now.
131 // Cannot use dieUsageMsg() directly because extensions
132 // might return custom error messages.
133 $errors = $status->getErrorsArray();
134 if( $errors[0] instanceof Message ) {
135 $code = 'aborted';
136 $desc = $errors[0];
137 } else {
138 $code = array_shift( $errors[0] );
139 $desc = wfMessage( $code, $errors[0] );
140 }
141 $this->dieUsage( $desc, $code );
142 } elseif( !$status->isGood() ) {
143 // Status is not good, but OK. This means warnings.
144 $result['result'] = 'warning';
145
146 // Add any warnings to the result
147 $warnings = $status->getErrorsByType( 'warning' );
148 if( $warnings ) {
149 foreach( $warnings as &$warning ) {
150 $apiResult->setIndexedTagName( $warning['params'], 'param' );
151 }
152 $apiResult->setIndexedTagName( $warnings, 'warning' );
153 $result['warnings'] = $warnings;
154 }
155 } else {
156 // Everything was fine.
157 $result['result'] = 'success';
158 }
159
160 $apiResult->addValue( null, 'createaccount', $result );
161 }
162
163 public function getDescription() {
164 return 'Create a new user account.';
165 }
166
167 public function mustBePosted() {
168 return true;
169 }
170
171 public function isReadMode() {
172 return false;
173 }
174
175 public function isWriteMode() {
176 return true;
177 }
178
179 public function getAllowedParams() {
180 global $wgEmailConfirmToEdit;
181 return array(
182 'name' => array(
183 ApiBase::PARAM_TYPE => 'user',
184 ApiBase::PARAM_REQUIRED => true
185 ),
186 'password' => null,
187 'domain' => null,
188 'token' => null,
189 'email' => array(
190 ApiBase::PARAM_TYPE => 'string',
191 ApiBase::PARAM_REQUIRED => $wgEmailConfirmToEdit
192 ),
193 'realname' => null,
194 'mailpassword' => array(
195 ApiBase::PARAM_TYPE => 'boolean',
196 ApiBase::PARAM_DFLT => false
197 ),
198 'reason' => null,
199 'language' => null
200 );
201 }
202
203 public function getParamDescription() {
204 $p = $this->getModulePrefix();
205 return array(
206 'name' => 'Username',
207 'password' => "Password (ignored if {$p}mailpassword is set)",
208 'domain' => 'Domain for external authentication (optional)',
209 'token' => 'Account creation token obtained in first request',
210 'email' => 'Email address of user (optional)',
211 'realname' => 'Real name of user (optional)',
212 'mailpassword' => 'If set to any value, a random password will be emailed to the user',
213 'reason' => 'Optional reason for creating the account to be put in the logs',
214 'language' => 'Language code to set as default for the user (optional, defaults to content language)'
215 );
216 }
217
218 public function getResultProperties() {
219 return array(
220 'createaccount' => array(
221 'result' => array(
222 ApiBase::PROP_TYPE => array(
223 'success',
224 'warning',
225 'needtoken'
226 )
227 ),
228 'username' => array(
229 ApiBase::PROP_TYPE => 'string',
230 ApiBase::PROP_NULLABLE => true
231 ),
232 'userid' => array(
233 ApiBase::PROP_TYPE => 'int',
234 ApiBase::PROP_NULLABLE => true
235 ),
236 'token' => array(
237 ApiBase::PROP_TYPE => 'string',
238 ApiBase::PROP_NULLABLE => true
239 ),
240 )
241 );
242 }
243
244 public function getPossibleErrors() {
245 // Note the following errors aren't possible and don't need to be listed:
246 // sessionfailure, nocookiesfornew, badretype
247 $localErrors = array(
248 'wrongpassword', // Actually caused by wrong domain field. Riddle me that...
249 'sorbs_create_account_reason',
250 'noname',
251 'userexists',
252 'password-name-match', // from User::getPasswordValidity
253 'password-login-forbidden', // from User::getPasswordValidity
254 'noemailtitle',
255 'invalidemailaddress',
256 'externaldberror',
257 'acct_creation_throttle_hit',
258 );
259
260 $errors = parent::getPossibleErrors();
261 // All local errors are from LoginForm, which means they're actually message keys.
262 foreach( $localErrors as $error ) {
263 $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
264 }
265
266 $errors[] = array(
267 'code' => 'permdenied-createaccount',
268 'info' => 'You do not have the right to create a new account'
269 );
270 $errors[] = array(
271 'code' => 'blocked',
272 'info' => 'You cannot create a new account because you are blocked'
273 );
274 $errors[] = array(
275 'code' => 'aborted',
276 'info' => 'Account creation aborted by hook (info may vary)'
277 );
278 $errors[] = array(
279 'code' => 'langinvalid',
280 'info' => 'Invalid language parameter'
281 );
282
283 // 'passwordtooshort' has parameters. :(
284 global $wgMinimalPasswordLength;
285 $errors[] = array(
286 'code' => 'passwordtooshort',
287 'info' => wfMessage( 'passwordtooshort', $wgMinimalPasswordLength )->parse()
288 );
289 return $errors;
290 }
291
292 public function getExamples() {
293 return array(
294 'api.php?action=createaccount&name=testuser&password=test123',
295 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
296 );
297 }
298
299 public function getHelpUrls() {
300 return 'https://www.mediawiki.org/wiki/API:Account_creation';
301 }
302 }