diff --git a/awaki.php b/awaki.php index eff3393..43e3a7c 100644 --- a/awaki.php +++ b/awaki.php @@ -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']); diff --git a/lib/index b/lib/index index 30064e6..99a4e0c 160000 --- a/lib/index +++ b/lib/index @@ -1 +1 @@ -Subproject commit 30064e6891d3d06abe0174b921dc505ff74f4300 +Subproject commit 99a4e0c3027a7b29c58e0e0164cb831a03461527 diff --git a/src/AwakiContext.php b/src/AwakiContext.php index 25e8a42..9f3dce2 100644 --- a/src/AwakiContext.php +++ b/src/AwakiContext.php @@ -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); } } diff --git a/src/RedirectorRoutes.php b/src/RedirectorRoutes.php index 4e842ce..1baf860 100644 --- a/src/RedirectorRoutes.php +++ b/src/RedirectorRoutes.php @@ -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); } }