mirror of
https://github.com/simon987/Much-Assembly-Required-Frontend.git
synced 2025-12-14 07:09:06 +00:00
Initial commit
This commit is contained in:
52
include/MessageCookie.php
Normal file
52
include/MessageCookie.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: simon
|
||||
* Date: 24/09/17
|
||||
* Time: 1:59 PM
|
||||
*/
|
||||
|
||||
class MessageCookie
|
||||
{
|
||||
|
||||
private $msg;
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* MessageCookie constructor.
|
||||
* @param $msg
|
||||
* @param $type
|
||||
*/
|
||||
public function __construct($msg, $type)
|
||||
{
|
||||
$this->msg = $msg;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function setCookie()
|
||||
{
|
||||
setCookie($this->type, $this->msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get message
|
||||
* @param $type string
|
||||
* @return string Cookie message
|
||||
*/
|
||||
public static function getMsg($type)
|
||||
{
|
||||
if (isset($_COOKIE[$type])) {
|
||||
|
||||
$msg = $_COOKIE[$type];
|
||||
|
||||
//Clear cookie
|
||||
setcookie($type, "", -1);
|
||||
|
||||
return $msg;
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
32
include/ServerInfo.php
Normal file
32
include/ServerInfo.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
class ServerInfo
|
||||
{
|
||||
|
||||
public $token;
|
||||
|
||||
public $username;
|
||||
|
||||
public $address;
|
||||
|
||||
public $tickLength;
|
||||
|
||||
public $serverName;
|
||||
|
||||
/**
|
||||
* ServerInfo constructor.
|
||||
* @param $token
|
||||
* @param $username
|
||||
*/
|
||||
public function __construct($token, $username)
|
||||
{
|
||||
$this->token = $token;
|
||||
$this->username = $username;
|
||||
|
||||
$this->address = MAR_ADDRESS;
|
||||
$this->serverName = MAR_SERVER_NAME;
|
||||
$this->tickLength = MAR_TICK_LENGTH;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
50
include/SessionManager.php
Normal file
50
include/SessionManager.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
include_once "include/config.php";
|
||||
|
||||
class SessionManager
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $user User
|
||||
*/
|
||||
public static function generate($user)
|
||||
{
|
||||
|
||||
session_name(SESSION_NAME);
|
||||
session_start();
|
||||
|
||||
$_SESSION['user'] = json_encode($user);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public static function get()
|
||||
{
|
||||
session_name(SESSION_NAME);
|
||||
session_start();
|
||||
|
||||
if (isset($_SESSION['user'])) {
|
||||
return json_decode($_SESSION['user'], true);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
public static function clear()
|
||||
{
|
||||
session_name(SESSION_NAME);
|
||||
session_start();
|
||||
|
||||
unset($_SESSION['user']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
include/SqlConnection.php
Normal file
11
include/SqlConnection.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
include_once "include/config.php";
|
||||
|
||||
|
||||
class SqlConnection extends PDO
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(SQL_HOST, SQL_USER, SQL_PASS);
|
||||
}
|
||||
}
|
||||
35
include/TokenManager.php
Normal file
35
include/TokenManager.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
include_once "include/SqlConnection.php";
|
||||
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
class TokenManager
|
||||
{
|
||||
/**
|
||||
* @param $user string Generate an authentication token for a user
|
||||
* @return string token
|
||||
*/
|
||||
public static function generateToken($user)
|
||||
{
|
||||
|
||||
$token = bin2hex(openssl_random_pseudo_bytes(64));
|
||||
|
||||
$conn = new SqlConnection();
|
||||
|
||||
$stmt_update = $conn->prepare("UPDATE mar_user SET authToken=?, tokenTime=NOW() WHERE username=?");
|
||||
$stmt_update->bindValue(1, $token);
|
||||
$stmt_update->bindValue(2, $user);
|
||||
$stmt_update->execute();
|
||||
|
||||
return $token;
|
||||
|
||||
}
|
||||
|
||||
public static function generateEmptyToken()
|
||||
{
|
||||
return "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
|
||||
}
|
||||
}
|
||||
26
include/User.php
Normal file
26
include/User.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
class User implements JsonSerializable
|
||||
{
|
||||
public $username;
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* User constructor.
|
||||
* @param $username
|
||||
*/
|
||||
public function __construct($username)
|
||||
{
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
"username" => $this->username
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
77
include/UserManager.php
Normal file
77
include/UserManager.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
include_once "SqlConnection.php";
|
||||
include_once "User.php";
|
||||
|
||||
|
||||
class UserManager
|
||||
{
|
||||
/**
|
||||
* Authenticate a user
|
||||
* @param $username string username
|
||||
* @param $password string plain text password
|
||||
*
|
||||
* @return User if sucess, NULL otherwise
|
||||
*/
|
||||
public static function auth($username, $password)
|
||||
{
|
||||
|
||||
$conn = new SqlConnection();
|
||||
|
||||
$stmt_select = $conn->prepare("SELECT username, password FROM mar_user WHERE username=?");
|
||||
$stmt_select->bindValue(1, $username);
|
||||
$stmt_select->execute();
|
||||
|
||||
$dbUser = $stmt_select->fetchObject();
|
||||
|
||||
if ($dbUser) {
|
||||
//Existing user
|
||||
if (password_verify($password, $dbUser->password)) {
|
||||
|
||||
return new User($dbUser->username);
|
||||
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} else {
|
||||
//Unknown user
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an user
|
||||
* @param $username string
|
||||
* @param $password string
|
||||
* @return bool sucess
|
||||
*/
|
||||
public static function register($username, $password)
|
||||
{
|
||||
$conn = new SqlConnection();
|
||||
|
||||
$stmt_select = $conn->prepare("SELECT username FROM mar_user WHERE username=?");
|
||||
$stmt_select->bindValue(1, $username);
|
||||
$stmt_select->execute();
|
||||
|
||||
$bdUser = $stmt_select->fetchObject();
|
||||
|
||||
if ($bdUser) {
|
||||
//User already exists
|
||||
return FALSE;
|
||||
} else {
|
||||
|
||||
$stmt_insert = $conn->prepare("INSERT INTO mar_user (username, password) VALUES (?, ?)");
|
||||
|
||||
$stmt_insert->bindValue(1, $username);
|
||||
$stmt_insert->bindValue(2, password_hash($password, PASSWORD_DEFAULT));
|
||||
$stmt_insert->execute();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
15
include/config.php
Normal file
15
include/config.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
define("SQL_HOST", "mysql:host=localhost;dbname=mar;charset=utf8");
|
||||
define("SQL_USER", "mar");
|
||||
define("SQL_PASS", "mar");
|
||||
|
||||
define("SESSION_NAME", "marSession");
|
||||
|
||||
define("MAR_ADDRESS", "ws://localhost:8887");
|
||||
define("MAR_TICK_LENGTH", 1000);
|
||||
define("MAR_SERVER_NAME", "Official MAR server");
|
||||
|
||||
//Plugins
|
||||
//Version
|
||||
//Server name
|
||||
Reference in New Issue
Block a user