(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / extauth / vB.php
blob0565a2e3a574603dae5c5b5d8c56c4cb1162d937
1 <?php
2 /**
3 * External authentication with a vBulletin database.
5 * Copyright © 2009 Aryeh Gregor
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.
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.
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
22 * @file
25 /**
26 * This class supports the proprietary vBulletin forum system
27 * <http://www.vbulletin.com>, versions 3.5 and up. It calls no functions or
28 * code, only reads from the database. Example lines to put in
29 * LocalSettings.php:
31 * $wgExternalAuthType = 'ExternalUser_vB';
32 * $wgExternalAuthConf = array(
33 * 'server' => 'localhost',
34 * 'username' => 'forum',
35 * 'password' => 'udE,jSqDJ<""p=fI.K9',
36 * 'dbname' => 'forum',
37 * 'tablePrefix' => '',
38 * 'cookieprefix' => 'bb'
39 * );
41 * @ingroup ExternalUser
43 class ExternalUser_vB extends ExternalUser {
44 private $mRow;
46 protected function initFromName( $name ) {
47 return $this->initFromCond( array( 'username' => $name ) );
50 protected function initFromId( $id ) {
51 return $this->initFromCond( array( 'userid' => $id ) );
54 protected function initFromCookie() {
55 # Try using the session table. It will only have a row if the user has
56 # an active session, so it might not always work, but it's a lot easier
57 # than trying to convince PHP to give us vB's $_SESSION.
58 global $wgExternalAuthConf, $wgRequest;
59 if ( !isset( $wgExternalAuthConf['cookieprefix'] ) ) {
60 $prefix = 'bb';
61 } else {
62 $prefix = $wgExternalAuthConf['cookieprefix'];
64 if ( $wgRequest->getCookie( 'sessionhash', $prefix ) === null ) {
65 return false;
68 $db = $this->getDb();
70 $row = $db->selectRow(
71 array( 'session', 'user' ),
72 $this->getFields(),
73 array(
74 'session.userid = user.userid',
75 'sessionhash' => $wgRequest->getCookie( 'sessionhash', $prefix ),
77 __METHOD__
79 if ( !$row ) {
80 return false;
82 $this->mRow = $row;
84 return true;
87 private function initFromCond( $cond ) {
88 $db = $this->getDb();
90 $row = $db->selectRow(
91 'user',
92 $this->getFields(),
93 $cond,
94 __METHOD__
96 if ( !$row ) {
97 return false;
99 $this->mRow = $row;
101 return true;
104 private function getDb() {
105 global $wgExternalAuthConf;
106 return DatabaseBase::factory( 'mysql',
107 array(
108 'host' => $wgExternalAuthConf['server'],
109 'user' => $wgExternalAuthConf['username'],
110 'password' => $wgExternalAuthConf['password'],
111 'dbname' => $wgExternalAuthConf['dbname'],
112 'tablePrefix' => $wgExternalAuthConf['tablePrefix'],
117 private function getFields() {
118 return array( 'user.userid', 'username', 'password', 'salt', 'email',
119 'usergroupid', 'membergroupids' );
122 public function getId() { return $this->mRow->userid; }
123 public function getName() { return $this->mRow->username; }
125 public function authenticate( $password ) {
126 # vBulletin seemingly strips whitespace from passwords
127 $password = trim( $password );
128 return $this->mRow->password == md5( md5( $password )
129 . $this->mRow->salt );
132 public function getPref( $pref ) {
133 if ( $pref == 'emailaddress' && $this->mRow->email ) {
134 # TODO: only return if validated?
135 return $this->mRow->email;
137 return null;
140 public function getGroups() {
141 $groups = array( $this->mRow->usergroupid );
142 $groups = array_merge( $groups, explode( ',', $this->mRow->membergroupids ) );
143 $groups = array_unique( $groups );
144 return $groups;