summaryrefslogtreecommitdiff
path: root/search/index.php
blob: 6f7174d6087db0394775b19e8b0101ee3376fef4 (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
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
<?php

header("Location: /");
die();

if (isset($_GET['q'])) {
    if (trim($_GET['q']) === "") {
        header("Location: /search");
        die();
    } else {
        $query = preg_replace("/ +/m", " ", preg_replace("/[^0-9_\-.?=+()a-zA-Z€*\[\]&@ ;,]/m", " ", trim($_GET['q'])));
        $title = $query . " | Search";
    }
} else {
    $query = null;
    $title = "Search";
}

require $_SERVER['DOCUMENT_ROOT'] . "/includes/main.php"; require $_SERVER['DOCUMENT_ROOT'] . "/includes/header.php"; ?>

<div class="container">
    <br>
    <form action="" style="margin-bottom: 20px;">
        <div style="display: grid; grid-template-columns: 1fr 32px; grid-gap: 10px;">
            <input type="text" name="q" autocomplete="off" value="<?= $query ?>" style="outline: none;color: white;background: transparent;border: none;border-bottom: 1px solid rgba(255, 255, 255, .25); width: 100%;" placeholder="Search across our websites...">
            <button type="submit" id="search-submit" style="border: 1px solid rgba(255, 255, 255, .25); background: transparent; border-radius: 100%; width: 32px; height: 32px; vertical-align: middle; padding: 4px;">
                <img src="/assets/icons/search.svg" alt="Search" style="display: inline-block; width: 24px; height: 24px; vertical-align: middle; position: relative; pointer-events: none; top: -2px;">
            </button>
        </div>
    </form>

    <?php if (isset($query)): ?>
    <?php

    $searchSource = "local";

    $out = [];
    chdir($_SERVER['DOCUMENT_ROOT'] . "/includes/search/search");

    $start = microtime(true);
    exec("node search.js \"" . $searchSource . "\" \"" . $query . "\"", $out);
    $end = microtime(true);

    $total = $end - $start;

    $data = json_decode(implode("\n", $out), true);

    ?>

    <?php if (is_array($data)): ?>
        <?php if (count($data["results"]) > 0): ?>
            <p class="muted"><?= count($data["results"]) ?> results (<?= round($total, 2) ?> seconds)</p>
            <div style="display: grid; grid-template-columns: 1.5fr 1fr; grid-gap: 20px;">
                <div id="results">
                    <?php foreach ($data["results"] as $result): $id = md5($result["url"]); ?>
                        <a class="result-anchor" id="result-<?= $id ?>-anchor" href="<?= strip_tags($result["url"]) ?>"><div class="result" id="result-<?= $id ?>" style="border: 1px solid transparent; background: rgba(0, 0, 0, .1); margin-bottom: 10px; border-radius: 10px; padding: 10px 15px;">
                                <div class="result-title" style="font-weight: bold; margin-bottom: 2px;" id="result-<?= $id ?>-title"><?= strip_tags($result["title"]) ?></div>
                                <div class="result-details" id="result-<?= $id ?>-details" style="font-size: smaller; margin-bottom: 2px;">
                                    <img src="<?= $result["icon"] ?>" style="width: 16px; height: 16px; vertical-align: middle; margin-right: 2px;">
                                    <span style="vertical-align: middle;"><?= strip_tags($result["link"]) ?></span>
                                </div>
                                <div class="result-snippet text-muted" id="result-<?= $id ?>-snippet"><?= preg_replace("/\[(.*)]/m", "<b>$1</b>", strip_tags($result["snippet"])) ?></div>
                                <?php if ($result["official"] === 2): ?>
                                    <div class="official official-web text-info" style="margin-top: 2px; font-size: smaller; opacity: .75;">This result is for an official website.</div>
                                <?php elseif ($result["official"] === 1 && $searchSource !== "local"): ?>
                                    <div class="official official-eqd text-warning" style="margin-top: 2px; font-size: smaller; opacity: .75;">This result is for an Equestria.dev official resource.</div>
                                <?php endif; ?>
                            </div></a>
                    <?php endforeach; ?>
                </div>
            </div>
            <!--<hr>
            <pre><?php var_dump($data); ?></pre>-->
        <?php else: ?>
            <p class="muted">
                Sorry, no results corresponding to your query has been found. Please try another query.
            </p>
        <?php endif; ?>
    <?php else: ?>
        <p class="muted">
            Sorry, something went wrong while trying to process your query, Izzy probably put too much glitter! Please try again later.<br>Response ID: <?= strtoupper(substr(sha1(implode("\n", $out)), 0, 8)) . "/" . strtoupper(substr(md5(implode("\n", $out)), 0, 8)) ?>
        </p>
    <?php endif; ?>

    <?php else: ?>
    <p>Search</p>
    <?php endif; ?>
</div>

<style>
    #search-submit:hover {
        background-color: rgba(255, 255, 255, .1) !important;
    }

    #search-submit:active, #search-submit:focus {
        background-color: rgba(255, 255, 255, .25) !important;
        border: 1px solid rgba(255, 255, 255, .5) !important;
    }

    a.result-anchor {
        text-decoration: none;
        color: white;
    }

    .result {
        pointer-events: none;
    }

    a.result-anchor:hover .result {
        border: 1px solid rgba(255, 255, 255, .05) !important;
        background-color: rgba(0, 0, 0, .05) !important;
    }

    a.result-anchor:active .result, a.result-anchor:focus .result {
        border: 1px solid rgba(255, 255, 255, .1) !important;
        background-color: rgba(0, 0, 0, .1) !important;
    }
</style>

<!-- /[^0-9_\-.?=+()a-zA-Z$€*\[\]&@ ;,]/gm -->

<?php require $_SERVER['DOCUMENT_ROOT'] . "/includes/footer.php"; ?>