friendship ended with social-app. php is my new best friend

add mariadb/mysql config

Changed files
+111 -1
lib
+2 -1
lib/bskyToucher.php
···
require_once('config.php');
require_once('vendor/autoload.php');
require_once('bskyProvider.php');
-
require_once('db.php');
+
//if (DB_TYPE === 'sqlite') require_once('db.php');
+
if (DB_TYPE === 'mysql') require_once('maria-db.php');
use Matrix\Async;
use React\Http\Browser;
+109
lib/maria-db.php
···
+
<?php
+
require_once('config.php');
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->set_charset('utf8mb4');
+
print_r($db->character_set_name());
+
+
// check for existence of db
+
$db->real_query('create database if not exists '.DB_NAME.';');
+
// select db
+
$db->real_query('use '.DB_NAME.';');
+
// create tables if they don't exist
+
$db->real_query('create table if not exists user_cache (did varchar(64) not null unique primary key, handle varchar(255) not null unique, display_name text, description text, avatar text, banner text, pds text not null, pinned_post text, expires int not null);');
+
$db->real_query('create table if not exists minidoc_cache (did varchar(64) not null primary key, handle varchar(255) not null unique, pds text not null, expires int not null);');
+
$db->real_query('create table if not exists post_cache (rkey varchar(64) not null primary key, did varchar(64) not null, text text, embed_type varchar(32), embed_data text, created_at datetime, expires int not null);');
+
$db->real_query('create table if not exists plc_cache (did varchar(64) not null primary key, plcdoc text, expires int not null);');
+
$db->real_query('create table if not exists feed_cache (at_uri varchar(255) not null primary key, title varchar(255) not null, description text, avatar text, creator_did varchar(64) not null, expires int not null);');
+
$db->real_query('create table if not exists pds_bans (domain varchar(255) not null primary key, reason text);');
+
$db->real_query('create table if not exists client_suspensions (id int not null auto_increment primary key, did varchar(64) not null, reason text, expires int not null);');
+
$db->real_query('create table if not exists bans (did varchar(64) not null primary key, reason text);');
+
// ok! we're good let's go
+
$db->close();
+
+
function deleteExpired(string $table): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$current_time = strtotime('now');
+
$db->real_query('delete from '.$table.' where expires < '.$current_time.';');
+
$db->close();
+
}
+
+
function requestUserCache(string $value, string $field = 'handle'): ?object {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$result = $db->query('select * from user_cache where '.$field.'="'.$db->real_escape_string($value).'";');
+
return $result->fetch_object();
+
$db->close();
+
}
+
+
function requestMinidocCache(string $value, string $field = 'handle'): ?object {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$result = $db->query('select * from minidoc_cache where '.$field.'="'.$db->real_escape_string($value).'";');
+
return $result->fetch_object();
+
$db->close();
+
}
+
+
function requestPostCache(string $rkey): ?object {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$result = $db->query('select * from post_cache where rkey="'.$db->real_escape_string($rkey).'";');
+
return $result->fetch_object();
+
$db->close();
+
}
+
+
function requestFeedCache(string $at_uri): ?object {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$result = $db->query('select * from feed_cache where at_uri="'.$db->real_escape_string($at_uri).'";');
+
return $result->fetch_object();
+
$db->close();
+
}
+
+
function updateUserCache(string $handle, string $did, ?string $display_name, string $pds, ?string $avatar, ?string $banner, ?string $description, ?string $pinned): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$expires = strtotime('now') + 60*60;
+
if (requestUserCache($did, 'did')) {
+
$db->query('update user_cache set handle="'.$db->real_escape_string($handle).'", display_name="'.$db->real_escape_string($display_name).'", description="'.$db->real_escape_string($description).'", avatar="'.$db->real_escape_string($avatar).'", banner="'.$db->real_escape_string($banner).'", pds="'.$db->real_escape_string($pds).'", pinned_post="'.$db->real_escape_string($pinned).'", expires='.$expires);
+
}
+
$db->query('insert into user_cache (did, handle, display_name, description, avatar, banner, pds, pinned_post, expires) values("'.$db->real_escape_string($did).'", "'.$db->real_escape_string($handle).'", "'.$db->real_escape_string($display_name).'", "'.$db->real_escape_string($description).'", "'.$db->real_escape_string($avatar).'", "'.$db->real_escape_string($banner).'", "'.$db->real_escape_string($pds).'", "'.$db->real_escape_string($pinned).'", '.$expires.');');
+
$db->close();
+
}
+
+
function updateMinidocCache(string $handle, string $did, string $pds, string $signingKey): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$db->close();
+
+
}
+
+
function updatePlcCache(string $did, string $plcdoc): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$db->close();
+
}
+
+
function updatefeedCache(string $atUri, string $title, ?string $description, ?string $avatar, string $creator_did): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$db->close();
+
}
+
+
function updatePostCache(string $rkey, string $did, string $text, ?string $embedType, ?string $embedData, string $createdAt): void {
+
$db = mysqli_init();
+
$db->real_connect(DB_HOST, DB_USERNAME, DB_PASS);
+
$db->real_query('use '.DB_NAME.';');
+
$db->close();
+
}
+
?>