fix(router): support case-insensitive route URLs and token query parameters
This commit is contained in:
parent
d970270d91
commit
71fed56884
3 changed files with 18 additions and 12 deletions
|
|
@ -3,7 +3,7 @@ require_once __DIR__ . '/../../core/Controller.php';
|
||||||
|
|
||||||
class RegisterController extends Controller {
|
class RegisterController extends Controller {
|
||||||
public function index($urlToken = null) {
|
public function index($urlToken = null) {
|
||||||
$token = trim($_GET['token'] ?? $urlToken ?? '');
|
$token = trim($_GET['token'] ?? $_GET['TOKEN'] ?? $urlToken ?? '');
|
||||||
|
|
||||||
if (empty($token)) {
|
if (empty($token)) {
|
||||||
$this->view('register', [
|
$this->view('register', [
|
||||||
|
|
@ -50,7 +50,7 @@ class RegisterController extends Controller {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = trim($_POST['token'] ?? '');
|
$token = trim($_POST['token'] ?? $_POST['TOKEN'] ?? '');
|
||||||
$name = trim($_POST['name'] ?? '');
|
$name = trim($_POST['name'] ?? '');
|
||||||
$email = trim(strtolower($_POST['email'] ?? ''));
|
$email = trim(strtolower($_POST['email'] ?? ''));
|
||||||
$password = $_POST['password'] ?? '';
|
$password = $_POST['password'] ?? '';
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class Invitation {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getByToken(string $token): ?array {
|
public function getByToken(string $token): ?array {
|
||||||
$sql = "SELECT * FROM home_invitations WHERE token = :token";
|
$sql = "SELECT * FROM home_invitations WHERE LOWER(token) = LOWER(:token)";
|
||||||
$this->db->query($sql);
|
$this->db->query($sql);
|
||||||
$this->db->bind(':token', $token);
|
$this->db->bind(':token', $token);
|
||||||
$invitation = $this->db->single();
|
$invitation = $this->db->single();
|
||||||
|
|
@ -52,7 +52,7 @@ class Invitation {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function recordUse(string $token): bool {
|
public function recordUse(string $token): bool {
|
||||||
$sql = "UPDATE home_invitations SET uses_count = uses_count + 1 WHERE token = :token";
|
$sql = "UPDATE home_invitations SET uses_count = uses_count + 1 WHERE LOWER(token) = LOWER(:token)";
|
||||||
$this->db->query($sql);
|
$this->db->query($sql);
|
||||||
$this->db->bind(':token', $token);
|
$this->db->bind(':token', $token);
|
||||||
return $this->db->execute();
|
return $this->db->execute();
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,25 @@ class Router {
|
||||||
public function route() {
|
public function route() {
|
||||||
$url = $this->parseUrl();
|
$url = $this->parseUrl();
|
||||||
$controllerPath = __DIR__ . '/../app/controllers/';
|
$controllerPath = __DIR__ . '/../app/controllers/';
|
||||||
// Controller Check
|
// Controller Check (Case-insensitive)
|
||||||
if (isset($url[0]) && file_exists($controllerPath . ucfirst($url[0]) . 'Controller.php')) {
|
if (isset($url[0])) {
|
||||||
$this->controller = ucfirst($url[0]) . 'Controller';
|
$candidate = ucfirst(strtolower($url[0])) . 'Controller';
|
||||||
unset($url[0]);
|
if (file_exists($controllerPath . $candidate . '.php')) {
|
||||||
|
$this->controller = $candidate;
|
||||||
|
unset($url[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
require_once $controllerPath . $this->controller . '.php';
|
require_once $controllerPath . $this->controller . '.php';
|
||||||
$this->controller = new $this->controller;
|
$this->controller = new $this->controller;
|
||||||
// Method Check
|
// Method Check (Case-insensitive)
|
||||||
if (isset($url[1])) {
|
if (isset($url[1])) {
|
||||||
$candidateMethod = $url[1];
|
$candidateMethod = $url[1];
|
||||||
if (is_callable([$this->controller, $candidateMethod]) && strpos($candidateMethod, '_') !== 0) {
|
foreach (get_class_methods($this->controller) as $methodName) {
|
||||||
$this->method = $candidateMethod;
|
if (strcasecmp($methodName, $candidateMethod) === 0 && strpos($methodName, '_') !== 0) {
|
||||||
unset($url[1]);
|
$this->method = $methodName;
|
||||||
|
unset($url[1]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Params & Execution
|
// Params & Execution
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue