Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Created January 27, 2011 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kballenegger/799269 to your computer and use it in GitHub Desktop.
Save kballenegger/799269 to your computer and use it in GitHub Desktop.
<?php
class User {
// static
private static $_current_user = null;
// non-static
private $_data = array();
// static
public static function current() {
if (!User::$_current_user) {
if (!empty($_COOKIE['user_id'])) {
$id = $_COOKIE['user_id'];
$signature = $_COOKIE['user_signature'];
$user = User::find_by_id($id);
if ($user && ($signature == $user->signature()))
User::$_current_user = $user;
}
}
return User::$_current_user;
}
public static function require_login() {
$user = User::current();
if (!$user)
return false;
else
return true;
}
public static function create($email, $password = null) {
$db = $GLOBALS['db'];
$m_users = $db->users;
$user_data = $m_users->findOne(array('email' => $email));
if ($user_data) // if it already exists, don't create
return false;
$user = new User();
$user->email = $email;
if (!empty($password))
$user->password_hash = sha1($password);
return true;
}
public static function signup($email, $password = null) {
$success = User::create($email, $password);
if ($success)
User::login($email, $password);
return $success;
}
public static function login($email, $password = null) {
$db = $GLOBALS['db'];
$m_users = $db->users;
$user_data = $m_users->findOne(array('email' => $email));
if ($user_data) {
if (!empty($user_data['password_hash']) && (sha1($password) != $user_data['password_hash']))
return false;
$id = $user_data['_id']->__toString();
$user = User::find_by_id($id);
User::$_current_user = $user;
setcookie('user_id', $id, time()+60*60*24*30*12*2, '/', ''); // two years
setcookie('user_signature', $user->signature(), time()+60*60*24*30*12*2, '/', ''); // two years
return true;
} else
return false;
}
public static function logout() {
setcookie('user_id', '', 1, '/', '');
setcookie('user_signature', '', 1, '/', '');
}
public static function find_by_id($id) {
if ($id) {
$db = $GLOBALS['db'];
$m_users = $db->users;
$user_data = $m_users->findOne(array('_id' => new MongoID($id)));
if ($user_data) {
$user = new User();
$user->_init_data($user_data);
return $user;
} else
return null;
}
}
// non-static
public function _init_data($data) {
$this->_data = $data;
}
public function signature() {
$id = $this->_id->__toString();
if (!empty($this->password_hash))
$password_hash = $this->password_hash;
else
$password_hash = '';
return sha1($id.$password_hash);
}
public function __get($key) {
if (isset($this->_data[$key]))
return $this->_data[$key];
else
return null;
}
public function __set($key, $value) {
$this->_data[$key] = $value;
$this->_save();
}
private function _save() {
$db = $GLOBALS['db'];
$m_users = $db->users;
$m_users->save($this->_data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment