r1437: Adding an authentication module for Drupal, AuthenticationDrupal
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 11 Mar 2007 06:16:00 +0000 (06:16 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 11 Mar 2007 06:16:00 +0000 (06:16 +0000)
includes/auth/auth_drupal.php [new file with mode: 0644]

diff --git a/includes/auth/auth_drupal.php b/includes/auth/auth_drupal.php
new file mode 100644 (file)
index 0000000..816e992
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+/*=====================================================================*\
+|| ###################################################################
+|| # Bugdar [#]version[#]
+|| # Copyright ©2002-[#]year[#] Blue Static
+|| #
+|| # 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; version [#]gpl[#] of the License.
+|| #
+|| # 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
+|| ###################################################################
+\*=====================================================================*/
+
+require_once('./includes/auth/auth.php');
+
+/**
+* Authentication: Drupal
+*
+* @author              Blue Static
+* @copyright   Copyright (c)2002 - [#]year[#], Blue Static
+* @version             $Revision$
+* @package             Bugdar
+*
+*/
+class AuthenticationDrupal extends Authentication
+{
+       /**
+       * Map for Bugdar fields to Drupal fields
+       */
+       var $fieldMap = array(
+               'authid'                => 'uid',
+               'displayname'   => 'name',
+               'email'                 => 'mail',
+       );
+       
+       /**
+       * The cookie name to use for Drupal. Leaving this NULL will get it from session_name()
+       */
+       var $cookieName = null;
+
+       // ###################################################################
+       function _setupDatabase()
+       {
+               // check and see if we need to call session_name()
+               $this->cookieName = ($this->cookieName == null ? session_name() : $this->cookieName);
+               
+               // connect to the DB
+               $this->authDb = new DB_MySQL($this->registry);
+               $this->authDb->connect('DRUPAL_DATABASE_SERVER', 'DATABASE_USER', 'DATABASE_PASSWORD', 'DATABASE_NAME', false);
+       }
+       
+       // ###################################################################
+       function _fetchCookieUniqueId()
+       {
+               if ($sessionId == null)
+               {
+                       $this->sessionId = $this->registry->in[ $this->cookieName ];
+               }
+               return $this->sessionId;
+       }
+       
+       // ###################################################################
+       function _fetchCookiePassword()
+       {
+               return true;
+       }
+       
+       // ###################################################################
+       function _fetchUserUsingCookies()
+       {
+               $session = $this->authDb->query_first("SELECT * FROM sessions WHERE sid = '" . $this->authDb->escape_string($this->_fetchCookieUniqueId()) . "'");
+               if (!$session OR $session['uid'] == 0)
+               {
+                       return false;
+               }
+               return $this->authDb->query_first("SELECT * FROM users WHERE uid = " . $session['uid']);
+       }
+       
+       // ###################################################################
+       function _verifyCookieData()
+       {
+               return ($this->_fetchUserUsingCookies() != false);
+       }
+       
+       // ###################################################################
+       function _fetchUserWithIdentifier($string)
+       {
+               return $this->authDb->query_first("SELECT * FROM users WHERE name = '" . $this->authDb->escape_string($string) . "'");
+       }
+       
+       // ###################################################################
+       function _verifyLoginUser($password)
+       {
+               return (md5($password) == $this->authUser['pass']);
+       }
+
+       // ###################################################################
+       function clearCookies()
+       {
+               $this->registry->funct->cookie($this->cookieName);
+               $this->authDb->query("DELETE FROM sessions WHERE sid = '" . $this->authDb->escape_string($this->_fetchCookieUniqueId()) . "'");
+       }
+       
+       // ###################################################################
+       function _setCookies($permanent = false)
+       {
+               $sid = $this->_fetchCookieUniqueId();
+               $sid = ($sid ? $sid : md5(microtime() . rand()));
+               $this->registry->funct->cookie($this->cookieName, $sid, $permanent);
+               $this->authDb->query("REPLACE INTO sessions (sid, uid, hostname, timestamp) VALUES ('$sid', '" . $this->authUser['uid'] . "', '" . $this->authDb->escape_string($_SERVER['REMOTE_ADDR']) . "', " . time() . ")");
+       }
+}
+
+/*=====================================================================*\
+|| ###################################################################
+|| # $HeadURL$
+|| # $Id$
+|| ###################################################################
+\*=====================================================================*/
+?>
\ No newline at end of file