MediaWiki  REL1_24
ApiCreateAccount.php
Go to the documentation of this file.
00001 <?php
00030 class ApiCreateAccount extends ApiBase {
00031     public function execute() {
00032         // If we're in JSON callback mode, no tokens can be obtained
00033         if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00034             $this->dieUsage( 'Cannot create account when using a callback', 'aborted' );
00035         }
00036 
00037         // $loginForm->addNewaccountInternal will throw exceptions
00038         // if wiki is read only (already handled by api), user is blocked or does not have rights.
00039         // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
00040         $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
00041         if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
00042             $this->dieUsage(
00043                 'You do not have the right to create a new account',
00044                 'permdenied-createaccount'
00045             );
00046         }
00047         if ( $this->getUser()->isBlockedFromCreateAccount() ) {
00048             $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
00049         }
00050 
00051         $params = $this->extractRequestParams();
00052 
00053         // Init session if necessary
00054         if ( session_id() == '' ) {
00055             wfSetupSession();
00056         }
00057 
00058         if ( $params['mailpassword'] && !$params['email'] ) {
00059             $this->dieUsageMsg( 'noemail' );
00060         }
00061 
00062         if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
00063             $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
00064         }
00065 
00066         $context = new DerivativeContext( $this->getContext() );
00067         $context->setRequest( new DerivativeRequest(
00068             $this->getContext()->getRequest(),
00069             array(
00070                 'type' => 'signup',
00071                 'uselang' => $params['language'],
00072                 'wpName' => $params['name'],
00073                 'wpPassword' => $params['password'],
00074                 'wpRetype' => $params['password'],
00075                 'wpDomain' => $params['domain'],
00076                 'wpEmail' => $params['email'],
00077                 'wpRealName' => $params['realname'],
00078                 'wpCreateaccountToken' => $params['token'],
00079                 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
00080                 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
00081             )
00082         ) );
00083 
00084         $loginForm = new LoginForm();
00085         $loginForm->setContext( $context );
00086         wfRunHooks( 'AddNewAccountApiForm', array( $this, $loginForm ) );
00087         $loginForm->load();
00088 
00089         $status = $loginForm->addNewaccountInternal();
00090         $result = array();
00091         if ( $status->isGood() ) {
00092             // Success!
00093             $user = $status->getValue();
00094 
00095             if ( $params['language'] ) {
00096                 $user->setOption( 'language', $params['language'] );
00097             }
00098 
00099             if ( $params['mailpassword'] ) {
00100                 // If mailpassword was set, disable the password and send an email.
00101                 $user->setPassword( null );
00102                 $status->merge( $loginForm->mailPasswordInternal(
00103                     $user,
00104                     false,
00105                     'createaccount-title',
00106                     'createaccount-text'
00107                 ) );
00108             } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer::validateEmail( $user->getEmail() ) ) {
00109                 // Send out an email authentication message if needed
00110                 $status->merge( $user->sendConfirmationMail() );
00111             }
00112 
00113             // Save settings (including confirmation token)
00114             $user->saveSettings();
00115 
00116             wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
00117 
00118             if ( $params['mailpassword'] ) {
00119                 $logAction = 'byemail';
00120             } elseif ( $this->getUser()->isLoggedIn() ) {
00121                 $logAction = 'create2';
00122             } else {
00123                 $logAction = 'create';
00124             }
00125             $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
00126 
00127             // Add username, id, and token to result.
00128             $result['username'] = $user->getName();
00129             $result['userid'] = $user->getId();
00130             $result['token'] = $user->getToken();
00131         }
00132 
00133         $apiResult = $this->getResult();
00134 
00135         if ( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
00136             // Token was incorrect, so add it to result, but don't throw an exception
00137             // since not having the correct token is part of the normal
00138             // flow of events.
00139             $result['token'] = LoginForm::getCreateaccountToken();
00140             $result['result'] = 'NeedToken';
00141         } elseif ( !$status->isOK() ) {
00142             // There was an error. Die now.
00143             $this->dieStatus( $status );
00144         } elseif ( !$status->isGood() ) {
00145             // Status is not good, but OK. This means warnings.
00146             $result['result'] = 'Warning';
00147 
00148             // Add any warnings to the result
00149             $warnings = $status->getErrorsByType( 'warning' );
00150             if ( $warnings ) {
00151                 foreach ( $warnings as &$warning ) {
00152                     $apiResult->setIndexedTagName( $warning['params'], 'param' );
00153                 }
00154                 $apiResult->setIndexedTagName( $warnings, 'warning' );
00155                 $result['warnings'] = $warnings;
00156             }
00157         } else {
00158             // Everything was fine.
00159             $result['result'] = 'Success';
00160         }
00161 
00162         // Give extensions a chance to modify the API result data
00163         wfRunHooks( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
00164 
00165         $apiResult->addValue( null, 'createaccount', $result );
00166     }
00167 
00168     public function getDescription() {
00169         return 'Create a new user account.';
00170     }
00171 
00172     public function mustBePosted() {
00173         return true;
00174     }
00175 
00176     public function isReadMode() {
00177         return false;
00178     }
00179 
00180     public function isWriteMode() {
00181         return true;
00182     }
00183 
00184     public function getAllowedParams() {
00185         return array(
00186             'name' => array(
00187                 ApiBase::PARAM_TYPE => 'user',
00188                 ApiBase::PARAM_REQUIRED => true
00189             ),
00190             'password' => null,
00191             'domain' => null,
00192             'token' => null,
00193             'email' => array(
00194                 ApiBase::PARAM_TYPE => 'string',
00195                 ApiBase::PARAM_REQUIRED => $this->getConfig()->get( 'EmailConfirmToEdit' ),
00196             ),
00197             'realname' => null,
00198             'mailpassword' => array(
00199                 ApiBase::PARAM_TYPE => 'boolean',
00200                 ApiBase::PARAM_DFLT => false
00201             ),
00202             'reason' => null,
00203             'language' => null
00204         );
00205     }
00206 
00207     public function getParamDescription() {
00208         $p = $this->getModulePrefix();
00209 
00210         return array(
00211             'name' => 'Username',
00212             'password' => "Password (ignored if {$p}mailpassword is set)",
00213             'domain' => 'Domain for external authentication (optional)',
00214             'token' => 'Account creation token obtained in first request',
00215             'email' => 'Email address of user (optional)',
00216             'realname' => 'Real name of user (optional)',
00217             'mailpassword' => 'If set to any value, a random password will be emailed to the user',
00218             'reason' => 'Optional reason for creating the account to be put in the logs',
00219             'language'
00220                 => 'Language code to set as default for the user (optional, defaults to content language)'
00221         );
00222     }
00223 
00224     public function getExamples() {
00225         return array(
00226             'api.php?action=createaccount&name=testuser&password=test123',
00227             'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
00228         );
00229     }
00230 
00231     public function getHelpUrls() {
00232         return 'https://www.mediawiki.org/wiki/API:Account_creation';
00233     }
00234 }