-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
76 lines (52 loc) · 1.79 KB
/
search.php
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
<?php
require_once("config.php");
// PARSE DATA
$data = json_decode(file_get_contents("./dataset/data.json"), true);
if($data["version"] != $allowed_data_version) {
$err = "Version of data is invalid. Please update your open-bangs server.";
print("ERROR: " . $err);
throw new Exception($err);
}
$bangs = $data["data"];
$query = $_GET["q"];
$upstream = $_GET["upstream"];
$upstream_data = $upstreams[$upstream];
if(!$upstream_data) {
$err = "Upstream not found.";
print("ERROR: " . $err);
// throw new Exception($err);
exit();
}
$upstream_url = $upstream_data["url"];
function getFallbackUrl($query, $upstream) {
global $upstream_url;
return str_replace("%s", $query, $upstream_url);
}
function containsBangs($query) {
global $bangs;
foreach($bangs as $bang) {
$keyword = $bang["keyword"];
// FIXME: do not match "!bang2" if "bang"
$found = preg_match("/(!$keyword)/", $query);
if($found == true) {
return $keyword;
}
}
return false;
}
$bang_keyword = containsBangs($query);
if($bang_keyword) {
// TODO: Replace hacky script
foreach($bangs as $bang) {
if($bang["keyword"] == "$bang_keyword") {
$cleaned_query = str_replace("!$bang_keyword", "", $query);
header("Location: " . str_replace("%s", $cleaned_query, $bang["url"]));
// Exit so no Exception is thrown
exit();
}
}
// Throw Exception if not found
throw new Exception();
} else {
header("Location: " . getFallbackUrl($query, $upstream));
}