Don't look for pipes in the root node.
[lhc/web/wiklou.git] / includes / extauth / vB.php
index 81e5bb6..860048f 100644 (file)
@@ -1,21 +1,26 @@
 <?php
-
-# Copyright (C) 2009 Aryeh Gregor
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
+/**
+ * External authentication with a vBulletin database.
+ *
+ * Copyright © 2009 Aryeh Gregor
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * This class supports the proprietary vBulletin forum system
  *       'username' => 'forum',
  *       'password' => 'udE,jSqDJ<""p=fI.K9',
  *       'dbname' => 'forum',
- *       'tableprefix' => ''
+ *       'tableprefix' => '',
+ *       'cookieprefix' => 'bb'
  *   );
+ *
+ * @ingroup ExternalUser
  */
 class ExternalUser_vB extends ExternalUser {
-       private $mDb, $mRow;
+       private $mRow;
 
        protected function initFromName( $name ) {
                return $this->initFromCond( array( 'username' => $name ) );
@@ -43,24 +51,45 @@ class ExternalUser_vB extends ExternalUser {
                return $this->initFromCond( array( 'userid' => $id ) );
        }
 
-       # initFromCookie() not yet implemented
+       protected function initFromCookie() {
+               # Try using the session table.  It will only have a row if the user has
+               # an active session, so it might not always work, but it's a lot easier
+               # than trying to convince PHP to give us vB's $_SESSION.
+               global $wgExternalAuthConf, $wgRequest;
+               if ( !isset( $wgExternalAuthConf['cookieprefix'] ) ) {
+                       $prefix = 'bb';
+               } else {
+                       $prefix = $wgExternalAuthConf['cookieprefix'];
+               }
+               if ( $wgRequest->getCookie( 'sessionhash', $prefix ) === null ) {
+                       return false;
+               }
 
-       private function initFromCond( $cond ) {
-               global $wgExternalAuthConf;
+               $db = $this->getDb();
 
-               $this->mDb = new Database(
-                       $wgExternalAuthConf['server'],
-                       $wgExternalAuthConf['username'],
-                       $wgExternalAuthConf['password'],
-                       $wgExternalAuthConf['dbname'],
-                       false, 0,
-                       $wgExternalAuthConf['tableprefix']
+               $row = $db->selectRow(
+                       array( 'session', 'user' ),
+                       $this->getFields(),
+                       array(
+                               'session.userid = user.userid',
+                               'sessionhash' => $wgRequest->getCookie( 'sessionhash', $prefix ),
+                       ),
+                       __METHOD__
                );
+               if ( !$row ) {
+                       return false;
+               }
+               $this->mRow = $row;
 
-               $row = $this->mDb->selectRow(
+               return true;
+       }
+
+       private function initFromCond( $cond ) {
+               $db = $this->getDb();
+
+               $row = $db->selectRow(
                        'user',
-                       array( 'userid', 'username', 'password', 'salt', 'email', 'usergroupid',
-                       'membergroupids' ),
+                       $this->getFields(),
                        $cond,
                        __METHOD__
                );
@@ -72,6 +101,23 @@ class ExternalUser_vB extends ExternalUser {
                return true;
        }
 
+       private function getDb() {
+               global $wgExternalAuthConf;
+               return new Database(
+                       $wgExternalAuthConf['server'],
+                       $wgExternalAuthConf['username'],
+                       $wgExternalAuthConf['password'],
+                       $wgExternalAuthConf['dbname'],
+                       false, 0,
+                       $wgExternalAuthConf['tableprefix']
+               );
+       }
+
+       private function getFields() {
+               return array( 'user.userid', 'username', 'password', 'salt', 'email',
+                       'usergroupid', 'membergroupids' );
+       }
+
        public function getId() { return $this->mRow->userid; }
        public function getName() { return $this->mRow->username; }