(bug 35885) remove api version string and parameter
[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, false ) );
93 $user->addNewUserLogEntry( $this->getUser()->isAnon(), $params['reason'] );
94
95 // Add username, id, and token to result.
96 $result['username'] = $user->getName();
97 $result['userid'] = $user->getId();
98 $result['token'] = $user->getToken();
99 }
100
101 $apiResult = $this->getResult();
102
103 if( $status->hasMessage( 'sessionfailure' ) ) {
104 // Token was incorrect, so add it to result, but don't throw an exception.
105 $result['token'] = LoginForm::getCreateaccountToken();
106 $result['result'] = 'needtoken';
107 } elseif( !$status->isOK() ) {
108 // There was an error. Die now.
109 // Cannot use dieUsageMsg() directly because extensions
110 // might return custom error messages.
111 $errors = $status->getErrorsArray();
112 if( $errors[0] instanceof Message ) {
113 $code = 'aborted';
114 $desc = $errors[0];
115 } else {
116 $code = array_shift( $errors[0] );
117 $desc = wfMessage( $code, $errors[0] );
118 }
119 $this->dieUsage( $desc, $code );
120 } elseif( !$status->isGood() ) {
121 // Status is not good, but OK. This means warnings.
122 $result['result'] = 'warning';
123
124 // Add any warnings to the result
125 $warnings = $status->getErrorsByType( 'warning' );
126 if( $warnings ) {
127 foreach( $warnings as &$warning ) {
128 $apiResult->setIndexedTagName( $warning['params'], 'param' );
129 }
130 $apiResult->setIndexedTagName( $warnings, 'warning' );
131 $result['warnings'] = $warnings;
132 }
133 } else {
134 // Everything was fine.
135 $result['result'] = 'success';
136 }
137
138 $apiResult->addValue( null, 'createaccount', $result );
139 }
140
141 public function getDescription() {
142 return 'Create a new user account.';
143 }
144
145 public function mustBePosted() {
146 return true;
147 }
148
149 public function isReadMode() {
150 return false;
151 }
152
153 public function isWriteMode() {
154 return true;
155 }
156
157 public function getAllowedParams() {
158 global $wgEmailConfirmToEdit;
159 return array(
160 'name' => array(
161 ApiBase::PARAM_TYPE => 'user',
162 ApiBase::PARAM_REQUIRED => true
163 ),
164 'password' => null,
165 'domain' => null,
166 'token' => null,
167 'email' => array(
168 ApiBase::PARAM_TYPE => 'string',
169 ApiBase::PARAM_REQUIRED => $wgEmailConfirmToEdit
170 ),
171 'realname' => null,
172 'mailpassword' => array(
173 ApiBase::PARAM_TYPE => 'boolean',
174 ApiBase::PARAM_DFLT => false
175 ),
176 'reason' => null,
177 'language' => null
178 );
179 }
180
181 public function getParamDescription() {
182 $p = $this->getModulePrefix();
183 return array(
184 'name' => 'User Name',
185 'password' => "Password (ignored if {$p}mailpassword is set)",
186 'domain' => 'Domain (optional)',
187 'token' => 'Account creation token obtained in first request',
188 'email' => 'Email address of user',
189 'realname' => 'Real Name of user',
190 'mailpassword' => 'Whether to generate and mail a random password to the user',
191 'reason' => "Optional reason for creating the account (used when {$p}mailpassword is set)",
192 'language' => 'Language code to set for the user.'
193 );
194 }
195
196 public function getResultProperties() {
197 return array(
198 'createaccount' => array(
199 'result' => array(
200 ApiBase::PROP_TYPE => array(
201 'success',
202 'warning',
203 'needtoken'
204 )
205 ),
206 'username' => array(
207 ApiBase::PROP_TYPE => 'string',
208 ApiBase::PROP_NULLABLE => true
209 ),
210 'userid' => array(
211 ApiBase::PROP_TYPE => 'int',
212 ApiBase::PROP_NULLABLE => true
213 ),
214 'token' => array(
215 ApiBase::PROP_TYPE => 'string',
216 ApiBase::PROP_NULLABLE => true
217 ),
218 )
219 );
220 }
221
222 public function getPossibleErrors() {
223 $localErrors = array(
224 'wrongpassword',
225 'sessionfailure',
226 'sorbs_create_account_reason',
227 'noname',
228 'userexists',
229 'password-name-match',
230 'password-login-forbidden',
231 'noemailtitle',
232 'invalidemailaddress',
233 'externaldberror'
234 );
235
236 $errors = parent::getPossibleErrors();
237 // All local errors are from LoginForm, which means they're actually message keys.
238 foreach( $localErrors as $error ) {
239 $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
240 }
241
242 // 'passwordtooshort' has parameters. :(
243 global $wgMinimalPasswordLength;
244 $errors[] = array(
245 'code' => 'passwordtooshort',
246 'info' => wfMessage( 'passwordtooshort', $wgMinimalPasswordLength )->parse()
247 );
248 return $errors;
249 }
250
251 public function getExamples() {
252 return array(
253 'api.php?action=createaccount&name=testuser&password=test123',
254 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
255 );
256 }
257
258 public function getHelpUrls() {
259 return 'https://www.mediawiki.org/wiki/API:Account creation';
260 }
261 }