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
|
<?php
if (isset($_GET['q']) && strpos($_GET['q'], "/") === false && strpos($_GET['q'], "&") === false && strpos($_GET['q'], ">") === false && strpos($_GET['q'], ">") === false) {
$q = $_GET['q'];
} else {
header("Location: /");
die();
}
$_TITLE = "Search Results"; require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/dom/header.php";
$orig = getArticlesList();
$list = [];
$words = explode(' ', $q);
$index = 0;
foreach ($words as $word) {
if (!ctype_alnum($word)) {
header("Location: /");
die();
}
}
foreach ($orig as $article) {
if (!$article["admin"]) {
$item = $article;
$add = false;
$matches = 0;
foreach ($words as $word) {
if (substr_count(strtolower($article["title"]), strtolower($word)) > 0) $add = true;
if (substr_count(strtolower($article["title"]), strtolower($word)) > 0) $matches += substr_count(strtolower($article["title"]), strtolower($word));
foreach ($article["author"] as $author) {
if (substr_count(strtolower($author), strtolower($word)) > 0) $add = true;
if (substr_count(strtolower($author), strtolower($word)) > 0) $matches += substr_count(strtolower($author), strtolower($word));
}
if (substr_count(strtolower($article["category"]), strtolower($word)) > 0) $add = true;
if (substr_count(strtolower($article["category"]), strtolower($word)) > 0) $matches += substr_count(strtolower($article["category"]), strtolower($word));
if (substr_count(strtolower($article["content"]["clean"]), strtolower($word)) > 0) $add = true;
if (substr_count(strtolower($article["content"]["clean"]), strtolower($word)) > 0) $matches += substr_count(strtolower($article["content"]["clean"]), strtolower($word));
if (substr_count(strtolower($article["date"]), strtolower($word)) > 0) $add = true;
if (substr_count(strtolower($article["date"]), strtolower($word)) > 0) $matches += substr_count(strtolower($article["date"]), strtolower($word));
}
if ($add) {
$list[$index]["matches"] = $matches;
$list[$index]["content"] = '<div class="card' . ($item["admin"] ? " card-admin" : "") . '">
<div class="card-body">
<p><span class="badge badge-secondary">' . $item["category"] . '</span></p>
<h2>' . $item["title"] . '</h2>
<p>' . $item["content"]["mini"] . '</p>
<a class="btn btn-primary" href="' . ($item["admin"] ? "/admin" : "") . '/article/' . $item["id"] . '">Read more</a>
</div>
<div class="card-footer">
<small class="text-muted">';
if ($item["admin"]) {
$list[$index]["content"] .= "<b>Unreleased</b>";
} else {
$dt = DateTime::createFromFormat('Ymd', $item["date"]);
$list[$index]["content"] .= $dt->format("M jS, Y");
}
$list[$index]["content"] .= ' · by ' . implode(" and ", $item["author"]) . '</small>
</div>
</div>';
$index++;
}
}
}
usort($list, function($a, $b) {
return strtoupper($a['matches']) <=> strtoupper($b['matches']);
});
$list = array_reverse($list);
?>
<div class="container" style="margin-top:30px;text-align: center;">
<h2>Results for « <?= $q ?> »</h2>
<p><?= count($list) ?> result<?= count($list) > 1 ? "s" : "" ?> found</p>
</div>
<div class="container">
<style>
@media (max-width: 800px) {
#results {
grid-template-columns: 1fr;
}
}
</style>
<div id="results" style="display: grid;grid-template-columns: 1fr 1fr 1fr;grid-gap: 20px;">
<?php foreach ($list as $item) {
echo($item["content"]);
} ?>
</div>
</div>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/dom/footer.php"; ?>
|