r1437: Adding an authentication module for Drupal, AuthenticationDrupal
[bugdar.git] / includes / auth / auth_drupal.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: Drupal
26 *
27 * @author Blue Static
28 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
29 * @version $Revision$
30 * @package Bugdar
31 *
32 */
33 class AuthenticationDrupal extends Authentication
34 {
35 /**
36 * Map for Bugdar fields to Drupal fields
37 */
38 var $fieldMap = array(
39 'authid' => 'uid',
40 'displayname' => 'name',
41 'email' => 'mail',
42 );
43
44 /**
45 * The cookie name to use for Drupal. Leaving this NULL will get it from session_name()
46 */
47 var $cookieName = null;
48
49 // ###################################################################
50 function _setupDatabase()
51 {
52 // check and see if we need to call session_name()
53 $this->cookieName = ($this->cookieName == null ? session_name() : $this->cookieName);
54
55 // connect to the DB
56 $this->authDb = new DB_MySQL($this->registry);
57 $this->authDb->connect('DRUPAL_DATABASE_SERVER', 'DATABASE_USER', 'DATABASE_PASSWORD', 'DATABASE_NAME', false);
58 }
59
60 // ###################################################################
61 function _fetchCookieUniqueId()
62 {
63 if ($sessionId == null)
64 {
65 $this->sessionId = $this->registry->in[ $this->cookieName ];
66 }
67 return $this->sessionId;
68 }
69
70 // ###################################################################
71 function _fetchCookiePassword()
72 {
73 return true;
74 }
75
76 // ###################################################################
77 function _fetchUserUsingCookies()
78 {
79 $session = $this->authDb->query_first("SELECT * FROM sessions WHERE sid = '" . $this->authDb->escape_string($this->_fetchCookieUniqueId()) . "'");
80 if (!$session OR $session['uid'] == 0)
81 {
82 return false;
83 }
84 return $this->authDb->query_first("SELECT * FROM users WHERE uid = " . $session['uid']);
85 }
86
87 // ###################################################################
88 function _verifyCookieData()
89 {
90 return ($this->_fetchUserUsingCookies() != false);
91 }
92
93 // ###################################################################
94 function _fetchUserWithIdentifier($string)
95 {
96 return $this->authDb->query_first("SELECT * FROM users WHERE name = '" . $this->authDb->escape_string($string) . "'");
97 }
98
99 // ###################################################################
100 function _verifyLoginUser($password)
101 {
102 return (md5($password) == $this->authUser['pass']);
103 }
104
105 // ###################################################################
106 function clearCookies()
107 {
108 $this->registry->funct->cookie($this->cookieName);
109 $this->authDb->query("DELETE FROM sessions WHERE sid = '" . $this->authDb->escape_string($this->_fetchCookieUniqueId()) . "'");
110 }
111
112 // ###################################################################
113 function _setCookies($permanent = false)
114 {
115 $sid = $this->_fetchCookieUniqueId();
116 $sid = ($sid ? $sid : md5(microtime() . rand()));
117 $this->registry->funct->cookie($this->cookieName, $sid, $permanent);
118 $this->authDb->query("REPLACE INTO sessions (sid, uid, hostname, timestamp) VALUES ('$sid', '" . $this->authUser['uid'] . "', '" . $this->authDb->escape_string($_SERVER['REMOTE_ADDR']) . "', " . time() . ")");
119 }
120 }
121
122 /*=====================================================================*\
123 || ###################################################################
124 || # $HeadURL$
125 || # $Id$
126 || ###################################################################
127 \*=====================================================================*/
128 ?>