Merge "Handle no pageids response field in MessagePoster factory"
[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 // If we're in a mode that breaks the same-origin policy, no tokens can
33 // be obtained
34 if ( $this->lacksSameOriginSecurity() ) {
35 $this->dieUsage(
36 'Cannot create account when the same-origin policy is not applied', 'aborted'
37 );
38 }
39
40 // $loginForm->addNewaccountInternal will throw exceptions
41 // if wiki is read only (already handled by api), user is blocked or does not have rights.
42 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
43 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
44 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
45 $this->dieUsage(
46 'You do not have the right to create a new account',
47 'permdenied-createaccount'
48 );
49 }
50 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
51 $this->dieUsage(
52 'You cannot create a new account because you are blocked',
53 'blocked',
54 0,
55 array( 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $this->getUser()->getBlock() ) )
56 );
57 }
58
59 $params = $this->extractRequestParams();
60
61 // Init session if necessary
62 if ( session_id() == '' ) {
63 wfSetupSession();
64 }
65
66 if ( $params['mailpassword'] && !$params['email'] ) {
67 $this->dieUsageMsg( 'noemail' );
68 }
69
70 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
71 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
72 }
73
74 $context = new DerivativeContext( $this->getContext() );
75 $context->setRequest( new DerivativeRequest(
76 $this->getContext()->getRequest(),
77 array(
78 'type' => 'signup',
79 'uselang' => $params['language'],
80 'wpName' => $params['name'],
81 'wpPassword' => $params['password'],
82 'wpRetype' => $params['password'],
83 'wpDomain' => $params['domain'],
84 'wpEmail' => $params['email'],
85 'wpRealName' => $params['realname'],
86 'wpCreateaccountToken' => $params['token'],
87 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
88 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
89 )
90 ) );
91
92 $loginForm = new LoginForm();
93 $loginForm->setContext( $context );
94 Hooks::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
95 $loginForm->load();
96
97 $status = $loginForm->addNewaccountInternal();
98 $result = array();
99 if ( $status->isGood() ) {
100 // Success!
101 $user = $status->getValue();
102
103 if ( $params['language'] ) {
104 $user->setOption( 'language', $params['language'] );
105 }
106
107 if ( $params['mailpassword'] ) {
108 // If mailpassword was set, disable the password and send an email.
109 $user->setPassword( null );
110 $status->merge( $loginForm->mailPasswordInternal(
111 $user,
112 false,
113 'createaccount-title',
114 'createaccount-text'
115 ) );
116 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer::validateEmail( $user->getEmail() ) ) {
117 // Send out an email authentication message if needed
118 $status->merge( $user->sendConfirmationMail() );
119 }
120
121 // Save settings (including confirmation token)
122 $user->saveSettings();
123
124 Hooks::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
125
126 if ( $params['mailpassword'] ) {
127 $logAction = 'byemail';
128 } elseif ( $this->getUser()->isLoggedIn() ) {
129 $logAction = 'create2';
130 } else {
131 $logAction = 'create';
132 }
133 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
134
135 // Add username, id, and token to result.
136 $result['username'] = $user->getName();
137 $result['userid'] = $user->getId();
138 $result['token'] = $user->getToken();
139 }
140
141 $apiResult = $this->getResult();
142
143 if ( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
144 // Token was incorrect, so add it to result, but don't throw an exception
145 // since not having the correct token is part of the normal
146 // flow of events.
147 $result['token'] = LoginForm::getCreateaccountToken();
148 $result['result'] = 'NeedToken';
149 } elseif ( !$status->isOK() ) {
150 // There was an error. Die now.
151 $this->dieStatus( $status );
152 } elseif ( !$status->isGood() ) {
153 // Status is not good, but OK. This means warnings.
154 $result['result'] = 'Warning';
155
156 // Add any warnings to the result
157 $warnings = $status->getErrorsByType( 'warning' );
158 if ( $warnings ) {
159 foreach ( $warnings as &$warning ) {
160 ApiResult::setIndexedTagName( $warning['params'], 'param' );
161 }
162 ApiResult::setIndexedTagName( $warnings, 'warning' );
163 $result['warnings'] = $warnings;
164 }
165 } else {
166 // Everything was fine.
167 $result['result'] = 'Success';
168 }
169
170 // Give extensions a chance to modify the API result data
171 Hooks::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
172
173 $apiResult->addValue( null, 'createaccount', $result );
174 }
175
176 public function mustBePosted() {
177 return true;
178 }
179
180 public function isReadMode() {
181 return false;
182 }
183
184 public function isWriteMode() {
185 return true;
186 }
187
188 public function getAllowedParams() {
189 return array(
190 'name' => array(
191 ApiBase::PARAM_TYPE => 'user',
192 ApiBase::PARAM_REQUIRED => true
193 ),
194 'password' => array(
195 ApiBase::PARAM_TYPE => 'password',
196 ),
197 'domain' => null,
198 'token' => null,
199 'email' => array(
200 ApiBase::PARAM_TYPE => 'string',
201 ApiBase::PARAM_REQUIRED => $this->getConfig()->get( 'EmailConfirmToEdit' ),
202 ),
203 'realname' => null,
204 'mailpassword' => array(
205 ApiBase::PARAM_TYPE => 'boolean',
206 ApiBase::PARAM_DFLT => false
207 ),
208 'reason' => null,
209 'language' => null
210 );
211 }
212
213 protected function getExamplesMessages() {
214 return array(
215 'action=createaccount&name=testuser&password=test123'
216 => 'apihelp-createaccount-example-pass',
217 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
218 => 'apihelp-createaccount-example-mail',
219 );
220 }
221
222 public function getHelpUrls() {
223 return 'https://www.mediawiki.org/wiki/API:Account_creation';
224 }
225 }