This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
forked from beniz/seeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
144 lines (106 loc) · 4.43 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
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
<?php
/* Copyright Camille Harang, Pablo Joubert, Emmanuel Benazera
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see http://www.fsf.org/licensing/licenses/agpl-3.0.html. */
/*****************************************************************************
* Important change to php.ini: *
*****************************************************************************
* You MUST set always_populate_raw_post_data=1 in your php.ini to get this *
* working!). E.g. I had to set this in FPM's seeks.conf: *
* php_admin_value[always_populate_raw_post_data] = 1 *
*****************************************************************************
* My file is located at /etc/php5/fpm/pool.d/seeks.conf. *
*****************************************************************************
*/
// Let's fix all of them
error_reporting(E_ALL | E_STRICT);
// Default is HTTP
$scheme = 'http://';
// Is HTTPS set?
if (isset($_SERVER['HTTPS'])) {
// Then assume https:// shall be used
$scheme = 'https://';
}
$seeks_uri = 'http://s.s';
$proxy = 'localhost:8250';
$base_url = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
if ($_SERVER['REQUEST_URI'] == '/search.php') {
header('Location: ' . $base_url . '/websearch-hp');
// Don't miss exit() as the server redirect doesn't abort the script
exit();
} else {
$url = $seeks_uri . str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
}
// Avoid E_NOTICE by initializing variables and checking on array elements
$lang_head = '';
$referer = '';
// Not all browsers are sending this header
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$lang_head = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
// Not all browsers are sending this header
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
}
$qc_redir = array();
preg_match('/qc_redir/', $url, $qc_redir);
$tbd = array();
preg_match('/tbd/', $url, $tbd);
$bqc = array();
preg_match('/find_bqc/', $url, $bqc);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ((isset($qc_redir[0]) && $qc_redir[0] == 'qc_redir') || (isset($tbd[0]) && $tbd[0] == 'tbd')) {
curl_setopt($curl, CURLOPT_HEADER, true);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
if (isset($bqc[0]) && $bqc[0] == 'find_bqc') {
$postdata = file_get_contents('php://input');
// For debugging purposes
//* DEBUG: */ file_put_contents('/tmp/foo', ini_get('always_populate_raw_post_data') . ':' . $postdata);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
}
curl_setopt($curl, CURLOPT_PROXY, $proxy);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Seeks-Remote-Location: ' . $base_url, 'Accept-Language: ' . $lang_head, 'Proxy-Connection: Close', 'Expect:', 'Referer: ' . $referer));
$result = curl_exec($curl);
$result_info = curl_getinfo($curl);
// Very noisy in syslog:
//* NOISY-DEBUG: */ syslog(LOG_INFO, 'result_info=' . json_encode($result_info));
if (curl_errno($curl)) {
echo 'CURL ERROR: '.curl_error($curl);
}
curl_close($curl);
if (headers_sent()) {
// Headers are already sent, cannot continue, maybe error?
exit();
}
header('Content-Type: ' . $result_info['content_type']);
if ((isset($qc_redir[0]) && $qc_redir[0] == 'qc_redir') || (isset($tbd[0]) && $tbd[0] == 'tbd')) {
preg_match('/\d\d\d/', $result, $status_code);
switch( $status_code[0] ) {
case 302:
$location = array();
preg_match('/Location: (.*)/', $result, $location);
$location[0] = substr($location[0], 10);
header('Location: '. $location[0]);
// Don't miss exit() as the server redirect doesn't abort the script
exit();
default:
syslog(LOG_WARNING, 'Status code ' . $status_code[0] . ' found.');
break;
}
}
// Very noisy in syslog:
//* NOISY-DEBUG: */ syslog(LOG_INFO, 'result=' . json_encode($result));
echo $result;
?>