Merge "Make sure Parsoid doesn't get snobbish and treat non-html5 tags badly."
[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 $params = $this->extractRequestParams();
33
34 $result = array();
35
36 // Init session if necessary
37 if ( session_id() == '' ) {
38 wfSetupSession();
39 }
40
41 if( $params['mailpassword'] && !$params['email'] ) {
42 $this->dieUsageMsg( 'noemail' );
43 }
44
45 $context = new DerivativeContext( $this->getContext() );
46 $context->setRequest( new DerivativeRequest(
47 $this->getContext()->getRequest(),
48 array(
49 'type' => 'signup',
50 'uselang' => $params['language'],
51 'wpName' => $params['name'],
52 'wpPassword' => $params['password'],
53 'wpRetype' => $params['password'],
54 'wpDomain' => $params['domain'],
55 'wpEmail' => $params['email'],
56 'wpRealName' => $params['realname'],
57 'wpCreateaccountToken' => $params['token'],
58 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
59 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
60 )
61 ) );
62
63 $loginForm = new LoginForm();
64 $loginForm->setContext( $context );
65 $loginForm->load();
66
67 $status = $loginForm->addNewaccountInternal();
68 $result = array();
69 if( $status->isGood() ) {
70 // Success!
71 $user = $status->getValue();
72
73 // If we showed up language selection links, and one was in use, be
74 // smart (and sensible) and save that language as the user's preference
75 global $wgLoginLanguageSelector, $wgEmailAuthentication;
76 if( $wgLoginLanguageSelector && $params['language'] ) {
77 $user->setOption( 'language', $params['language'] );
78 }
79
80 if( $params['mailpassword'] ) {
81 // If mailpassword was set, disable the password and send an email.
82 $user->setPassword( null );
83 $status->merge( $loginForm->mailPasswordInternal( $user, false, 'createaccount-title', 'createaccount-text' ) );
84 } elseif( $wgEmailAuthentication && Sanitizer::validateEmail( $user->getEmail() ) ) {
85 // Send out an email authentication message if needed
86 $status->merge( $user->sendConfirmationMail() );
87 }
88
89 // Save settings (including confirmation token)
90 $user->saveSettings();
91
92 wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
93
94 if ( $params['mailpassword'] ) {
95 $logAction = 'byemail';
96 } elseif ( $this->getUser()->isLoggedIn() ) {
97 $logAction = 'create2';
98 } else {
99 $logAction = 'create';
100 }
101 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
102
103 // Add username, id, and token to result.
104 $result['username'] = $user->getName();
105 $result['userid'] = $user->getId();
106 $result['token'] = $user->getToken();
107 }
108
109 $apiResult = $this->getResult();
110
111 if( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
112 // Token was incorrect, so add it to result, but don't throw an exception
113 // since not having the correct token is part of the normal
114 // flow of events.
115 $result['token'] = LoginForm::getCreateaccountToken();
116 $result['result'] = 'needtoken';
117 } elseif( !$status->isOK() ) {
118 // There was an error. Die now.
119 // Cannot use dieUsageMsg() directly because extensions
120 // might return custom error messages.
121 $errors = $status->getErrorsArray();
122 if( $errors[0] instanceof Message ) {
123 $code = 'aborted';
124 $desc = $errors[0];
125 } else {
126 $code = array_shift( $errors[0] );
127 $desc = wfMessage( $code, $errors[0] );
128 }
129 $this->dieUsage( $desc, $code );
130 } elseif( !$status->isGood() ) {
131 // Status is not good, but OK. This means warnings.
132 $result['result'] = 'warning';
133
134 // Add any warnings to the result
135 $warnings = $status->getErrorsByType( 'warning' );
136 if( $warnings ) {
137 foreach( $warnings as &$warning ) {
138 $apiResult->setIndexedTagName( $warning['params'], 'param' );
139 }
140 $apiResult->setIndexedTagName( $warnings, 'warning' );
141 $result['warnings'] = $warnings;
142 }
143 } else {
144 // Everything was fine.
145 $result['result'] = 'success';
146 }
147
148 $apiResult->addValue( null, 'createaccount', $result );
149 }
150
151 public function getDescription() {
152 return 'Create a new user account.';
153 }
154
155 public function mustBePosted() {
156 return true;
157 }
158
159 public function isReadMode() {
160 return false;
161 }
162
163 public function isWriteMode() {
164 return true;
165 }
166
167 public function getAllowedParams() {
168 global $wgEmailConfirmToEdit;
169 return array(
170 'name' => array(
171 ApiBase::PARAM_TYPE => 'user',
172 ApiBase::PARAM_REQUIRED => true
173 ),
174 'password' => null,
175 'domain' => null,
176 'token' => null,
177 'email' => array(
178 ApiBase::PARAM_TYPE => 'string',
179 ApiBase::PARAM_REQUIRED => $wgEmailConfirmToEdit
180 ),
181 'realname' => null,
182 'mailpassword' => array(
183 ApiBase::PARAM_TYPE => 'boolean',
184 ApiBase::PARAM_DFLT => false
185 ),
186 'reason' => null,
187 'language' => null
188 );
189 }
190
191 public function getParamDescription() {
192 $p = $this->getModulePrefix();
193 return array(
194 'name' => 'Username',
195 'password' => "Password (ignored if {$p}mailpassword is set)",
196 'domain' => 'Domain for external authentication (optional)',
197 'token' => 'Account creation token obtained in first request',
198 'email' => 'Email address of user (optional)',
199 'realname' => 'Real name of user (optional)',
200 'mailpassword' => 'If set to any value, a random password will be emailed to the user',
201 'reason' => 'Optional reason for creating the account to be put in the logs',
202 'language' => 'Language code to set as default for the user (optional, defaults to content language)'
203 );
204 }
205
206 public function getResultProperties() {
207 return array(
208 'createaccount' => array(
209 'result' => array(
210 ApiBase::PROP_TYPE => array(
211 'success',
212 'warning',
213 'needtoken'
214 )
215 ),
216 'username' => array(
217 ApiBase::PROP_TYPE => 'string',
218 ApiBase::PROP_NULLABLE => true
219 ),
220 'userid' => array(
221 ApiBase::PROP_TYPE => 'int',
222 ApiBase::PROP_NULLABLE => true
223 ),
224 'token' => array(
225 ApiBase::PROP_TYPE => 'string',
226 ApiBase::PROP_NULLABLE => true
227 ),
228 )
229 );
230 }
231
232 public function getPossibleErrors() {
233 $localErrors = array(
234 'wrongpassword',
235 'sorbs_create_account_reason',
236 'noname',
237 'userexists',
238 'password-name-match',
239 'password-login-forbidden',
240 'noemailtitle',
241 'invalidemailaddress',
242 'externaldberror'
243 );
244
245 $errors = parent::getPossibleErrors();
246 // All local errors are from LoginForm, which means they're actually message keys.
247 foreach( $localErrors as $error ) {
248 $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
249 }
250
251 // 'passwordtooshort' has parameters. :(
252 global $wgMinimalPasswordLength;
253 $errors[] = array(
254 'code' => 'passwordtooshort',
255 'info' => wfMessage( 'passwordtooshort', $wgMinimalPasswordLength )->parse()
256 );
257 return $errors;
258 }
259
260 public function getExamples() {
261 return array(
262 'api.php?action=createaccount&name=testuser&password=test123',
263 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
264 );
265 }
266
267 public function getHelpUrls() {
268 return 'https://www.mediawiki.org/wiki/API:Account_creation';
269 }
270 }