r25316 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25315‎ | r25316 | r25317 >
Date:12:53, 30 August 2007
Author:jvelezv
Status:old
Tags:
Comment:
Create new user accounts using ApiRegUser.php
SpecialUserlogin.php updated to support this new functionality.
Modified paths:
  • /branches/ApiEdit_Vodafone/includes/SpecialUserlogin.php (modified) (history)
  • /branches/ApiEdit_Vodafone/includes/api/ApiRegUser.php (added) (history)

Diff [purge]

Index: branches/ApiEdit_Vodafone/includes/SpecialUserlogin.php
@@ -8,6 +8,7 @@
99 * constructor
1010 */
1111 function wfSpecialUserlogin() {
 12+
1213 global $wgRequest;
1314 if( session_id() == '' ) {
1415 wfSetupSession();
@@ -23,16 +24,40 @@
2425 */
2526 class LoginForm {
2627
27 - const SUCCESS = 0;
28 - const NO_NAME = 1;
29 - const ILLEGAL = 2;
30 - const WRONG_PLUGIN_PASS = 3;
31 - const NOT_EXISTS = 4;
32 - const WRONG_PASS = 5;
33 - const EMPTY_PASS = 6;
34 - const RESET_PASS = 7;
35 - const ABORTED = 8;
36 -
 28+ const SUCCESS = 0;
 29+ const NO_NAME = 1;
 30+ const ILLEGAL = 2;
 31+ const WRONG_PLUGIN_PASS = 3;
 32+ const NOT_EXISTS = 4;
 33+ const WRONG_PASS = 5;
 34+ const EMPTY_PASS = 6;
 35+ const RESET_PASS = 7;
 36+ const ABORTED = 8;
 37+ const COOKIE = 9;
 38+ const NOCOOKIE = 10;
 39+ const READ_ONLY = 11;
 40+ const NOT_ALLOWED = 12;
 41+ const SORBS = 13;
 42+ const USER_EXISTS = 14;
 43+ const BAD_RETYPE = 15;
 44+ const TOO_SHORT = 16;
 45+ const ABORT_ERROR = 17;
 46+ const DB_ERROR = 18;
 47+ const NO_EMAIL = 19;
 48+ const MAIL_ERROR = 20;
 49+ const ACCMAILTEXT = 21;
 50+ const PASSWORD_SENT = 22;
 51+ const PASSWORD_REMINDER_THROTTLED = 23;
 52+ const NO_SUCH_USER = 24;
 53+ const RATE_LIMITED = 25;
 54+ const MAILPASSWORD_BLOCKED = 26;
 55+ const RESETPASS_FORBIDDEN = 27;
 56+ const NO_COOKIES_NEW = 28;
 57+ const NO_COOKIES_LOGIN = 29;
 58+ const ERROR = 30;
 59+ const SUCCESFUL_LOGIN = 31;
 60+
 61+
3762 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
3863 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
3964 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
@@ -51,6 +76,8 @@
5277 $this->mRetype = $request->getText( 'wpRetype' );
5378 $this->mDomain = $request->getText( 'wpDomain' );
5479 $this->mReturnTo = $request->getVal( 'returnto' );
 80+
 81+ //
5582 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
5683 $this->mPosted = $request->wasPosted();
5784 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
@@ -86,16 +113,22 @@
87114 }
88115
89116 function execute() {
 117+ $resultDetails = null;
 118+ $value = null;
 119+
90120 if ( !is_null( $this->mCookieCheck ) ) {
91 - $this->onCookieRedirectCheck( $this->mCookieCheck );
92 - return;
 121+ $value = $this->onCookieRedirectCheck( $this->mCookieCheck, $resultDetails );
 122+ return $this->processRest($value, $resultDetails);
93123 } else if( $this->mPosted ) {
94124 if( $this->mCreateaccount ) {
95 - return $this->addNewAccount();
 125+ $value = $this->addNewAccount($resultDetails);
 126+ return $this->processRest($value,$resultDetails);
96127 } else if ( $this->mCreateaccountMail ) {
97 - return $this->addNewAccountMailPassword();
 128+ $value = $this->addNewAccountMailPassword($resultDetails);
 129+ return $this->processRest($value,$resultDetails);
98130 } else if ( $this->mMailmypassword ) {
99 - return $this->mailPassword();
 131+ $value = $this->mailPassword($resultDetails);
 132+ return $this->processRest($value,$resultDetails);
100133 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
101134 return $this->processLogin();
102135 }
@@ -106,51 +139,46 @@
107140 /**
108141 * @private
109142 */
110 - function addNewAccountMailPassword() {
 143+ function addNewAccountMailPassword(&$results) {
111144 global $wgOut;
112145
113146 if ('' == $this->mEmail) {
114 - $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
115 - return;
 147+ return self::NO_EMAIL;
116148 }
117149
118 - $u = $this->addNewaccountInternal();
 150+ $u = $this->addNewaccountInternal($results);
119151
120 - if ($u == NULL) {
121 - return;
 152+ if( !is_object($u) ) {
 153+ return $u;
122154 }
123155
124156 // Wipe the initial password and mail a temporary one
125157 $u->setPassword( null );
126158 $u->saveSettings();
127 - $result = $this->mailPasswordInternal( $u, false );
 159+ $results['error'] = $this->mailPasswordInternal( $u, false );
128160
129161 wfRunHooks( 'AddNewAccount', array( $u ) );
130162
131 - $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
132 - $wgOut->setRobotpolicy( 'noindex,nofollow' );
133 - $wgOut->setArticleRelated( false );
134163
135 - if( WikiError::isError( $result ) ) {
136 - $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
137 - } else {
138 - $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
139 - $wgOut->returnToMain( false );
 164+ $results['user'] = $u;
 165+ if( WikiError::isError( $results['error'] ) ) {
 166+ return self::MAIL_ERROR;
 167+ } else {
 168+ return self::ACCMAILTEXT;
140169 }
141 - $u = 0;
142170 }
 171+
143172
144 -
145173 /**
146174 * @private
147175 */
148 - function addNewAccount() {
 176+ function addNewAccount(&$results) {
149177 global $wgUser, $wgEmailAuthentication;
150178
151179 # Create the account and abort if there's a problem doing so
152 - $u = $this->addNewAccountInternal();
153 - if( $u == NULL )
154 - return;
 180+ $u = $this->addNewAccountInternal($results);
 181+ if( !is_object($u) )
 182+ return $u;
155183
156184 # If we showed up language selection links, and one was in use, be
157185 # smart (and sensible) and save that language as the user's preference
@@ -162,11 +190,12 @@
163191 $u->saveSettings();
164192 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
165193 global $wgOut;
166 - $error = $u->sendConfirmationMail();
167 - if( WikiError::isError( $error ) ) {
168 - $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $error->getMessage() ) );
 194+ $results['error'] = $u->sendConfirmationMail();
 195+ $results['mailMsg'] = 0;
 196+ if( WikiError::isError( $results['error'] ) ) {
 197+ $results['mailMsg'] = 2;
169198 } else {
170 - $wgOut->addWikiText( wfMsg( 'confirmemail_oncreate' ) );
 199+ $results['mailMsg'] = 1;
171200 }
172201 }
173202
@@ -177,37 +206,31 @@
178207 $wgUser->setCookies();
179208 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
180209 if( $this->hasSessionCookie() ) {
181 - return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
 210+ return self::COOKIE;
182211 } else {
183 - return $this->cookieRedirectCheck( 'new' );
 212+ return self::NOCOOKIE;
184213 }
185214 } else {
186215 # Confirm that the account was created
187 - global $wgOut;
188 - $self = SpecialPage::getTitleFor( 'Userlogin' );
189 - $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
190 - $wgOut->setArticleRelated( false );
191 - $wgOut->setRobotPolicy( 'noindex,nofollow' );
192 - $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
193 - $wgOut->returnToMain( $self->getPrefixedText() );
 216+ $results['user'] = $u;
194217 wfRunHooks( 'AddNewAccount', array( $u ) );
195 - return true;
 218+ return self::SUCCESS;
196219 }
197220 }
198221
199222 /**
200223 * @private
201224 */
202 - function addNewAccountInternal() {
 225+ function addNewAccountInternal(&$results) {
203226 global $wgUser, $wgOut;
204227 global $wgEnableSorbs, $wgProxyWhitelist;
205228 global $wgMemc, $wgAccountCreationThrottle;
206229 global $wgAuth, $wgMinimalPasswordLength;
207 -
 230+
208231 // If the user passes an invalid domain, something is fishy
209232 if( !$wgAuth->validDomain( $this->mDomain ) ) {
210 - $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
211 - return false;
 233+ return self::WRONG_PASS;
 234+
212235 }
213236
214237 // If we are not allowing users to login locally, we should
@@ -218,51 +241,52 @@
219242 // domains that aren't local.
220243 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
221244 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
222 - $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
223 - return false;
 245+ return self::WRONG_PASS;
 246+
224247 }
225248 }
226249
227250 if ( wfReadOnly() ) {
228 - $wgOut->readOnlyPage();
229 - return false;
 251+ return self::READ_ONLY;
 252+
230253 }
231254
232255 # Check anonymous user ($wgUser) limitations :
233256 if (!$wgUser->isAllowedToCreateAccount()) {
234 - $this->userNotPrivilegedMessage();
235 - return false;
 257+ return self::NOT_ALLOWED;
 258+
236259 }
237260
238261 $ip = wfGetIP();
239262 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
240263 $wgUser->inSorbsBlacklist( $ip ) )
241264 {
242 - $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
243 - return;
 265+ $results['ip'] = $ip;
 266+ return self::SORBS;
 267+
244268 }
245269
246270 # Now create a dummy user ($u) and check if it is valid
247271 $name = trim( $this->mName );
248272 $u = User::newFromName( $name, 'creatable' );
249273 if ( is_null( $u ) ) {
250 - $this->mainLoginForm( wfMsg( 'noname' ) );
251 - return false;
 274+ return self::NO_NAME;
 275+
252276 }
253277
254278 if ( 0 != $u->idForName() ) {
255 - $this->mainLoginForm( wfMsg( 'userexists' ) );
256 - return false;
 279+ return self::USER_EXISTS;
 280+
257281 }
258282
259283 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
260 - $this->mainLoginForm( wfMsg( 'badretype' ) );
261 - return false;
 284+ return self::BAD_RETYPE;
 285+
262286 }
263287
264288 if ( !$u->isValidPassword( $this->mPassword ) ) {
265 - $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
266 - return false;
 289+ return self::TOO_SHORT;
 290+
267291 }
268292
269293 # Set some additional data so the AbortNewAccount hook can be
@@ -271,11 +295,12 @@
272296 $u->setRealName( $this->mRealName );
273297
274298 $abortError = '';
275 - if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
 299+ if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
276300 // Hook point to add extra creation throttles and blocks
277301 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
278 - $this->mainLoginForm( $abortError );
279 - return false;
 302+ $results['error'] = $abortError;
 303+ return self::ABORT_ERROR;
 304+
280305 }
281306
282307 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
@@ -285,14 +310,14 @@
286311 $wgMemc->set( $key, 1, 86400 );
287312 }
288313 if ( $value > $wgAccountCreationThrottle ) {
289 - $this->throttleHit( $wgAccountCreationThrottle );
290 - return false;
 314+
 315+ return self::ILLEGAL;
291316 }
292317 }
293318
294319 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
295 - $this->mainLoginForm( wfMsg( 'externaldberror' ) );
296 - return false;
 320+ return self::DB_ERROR;
 321+
297322 }
298323
299324 return $this->initUser( $u, false );
@@ -419,6 +444,116 @@
420445 return $retval;
421446 }
422447
 448+ function processRest($value,$results = null) {
 449+ global $wgUser, $wgAuth, $wgOut;
 450+ if ($results['mailMsg'] == 1) {
 451+ $wgOut->addWikiText( wfMsg( 'confirmemail_oncreate' ) );
 452+ } else if ($results['mailMsg'] == 2) {
 453+ $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $results['error']->getMessage() ) );
 454+ }
 455+ switch ($value)
 456+ {
 457+ case self::SUCCESS:
 458+ $self = SpecialPage::getTitleFor( 'Userlogin' );
 459+ $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
 460+ $wgOut->setArticleRelated( false );
 461+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 462+ $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $this->mName ) );
 463+ $wgOut->returnToMain( $self->getPrefixedText() );
 464+ break;
 465+
 466+ case self::COOKIE:
 467+ $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
 468+ break;
 469+ case self::NOCOOKIE:
 470+ $this->cookieRedirectCheck( 'new' );
 471+ break;
 472+ case self::WRONG_PASS :
 473+ $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
 474+ break;
 475+ case self::READ_ONLY:
 476+ $wgOut->readOnlyPage();
 477+ break;
 478+ case self::NOT_ALLOWED:
 479+ $this->userNotPrivilegedMessage();
 480+ break;
 481+ case self::SORBS:
 482+ $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $results['ip'] ) . ')' );
 483+ break;
 484+ case self::NO_NAME:
 485+ $this->mainLoginForm( wfMsg( 'noname' ) );
 486+ break;
 487+ case self::USER_EXISTS:
 488+ $this->mainLoginForm( wfMsg( 'userexists' ) );
 489+ break;
 490+ case self::BAD_RETYPE:
 491+ $this->mainLoginForm( wfMsg( 'badretype' ) );
 492+ break;
 493+ case self::TOO_SHORT:
 494+ $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
 495+ break;
 496+ case self::ABORT_ERROR:
 497+ $this->mainLoginForm( $results['error'] );
 498+ break;
 499+ case self::DB_ERROR:
 500+ $this->mainLoginForm( wfMsg( 'externaldberror' ) );
 501+ break;
 502+ case self::NO_EMAIL:
 503+ $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
 504+ break;
 505+ case self::MAIL_ERROR:
 506+ $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
 507+ $wgOut->setRobotpolicy( 'noindex,nofollow' );
 508+ $wgOut->setArticleRelated( false );
 509+ $this->mainLoginForm( wfMsg( 'mailerror', $results['error']->getMessage() ) );
 510+ break;
 511+ case self::ACCMAILTEXT:
 512+ $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
 513+ $wgOut->setRobotpolicy( 'noindex,nofollow' );
 514+ $wgOut->setArticleRelated( false );
 515+ $wgOut->addWikiText( wfMsg( 'accmailtext', $results['user']->getName(), $results['user']->getEmail() ) );
 516+ $wgOut->returnToMain( false );
 517+ break;
 518+ case self::ILLEGAL:
 519+ $this->throttleHit( $wgAccountCreationThrottle );
 520+ break;
 521+ case self::PASSWORD_SENT:
 522+ $this->mainLoginForm( wfMsg( 'passwordsent', $results['user']->getName() ), 'success' );
 523+ break;
 524+ case self::PASSWORD_REMINDER_THROTTLED:
 525+ global $wgPasswordReminderResendTime;
 526+ # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
 527+ $this->mainLoginForm( wfMsg( 'throttled-mailpassword', round( $wgPasswordReminderResendTime, 3 ) ) );
 528+ break;
 529+ case self::NO_SUCH_USER:
 530+ $this->mainLoginForm( wfMsg( 'nosuchuser', $results['user']->getName() ) );
 531+ break;
 532+ case self::RATE_LIMITED:
 533+ $wgOut->rateLimited();
 534+ break;
 535+ case self::MAILPASSWORD_BLOCKED:
 536+ $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
 537+ break;
 538+ case self::RESETPASS_FORBIDDEN:
 539+ $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
 540+ break;
 541+ case self::NO_COOKIES_NEW:
 542+ $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
 543+ break;
 544+ case self::NO_COOKIES_LOGIN:
 545+ $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
 546+ break;
 547+ case self::ERROR:
 548+ $this->mainLoginForm( wfMsg( 'error' ) );
 549+ break;
 550+ case self::LOGIN_SUCCESS:
 551+ $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
 552+ break;
 553+ default:
 554+ wfDebugDieBacktrace( "Unhandled case value" );
 555+ }
 556+ }
 557+
423558 function processLogin() {
424559 global $wgUser, $wgAuth;
425560
@@ -475,55 +610,53 @@
476611 /**
477612 * @private
478613 */
479 - function mailPassword() {
 614+ function mailPassword(&$results){
480615 global $wgUser, $wgOut, $wgAuth;
481616
482617 if( !$wgAuth->allowPasswordChange() ) {
483 - $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
484 - return;
 618+ return self::RESETPASS_FORBIDDEN;
 619+
485620 }
486621
487622 # Check against blocked IPs
488623 # fixme -- should we not?
489624 if( $wgUser->isBlocked() ) {
490 - $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
491 - return;
 625+ return self::MAILPASSWORD_BLOCKED;
 626+
492627 }
493628
494629 # Check against the rate limiter
495630 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
496 - $wgOut->rateLimited();
497 - return;
 631+ return self::RATE_LIMITED;
 632+
498633 }
499634
500635 if ( '' == $this->mName ) {
501 - $this->mainLoginForm( wfMsg( 'noname' ) );
502 - return;
 636+ return self::NO_NAME;
 637+
503638 }
504639 $u = User::newFromName( $this->mName );
505640 if( is_null( $u ) ) {
506 - $this->mainLoginForm( wfMsg( 'noname' ) );
507 - return;
 641+ return self::NO_NAME;
 642+
508643 }
509644 if ( 0 == $u->getID() ) {
510 - $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
511 - return;
 645+ $results['user']=$u;
 646+ return self::NO_SUCH_USER;
 647+
512648 }
513649
514650 # Check against password throttle
515651 if ( $u->isPasswordReminderThrottled() ) {
516 - global $wgPasswordReminderResendTime;
517 - # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
518 - $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
519 - round( $wgPasswordReminderResendTime, 3 ) ) );
520 - return;
 652+ return self::PASSWORD_REMINDER_THROTTLED;
521653 }
522654
523 - $result = $this->mailPasswordInternal( $u, true );
524 - if( WikiError::isError( $result ) ) {
525 - $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
 655+ $results['error'] = $this->mailPasswordInternal( $u, true );
 656+ $results['user'] = $u;
 657+ if( WikiError::isError( $results['error'] ) ) {
 658+ return self::MAIL_ERROR;
526659 } else {
527 - $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
 660+ return self::PASSWORD_SENT;
528661 }
529662 }
530663
@@ -756,20 +889,20 @@
757890 /**
758891 * @private
759892 */
760 - function onCookieRedirectCheck( $type ) {
 893+ function onCookieRedirectCheck( $type, &$results ) {
761894 global $wgUser;
762895
763896 if ( !$this->hasSessionCookie() ) {
764897 if ( $type == 'new' ) {
765 - return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
 898+ return self::NO_COOKIES_NEW;
766899 } else if ( $type == 'login' ) {
767 - return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
 900+ return self::NO_COOKIES_LOGIN;
768901 } else {
769 - # shouldn't happen
770 - return $this->mainLoginForm( wfMsg( 'error' ) );
 902+ # shouldn't happen
 903+ return self::ERROR;
771904 }
772905 } else {
773 - return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
 906+ return self::LOGIN_SUCCESS;
774907 }
775908 }
776909
Index: branches/ApiEdit_Vodafone/includes/api/ApiRegUser.php
@@ -0,0 +1,230 @@
 2+<?php
 3+
 4+/*
 5+ * Created on Aug 1, 2007
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2007 Jesus Velez
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if (!defined('MEDIAWIKI')) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once ("ApiBase.php");
 30+}
 31+
 32+
 33+/**
 34+ * A module to register new user accounts.
 35+ *
 36+ * @addtogroup API
 37+ */
 38+class ApiRegUser extends ApiBase {
 39+
 40+ const GET_CAPTCHA = -1;
 41+ const MISSING_CAPTCHA = -2;
 42+
 43+ public function __construct($query, $moduleName) {
 44+ parent :: __construct($query, $moduleName, 'ru');
 45+ }
 46+
 47+
 48+
 49+ public function process($value,$results = null) {
 50+ switch ($value) {
 51+ case ApiRegUser::GET_CAPTCHA :
 52+ $myCaptcha = new FancyCaptcha();
 53+ $myCaptcha->storage->clearAll();
 54+ $result['result'] = 'CaptchaIdGenerated';
 55+ $myCaptcha->getXML($result);
 56+ break;
 57+ case ApiRegUser::MISSING_CAPTCHA :
 58+ $myCaptcha = new FancyCaptcha();
 59+ $myCaptcha->storage->clearAll();
 60+ $result['result'] = 'MissingCaptcha';
 61+ $myCaptcha->getXML($result);
 62+ break;
 63+ case LoginForm::SUCCESS :
 64+ $result['result'] = 'Success';
 65+ break;
 66+ case LoginForm::COOKIE :
 67+ $result['result'] = 'Logged';
 68+ $result['userid'] = $_SESSION['wsUserID'];
 69+ $result['username'] = $_SESSION['wsUserName'];
 70+ $result['token'] = $_SESSION['wsToken'];
 71+ break;
 72+ case LoginForm::NOCOOKIE :
 73+ $result['result'] = 'NoCookie';
 74+ $result['userid'] = $_SESSION['wsUserID'];
 75+ $result['username'] = $_SESSION['wsUserName'];
 76+ $result['token'] = $_SESSION['wsToken'];
 77+ break;
 78+ case LoginForm::WRONG_PASS :
 79+ $result['result'] = 'WrongPassword';
 80+ break;
 81+ case LoginForm::READ_ONLY :
 82+ $result['result'] = 'ReadOnly';
 83+ break;
 84+ case LoginForm::NOT_ALLOWED :
 85+ $result['result'] = 'NotAllowed';
 86+ break;
 87+ case LoginForm::SORBS :
 88+ $result['result'] = 'Sorbs';
 89+ $result['blockedIp'] = $results['ip'];
 90+ break;
 91+ case LoginForm::NO_NAME :
 92+ $result['result'] = 'NoName';
 93+ break;
 94+ case LoginForm::USER_EXISTS :
 95+ $result['result'] = 'UserExists';
 96+ break;
 97+ case LoginForm::BAD_RETYPE :
 98+ $result['result'] = 'BadRetype';
 99+ break;
 100+ case LoginForm::TOO_SHORT :
 101+ $result['result'] = 'TooShort';
 102+ break;
 103+ case LoginForm::ABORT_ERROR :
 104+ $result['result'] = 'AbortError';
 105+ break;
 106+ case LoginForm::DB_ERROR :
 107+ $result['result'] = 'DbError';
 108+ break;
 109+ case LoginForm::NO_EMAIL :
 110+ $result['result'] = 'NoEmail';
 111+ break;
 112+ case LoginForm::MAIL_ERROR :
 113+ $result['result'] = 'MailError';
 114+ break;
 115+ case LoginForm::ACCMAILTEXT :
 116+ $result['result'] = 'AccMailText';
 117+ $result['userid'] = $_SESSION['wsUserID'];
 118+ $result['username'] = $_SESSION['wsUserName'];
 119+ $result['token'] = $_SESSION['wsToken'];
 120+ break;
 121+ default :
 122+ $result['result'] = 'Invalid';
 123+
 124+ }
 125+ if ($results['mailMsg'] == 1) {
 126+ $result['confirmEmail'] = 'MailSent';
 127+ } else if ($results['mailMsg'] == 2) {
 128+ $result['confirmEmail'] = $results['error']->getMessage();
 129+ }
 130+ $this->getResult()->addValue(null, 'reguser', $result);
 131+ }
 132+
 133+ public function checkCaptcha() {
 134+ global $wgHooks;
 135+ $i = 0;
 136+ $value = false;
 137+ while ($i < sizeof($wgHooks['UserCreateForm'])) {
 138+ if ($wgHooks['UserCreateForm'][$i][0] instanceof FancyCaptcha) $value = true;
 139+ $i++;
 140+ }
 141+ return $value;
 142+ }
 143+
 144+ public function execute() {
 145+ global $wgRequest;
 146+
 147+ $resultDetails = null;
 148+ $value = null;
 149+ if( session_id() == '' ) {
 150+ wfSetupSession();
 151+ }
 152+
 153+ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 154+ extract($this->extractRequestParams());
 155+ if ( (strlen($user) == 0) && (strlen($password) == 0) && ($this->checkCaptcha()) ) {
 156+ $value = ApiRegUser::GET_CAPTCHA;
 157+ } elseif ($this->checkCaptcha() && ($captchaid == 0)) {
 158+ $value = ApiRegUser::MISSING_CAPTCHA;
 159+ } else {
 160+ $data = array('wpName' => $user,
 161+ 'wpPassword' => $password,
 162+ 'wpRetype' => $password,
 163+ 'wpEmail' => $email,
 164+ 'wpRealName' => $name,
 165+ 'wpCaptchaWord' => $captchaword,
 166+ 'wpCaptchaId' => $captchaid,
 167+ 'wpDomain' => $domain);
 168+ $request = new FauxRequest($data);
 169+ $wgRequest = $request;
 170+ $form = new LoginForm( $request );
 171+ $value = $form->addNewAccount($resultDetails);
 172+ }
 173+ }
 174+ $this->process($value,$resultDetails);
 175+ }
 176+
 177+ protected function getDescription() {
 178+ return 'Create new user account';
 179+ }
 180+
 181+ protected function getExamples() {
 182+ return array (
 183+ "Create new user account using MediaWiki API",
 184+ " You must register using POST method."
 185+ );
 186+ }
 187+
 188+ protected function getAllowedParams() {
 189+ return array (
 190+ 'user' => array(
 191+ ApiBase :: PARAM_TYPE => 'string'
 192+ ),
 193+ 'password' => array(
 194+ ApiBase :: PARAM_TYPE => 'string'
 195+ ),
 196+ 'email' => array(
 197+ ApiBase :: PARAM_TYPE => 'string'
 198+ ),
 199+ 'name' => array(
 200+ ApiBase :: PARAM_TYPE => 'string'
 201+ ),
 202+ 'captchaword' => array(
 203+ ApiBase :: PARAM_TYPE => 'string'
 204+ ),
 205+ 'captchaid' => array(
 206+ ApiBase :: PARAM_TYPE => 'string'
 207+ ),
 208+ 'domain' => array(
 209+ ApiBase :: PARAM_TYPE => 'string'
 210+ )
 211+
 212+ );
 213+ }
 214+
 215+ protected function getParamDescription() {
 216+ return array (
 217+ 'user' => 'user login name',
 218+ 'password' => 'user password',
 219+ 'email' => 'user email',
 220+ 'name' => 'user name',
 221+ 'domain' => 'domain',
 222+ 'captchaid' => 'question',
 223+ 'captchaword' => 'answer'
 224+ );
 225+ }
 226+
 227+ public function getVersion() {
 228+ return __CLASS__ . ': $Id: ApiRegUser.php 22289 2007-05-20 23:31:44Z jvelezv $';
 229+ }
 230+}
 231+?>

Status & tagging log