forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchplugin.php
129 lines (118 loc) · 5.16 KB
/
searchplugin.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
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
127
128
129
<?php
use src\Utils\Database\XDb;
use src\Utils\Text\TextConverter;
use src\Utils\Uri\SimpleRouter;
use src\Models\ApplicationContainer;
require_once(__DIR__.'/lib/common.inc.php');
// former: searchplugin.inc.php');
$errmsg_no_cache_found = '<span class="errormsg">' . tr('searchplugin_dnf') . '</span>';
$errmsg_many_caches_found = '<span class="errormsg">' . tr('searchplugin_many') .'</span>';
$errmsg_unknown_format = '<span class="errormsg">' . tr('searchplugin_format') . '</span>';
$ocWP = strtolower($GLOBALS['oc_waypoint']);
// initialize
$keyword_name = 'name';
$keyword_finder = 'finder';
$keyword_owner = 'owner';
$keyword_town = 'town';
$keyword_zipcode = 'place';
$keyword_cacheid = 'id';
$keyword_wp = 'wp';
$searchurl = 'search.php';
# get parameter from URL
$userinput = isset($_REQUEST['userinput']) ? $_REQUEST['userinput'] : '';
// $wp = isset($_REQUEST['wp']) ? $_REQUEST['wp'] : '';
$sourceid = isset($_REQUEST['sourceid']) ? $_REQUEST['sourceid'] : 0;
if (($sourceid == 'waypoint-search') && ($userinput != '')) {
$sourceid = 'mozilla-search';
$userinput = 'wp:' . $userinput;
// $wp = 'wp:'.$wp;
}
if (($sourceid == 'mozilla-search') && ($userinput != '')) {
$params = mb_split(':', $userinput);
if ($params !== false) {
if (count($params) == 1) {
$searchto = 'name';
$searchfor = urlencode($params[0]);
} else {
$searchto = $params[0];
array_splice($params, 0, 1);
$searchfor = urlencode(implode(':', $params));
}
unset($params);
// for zipcode/town-search: if logged in, sort by distance
if (ApplicationContainer::GetAuthorizedUser()) {
$order = 'byname';
} else {
$order = 'bydistance';
}
$targeturl = 'search.php?showresult=1&expert=0&output=HTML&f_userowner=0&f_userfound=0';
switch ($searchto) {
case $keyword_name:
$targeturl .= '&sort=byname&searchbyname=1&f_inactive=1&cachename=' . $searchfor;
break;
case $keyword_finder:
$targeturl .= '&sort=byname&searchbyfinder=1&f_inactive=0&finder=' . $searchfor;
break;
case $keyword_owner:
$targeturl .= '&sort=byname&searchbyowner=1&f_inactive=0&owner=' . $searchfor;
break;
case $keyword_town:
$targeturl .= '&searchbyort=1&f_inactive=1&ort=' . $searchfor . '&sort=' . $order;
break;
case $keyword_zipcode:
$targeturl .= '&searchbyplz=1&f_inactive=1&plz=' . $searchfor . '&sort=' . $order;
break;
case $keyword_cacheid:
$targeturl .= '&sort=byname&searchbycacheid=1&f_inactive=1&cacheid=' . $searchfor;
break;
case $keyword_wp:
$targeturl = 'index.php';
$searchfor = TextConverter::mb_trim($searchfor);
$target = mb_strtolower(mb_substr($searchfor, 0, 2));
if (mb_substr($target, 0, 1) == 'n') {
$target = 'nc';
}
if (mb_ereg_match('([a-f0-9]){4,4}$', mb_strtolower($searchfor))) {
$target = $ocWP;
$searchfor = $target . '' . $searchfor;
}
if ((($target == 'oc') || ($target == $ocWP) || ($target == 'nc') || ($target == 'gc')) &&
mb_ereg_match('((' . $ocWP . '|oc)([a-z0-9]){4,4}|gc([a-z0-9]){4,5}|n([a-f0-9]){5,5})$', mb_strtolower($searchfor))) {
// get cache_id from DB
if ($target == $ocWP) {
$target = 'oc';
}
$rs = XDb::xSql(
"SELECT `cache_id`, `latitude`, `longitude` FROM `caches`
WHERE `wp_" . XDb::xEscape($target) . "`= ? ", $searchfor);
$count = XDb::xNumRows($rs);
if ($count == 1) {
$record = XDb::xFetchArray($rs);
$targeturl = 'viewcache.php?cacheid=' . $record['cache_id'];
unset($record);
}
else if ($count == 0) {
$tplname = 'searchplugin';
tpl_set_var('error_msg', mb_ereg_replace('{wp}', $searchfor, $errmsg_no_cache_found));
tpl_BuildTemplate();
exit;
} else if ($count > 1) {
$tplname = 'searchplugin';
tpl_set_var('error_msg', mb_ereg_replace('{wp}', $searchfor, $errmsg_many_caches_found));
tpl_BuildTemplate();
exit;
}
XDb::xFreeResults($rs);
unset($count);
} else {
// wrong waypoint format
$tplname = 'searchplugin';
tpl_set_var('error_msg', $errmsg_unknown_format);
tpl_BuildTemplate();
exit;
}
break;
}
SimpleRouter::redirect($targeturl);
}
}