Public feed
#603895
<?php
require_once 'Zend/Registry.php';
class Auth
{
/* {{{ properties */
/**
* logged
*
* @var bool
* @access protected
*/
protected $logged;
/**
* level
*
* @var int
* @access protected
*/
protected $level;
/**
* db
*
* @var PDO Object
* @access private
*/
private $db;
/**
* error log
*
* @var string
* @access protected
*/
protected $errorlog;
/* }}} properties */
/* {{{ constants */
/**
* GUEST constant
* @access static
*/
const GUEST = 1;
/**
* Member constant
* @access static
*/
const MEMBER = 2;
/**
* EDITOR constant
* @access static
*/
const MODERATOR = 4;
/**
* ADMIN constant
* @access static
*/
const ADMIN = 8;
/* }}} constants */
/* {{{ constructor */
/**
* __construct
*
* @param PDO $db
* @access public
* @return void
*/
private function __construct(PDO $db)
{
$this->db = $db;
$this->level = self::GUEST ;
$this->logged = FALSE;
$this->errorlog = Zend_Registry::get('config')->errorlog;
}
/* }}} constructor */
/* {{{ getInstance */
/**
* getInstance
* Implements the Singletom pattern
*
* @param PDO $db
* @static
* @access public
* @return void
*/
public static function getInstance(PDO $db)
{
if(!isset($_SESSION['user']))
{
$_SESSION['user'] = new Auth($db);
}
else
{
// give back the DB connection
$_SESSION['user']->db = $db;
}
return $_SESSION['user'] ;
}
/* }}} getInstance */
/* {{{ getLevel */
/**
* getLevel
* get the object's access level
*
* @access public
* @return int
*/
public function getLevel()
{
return $this->level;
}
/* }}} getLevel */
/* {{{ getLoginStatus */
/**
* getLoginStatus
* get the objects login status
*
* @access public
* @return bool
*/
public function getLoginStatus()
{
return $this->logged;
}
/* }}} getLoginStatus */
/* {{{ logout */
/**
* logout
* clear the objects properties
*
* @access public
* @return void
*/
public function logout()
{
$this->logged = FALSE;
$this->level = self::GUEST;
}
/* }}} logout */
/* {{{ login */
/**
* login
* try to login a user - only accessable through check()
* This has been very customized for this application because a user
* has 2 ways of identifying themselves (custNo or phoneNo).
*
* @param int $type
* @param array $input
* @access private
* @return bool
*/
private function login($type, array $input)
{
// build my query
$sql = "SELECT Active FROM Cust_Web WHERE ";
switch($input['type'])
{
// CustNo
case 0:
$sql .= "CustomerNo = :id and Pass = SHA1(:pass)";
$id = (isset($input['CustNo'])) ? $input['CustNo'] : '';
break;
// PhoneNo
case 1:
$sql .= "PhoneNo = :id and Pass = SHA1(:pass)";
$id = (isset($input['Phone'])) ? $input['Phone'] : '';
break;
default:
$sql = 'SELECT Active = 0 WHERE 1=1 OR 1= :id OR 1= :pass';
}
// run the query on the db
try{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->bindParam(':pass', $input['pass']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
}
catch(PDOException $e){
// something wrong with the DB - handle it
if(DEBUG)
{
print "Error in Auth!: " . $e->getMessage() . "";
exit();
}
else
{
error_log('Database error in Auth.class. '."\n", 3, $this->errorlog);
error_log('Message: ' . $e->getMessage() . "\n", 3, $this->errorlog);
}
exit();
}
// $result = 1;
if(empty($row))
{
return FALSE;
}
else
{
$this->level = ($row['Active'] == TRUE)? self::MEMBER : self::GUEST ;
$this->logged = ($row['Active'] == TRUE)? TRUE : FALSE;
if(DEBUG) { echo 'Auth::login | ', var_dump($this); }
return TRUE;
}
}
/* }}} login */
/* {{{ check */
/**
* check
* check to see if a user has access
* This has been very customized for this application because a user
* has 2 ways of identifying themselves (custNo or phoneNo).
*
* @param int $pageLevel
* @access public
* @return bool
*/
public function check($minLevel)
{
if(!$this->logged)
{
if(DEBUG) { echo 'Auth::check | ', var_dump($this); }
// try to log them in
if(isset($_POST['type']) && isset($_POST['pass']))
{
if(!$this->login($_POST['type'], $_POST))
{
return FALSE;
}
}
else
{
return FALSE;
}
}
$_SESSION['user'] = $this;
if(DEBUG) { echo 'Auth class: ', var_dump($this); }
return ($this->level >= $minLevel);
}
/* }}} check */
/* {{{ __sleep */
/**
* __sleep
* used when session serializes the object. removes the DB connection.
*/
public function __sleep()
{
return array('logged','level', 'errorlog');
}
/* }}} __sleep */
/* {{{ __wakeup*/
/**
* __wakeup
* used when the object is unserialized
*/
public function __wakeup()
{
//echo('in wake up');
}
/* }}} __wakeup*/
}
?>