PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/extauth/vB.php

https://bitbucket.org/ghostfreeman/freeside-wiki
PHP | 146 lines | 85 code | 16 blank | 45 comment | 9 complexity | 6188bb381f788a378eec458563e52834 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * External authentication with a vBulletin database.
  4. *
  5. * Copyright © 2009 Aryeh Gregor
  6. *
  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.
  11. *
  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.
  16. *
  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
  21. *
  22. * @file
  23. */
  24. /**
  25. * This class supports the proprietary vBulletin forum system
  26. * <http://www.vbulletin.com>, versions 3.5 and up. It calls no functions or
  27. * code, only reads from the database. Example lines to put in
  28. * LocalSettings.php:
  29. *
  30. * $wgExternalAuthType = 'ExternalUser_vB';
  31. * $wgExternalAuthConf = array(
  32. * 'server' => 'localhost',
  33. * 'username' => 'forum',
  34. * 'password' => 'udE,jSqDJ<""p=fI.K9',
  35. * 'dbname' => 'forum',
  36. * 'tablePrefix' => '',
  37. * 'cookieprefix' => 'bb'
  38. * );
  39. *
  40. * @ingroup ExternalUser
  41. */
  42. class ExternalUser_vB extends ExternalUser {
  43. private $mRow;
  44. protected function initFromName( $name ) {
  45. return $this->initFromCond( array( 'username' => $name ) );
  46. }
  47. protected function initFromId( $id ) {
  48. return $this->initFromCond( array( 'userid' => $id ) );
  49. }
  50. protected function initFromCookie() {
  51. # Try using the session table. It will only have a row if the user has
  52. # an active session, so it might not always work, but it's a lot easier
  53. # than trying to convince PHP to give us vB's $_SESSION.
  54. global $wgExternalAuthConf, $wgRequest;
  55. if ( !isset( $wgExternalAuthConf['cookieprefix'] ) ) {
  56. $prefix = 'bb';
  57. } else {
  58. $prefix = $wgExternalAuthConf['cookieprefix'];
  59. }
  60. if ( $wgRequest->getCookie( 'sessionhash', $prefix ) === null ) {
  61. return false;
  62. }
  63. $db = $this->getDb();
  64. $row = $db->selectRow(
  65. array( 'session', 'user' ),
  66. $this->getFields(),
  67. array(
  68. 'session.userid = user.userid',
  69. 'sessionhash' => $wgRequest->getCookie( 'sessionhash', $prefix ),
  70. ),
  71. __METHOD__
  72. );
  73. if ( !$row ) {
  74. return false;
  75. }
  76. $this->mRow = $row;
  77. return true;
  78. }
  79. private function initFromCond( $cond ) {
  80. $db = $this->getDb();
  81. $row = $db->selectRow(
  82. 'user',
  83. $this->getFields(),
  84. $cond,
  85. __METHOD__
  86. );
  87. if ( !$row ) {
  88. return false;
  89. }
  90. $this->mRow = $row;
  91. return true;
  92. }
  93. private function getDb() {
  94. global $wgExternalAuthConf;
  95. return DatabaseBase::factory( 'mysql',
  96. array(
  97. 'host' => $wgExternalAuthConf['server'],
  98. 'user' => $wgExternalAuthConf['username'],
  99. 'password' => $wgExternalAuthConf['password'],
  100. 'dbname' => $wgExternalAuthConf['dbname'],
  101. 'tablePrefix' => $wgExternalAuthConf['tablePrefix'],
  102. )
  103. );
  104. }
  105. private function getFields() {
  106. return array( 'user.userid', 'username', 'password', 'salt', 'email',
  107. 'usergroupid', 'membergroupids' );
  108. }
  109. public function getId() { return $this->mRow->userid; }
  110. public function getName() { return $this->mRow->username; }
  111. public function authenticate( $password ) {
  112. # vBulletin seemingly strips whitespace from passwords
  113. $password = trim( $password );
  114. return $this->mRow->password == md5( md5( $password )
  115. . $this->mRow->salt );
  116. }
  117. public function getPref( $pref ) {
  118. if ( $pref == 'emailaddress' && $this->mRow->email ) {
  119. # TODO: only return if validated?
  120. return $this->mRow->email;
  121. }
  122. return null;
  123. }
  124. public function getGroups() {
  125. $groups = array( $this->mRow->usergroupid );
  126. $groups = array_merge( $groups, explode( ',', $this->mRow->membergroupids ) );
  127. $groups = array_unique( $groups );
  128. return $groups;
  129. }
  130. }