Added a separate error message for mkdir failures
[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 // Make sure session is persisted
63 MediaWiki\Session\SessionManager::getGlobalSession()->persist();
64
65 if ( $params['mailpassword'] && !$params['email'] ) {
66 $this->dieUsageMsg( 'noemail' );
67 }
68
69 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
70 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
71 }
72
73 $context = new DerivativeContext( $this->getContext() );
74 $context->setRequest( new DerivativeRequest(
75 $this->getContext()->getRequest(),
76 array(
77 'type' => 'signup',
78 'uselang' => $params['language'],
79 'wpName' => $params['name'],
80 'wpPassword' => $params['password'],
81 'wpRetype' => $params['password'],
82 'wpDomain' => $params['domain'],
83 'wpEmail' => $params['email'],
84 'wpRealName' => $params['realname'],
85 'wpCreateaccountToken' => $params['token'],
86 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
87 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
88 )
89 ) );
90
91 $loginForm = new LoginForm();
92 $loginForm->setContext( $context );
93 Hooks::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
94 $loginForm->load();
95
96 $status = $loginForm->addNewAccountInternal();
97 LoggerFactory::getInstance( 'authmanager' )->info( 'Account creation attempt via API', array(
98 'event' => 'accountcreation',
99 'status' => $status,
100 ) );
101 $result = array();
102 if ( $status->isGood() ) {
103 // Success!
104 $user = $status->getValue();
105
106 if ( $params['language'] ) {
107 $user->setOption( 'language', $params['language'] );
108 }
109
110 if ( $params['mailpassword'] ) {
111 // If mailpassword was set, disable the password and send an email.
112 $user->setPassword( null );
113 $status->merge( $loginForm->mailPasswordInternal(
114 $user,
115 false,
116 'createaccount-title',
117 'createaccount-text'
118 ) );
119 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) &&
120 Sanitizer::validateEmail( $user->getEmail() )
121 ) {
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()->toString();
153 $result['result'] = 'NeedToken';
154 $this->setWarning( 'Fetching a token via action=createaccount is deprecated. ' .
155 'Use action=query&meta=tokens&type=createaccount instead.' );
156 $this->logFeatureUsage( 'action=createaccount&!token' );
157 } elseif ( !$status->isOK() ) {
158 // There was an error. Die now.
159 $this->dieStatus( $status );
160 } elseif ( !$status->isGood() ) {
161 // Status is not good, but OK. This means warnings.
162 $result['result'] = 'Warning';
163
164 // Add any warnings to the result
165 $warnings = $status->getErrorsByType( 'warning' );
166 if ( $warnings ) {
167 foreach ( $warnings as &$warning ) {
168 ApiResult::setIndexedTagName( $warning['params'], 'param' );
169 }
170 ApiResult::setIndexedTagName( $warnings, 'warning' );
171 $result['warnings'] = $warnings;
172 }
173 } else {
174 // Everything was fine.
175 $result['result'] = 'Success';
176 }
177
178 // Give extensions a chance to modify the API result data
179 Hooks::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
180
181 $apiResult->addValue( null, 'createaccount', $result );
182 }
183
184 public function mustBePosted() {
185 return true;
186 }
187
188 public function isReadMode() {
189 return false;
190 }
191
192 public function isWriteMode() {
193 return true;
194 }
195
196 public function getAllowedParams() {
197 return array(
198 'name' => array(
199 ApiBase::PARAM_TYPE => 'user',
200 ApiBase::PARAM_REQUIRED => true
201 ),
202 'password' => array(
203 ApiBase::PARAM_TYPE => 'password',
204 ),
205 'domain' => null,
206 'token' => array(
207 ApiBase::PARAM_TYPE => 'string',
208 ApiBase::PARAM_REQUIRED => false, // for BC
209 ApiBase::PARAM_HELP_MSG => array( 'api-help-param-token', 'createaccount' ),
210 ),
211 'email' => array(
212 ApiBase::PARAM_TYPE => 'string',
213 ApiBase::PARAM_REQUIRED => $this->getConfig()->get( 'EmailConfirmToEdit' ),
214 ),
215 'realname' => null,
216 'mailpassword' => array(
217 ApiBase::PARAM_TYPE => 'boolean',
218 ApiBase::PARAM_DFLT => false
219 ),
220 'reason' => null,
221 'language' => null
222 );
223 }
224
225 protected function getExamplesMessages() {
226 return array(
227 'action=createaccount&name=testuser&password=test123'
228 => 'apihelp-createaccount-example-pass',
229 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
230 => 'apihelp-createaccount-example-mail',
231 );
232 }
233
234 public function getHelpUrls() {
235 return 'https://www.mediawiki.org/wiki/API:Account_creation';
236 }
237 }