<?php
class UserManager {
private static function populateUser($row) {
$user = new User_Model;
$user->setUserID($row->userID);
$user->setUsername($row->username);
$user->setPasswordHash($row->passwordHash);
$user->setEmail($row->email);
$user->setIsActivated($row->isActivated);
$user->setActivationCode($row->activationCode);
$user->setSalt($row->salt);
$user->setPasswordResetCode($row->passwordResetCode);
if (! empty($row->recentlyUsedAddresses)) {
$user->setRecentlyUsedAddresses(unserialize($row->recentlyUsedAddresses));
}
return $user;
}
public static function doesPasswordMeetPolicy($username, $password) {
//password must be at least 5 characters
if (strlen($password) < 5) {
return FALSE;
}
//password must not be username
if (strtolower($password) == strtolower($username)) {
return FALSE;
}
//looks good
return TRUE;
}
protected static function generateRandomCode() {
$db = Database::instance();
$unique = 0;
while ($unique == 0) {
$randomString = rand(5, 100000).time().rand(5, 100000);
$randomString = sha1($randomString);
$randomString = md5($randomString);
$randomString = sha1($randomString);
$activationCode = substr($randomString, 0, 40);
$result = $db->query("SELECT activationCode from users WHERE activationCode='$activationCode'");
if ($result->count() == 0) {
$unique = 1;
} else {
$unique = 0;
}
}
return $activationCode;
}
public static function addUser(User_Model $user) {
$db = Database::instance();
$username = $user->getUsername();
$passwordHash = $user->getPasswordHash();
$salt = $user->getSalt();
$email = $user->getEmail();
$isActivated = $user->getIsActivated();
$activationCode = self::generateRandomCode();
$user->setActivationCode($activationCode);
$query = "INSERT INTO users (username, passwordHash, salt, email, activationCode, isActivated) VALUES ('$username', '$passwordHash', '$salt', '$email', '$activationCode', '$isActivated')";
$result = $db->query($query);
$user->setUserID($result->insert_id());
return $user;
}
public static function updateUser(User_Model $user) {
$db = Database::instance();
$username = $user->getUsername();
$passwordHash = $user->getPasswordHash();
$salt = $user->getSalt();
$email = $user->getEmail();
$isActivated = $user->getIsActivated();
$activationCode = $user->getActivationCode();
$userID = $user->getUserID();
$passwordResetCode = $user->getPasswordResetCode();
$recentlyUsedAddresses = serialize($user->getRecentlyUsedAddresses());
$query = "UPDATE users SET username='$username', passwordHash='$passwordHash', salt='$salt', email='$email', isActivated='$isActivated', activationCode='$activationCode', passwordResetCode='$passwordResetCode', recentlyUsedAddresses='$recentlyUsedAddresses' WHERE userID='$userID' LIMIT 1";
$db->query($query);
return $user;
}
public static function isUsernameTaken($username) {
$db = Database::instance();
$result = $db->query("SELECT username FROM users WHERE username='$username'");
if ($result->count() > 0) {
return TRUE;
} else {
return FALSE;
}
}
public static function isEmailUsed($email) {
$db = Database::instance();
$result = $db->query("SELECT email FROM users WHERE email='$email'");
if ($result->count() > 0) {
return TRUE;
} else {
return FALSE;
}
}
public static function sendActivationEmail(User_Model $user) {
require_once (APPPATH.'/libraries/swiftmailer/swift_required.php');
$email = $user->getEmail();
$activationCode = $user->getActivationCode();
$domain = Kohana::config_load('config');
$domain = $domain['site_domain'];
$body = 'Thank you for registering on JesseWeather.com. Please click the below link to activate your account:'.PHP_EOL;
$body .= 'http://'.$domain.'/user/activate/'.$activationCode;
$transport = Swift_MailTransport::newInstance();
$message = Swift_Message::newInstance();
$message->setSubject('Activate Account');
$message->setFrom('support@jesseweather.com');
$message->setTo($email);
$message->setBody($body);
$transport->send($message);
}
public static function sendPasswordResetEmail(User_Model $user) {
require_once (APPPATH.'/libraries/swiftmailer/swift_required.php');
$email = $user->getEmail();
$randomCode = self::generateRandomCode();
//save to user
$user->setPasswordResetCode($randomCode);
self::updateUser($user);
$domain = Kohana::config_load('config');
$domain = $domain['site_domain'];
$body = 'You may reset your JesseWeather password with the below link.'.PHP_EOL;
$body .= 'http://'.$domain.'/user/reset/'.$randomCode.PHP_EOL;
$body .= 'Your username is: '.$user->getUsername();
$transport = Swift_MailTransport::newInstance();
$message = Swift_Message::newInstance();
$message->setSubject('Reset Password');
$message->setFrom('donotreply@jesseweather.com');
$message->setTo($email);
$message->setBody($body);
$transport->send($message);
}
public static function activateUser($activationCode) {
$db = Database::instance();
$query = "SELECT * from users WHERE activationCode='$activationCode'";
$result = $db->query($query);
foreach ($result as $row) {
$user = self::populateUser($row);
$user->setIsActivated(1);
$user = self::updateUser($user);
return $user;
}
return FALSE;
}
public static function isActivationCodeActivated($activationCode) {
$db = Database::instance();
$query = "SELECT * from users WHERE activationCode='$activationCode'";
$result = $db->query($query);
foreach ($result as $row) {
if ($row->isActivated == '1') {
return TRUE;
} else {
return FALSE;
}
}
return FALSE;
}
public static function getUserByUsername($username) {
$db = Database::instance();
$result = $db->query("SELECT * FROM users WHERE username='$username'");
if ($result->count() > 0) {
foreach ($result as $row) {
$user = self::populateUser($row);
return $user;
}
} else {
return FALSE;
}
}
public static function getAllUsers() {
$db = Database::instance();
$result = $db->query("SELECT * FROM users");
$list = array();
foreach ($result as $row) {
$list[] = self::populateUser($row);
}
return $list;
}
public static function getUserByUserID($userID) {
$db = Database::instance();
$result = $db->query("SELECT * FROM users WHERE userID='$userID'");
if ($result->count() > 0) {
foreach ($result as $row) {
$user = self::populateUser($row);
return $user;
}
} else {
return FALSE;
}
}
public static function getUserByEmail($email) {
$db = Database::instance();
$result = $db->query("SELECT * FROM users WHERE email='$email'");
if ($result->count() > 0) {
foreach ($result as $row) {
$user = self::populateUser($row);
return $user;
}
} else {
return FALSE;
}
}
public static function getUserByPasswordResetCode($resetCode) {
$db = Database::instance();
$result = $db->query("SELECT * FROM users WHERE passwordResetCode='$resetCode'");
if ($result->count() > 0) {
foreach ($result as $row) {
$user = self::populateUser($row);
return $user;
}
} else {
return FALSE;
}
}
public static function authenticateUser($username, $password, $rememberMe) {
$user = self::getUserByUsername($username);
if (!$user) {
return FALSE;
} else {
$passwordHash = $user->hashPassword($password);
if ($passwordHash == $user->getPasswordHash()) {
$session = Session::instance();
$_SESSION['userID'] = $user->getUserID();
//set a rememberMe for a year as well if $rememberMe
if ($rememberMe) {
self::setRememberMeCookie();
}
return $user;
} else {
return FALSE;
}
}
}
private static function setRememberMeCookie() {
$rememberMeID = uuidhelper::generate();
$rememberMeID .= '-'.uuidhelper::generate();
$oneYearFromNow = time() + 86400 * 365;
$domain = Kohana::config('config');
$domain = $domain['site_domain'];
setcookie('rememberme', $rememberMeID, $oneYearFromNow, '/', $domain);
}
public static function isUserLoggedIn() {
$session = Session::instance();
if (isset($_SESSION['userID'])) {
return self::getUserByUserID($_SESSION['userID']);
}
}
public static function logout() {
self::deleteRememberMeCookie();
$session = Session::instance();
$session->destroy();
}
private static function deleteRememberMeCookie() {
$domain = Kohana::config('config');
$domain = $domain['site_domain'];
setcookie('rememberme', '', time() - 86400 * 365, '/', $domain);
return TRUE;
}
}
?>