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

113
bot.php
View File

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