Skip to content

Commit

Permalink
added register method
Browse files Browse the repository at this point in the history
  • Loading branch information
zolex committed Apr 25, 2012
1 parent 7ca04d5 commit 58b01b6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions accounts.class.php
Expand Up @@ -111,6 +111,54 @@ public static function authenticate($name, $password) {
}
}

/**
* Register a name
*
* @param String $name
* @param String $password
* @param String $email
*/
public static function register($name, $password, $email) {

if (!self::isNameAvailable($name)) {

return false;
}

$salt = substr(Encrypter::password(uniqid(microtime())), 0, 32);
$stmt = self::$dbh->prepare("INSERT INTO `players` (`name`, `password`, `salt`, `email`) VALUES(:name, :password, :salt, :email) ON DUPLICATE KEY UPDATE `password` = VALUES(`password`), `salt` = VALUES(`salt`), `email` = VALUES(`email`)");
$stmt->bindValue("name", $name);
$stmt->bindValue("email", $email);
$stmt->bindValue("salt", $salt);
$stmt->bindValue("password", Encrypter::password($password, $salt));
if (!$stmt->execute()) {

throw new Exception("Could not register player");
}

$result = $stmt->rowCount();
return ($result == 1 || $result == 2);
}

/**
* Get a player by his session
*
* @param String $sessionID
* @return Object
*/
public static function getPlayerBySession($sessionID) {

$sessionID = Encrypter::decryptSession($sessionID);
$stmt = self::$dbh->prepare("SELECT * FROM `players` WHERE `session` = :session LIMIT 1");
$stmt->bindValue("session", $sessionID);
if (!$stmt->execute()) {

throw new Exception("Could not load player by session");
}

return $stmt->fetchObject();
}

/**
* Create a new session for the given player
*
Expand Down Expand Up @@ -201,6 +249,11 @@ public static function renewSession($oldSessionID) {
}
}

/**
* Check if the session exists
*
* @param String $sessionID
*/
public static function checkSession($sessionID) {

$sessionID = Encrypter::decryptSession($sessionID);
Expand Down

0 comments on commit 58b01b6

Please sign in to comment.