mirror of
https://codeberg.org/vlw/stadia-avatar.git
synced 2025-09-14 07:23:41 +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 {
|
class DBConnector extends mysqli {
|
||||||
|
|
||||||
public static $config = [
|
protected static $config = [
|
||||||
"host" => "",
|
"host" => "",
|
||||||
"username" => "",
|
"username" => "",
|
||||||
"password" => "",
|
"password" => "",
|
||||||
"database" => "stadia_avatars"
|
"database" => "stadia_avatars"
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct() {
|
protected static $instance;
|
||||||
parent::init();
|
|
||||||
|
|
||||||
if(!parent::options(MYSQLI_INIT_COMMAND,"SET AUTOCOMMIT = 0")) {
|
private function __construct() {
|
||||||
die("Setting MYSQLI_INIT_COMMAND failed");
|
$config = self::$config;
|
||||||
}
|
|
||||||
|
|
||||||
if(!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT,5)) {
|
@parent::__construct($config["host"],$config["username"],$config["password"],$config["database"]);
|
||||||
die("Setting MYSQLI_OPT_CONNECT_TIMEOUT failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!parent::real_connect(DBConnector::$config["host"],DBConnector::$config["username"],DBConnector::$config["password"],DBConnector::$config["database"])) {
|
if(mysqli_connect_error()) {
|
||||||
die("Connect Error (".mysqli_connect_errno().") ".mysqli_connect_error());
|
throw new Exception(mysqli_connect_error(),mysqli_connect_errno());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function check_connection() {
|
public static function getInstance() {
|
||||||
if(parent::connect_errno) {
|
if(!self::$instance) {
|
||||||
die("Invalid connection");
|
self::$instance = new self();
|
||||||
}
|
}
|
||||||
|
return self::$instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function insert_avatar($user_id,$value) {
|
public function exec_query($query) {
|
||||||
$time = time();
|
if(!$this->real_query($query)) {
|
||||||
$query = "INSERT INTO avatars (userid, avatar, modified) VALUES ('${user_id}', '${value}', '${time}');";
|
throw new Exception($this->error,$this->errno);
|
||||||
|
|
||||||
if($result = parent::query($query) === true) {
|
|
||||||
http_response_code("206");
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
$result = new mysqli_result($this);
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update_avatar($user_id,$value) {
|
public function prepare($query) {
|
||||||
$time = time();
|
$stmt = new mysqli_stmt($this,$query);
|
||||||
$query = "UPDATE avatars SET avatar = '${value}', modified = '${time}' WHERE avatars.userid = '${user_id}';";
|
return $stmt;
|
||||||
|
}
|
||||||
if($result = parent::query($query) === true) {
|
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
class StadiaAvatarDB extends DBConnector {
|
||||||
return false;
|
|
||||||
}
|
public function __construct() {
|
||||||
|
$this->sql = DBConnector::getInstance();
|
||||||
// ----
|
}
|
||||||
|
|
||||||
public function get_avatar($user_id) {
|
private function insert_avatar($user_id,$avatar) {
|
||||||
$query = "SELECT userid, avatar FROM avatars WHERE userid = '${user_id}';";
|
$time = time();
|
||||||
|
|
||||||
if($result = parent::query($query)) {
|
$SQL = "INSERT INTO `avatars` (`userid`, `avatar`, `modified`) VALUES ('${user_id}', '${avatar}', '${time}');";
|
||||||
return $result->fetch_array(MYSQLI_NUM)[1];
|
return $this->sql->exec_query($SQL);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
private function update_avatar($user_id,$avatar) {
|
||||||
}
|
$time = time();
|
||||||
|
|
||||||
public function set_avatar($user_id,$value) {
|
$SQL = "UPDATE `avatars` SET `avatar` = '${avatar}', `modified` = '${time}' WHERE `avatars`.`userid` = '${user_id}';";
|
||||||
if($this->get_avatar($user_id)) {
|
return $this->sql->exec_query($SQL);
|
||||||
return $this->update_avatar($user_id,$value);
|
}
|
||||||
}
|
|
||||||
|
public function get_avatar($user_id) {
|
||||||
return $this->insert_avatar($user_id,$value);
|
$SQL = "SELECT `userid`, `avatar` FROM `avatars` WHERE `userid` = '${user_id}'";
|
||||||
|
$query = $this->sql->exec_query($SQL);
|
||||||
|
|
||||||
|
while($row = $query->fetch_assoc()) {
|
||||||
|
return $row["avatar"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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->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");
|
$user_id = $_GET["userID"] ?? error("400","No userID provided");
|
||||||
|
|
||||||
$db = new DBConnector();
|
$db = new StadiaAvatarDB();
|
||||||
$avatar = $db->get_avatar($user_id);
|
$avatar = $db->get_avatar($user_id);
|
||||||
|
|
||||||
if($avatar) {
|
if(!$avatar) {
|
||||||
echo "{\"status\":\"OK\",\"avatar\":\"${avatar}\"}";
|
error("404","No avatar was found for the supplied userID");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error("404","No avatar was found for the supplied userID");
|
echo "{\"status\":\"OK\",\"avatar\":\"${avatar}\"}";
|
|
@ -1,17 +1,21 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require "../classes/Message.php";
|
require "../classes/Message.php";
|
||||||
|
require "../classes/Gravatar.php";
|
||||||
require "../classes/Database.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.");
|
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.");
|
error("500","Something went wrong.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue