Added possibility for profile and forum shortlinks.

This commit is contained in:
flash 2023-01-19 18:07:51 +00:00
parent cdd879093d
commit d96209a059
4 changed files with 59 additions and 13 deletions

View file

@ -20,7 +20,7 @@ require_once AWK_DIR_LIB . '/index/index.php';
Autoloader::addNamespace(__NAMESPACE__, AWK_DIR_SRC);
Environment::setDebug(AWK_DEBUG);
$config = parse_ini_file(AWK_DIR_CFG . '/config.ini');
$config = parse_ini_file(AWK_DIR_CFG . '/config.ini', true);
if($config === false)
die('Config sux.');
@ -31,4 +31,4 @@ try {
die($ex->getMessage());
}
$awk = new AwakiContext($db);
$awk = new AwakiContext($db, $config['urls']);

@ -1 +1 @@
Subproject commit 30064e6891d3d06abe0174b921dc505ff74f4300
Subproject commit 99a4e0c3027a7b29c58e0e0164cb831a03461527

View file

@ -17,9 +17,11 @@ class AwakiContext {
private IDbConnection $dbConn;
private HttpFx $router;
private array $urls;
public function __construct(IDbConnection $dbConn) {
public function __construct(IDbConnection $dbConn, array $urls) {
$this->dbConn = $dbConn;
$this->urls = $urls;
$dbConn->execute(self::DB_INIT);
}
@ -66,6 +68,6 @@ class AwakiContext {
}
private function registerHttpRoutes(): void {
new RedirectorRoutes($this->router, $this->dbConn);
new RedirectorRoutes($this->router, $this->dbConn, $this->urls);
}
}

View file

@ -7,12 +7,30 @@ use Index\Routing\IRouter;
final class RedirectorRoutes {
private IDbConnection $dbConn;
private array $urls;
public function __construct(IRouter $router, IDbConnection $dbConn) {
public function __construct(IRouter $router, IDbConnection $dbConn, array $urls) {
$this->dbConn = $dbConn;
$this->urls = $urls;
$router->get('/', [$this, 'index']);
$router->get('/:id', [$this, 'redirect']);
// databased
$router->get('/:id', [$this, 'redirectDatabase']);
// profile
$router->get('/u/:user', [$this, 'redirectProfile']);
$router->get('/p/:user', [$this, 'redirectProfile']);
// forum categories
$router->get('/f/:category', [$this, 'redirectForumCategory']);
$router->get('/fc/:category', [$this, 'redirectForumCategory']);
// forum topic
$router->get('/ft/:topic', [$this, 'redirectForumTopic']);
// forum post
$router->get('/fp/:post', [$this, 'redirectForumPost']);
}
public function index($response): void {
@ -20,7 +38,15 @@ final class RedirectorRoutes {
$response->setTypeHTML();
}
public function redirect($response, $request, $id) {
private function redirect($response, $request, $url) {
$params = $request->getParamString();
if(!empty($params))
$url .= (strpos($url, '?') === false ? '?' : '&') . $params;
$response->redirect($url, true);
}
public function redirectDatabase($response, $request, $id) {
$getInfo = $this->dbConn->prepare('SELECT redir_url FROM awk_redirects WHERE redir_id = ? OR redir_vanity = ?');
$getInfo->addParameter(1, $id, DbType::INTEGER);
$getInfo->addParameter(2, $id, DbType::STRING);
@ -30,12 +56,30 @@ final class RedirectorRoutes {
if(!$info->next())
return 404;
$targetUrl = $info->getString(0);
$this->redirect($response, $request, $info->getString(0));
}
$params = $request->getParamString();
if(!empty($params))
$targetUrl .= '?' . $params;
private function redirectSimple($response, $request, string $format, string $argument) {
$scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https';
$argument = rawurlencode($argument);
$url = sprintf($format, $scheme, $argument);
$response->redirect($targetUrl);
$this->redirect($response, $request, $url);
}
public function redirectProfile($response, $request, string $userId) {
$this->redirectSimple($response, $request, $this->urls['user_profile'], $userId);
}
public function redirectForumCategory($response, $request, string $categoryId) {
$this->redirectSimple($response, $request, $this->urls['forum_category'], $categoryId);
}
public function redirectForumTopic($response, $request, string $topicId) {
$this->redirectSimple($response, $request, $this->urls['forum_topic'], $topicId);
}
public function redirectForumPost($response, $request, string $postId) {
$this->redirectSimple($response, $request, $this->urls['forum_post'], $postId);
}
}