Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Accounts {
- private static $userdata = false;
- public static function userExists($id) {
- }
- public static function getUserData($id, $override = true) {
- if(self::$userdata === false || $override) {
- $q = dibi::query("SELECT id, status, username, email, password_hash FROM accounts WHERE id = %i", $id);
- self::$userdata = $q->fetch();
- }
- return self::$userdata;
- }
- public static function getCurrentUserId() {
- $q = dibi::query("SELECT user_id FROM sessions WHERE hash = %s", Session::getSession());
- return $q->fetchSingle();
- }
- public static function getIdByUsername($username) {
- $q = dibi::query("SELECT id FROM accounts WHERE username = %s", $username);
- return $q->fetchSingle();
- }
- public static function getIdByEmail($email) {
- $q = dibi::query("SELECT id FROM accounts WHERE email = %s", $email);
- return $q->fetchSingle();
- }
- public static function getIdByRefAndHash($ref, $hash) {
- $q = dibi::query("SELECT id FROM accounts WHERE activate_hash = %s", $hash);
- return $q->fetchSingle();
- }
- public static function verifyUserCredentials($email, $password) {
- $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE email = %s", $email, " AND password_hash = %s", password($password));
- return (intval($q->fetchSingle()) > 0);
- }
- public static function verifyAccountIsActive($email) {
- $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE status = 1 AND email = %s", $email);
- return (intval($q->fetchSingle()) > 0);
- }
- public static function emailTaken($email) {
- $q = dibi::query("SELECT COUNT(*) FROM accounts WHERE email = %s", $email);
- return (intval($q->fetchSingle()) !== 0);
- }
- public static function createAccount($email, $password) {
- $hash = generate_hash();
- dibi::query("INSERT INTO accounts (email, password_hash, activate_hash, activate_ref) VALUES (%s", $email,", %s", password($password),", %s", $hash, ",%s", sha1($hash),")");
- }
- public static function activateAccount($hash, $ref) {
- $db_hash = dibi::query("SELECT activate_hash FROM accounts where activate_ref = %s", $ref);
- $db_hash = $db_hash->fetchSingle();
- if($db_hash === false) return false;
- if($db_hash === $hash) {
- dibi::query('UPDATE accounts SET status = 1 WHERE activate_ref = %s', $ref, ' AND [accounts.activate_hash] = %s', $hash);
- $id = dibi::query("SELECT id FROM accounts where activate_ref = %s", $ref);
- $event = new EventFactory();
- $event->setEvent('ActivateAccount');
- $event->addParameter('id', $id->fetchSingle());
- $event->createEvent();
- return true;
- }
- }
- public static function deactivateAccount($id) {
- dibi::query('UPDATE accounts SET status = 0 WHERE id = %i', $id);
- $event = new EventFactory();
- $event->setEvent('DeactivateAccount');
- $event->addParameter('id', $id);
- $event->createEvent();
- return true;
- }
- public static function updateUsername($id, $username) {
- if(dibi::query("UPDATE accounts SET username = %s", $username, " WHERE id = %i", $id)) {
- return true;
- }
- return false;
- }
- public static function updatePassword($id, $password, $revovery = false) {
- $password = password($password);
- if(dibi::query("UPDATE accounts SET password_hash = %s", $password, " WHERE id = %i", $id)) {
- $event = new EventFactory();
- $event->setEvent('ChangePassword');
- $event->addParameter('id', $id);
- $event->addParameter('password', $password);
- $event->createEvent();
- return true;
- }
- return false;
- }
- public static function updateEmail($id, $email) {
- if(self::getIdByEmail($email) !== false) return false;
- if(dibi::query("UPDATE accounts SET email = %s", $email, " WHERE id = %i", $id)) {
- $event = new EventFactory();
- $event->setEvent('ChangeEmail');
- $event->addParameter('id', $id);
- $event->addParameter('email', $email);
- $event->createEvent();
- return true;
- }
- return false;
- }
- public static function regenerateHash($id) {
- $hash = generate_hash();
- $ref = sha1($hash);
- dibi::query("UPDATE accounts SET activate_ref = %s", $ref, ", activate_hash = %s", $hash, " WHERE id = %i", $id);
- return [$hash, $ref];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement