all repos — nasg @ 5ec437de8feef9b7ba30b6b172f15f552a369ca6

templates/Index.j2.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
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
<?php

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

$redirects_re = array(
    '^(?:sysadmin|it|linux-tech-coding|sysadmin-blog)\/?(page.*)?$' => 'category/article/',
    '^(?:fotography|photoblog)\/?(page.*)?$' => '/category/photo/$1',
    '^blog\/?(page.*)?$' => '/category/journal/',
    '^blips\/?(page.*)?$' => '/category/note/$1',
    '^r\/?(page.*)?$' => '/category/note/$1',
    '^(?:linux-tech-coding|it|sysadmin-blog|sysadmin|fotography|blips|blog|photoblog|article|journal|photo|note|r)\/((?!page).*)' => '/$1',
);

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

$gone_re = array(
    '^cache/.*$',
    '^files/.*$',
    '^wp-content/.*$',
    '^broadcast\/wp-ffpc.message$',
);


function redirect_to($uri) {
    header('HTTP/1.1 301 Moved Permanently');
    if (preg_match("/^https?/", $uri))
        $target = $uri;
    else
        $target = '{{ site.url }}/'.  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><center>This content was deleted.</center></h1>
<hr>
<p><center>{{ site.domain }}</center></p>
 </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><center>This was not found.</center></h1>
<h2><center>Please search for it instead.</center></h2>
<p>
    <center>
<form action="/search.php" class="search-form" method="get" role="search">
  <label for="search">Search</label>
  <input id="q" name="q" placeholder="search..." title="Search for:" type="search" value=""/>
  <input type="submit" value="OK"/>
</form>
    </center>
</p>
 </body>
</html>');
}

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

$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, '/');

foreach ($gone_re as $pattern) {
    if (preg_match(sprintf('/%s/', $pattern), $uri)) {
        gone($uri);
    }
}

foreach ($redirects_re as $pattern => $target) {
    $maybe = preg_match(sprintf('/%s/i', $pattern), $uri, $matches);
    if ($maybe) {
        $target = str_replace('$1', $matches[1], $target);
        redirect_to($target);
    }
}

/* "logic" */
if (isset($gone[$uri])) {
    gone($uri);
}
elseif (isset($redirects[$uri])) {
    redirect_to($redirects[$uri]);
}
elseif (preg_match('/^\.well-known\/(host-meta|webfinger).*$/', $uri)) {
    redirect_to("https://fed.brid.gy/{$uri}");
}
elseif (strstr($uri, '_')) {
    maybe_redirect(str_replace('_', '-', $uri));
}
else {
    notfound();
}