MediaWiki  REL1_19
vB.php
Go to the documentation of this file.
00001 <?php
00043 class ExternalUser_vB extends ExternalUser {
00044         private $mRow;
00045 
00046         protected function initFromName( $name ) {
00047                 return $this->initFromCond( array( 'username' => $name ) );
00048         }
00049 
00050         protected function initFromId( $id ) {
00051                 return $this->initFromCond( array( 'userid' => $id ) );
00052         }
00053 
00054         protected function initFromCookie() {
00055                 # Try using the session table.  It will only have a row if the user has
00056                 # an active session, so it might not always work, but it's a lot easier
00057                 # than trying to convince PHP to give us vB's $_SESSION.
00058                 global $wgExternalAuthConf, $wgRequest;
00059                 if ( !isset( $wgExternalAuthConf['cookieprefix'] ) ) {
00060                         $prefix = 'bb';
00061                 } else {
00062                         $prefix = $wgExternalAuthConf['cookieprefix'];
00063                 }
00064                 if ( $wgRequest->getCookie( 'sessionhash', $prefix ) === null ) {
00065                         return false;
00066                 }
00067 
00068                 $db = $this->getDb();
00069 
00070                 $row = $db->selectRow(
00071                         array( 'session', 'user' ),
00072                         $this->getFields(),
00073                         array(
00074                                 'session.userid = user.userid',
00075                                 'sessionhash' => $wgRequest->getCookie( 'sessionhash', $prefix ),
00076                         ),
00077                         __METHOD__
00078                 );
00079                 if ( !$row ) {
00080                         return false;
00081                 }
00082                 $this->mRow = $row;
00083 
00084                 return true;
00085         }
00086 
00087         private function initFromCond( $cond ) {
00088                 $db = $this->getDb();
00089 
00090                 $row = $db->selectRow(
00091                         'user',
00092                         $this->getFields(),
00093                         $cond,
00094                         __METHOD__
00095                 );
00096                 if ( !$row ) {
00097                         return false;
00098                 }
00099                 $this->mRow = $row;
00100 
00101                 return true;
00102         }
00103 
00104         private function getDb() {
00105                 global $wgExternalAuthConf;
00106                 return DatabaseBase::factory( 'mysql',
00107                         array(
00108                                 'host' => $wgExternalAuthConf['server'],
00109                                 'user' => $wgExternalAuthConf['username'],
00110                                 'password' => $wgExternalAuthConf['password'],
00111                                 'dbname' => $wgExternalAuthConf['dbname'],
00112                                 'tablePrefix' => $wgExternalAuthConf['tablePrefix'],
00113                         )
00114                 );
00115         }
00116 
00117         private function getFields() {
00118                 return array( 'user.userid', 'username', 'password', 'salt', 'email',
00119                         'usergroupid', 'membergroupids' );
00120         }
00121 
00122         public function getId() { return $this->mRow->userid; }
00123         public function getName() { return $this->mRow->username; }
00124 
00125         public function authenticate( $password ) {
00126                 # vBulletin seemingly strips whitespace from passwords
00127                 $password = trim( $password );
00128                 return $this->mRow->password == md5( md5( $password )
00129                         . $this->mRow->salt );
00130         }
00131 
00132         public function getPref( $pref ) {
00133                 if ( $pref == 'emailaddress' && $this->mRow->email ) {
00134                         # TODO: only return if validated?
00135                         return $this->mRow->email;
00136                 }
00137                 return null;
00138         }
00139 
00140         public function getGroups() {
00141                 $groups = array( $this->mRow->usergroupid );
00142                 $groups = array_merge( $groups, explode( ',', $this->mRow->membergroupids ) );
00143                 $groups = array_unique( $groups );
00144                 return $groups;
00145         }
00146 }