Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirects #644

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/Middleware/Lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Lifecycle implements MiddlewareInterface {
private Throwable $throwable;

public function start():void {
// Before we start, we check if the current URI should be redirected. If it
// should, we won't go any further into the lifecycle.
$this->handleRedirects();

// The first thing that's done within the WebEngine lifecycle is start a timer.
// This timer is only used again at the end of the call, when finish() is
// called - at which point the entire duration of the request is logged out (and
Expand Down Expand Up @@ -237,4 +241,65 @@ public function finish(

exit;
}

private function handleRedirects():void {
$redirectFiles = [
"\t" => "redirects.tsv",
"," => "redirects.csv",
];
foreach($redirectFiles as $separatorCharacter => $fileName) {
if(!is_file($fileName)) {
continue;
}

Log::debug("Checking redirect file: $fileName");
$currentUri = $_SERVER["REQUEST_URI"];

$lines = file($fileName);
usort($lines, function(string $lineA, string $lineB):int {
$lineARegex = str_starts_with($lineA, "~");
$lineBRegex = str_starts_with($lineB, "~");
if($lineARegex && !$lineBRegex) {
return -1;
}

if(!$lineARegex && $lineBRegex) {
return 1;
}

return 0;
});

foreach($lines as $line) {
$row = str_getcsv($line, $separatorCharacter);
if(!$row || !$row[0]) {
continue;
}

$matchingUri = $row[0];
$redirectUri = $row[1];
$responseCode = $row[2] ?? 302;

$match = $currentUri === $matchingUri;
if($matchingUri[0] === "~") {
$matchingUri = substr($matchingUri, 1);
if(preg_match("~$matchingUri~", $currentUri, $matches)) {
$match = true;
$matchIndex = 1;
while(str_contains($redirectUri, '$' . $matchIndex)) {
$redirectUri = str_replace('$' . $matchIndex, $matches[$matchIndex], $redirectUri);
}
}
}

if($match) {
Log::notice("Redirecting: $currentUri -> $redirectUri ($responseCode)");
header("Location: $redirectUri", true, $responseCode);
exit;
}
}
return;
}
}

}
Loading