migrate to postgres

add getID3 as a submodule
This commit is contained in:
jkhsjdhjs 2019-09-18 20:08:06 +00:00
parent 2e3a7a1701
commit 2e9d7cca51
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
4 changed files with 70 additions and 54 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "getID3"]
path = getID3
url = https://github.com/JamesHeinrich/getID3

111
bot.php
View File

@ -4,17 +4,17 @@
private $token = "";
private $webhook = "";
private $validIDs = null;
private $mysql_user = null;
private $mysql_pw = null;
private $mysql_db = null;
private $psql_user = null;
private $psql_pw = null;
private $psql_db = null;
public function __construct($token, $webhook = "", $validIDs = null, $mysqlUser = null, $mysqlPw = null, $mysqlDb = null) {
public function __construct($token, $webhook = "", $validIDs = null, $psqlUser = null, $psqlPw = null, $psqlDb = null) {
$this->setToken($token);
$this->setWebhookURL($webhook);
$this->validIDs = $validIDs;
$this->mysql_user = $mysqlUser;
$this->mysql_pw = $mysqlPw;
$this->mysql_db = $mysqlDb;
$this->psql_user = $psqlUser;
$this->psql_pw = $psqlPw;
$this->psql_db = $psqlDb;
}
public function setToken($token) {
@ -26,48 +26,54 @@
}
public function setWebhook() {
$this->curlRequest("setWebhook", array("url" => $this->webhook));
$this->curlRequest("setWebhook", ["url" => $this->webhook]);
}
public function deleteWebhook() {
$this->curlRequest("setWebhook", array("url" => ""));
$this->curlRequest("setWebhook", ["url" => ""]);
}
public function addID($id) {
array_push($this->validIDs, $id);
}
public function setMysqlUser($user) {
$this->mysql_user = $user;
public function setPsqlUser($user) {
$this->psql_user = $user;
}
public function setMysqlPw($pw) {
$this->mysql_pw = $pw;
public function setPsqlPw($pw) {
$this->psql_pw = $pw;
}
public function setMysqlDb($db) {
$this->mysql_db = $db;
public function setPsqlDb($db) {
$this->psql_db = $db;
}
private function mysql_connect() {
return new mysqli("localhost", $this->mysql_user, $this->mysql_pw, $this->mysql_db);
private function psql_connect() {
$conn_string = "";
if($this->psql_user !== null)
$conn_string .= "user=$this->psql_user ";
if($this->psql_pw !== null)
$conn_string .= "password=$this->psql_pw ";
if($this->psql_db !== null)
$conn_string .= "dbname= $this->psql_db ";
return pg_connect($conn_string);
}
private function mysql_check_file($filename) {
$link = $this->mysql_connect();
$res = $link->query("SELECT file_id FROM files WHERE file = '$filename' LIMIT 1");
$link->close();
if($res->num_rows == 0) {
private function psql_check_file($filename) {
$link = $this->psql_connect();
$res = pg_query_params($link, "SELECT file_id FROM files WHERE file = $1 LIMIT 1", [$filename]);
pg_close($link);
if(pg_num_rows($res) === 0)
return false;
}
$res = $res->fetch_object();
$res = pg_fetch_object($res);
return $res->file_id;
}
private function mysql_add_file($filename, $file_id) {
$link = $this->mysql_connect();
$link->query("INSERT INTO files (file, file_id) VALUES ('$filename', '$file_id')");
$link->close();
private function psql_add_file($filename, $file_id) {
$link = $this->psql_connect();
pg_query_params($link, "INSERT INTO files (file, file_id) VALUES ($1, $2)", [$filename, $file_id]);
pg_close($link);
}
public function checkID($id) {
@ -87,8 +93,8 @@
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
if(!$parameters) {
$parameters = [];
}
else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
@ -97,17 +103,17 @@
$parameters["method"] = $method;
$options = array(
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_HTTPHEADER => array("Content-Type: application/json")
);
CURLOPT_HTTPHEADER => ["Content-Type: application/json"]
];
$ch = curl_init(Bot::api . $this->token . "/");
curl_setopt_array($ch, $options);
curl_exec($ch);
var_dump(curl_exec($ch));
curl_close($ch);
}
@ -116,7 +122,7 @@
return false;
}
$param = strtolower(str_replace("send", "", $method));
$rv = $this->mysql_check_file($file);
$rv = $this->psql_check_file($file);
if($rv !== false) {
$parameters[$param] = $rv;
$this->curlRequest($method, $parameters);
@ -124,11 +130,11 @@
else {
$parameters[$param] = new CURLFile(realpath("files/" . $file));
$parameters["method"] = $method;
$options = array(
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $parameters,
CURLOPT_HTTPHEADER => array("Content-Type: multipart/form-data")
);
CURLOPT_HTTPHEADER => ["Content-Type: multipart/form-data"]
];
$ch = curl_init(Bot::api . $this->token . "/");
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
@ -142,7 +148,7 @@
else {
$file_id = end($file_id)["file_id"];
}
$this->mysql_add_file($file, $file_id);
$this->psql_add_file($file, $file_id);
}
else {
return false;
@ -154,10 +160,10 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($text))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id,
"text" => $text
);
];
if(is_integer($reply_to_message_id)) {
$parameters["reply_to_message_id"] = $reply_to_message_id;
}
@ -180,9 +186,9 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($photo))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id
);
];
if(is_integer($reply_to_message_id)) {
$parameters["reply_to_message_id"] = $reply_to_message_id;
}
@ -202,9 +208,9 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($document))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id
);
];
if(is_integer($reply_to_message_id)) {
$parameters["reply_to_message_id"] = $reply_to_message_id;
}
@ -224,9 +230,9 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($sticker))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id
);
];
if(is_integer($reply_to_message_id)) {
$parameters["reply_to_message_id"] = $reply_to_message_id;
}
@ -243,9 +249,9 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($video))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id
);
];
if(is_string($caption)) {
$parameters["caption"] = $caption;
}
@ -258,7 +264,7 @@
if(is_object($reply_markup)) {
$parameters["reply_markup"] = $reply_markup;
}
require_once("/var/www/php/getID3/getid3/getid3.php");
require_once "getID3/getid3/getid3.php";
$getID3 = new getID3();
$info = $getID3->analyze("files/" . $video);
if(isset($info["playtime_seconds"])) {
@ -276,12 +282,11 @@
if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($action))) {
return false;
}
$parameters = array(
$parameters = [
"chat_id" => $chat_id,
"action" => $action
);
];
$this->curlRequest("sendChatAction", $parameters);
}
}
?>

1
getID3 Submodule

@ -0,0 +1 @@
Subproject commit 0723b77cafe9278618cfb6bc91b75e73705d3de8

7
schema.sql Normal file
View File

@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS "files" (
"id" serial PRIMARY KEY,
"file" text NOT NULL,
"file_id" text NOT NULL
);
CREATE INDEX IF NOT EXISTS files_file ON files (file);