From 00f98e463b12faab428261b4336d7f4ae3c37057 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 22 May 2024 21:56:39 +0000 Subject: [PATCH] Added Satori routes to Awaki. --- awaki.php | 2 +- src/AwakiContext.php | 12 +++---- src/RedirectorRoutes.php | 15 --------- src/SatoriRoutes.php | 68 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 src/SatoriRoutes.php diff --git a/awaki.php b/awaki.php index 0ff768a..c783689 100644 --- a/awaki.php +++ b/awaki.php @@ -35,4 +35,4 @@ if($config->hasValues('sentry:dsn')) $db = DbTools::create($config->getString('database:dsn', 'null:')); $db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'); -$awk = new AwakiContext($db, $config->scopeTo('urls')); +$awk = new AwakiContext($db, $config); diff --git a/src/AwakiContext.php b/src/AwakiContext.php index c54848c..5ee2df3 100644 --- a/src/AwakiContext.php +++ b/src/AwakiContext.php @@ -12,13 +12,12 @@ use Syokuhou\IConfig; class AwakiContext { private const DB_INIT = 'SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'; - private IDbConnection $dbConn; private HttpRouter $router; - private IConfig $urls; - public function __construct(IDbConnection $dbConn, IConfig $urls) { - $this->dbConn = $dbConn; - $this->urls = $urls; + public function __construct( + private IDbConnection $dbConn, + private IConfig $config + ) { $dbConn->execute(self::DB_INIT); } @@ -55,6 +54,7 @@ class AwakiContext { } private function registerHttpRoutes(): void { - $this->router->register(new RedirectorRoutes($this->dbConn, $this->urls)); + $this->router->register(new SatoriRoutes($this->dbConn, $this->config->scopeTo('satori'))); + $this->router->register(new RedirectorRoutes($this->dbConn, $this->config->scopeTo('urls'))); } } diff --git a/src/RedirectorRoutes.php b/src/RedirectorRoutes.php index fe6b8a6..d006391 100644 --- a/src/RedirectorRoutes.php +++ b/src/RedirectorRoutes.php @@ -50,21 +50,6 @@ final class RedirectorRoutes implements IRouteHandler { $this->redirect($response, $request, $info->getString(1)); } - #[HttpGet('/[bg]/([A-Za-z0-9]+)')] - public function redirectSatoriShort($response, $request, string $linkId) { - $linkId = Base62::decode($linkId); - - $getInfo = $this->dbConn->prepare('SELECT redir_url FROM awk_satori_redirects WHERE redir_id = ?'); - $getInfo->addParameter(1, $linkId); - $getInfo->execute(); - $info = $getInfo->getResult(); - - if(!$info->next()) - return 404; - - $this->redirect($response, $request, $info->getString(0)); - } - private function redirectSimple($response, $request, string $format, string $argument) { $scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https'; $argument = rawurlencode($argument); diff --git a/src/SatoriRoutes.php b/src/SatoriRoutes.php new file mode 100644 index 0000000..c591989 --- /dev/null +++ b/src/SatoriRoutes.php @@ -0,0 +1,68 @@ +dbConn->prepare('SELECT redir_url FROM awk_satori_redirects WHERE redir_id = ?'); + $getInfo->addParameter(1, $linkId); + $getInfo->execute(); + $info = $getInfo->getResult(); + + if(!$info->next()) + return 404; + + $response->redirect($info->getString(0), true); + } + + #[HttpPost('/satori/create')] + public function createRedirect($response, $request) { + if(!$request->isFormContent()) + return 400; + + $content = $request->getContent(); + + $url = (string)$content->getParam('u'); + $time = (int)$content->getParam('t', FILTER_SANITIZE_NUMBER_INT); + $sign = base64_decode((string)$content->getParam('s')); + $hash = hash_hmac('sha256', "satori#create#{$time}#{$url}", $this->config->getString('secret'), true); + + if(!hash_equals($hash, $sign)) + return 403; + + $currentTime = time(); + if($time < $currentTime - 30 || $time > $currentTime + 30) + return 403; + + $stmt = $this->dbConn->prepare('SELECT redir_id FROM awk_satori_redirects WHERE redir_url = ?'); + $stmt->addParameter(1, $url); + $stmt->execute(); + + $result = $stmt->getResult(); + if($result->next()) { + $linkId = $stmt->getInteger(0); + } else { + $stmt = $this->dbConn->prepare('INSERT INTO awk_satori_redirects (redir_url) VALUES (?)'); + $stmt->addParameter(1, $url); + $stmt->execute(); + + $linkId = (int)$stmt->getLastInsertId(); + } + + return [ + 'url' => sprintf($this->config->getString('format'), Base62::encode($linkId)), + ]; + } +}