mirror of
https://codeberg.org/vlw/stadia-avatar.git
synced 2025-09-13 15:23:40 +02:00
Added StadiaAvatarDB and created Gravatar
Added new extension of DBConnector called StadiaAvatarDB. This class contains all avatar manipulation queries. Fixed get and update. Added Gravatar hash generator.
This commit is contained in:
parent
ef58aa530e
commit
b586850c26
4 changed files with 77 additions and 57 deletions
|
@ -2,76 +2,84 @@
|
|||
|
||||
class DBConnector extends mysqli {
|
||||
|
||||
public static $config = [
|
||||
protected static $config = [
|
||||
"host" => "",
|
||||
"username" => "",
|
||||
"password" => "",
|
||||
"database" => "stadia_avatars"
|
||||
];
|
||||
|
||||
protected static $instance;
|
||||
|
||||
private function __construct() {
|
||||
$config = self::$config;
|
||||
|
||||
@parent::__construct($config["host"],$config["username"],$config["password"],$config["database"]);
|
||||
|
||||
if(mysqli_connect_error()) {
|
||||
throw new Exception(mysqli_connect_error(),mysqli_connect_errno());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if(!self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function exec_query($query) {
|
||||
if(!$this->real_query($query)) {
|
||||
throw new Exception($this->error,$this->errno);
|
||||
}
|
||||
|
||||
$result = new mysqli_result($this);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function prepare($query) {
|
||||
$stmt = new mysqli_stmt($this,$query);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class StadiaAvatarDB extends DBConnector {
|
||||
|
||||
public function __construct() {
|
||||
parent::init();
|
||||
|
||||
if(!parent::options(MYSQLI_INIT_COMMAND,"SET AUTOCOMMIT = 0")) {
|
||||
die("Setting MYSQLI_INIT_COMMAND failed");
|
||||
}
|
||||
|
||||
if(!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT,5)) {
|
||||
die("Setting MYSQLI_OPT_CONNECT_TIMEOUT failed");
|
||||
}
|
||||
|
||||
if(!parent::real_connect(DBConnector::$config["host"],DBConnector::$config["username"],DBConnector::$config["password"],DBConnector::$config["database"])) {
|
||||
die("Connect Error (".mysqli_connect_errno().") ".mysqli_connect_error());
|
||||
}
|
||||
$this->sql = DBConnector::getInstance();
|
||||
}
|
||||
|
||||
private function check_connection() {
|
||||
if(parent::connect_errno) {
|
||||
die("Invalid connection");
|
||||
}
|
||||
}
|
||||
|
||||
private function insert_avatar($user_id,$value) {
|
||||
private function insert_avatar($user_id,$avatar) {
|
||||
$time = time();
|
||||
$query = "INSERT INTO avatars (userid, avatar, modified) VALUES ('${user_id}', '${value}', '${time}');";
|
||||
|
||||
if($result = parent::query($query) === true) {
|
||||
http_response_code("206");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$SQL = "INSERT INTO `avatars` (`userid`, `avatar`, `modified`) VALUES ('${user_id}', '${avatar}', '${time}');";
|
||||
return $this->sql->exec_query($SQL);
|
||||
}
|
||||
|
||||
private function update_avatar($user_id,$value) {
|
||||
private function update_avatar($user_id,$avatar) {
|
||||
$time = time();
|
||||
$query = "UPDATE avatars SET avatar = '${value}', modified = '${time}' WHERE avatars.userid = '${user_id}';";
|
||||
|
||||
if($result = parent::query($query) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$SQL = "UPDATE `avatars` SET `avatar` = '${avatar}', `modified` = '${time}' WHERE `avatars`.`userid` = '${user_id}';";
|
||||
return $this->sql->exec_query($SQL);
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
public function get_avatar($user_id) {
|
||||
$query = "SELECT userid, avatar FROM avatars WHERE userid = '${user_id}';";
|
||||
$SQL = "SELECT `userid`, `avatar` FROM `avatars` WHERE `userid` = '${user_id}'";
|
||||
$query = $this->sql->exec_query($SQL);
|
||||
|
||||
if($result = parent::query($query)) {
|
||||
return $result->fetch_array(MYSQLI_NUM)[1];
|
||||
while($row = $query->fetch_assoc()) {
|
||||
return $row["avatar"];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function set_avatar($user_id,$value) {
|
||||
if($this->get_avatar($user_id)) {
|
||||
return $this->update_avatar($user_id,$value);
|
||||
public function set_avatar($user_id,$avatar) {
|
||||
if(!$this->get_avatar($user_id)) {
|
||||
http_response_code("201");
|
||||
return $this->insert_avatar($user_id,$avatar);
|
||||
}
|
||||
|
||||
return $this->insert_avatar($user_id,$value);
|
||||
return $this->update_avatar($user_id,$avatar);
|
||||
}
|
||||
|
||||
}
|
9
classes/Gravatar.php
Normal file
9
classes/Gravatar.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
class Gravatar {
|
||||
|
||||
public function __construct($email) {
|
||||
$this->hash = md5(strtolower(trim($email)));
|
||||
}
|
||||
|
||||
}
|
|
@ -5,12 +5,11 @@
|
|||
|
||||
$user_id = $_GET["userID"] ?? error("400","No userID provided");
|
||||
|
||||
$db = new DBConnector();
|
||||
$db = new StadiaAvatarDB();
|
||||
$avatar = $db->get_avatar($user_id);
|
||||
|
||||
if($avatar) {
|
||||
echo "{\"status\":\"OK\",\"avatar\":\"${avatar}\"}";
|
||||
return;
|
||||
if(!$avatar) {
|
||||
error("404","No avatar was found for the supplied userID");
|
||||
}
|
||||
|
||||
error("404","No avatar was found for the supplied userID");
|
||||
|
||||
echo "{\"status\":\"OK\",\"avatar\":\"${avatar}\"}";
|
|
@ -1,18 +1,22 @@
|
|||
<?php
|
||||
|
||||
require "../classes/Message.php";
|
||||
require "../classes/Gravatar.php";
|
||||
require "../classes/Database.php";
|
||||
|
||||
$sharedSecret = ""; // Key to update the database
|
||||
$shared_secret = ""; // Key to update the database
|
||||
|
||||
if($sharedSecret !== "") {
|
||||
$request = json_decode(file_get_contents("php://input")) ?? error("400","Bad request");
|
||||
|
||||
if(!$request->sharedSecret || $request->sharedSecret !== $shared_secret) {
|
||||
error("403","Invalid shared secret.");
|
||||
}
|
||||
|
||||
$db = new DBConnector();
|
||||
$db = new StadiaAvatarDB();
|
||||
$gravatar = new Gravatar($request->payload);
|
||||
|
||||
if(!$db->set_avatar("foo","bario")) {
|
||||
if(!$db->set_avatar($request->userID,$gravatar->hash)) {
|
||||
error("500","Something went wrong.");
|
||||
}
|
||||
|
||||
|
||||
echo "{\"status\":\"OK\"}";
|
Loading…
Add table
Reference in a new issue