aboutsummaryrefslogtreecommitdiff
path: root/out/index.php
blob: cefe36b3c701f092dcc2eb3596c1d181ee345557 (plain)
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
<?php

if (!isset($_GET['q']) || !isset($_GET['u']) || !isset($_GET['i'])) {
    header("Location: /");
    die();
}

$_GET['q'] = strtolower(substr($_GET['q'], 0, 200));
$_GET['q'] = preg_replace("/[^A-Za-z0-9 ]/", '', preg_replace("/[\.]/", ' ', $_GET['q']));

$data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/db.json"), true);

if ($_GET['i'] !== "_off" && isset($data["entries"][$_GET['i']])) {
    header("Location: " . $data["entries"][$_GET['i']]["url"]);
    die();
} elseif ($_GET['i'] === "_off") {
    $domains = [];

    foreach ($data["entries"] as $id => $entry) {
        $domain = explode("/", $entry["url"])[2];
        $sim = similar_text($domain, $_GET['q'], $perc);
        if (substr($domain, 0, 4) === "www.") {
            $domainstr = substr($domain, 4);
        } else {
            $domainstr = $domain;
        }
        $domains[] = [
            "id" => $id,
            "url" => "https://" . $domain . "/",
            "domain" => $domainstr,
            "relevance" => $perc / 50 - 1
        ];
    }

    usort($domains, function($a, $b) {
        return $a['relevance'] <=> $b['relevance'];
    });

    $domains = array_reverse($domains);
    header("Location: " . $domains[0]['url']);
    die();
}