summaryrefslogtreecommitdiffstats
path: root/prog/sear.php/index.php
blob: 4324fba2cf0f73a2917d10b36b57a191a9b19265 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
function find_class ($haystack, $definition) {
	$pos = stripos($haystack, $definition);
	if ($pos === false)
		return false;
	while ($haystack[$pos] != ".")
		$pos--;
	$endofclass = ++$pos;
	while (ctype_alnum($haystack[$endofclass]))
		$endofclass++;
	return substr($haystack, $pos, $endofclass-$pos);
}
function fix_url ($h) {
	if (str_starts_with($h, "/url?q=")) {
		$h = substr($h, strlen("/url?q="));
		$h = substr($h, 0, strpos($h, "&"));
		$h = urldecode($h);
	}
	if (strpos($h, "googleweblight.com/fp?u=") !== false) {
		$h = substr($h, strpos($h, "googleweblight.com/fp?u=")+strlen("googleweblight.com/fp?u="));
		$h = substr($h, 0, strpos($h, "&"));
		$h = urldecode($h);
	}
	return $h;
}
function only_text_content ($node) {
	$r = "";
	foreach($node->childNodes as $n)
		if ($n->nodeType == XML_TEXT_NODE)
			$r .= $n->nodeValue;
	return $r;
}
function query_google ($s, $image = false, $noredirect = false, $bindstring = false) {
	$us = urlencode($s);
	$a = "";
	if ($image)
		$a .= "&tbm=isch";
	if ($noredirect)
		$a .= "&nfpr=1";
	$url = "http://wap.google.com/search?q=$us&num=100&ie=UTF-8$a";
	$sockarr = [];
	if ($bindstring)
		$sockarr["bindto"] = $bindstring;
	$txtdoc = file_get_contents($url, false, stream_context_create(["socket" => $sockarr, "http" => ["ignore_errors" => true, "header" => "User-Agent: Nokia WAP Gateway 4.1 CD1/ECD13_D/4.1.04)\r\n"]]));
	if ($txtdoc == false)
		return ["status" => false, "code" => "failed"];
	if (str_contains($txtdoc, "In the meantime, solving the above CAPTCHA will let you continue"))
		return ["status" => false, "code" => "captcha", "url" => $url];
	$resultsforclass = find_class($txtdoc, "{color:#1967d2}");
	if ($image) {
		$imageclass = find_class($txtdoc, "{font-family:Roboto,Helvetica,Arial,sans-serif}");
		if (!$imageclass)
			return ["status" => false, "code" => "noimgclass", "url" => $url];
	} else {
		$titleclass = find_class($txtdoc, "{color:#1967D2;font-size:14px;line-height:16px}");
		$descclass = find_class($txtdoc, "{word-break:break-word}");
		if (!$titleclass || !$descclass)
			return ["status" => false, "code" => "noclass", "url" => $url];
	}
	$results = [];
	$x = new DOMDocument();
	$x->loadHTML($txtdoc);
	if ($image)
		foreach ($x->getElementsByTagName("div") as $div) {
			if ($div->getAttribute("class") != $imageclass)
				continue;
			$hreflink = $div->getElementsByTagName("a")[0]->getAttribute("href");
			if (!$hreflink)
				continue;
			preg_match(",.*s?imgurl=([^&]*)&imgrefurl=([^&]*).*,", $hreflink, $matches);
			$imgurl = $matches[1];
			$imgrefurl = $matches[2];
			if (!$imgurl && !$imgrefurl)
				continue;
			$imgurl = urldecode(fix_url($imgurl));
			$imgrefurl = urldecode(fix_url($imgrefurl));
			$results[] = ["imgsrc" => $imgurl, "url" => $imgrefurl];
		}
	else
		foreach ($x->getElementsByTagName("a") as $a) {
			if (!$a->getAttribute("class"))
				continue;
			if (!str_contains($a->getAttribute("class"), $titleclass))
				continue;
			$hreflink = fix_url($a->getAttribute("href"));
			if (!$hreflink)
				continue;
			$firstspan = null;
			foreach ($a->getElementsByTagName("span") as $span) {
				if ($firstspan == null)
					$firstspan = $span;
				if ($span->getAttribute("class") == $descclass)
					$breadcrumbs = $span->nodeValue;
			}
			$table = $a->parentNode->parentNode->getElementsByTagName("table")[0];
			$description = "nisem uspel izluščiti opisa.";
			if ($table)
				foreach ($table->getElementsByTagName("span") as $span)
					if ($span->getAttribute("class") == $descclass)
						$description = $span->nodeValue;
			$results[] = ["url" => $hreflink, "title" => $firstspan->nodeValue, "breadcrumbs" => $breadcrumbs, "description" => $description];
		}
	$suggested = [];
	if ($resultsforclass)
		foreach ($x->getElementsByTagName("a") as $a)
			if (str_contains($a->getAttribute("class"), $resultsforclass))
				if (str_contains($a->getAttribute("href"), "&spell=1&") || (str_contains($a->getAttribute("href"), "&nfpr=1&") && sizeof($suggested) > 0))
					$suggested[] = $a->nodeValue;
	if (sizeof($suggested) > 1 && !$noredirect) // aaaaaaaaaaa tukaj obstaja še ... "Skupaj z rezultati za" response!!!!!!!!
		$s = $suggested[0];
	$suggestion = false;
	if (sizeof($suggested) == 1 || ($noredirect && sizeof($suggested) > 0))
		$suggestion = $suggested[0];
	return ["status" => true, "query" => $s, "suggestion" => $suggestion, "results" => $results, "url" => $url];
}
function template ($title, $queryinfo, $body, $query = "", $additionalform = "", $imgfirst = false) {
	$query = htmlspecialchars($query);
	$title = htmlspecialchars($title);
	$buttons = "
		<button type=submit value=🔍 ><span>=&gt;</span></button>
		<button accesskey=f type=submit name=f value=Ʊ ><span>1.</span></button>
		<button accesskey=i type=submit name=i value=🖼><span>[^]</span></button>
	";
	if ($imgfirst) {
		$buttons = "
			<button type=submit name=i value=🖼><span>[^]</span></button>
			<button accesskey=i type=submit value=🔍 ><span>=&gt;</span></button>
			<button accesskey=f type=submit name=f value=Ʊ ><span>1.</span></button>
		";
	}
	return "
	<!DOCTYPE html>
	<html lang=sl>
		<head>
			<meta charset=UTF-8>
			<title>$title :: sear.php</title>
			<meta name=viewport content='width=device-width, initial-scale=1'>
			<link rel=stylesheet href=/css.css>
			<link rel=icon type=image/x-icon href=favicon.ico>
			<link title=sear.php rel=search type=application/opensearchdescription+xml href=/osdd.xml>
		</head>
		<body>
			<form class=container action=.>
				<input accesskey=s type=text name=q value='$query' placeholder='sear.php ...' size=50 />
				$buttons
				$additionalform
			</form>
			<h3>
				$queryinfo
			</h3>
			$body
			<hr>
			<h4 align=center>
				<a href=//ni.šijanec.eu/projects/r/tree/prog/sear.php>sear.php</a>
				<a href=javascript:window.external.AddSearchProvider(window.location.origin+'/osdd.xml') id=r hidden=hidden >registriraj v brskalnik</a>
			</h4>
			<script>
				if (typeof window.external.AddSearchProvider === 'function')
					document.getElementById('r').hidden = false;
			</script>
		</body>
	";
}
function results_html ($results, $plaintext, $limit) {
	$r = "";
	$i = 0;
	foreach ($results as $result) {
		if ($limit && $i == $limit)
			return $r;
		$safeurl = htmlspecialchars($result["url"]);
		if ($plaintext)
			$safeurl = htmlspecialchars(str_replace("https:", "http:", $result["url"], 1));
		if (isset($result["imgsrc"])) {
			$safeimgsrc = htmlspecialchars($result["imgsrc"]);
			if ($plaintext)
				$safeimgsrc = htmlspecialchars(str_replace("https:", "http:", $result["imgsrc"], 1));
			$r .= "
				<a id=result$i class=result href='$safeurl'>
					<img src='$safeimgsrc' />
				</a>
			";
		} else {
			$safetitle = trim(htmlspecialchars($result["title"]));
			$safebreadcrumb = htmlspecialchars($result["breadcrumbs"]);
			$safedesc = htmlspecialchars($result["description"]);
			$r .= "
				<div id=result$i class=result>
					<h4>
						<a href='$safeurl' accesskey=$i>$safetitle</a> 
						<span class=breadcrumb>
							$safebreadcrumb
						</span>
					</h4>
					<p>
						$safedesc
					</p>
				</div>
			";
		}
		$i++;
	}
	return $r;
}
function handle_response ($query, $response, $limit, $plaintext, $horseshoe, $add_url, $add_form, $image) {
	if (sizeof($response["results"]) == 0)
		die(template($query, "ni rezultatov", "vaše iskanje ni obrodilo sadov.", $query, "", $image));
	if ($horseshoe)
		header("Location: " . $response["results"][0]["url"]);
	$queryinfo = "";
	if ($response["query"] != $query) {
		$safequeryurl = urlencode($query);
		$safequeryhtml = htmlspecialchars($query);
		$queryinfo .= "preusmeril sem vas iz <a href='?e=e$add_url&q=$safequeryurl'>$safequeryhtml</a> | ";
	}
	if ($response["suggestion"] != false) {
		$safequeryurl = urlencode($response["suggestion"]);
		$safequeryhtml = htmlspecialchars($response["suggestion"]);
		$queryinfo .= "predlagam iskanje <a href='?e=e$add_url&q=$safequeryurl'>$safequeryhtml</a> | ";
	}
	$queryinfo .= sizeof($response["results"]) . " zadetkov";
	$resultshtml = results_html($response["results"], $plaintext, $limit);
	die(template($response["query"], $queryinfo, $resultshtml, $response["query"], $add_form, $image));
}
function try_query_google ($q, $image, $exact) {
	$bindstrings = [];
	foreach (net_get_interfaces() as $interface)
		foreach ($interface["unicast"] as $unicast) {
			if (!isset($unicast["address"]))
				continue;
			$address = $unicast["address"];
			if ($address == "::1") // tu timeouta
				continue;
			$bindstring = "$address:0";
			if (str_contains($address, ":"))
				$bindstring = "[$address]:0";
			$bindstrings[] = $bindstring;
		}
	if (getenv("BINDSTRINGS"))
		$bindstrings = explode(",", getenv("BINDSTRINGS"));
	foreach ($bindstrings as $bindstring) {
		// file_put_contents("/dev/stderr", "trying bindstring $bindstring\n");
		$response = query_google($q, $image, $exact, $bindstring);
		if ($response["status"] == true)
			break;
		if ($response["code"] == "captcha")
			continue;
		if ($response["code"] == "failed")
			continue;
		break;
	}
	return $response;
}
if (!isset($_REQUEST["debug"]))
	ini_set('display_errors','Off');
$add_form = "";
$add_url = "";
$q = null;
if (isset($_REQUEST["q"]))
	$q = $_REQUEST["q"];
$image = false;
if (!empty($_REQUEST["i"]))
	$image = true;
$exact = false;
if (!empty($_REQUEST["e"])) {
	$add_url .= "&e=e";
	$exact = true;
	$add_form .= "<input type=hidden name=e value=e />";
}
if (!empty($_REQUEST["json"])) {
	header("Content-Type: application/json");
	$response = try_query_google($q, $image, $exact);
	if ($response["status"] == false)
		http_response_code(500);
	die(json_encode($response));
}
$limit = false;
if (!empty($_REQUEST["l"])) {
	$limit = int($_REQUEST["l"]);
	$add_url .= "&l=$limit";
	$add_form .= "<input type=hidden name=l value=$limit />";
}
$plaintext = false;
if (!empty($_REQUEST["h"])) {
	$plaintext = true;
	$add_url .= "&h=h";
	$add_form .= "<input type=hidden name=h value=h />";
}
$horseshoe = false;
if (!empty($_REQUEST["f"])) {
	$horseshoe = true;
	$add_url .= "&f=f";
	$add_form .= "<input type=hidden name=f value=f />";
}
if ($q == null) {
	die(template("", "iskalnik sear.php", "dobrodošli v iskalniku sear.php, nasledniku programa sear.c. za iskanje po spletu nekaj vnesite v iskalno vrstico zgoraj in pritisnite na gumb."));
}
$response = try_query_google($q, $image, $exact);
if ($response["status"] != true) {
	if ($response["code"] == "captcha") {
		foreach (["http://searc.oliwerix.com/sear.php?ref=b"] as $fallback) {
			$add_url = "";
			if ($exact)
				$add_url .= "&e=e";
			$json = json_decode(file_get_contents($fallback . $add_url . "&q=" . urlencode($q)), true);
			if (!isset($json["status"]))
				continue;
			if ($json["status"] == false) {
				if ($json["code"] == "captcha")
					continue;
				http_response_code(500);
				die(template("napaka", "napaka pri posredovanju zahteve", "ker je poizvedba vrnila captcho, sem jo posredoval na drug strežnik, ta pa je vrnil napako " . $json["code"] . ".", $q));
			}
			handle_response($q, $json, $limit, $plaintext, $horseshoe, $add_url, $add_form, $image);
		}
		http_response_code(500);
		die(template("napaka", "iskalno zaledje omejuje iskanja", "iskalno zaledje se je odzvalo s CAPTCHO, češ, da ta instanca prehitro pošilja zahteve. obenem se je enako zgodilo tudi vsem rezervnim strežnikom. iščete lahko ročno preko spletne strani zaledne storitve: <a href='" . htmlspecialchars($response["url"]) . "'>kliknite sem</a>.", $q));
	}
	http_response_code(500);
	die(template("napaka", "napaka pri poizvedbi", "poizvedba na iskalno zaledje je vrnila napako " . $response["code"] . ".", $q));
}
handle_response($q, $response, $limit, $plaintext, $horseshoe, $add_url, $add_form, $image);
?>