Skip to content

Commit

Permalink
Redirects (#644)
Browse files Browse the repository at this point in the history
* wip: redirects file for #643

* tweak: remove todo
  • Loading branch information
g105b authored Dec 18, 2023
1 parent 286b441 commit f7937cc
Showing 1 changed file with 65 additions and 0 deletions.
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;
}
}

}

0 comments on commit f7937cc

Please sign in to comment.