92 lines
3.3 KiB
PHP
92 lines
3.3 KiB
PHP
<?php
|
|
namespace Awaki;
|
|
|
|
use Index\Data\IDbConnection;
|
|
use Index\Http\Routing\IRouter;
|
|
use Syokuhou\IConfig;
|
|
|
|
final class RedirectorRoutes {
|
|
private IDbConnection $dbConn;
|
|
private IConfig $urls;
|
|
|
|
public function __construct(IRouter $router, IDbConnection $dbConn, IConfig $urls) {
|
|
$this->dbConn = $dbConn;
|
|
$this->urls = $urls;
|
|
|
|
$router->get('/', [$this, 'index']);
|
|
|
|
// profile
|
|
$router->get('/u([0-9]+)', [$this, 'redirectProfile']);
|
|
$router->get('/p([0-9]+)', [$this, 'redirectProfile']);
|
|
$router->get('/u/([A-Za-z0-9\-_]+)', [$this, 'redirectProfile']);
|
|
$router->get('/p/([A-Za-z0-9\-_]+)', [$this, 'redirectProfile']);
|
|
|
|
// forum categories
|
|
$router->get('/f/?([0-9]+)', [$this, 'redirectForumCategory']);
|
|
$router->get('/fc/?([0-9]+)', [$this, 'redirectForumCategory']);
|
|
|
|
// forum topic
|
|
$router->get('/ft/?([0-9]+)', [$this, 'redirectForumTopic']);
|
|
|
|
// forum post
|
|
$router->get('/fp/?([0-9]+)', [$this, 'redirectForumPost']);
|
|
|
|
// databased, registered last cuz it matches everything otherwise!
|
|
$router->get('/([A-Za-z0-9\-_]+)', [$this, 'redirectDatabase']);
|
|
}
|
|
|
|
public function index($response): void {
|
|
$response->accelRedirect('/index.html');
|
|
$response->setTypeHTML();
|
|
}
|
|
|
|
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_id, redir_url FROM awk_redirects WHERE redir_id = ? OR redir_vanity = ?');
|
|
$getInfo->addParameter(1, $id);
|
|
$getInfo->addParameter(2, $id);
|
|
$getInfo->execute();
|
|
$info = $getInfo->getResult();
|
|
|
|
if(!$info->next())
|
|
return 404;
|
|
|
|
$incClicks = $this->dbConn->prepare('UPDATE awk_redirects SET redir_clicks = redir_clicks + 1 WHERE redir_id = ?');
|
|
$incClicks->addParameter(1, $info->getInteger(0));
|
|
$incClicks->execute();
|
|
|
|
$this->redirect($response, $request, $info->getString(1));
|
|
}
|
|
|
|
private function redirectSimple($response, $request, string $format, string $argument) {
|
|
$scheme = empty($_SERVER['HTTPS']) ? 'http' : 'https';
|
|
$argument = rawurlencode($argument);
|
|
$url = sprintf($format, $scheme, $argument);
|
|
|
|
$this->redirect($response, $request, $url);
|
|
}
|
|
|
|
public function redirectProfile($response, $request, string $userId) {
|
|
$this->redirectSimple($response, $request, $this->urls->getString('user_profile'), $userId);
|
|
}
|
|
|
|
public function redirectForumCategory($response, $request, string $categoryId) {
|
|
$this->redirectSimple($response, $request, $this->urls->getString('forum_category'), $categoryId);
|
|
}
|
|
|
|
public function redirectForumTopic($response, $request, string $topicId) {
|
|
$this->redirectSimple($response, $request, $this->urls->getString('forum_topic'), $topicId);
|
|
}
|
|
|
|
public function redirectForumPost($response, $request, string $postId) {
|
|
$this->redirectSimple($response, $request, $this->urls->getString('forum_post'), $postId);
|
|
}
|
|
}
|