Added Satori redirects table and read endpoint.

This commit is contained in:
flash 2024-05-22 20:51:44 +00:00
parent 0ae839b26a
commit 627c28bd4d
2 changed files with 34 additions and 0 deletions

View file

@ -0,0 +1,16 @@
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class AddSatoriRedirsTable_20240522_204323 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->execute('
CREATE TABLE awk_satori_redirects (
redir_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
redir_url VARCHAR(255) NOT NULL,
redir_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (redir_id)
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
}
}

View file

@ -3,6 +3,7 @@ namespace Awaki;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Http\Routing\IRouter; use Index\Http\Routing\IRouter;
use Index\Serialisation\Base62;
use Syokuhou\IConfig; use Syokuhou\IConfig;
final class RedirectorRoutes { final class RedirectorRoutes {
@ -19,6 +20,9 @@ final class RedirectorRoutes {
$router->get('/[up]([0-9]+)', $this->redirectProfile(...)); $router->get('/[up]([0-9]+)', $this->redirectProfile(...));
$router->get('/[up]/([A-Za-z0-9\-_]+)', $this->redirectProfile(...)); $router->get('/[up]/([A-Za-z0-9\-_]+)', $this->redirectProfile(...));
// satori short urls
$router->get('/[bg]/([A-Za-z0-9]+)', $this->redirectSatoriShort(...));
// forum categories // forum categories
$router->get('/fc?/?([0-9]+)', $this->redirectForumCategory(...)); $router->get('/fc?/?([0-9]+)', $this->redirectForumCategory(...));
@ -62,6 +66,20 @@ final class RedirectorRoutes {
$this->redirect($response, $request, $info->getString(1)); $this->redirect($response, $request, $info->getString(1));
} }
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) { private function redirectSimple($response, $request, string $format, string $argument) {
$scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https'; $scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$argument = rawurlencode($argument); $argument = rawurlencode($argument);