From 6aef81f5762fde64d3dbc1d3571a60606bb3aa95 Mon Sep 17 00:00:00 2001 From: CM <5914364+ssep1ol@users.noreply.github.com> Date: Fri, 23 Apr 2021 01:45:17 +0200 Subject: [PATCH] New command and bugfixes New function /delshorten added, some minor bugfixes on other commands (see https://areait.runpolito.it/bugtracker/view.php?id=2 ) --- Commands/DelshortenCommand.php | 149 +++++++++++++++++++++++++++++++++ Commands/ShortenCommand.php | 2 +- Commands/UploadCommand.php | 2 +- 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Commands/DelshortenCommand.php diff --git a/Commands/DelshortenCommand.php b/Commands/DelshortenCommand.php new file mode 100644 index 0000000..9cf7271 --- /dev/null +++ b/Commands/DelshortenCommand.php @@ -0,0 +1,149 @@ +'; + + /** + * @var string + */ + protected $version = '1.2.0'; + + /** + * Main command execution + * + * @return ServerResponse + * @throws TelegramException + */ + public function execute(): ServerResponse + { + $config = require __DIR__ . '/../config.php'; + $auth = $config['authids']; + + $message = $this->getMessage(); + $from = $message->getFrom(); + $user_id = $from->getId(); + $chat_id = $message->getChat()->getId(); + if(!in_array($chat_id, $auth)) + { + $data_tlg = [ + 'chat_id' => $chat_id, + 'text' => "Non autorizzato", + ]; + + return Request::sendMessage($data_tlg); + die("Unauthorised"); + } + $text = trim($message->getText(true)); + if ($text === 'help') { + $replytext = 'Command usage: ' . $this->getUsage(); + } + else + { + if($text != NULL) + { + $url = strtolower($text); + $result = json_decode($this->delshorten($url, $user_id), true); + + switch ($result['message']) { + case "success: deleted": + $replytext = "Shortened link ".$url." succesfully deleted."; + break; + case "error: not found": + $replytext = "Shortened link ".$url." doesn't exist: maybe it has already been deleted?"; + break; + + default: + $replytext = "Whoops! An error occurred, ".$url." not deleted. Contact IT support (".$config['itsupport']."). Diag: ".$result['message']; + break; + } + + } + else + { + $replytext = "Devi inserire un link accorciato da eliminare /shorten . Scrivi /shorten help per ulteriori info"; + } + } + $data_tlg = [ + 'chat_id' => $chat_id, + 'text' => $replytext, + ]; + + return Request::sendMessage($data_tlg); + + } + + + private function delshorten($url, $userid) + { + $config = require __DIR__ . '/../config.php'; + $yls = $config['yourls']; + + $timestamp = time(); + $signature = md5( $timestamp . $yls['token'] ); + + $format = 'json'; // output format: 'json', 'xml' or 'simple' + + $api_url = $yls['apiurl']; + + // Init the CURL session + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $api_url ); + curl_setopt( $ch, CURLOPT_HEADER, 0 ); // No header in the result + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result + curl_setopt( $ch, CURLOPT_POST, 1 ); // This is a POST request + curl_setopt( $ch, CURLOPT_POSTFIELDS, array( // Data to POST + 'shorturl' => $url, + 'format' => $format, + 'action' => 'delete', + 'timestamp' => $timestamp, + 'signature' => $signature + ) ); + + // Fetch and return content + $data = curl_exec($ch); + curl_close($ch); + + $jsr = json_decode($data, true); + if($jsr['statusCode'] == "200") + { + $rundb = $config['rundb']; + $mysqli = new \mysqli($rundb['host'], $rundb['user'], $rundb['password'], $rundb['database']); + $query = $mysqli->prepare("DELETE FROM `shortener` WHERE shorturl = ?"); + $query->bind_param('s',$url); + $query->execute(); + } + + return $data; + } +} \ No newline at end of file diff --git a/Commands/ShortenCommand.php b/Commands/ShortenCommand.php index 35a4a98..bb88fd4 100644 --- a/Commands/ShortenCommand.php +++ b/Commands/ShortenCommand.php @@ -74,7 +74,7 @@ class ShortenCommand extends UserCommand if($reply != NULL) { $replytext1 = $reply->getText(); - $result = json_decode($this->shorten($replytext1, $text, $user_id), true); + $result = json_decode($this->shorten($replytext1, strtolower($text), $user_id), true); $replytext = $result['message']."\n\nShortened link: ".$result['shorturl']."\n\nQR Code: ".$result['shorturl'].".qr\n\nStats: ".$result['shorturl']."+"; } diff --git a/Commands/UploadCommand.php b/Commands/UploadCommand.php index b51a3c5..2e6552c 100644 --- a/Commands/UploadCommand.php +++ b/Commands/UploadCommand.php @@ -129,7 +129,7 @@ class UploadCommand extends UserCommand $newfilename = strtoupper(uniqid()) . '.' . end($temp); if(rename($tempfile, $path."/".$newfilename)) { - // if($clamav->scan("/ssda1/www/portal.runpolito.it/lib/tmp/".$newfilename)) + // if($clamav->scan("".$newfilename)) // { $mime = mime_content_type($path."/".$newfilename); $extp = explode("/", $mime);