r1425: - The new vBulletin authentication API now works
[bugdar.git] / includes / auth / auth_vbulletin.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 require_once('./includes/auth/auth.php');
23
24 /**
25 * Authentication: vBulletin
26 *
27 * This is used to authenticate against the vBulletin user database. It
28 * does NOT use the vBulletin permission system, however. Be sure you set
29 * all the database variables and your license key.
30 *
31 * @author Blue Static
32 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
33 * @version $Revision$
34 * @package Bugdar
35 *
36 */
37 class AuthenticationVbulletin extends Authentication
38 {
39 /**
40 * This is the vBulletin license key that you can find in the Members' Area that is used in creating cookies
41 * @var string
42 */
43 var $licenseKey = 'LXXXXXXX';
44
45 /**
46 * The table prefix for all of vBulletin's tables
47 * @var string
48 */
49 var $vBTablePrefix = '';
50
51 // ###################################################################
52 function _setupDatabase()
53 {
54 $this->authDb = new DB_MySQL($this->registry);
55 $this->authDb->connect('VBULLETIN_DATABASE_SERVER', 'VB_DATABASE_USER', 'VB_DATABASE_PASSWORD', 'VBULLETIN_DATABASE_NAME', false);
56 }
57
58 // ###################################################################
59 function _fetchCookieUniqueId()
60 {
61 return $this->registry->input_clean('bbuserid', TYPE_UINT);
62 }
63
64 // ###################################################################
65 function _fetchCookiePassword()
66 {
67 return $this->registry->in['bbpassword'];
68 }
69
70 // ###################################################################
71 function _fetchUserUsingCookies()
72 {
73 return $this->authDb->query_first("SELECT * FROM {$this->vBTablePrefix}user WHERE userid = " . $this->_fetchCookieUniqueId());
74 }
75
76 // ###################################################################
77 function _verifyCookieData()
78 {
79 return (md5($this->authUser['password'] . $this->licenseKey) == $this->_fetchCookiePassword());
80 }
81
82 // ###################################################################
83 function _setCookies($sticky = false)
84 {
85 $this->registry->funct->cookie('bbuserid', $this->authUser['userid'], $sticky);
86 $this->registry->funct->cookie('bbpassword', md5($this->authUser['password'] . $this->licenseKey), $sticky);
87 }
88
89 // ###################################################################
90 function _clearCookies()
91 {
92 $this->registry->funct->cookie('bbpassword');
93 $this->registry->funct->cookie('bbuserid');
94 }
95
96 // ###################################################################
97 function _fetchBugdarUserFromAuthUser()
98 {
99 $user = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE authid = " . $this->authUser['userid']);
100 if (!$user)
101 {
102 return $this->_createBugdarUser();
103 }
104 return $user;
105 }
106
107 // ###################################################################
108 function _createBugdarUser()
109 {
110 $user = new UserAPI($this->registry);
111
112 // if the email already exists in the DB, it must be the same person so just hook up the authid
113 if ($check = $this->db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email = '" . $this->db->escape_string($this->authUser['email']) . "'"))
114 {
115 $user->set('userid', $check['userid']);
116 $user->set_condition();
117 $user->set('authid', $this->authUser['userid']);
118 $user->update();
119 $user->fetch();
120
121 return $user->objdata;
122 }
123 else
124 {
125 $user = new UserAPI($this->registry);
126 $user->set('email', $this->authUser['email']);
127 $user->set('displayname', $this->authUser['username']);
128 $user->set('password', $this->authUser['password']);
129 $user->set('usergroupid', 2);
130 $user->set('authid', $this->authUser['userid']);
131 $user->set('timezone', $this->authUser['timezoneoffset']);
132 $user->insert();
133
134 return $user->values;
135 }
136 }
137
138 // ###################################################################
139 function _fetchUserWithIdentifier($username)
140 {
141 return $this->authDb->query_first("SELECT * FROM {$this->vBTablePrefix}user WHERE username = '" . $this->authDb->escape_string($username) . "'");
142 }
143
144 // ###################################################################
145 function _verifyLoginUser($password)
146 {
147 return ($this->authUser['password'] == md5(md5($password) . $this->authUser['salt']));
148 }
149 }
150
151 /*=====================================================================*\
152 || ###################################################################
153 || # $HeadURL$
154 || # $Id$
155 || ###################################################################
156 \*=====================================================================*/
157 ?>