/** * Constructor * @param MVC\Db\Adapter $adapter Database adapter */ public function __construct($adapter = null) { // Set the default database adapter if none supplied if ($adapter == null) { $adapter = Db::getDefaultAdapter(); } $this->adapter = $adapter; }
/** * Staticly authenticate the user by checing a username/password match * @param string $username Username to check * @param string $password Coupling password * @return false|MVC\User False if no match, user instance if they do */ public static function authenticate($username, $password) { // Prepare the password $password = self::password($password); // Retrieve data from the database $check = \MVC\Db::prepare('SELECT id, username, email, registered FROM users WHERE username = ? AND password = ? LIMIT 1'); $check->bind_param('ss', $username, $password); $check->execute(); $check->store_result(); // Return false if no match found if ($check->num_rows == 0) { $check->close(); return false; } // Setup a new user instance $user = new self(); $check->bind_result($userId, $userUsername, $userEmail, $userRegistered); $check->fetch(); $check->close(); $user->id = $userId; $user->username = $userUsername; $user->email = $userEmail; $user->registered = $userRegistered; return $user; }
/** * Check whether the session exists * @param int $userId User id for session * @param string $sessionKey Associated session key * @return false|array False if session didn't exist, otherwise array of keys */ private static function exists($userId, $sessionKey) { // Prepare check query $query = 'SELECT session_key, logout_key FROM sessions WHERE user = ? AND session_key = ? AND expires > NOW() LIMIT 1'; $check = Db::prepare($query); $check->bind_param('is', $userId, $sessionKey); $check->execute(); $check->store_result(); // Return false if session didn't exist if ($check->num_rows == 0) { $check->close(); return false; } // Return session and logout keys $return = array(); $check->bind_result($return['session_key'], $return['logout_key']); $check->fetch(); $check->close(); return $return; }