all repos — nasg @ c443c94979bfba5c8011310f578e6e632c98f5b9

magic.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
<?php

function redirect_to($uri) {
    header('HTTP/1.1 301 Moved Permanently');
    if (preg_match("/^https?/", $uri))
        $target = $uri;
    else
        $target = 'https://petermolnar.net/'.  trim($uri, '/') . '/';
    header("Location: ". $target);
    exit;
}

function gone($uri) {
    header('HTTP/1.1 410 Gone');
    die('<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1,minimum-scale=1" name="viewport"/>
  <title>Gone</title>
 </head>
 <body>
<h1>This content was deleted.</h1>
 </body>
</html>');
}

function notfound() {
    header('HTTP/1.0 404 Not Found');
    die('<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1,minimum-scale=1" name="viewport"/>
  <title>Not found</title>
 </head>
 <body>

<h1>This was not found.</h1>
<h2>Please search for it instead.</h2>
<p>
<form action="/search" class="search-form" method="get" role="search">
  <label for="search">Search</label>
  <input id="s" name="s" placeholder="search..." title="Search for:" type="search" value=""/>
  <input type="submit" value="OK"/>
</form>
</p>
 </body>
</html>');
}

function maybe_redirect($uri) {
    if (file_exists("./{$uri}/index.html")) {
        redirect_to($uri);
    }
}

$redirects = array(
{% for (from, to) in redirects %}
    "{{ from }}" => "{{ to }}",
{%- endfor -%}
);

$gone = array(
{% for gone in gones %}
    "{{ gone }}" => true,
{%- endfor -%}
);

$uri = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
$uri = str_replace('../', '', $uri);
$uri = str_replace('/feed/', '', $uri);
$uri = str_replace('/atom/', '', $uri);
$uri = trim($uri, '/');

if (isset($gone[$uri]))
    gone($uri);
elseif (isset($redirects[$uri]))
    redirect_to($redirects[$uri]);
// replace _ with - and look for a file
elseif (strstr($uri, '_'))
    maybe_redirect(str_replace('_', '-', $uri));
// try getting rid of -by-xyz
elseif (stristr($uri,'-by-'))
    maybe_redirect(preg_replace('/(.*?)-by-.*$/i','${1}',$uri));
// try getting rid of -2, WordPress artifacts
elseif (stristr($uri,'-2'))
    maybe_redirect(preg_replace('/(.*?)-2$/i','${1}',$uri));
else
    notfound();