awaki/src/RedirectorRoutes.php

42 lines
1.1 KiB
PHP

<?php
namespace Awaki;
use Index\Data\IDbConnection;
use Index\Data\DbType;
use Index\Routing\IRouter;
final class RedirectorRoutes {
private IDbConnection $dbConn;
public function __construct(IRouter $router, IDbConnection $dbConn) {
$this->dbConn = $dbConn;
$router->get('/', [$this, 'index']);
$router->get('/:id', [$this, 'redirect']);
}
public function index($response): void {
$response->accelRedirect('/index.html');
$response->setTypeHTML();
}
public function redirect($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);
$getInfo->execute();
$info = $getInfo->getResult();
if(!$info->next())
return 404;
$targetUrl = $info->getString(0);
$params = $request->getParamString();
if(!empty($params))
$targetUrl .= '?' . $params;
$response->redirect($targetUrl);
}
}