This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from PAXANDDOS/dev
Small front-end fixes. Database path fixed, aimed to my pc server
- Loading branch information
Showing
5 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
|
||
include_once "GameClasses.php"; | ||
|
||
include_once "controller_database.php"; | ||
|
||
class GameSession | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
class DatabaseConnection { | ||
public $connection; | ||
public function __construct($host, $port, $username, $password, $database) { | ||
try { | ||
$this->connection = new PDO("mysql:host=$host;dbname=$database", $username, $password); | ||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} catch(PDOException $e) { | ||
echo "::Connection failed: ".$e->getMessage().'\n'; | ||
} | ||
} | ||
public function __destruct() { | ||
$this->connection = null; | ||
} | ||
public function getConnectionStatus() | ||
{ | ||
if(isset($this->connection)) | ||
return true; | ||
return false; | ||
} | ||
} | ||
|
||
abstract class Model_database | ||
{ | ||
public $connection; | ||
public $table; | ||
|
||
abstract public function findUsername($username); | ||
abstract public function findEmail($email); | ||
abstract public function save(); | ||
abstract public function delete(); | ||
abstract public function checkPass($username, $password); | ||
abstract public function renew(); | ||
abstract public function update($username, $name, $email, $password); | ||
abstract public function getAvatar($username); | ||
abstract public function getTotalGames($username); | ||
abstract public function getTotalWins($username); | ||
abstract public function getTotalLoses($username); | ||
abstract public function incrementTotalGames($username); | ||
abstract public function incrementTotalWins($username); | ||
abstract public function incrementTotalLoses($username); | ||
abstract public function updateAvatar($username, $avatar_name); | ||
|
||
public function __construct($table) { | ||
$this->setConnection(); | ||
$this->setTable($table); | ||
} | ||
|
||
protected function setTable($table) { | ||
$this->table = $table; | ||
} | ||
protected function setConnection() { | ||
$this->connection = new DatabaseConnection('localhost', 3306, 'root', '', 'marvel_heroes'); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
require_once 'Model_database.php'; | ||
class User extends Model_database { | ||
public $id; | ||
public $username; | ||
public $password; | ||
public $name; | ||
public $email; | ||
public $avatar_name; | ||
|
||
public function __construct() { | ||
parent::__construct("users"); | ||
} | ||
public function __destruct() { | ||
$this->connection = null; | ||
} | ||
|
||
public function findUsername($username) | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if ($pdo) { | ||
if($username == $pdo["username"]) | ||
return true; | ||
else return false; | ||
} | ||
} | ||
} | ||
public function findEmail($email) | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT * FROM $this->table WHERE email='$email'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if ($pdo) { | ||
if($email == $pdo["email"]) | ||
return true; | ||
else return false; | ||
} | ||
} | ||
} | ||
public function delete() | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT id FROM " . $this->table . " WHERE id = " . $this->id . ";"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if ($pdo["id"]) { | ||
$sql = "DELETE FROM $this->table WHERE id=$this->id"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->execute(); | ||
$this->id = null; | ||
$this->username = null; | ||
$this->password = null; | ||
$this->name = null; | ||
$this->email = null; | ||
$this->avatar_name = null; | ||
} | ||
} | ||
} | ||
public function save() | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$sql = "INSERT INTO `users` (username, name, email, password, avatar_name) VALUES (:username, :name, :email, :password, :avatar_name)"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $this->username); | ||
$stmt->bindParam(":name", $this->name); | ||
$stmt->bindParam(":email", $this->email); | ||
$stmt->bindParam(":password", $this->password); | ||
$stmt->bindParam(":avatar_name", $this->avatar_name); | ||
$stmt->execute(); | ||
} | ||
} | ||
public function renew() | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$this->username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
$email = $pdo['email']; | ||
$pass = substr($pdo['password'], 0, 9); | ||
$passHash = md5($pass); | ||
$text = "\nYou have requested a renewal of your password.\nDo not give your password to anyone!\n\nYour new password is $pass\n\nDon't lose it anymore;)\n"; | ||
mail($email, "Password reminder.", $text); | ||
$this->update($pdo['username'], $pdo['name'], $pdo['email'], $passHash); | ||
} | ||
} | ||
public function update($username, $name, $email, $password) | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$sql = "UPDATE users SET name=:name, email=:email, password=:password WHERE username=:username"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $username); | ||
$stmt->bindParam(":name", $name); | ||
$stmt->bindParam(":email", $email); | ||
$stmt->bindParam(":password", $password); | ||
$stmt->execute(); | ||
} | ||
} | ||
public function checkPass($username, $password) | ||
{ | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT * FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if($pdo['password'] == $password) | ||
return true; | ||
else | ||
return false; | ||
} | ||
} | ||
public function getAvatar($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT avatar_name FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
return $pdo['avatar_name']; | ||
} | ||
} | ||
public function getTotalGames($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_games FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if(isset($pdo['total_games'])) | ||
return $pdo['total_games']; | ||
} | ||
} | ||
public function getTotalWins($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_wins FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if(isset($pdo['total_wins'])) | ||
return $pdo['total_wins']; | ||
} | ||
} | ||
public function getTotalLoses($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_loses FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
if(isset($pdo['total_loses'])) | ||
return $pdo['total_loses']; | ||
} | ||
} | ||
public function incrementTotalGames($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_games FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
$totalGames = $pdo['total_games']; | ||
$totalGames += 1; | ||
|
||
$sql = "UPDATE $this->table SET total_games=:total_games WHERE username=:username"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $username); | ||
$stmt->bindParam(":total_games", $totalGames); | ||
$stmt->execute(); | ||
} | ||
} | ||
public function incrementTotalWins($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_wins FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
$totalWins = $pdo['total_wins']; | ||
$totalWins += 1; | ||
|
||
$sql = "UPDATE $this->table SET total_wins=:total_wins WHERE username=:username"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $username); | ||
$stmt->bindParam(":total_wins", $totalWins); | ||
$stmt->execute(); | ||
} | ||
} | ||
public function incrementTotalLoses($username) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$result = $this->connection->connection->query("SELECT total_loses FROM $this->table WHERE username='$username'"); | ||
$pdo = $result->fetch(PDO::FETCH_ASSOC); | ||
$totalLoses = $pdo['total_loses']; | ||
$totalLoses += 1; | ||
|
||
$sql = "UPDATE $this->table SET total_loses=:total_loses WHERE username=:username"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $username); | ||
$stmt->bindParam(":total_loses", $totalLoses); | ||
$stmt->execute(); | ||
} | ||
} | ||
public function updateAvatar($username, $avatar_name) { | ||
if ($this->connection->getConnectionStatus()) { | ||
$sql = "UPDATE $this->table SET avatar_name=:avatar_name WHERE username=:username"; | ||
$stmt = $this->connection->connection->prepare($sql); | ||
$stmt->bindParam(":username", $username); | ||
$stmt->bindParam(":avatar_name", $avatar_name); | ||
$stmt->execute(); | ||
} | ||
} | ||
} | ||
|
||
?> |