From 2e9d7cca519965d726e03c4cb56e75c64423f61a Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Wed, 18 Sep 2019 20:08:06 +0000 Subject: [PATCH] migrate to postgres add getID3 as a submodule --- .gitmodules | 3 ++ bot.php | 113 +++++++++++++++++++++++++++------------------------- getID3 | 1 + schema.sql | 7 ++++ 4 files changed, 70 insertions(+), 54 deletions(-) create mode 100644 .gitmodules create mode 160000 getID3 create mode 100644 schema.sql diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4d18c70 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "getID3"] + path = getID3 + url = https://github.com/JamesHeinrich/getID3 diff --git a/bot.php b/bot.php index 4d6e206..906b6d5 100755 --- a/bot.php +++ b/bot.php @@ -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); } } - -?> \ No newline at end of file +?> diff --git a/getID3 b/getID3 new file mode 160000 index 0000000..0723b77 --- /dev/null +++ b/getID3 @@ -0,0 +1 @@ +Subproject commit 0723b77cafe9278618cfb6bc91b75e73705d3de8 diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..e10e4b8 --- /dev/null +++ b/schema.sql @@ -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);