@@ -1,66 +1,66 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | class Search_Sphinx extends Plugin { |
4 | - public function about() { |
|
5 | - return array(1.0, |
|
6 | - "Delegate searching for articles to Sphinx (don't forget to set options in config.php)", |
|
7 | - "hoelzro", |
|
8 | - true, |
|
9 | - "https://git.tt-rss.org/fox/tt-rss/wiki/SphinxSearch"); |
|
10 | - } |
|
4 | + public function about() { |
|
5 | + return array(1.0, |
|
6 | + "Delegate searching for articles to Sphinx (don't forget to set options in config.php)", |
|
7 | + "hoelzro", |
|
8 | + true, |
|
9 | + "https://git.tt-rss.org/fox/tt-rss/wiki/SphinxSearch"); |
|
10 | + } |
|
11 | 11 | |
12 | - public function init($host) { |
|
13 | - $host->add_hook($host::HOOK_SEARCH, $this); |
|
12 | + public function init($host) { |
|
13 | + $host->add_hook($host::HOOK_SEARCH, $this); |
|
14 | 14 | |
15 | - // idk if that would work but checking for the class being loaded is somehow not enough |
|
16 | - if (class_exists("SphinxClient") && !defined('SEARCHD_COMMAND_SEARCH')) { |
|
17 | - user_error("Your PHP has a separate systemwide Sphinx client installed which conflicts with the client library used by tt-rss. Either remove the system library or disable Sphinx support."); |
|
18 | - } |
|
15 | + // idk if that would work but checking for the class being loaded is somehow not enough |
|
16 | + if (class_exists("SphinxClient") && !defined('SEARCHD_COMMAND_SEARCH')) { |
|
17 | + user_error("Your PHP has a separate systemwide Sphinx client installed which conflicts with the client library used by tt-rss. Either remove the system library or disable Sphinx support."); |
|
18 | + } |
|
19 | 19 | |
20 | - require_once __DIR__."/sphinxapi.php"; |
|
21 | - } |
|
20 | + require_once __DIR__."/sphinxapi.php"; |
|
21 | + } |
|
22 | 22 | |
23 | - public function hook_search($search) { |
|
24 | - $offset = 0; |
|
25 | - $limit = 500; |
|
23 | + public function hook_search($search) { |
|
24 | + $offset = 0; |
|
25 | + $limit = 500; |
|
26 | 26 | |
27 | - $sphinxClient = new SphinxClient(); |
|
27 | + $sphinxClient = new SphinxClient(); |
|
28 | 28 | |
29 | - $sphinxpair = explode(":", SPHINX_SERVER, 2); |
|
29 | + $sphinxpair = explode(":", SPHINX_SERVER, 2); |
|
30 | 30 | |
31 | - $sphinxClient->SetServer($sphinxpair[0], (int) $sphinxpair[1]); |
|
32 | - $sphinxClient->SetConnectTimeout(1); |
|
31 | + $sphinxClient->SetServer($sphinxpair[0], (int) $sphinxpair[1]); |
|
32 | + $sphinxClient->SetConnectTimeout(1); |
|
33 | 33 | |
34 | - $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, |
|
35 | - 'feed_title' => 20)); |
|
34 | + $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, |
|
35 | + 'feed_title' => 20)); |
|
36 | 36 | |
37 | - $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); |
|
38 | - $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); |
|
39 | - $sphinxClient->SetLimits($offset, $limit, 1000); |
|
40 | - $sphinxClient->SetArrayResult(false); |
|
41 | - $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid'])); |
|
37 | + $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); |
|
38 | + $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); |
|
39 | + $sphinxClient->SetLimits($offset, $limit, 1000); |
|
40 | + $sphinxClient->SetArrayResult(false); |
|
41 | + $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid'])); |
|
42 | 42 | |
43 | - $result = $sphinxClient->Query($search, SPHINX_INDEX); |
|
43 | + $result = $sphinxClient->Query($search, SPHINX_INDEX); |
|
44 | 44 | |
45 | - $ids = array(); |
|
45 | + $ids = array(); |
|
46 | 46 | |
47 | - if (is_array($result['matches'])) { |
|
48 | - foreach (array_keys($result['matches']) as $int_id) { |
|
49 | - $ref_id = $result['matches'][$int_id]['attrs']['ref_id']; |
|
50 | - array_push($ids, $ref_id); |
|
51 | - } |
|
52 | - } |
|
47 | + if (is_array($result['matches'])) { |
|
48 | + foreach (array_keys($result['matches']) as $int_id) { |
|
49 | + $ref_id = $result['matches'][$int_id]['attrs']['ref_id']; |
|
50 | + array_push($ids, $ref_id); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - $ids = join(",", $ids); |
|
54 | + $ids = join(",", $ids); |
|
55 | 55 | |
56 | - if ($ids) { |
|
57 | - return array("ref_id IN ($ids)", array()); |
|
58 | - } else { |
|
59 | - return array("ref_id = -1", array()); |
|
60 | - } |
|
61 | - } |
|
56 | + if ($ids) { |
|
57 | + return array("ref_id IN ($ids)", array()); |
|
58 | + } else { |
|
59 | + return array("ref_id = -1", array()); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - public function api_version() { |
|
64 | - return 2; |
|
65 | - } |
|
63 | + public function api_version() { |
|
64 | + return 2; |
|
65 | + } |
|
66 | 66 | } |
@@ -123,1571 +123,1571 @@ |
||
123 | 123 | /// pack 64-bit signed |
124 | 124 | function sphPackI64($v) |
125 | 125 | { |
126 | - assert(is_numeric($v)); |
|
127 | - |
|
128 | - // x64 |
|
129 | - if (PHP_INT_SIZE >= 8) |
|
130 | - { |
|
131 | - $v = (int) $v; |
|
132 | - return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
133 | - } |
|
134 | - |
|
135 | - // x32, int |
|
136 | - if (is_int($v)) |
|
137 | - return pack("NN", $v < 0 ? -1 : 0, $v); |
|
138 | - |
|
139 | - // x32, bcmath |
|
140 | - if (function_exists("bcmul")) |
|
141 | - { |
|
142 | - if (bccomp($v, 0) == -1) |
|
143 | - $v = bcadd("18446744073709551616", $v); |
|
144 | - $h = bcdiv($v, "4294967296", 0); |
|
145 | - $l = bcmod($v, "4294967296"); |
|
146 | - return pack("NN", (float) $h, (float) $l); // conversion to float is intentional; int would lose 31st bit |
|
147 | - } |
|
148 | - |
|
149 | - // x32, no-bcmath |
|
150 | - $p = max(0, strlen($v) - 13); |
|
151 | - $lo = abs((float) substr($v, $p)); |
|
152 | - $hi = abs((float) substr($v, 0, $p)); |
|
153 | - |
|
154 | - $m = $lo + $hi * 1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912 |
|
155 | - $q = floor($m / 4294967296.0); |
|
156 | - $l = $m - ($q * 4294967296.0); |
|
157 | - $h = $hi * 2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328 |
|
158 | - |
|
159 | - if ($v < 0) |
|
160 | - { |
|
161 | - if ($l == 0) |
|
162 | - $h = 4294967296.0 - $h; |
|
163 | - else |
|
164 | - { |
|
165 | - $h = 4294967295.0 - $h; |
|
166 | - $l = 4294967296.0 - $l; |
|
167 | - } |
|
168 | - } |
|
169 | - return pack("NN", $h, $l); |
|
126 | + assert(is_numeric($v)); |
|
127 | + |
|
128 | + // x64 |
|
129 | + if (PHP_INT_SIZE >= 8) |
|
130 | + { |
|
131 | + $v = (int) $v; |
|
132 | + return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
133 | + } |
|
134 | + |
|
135 | + // x32, int |
|
136 | + if (is_int($v)) |
|
137 | + return pack("NN", $v < 0 ? -1 : 0, $v); |
|
138 | + |
|
139 | + // x32, bcmath |
|
140 | + if (function_exists("bcmul")) |
|
141 | + { |
|
142 | + if (bccomp($v, 0) == -1) |
|
143 | + $v = bcadd("18446744073709551616", $v); |
|
144 | + $h = bcdiv($v, "4294967296", 0); |
|
145 | + $l = bcmod($v, "4294967296"); |
|
146 | + return pack("NN", (float) $h, (float) $l); // conversion to float is intentional; int would lose 31st bit |
|
147 | + } |
|
148 | + |
|
149 | + // x32, no-bcmath |
|
150 | + $p = max(0, strlen($v) - 13); |
|
151 | + $lo = abs((float) substr($v, $p)); |
|
152 | + $hi = abs((float) substr($v, 0, $p)); |
|
153 | + |
|
154 | + $m = $lo + $hi * 1316134912.0; // (10 ^ 13) % (1 << 32) = 1316134912 |
|
155 | + $q = floor($m / 4294967296.0); |
|
156 | + $l = $m - ($q * 4294967296.0); |
|
157 | + $h = $hi * 2328.0 + $q; // (10 ^ 13) / (1 << 32) = 2328 |
|
158 | + |
|
159 | + if ($v < 0) |
|
160 | + { |
|
161 | + if ($l == 0) |
|
162 | + $h = 4294967296.0 - $h; |
|
163 | + else |
|
164 | + { |
|
165 | + $h = 4294967295.0 - $h; |
|
166 | + $l = 4294967296.0 - $l; |
|
167 | + } |
|
168 | + } |
|
169 | + return pack("NN", $h, $l); |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /// pack 64-bit unsigned |
173 | 173 | function sphPackU64($v) |
174 | 174 | { |
175 | - assert(is_numeric($v)); |
|
176 | - |
|
177 | - // x64 |
|
178 | - if (PHP_INT_SIZE >= 8) |
|
179 | - { |
|
180 | - assert($v >= 0); |
|
181 | - |
|
182 | - // x64, int |
|
183 | - if (is_int($v)) |
|
184 | - return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
185 | - |
|
186 | - // x64, bcmath |
|
187 | - if (function_exists("bcmul")) |
|
188 | - { |
|
189 | - $h = bcdiv($v, 4294967296, 0); |
|
190 | - $l = bcmod($v, 4294967296); |
|
191 | - return pack("NN", $h, $l); |
|
192 | - } |
|
193 | - |
|
194 | - // x64, no-bcmath |
|
195 | - $p = max(0, strlen($v) - 13); |
|
196 | - $lo = (int) substr($v, $p); |
|
197 | - $hi = (int) substr($v, 0, $p); |
|
198 | - |
|
199 | - $m = $lo + $hi * 1316134912; |
|
200 | - $l = $m % 4294967296; |
|
201 | - $h = $hi * 2328 + (int) ($m / 4294967296); |
|
202 | - |
|
203 | - return pack("NN", $h, $l); |
|
204 | - } |
|
205 | - |
|
206 | - // x32, int |
|
207 | - if (is_int($v)) |
|
208 | - return pack("NN", 0, $v); |
|
209 | - |
|
210 | - // x32, bcmath |
|
211 | - if (function_exists("bcmul")) |
|
212 | - { |
|
213 | - $h = bcdiv($v, "4294967296", 0); |
|
214 | - $l = bcmod($v, "4294967296"); |
|
215 | - return pack("NN", (float) $h, (float) $l); // conversion to float is intentional; int would lose 31st bit |
|
216 | - } |
|
217 | - |
|
218 | - // x32, no-bcmath |
|
219 | - $p = max(0, strlen($v) - 13); |
|
220 | - $lo = (float) substr($v, $p); |
|
221 | - $hi = (float) substr($v, 0, $p); |
|
222 | - |
|
223 | - $m = $lo + $hi * 1316134912.0; |
|
224 | - $q = floor($m / 4294967296.0); |
|
225 | - $l = $m - ($q * 4294967296.0); |
|
226 | - $h = $hi * 2328.0 + $q; |
|
227 | - |
|
228 | - return pack("NN", $h, $l); |
|
175 | + assert(is_numeric($v)); |
|
176 | + |
|
177 | + // x64 |
|
178 | + if (PHP_INT_SIZE >= 8) |
|
179 | + { |
|
180 | + assert($v >= 0); |
|
181 | + |
|
182 | + // x64, int |
|
183 | + if (is_int($v)) |
|
184 | + return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
185 | + |
|
186 | + // x64, bcmath |
|
187 | + if (function_exists("bcmul")) |
|
188 | + { |
|
189 | + $h = bcdiv($v, 4294967296, 0); |
|
190 | + $l = bcmod($v, 4294967296); |
|
191 | + return pack("NN", $h, $l); |
|
192 | + } |
|
193 | + |
|
194 | + // x64, no-bcmath |
|
195 | + $p = max(0, strlen($v) - 13); |
|
196 | + $lo = (int) substr($v, $p); |
|
197 | + $hi = (int) substr($v, 0, $p); |
|
198 | + |
|
199 | + $m = $lo + $hi * 1316134912; |
|
200 | + $l = $m % 4294967296; |
|
201 | + $h = $hi * 2328 + (int) ($m / 4294967296); |
|
202 | + |
|
203 | + return pack("NN", $h, $l); |
|
204 | + } |
|
205 | + |
|
206 | + // x32, int |
|
207 | + if (is_int($v)) |
|
208 | + return pack("NN", 0, $v); |
|
209 | + |
|
210 | + // x32, bcmath |
|
211 | + if (function_exists("bcmul")) |
|
212 | + { |
|
213 | + $h = bcdiv($v, "4294967296", 0); |
|
214 | + $l = bcmod($v, "4294967296"); |
|
215 | + return pack("NN", (float) $h, (float) $l); // conversion to float is intentional; int would lose 31st bit |
|
216 | + } |
|
217 | + |
|
218 | + // x32, no-bcmath |
|
219 | + $p = max(0, strlen($v) - 13); |
|
220 | + $lo = (float) substr($v, $p); |
|
221 | + $hi = (float) substr($v, 0, $p); |
|
222 | + |
|
223 | + $m = $lo + $hi * 1316134912.0; |
|
224 | + $q = floor($m / 4294967296.0); |
|
225 | + $l = $m - ($q * 4294967296.0); |
|
226 | + $h = $hi * 2328.0 + $q; |
|
227 | + |
|
228 | + return pack("NN", $h, $l); |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | // unpack 64-bit unsigned |
232 | 232 | function sphUnpackU64($v) |
233 | 233 | { |
234 | - list ($hi, $lo) = array_values(unpack("N*N*", $v)); |
|
235 | - |
|
236 | - if (PHP_INT_SIZE >= 8) |
|
237 | - { |
|
238 | - if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
239 | - if ($lo < 0) $lo += (1 << 32); |
|
240 | - |
|
241 | - // x64, int |
|
242 | - if ($hi <= 2147483647) |
|
243 | - return ($hi << 32) + $lo; |
|
244 | - |
|
245 | - // x64, bcmath |
|
246 | - if (function_exists("bcmul")) |
|
247 | - return bcadd($lo, bcmul($hi, "4294967296")); |
|
248 | - |
|
249 | - // x64, no-bcmath |
|
250 | - $C = 100000; |
|
251 | - $h = ((int) ($hi / $C) << 32) + (int) ($lo / $C); |
|
252 | - $l = (($hi % $C) << 32) + ($lo % $C); |
|
253 | - if ($l > $C) |
|
254 | - { |
|
255 | - $h += (int) ($l / $C); |
|
256 | - $l = $l % $C; |
|
257 | - } |
|
258 | - |
|
259 | - if ($h == 0) |
|
260 | - return $l; |
|
261 | - return sprintf("%d%05d", $h, $l); |
|
262 | - } |
|
263 | - |
|
264 | - // x32, int |
|
265 | - if ($hi == 0) |
|
266 | - { |
|
267 | - if ($lo > 0) |
|
268 | - return $lo; |
|
269 | - return sprintf("%u", $lo); |
|
270 | - } |
|
271 | - |
|
272 | - $hi = sprintf("%u", $hi); |
|
273 | - $lo = sprintf("%u", $lo); |
|
274 | - |
|
275 | - // x32, bcmath |
|
276 | - if (function_exists("bcmul")) |
|
277 | - return bcadd($lo, bcmul($hi, "4294967296")); |
|
278 | - |
|
279 | - // x32, no-bcmath |
|
280 | - $hi = (float) $hi; |
|
281 | - $lo = (float) $lo; |
|
282 | - |
|
283 | - $q = floor($hi / 10000000.0); |
|
284 | - $r = $hi - $q * 10000000.0; |
|
285 | - $m = $lo + $r * 4967296.0; |
|
286 | - $mq = floor($m / 10000000.0); |
|
287 | - $l = $m - $mq * 10000000.0; |
|
288 | - $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
289 | - |
|
290 | - $h = sprintf("%.0f", $h); |
|
291 | - $l = sprintf("%07.0f", $l); |
|
292 | - if ($h == "0") |
|
293 | - return sprintf("%.0f", (float) $l); |
|
294 | - return $h.$l; |
|
234 | + list ($hi, $lo) = array_values(unpack("N*N*", $v)); |
|
235 | + |
|
236 | + if (PHP_INT_SIZE >= 8) |
|
237 | + { |
|
238 | + if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
239 | + if ($lo < 0) $lo += (1 << 32); |
|
240 | + |
|
241 | + // x64, int |
|
242 | + if ($hi <= 2147483647) |
|
243 | + return ($hi << 32) + $lo; |
|
244 | + |
|
245 | + // x64, bcmath |
|
246 | + if (function_exists("bcmul")) |
|
247 | + return bcadd($lo, bcmul($hi, "4294967296")); |
|
248 | + |
|
249 | + // x64, no-bcmath |
|
250 | + $C = 100000; |
|
251 | + $h = ((int) ($hi / $C) << 32) + (int) ($lo / $C); |
|
252 | + $l = (($hi % $C) << 32) + ($lo % $C); |
|
253 | + if ($l > $C) |
|
254 | + { |
|
255 | + $h += (int) ($l / $C); |
|
256 | + $l = $l % $C; |
|
257 | + } |
|
258 | + |
|
259 | + if ($h == 0) |
|
260 | + return $l; |
|
261 | + return sprintf("%d%05d", $h, $l); |
|
262 | + } |
|
263 | + |
|
264 | + // x32, int |
|
265 | + if ($hi == 0) |
|
266 | + { |
|
267 | + if ($lo > 0) |
|
268 | + return $lo; |
|
269 | + return sprintf("%u", $lo); |
|
270 | + } |
|
271 | + |
|
272 | + $hi = sprintf("%u", $hi); |
|
273 | + $lo = sprintf("%u", $lo); |
|
274 | + |
|
275 | + // x32, bcmath |
|
276 | + if (function_exists("bcmul")) |
|
277 | + return bcadd($lo, bcmul($hi, "4294967296")); |
|
278 | + |
|
279 | + // x32, no-bcmath |
|
280 | + $hi = (float) $hi; |
|
281 | + $lo = (float) $lo; |
|
282 | + |
|
283 | + $q = floor($hi / 10000000.0); |
|
284 | + $r = $hi - $q * 10000000.0; |
|
285 | + $m = $lo + $r * 4967296.0; |
|
286 | + $mq = floor($m / 10000000.0); |
|
287 | + $l = $m - $mq * 10000000.0; |
|
288 | + $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
289 | + |
|
290 | + $h = sprintf("%.0f", $h); |
|
291 | + $l = sprintf("%07.0f", $l); |
|
292 | + if ($h == "0") |
|
293 | + return sprintf("%.0f", (float) $l); |
|
294 | + return $h.$l; |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | // unpack 64-bit signed |
298 | 298 | function sphUnpackI64($v) |
299 | 299 | { |
300 | - list ($hi, $lo) = array_values(unpack("N*N*", $v)); |
|
301 | - |
|
302 | - // x64 |
|
303 | - if (PHP_INT_SIZE >= 8) |
|
304 | - { |
|
305 | - if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
306 | - if ($lo < 0) $lo += (1 << 32); |
|
307 | - |
|
308 | - return ($hi << 32) + $lo; |
|
309 | - } |
|
310 | - |
|
311 | - // x32, int |
|
312 | - if ($hi == 0) |
|
313 | - { |
|
314 | - if ($lo > 0) |
|
315 | - return $lo; |
|
316 | - return sprintf("%u", $lo); |
|
317 | - } |
|
318 | - // x32, int |
|
319 | - elseif ($hi == -1) |
|
320 | - { |
|
321 | - if ($lo < 0) |
|
322 | - return $lo; |
|
323 | - return sprintf("%.0f", $lo - 4294967296.0); |
|
324 | - } |
|
325 | - |
|
326 | - $neg = ""; |
|
327 | - $c = 0; |
|
328 | - if ($hi < 0) |
|
329 | - { |
|
330 | - $hi = ~$hi; |
|
331 | - $lo = ~$lo; |
|
332 | - $c = 1; |
|
333 | - $neg = "-"; |
|
334 | - } |
|
335 | - |
|
336 | - $hi = sprintf("%u", $hi); |
|
337 | - $lo = sprintf("%u", $lo); |
|
338 | - |
|
339 | - // x32, bcmath |
|
340 | - if (function_exists("bcmul")) |
|
341 | - return $neg.bcadd(bcadd($lo, bcmul($hi, "4294967296")), $c); |
|
342 | - |
|
343 | - // x32, no-bcmath |
|
344 | - $hi = (float) $hi; |
|
345 | - $lo = (float) $lo; |
|
346 | - |
|
347 | - $q = floor($hi / 10000000.0); |
|
348 | - $r = $hi - $q * 10000000.0; |
|
349 | - $m = $lo + $r * 4967296.0; |
|
350 | - $mq = floor($m / 10000000.0); |
|
351 | - $l = $m - $mq * 10000000.0 + $c; |
|
352 | - $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
353 | - if ($l == 10000000) |
|
354 | - { |
|
355 | - $l = 0; |
|
356 | - $h += 1; |
|
357 | - } |
|
358 | - |
|
359 | - $h = sprintf("%.0f", $h); |
|
360 | - $l = sprintf("%07.0f", $l); |
|
361 | - if ($h == "0") |
|
362 | - return $neg.sprintf("%.0f", (float) $l); |
|
363 | - return $neg.$h.$l; |
|
300 | + list ($hi, $lo) = array_values(unpack("N*N*", $v)); |
|
301 | + |
|
302 | + // x64 |
|
303 | + if (PHP_INT_SIZE >= 8) |
|
304 | + { |
|
305 | + if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
306 | + if ($lo < 0) $lo += (1 << 32); |
|
307 | + |
|
308 | + return ($hi << 32) + $lo; |
|
309 | + } |
|
310 | + |
|
311 | + // x32, int |
|
312 | + if ($hi == 0) |
|
313 | + { |
|
314 | + if ($lo > 0) |
|
315 | + return $lo; |
|
316 | + return sprintf("%u", $lo); |
|
317 | + } |
|
318 | + // x32, int |
|
319 | + elseif ($hi == -1) |
|
320 | + { |
|
321 | + if ($lo < 0) |
|
322 | + return $lo; |
|
323 | + return sprintf("%.0f", $lo - 4294967296.0); |
|
324 | + } |
|
325 | + |
|
326 | + $neg = ""; |
|
327 | + $c = 0; |
|
328 | + if ($hi < 0) |
|
329 | + { |
|
330 | + $hi = ~$hi; |
|
331 | + $lo = ~$lo; |
|
332 | + $c = 1; |
|
333 | + $neg = "-"; |
|
334 | + } |
|
335 | + |
|
336 | + $hi = sprintf("%u", $hi); |
|
337 | + $lo = sprintf("%u", $lo); |
|
338 | + |
|
339 | + // x32, bcmath |
|
340 | + if (function_exists("bcmul")) |
|
341 | + return $neg.bcadd(bcadd($lo, bcmul($hi, "4294967296")), $c); |
|
342 | + |
|
343 | + // x32, no-bcmath |
|
344 | + $hi = (float) $hi; |
|
345 | + $lo = (float) $lo; |
|
346 | + |
|
347 | + $q = floor($hi / 10000000.0); |
|
348 | + $r = $hi - $q * 10000000.0; |
|
349 | + $m = $lo + $r * 4967296.0; |
|
350 | + $mq = floor($m / 10000000.0); |
|
351 | + $l = $m - $mq * 10000000.0 + $c; |
|
352 | + $h = $q * 4294967296.0 + $r * 429.0 + $mq; |
|
353 | + if ($l == 10000000) |
|
354 | + { |
|
355 | + $l = 0; |
|
356 | + $h += 1; |
|
357 | + } |
|
358 | + |
|
359 | + $h = sprintf("%.0f", $h); |
|
360 | + $l = sprintf("%07.0f", $l); |
|
361 | + if ($h == "0") |
|
362 | + return $neg.sprintf("%.0f", (float) $l); |
|
363 | + return $neg.$h.$l; |
|
364 | 364 | } |
365 | 365 | |
366 | 366 | |
367 | 367 | function sphFixUint($value) |
368 | 368 | { |
369 | - if (PHP_INT_SIZE >= 8) |
|
370 | - { |
|
371 | - // x64 route, workaround broken unpack() in 5.2.2+ |
|
372 | - if ($value < 0) $value += (1 << 32); |
|
373 | - return $value; |
|
374 | - } |
|
375 | - else |
|
376 | - { |
|
377 | - // x32 route, workaround php signed/unsigned braindamage |
|
378 | - return sprintf("%u", $value); |
|
379 | - } |
|
369 | + if (PHP_INT_SIZE >= 8) |
|
370 | + { |
|
371 | + // x64 route, workaround broken unpack() in 5.2.2+ |
|
372 | + if ($value < 0) $value += (1 << 32); |
|
373 | + return $value; |
|
374 | + } |
|
375 | + else |
|
376 | + { |
|
377 | + // x32 route, workaround php signed/unsigned braindamage |
|
378 | + return sprintf("%u", $value); |
|
379 | + } |
|
380 | 380 | } |
381 | 381 | |
382 | 382 | |
383 | 383 | /// sphinx searchd client class |
384 | 384 | class SphinxClient |
385 | 385 | { |
386 | - var $_host; ///< searchd host (default is "localhost") |
|
387 | - var $_port; ///< searchd port (default is 9312) |
|
388 | - var $_offset; ///< how many records to seek from result-set start (default is 0) |
|
389 | - var $_limit; ///< how many records to return from result-set starting at offset (default is 20) |
|
390 | - var $_mode; ///< query matching mode (default is SPH_MATCH_ALL) |
|
391 | - var $_weights; ///< per-field weights (default is 1 for all fields) |
|
392 | - var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE) |
|
393 | - var $_sortby; ///< attribute to sort by (defualt is "") |
|
394 | - var $_min_id; ///< min ID to match (default is 0, which means no limit) |
|
395 | - var $_max_id; ///< max ID to match (default is 0, which means no limit) |
|
396 | - var $_filters; ///< search filters |
|
397 | - var $_groupby; ///< group-by attribute name |
|
398 | - var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with) |
|
399 | - var $_groupsort; ///< group-by sorting clause (to sort groups in result set with) |
|
400 | - var $_groupdistinct; ///< group-by count-distinct attribute |
|
401 | - var $_maxmatches; ///< max matches to retrieve |
|
402 | - var $_cutoff; ///< cutoff to stop searching at (default is 0) |
|
403 | - var $_retrycount; ///< distributed retries count |
|
404 | - var $_retrydelay; ///< distributed retries delay |
|
405 | - var $_anchor; ///< geographical anchor point |
|
406 | - var $_indexweights; ///< per-index weights |
|
407 | - var $_ranker; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25) |
|
408 | - var $_maxquerytime; ///< max query time, milliseconds (default is 0, do not limit) |
|
409 | - var $_fieldweights; ///< per-field-name weights |
|
410 | - var $_overrides; ///< per-query attribute values overrides |
|
411 | - var $_select; ///< select-list (attributes or expressions, with optional aliases) |
|
412 | - |
|
413 | - var $_error; ///< last error message |
|
414 | - var $_warning; ///< last warning message |
|
415 | - var $_connerror; ///< connection error vs remote error flag |
|
416 | - |
|
417 | - var $_reqs; ///< requests array for multi-query |
|
418 | - var $_mbenc; ///< stored mbstring encoding |
|
419 | - var $_arrayresult; ///< whether $result["matches"] should be a hash or an array |
|
420 | - var $_timeout; ///< connect timeout |
|
421 | - |
|
422 | - ///////////////////////////////////////////////////////////////////////////// |
|
423 | - // common stuff |
|
424 | - ///////////////////////////////////////////////////////////////////////////// |
|
425 | - |
|
426 | - /// create a new client object and fill defaults |
|
427 | - public function __construct() |
|
428 | - { |
|
429 | - // per-client-object settings |
|
430 | - $this->_host = "localhost"; |
|
431 | - $this->_port = 9312; |
|
432 | - $this->_path = false; |
|
433 | - $this->_socket = false; |
|
434 | - |
|
435 | - // per-query settings |
|
436 | - $this->_offset = 0; |
|
437 | - $this->_limit = 20; |
|
438 | - $this->_mode = SPH_MATCH_ALL; |
|
439 | - $this->_weights = array(); |
|
440 | - $this->_sort = SPH_SORT_RELEVANCE; |
|
441 | - $this->_sortby = ""; |
|
442 | - $this->_min_id = 0; |
|
443 | - $this->_max_id = 0; |
|
444 | - $this->_filters = array(); |
|
445 | - $this->_groupby = ""; |
|
446 | - $this->_groupfunc = SPH_GROUPBY_DAY; |
|
447 | - $this->_groupsort = "@group desc"; |
|
448 | - $this->_groupdistinct = ""; |
|
449 | - $this->_maxmatches = 1000; |
|
450 | - $this->_cutoff = 0; |
|
451 | - $this->_retrycount = 0; |
|
452 | - $this->_retrydelay = 0; |
|
453 | - $this->_anchor = array(); |
|
454 | - $this->_indexweights = array(); |
|
455 | - $this->_ranker = SPH_RANK_PROXIMITY_BM25; |
|
456 | - $this->_maxquerytime = 0; |
|
457 | - $this->_fieldweights = array(); |
|
458 | - $this->_overrides = array(); |
|
459 | - $this->_select = "*"; |
|
460 | - |
|
461 | - $this->_error = ""; // per-reply fields (for single-query case) |
|
462 | - $this->_warning = ""; |
|
463 | - $this->_connerror = false; |
|
464 | - |
|
465 | - $this->_reqs = array(); // requests storage (for multi-query case) |
|
466 | - $this->_mbenc = ""; |
|
467 | - $this->_arrayresult = false; |
|
468 | - $this->_timeout = 0; |
|
469 | - } |
|
470 | - |
|
471 | - public function __destruct() |
|
472 | - { |
|
473 | - if ($this->_socket !== false) |
|
474 | - fclose($this->_socket); |
|
475 | - } |
|
476 | - |
|
477 | - /// get last error message (string) |
|
478 | - public function GetLastError() |
|
479 | - { |
|
480 | - return $this->_error; |
|
481 | - } |
|
482 | - |
|
483 | - /// get last warning message (string) |
|
484 | - public function GetLastWarning() |
|
485 | - { |
|
486 | - return $this->_warning; |
|
487 | - } |
|
488 | - |
|
489 | - /// get last error flag (to tell network connection errors from searchd errors or broken responses) |
|
490 | - public function IsConnectError() |
|
491 | - { |
|
492 | - return $this->_connerror; |
|
493 | - } |
|
494 | - |
|
495 | - /// set searchd host name (string) and port (integer) |
|
496 | - public function SetServer($host, $port = 0) |
|
497 | - { |
|
498 | - assert(is_string($host)); |
|
499 | - if ($host[0] == '/') |
|
500 | - { |
|
501 | - $this->_path = 'unix://'.$host; |
|
502 | - return; |
|
503 | - } |
|
504 | - if (substr($host, 0, 7) == "unix://") |
|
505 | - { |
|
506 | - $this->_path = $host; |
|
507 | - return; |
|
508 | - } |
|
509 | - |
|
510 | - assert(is_int($port)); |
|
511 | - $this->_host = $host; |
|
512 | - $this->_port = $port; |
|
513 | - $this->_path = ''; |
|
514 | - |
|
515 | - } |
|
516 | - |
|
517 | - /// set server connection timeout (0 to remove) |
|
518 | - public function SetConnectTimeout($timeout) |
|
519 | - { |
|
520 | - assert(is_numeric($timeout)); |
|
521 | - $this->_timeout = $timeout; |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - public function _Send($handle, $data, $length) |
|
526 | - { |
|
527 | - if (feof($handle) || fwrite($handle, $data, $length) !== $length) |
|
528 | - { |
|
529 | - $this->_error = 'connection unexpectedly closed (timed out?)'; |
|
530 | - $this->_connerror = true; |
|
531 | - return false; |
|
532 | - } |
|
533 | - return true; |
|
534 | - } |
|
535 | - |
|
536 | - ///////////////////////////////////////////////////////////////////////////// |
|
537 | - |
|
538 | - /// enter mbstring workaround mode |
|
539 | - public function _MBPush() |
|
540 | - { |
|
541 | - $this->_mbenc = ""; |
|
542 | - if (ini_get("mbstring.func_overload") & 2) |
|
543 | - { |
|
544 | - $this->_mbenc = mb_internal_encoding(); |
|
545 | - mb_internal_encoding("latin1"); |
|
546 | - } |
|
386 | + var $_host; ///< searchd host (default is "localhost") |
|
387 | + var $_port; ///< searchd port (default is 9312) |
|
388 | + var $_offset; ///< how many records to seek from result-set start (default is 0) |
|
389 | + var $_limit; ///< how many records to return from result-set starting at offset (default is 20) |
|
390 | + var $_mode; ///< query matching mode (default is SPH_MATCH_ALL) |
|
391 | + var $_weights; ///< per-field weights (default is 1 for all fields) |
|
392 | + var $_sort; ///< match sorting mode (default is SPH_SORT_RELEVANCE) |
|
393 | + var $_sortby; ///< attribute to sort by (defualt is "") |
|
394 | + var $_min_id; ///< min ID to match (default is 0, which means no limit) |
|
395 | + var $_max_id; ///< max ID to match (default is 0, which means no limit) |
|
396 | + var $_filters; ///< search filters |
|
397 | + var $_groupby; ///< group-by attribute name |
|
398 | + var $_groupfunc; ///< group-by function (to pre-process group-by attribute value with) |
|
399 | + var $_groupsort; ///< group-by sorting clause (to sort groups in result set with) |
|
400 | + var $_groupdistinct; ///< group-by count-distinct attribute |
|
401 | + var $_maxmatches; ///< max matches to retrieve |
|
402 | + var $_cutoff; ///< cutoff to stop searching at (default is 0) |
|
403 | + var $_retrycount; ///< distributed retries count |
|
404 | + var $_retrydelay; ///< distributed retries delay |
|
405 | + var $_anchor; ///< geographical anchor point |
|
406 | + var $_indexweights; ///< per-index weights |
|
407 | + var $_ranker; ///< ranking mode (default is SPH_RANK_PROXIMITY_BM25) |
|
408 | + var $_maxquerytime; ///< max query time, milliseconds (default is 0, do not limit) |
|
409 | + var $_fieldweights; ///< per-field-name weights |
|
410 | + var $_overrides; ///< per-query attribute values overrides |
|
411 | + var $_select; ///< select-list (attributes or expressions, with optional aliases) |
|
412 | + |
|
413 | + var $_error; ///< last error message |
|
414 | + var $_warning; ///< last warning message |
|
415 | + var $_connerror; ///< connection error vs remote error flag |
|
416 | + |
|
417 | + var $_reqs; ///< requests array for multi-query |
|
418 | + var $_mbenc; ///< stored mbstring encoding |
|
419 | + var $_arrayresult; ///< whether $result["matches"] should be a hash or an array |
|
420 | + var $_timeout; ///< connect timeout |
|
421 | + |
|
422 | + ///////////////////////////////////////////////////////////////////////////// |
|
423 | + // common stuff |
|
424 | + ///////////////////////////////////////////////////////////////////////////// |
|
425 | + |
|
426 | + /// create a new client object and fill defaults |
|
427 | + public function __construct() |
|
428 | + { |
|
429 | + // per-client-object settings |
|
430 | + $this->_host = "localhost"; |
|
431 | + $this->_port = 9312; |
|
432 | + $this->_path = false; |
|
433 | + $this->_socket = false; |
|
434 | + |
|
435 | + // per-query settings |
|
436 | + $this->_offset = 0; |
|
437 | + $this->_limit = 20; |
|
438 | + $this->_mode = SPH_MATCH_ALL; |
|
439 | + $this->_weights = array(); |
|
440 | + $this->_sort = SPH_SORT_RELEVANCE; |
|
441 | + $this->_sortby = ""; |
|
442 | + $this->_min_id = 0; |
|
443 | + $this->_max_id = 0; |
|
444 | + $this->_filters = array(); |
|
445 | + $this->_groupby = ""; |
|
446 | + $this->_groupfunc = SPH_GROUPBY_DAY; |
|
447 | + $this->_groupsort = "@group desc"; |
|
448 | + $this->_groupdistinct = ""; |
|
449 | + $this->_maxmatches = 1000; |
|
450 | + $this->_cutoff = 0; |
|
451 | + $this->_retrycount = 0; |
|
452 | + $this->_retrydelay = 0; |
|
453 | + $this->_anchor = array(); |
|
454 | + $this->_indexweights = array(); |
|
455 | + $this->_ranker = SPH_RANK_PROXIMITY_BM25; |
|
456 | + $this->_maxquerytime = 0; |
|
457 | + $this->_fieldweights = array(); |
|
458 | + $this->_overrides = array(); |
|
459 | + $this->_select = "*"; |
|
460 | + |
|
461 | + $this->_error = ""; // per-reply fields (for single-query case) |
|
462 | + $this->_warning = ""; |
|
463 | + $this->_connerror = false; |
|
464 | + |
|
465 | + $this->_reqs = array(); // requests storage (for multi-query case) |
|
466 | + $this->_mbenc = ""; |
|
467 | + $this->_arrayresult = false; |
|
468 | + $this->_timeout = 0; |
|
469 | + } |
|
470 | + |
|
471 | + public function __destruct() |
|
472 | + { |
|
473 | + if ($this->_socket !== false) |
|
474 | + fclose($this->_socket); |
|
475 | + } |
|
476 | + |
|
477 | + /// get last error message (string) |
|
478 | + public function GetLastError() |
|
479 | + { |
|
480 | + return $this->_error; |
|
481 | + } |
|
482 | + |
|
483 | + /// get last warning message (string) |
|
484 | + public function GetLastWarning() |
|
485 | + { |
|
486 | + return $this->_warning; |
|
487 | + } |
|
488 | + |
|
489 | + /// get last error flag (to tell network connection errors from searchd errors or broken responses) |
|
490 | + public function IsConnectError() |
|
491 | + { |
|
492 | + return $this->_connerror; |
|
493 | + } |
|
494 | + |
|
495 | + /// set searchd host name (string) and port (integer) |
|
496 | + public function SetServer($host, $port = 0) |
|
497 | + { |
|
498 | + assert(is_string($host)); |
|
499 | + if ($host[0] == '/') |
|
500 | + { |
|
501 | + $this->_path = 'unix://'.$host; |
|
502 | + return; |
|
503 | + } |
|
504 | + if (substr($host, 0, 7) == "unix://") |
|
505 | + { |
|
506 | + $this->_path = $host; |
|
507 | + return; |
|
508 | + } |
|
509 | + |
|
510 | + assert(is_int($port)); |
|
511 | + $this->_host = $host; |
|
512 | + $this->_port = $port; |
|
513 | + $this->_path = ''; |
|
514 | + |
|
515 | + } |
|
516 | + |
|
517 | + /// set server connection timeout (0 to remove) |
|
518 | + public function SetConnectTimeout($timeout) |
|
519 | + { |
|
520 | + assert(is_numeric($timeout)); |
|
521 | + $this->_timeout = $timeout; |
|
522 | + } |
|
523 | + |
|
524 | + |
|
525 | + public function _Send($handle, $data, $length) |
|
526 | + { |
|
527 | + if (feof($handle) || fwrite($handle, $data, $length) !== $length) |
|
528 | + { |
|
529 | + $this->_error = 'connection unexpectedly closed (timed out?)'; |
|
530 | + $this->_connerror = true; |
|
531 | + return false; |
|
532 | + } |
|
533 | + return true; |
|
534 | + } |
|
535 | + |
|
536 | + ///////////////////////////////////////////////////////////////////////////// |
|
537 | + |
|
538 | + /// enter mbstring workaround mode |
|
539 | + public function _MBPush() |
|
540 | + { |
|
541 | + $this->_mbenc = ""; |
|
542 | + if (ini_get("mbstring.func_overload") & 2) |
|
543 | + { |
|
544 | + $this->_mbenc = mb_internal_encoding(); |
|
545 | + mb_internal_encoding("latin1"); |
|
546 | + } |
|
547 | + } |
|
548 | + |
|
549 | + /// leave mbstring workaround mode |
|
550 | + public function _MBPop() |
|
551 | + { |
|
552 | + if ($this->_mbenc) |
|
553 | + mb_internal_encoding($this->_mbenc); |
|
554 | + } |
|
555 | + |
|
556 | + /// connect to searchd server |
|
557 | + public function _Connect() |
|
558 | + { |
|
559 | + if ($this->_socket !== false) |
|
560 | + { |
|
561 | + // we are in persistent connection mode, so we have a socket |
|
562 | + // however, need to check whether it's still alive |
|
563 | + if (!@feof($this->_socket)) |
|
564 | + return $this->_socket; |
|
565 | + |
|
566 | + // force reopen |
|
567 | + $this->_socket = false; |
|
568 | + } |
|
569 | + |
|
570 | + $errno = 0; |
|
571 | + $errstr = ""; |
|
572 | + $this->_connerror = false; |
|
573 | + |
|
574 | + if ($this->_path) |
|
575 | + { |
|
576 | + $host = $this->_path; |
|
577 | + $port = 0; |
|
578 | + } |
|
579 | + else |
|
580 | + { |
|
581 | + $host = $this->_host; |
|
582 | + $port = $this->_port; |
|
583 | + } |
|
584 | + |
|
585 | + if ($this->_timeout <= 0) |
|
586 | + $fp = @fsockopen($host, $port, $errno, $errstr); |
|
587 | + else |
|
588 | + $fp = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); |
|
589 | + |
|
590 | + if (!$fp) |
|
591 | + { |
|
592 | + if ($this->_path) |
|
593 | + $location = $this->_path; |
|
594 | + else |
|
595 | + $location = "{$this->_host}:{$this->_port}"; |
|
596 | + |
|
597 | + $errstr = trim($errstr); |
|
598 | + $this->_error = "connection to $location failed (errno=$errno, msg=$errstr)"; |
|
599 | + $this->_connerror = true; |
|
600 | + return false; |
|
601 | + } |
|
602 | + |
|
603 | + // send my version |
|
604 | + // this is a subtle part. we must do it before (!) reading back from searchd. |
|
605 | + // because otherwise under some conditions (reported on FreeBSD for instance) |
|
606 | + // TCP stack could throttle write-write-read pattern because of Nagle. |
|
607 | + if (!$this->_Send($fp, pack("N", 1), 4)) |
|
608 | + { |
|
609 | + fclose($fp); |
|
610 | + $this->_error = "failed to send client protocol version"; |
|
611 | + return false; |
|
612 | + } |
|
613 | + |
|
614 | + // check version |
|
615 | + list(,$v) = unpack("N*", fread($fp, 4)); |
|
616 | + $v = (int) $v; |
|
617 | + if ($v < 1) |
|
618 | + { |
|
619 | + fclose($fp); |
|
620 | + $this->_error = "expected searchd protocol version 1+, got version '$v'"; |
|
621 | + return false; |
|
622 | + } |
|
623 | + |
|
624 | + return $fp; |
|
625 | + } |
|
626 | + |
|
627 | + /// get and check response packet from searchd server |
|
628 | + public function _GetResponse($fp, $client_ver) |
|
629 | + { |
|
630 | + $response = ""; |
|
631 | + $len = 0; |
|
632 | + |
|
633 | + $header = fread($fp, 8); |
|
634 | + if (strlen($header) == 8) |
|
635 | + { |
|
636 | + list ($status, $ver, $len) = array_values(unpack("n2a/Nb", $header)); |
|
637 | + $left = $len; |
|
638 | + while ($left > 0 && !feof($fp)) |
|
639 | + { |
|
640 | + $chunk = fread($fp, min(8192, $left)); |
|
641 | + if ($chunk) |
|
642 | + { |
|
643 | + $response .= $chunk; |
|
644 | + $left -= strlen($chunk); |
|
645 | + } |
|
646 | + } |
|
647 | + } |
|
648 | + if ($this->_socket === false) |
|
649 | + fclose($fp); |
|
650 | + |
|
651 | + // check response |
|
652 | + $read = strlen($response); |
|
653 | + if (!$response || $read != $len) |
|
654 | + { |
|
655 | + $this->_error = $len |
|
656 | + ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)" |
|
657 | + : "received zero-sized searchd response"; |
|
658 | + return false; |
|
659 | + } |
|
660 | + |
|
661 | + // check status |
|
662 | + if ($status == SEARCHD_WARNING) |
|
663 | + { |
|
664 | + list(,$wlen) = unpack("N*", substr($response, 0, 4)); |
|
665 | + $this->_warning = substr($response, 4, $wlen); |
|
666 | + return substr($response, 4 + $wlen); |
|
667 | + } |
|
668 | + if ($status == SEARCHD_ERROR) |
|
669 | + { |
|
670 | + $this->_error = "searchd error: ".substr($response, 4); |
|
671 | + return false; |
|
672 | + } |
|
673 | + if ($status == SEARCHD_RETRY) |
|
674 | + { |
|
675 | + $this->_error = "temporary searchd error: ".substr($response, 4); |
|
676 | + return false; |
|
677 | + } |
|
678 | + if ($status != SEARCHD_OK) |
|
679 | + { |
|
680 | + $this->_error = "unknown status code '$status'"; |
|
681 | + return false; |
|
682 | + } |
|
683 | + |
|
684 | + // check version |
|
685 | + if ($ver < $client_ver) |
|
686 | + { |
|
687 | + $this->_warning = sprintf("searchd command v.%d.%d older than client's v.%d.%d, some options might not work", |
|
688 | + $ver >> 8, $ver & 0xff, $client_ver >> 8, $client_ver & 0xff); |
|
689 | + } |
|
690 | + |
|
691 | + return $response; |
|
692 | + } |
|
693 | + |
|
694 | + ///////////////////////////////////////////////////////////////////////////// |
|
695 | + // searching |
|
696 | + ///////////////////////////////////////////////////////////////////////////// |
|
697 | + |
|
698 | + /// set offset and count into result set, |
|
699 | + /// and optionally set max-matches and cutoff limits |
|
700 | + public function SetLimits($offset, $limit, $max = 0, $cutoff = 0) |
|
701 | + { |
|
702 | + assert(is_int($offset)); |
|
703 | + assert(is_int($limit)); |
|
704 | + assert($offset >= 0); |
|
705 | + assert($limit > 0); |
|
706 | + assert($max >= 0); |
|
707 | + $this->_offset = $offset; |
|
708 | + $this->_limit = $limit; |
|
709 | + if ($max > 0) |
|
710 | + $this->_maxmatches = $max; |
|
711 | + if ($cutoff > 0) |
|
712 | + $this->_cutoff = $cutoff; |
|
713 | + } |
|
714 | + |
|
715 | + /// set maximum query time, in milliseconds, per-index |
|
716 | + /// integer, 0 means "do not limit" |
|
717 | + public function SetMaxQueryTime($max) |
|
718 | + { |
|
719 | + assert(is_int($max)); |
|
720 | + assert($max >= 0); |
|
721 | + $this->_maxquerytime = $max; |
|
722 | + } |
|
723 | + |
|
724 | + /// set matching mode |
|
725 | + public function SetMatchMode($mode) |
|
726 | + { |
|
727 | + assert($mode == SPH_MATCH_ALL |
|
728 | + || $mode == SPH_MATCH_ANY |
|
729 | + || $mode == SPH_MATCH_PHRASE |
|
730 | + || $mode == SPH_MATCH_BOOLEAN |
|
731 | + || $mode == SPH_MATCH_EXTENDED |
|
732 | + || $mode == SPH_MATCH_FULLSCAN |
|
733 | + || $mode == SPH_MATCH_EXTENDED2); |
|
734 | + $this->_mode = $mode; |
|
735 | + } |
|
736 | + |
|
737 | + /// set ranking mode |
|
738 | + public function SetRankingMode($ranker) |
|
739 | + { |
|
740 | + assert($ranker >= 0 && $ranker < SPH_RANK_TOTAL); |
|
741 | + $this->_ranker = $ranker; |
|
742 | + } |
|
743 | + |
|
744 | + /// set matches sorting mode |
|
745 | + public function SetSortMode($mode, $sortby = "") |
|
746 | + { |
|
747 | + assert( |
|
748 | + $mode == SPH_SORT_RELEVANCE || |
|
749 | + $mode == SPH_SORT_ATTR_DESC || |
|
750 | + $mode == SPH_SORT_ATTR_ASC || |
|
751 | + $mode == SPH_SORT_TIME_SEGMENTS || |
|
752 | + $mode == SPH_SORT_EXTENDED || |
|
753 | + $mode == SPH_SORT_EXPR ); |
|
754 | + assert(is_string($sortby)); |
|
755 | + assert($mode == SPH_SORT_RELEVANCE || strlen($sortby) > 0); |
|
756 | + |
|
757 | + $this->_sort = $mode; |
|
758 | + $this->_sortby = $sortby; |
|
759 | + } |
|
760 | + |
|
761 | + /// bind per-field weights by order |
|
762 | + /// DEPRECATED; use SetFieldWeights() instead |
|
763 | + public function SetWeights($weights) |
|
764 | + { |
|
765 | + assert(is_array($weights)); |
|
766 | + foreach ($weights as $weight) |
|
767 | + assert(is_int($weight)); |
|
768 | + |
|
769 | + $this->_weights = $weights; |
|
770 | + } |
|
771 | + |
|
772 | + /// bind per-field weights by name |
|
773 | + public function SetFieldWeights($weights) |
|
774 | + { |
|
775 | + assert(is_array($weights)); |
|
776 | + foreach ($weights as $name=>$weight) |
|
777 | + { |
|
778 | + assert(is_string($name)); |
|
779 | + assert(is_int($weight)); |
|
780 | + } |
|
781 | + $this->_fieldweights = $weights; |
|
782 | + } |
|
783 | + |
|
784 | + /// bind per-index weights by name |
|
785 | + public function SetIndexWeights($weights) |
|
786 | + { |
|
787 | + assert(is_array($weights)); |
|
788 | + foreach ($weights as $index=>$weight) |
|
789 | + { |
|
790 | + assert(is_string($index)); |
|
791 | + assert(is_int($weight)); |
|
792 | + } |
|
793 | + $this->_indexweights = $weights; |
|
794 | + } |
|
795 | + |
|
796 | + /// set IDs range to match |
|
797 | + /// only match records if document ID is beetwen $min and $max (inclusive) |
|
798 | + public function SetIDRange($min, $max) |
|
799 | + { |
|
800 | + assert(is_numeric($min)); |
|
801 | + assert(is_numeric($max)); |
|
802 | + assert($min <= $max); |
|
803 | + $this->_min_id = $min; |
|
804 | + $this->_max_id = $max; |
|
805 | + } |
|
806 | + |
|
807 | + /// set values set filter |
|
808 | + /// only match records where $attribute value is in given set |
|
809 | + public function SetFilter($attribute, $values, $exclude = false) |
|
810 | + { |
|
811 | + assert(is_string($attribute)); |
|
812 | + assert(is_array($values)); |
|
813 | + assert(count($values)); |
|
814 | + |
|
815 | + if (is_array($values) && count($values)) |
|
816 | + { |
|
817 | + foreach ($values as $value) |
|
818 | + assert(is_numeric($value)); |
|
819 | + |
|
820 | + $this->_filters[] = array("type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values); |
|
821 | + } |
|
822 | + } |
|
823 | + |
|
824 | + /// set range filter |
|
825 | + /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
826 | + public function SetFilterRange($attribute, $min, $max, $exclude = false) |
|
827 | + { |
|
828 | + assert(is_string($attribute)); |
|
829 | + assert(is_numeric($min)); |
|
830 | + assert(is_numeric($max)); |
|
831 | + assert($min <= $max); |
|
832 | + |
|
833 | + $this->_filters[] = array("type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max); |
|
834 | + } |
|
835 | + |
|
836 | + /// set float range filter |
|
837 | + /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
838 | + public function SetFilterFloatRange($attribute, $min, $max, $exclude = false) |
|
839 | + { |
|
840 | + assert(is_string($attribute)); |
|
841 | + assert(is_float($min)); |
|
842 | + assert(is_float($max)); |
|
843 | + assert($min <= $max); |
|
844 | + |
|
845 | + $this->_filters[] = array("type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max); |
|
846 | + } |
|
847 | + |
|
848 | + /// setup anchor point for geosphere distance calculations |
|
849 | + /// required to use @geodist in filters and sorting |
|
850 | + /// latitude and longitude must be in radians |
|
851 | + public function SetGeoAnchor($attrlat, $attrlong, $lat, $long) |
|
852 | + { |
|
853 | + assert(is_string($attrlat)); |
|
854 | + assert(is_string($attrlong)); |
|
855 | + assert(is_float($lat)); |
|
856 | + assert(is_float($long)); |
|
857 | + |
|
858 | + $this->_anchor = array("attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long); |
|
859 | + } |
|
860 | + |
|
861 | + /// set grouping attribute and function |
|
862 | + public function SetGroupBy($attribute, $func, $groupsort = "@group desc") |
|
863 | + { |
|
864 | + assert(is_string($attribute)); |
|
865 | + assert(is_string($groupsort)); |
|
866 | + assert($func == SPH_GROUPBY_DAY |
|
867 | + || $func == SPH_GROUPBY_WEEK |
|
868 | + || $func == SPH_GROUPBY_MONTH |
|
869 | + || $func == SPH_GROUPBY_YEAR |
|
870 | + || $func == SPH_GROUPBY_ATTR |
|
871 | + || $func == SPH_GROUPBY_ATTRPAIR); |
|
872 | + |
|
873 | + $this->_groupby = $attribute; |
|
874 | + $this->_groupfunc = $func; |
|
875 | + $this->_groupsort = $groupsort; |
|
876 | + } |
|
877 | + |
|
878 | + /// set count-distinct attribute for group-by queries |
|
879 | + public function SetGroupDistinct($attribute) |
|
880 | + { |
|
881 | + assert(is_string($attribute)); |
|
882 | + $this->_groupdistinct = $attribute; |
|
883 | + } |
|
884 | + |
|
885 | + /// set distributed retries count and delay |
|
886 | + public function SetRetries($count, $delay = 0) |
|
887 | + { |
|
888 | + assert(is_int($count) && $count >= 0); |
|
889 | + assert(is_int($delay) && $delay >= 0); |
|
890 | + $this->_retrycount = $count; |
|
891 | + $this->_retrydelay = $delay; |
|
892 | + } |
|
893 | + |
|
894 | + /// set result set format (hash or array; hash by default) |
|
895 | + /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs |
|
896 | + public function SetArrayResult($arrayresult) |
|
897 | + { |
|
898 | + assert(is_bool($arrayresult)); |
|
899 | + $this->_arrayresult = $arrayresult; |
|
900 | + } |
|
901 | + |
|
902 | + /// set attribute values override |
|
903 | + /// there can be only one override per attribute |
|
904 | + /// $values must be a hash that maps document IDs to attribute values |
|
905 | + public function SetOverride($attrname, $attrtype, $values) |
|
906 | + { |
|
907 | + assert(is_string($attrname)); |
|
908 | + assert(in_array($attrtype, array(SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT))); |
|
909 | + assert(is_array($values)); |
|
910 | + |
|
911 | + $this->_overrides[$attrname] = array("attr"=>$attrname, "type"=>$attrtype, "values"=>$values); |
|
912 | + } |
|
913 | + |
|
914 | + /// set select-list (attributes or expressions), SQL-like syntax |
|
915 | + public function SetSelect($select) |
|
916 | + { |
|
917 | + assert(is_string($select)); |
|
918 | + $this->_select = $select; |
|
919 | + } |
|
920 | + |
|
921 | + ////////////////////////////////////////////////////////////////////////////// |
|
922 | + |
|
923 | + /// clear all filters (for multi-queries) |
|
924 | + public function ResetFilters() |
|
925 | + { |
|
926 | + $this->_filters = array(); |
|
927 | + $this->_anchor = array(); |
|
928 | + } |
|
929 | + |
|
930 | + /// clear groupby settings (for multi-queries) |
|
931 | + public function ResetGroupBy() |
|
932 | + { |
|
933 | + $this->_groupby = ""; |
|
934 | + $this->_groupfunc = SPH_GROUPBY_DAY; |
|
935 | + $this->_groupsort = "@group desc"; |
|
936 | + $this->_groupdistinct = ""; |
|
547 | 937 | } |
548 | 938 | |
549 | - /// leave mbstring workaround mode |
|
550 | - public function _MBPop() |
|
551 | - { |
|
552 | - if ($this->_mbenc) |
|
553 | - mb_internal_encoding($this->_mbenc); |
|
554 | - } |
|
555 | - |
|
556 | - /// connect to searchd server |
|
557 | - public function _Connect() |
|
558 | - { |
|
559 | - if ($this->_socket !== false) |
|
560 | - { |
|
561 | - // we are in persistent connection mode, so we have a socket |
|
562 | - // however, need to check whether it's still alive |
|
563 | - if (!@feof($this->_socket)) |
|
564 | - return $this->_socket; |
|
565 | - |
|
566 | - // force reopen |
|
567 | - $this->_socket = false; |
|
568 | - } |
|
569 | - |
|
570 | - $errno = 0; |
|
571 | - $errstr = ""; |
|
572 | - $this->_connerror = false; |
|
573 | - |
|
574 | - if ($this->_path) |
|
575 | - { |
|
576 | - $host = $this->_path; |
|
577 | - $port = 0; |
|
578 | - } |
|
579 | - else |
|
580 | - { |
|
581 | - $host = $this->_host; |
|
582 | - $port = $this->_port; |
|
583 | - } |
|
584 | - |
|
585 | - if ($this->_timeout <= 0) |
|
586 | - $fp = @fsockopen($host, $port, $errno, $errstr); |
|
587 | - else |
|
588 | - $fp = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); |
|
589 | - |
|
590 | - if (!$fp) |
|
591 | - { |
|
592 | - if ($this->_path) |
|
593 | - $location = $this->_path; |
|
594 | - else |
|
595 | - $location = "{$this->_host}:{$this->_port}"; |
|
596 | - |
|
597 | - $errstr = trim($errstr); |
|
598 | - $this->_error = "connection to $location failed (errno=$errno, msg=$errstr)"; |
|
599 | - $this->_connerror = true; |
|
600 | - return false; |
|
601 | - } |
|
602 | - |
|
603 | - // send my version |
|
604 | - // this is a subtle part. we must do it before (!) reading back from searchd. |
|
605 | - // because otherwise under some conditions (reported on FreeBSD for instance) |
|
606 | - // TCP stack could throttle write-write-read pattern because of Nagle. |
|
607 | - if (!$this->_Send($fp, pack("N", 1), 4)) |
|
608 | - { |
|
609 | - fclose($fp); |
|
610 | - $this->_error = "failed to send client protocol version"; |
|
611 | - return false; |
|
612 | - } |
|
613 | - |
|
614 | - // check version |
|
615 | - list(,$v) = unpack("N*", fread($fp, 4)); |
|
616 | - $v = (int) $v; |
|
617 | - if ($v < 1) |
|
618 | - { |
|
619 | - fclose($fp); |
|
620 | - $this->_error = "expected searchd protocol version 1+, got version '$v'"; |
|
621 | - return false; |
|
622 | - } |
|
623 | - |
|
624 | - return $fp; |
|
625 | - } |
|
626 | - |
|
627 | - /// get and check response packet from searchd server |
|
628 | - public function _GetResponse($fp, $client_ver) |
|
629 | - { |
|
630 | - $response = ""; |
|
631 | - $len = 0; |
|
632 | - |
|
633 | - $header = fread($fp, 8); |
|
634 | - if (strlen($header) == 8) |
|
635 | - { |
|
636 | - list ($status, $ver, $len) = array_values(unpack("n2a/Nb", $header)); |
|
637 | - $left = $len; |
|
638 | - while ($left > 0 && !feof($fp)) |
|
639 | - { |
|
640 | - $chunk = fread($fp, min(8192, $left)); |
|
641 | - if ($chunk) |
|
642 | - { |
|
643 | - $response .= $chunk; |
|
644 | - $left -= strlen($chunk); |
|
645 | - } |
|
646 | - } |
|
647 | - } |
|
648 | - if ($this->_socket === false) |
|
649 | - fclose($fp); |
|
650 | - |
|
651 | - // check response |
|
652 | - $read = strlen($response); |
|
653 | - if (!$response || $read != $len) |
|
654 | - { |
|
655 | - $this->_error = $len |
|
656 | - ? "failed to read searchd response (status=$status, ver=$ver, len=$len, read=$read)" |
|
657 | - : "received zero-sized searchd response"; |
|
658 | - return false; |
|
659 | - } |
|
660 | - |
|
661 | - // check status |
|
662 | - if ($status == SEARCHD_WARNING) |
|
663 | - { |
|
664 | - list(,$wlen) = unpack("N*", substr($response, 0, 4)); |
|
665 | - $this->_warning = substr($response, 4, $wlen); |
|
666 | - return substr($response, 4 + $wlen); |
|
667 | - } |
|
668 | - if ($status == SEARCHD_ERROR) |
|
669 | - { |
|
670 | - $this->_error = "searchd error: ".substr($response, 4); |
|
671 | - return false; |
|
672 | - } |
|
673 | - if ($status == SEARCHD_RETRY) |
|
674 | - { |
|
675 | - $this->_error = "temporary searchd error: ".substr($response, 4); |
|
676 | - return false; |
|
677 | - } |
|
678 | - if ($status != SEARCHD_OK) |
|
679 | - { |
|
680 | - $this->_error = "unknown status code '$status'"; |
|
681 | - return false; |
|
682 | - } |
|
683 | - |
|
684 | - // check version |
|
685 | - if ($ver < $client_ver) |
|
686 | - { |
|
687 | - $this->_warning = sprintf("searchd command v.%d.%d older than client's v.%d.%d, some options might not work", |
|
688 | - $ver >> 8, $ver & 0xff, $client_ver >> 8, $client_ver & 0xff); |
|
689 | - } |
|
690 | - |
|
691 | - return $response; |
|
692 | - } |
|
693 | - |
|
694 | - ///////////////////////////////////////////////////////////////////////////// |
|
695 | - // searching |
|
696 | - ///////////////////////////////////////////////////////////////////////////// |
|
697 | - |
|
698 | - /// set offset and count into result set, |
|
699 | - /// and optionally set max-matches and cutoff limits |
|
700 | - public function SetLimits($offset, $limit, $max = 0, $cutoff = 0) |
|
701 | - { |
|
702 | - assert(is_int($offset)); |
|
703 | - assert(is_int($limit)); |
|
704 | - assert($offset >= 0); |
|
705 | - assert($limit > 0); |
|
706 | - assert($max >= 0); |
|
707 | - $this->_offset = $offset; |
|
708 | - $this->_limit = $limit; |
|
709 | - if ($max > 0) |
|
710 | - $this->_maxmatches = $max; |
|
711 | - if ($cutoff > 0) |
|
712 | - $this->_cutoff = $cutoff; |
|
713 | - } |
|
714 | - |
|
715 | - /// set maximum query time, in milliseconds, per-index |
|
716 | - /// integer, 0 means "do not limit" |
|
717 | - public function SetMaxQueryTime($max) |
|
718 | - { |
|
719 | - assert(is_int($max)); |
|
720 | - assert($max >= 0); |
|
721 | - $this->_maxquerytime = $max; |
|
722 | - } |
|
723 | - |
|
724 | - /// set matching mode |
|
725 | - public function SetMatchMode($mode) |
|
726 | - { |
|
727 | - assert($mode == SPH_MATCH_ALL |
|
728 | - || $mode == SPH_MATCH_ANY |
|
729 | - || $mode == SPH_MATCH_PHRASE |
|
730 | - || $mode == SPH_MATCH_BOOLEAN |
|
731 | - || $mode == SPH_MATCH_EXTENDED |
|
732 | - || $mode == SPH_MATCH_FULLSCAN |
|
733 | - || $mode == SPH_MATCH_EXTENDED2); |
|
734 | - $this->_mode = $mode; |
|
735 | - } |
|
736 | - |
|
737 | - /// set ranking mode |
|
738 | - public function SetRankingMode($ranker) |
|
739 | - { |
|
740 | - assert($ranker >= 0 && $ranker < SPH_RANK_TOTAL); |
|
741 | - $this->_ranker = $ranker; |
|
742 | - } |
|
743 | - |
|
744 | - /// set matches sorting mode |
|
745 | - public function SetSortMode($mode, $sortby = "") |
|
746 | - { |
|
747 | - assert( |
|
748 | - $mode == SPH_SORT_RELEVANCE || |
|
749 | - $mode == SPH_SORT_ATTR_DESC || |
|
750 | - $mode == SPH_SORT_ATTR_ASC || |
|
751 | - $mode == SPH_SORT_TIME_SEGMENTS || |
|
752 | - $mode == SPH_SORT_EXTENDED || |
|
753 | - $mode == SPH_SORT_EXPR ); |
|
754 | - assert(is_string($sortby)); |
|
755 | - assert($mode == SPH_SORT_RELEVANCE || strlen($sortby) > 0); |
|
756 | - |
|
757 | - $this->_sort = $mode; |
|
758 | - $this->_sortby = $sortby; |
|
759 | - } |
|
760 | - |
|
761 | - /// bind per-field weights by order |
|
762 | - /// DEPRECATED; use SetFieldWeights() instead |
|
763 | - public function SetWeights($weights) |
|
764 | - { |
|
765 | - assert(is_array($weights)); |
|
766 | - foreach ($weights as $weight) |
|
767 | - assert(is_int($weight)); |
|
768 | - |
|
769 | - $this->_weights = $weights; |
|
770 | - } |
|
771 | - |
|
772 | - /// bind per-field weights by name |
|
773 | - public function SetFieldWeights($weights) |
|
774 | - { |
|
775 | - assert(is_array($weights)); |
|
776 | - foreach ($weights as $name=>$weight) |
|
777 | - { |
|
778 | - assert(is_string($name)); |
|
779 | - assert(is_int($weight)); |
|
780 | - } |
|
781 | - $this->_fieldweights = $weights; |
|
782 | - } |
|
783 | - |
|
784 | - /// bind per-index weights by name |
|
785 | - public function SetIndexWeights($weights) |
|
786 | - { |
|
787 | - assert(is_array($weights)); |
|
788 | - foreach ($weights as $index=>$weight) |
|
789 | - { |
|
790 | - assert(is_string($index)); |
|
791 | - assert(is_int($weight)); |
|
792 | - } |
|
793 | - $this->_indexweights = $weights; |
|
794 | - } |
|
795 | - |
|
796 | - /// set IDs range to match |
|
797 | - /// only match records if document ID is beetwen $min and $max (inclusive) |
|
798 | - public function SetIDRange($min, $max) |
|
799 | - { |
|
800 | - assert(is_numeric($min)); |
|
801 | - assert(is_numeric($max)); |
|
802 | - assert($min <= $max); |
|
803 | - $this->_min_id = $min; |
|
804 | - $this->_max_id = $max; |
|
805 | - } |
|
806 | - |
|
807 | - /// set values set filter |
|
808 | - /// only match records where $attribute value is in given set |
|
809 | - public function SetFilter($attribute, $values, $exclude = false) |
|
810 | - { |
|
811 | - assert(is_string($attribute)); |
|
812 | - assert(is_array($values)); |
|
813 | - assert(count($values)); |
|
814 | - |
|
815 | - if (is_array($values) && count($values)) |
|
816 | - { |
|
817 | - foreach ($values as $value) |
|
818 | - assert(is_numeric($value)); |
|
819 | - |
|
820 | - $this->_filters[] = array("type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values); |
|
821 | - } |
|
822 | - } |
|
823 | - |
|
824 | - /// set range filter |
|
825 | - /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
826 | - public function SetFilterRange($attribute, $min, $max, $exclude = false) |
|
827 | - { |
|
828 | - assert(is_string($attribute)); |
|
829 | - assert(is_numeric($min)); |
|
830 | - assert(is_numeric($max)); |
|
831 | - assert($min <= $max); |
|
832 | - |
|
833 | - $this->_filters[] = array("type"=>SPH_FILTER_RANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max); |
|
834 | - } |
|
835 | - |
|
836 | - /// set float range filter |
|
837 | - /// only match records if $attribute value is beetwen $min and $max (inclusive) |
|
838 | - public function SetFilterFloatRange($attribute, $min, $max, $exclude = false) |
|
839 | - { |
|
840 | - assert(is_string($attribute)); |
|
841 | - assert(is_float($min)); |
|
842 | - assert(is_float($max)); |
|
843 | - assert($min <= $max); |
|
844 | - |
|
845 | - $this->_filters[] = array("type"=>SPH_FILTER_FLOATRANGE, "attr"=>$attribute, "exclude"=>$exclude, "min"=>$min, "max"=>$max); |
|
846 | - } |
|
847 | - |
|
848 | - /// setup anchor point for geosphere distance calculations |
|
849 | - /// required to use @geodist in filters and sorting |
|
850 | - /// latitude and longitude must be in radians |
|
851 | - public function SetGeoAnchor($attrlat, $attrlong, $lat, $long) |
|
852 | - { |
|
853 | - assert(is_string($attrlat)); |
|
854 | - assert(is_string($attrlong)); |
|
855 | - assert(is_float($lat)); |
|
856 | - assert(is_float($long)); |
|
857 | - |
|
858 | - $this->_anchor = array("attrlat"=>$attrlat, "attrlong"=>$attrlong, "lat"=>$lat, "long"=>$long); |
|
859 | - } |
|
860 | - |
|
861 | - /// set grouping attribute and function |
|
862 | - public function SetGroupBy($attribute, $func, $groupsort = "@group desc") |
|
863 | - { |
|
864 | - assert(is_string($attribute)); |
|
865 | - assert(is_string($groupsort)); |
|
866 | - assert($func == SPH_GROUPBY_DAY |
|
867 | - || $func == SPH_GROUPBY_WEEK |
|
868 | - || $func == SPH_GROUPBY_MONTH |
|
869 | - || $func == SPH_GROUPBY_YEAR |
|
870 | - || $func == SPH_GROUPBY_ATTR |
|
871 | - || $func == SPH_GROUPBY_ATTRPAIR); |
|
872 | - |
|
873 | - $this->_groupby = $attribute; |
|
874 | - $this->_groupfunc = $func; |
|
875 | - $this->_groupsort = $groupsort; |
|
876 | - } |
|
877 | - |
|
878 | - /// set count-distinct attribute for group-by queries |
|
879 | - public function SetGroupDistinct($attribute) |
|
880 | - { |
|
881 | - assert(is_string($attribute)); |
|
882 | - $this->_groupdistinct = $attribute; |
|
883 | - } |
|
884 | - |
|
885 | - /// set distributed retries count and delay |
|
886 | - public function SetRetries($count, $delay = 0) |
|
887 | - { |
|
888 | - assert(is_int($count) && $count >= 0); |
|
889 | - assert(is_int($delay) && $delay >= 0); |
|
890 | - $this->_retrycount = $count; |
|
891 | - $this->_retrydelay = $delay; |
|
892 | - } |
|
893 | - |
|
894 | - /// set result set format (hash or array; hash by default) |
|
895 | - /// PHP specific; needed for group-by-MVA result sets that may contain duplicate IDs |
|
896 | - public function SetArrayResult($arrayresult) |
|
897 | - { |
|
898 | - assert(is_bool($arrayresult)); |
|
899 | - $this->_arrayresult = $arrayresult; |
|
900 | - } |
|
901 | - |
|
902 | - /// set attribute values override |
|
903 | - /// there can be only one override per attribute |
|
904 | - /// $values must be a hash that maps document IDs to attribute values |
|
905 | - public function SetOverride($attrname, $attrtype, $values) |
|
906 | - { |
|
907 | - assert(is_string($attrname)); |
|
908 | - assert(in_array($attrtype, array(SPH_ATTR_INTEGER, SPH_ATTR_TIMESTAMP, SPH_ATTR_BOOL, SPH_ATTR_FLOAT, SPH_ATTR_BIGINT))); |
|
909 | - assert(is_array($values)); |
|
910 | - |
|
911 | - $this->_overrides[$attrname] = array("attr"=>$attrname, "type"=>$attrtype, "values"=>$values); |
|
912 | - } |
|
913 | - |
|
914 | - /// set select-list (attributes or expressions), SQL-like syntax |
|
915 | - public function SetSelect($select) |
|
916 | - { |
|
917 | - assert(is_string($select)); |
|
918 | - $this->_select = $select; |
|
919 | - } |
|
920 | - |
|
921 | - ////////////////////////////////////////////////////////////////////////////// |
|
922 | - |
|
923 | - /// clear all filters (for multi-queries) |
|
924 | - public function ResetFilters() |
|
925 | - { |
|
926 | - $this->_filters = array(); |
|
927 | - $this->_anchor = array(); |
|
928 | - } |
|
929 | - |
|
930 | - /// clear groupby settings (for multi-queries) |
|
931 | - public function ResetGroupBy() |
|
932 | - { |
|
933 | - $this->_groupby = ""; |
|
934 | - $this->_groupfunc = SPH_GROUPBY_DAY; |
|
935 | - $this->_groupsort = "@group desc"; |
|
936 | - $this->_groupdistinct = ""; |
|
937 | - } |
|
938 | - |
|
939 | - /// clear all attribute value overrides (for multi-queries) |
|
940 | - public function ResetOverrides() |
|
939 | + /// clear all attribute value overrides (for multi-queries) |
|
940 | + public function ResetOverrides() |
|
941 | 941 | { |
942 | - $this->_overrides = array(); |
|
942 | + $this->_overrides = array(); |
|
943 | 943 | } |
944 | 944 | |
945 | - ////////////////////////////////////////////////////////////////////////////// |
|
946 | - |
|
947 | - /// connect to searchd server, run given search query through given indexes, |
|
948 | - /// and return the search results |
|
949 | - public function Query($query, $index = "*", $comment = "") |
|
950 | - { |
|
951 | - assert(empty($this->_reqs)); |
|
952 | - |
|
953 | - $this->AddQuery($query, $index, $comment); |
|
954 | - $results = $this->RunQueries(); |
|
955 | - $this->_reqs = array(); // just in case it failed too early |
|
956 | - |
|
957 | - if (!is_array($results)) |
|
958 | - return false; // probably network error; error message should be already filled |
|
959 | - |
|
960 | - $this->_error = $results[0]["error"]; |
|
961 | - $this->_warning = $results[0]["warning"]; |
|
962 | - if ($results[0]["status"] == SEARCHD_ERROR) |
|
963 | - return false; |
|
964 | - else |
|
965 | - return $results[0]; |
|
966 | - } |
|
967 | - |
|
968 | - /// helper to pack floats in network byte order |
|
969 | - public function _PackFloat($f) |
|
970 | - { |
|
971 | - $t1 = pack("f", $f); // machine order |
|
972 | - list(,$t2) = unpack("L*", $t1); // int in machine order |
|
973 | - return pack("N", $t2); |
|
974 | - } |
|
975 | - |
|
976 | - /** |
|
977 | - * @SuppressWarnings(unused) |
|
978 | - */ |
|
979 | - |
|
980 | - /// add query to multi-query batch |
|
981 | - /// returns index into results array from RunQueries() call |
|
982 | - public function AddQuery($query, $index = "*", $comment = "") |
|
983 | - { |
|
984 | - // mbstring workaround |
|
985 | - $this->_MBPush(); |
|
986 | - |
|
987 | - // build request |
|
988 | - $req = pack("NNNNN", $this->_offset, $this->_limit, $this->_mode, $this->_ranker, $this->_sort); // mode and limits |
|
989 | - $req .= pack("N", strlen($this->_sortby)).$this->_sortby; |
|
990 | - $req .= pack("N", strlen($query)).$query; // query itself |
|
991 | - $req .= pack("N", count($this->_weights)); // weights |
|
992 | - foreach ($this->_weights as $weight) |
|
993 | - $req .= pack("N", (int) $weight); |
|
994 | - $req .= pack("N", strlen($index)).$index; // indexes |
|
995 | - $req .= pack("N", 1); // id64 range marker |
|
996 | - $req .= sphPackU64($this->_min_id).sphPackU64($this->_max_id); // id64 range |
|
997 | - |
|
998 | - // filters |
|
999 | - $req .= pack("N", count($this->_filters)); |
|
1000 | - foreach ($this->_filters as $filter) |
|
1001 | - { |
|
1002 | - $req .= pack("N", strlen($filter["attr"])).$filter["attr"]; |
|
1003 | - $req .= pack("N", $filter["type"]); |
|
1004 | - switch ($filter["type"]) |
|
1005 | - { |
|
1006 | - case SPH_FILTER_VALUES: |
|
1007 | - $req .= pack("N", count($filter["values"])); |
|
1008 | - foreach ($filter["values"] as $value) |
|
1009 | - $req .= sphPackI64($value); |
|
1010 | - break; |
|
1011 | - |
|
1012 | - case SPH_FILTER_RANGE: |
|
1013 | - $req .= sphPackI64($filter["min"]).sphPackI64($filter["max"]); |
|
1014 | - break; |
|
1015 | - |
|
1016 | - case SPH_FILTER_FLOATRANGE: |
|
1017 | - $req .= $this->_PackFloat($filter["min"]).$this->_PackFloat($filter["max"]); |
|
1018 | - break; |
|
1019 | - |
|
1020 | - default: |
|
1021 | - assert(0 && "internal error: unhandled filter type"); |
|
1022 | - } |
|
1023 | - $req .= pack("N", $filter["exclude"]); |
|
1024 | - } |
|
1025 | - |
|
1026 | - // group-by clause, max-matches count, group-sort clause, cutoff count |
|
1027 | - $req .= pack("NN", $this->_groupfunc, strlen($this->_groupby)).$this->_groupby; |
|
1028 | - $req .= pack("N", $this->_maxmatches); |
|
1029 | - $req .= pack("N", strlen($this->_groupsort)).$this->_groupsort; |
|
1030 | - $req .= pack("NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay); |
|
1031 | - $req .= pack("N", strlen($this->_groupdistinct)).$this->_groupdistinct; |
|
1032 | - |
|
1033 | - // anchor point |
|
1034 | - if (empty($this->_anchor)) |
|
1035 | - { |
|
1036 | - $req .= pack("N", 0); |
|
1037 | - } else |
|
1038 | - { |
|
1039 | - $a = & $this->_anchor; |
|
1040 | - $req .= pack("N", 1); |
|
1041 | - $req .= pack("N", strlen($a["attrlat"])).$a["attrlat"]; |
|
1042 | - $req .= pack("N", strlen($a["attrlong"])).$a["attrlong"]; |
|
1043 | - $req .= $this->_PackFloat($a["lat"]).$this->_PackFloat($a["long"]); |
|
1044 | - } |
|
1045 | - |
|
1046 | - // per-index weights |
|
1047 | - $req .= pack("N", count($this->_indexweights)); |
|
1048 | - foreach ($this->_indexweights as $idx=>$weight) |
|
1049 | - $req .= pack("N", strlen($idx)).$idx.pack("N", $weight); |
|
1050 | - |
|
1051 | - // max query time |
|
1052 | - $req .= pack("N", $this->_maxquerytime); |
|
1053 | - |
|
1054 | - // per-field weights |
|
1055 | - $req .= pack("N", count($this->_fieldweights)); |
|
1056 | - foreach ($this->_fieldweights as $field=>$weight) |
|
1057 | - $req .= pack("N", strlen($field)).$field.pack("N", $weight); |
|
1058 | - |
|
1059 | - // comment |
|
1060 | - $req .= pack("N", strlen($comment)).$comment; |
|
1061 | - |
|
1062 | - // attribute overrides |
|
1063 | - $req .= pack("N", count($this->_overrides)); |
|
1064 | - foreach ($this->_overrides as $key => $entry) |
|
1065 | - { |
|
1066 | - $req .= pack("N", strlen($entry["attr"])).$entry["attr"]; |
|
1067 | - $req .= pack("NN", $entry["type"], count($entry["values"])); |
|
1068 | - foreach ($entry["values"] as $id=>$val) |
|
1069 | - { |
|
1070 | - assert(is_numeric($id)); |
|
1071 | - assert(is_numeric($val)); |
|
1072 | - |
|
1073 | - $req .= sphPackU64($id); |
|
1074 | - switch ($entry["type"]) |
|
1075 | - { |
|
1076 | - case SPH_ATTR_FLOAT: $req .= $this->_PackFloat($val); break; |
|
1077 | - case SPH_ATTR_BIGINT: $req .= sphPackI64($val); break; |
|
1078 | - default: $req .= pack("N", $val); break; |
|
1079 | - } |
|
1080 | - } |
|
1081 | - } |
|
1082 | - |
|
1083 | - // select-list |
|
1084 | - $req .= pack("N", strlen($this->_select)).$this->_select; |
|
1085 | - |
|
1086 | - // mbstring workaround |
|
1087 | - $this->_MBPop(); |
|
1088 | - |
|
1089 | - // store request to requests array |
|
1090 | - $this->_reqs[] = $req; |
|
1091 | - return count($this->_reqs) - 1; |
|
1092 | - } |
|
1093 | - |
|
1094 | - /// connect to searchd, run queries batch, and return an array of result sets |
|
1095 | - public function RunQueries() |
|
1096 | - { |
|
1097 | - if (empty($this->_reqs)) |
|
1098 | - { |
|
1099 | - $this->_error = "no queries defined, issue AddQuery() first"; |
|
1100 | - return false; |
|
1101 | - } |
|
1102 | - |
|
1103 | - // mbstring workaround |
|
1104 | - $this->_MBPush(); |
|
1105 | - |
|
1106 | - if (!($fp = $this->_Connect())) |
|
1107 | - { |
|
1108 | - $this->_MBPop(); |
|
1109 | - return false; |
|
1110 | - } |
|
1111 | - |
|
1112 | - // send query, get response |
|
1113 | - $nreqs = count($this->_reqs); |
|
1114 | - $req = join("", $this->_reqs); |
|
1115 | - $len = 8 + strlen($req); |
|
1116 | - $req = pack("nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs).$req; // add header |
|
1117 | - |
|
1118 | - if (!($this->_Send($fp, $req, $len + 8)) || |
|
1119 | - !($response = $this->_GetResponse($fp, VER_COMMAND_SEARCH))) |
|
1120 | - { |
|
1121 | - $this->_MBPop(); |
|
1122 | - return false; |
|
1123 | - } |
|
1124 | - |
|
1125 | - // query sent ok; we can reset reqs now |
|
1126 | - $this->_reqs = array(); |
|
1127 | - |
|
1128 | - // parse and return response |
|
1129 | - return $this->_ParseSearchResponse($response, $nreqs); |
|
1130 | - } |
|
1131 | - |
|
1132 | - /// parse and return search query (or queries) response |
|
1133 | - public function _ParseSearchResponse($response, $nreqs) |
|
1134 | - { |
|
1135 | - $p = 0; // current position |
|
1136 | - $max = strlen($response); // max position for checks, to protect against broken responses |
|
1137 | - |
|
1138 | - $results = array(); |
|
1139 | - for ($ires = 0; $ires < $nreqs && $p < $max; $ires++) |
|
1140 | - { |
|
1141 | - $results[] = array(); |
|
1142 | - $result = & $results[$ires]; |
|
1143 | - |
|
1144 | - $result["error"] = ""; |
|
1145 | - $result["warning"] = ""; |
|
1146 | - |
|
1147 | - // extract status |
|
1148 | - list(,$status) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1149 | - $result["status"] = $status; |
|
1150 | - if ($status != SEARCHD_OK) |
|
1151 | - { |
|
1152 | - list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1153 | - $message = substr($response, $p, $len); $p += $len; |
|
1154 | - |
|
1155 | - if ($status == SEARCHD_WARNING) |
|
1156 | - { |
|
1157 | - $result["warning"] = $message; |
|
1158 | - } else |
|
1159 | - { |
|
1160 | - $result["error"] = $message; |
|
1161 | - continue; |
|
1162 | - } |
|
1163 | - } |
|
1164 | - |
|
1165 | - // read schema |
|
1166 | - $fields = array(); |
|
1167 | - $attrs = array(); |
|
1168 | - |
|
1169 | - list(,$nfields) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1170 | - while ($nfields-- > 0 && $p < $max) |
|
1171 | - { |
|
1172 | - list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1173 | - $fields[] = substr($response, $p, $len); $p += $len; |
|
1174 | - } |
|
1175 | - $result["fields"] = $fields; |
|
1176 | - |
|
1177 | - list(,$nattrs) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1178 | - while ($nattrs-- > 0 && $p < $max) |
|
1179 | - { |
|
1180 | - list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1181 | - $attr = substr($response, $p, $len); $p += $len; |
|
1182 | - list(,$type) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1183 | - $attrs[$attr] = $type; |
|
1184 | - } |
|
1185 | - $result["attrs"] = $attrs; |
|
1186 | - |
|
1187 | - // read match count |
|
1188 | - list(,$count) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1189 | - list(,$id64) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1190 | - |
|
1191 | - // read matches |
|
1192 | - $idx = -1; |
|
1193 | - while ($count-- > 0 && $p < $max) |
|
1194 | - { |
|
1195 | - // index into result array |
|
1196 | - $idx++; |
|
1197 | - |
|
1198 | - // parse document id and weight |
|
1199 | - if ($id64) |
|
1200 | - { |
|
1201 | - $doc = sphUnpackU64(substr($response, $p, 8)); $p += 8; |
|
1202 | - list(,$weight) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1203 | - } |
|
1204 | - else |
|
1205 | - { |
|
1206 | - list ($doc, $weight) = array_values(unpack("N*N*", |
|
1207 | - substr($response, $p, 8))); |
|
1208 | - $p += 8; |
|
1209 | - $doc = sphFixUint($doc); |
|
1210 | - } |
|
1211 | - $weight = sprintf("%u", $weight); |
|
1212 | - |
|
1213 | - // create match entry |
|
1214 | - if ($this->_arrayresult) |
|
1215 | - $result["matches"][$idx] = array("id"=>$doc, "weight"=>$weight); |
|
1216 | - else |
|
1217 | - $result["matches"][$doc]["weight"] = $weight; |
|
1218 | - |
|
1219 | - // parse and create attributes |
|
1220 | - $attrvals = array(); |
|
1221 | - foreach ($attrs as $attr=>$type) |
|
1222 | - { |
|
1223 | - // handle 64bit ints |
|
1224 | - if ($type == SPH_ATTR_BIGINT) |
|
1225 | - { |
|
1226 | - $attrvals[$attr] = sphUnpackI64(substr($response, $p, 8)); $p += 8; |
|
1227 | - continue; |
|
1228 | - } |
|
1229 | - |
|
1230 | - // handle floats |
|
1231 | - if ($type == SPH_ATTR_FLOAT) |
|
1232 | - { |
|
1233 | - list(,$uval) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1234 | - list(,$fval) = unpack("f*", pack("L", $uval)); |
|
1235 | - $attrvals[$attr] = $fval; |
|
1236 | - continue; |
|
1237 | - } |
|
1238 | - |
|
1239 | - // handle everything else as unsigned ints |
|
1240 | - list(,$val) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1241 | - if ($type & SPH_ATTR_MULTI) |
|
1242 | - { |
|
1243 | - $attrvals[$attr] = array(); |
|
1244 | - $nvalues = $val; |
|
1245 | - while ($nvalues-- > 0 && $p < $max) |
|
1246 | - { |
|
1247 | - list(,$val) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1248 | - $attrvals[$attr][] = sphFixUint($val); |
|
1249 | - } |
|
1250 | - } else if ($type == SPH_ATTR_STRING) |
|
1251 | - { |
|
1252 | - $attrvals[$attr] = substr($response, $p, $val); |
|
1253 | - $p += $val; |
|
1254 | - } else |
|
1255 | - { |
|
1256 | - $attrvals[$attr] = sphFixUint($val); |
|
1257 | - } |
|
1258 | - } |
|
1259 | - |
|
1260 | - if ($this->_arrayresult) |
|
1261 | - $result["matches"][$idx]["attrs"] = $attrvals; |
|
1262 | - else |
|
1263 | - $result["matches"][$doc]["attrs"] = $attrvals; |
|
1264 | - } |
|
1265 | - |
|
1266 | - list ($total, $total_found, $msecs, $words) = |
|
1267 | - array_values(unpack("N*N*N*N*", substr($response, $p, 16))); |
|
1268 | - $result["total"] = sprintf("%u", $total); |
|
1269 | - $result["total_found"] = sprintf("%u", $total_found); |
|
1270 | - $result["time"] = sprintf("%.3f", $msecs / 1000); |
|
1271 | - $p += 16; |
|
1272 | - |
|
1273 | - while ($words-- > 0 && $p < $max) |
|
1274 | - { |
|
1275 | - list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1276 | - $word = substr($response, $p, $len); $p += $len; |
|
1277 | - list ($docs, $hits) = array_values(unpack("N*N*", substr($response, $p, 8))); $p += 8; |
|
1278 | - $result["words"][$word] = array( |
|
1279 | - "docs"=>sprintf("%u", $docs), |
|
1280 | - "hits"=>sprintf("%u", $hits) ); |
|
1281 | - } |
|
1282 | - } |
|
1283 | - |
|
1284 | - $this->_MBPop(); |
|
1285 | - return $results; |
|
1286 | - } |
|
1287 | - |
|
1288 | - ///////////////////////////////////////////////////////////////////////////// |
|
1289 | - // excerpts generation |
|
1290 | - ///////////////////////////////////////////////////////////////////////////// |
|
1291 | - |
|
1292 | - /// connect to searchd server, and generate exceprts (snippets) |
|
1293 | - /// of given documents for given query. returns false on failure, |
|
1294 | - /// an array of snippets on success |
|
1295 | - public function BuildExcerpts($docs, $index, $words, $opts = array()) |
|
1296 | - { |
|
1297 | - assert(is_array($docs)); |
|
1298 | - assert(is_string($index)); |
|
1299 | - assert(is_string($words)); |
|
1300 | - assert(is_array($opts)); |
|
1301 | - |
|
1302 | - $this->_MBPush(); |
|
1303 | - |
|
1304 | - if (!($fp = $this->_Connect())) |
|
1305 | - { |
|
1306 | - $this->_MBPop(); |
|
1307 | - return false; |
|
1308 | - } |
|
1309 | - |
|
1310 | - ///////////////// |
|
1311 | - // fixup options |
|
1312 | - ///////////////// |
|
1313 | - |
|
1314 | - if (!isset($opts["before_match"])) $opts["before_match"] = "<b>"; |
|
1315 | - if (!isset($opts["after_match"])) $opts["after_match"] = "</b>"; |
|
1316 | - if (!isset($opts["chunk_separator"])) $opts["chunk_separator"] = " ... "; |
|
1317 | - if (!isset($opts["limit"])) $opts["limit"] = 256; |
|
1318 | - if (!isset($opts["limit_passages"])) $opts["limit_passages"] = 0; |
|
1319 | - if (!isset($opts["limit_words"])) $opts["limit_words"] = 0; |
|
1320 | - if (!isset($opts["around"])) $opts["around"] = 5; |
|
1321 | - if (!isset($opts["exact_phrase"])) $opts["exact_phrase"] = false; |
|
1322 | - if (!isset($opts["single_passage"])) $opts["single_passage"] = false; |
|
1323 | - if (!isset($opts["use_boundaries"])) $opts["use_boundaries"] = false; |
|
1324 | - if (!isset($opts["weight_order"])) $opts["weight_order"] = false; |
|
1325 | - if (!isset($opts["query_mode"])) $opts["query_mode"] = false; |
|
1326 | - if (!isset($opts["force_all_words"])) $opts["force_all_words"] = false; |
|
1327 | - if (!isset($opts["start_passage_id"])) $opts["start_passage_id"] = 1; |
|
1328 | - if (!isset($opts["load_files"])) $opts["load_files"] = false; |
|
1329 | - if (!isset($opts["html_strip_mode"])) $opts["html_strip_mode"] = "index"; |
|
1330 | - if (!isset($opts["allow_empty"])) $opts["allow_empty"] = false; |
|
1331 | - if (!isset($opts["passage_boundary"])) $opts["passage_boundary"] = "none"; |
|
1332 | - if (!isset($opts["emit_zones"])) $opts["emit_zones"] = false; |
|
1333 | - |
|
1334 | - ///////////////// |
|
1335 | - // build request |
|
1336 | - ///////////////// |
|
1337 | - |
|
1338 | - // v.1.2 req |
|
1339 | - $flags = 1; // remove spaces |
|
1340 | - if ($opts["exact_phrase"]) $flags |= 2; |
|
1341 | - if ($opts["single_passage"]) $flags |= 4; |
|
1342 | - if ($opts["use_boundaries"]) $flags |= 8; |
|
1343 | - if ($opts["weight_order"]) $flags |= 16; |
|
1344 | - if ($opts["query_mode"]) $flags |= 32; |
|
1345 | - if ($opts["force_all_words"]) $flags |= 64; |
|
1346 | - if ($opts["load_files"]) $flags |= 128; |
|
1347 | - if ($opts["allow_empty"]) $flags |= 256; |
|
1348 | - if ($opts["emit_zones"]) $flags |= 512; |
|
1349 | - $req = pack("NN", 0, $flags); // mode=0, flags=$flags |
|
1350 | - $req .= pack("N", strlen($index)).$index; // req index |
|
1351 | - $req .= pack("N", strlen($words)).$words; // req words |
|
1352 | - |
|
1353 | - // options |
|
1354 | - $req .= pack("N", strlen($opts["before_match"])).$opts["before_match"]; |
|
1355 | - $req .= pack("N", strlen($opts["after_match"])).$opts["after_match"]; |
|
1356 | - $req .= pack("N", strlen($opts["chunk_separator"])).$opts["chunk_separator"]; |
|
1357 | - $req .= pack("NN", (int) $opts["limit"], (int) $opts["around"]); |
|
1358 | - $req .= pack("NNN", (int) $opts["limit_passages"], (int) $opts["limit_words"], (int) $opts["start_passage_id"]); // v.1.2 |
|
1359 | - $req .= pack("N", strlen($opts["html_strip_mode"])).$opts["html_strip_mode"]; |
|
1360 | - $req .= pack("N", strlen($opts["passage_boundary"])).$opts["passage_boundary"]; |
|
1361 | - |
|
1362 | - // documents |
|
1363 | - $req .= pack("N", count($docs)); |
|
1364 | - foreach ($docs as $doc) |
|
1365 | - { |
|
1366 | - assert(is_string($doc)); |
|
1367 | - $req .= pack("N", strlen($doc)).$doc; |
|
1368 | - } |
|
1369 | - |
|
1370 | - //////////////////////////// |
|
1371 | - // send query, get response |
|
1372 | - //////////////////////////// |
|
1373 | - |
|
1374 | - $len = strlen($req); |
|
1375 | - $req = pack("nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len).$req; // add header |
|
1376 | - if (!($this->_Send($fp, $req, $len + 8)) || |
|
1377 | - !($response = $this->_GetResponse($fp, VER_COMMAND_EXCERPT))) |
|
1378 | - { |
|
1379 | - $this->_MBPop(); |
|
1380 | - return false; |
|
1381 | - } |
|
1382 | - |
|
1383 | - ////////////////// |
|
1384 | - // parse response |
|
1385 | - ////////////////// |
|
1386 | - |
|
1387 | - $pos = 0; |
|
1388 | - $res = array(); |
|
1389 | - $rlen = strlen($response); |
|
1390 | - for ($i = 0; $i < count($docs); $i++) |
|
1391 | - { |
|
1392 | - list(,$len) = unpack("N*", substr($response, $pos, 4)); |
|
1393 | - $pos += 4; |
|
1394 | - |
|
1395 | - if ($pos + $len > $rlen) |
|
1396 | - { |
|
1397 | - $this->_error = "incomplete reply"; |
|
1398 | - $this->_MBPop(); |
|
1399 | - return false; |
|
1400 | - } |
|
1401 | - $res[] = $len ? substr ($response, $pos, $len) : ""; |
|
1402 | - $pos += $len; |
|
1403 | - } |
|
1404 | - |
|
1405 | - $this->_MBPop(); |
|
1406 | - return $res; |
|
1407 | - } |
|
1408 | - |
|
1409 | - |
|
1410 | - ///////////////////////////////////////////////////////////////////////////// |
|
1411 | - // keyword generation |
|
1412 | - ///////////////////////////////////////////////////////////////////////////// |
|
1413 | - |
|
1414 | - /// connect to searchd server, and generate keyword list for a given query |
|
1415 | - /// returns false on failure, |
|
1416 | - /// an array of words on success |
|
1417 | - public function BuildKeywords($query, $index, $hits) |
|
1418 | - { |
|
1419 | - assert(is_string($query)); |
|
1420 | - assert(is_string($index)); |
|
1421 | - assert(is_bool($hits)); |
|
1422 | - |
|
1423 | - $this->_MBPush(); |
|
1424 | - |
|
1425 | - if (!($fp = $this->_Connect())) |
|
1426 | - { |
|
1427 | - $this->_MBPop(); |
|
1428 | - return false; |
|
1429 | - } |
|
1430 | - |
|
1431 | - ///////////////// |
|
1432 | - // build request |
|
1433 | - ///////////////// |
|
1434 | - |
|
1435 | - // v.1.0 req |
|
1436 | - $req = pack("N", strlen($query)).$query; // req query |
|
1437 | - $req .= pack("N", strlen($index)).$index; // req index |
|
1438 | - $req .= pack("N", (int) $hits); |
|
1439 | - |
|
1440 | - //////////////////////////// |
|
1441 | - // send query, get response |
|
1442 | - //////////////////////////// |
|
1443 | - |
|
1444 | - $len = strlen($req); |
|
1445 | - $req = pack("nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len).$req; // add header |
|
1446 | - if (!($this->_Send($fp, $req, $len + 8)) || |
|
1447 | - !($response = $this->_GetResponse($fp, VER_COMMAND_KEYWORDS))) |
|
1448 | - { |
|
1449 | - $this->_MBPop(); |
|
1450 | - return false; |
|
1451 | - } |
|
1452 | - |
|
1453 | - ////////////////// |
|
1454 | - // parse response |
|
1455 | - ////////////////// |
|
1456 | - |
|
1457 | - $pos = 0; |
|
1458 | - $res = array(); |
|
1459 | - $rlen = strlen($response); |
|
1460 | - list(,$nwords) = unpack("N*", substr($response, $pos, 4)); |
|
1461 | - $pos += 4; |
|
1462 | - for ($i = 0; $i < $nwords; $i++) |
|
1463 | - { |
|
1464 | - list(,$len) = unpack("N*", substr($response, $pos, 4)); $pos += 4; |
|
1465 | - $tokenized = $len ? substr ($response, $pos, $len) : ""; |
|
1466 | - $pos += $len; |
|
1467 | - |
|
1468 | - list(,$len) = unpack("N*", substr($response, $pos, 4)); $pos += 4; |
|
1469 | - $normalized = $len ? substr ($response, $pos, $len) : ""; |
|
1470 | - $pos += $len; |
|
1471 | - |
|
1472 | - $res[] = array("tokenized"=>$tokenized, "normalized"=>$normalized); |
|
1473 | - |
|
1474 | - if ($hits) |
|
1475 | - { |
|
1476 | - list($ndocs, $nhits) = array_values(unpack("N*N*", substr($response, $pos, 8))); |
|
1477 | - $pos += 8; |
|
1478 | - $res [$i]["docs"] = $ndocs; |
|
1479 | - $res [$i]["hits"] = $nhits; |
|
1480 | - } |
|
1481 | - |
|
1482 | - if ($pos > $rlen) |
|
1483 | - { |
|
1484 | - $this->_error = "incomplete reply"; |
|
1485 | - $this->_MBPop(); |
|
1486 | - return false; |
|
1487 | - } |
|
1488 | - } |
|
1489 | - |
|
1490 | - $this->_MBPop(); |
|
1491 | - return $res; |
|
1492 | - } |
|
1493 | - |
|
1494 | - public function EscapeString($string) |
|
1495 | - { |
|
1496 | - $from = array('\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '='); |
|
1497 | - $to = array('\\\\', '\(', '\)', '\|', '\-', '\!', '\@', '\~', '\"', '\&', '\/', '\^', '\$', '\='); |
|
1498 | - |
|
1499 | - return str_replace($from, $to, $string); |
|
1500 | - } |
|
1501 | - |
|
1502 | - ///////////////////////////////////////////////////////////////////////////// |
|
1503 | - // attribute updates |
|
1504 | - ///////////////////////////////////////////////////////////////////////////// |
|
1505 | - |
|
1506 | - /// batch update given attributes in given rows in given indexes |
|
1507 | - /// returns amount of updated documents (0 or more) on success, or -1 on failure |
|
1508 | - public function UpdateAttributes($index, $attrs, $values, $mva = false) |
|
1509 | - { |
|
1510 | - // verify everything |
|
1511 | - assert(is_string($index)); |
|
1512 | - assert(is_bool($mva)); |
|
1513 | - |
|
1514 | - assert(is_array($attrs)); |
|
1515 | - foreach ($attrs as $attr) |
|
1516 | - assert(is_string($attr)); |
|
1517 | - |
|
1518 | - assert(is_array($values)); |
|
1519 | - foreach ($values as $id=>$entry) |
|
1520 | - { |
|
1521 | - assert(is_numeric($id)); |
|
1522 | - assert(is_array($entry)); |
|
1523 | - assert(count($entry) == count($attrs)); |
|
1524 | - foreach ($entry as $v) |
|
1525 | - { |
|
1526 | - if ($mva) |
|
1527 | - { |
|
1528 | - assert(is_array($v)); |
|
1529 | - foreach ($v as $vv) |
|
1530 | - assert(is_int($vv)); |
|
1531 | - } else |
|
1532 | - assert(is_int($v)); |
|
1533 | - } |
|
1534 | - } |
|
1535 | - |
|
1536 | - // build request |
|
1537 | - $this->_MBPush(); |
|
1538 | - $req = pack("N", strlen($index)).$index; |
|
1539 | - |
|
1540 | - $req .= pack("N", count($attrs)); |
|
1541 | - foreach ($attrs as $attr) |
|
1542 | - { |
|
1543 | - $req .= pack("N", strlen($attr)).$attr; |
|
1544 | - $req .= pack("N", $mva ? 1 : 0); |
|
1545 | - } |
|
1546 | - |
|
1547 | - $req .= pack("N", count($values)); |
|
1548 | - foreach ($values as $id=>$entry) |
|
1549 | - { |
|
1550 | - $req .= sphPackU64($id); |
|
1551 | - foreach ($entry as $v) |
|
1552 | - { |
|
1553 | - $req .= pack("N", $mva ? count($v) : $v); |
|
1554 | - if ($mva) |
|
1555 | - foreach ($v as $vv) |
|
1556 | - $req .= pack("N", $vv); |
|
1557 | - } |
|
1558 | - } |
|
1559 | - |
|
1560 | - // connect, send query, get response |
|
1561 | - if (!($fp = $this->_Connect())) |
|
1562 | - { |
|
1563 | - $this->_MBPop(); |
|
1564 | - return -1; |
|
1565 | - } |
|
1566 | - |
|
1567 | - $len = strlen($req); |
|
1568 | - $req = pack("nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len).$req; // add header |
|
1569 | - if (!$this->_Send($fp, $req, $len + 8)) |
|
1570 | - { |
|
1571 | - $this->_MBPop(); |
|
1572 | - return -1; |
|
1573 | - } |
|
1574 | - |
|
1575 | - if (!($response = $this->_GetResponse($fp, VER_COMMAND_UPDATE))) |
|
1576 | - { |
|
1577 | - $this->_MBPop(); |
|
1578 | - return -1; |
|
1579 | - } |
|
1580 | - |
|
1581 | - // parse response |
|
1582 | - list(,$updated) = unpack("N*", substr($response, 0, 4)); |
|
1583 | - $this->_MBPop(); |
|
1584 | - return $updated; |
|
1585 | - } |
|
1586 | - |
|
1587 | - ///////////////////////////////////////////////////////////////////////////// |
|
1588 | - // persistent connections |
|
1589 | - ///////////////////////////////////////////////////////////////////////////// |
|
1590 | - |
|
1591 | - public function Open() |
|
1592 | - { |
|
1593 | - if ($this->_socket !== false) |
|
1594 | - { |
|
1595 | - $this->_error = 'already connected'; |
|
1596 | - return false; |
|
1597 | - } |
|
1598 | - if (!$fp = $this->_Connect()) |
|
1599 | - return false; |
|
1600 | - |
|
1601 | - // command, command version = 0, body length = 4, body = 1 |
|
1602 | - $req = pack("nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
|
1603 | - if (!$this->_Send($fp, $req, 12)) |
|
1604 | - return false; |
|
1605 | - |
|
1606 | - $this->_socket = $fp; |
|
1607 | - return true; |
|
1608 | - } |
|
1609 | - |
|
1610 | - public function Close() |
|
1611 | - { |
|
1612 | - if ($this->_socket === false) |
|
1613 | - { |
|
1614 | - $this->_error = 'not connected'; |
|
1615 | - return false; |
|
1616 | - } |
|
1617 | - |
|
1618 | - fclose($this->_socket); |
|
1619 | - $this->_socket = false; |
|
1620 | - |
|
1621 | - return true; |
|
1622 | - } |
|
1623 | - |
|
1624 | - ////////////////////////////////////////////////////////////////////////// |
|
1625 | - // status |
|
1626 | - ////////////////////////////////////////////////////////////////////////// |
|
1627 | - |
|
1628 | - public function Status() |
|
1629 | - { |
|
1630 | - $this->_MBPush(); |
|
1631 | - if (!($fp = $this->_Connect())) |
|
1632 | - { |
|
1633 | - $this->_MBPop(); |
|
1634 | - return false; |
|
1635 | - } |
|
1636 | - |
|
1637 | - $req = pack("nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, 1); // len=4, body=1 |
|
1638 | - if (!($this->_Send($fp, $req, 12)) || |
|
1639 | - !($response = $this->_GetResponse($fp, VER_COMMAND_STATUS))) |
|
1640 | - { |
|
1641 | - $this->_MBPop(); |
|
1642 | - return false; |
|
1643 | - } |
|
1644 | - |
|
1645 | - $res = substr($response, 4); // just ignore length, error handling, etc |
|
1646 | - $p = 0; |
|
1647 | - list ($rows, $cols) = array_values(unpack("N*N*", substr($response, $p, 8))); $p += 8; |
|
1648 | - |
|
1649 | - $res = array(); |
|
1650 | - for ($i = 0; $i < $rows; $i++) |
|
1651 | - for ($j = 0; $j < $cols; $j++) |
|
1652 | - { |
|
1653 | - list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1654 | - $res[$i][] = substr($response, $p, $len); $p += $len; |
|
1655 | - } |
|
1656 | - |
|
1657 | - $this->_MBPop(); |
|
1658 | - return $res; |
|
1659 | - } |
|
1660 | - |
|
1661 | - ////////////////////////////////////////////////////////////////////////// |
|
1662 | - // flush |
|
1663 | - ////////////////////////////////////////////////////////////////////////// |
|
1664 | - |
|
1665 | - public function FlushAttributes() |
|
1666 | - { |
|
1667 | - $this->_MBPush(); |
|
1668 | - if (!($fp = $this->_Connect())) |
|
1669 | - { |
|
1670 | - $this->_MBPop(); |
|
1671 | - return -1; |
|
1672 | - } |
|
1673 | - |
|
1674 | - $req = pack("nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0); // len=0 |
|
1675 | - if (!($this->_Send($fp, $req, 8)) || |
|
1676 | - !($response = $this->_GetResponse($fp, VER_COMMAND_FLUSHATTRS))) |
|
1677 | - { |
|
1678 | - $this->_MBPop(); |
|
1679 | - return -1; |
|
1680 | - } |
|
1681 | - |
|
1682 | - $tag = -1; |
|
1683 | - if (strlen($response) == 4) |
|
1684 | - list(,$tag) = unpack("N*", $response); |
|
1685 | - else |
|
1686 | - $this->_error = "unexpected response length"; |
|
1687 | - |
|
1688 | - $this->_MBPop(); |
|
1689 | - return $tag; |
|
1690 | - } |
|
945 | + ////////////////////////////////////////////////////////////////////////////// |
|
946 | + |
|
947 | + /// connect to searchd server, run given search query through given indexes, |
|
948 | + /// and return the search results |
|
949 | + public function Query($query, $index = "*", $comment = "") |
|
950 | + { |
|
951 | + assert(empty($this->_reqs)); |
|
952 | + |
|
953 | + $this->AddQuery($query, $index, $comment); |
|
954 | + $results = $this->RunQueries(); |
|
955 | + $this->_reqs = array(); // just in case it failed too early |
|
956 | + |
|
957 | + if (!is_array($results)) |
|
958 | + return false; // probably network error; error message should be already filled |
|
959 | + |
|
960 | + $this->_error = $results[0]["error"]; |
|
961 | + $this->_warning = $results[0]["warning"]; |
|
962 | + if ($results[0]["status"] == SEARCHD_ERROR) |
|
963 | + return false; |
|
964 | + else |
|
965 | + return $results[0]; |
|
966 | + } |
|
967 | + |
|
968 | + /// helper to pack floats in network byte order |
|
969 | + public function _PackFloat($f) |
|
970 | + { |
|
971 | + $t1 = pack("f", $f); // machine order |
|
972 | + list(,$t2) = unpack("L*", $t1); // int in machine order |
|
973 | + return pack("N", $t2); |
|
974 | + } |
|
975 | + |
|
976 | + /** |
|
977 | + * @SuppressWarnings(unused) |
|
978 | + */ |
|
979 | + |
|
980 | + /// add query to multi-query batch |
|
981 | + /// returns index into results array from RunQueries() call |
|
982 | + public function AddQuery($query, $index = "*", $comment = "") |
|
983 | + { |
|
984 | + // mbstring workaround |
|
985 | + $this->_MBPush(); |
|
986 | + |
|
987 | + // build request |
|
988 | + $req = pack("NNNNN", $this->_offset, $this->_limit, $this->_mode, $this->_ranker, $this->_sort); // mode and limits |
|
989 | + $req .= pack("N", strlen($this->_sortby)).$this->_sortby; |
|
990 | + $req .= pack("N", strlen($query)).$query; // query itself |
|
991 | + $req .= pack("N", count($this->_weights)); // weights |
|
992 | + foreach ($this->_weights as $weight) |
|
993 | + $req .= pack("N", (int) $weight); |
|
994 | + $req .= pack("N", strlen($index)).$index; // indexes |
|
995 | + $req .= pack("N", 1); // id64 range marker |
|
996 | + $req .= sphPackU64($this->_min_id).sphPackU64($this->_max_id); // id64 range |
|
997 | + |
|
998 | + // filters |
|
999 | + $req .= pack("N", count($this->_filters)); |
|
1000 | + foreach ($this->_filters as $filter) |
|
1001 | + { |
|
1002 | + $req .= pack("N", strlen($filter["attr"])).$filter["attr"]; |
|
1003 | + $req .= pack("N", $filter["type"]); |
|
1004 | + switch ($filter["type"]) |
|
1005 | + { |
|
1006 | + case SPH_FILTER_VALUES: |
|
1007 | + $req .= pack("N", count($filter["values"])); |
|
1008 | + foreach ($filter["values"] as $value) |
|
1009 | + $req .= sphPackI64($value); |
|
1010 | + break; |
|
1011 | + |
|
1012 | + case SPH_FILTER_RANGE: |
|
1013 | + $req .= sphPackI64($filter["min"]).sphPackI64($filter["max"]); |
|
1014 | + break; |
|
1015 | + |
|
1016 | + case SPH_FILTER_FLOATRANGE: |
|
1017 | + $req .= $this->_PackFloat($filter["min"]).$this->_PackFloat($filter["max"]); |
|
1018 | + break; |
|
1019 | + |
|
1020 | + default: |
|
1021 | + assert(0 && "internal error: unhandled filter type"); |
|
1022 | + } |
|
1023 | + $req .= pack("N", $filter["exclude"]); |
|
1024 | + } |
|
1025 | + |
|
1026 | + // group-by clause, max-matches count, group-sort clause, cutoff count |
|
1027 | + $req .= pack("NN", $this->_groupfunc, strlen($this->_groupby)).$this->_groupby; |
|
1028 | + $req .= pack("N", $this->_maxmatches); |
|
1029 | + $req .= pack("N", strlen($this->_groupsort)).$this->_groupsort; |
|
1030 | + $req .= pack("NNN", $this->_cutoff, $this->_retrycount, $this->_retrydelay); |
|
1031 | + $req .= pack("N", strlen($this->_groupdistinct)).$this->_groupdistinct; |
|
1032 | + |
|
1033 | + // anchor point |
|
1034 | + if (empty($this->_anchor)) |
|
1035 | + { |
|
1036 | + $req .= pack("N", 0); |
|
1037 | + } else |
|
1038 | + { |
|
1039 | + $a = & $this->_anchor; |
|
1040 | + $req .= pack("N", 1); |
|
1041 | + $req .= pack("N", strlen($a["attrlat"])).$a["attrlat"]; |
|
1042 | + $req .= pack("N", strlen($a["attrlong"])).$a["attrlong"]; |
|
1043 | + $req .= $this->_PackFloat($a["lat"]).$this->_PackFloat($a["long"]); |
|
1044 | + } |
|
1045 | + |
|
1046 | + // per-index weights |
|
1047 | + $req .= pack("N", count($this->_indexweights)); |
|
1048 | + foreach ($this->_indexweights as $idx=>$weight) |
|
1049 | + $req .= pack("N", strlen($idx)).$idx.pack("N", $weight); |
|
1050 | + |
|
1051 | + // max query time |
|
1052 | + $req .= pack("N", $this->_maxquerytime); |
|
1053 | + |
|
1054 | + // per-field weights |
|
1055 | + $req .= pack("N", count($this->_fieldweights)); |
|
1056 | + foreach ($this->_fieldweights as $field=>$weight) |
|
1057 | + $req .= pack("N", strlen($field)).$field.pack("N", $weight); |
|
1058 | + |
|
1059 | + // comment |
|
1060 | + $req .= pack("N", strlen($comment)).$comment; |
|
1061 | + |
|
1062 | + // attribute overrides |
|
1063 | + $req .= pack("N", count($this->_overrides)); |
|
1064 | + foreach ($this->_overrides as $key => $entry) |
|
1065 | + { |
|
1066 | + $req .= pack("N", strlen($entry["attr"])).$entry["attr"]; |
|
1067 | + $req .= pack("NN", $entry["type"], count($entry["values"])); |
|
1068 | + foreach ($entry["values"] as $id=>$val) |
|
1069 | + { |
|
1070 | + assert(is_numeric($id)); |
|
1071 | + assert(is_numeric($val)); |
|
1072 | + |
|
1073 | + $req .= sphPackU64($id); |
|
1074 | + switch ($entry["type"]) |
|
1075 | + { |
|
1076 | + case SPH_ATTR_FLOAT: $req .= $this->_PackFloat($val); break; |
|
1077 | + case SPH_ATTR_BIGINT: $req .= sphPackI64($val); break; |
|
1078 | + default: $req .= pack("N", $val); break; |
|
1079 | + } |
|
1080 | + } |
|
1081 | + } |
|
1082 | + |
|
1083 | + // select-list |
|
1084 | + $req .= pack("N", strlen($this->_select)).$this->_select; |
|
1085 | + |
|
1086 | + // mbstring workaround |
|
1087 | + $this->_MBPop(); |
|
1088 | + |
|
1089 | + // store request to requests array |
|
1090 | + $this->_reqs[] = $req; |
|
1091 | + return count($this->_reqs) - 1; |
|
1092 | + } |
|
1093 | + |
|
1094 | + /// connect to searchd, run queries batch, and return an array of result sets |
|
1095 | + public function RunQueries() |
|
1096 | + { |
|
1097 | + if (empty($this->_reqs)) |
|
1098 | + { |
|
1099 | + $this->_error = "no queries defined, issue AddQuery() first"; |
|
1100 | + return false; |
|
1101 | + } |
|
1102 | + |
|
1103 | + // mbstring workaround |
|
1104 | + $this->_MBPush(); |
|
1105 | + |
|
1106 | + if (!($fp = $this->_Connect())) |
|
1107 | + { |
|
1108 | + $this->_MBPop(); |
|
1109 | + return false; |
|
1110 | + } |
|
1111 | + |
|
1112 | + // send query, get response |
|
1113 | + $nreqs = count($this->_reqs); |
|
1114 | + $req = join("", $this->_reqs); |
|
1115 | + $len = 8 + strlen($req); |
|
1116 | + $req = pack("nnNNN", SEARCHD_COMMAND_SEARCH, VER_COMMAND_SEARCH, $len, 0, $nreqs).$req; // add header |
|
1117 | + |
|
1118 | + if (!($this->_Send($fp, $req, $len + 8)) || |
|
1119 | + !($response = $this->_GetResponse($fp, VER_COMMAND_SEARCH))) |
|
1120 | + { |
|
1121 | + $this->_MBPop(); |
|
1122 | + return false; |
|
1123 | + } |
|
1124 | + |
|
1125 | + // query sent ok; we can reset reqs now |
|
1126 | + $this->_reqs = array(); |
|
1127 | + |
|
1128 | + // parse and return response |
|
1129 | + return $this->_ParseSearchResponse($response, $nreqs); |
|
1130 | + } |
|
1131 | + |
|
1132 | + /// parse and return search query (or queries) response |
|
1133 | + public function _ParseSearchResponse($response, $nreqs) |
|
1134 | + { |
|
1135 | + $p = 0; // current position |
|
1136 | + $max = strlen($response); // max position for checks, to protect against broken responses |
|
1137 | + |
|
1138 | + $results = array(); |
|
1139 | + for ($ires = 0; $ires < $nreqs && $p < $max; $ires++) |
|
1140 | + { |
|
1141 | + $results[] = array(); |
|
1142 | + $result = & $results[$ires]; |
|
1143 | + |
|
1144 | + $result["error"] = ""; |
|
1145 | + $result["warning"] = ""; |
|
1146 | + |
|
1147 | + // extract status |
|
1148 | + list(,$status) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1149 | + $result["status"] = $status; |
|
1150 | + if ($status != SEARCHD_OK) |
|
1151 | + { |
|
1152 | + list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1153 | + $message = substr($response, $p, $len); $p += $len; |
|
1154 | + |
|
1155 | + if ($status == SEARCHD_WARNING) |
|
1156 | + { |
|
1157 | + $result["warning"] = $message; |
|
1158 | + } else |
|
1159 | + { |
|
1160 | + $result["error"] = $message; |
|
1161 | + continue; |
|
1162 | + } |
|
1163 | + } |
|
1164 | + |
|
1165 | + // read schema |
|
1166 | + $fields = array(); |
|
1167 | + $attrs = array(); |
|
1168 | + |
|
1169 | + list(,$nfields) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1170 | + while ($nfields-- > 0 && $p < $max) |
|
1171 | + { |
|
1172 | + list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1173 | + $fields[] = substr($response, $p, $len); $p += $len; |
|
1174 | + } |
|
1175 | + $result["fields"] = $fields; |
|
1176 | + |
|
1177 | + list(,$nattrs) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1178 | + while ($nattrs-- > 0 && $p < $max) |
|
1179 | + { |
|
1180 | + list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1181 | + $attr = substr($response, $p, $len); $p += $len; |
|
1182 | + list(,$type) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1183 | + $attrs[$attr] = $type; |
|
1184 | + } |
|
1185 | + $result["attrs"] = $attrs; |
|
1186 | + |
|
1187 | + // read match count |
|
1188 | + list(,$count) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1189 | + list(,$id64) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1190 | + |
|
1191 | + // read matches |
|
1192 | + $idx = -1; |
|
1193 | + while ($count-- > 0 && $p < $max) |
|
1194 | + { |
|
1195 | + // index into result array |
|
1196 | + $idx++; |
|
1197 | + |
|
1198 | + // parse document id and weight |
|
1199 | + if ($id64) |
|
1200 | + { |
|
1201 | + $doc = sphUnpackU64(substr($response, $p, 8)); $p += 8; |
|
1202 | + list(,$weight) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1203 | + } |
|
1204 | + else |
|
1205 | + { |
|
1206 | + list ($doc, $weight) = array_values(unpack("N*N*", |
|
1207 | + substr($response, $p, 8))); |
|
1208 | + $p += 8; |
|
1209 | + $doc = sphFixUint($doc); |
|
1210 | + } |
|
1211 | + $weight = sprintf("%u", $weight); |
|
1212 | + |
|
1213 | + // create match entry |
|
1214 | + if ($this->_arrayresult) |
|
1215 | + $result["matches"][$idx] = array("id"=>$doc, "weight"=>$weight); |
|
1216 | + else |
|
1217 | + $result["matches"][$doc]["weight"] = $weight; |
|
1218 | + |
|
1219 | + // parse and create attributes |
|
1220 | + $attrvals = array(); |
|
1221 | + foreach ($attrs as $attr=>$type) |
|
1222 | + { |
|
1223 | + // handle 64bit ints |
|
1224 | + if ($type == SPH_ATTR_BIGINT) |
|
1225 | + { |
|
1226 | + $attrvals[$attr] = sphUnpackI64(substr($response, $p, 8)); $p += 8; |
|
1227 | + continue; |
|
1228 | + } |
|
1229 | + |
|
1230 | + // handle floats |
|
1231 | + if ($type == SPH_ATTR_FLOAT) |
|
1232 | + { |
|
1233 | + list(,$uval) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1234 | + list(,$fval) = unpack("f*", pack("L", $uval)); |
|
1235 | + $attrvals[$attr] = $fval; |
|
1236 | + continue; |
|
1237 | + } |
|
1238 | + |
|
1239 | + // handle everything else as unsigned ints |
|
1240 | + list(,$val) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1241 | + if ($type & SPH_ATTR_MULTI) |
|
1242 | + { |
|
1243 | + $attrvals[$attr] = array(); |
|
1244 | + $nvalues = $val; |
|
1245 | + while ($nvalues-- > 0 && $p < $max) |
|
1246 | + { |
|
1247 | + list(,$val) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1248 | + $attrvals[$attr][] = sphFixUint($val); |
|
1249 | + } |
|
1250 | + } else if ($type == SPH_ATTR_STRING) |
|
1251 | + { |
|
1252 | + $attrvals[$attr] = substr($response, $p, $val); |
|
1253 | + $p += $val; |
|
1254 | + } else |
|
1255 | + { |
|
1256 | + $attrvals[$attr] = sphFixUint($val); |
|
1257 | + } |
|
1258 | + } |
|
1259 | + |
|
1260 | + if ($this->_arrayresult) |
|
1261 | + $result["matches"][$idx]["attrs"] = $attrvals; |
|
1262 | + else |
|
1263 | + $result["matches"][$doc]["attrs"] = $attrvals; |
|
1264 | + } |
|
1265 | + |
|
1266 | + list ($total, $total_found, $msecs, $words) = |
|
1267 | + array_values(unpack("N*N*N*N*", substr($response, $p, 16))); |
|
1268 | + $result["total"] = sprintf("%u", $total); |
|
1269 | + $result["total_found"] = sprintf("%u", $total_found); |
|
1270 | + $result["time"] = sprintf("%.3f", $msecs / 1000); |
|
1271 | + $p += 16; |
|
1272 | + |
|
1273 | + while ($words-- > 0 && $p < $max) |
|
1274 | + { |
|
1275 | + list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1276 | + $word = substr($response, $p, $len); $p += $len; |
|
1277 | + list ($docs, $hits) = array_values(unpack("N*N*", substr($response, $p, 8))); $p += 8; |
|
1278 | + $result["words"][$word] = array( |
|
1279 | + "docs"=>sprintf("%u", $docs), |
|
1280 | + "hits"=>sprintf("%u", $hits) ); |
|
1281 | + } |
|
1282 | + } |
|
1283 | + |
|
1284 | + $this->_MBPop(); |
|
1285 | + return $results; |
|
1286 | + } |
|
1287 | + |
|
1288 | + ///////////////////////////////////////////////////////////////////////////// |
|
1289 | + // excerpts generation |
|
1290 | + ///////////////////////////////////////////////////////////////////////////// |
|
1291 | + |
|
1292 | + /// connect to searchd server, and generate exceprts (snippets) |
|
1293 | + /// of given documents for given query. returns false on failure, |
|
1294 | + /// an array of snippets on success |
|
1295 | + public function BuildExcerpts($docs, $index, $words, $opts = array()) |
|
1296 | + { |
|
1297 | + assert(is_array($docs)); |
|
1298 | + assert(is_string($index)); |
|
1299 | + assert(is_string($words)); |
|
1300 | + assert(is_array($opts)); |
|
1301 | + |
|
1302 | + $this->_MBPush(); |
|
1303 | + |
|
1304 | + if (!($fp = $this->_Connect())) |
|
1305 | + { |
|
1306 | + $this->_MBPop(); |
|
1307 | + return false; |
|
1308 | + } |
|
1309 | + |
|
1310 | + ///////////////// |
|
1311 | + // fixup options |
|
1312 | + ///////////////// |
|
1313 | + |
|
1314 | + if (!isset($opts["before_match"])) $opts["before_match"] = "<b>"; |
|
1315 | + if (!isset($opts["after_match"])) $opts["after_match"] = "</b>"; |
|
1316 | + if (!isset($opts["chunk_separator"])) $opts["chunk_separator"] = " ... "; |
|
1317 | + if (!isset($opts["limit"])) $opts["limit"] = 256; |
|
1318 | + if (!isset($opts["limit_passages"])) $opts["limit_passages"] = 0; |
|
1319 | + if (!isset($opts["limit_words"])) $opts["limit_words"] = 0; |
|
1320 | + if (!isset($opts["around"])) $opts["around"] = 5; |
|
1321 | + if (!isset($opts["exact_phrase"])) $opts["exact_phrase"] = false; |
|
1322 | + if (!isset($opts["single_passage"])) $opts["single_passage"] = false; |
|
1323 | + if (!isset($opts["use_boundaries"])) $opts["use_boundaries"] = false; |
|
1324 | + if (!isset($opts["weight_order"])) $opts["weight_order"] = false; |
|
1325 | + if (!isset($opts["query_mode"])) $opts["query_mode"] = false; |
|
1326 | + if (!isset($opts["force_all_words"])) $opts["force_all_words"] = false; |
|
1327 | + if (!isset($opts["start_passage_id"])) $opts["start_passage_id"] = 1; |
|
1328 | + if (!isset($opts["load_files"])) $opts["load_files"] = false; |
|
1329 | + if (!isset($opts["html_strip_mode"])) $opts["html_strip_mode"] = "index"; |
|
1330 | + if (!isset($opts["allow_empty"])) $opts["allow_empty"] = false; |
|
1331 | + if (!isset($opts["passage_boundary"])) $opts["passage_boundary"] = "none"; |
|
1332 | + if (!isset($opts["emit_zones"])) $opts["emit_zones"] = false; |
|
1333 | + |
|
1334 | + ///////////////// |
|
1335 | + // build request |
|
1336 | + ///////////////// |
|
1337 | + |
|
1338 | + // v.1.2 req |
|
1339 | + $flags = 1; // remove spaces |
|
1340 | + if ($opts["exact_phrase"]) $flags |= 2; |
|
1341 | + if ($opts["single_passage"]) $flags |= 4; |
|
1342 | + if ($opts["use_boundaries"]) $flags |= 8; |
|
1343 | + if ($opts["weight_order"]) $flags |= 16; |
|
1344 | + if ($opts["query_mode"]) $flags |= 32; |
|
1345 | + if ($opts["force_all_words"]) $flags |= 64; |
|
1346 | + if ($opts["load_files"]) $flags |= 128; |
|
1347 | + if ($opts["allow_empty"]) $flags |= 256; |
|
1348 | + if ($opts["emit_zones"]) $flags |= 512; |
|
1349 | + $req = pack("NN", 0, $flags); // mode=0, flags=$flags |
|
1350 | + $req .= pack("N", strlen($index)).$index; // req index |
|
1351 | + $req .= pack("N", strlen($words)).$words; // req words |
|
1352 | + |
|
1353 | + // options |
|
1354 | + $req .= pack("N", strlen($opts["before_match"])).$opts["before_match"]; |
|
1355 | + $req .= pack("N", strlen($opts["after_match"])).$opts["after_match"]; |
|
1356 | + $req .= pack("N", strlen($opts["chunk_separator"])).$opts["chunk_separator"]; |
|
1357 | + $req .= pack("NN", (int) $opts["limit"], (int) $opts["around"]); |
|
1358 | + $req .= pack("NNN", (int) $opts["limit_passages"], (int) $opts["limit_words"], (int) $opts["start_passage_id"]); // v.1.2 |
|
1359 | + $req .= pack("N", strlen($opts["html_strip_mode"])).$opts["html_strip_mode"]; |
|
1360 | + $req .= pack("N", strlen($opts["passage_boundary"])).$opts["passage_boundary"]; |
|
1361 | + |
|
1362 | + // documents |
|
1363 | + $req .= pack("N", count($docs)); |
|
1364 | + foreach ($docs as $doc) |
|
1365 | + { |
|
1366 | + assert(is_string($doc)); |
|
1367 | + $req .= pack("N", strlen($doc)).$doc; |
|
1368 | + } |
|
1369 | + |
|
1370 | + //////////////////////////// |
|
1371 | + // send query, get response |
|
1372 | + //////////////////////////// |
|
1373 | + |
|
1374 | + $len = strlen($req); |
|
1375 | + $req = pack("nnN", SEARCHD_COMMAND_EXCERPT, VER_COMMAND_EXCERPT, $len).$req; // add header |
|
1376 | + if (!($this->_Send($fp, $req, $len + 8)) || |
|
1377 | + !($response = $this->_GetResponse($fp, VER_COMMAND_EXCERPT))) |
|
1378 | + { |
|
1379 | + $this->_MBPop(); |
|
1380 | + return false; |
|
1381 | + } |
|
1382 | + |
|
1383 | + ////////////////// |
|
1384 | + // parse response |
|
1385 | + ////////////////// |
|
1386 | + |
|
1387 | + $pos = 0; |
|
1388 | + $res = array(); |
|
1389 | + $rlen = strlen($response); |
|
1390 | + for ($i = 0; $i < count($docs); $i++) |
|
1391 | + { |
|
1392 | + list(,$len) = unpack("N*", substr($response, $pos, 4)); |
|
1393 | + $pos += 4; |
|
1394 | + |
|
1395 | + if ($pos + $len > $rlen) |
|
1396 | + { |
|
1397 | + $this->_error = "incomplete reply"; |
|
1398 | + $this->_MBPop(); |
|
1399 | + return false; |
|
1400 | + } |
|
1401 | + $res[] = $len ? substr ($response, $pos, $len) : ""; |
|
1402 | + $pos += $len; |
|
1403 | + } |
|
1404 | + |
|
1405 | + $this->_MBPop(); |
|
1406 | + return $res; |
|
1407 | + } |
|
1408 | + |
|
1409 | + |
|
1410 | + ///////////////////////////////////////////////////////////////////////////// |
|
1411 | + // keyword generation |
|
1412 | + ///////////////////////////////////////////////////////////////////////////// |
|
1413 | + |
|
1414 | + /// connect to searchd server, and generate keyword list for a given query |
|
1415 | + /// returns false on failure, |
|
1416 | + /// an array of words on success |
|
1417 | + public function BuildKeywords($query, $index, $hits) |
|
1418 | + { |
|
1419 | + assert(is_string($query)); |
|
1420 | + assert(is_string($index)); |
|
1421 | + assert(is_bool($hits)); |
|
1422 | + |
|
1423 | + $this->_MBPush(); |
|
1424 | + |
|
1425 | + if (!($fp = $this->_Connect())) |
|
1426 | + { |
|
1427 | + $this->_MBPop(); |
|
1428 | + return false; |
|
1429 | + } |
|
1430 | + |
|
1431 | + ///////////////// |
|
1432 | + // build request |
|
1433 | + ///////////////// |
|
1434 | + |
|
1435 | + // v.1.0 req |
|
1436 | + $req = pack("N", strlen($query)).$query; // req query |
|
1437 | + $req .= pack("N", strlen($index)).$index; // req index |
|
1438 | + $req .= pack("N", (int) $hits); |
|
1439 | + |
|
1440 | + //////////////////////////// |
|
1441 | + // send query, get response |
|
1442 | + //////////////////////////// |
|
1443 | + |
|
1444 | + $len = strlen($req); |
|
1445 | + $req = pack("nnN", SEARCHD_COMMAND_KEYWORDS, VER_COMMAND_KEYWORDS, $len).$req; // add header |
|
1446 | + if (!($this->_Send($fp, $req, $len + 8)) || |
|
1447 | + !($response = $this->_GetResponse($fp, VER_COMMAND_KEYWORDS))) |
|
1448 | + { |
|
1449 | + $this->_MBPop(); |
|
1450 | + return false; |
|
1451 | + } |
|
1452 | + |
|
1453 | + ////////////////// |
|
1454 | + // parse response |
|
1455 | + ////////////////// |
|
1456 | + |
|
1457 | + $pos = 0; |
|
1458 | + $res = array(); |
|
1459 | + $rlen = strlen($response); |
|
1460 | + list(,$nwords) = unpack("N*", substr($response, $pos, 4)); |
|
1461 | + $pos += 4; |
|
1462 | + for ($i = 0; $i < $nwords; $i++) |
|
1463 | + { |
|
1464 | + list(,$len) = unpack("N*", substr($response, $pos, 4)); $pos += 4; |
|
1465 | + $tokenized = $len ? substr ($response, $pos, $len) : ""; |
|
1466 | + $pos += $len; |
|
1467 | + |
|
1468 | + list(,$len) = unpack("N*", substr($response, $pos, 4)); $pos += 4; |
|
1469 | + $normalized = $len ? substr ($response, $pos, $len) : ""; |
|
1470 | + $pos += $len; |
|
1471 | + |
|
1472 | + $res[] = array("tokenized"=>$tokenized, "normalized"=>$normalized); |
|
1473 | + |
|
1474 | + if ($hits) |
|
1475 | + { |
|
1476 | + list($ndocs, $nhits) = array_values(unpack("N*N*", substr($response, $pos, 8))); |
|
1477 | + $pos += 8; |
|
1478 | + $res [$i]["docs"] = $ndocs; |
|
1479 | + $res [$i]["hits"] = $nhits; |
|
1480 | + } |
|
1481 | + |
|
1482 | + if ($pos > $rlen) |
|
1483 | + { |
|
1484 | + $this->_error = "incomplete reply"; |
|
1485 | + $this->_MBPop(); |
|
1486 | + return false; |
|
1487 | + } |
|
1488 | + } |
|
1489 | + |
|
1490 | + $this->_MBPop(); |
|
1491 | + return $res; |
|
1492 | + } |
|
1493 | + |
|
1494 | + public function EscapeString($string) |
|
1495 | + { |
|
1496 | + $from = array('\\', '(', ')', '|', '-', '!', '@', '~', '"', '&', '/', '^', '$', '='); |
|
1497 | + $to = array('\\\\', '\(', '\)', '\|', '\-', '\!', '\@', '\~', '\"', '\&', '\/', '\^', '\$', '\='); |
|
1498 | + |
|
1499 | + return str_replace($from, $to, $string); |
|
1500 | + } |
|
1501 | + |
|
1502 | + ///////////////////////////////////////////////////////////////////////////// |
|
1503 | + // attribute updates |
|
1504 | + ///////////////////////////////////////////////////////////////////////////// |
|
1505 | + |
|
1506 | + /// batch update given attributes in given rows in given indexes |
|
1507 | + /// returns amount of updated documents (0 or more) on success, or -1 on failure |
|
1508 | + public function UpdateAttributes($index, $attrs, $values, $mva = false) |
|
1509 | + { |
|
1510 | + // verify everything |
|
1511 | + assert(is_string($index)); |
|
1512 | + assert(is_bool($mva)); |
|
1513 | + |
|
1514 | + assert(is_array($attrs)); |
|
1515 | + foreach ($attrs as $attr) |
|
1516 | + assert(is_string($attr)); |
|
1517 | + |
|
1518 | + assert(is_array($values)); |
|
1519 | + foreach ($values as $id=>$entry) |
|
1520 | + { |
|
1521 | + assert(is_numeric($id)); |
|
1522 | + assert(is_array($entry)); |
|
1523 | + assert(count($entry) == count($attrs)); |
|
1524 | + foreach ($entry as $v) |
|
1525 | + { |
|
1526 | + if ($mva) |
|
1527 | + { |
|
1528 | + assert(is_array($v)); |
|
1529 | + foreach ($v as $vv) |
|
1530 | + assert(is_int($vv)); |
|
1531 | + } else |
|
1532 | + assert(is_int($v)); |
|
1533 | + } |
|
1534 | + } |
|
1535 | + |
|
1536 | + // build request |
|
1537 | + $this->_MBPush(); |
|
1538 | + $req = pack("N", strlen($index)).$index; |
|
1539 | + |
|
1540 | + $req .= pack("N", count($attrs)); |
|
1541 | + foreach ($attrs as $attr) |
|
1542 | + { |
|
1543 | + $req .= pack("N", strlen($attr)).$attr; |
|
1544 | + $req .= pack("N", $mva ? 1 : 0); |
|
1545 | + } |
|
1546 | + |
|
1547 | + $req .= pack("N", count($values)); |
|
1548 | + foreach ($values as $id=>$entry) |
|
1549 | + { |
|
1550 | + $req .= sphPackU64($id); |
|
1551 | + foreach ($entry as $v) |
|
1552 | + { |
|
1553 | + $req .= pack("N", $mva ? count($v) : $v); |
|
1554 | + if ($mva) |
|
1555 | + foreach ($v as $vv) |
|
1556 | + $req .= pack("N", $vv); |
|
1557 | + } |
|
1558 | + } |
|
1559 | + |
|
1560 | + // connect, send query, get response |
|
1561 | + if (!($fp = $this->_Connect())) |
|
1562 | + { |
|
1563 | + $this->_MBPop(); |
|
1564 | + return -1; |
|
1565 | + } |
|
1566 | + |
|
1567 | + $len = strlen($req); |
|
1568 | + $req = pack("nnN", SEARCHD_COMMAND_UPDATE, VER_COMMAND_UPDATE, $len).$req; // add header |
|
1569 | + if (!$this->_Send($fp, $req, $len + 8)) |
|
1570 | + { |
|
1571 | + $this->_MBPop(); |
|
1572 | + return -1; |
|
1573 | + } |
|
1574 | + |
|
1575 | + if (!($response = $this->_GetResponse($fp, VER_COMMAND_UPDATE))) |
|
1576 | + { |
|
1577 | + $this->_MBPop(); |
|
1578 | + return -1; |
|
1579 | + } |
|
1580 | + |
|
1581 | + // parse response |
|
1582 | + list(,$updated) = unpack("N*", substr($response, 0, 4)); |
|
1583 | + $this->_MBPop(); |
|
1584 | + return $updated; |
|
1585 | + } |
|
1586 | + |
|
1587 | + ///////////////////////////////////////////////////////////////////////////// |
|
1588 | + // persistent connections |
|
1589 | + ///////////////////////////////////////////////////////////////////////////// |
|
1590 | + |
|
1591 | + public function Open() |
|
1592 | + { |
|
1593 | + if ($this->_socket !== false) |
|
1594 | + { |
|
1595 | + $this->_error = 'already connected'; |
|
1596 | + return false; |
|
1597 | + } |
|
1598 | + if (!$fp = $this->_Connect()) |
|
1599 | + return false; |
|
1600 | + |
|
1601 | + // command, command version = 0, body length = 4, body = 1 |
|
1602 | + $req = pack("nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
|
1603 | + if (!$this->_Send($fp, $req, 12)) |
|
1604 | + return false; |
|
1605 | + |
|
1606 | + $this->_socket = $fp; |
|
1607 | + return true; |
|
1608 | + } |
|
1609 | + |
|
1610 | + public function Close() |
|
1611 | + { |
|
1612 | + if ($this->_socket === false) |
|
1613 | + { |
|
1614 | + $this->_error = 'not connected'; |
|
1615 | + return false; |
|
1616 | + } |
|
1617 | + |
|
1618 | + fclose($this->_socket); |
|
1619 | + $this->_socket = false; |
|
1620 | + |
|
1621 | + return true; |
|
1622 | + } |
|
1623 | + |
|
1624 | + ////////////////////////////////////////////////////////////////////////// |
|
1625 | + // status |
|
1626 | + ////////////////////////////////////////////////////////////////////////// |
|
1627 | + |
|
1628 | + public function Status() |
|
1629 | + { |
|
1630 | + $this->_MBPush(); |
|
1631 | + if (!($fp = $this->_Connect())) |
|
1632 | + { |
|
1633 | + $this->_MBPop(); |
|
1634 | + return false; |
|
1635 | + } |
|
1636 | + |
|
1637 | + $req = pack("nnNN", SEARCHD_COMMAND_STATUS, VER_COMMAND_STATUS, 4, 1); // len=4, body=1 |
|
1638 | + if (!($this->_Send($fp, $req, 12)) || |
|
1639 | + !($response = $this->_GetResponse($fp, VER_COMMAND_STATUS))) |
|
1640 | + { |
|
1641 | + $this->_MBPop(); |
|
1642 | + return false; |
|
1643 | + } |
|
1644 | + |
|
1645 | + $res = substr($response, 4); // just ignore length, error handling, etc |
|
1646 | + $p = 0; |
|
1647 | + list ($rows, $cols) = array_values(unpack("N*N*", substr($response, $p, 8))); $p += 8; |
|
1648 | + |
|
1649 | + $res = array(); |
|
1650 | + for ($i = 0; $i < $rows; $i++) |
|
1651 | + for ($j = 0; $j < $cols; $j++) |
|
1652 | + { |
|
1653 | + list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
|
1654 | + $res[$i][] = substr($response, $p, $len); $p += $len; |
|
1655 | + } |
|
1656 | + |
|
1657 | + $this->_MBPop(); |
|
1658 | + return $res; |
|
1659 | + } |
|
1660 | + |
|
1661 | + ////////////////////////////////////////////////////////////////////////// |
|
1662 | + // flush |
|
1663 | + ////////////////////////////////////////////////////////////////////////// |
|
1664 | + |
|
1665 | + public function FlushAttributes() |
|
1666 | + { |
|
1667 | + $this->_MBPush(); |
|
1668 | + if (!($fp = $this->_Connect())) |
|
1669 | + { |
|
1670 | + $this->_MBPop(); |
|
1671 | + return -1; |
|
1672 | + } |
|
1673 | + |
|
1674 | + $req = pack("nnN", SEARCHD_COMMAND_FLUSHATTRS, VER_COMMAND_FLUSHATTRS, 0); // len=0 |
|
1675 | + if (!($this->_Send($fp, $req, 8)) || |
|
1676 | + !($response = $this->_GetResponse($fp, VER_COMMAND_FLUSHATTRS))) |
|
1677 | + { |
|
1678 | + $this->_MBPop(); |
|
1679 | + return -1; |
|
1680 | + } |
|
1681 | + |
|
1682 | + $tag = -1; |
|
1683 | + if (strlen($response) == 4) |
|
1684 | + list(,$tag) = unpack("N*", $response); |
|
1685 | + else |
|
1686 | + $this->_error = "unexpected response length"; |
|
1687 | + |
|
1688 | + $this->_MBPop(); |
|
1689 | + return $tag; |
|
1690 | + } |
|
1691 | 1691 | } |
1692 | 1692 | |
1693 | 1693 | // |
@@ -1003,22 +1003,22 @@ discard block |
||
1003 | 1003 | $req .= pack("N", $filter["type"]); |
1004 | 1004 | switch ($filter["type"]) |
1005 | 1005 | { |
1006 | - case SPH_FILTER_VALUES: |
|
1007 | - $req .= pack("N", count($filter["values"])); |
|
1008 | - foreach ($filter["values"] as $value) |
|
1009 | - $req .= sphPackI64($value); |
|
1010 | - break; |
|
1011 | - |
|
1012 | - case SPH_FILTER_RANGE: |
|
1013 | - $req .= sphPackI64($filter["min"]).sphPackI64($filter["max"]); |
|
1014 | - break; |
|
1015 | - |
|
1016 | - case SPH_FILTER_FLOATRANGE: |
|
1017 | - $req .= $this->_PackFloat($filter["min"]).$this->_PackFloat($filter["max"]); |
|
1018 | - break; |
|
1019 | - |
|
1020 | - default: |
|
1021 | - assert(0 && "internal error: unhandled filter type"); |
|
1006 | + case SPH_FILTER_VALUES: |
|
1007 | + $req .= pack("N", count($filter["values"])); |
|
1008 | + foreach ($filter["values"] as $value) |
|
1009 | + $req .= sphPackI64($value); |
|
1010 | + break; |
|
1011 | + |
|
1012 | + case SPH_FILTER_RANGE: |
|
1013 | + $req .= sphPackI64($filter["min"]).sphPackI64($filter["max"]); |
|
1014 | + break; |
|
1015 | + |
|
1016 | + case SPH_FILTER_FLOATRANGE: |
|
1017 | + $req .= $this->_PackFloat($filter["min"]).$this->_PackFloat($filter["max"]); |
|
1018 | + break; |
|
1019 | + |
|
1020 | + default: |
|
1021 | + assert(0 && "internal error: unhandled filter type"); |
|
1022 | 1022 | } |
1023 | 1023 | $req .= pack("N", $filter["exclude"]); |
1024 | 1024 | } |
@@ -1073,9 +1073,9 @@ discard block |
||
1073 | 1073 | $req .= sphPackU64($id); |
1074 | 1074 | switch ($entry["type"]) |
1075 | 1075 | { |
1076 | - case SPH_ATTR_FLOAT: $req .= $this->_PackFloat($val); break; |
|
1077 | - case SPH_ATTR_BIGINT: $req .= sphPackI64($val); break; |
|
1078 | - default: $req .= pack("N", $val); break; |
|
1076 | + case SPH_ATTR_FLOAT: $req .= $this->_PackFloat($val); break; |
|
1077 | + case SPH_ATTR_BIGINT: $req .= sphPackI64($val); break; |
|
1078 | + default: $req .= pack("N", $val); break; |
|
1079 | 1079 | } |
1080 | 1080 | } |
1081 | 1081 | } |
@@ -133,14 +133,16 @@ discard block |
||
133 | 133 | } |
134 | 134 | |
135 | 135 | // x32, int |
136 | - if (is_int($v)) |
|
137 | - return pack("NN", $v < 0 ? -1 : 0, $v); |
|
136 | + if (is_int($v)) { |
|
137 | + return pack("NN", $v < 0 ? -1 : 0, $v); |
|
138 | + } |
|
138 | 139 | |
139 | 140 | // x32, bcmath |
140 | 141 | if (function_exists("bcmul")) |
141 | 142 | { |
142 | - if (bccomp($v, 0) == -1) |
|
143 | - $v = bcadd("18446744073709551616", $v); |
|
143 | + if (bccomp($v, 0) == -1) { |
|
144 | + $v = bcadd("18446744073709551616", $v); |
|
145 | + } |
|
144 | 146 | $h = bcdiv($v, "4294967296", 0); |
145 | 147 | $l = bcmod($v, "4294967296"); |
146 | 148 | return pack("NN", (float) $h, (float) $l); // conversion to float is intentional; int would lose 31st bit |
@@ -158,9 +160,9 @@ discard block |
||
158 | 160 | |
159 | 161 | if ($v < 0) |
160 | 162 | { |
161 | - if ($l == 0) |
|
162 | - $h = 4294967296.0 - $h; |
|
163 | - else |
|
163 | + if ($l == 0) { |
|
164 | + $h = 4294967296.0 - $h; |
|
165 | + } else |
|
164 | 166 | { |
165 | 167 | $h = 4294967295.0 - $h; |
166 | 168 | $l = 4294967296.0 - $l; |
@@ -180,8 +182,9 @@ discard block |
||
180 | 182 | assert($v >= 0); |
181 | 183 | |
182 | 184 | // x64, int |
183 | - if (is_int($v)) |
|
184 | - return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
185 | + if (is_int($v)) { |
|
186 | + return pack("NN", $v >> 32, $v & 0xFFFFFFFF); |
|
187 | + } |
|
185 | 188 | |
186 | 189 | // x64, bcmath |
187 | 190 | if (function_exists("bcmul")) |
@@ -204,8 +207,9 @@ discard block |
||
204 | 207 | } |
205 | 208 | |
206 | 209 | // x32, int |
207 | - if (is_int($v)) |
|
208 | - return pack("NN", 0, $v); |
|
210 | + if (is_int($v)) { |
|
211 | + return pack("NN", 0, $v); |
|
212 | + } |
|
209 | 213 | |
210 | 214 | // x32, bcmath |
211 | 215 | if (function_exists("bcmul")) |
@@ -235,16 +239,23 @@ discard block |
||
235 | 239 | |
236 | 240 | if (PHP_INT_SIZE >= 8) |
237 | 241 | { |
238 | - if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
239 | - if ($lo < 0) $lo += (1 << 32); |
|
242 | + if ($hi < 0) { |
|
243 | + $hi += (1 << 32); |
|
244 | + } |
|
245 | + // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
246 | + if ($lo < 0) { |
|
247 | + $lo += (1 << 32); |
|
248 | + } |
|
240 | 249 | |
241 | 250 | // x64, int |
242 | - if ($hi <= 2147483647) |
|
243 | - return ($hi << 32) + $lo; |
|
251 | + if ($hi <= 2147483647) { |
|
252 | + return ($hi << 32) + $lo; |
|
253 | + } |
|
244 | 254 | |
245 | 255 | // x64, bcmath |
246 | - if (function_exists("bcmul")) |
|
247 | - return bcadd($lo, bcmul($hi, "4294967296")); |
|
256 | + if (function_exists("bcmul")) { |
|
257 | + return bcadd($lo, bcmul($hi, "4294967296")); |
|
258 | + } |
|
248 | 259 | |
249 | 260 | // x64, no-bcmath |
250 | 261 | $C = 100000; |
@@ -256,16 +267,18 @@ discard block |
||
256 | 267 | $l = $l % $C; |
257 | 268 | } |
258 | 269 | |
259 | - if ($h == 0) |
|
260 | - return $l; |
|
270 | + if ($h == 0) { |
|
271 | + return $l; |
|
272 | + } |
|
261 | 273 | return sprintf("%d%05d", $h, $l); |
262 | 274 | } |
263 | 275 | |
264 | 276 | // x32, int |
265 | 277 | if ($hi == 0) |
266 | 278 | { |
267 | - if ($lo > 0) |
|
268 | - return $lo; |
|
279 | + if ($lo > 0) { |
|
280 | + return $lo; |
|
281 | + } |
|
269 | 282 | return sprintf("%u", $lo); |
270 | 283 | } |
271 | 284 | |
@@ -273,8 +286,9 @@ discard block |
||
273 | 286 | $lo = sprintf("%u", $lo); |
274 | 287 | |
275 | 288 | // x32, bcmath |
276 | - if (function_exists("bcmul")) |
|
277 | - return bcadd($lo, bcmul($hi, "4294967296")); |
|
289 | + if (function_exists("bcmul")) { |
|
290 | + return bcadd($lo, bcmul($hi, "4294967296")); |
|
291 | + } |
|
278 | 292 | |
279 | 293 | // x32, no-bcmath |
280 | 294 | $hi = (float) $hi; |
@@ -289,8 +303,9 @@ discard block |
||
289 | 303 | |
290 | 304 | $h = sprintf("%.0f", $h); |
291 | 305 | $l = sprintf("%07.0f", $l); |
292 | - if ($h == "0") |
|
293 | - return sprintf("%.0f", (float) $l); |
|
306 | + if ($h == "0") { |
|
307 | + return sprintf("%.0f", (float) $l); |
|
308 | + } |
|
294 | 309 | return $h.$l; |
295 | 310 | } |
296 | 311 | |
@@ -302,8 +317,13 @@ discard block |
||
302 | 317 | // x64 |
303 | 318 | if (PHP_INT_SIZE >= 8) |
304 | 319 | { |
305 | - if ($hi < 0) $hi += (1 << 32); // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
306 | - if ($lo < 0) $lo += (1 << 32); |
|
320 | + if ($hi < 0) { |
|
321 | + $hi += (1 << 32); |
|
322 | + } |
|
323 | + // because php 5.2.2 to 5.2.5 is totally fucked up again |
|
324 | + if ($lo < 0) { |
|
325 | + $lo += (1 << 32); |
|
326 | + } |
|
307 | 327 | |
308 | 328 | return ($hi << 32) + $lo; |
309 | 329 | } |
@@ -311,15 +331,17 @@ discard block |
||
311 | 331 | // x32, int |
312 | 332 | if ($hi == 0) |
313 | 333 | { |
314 | - if ($lo > 0) |
|
315 | - return $lo; |
|
334 | + if ($lo > 0) { |
|
335 | + return $lo; |
|
336 | + } |
|
316 | 337 | return sprintf("%u", $lo); |
317 | 338 | } |
318 | 339 | // x32, int |
319 | 340 | elseif ($hi == -1) |
320 | 341 | { |
321 | - if ($lo < 0) |
|
322 | - return $lo; |
|
342 | + if ($lo < 0) { |
|
343 | + return $lo; |
|
344 | + } |
|
323 | 345 | return sprintf("%.0f", $lo - 4294967296.0); |
324 | 346 | } |
325 | 347 | |
@@ -337,8 +359,9 @@ discard block |
||
337 | 359 | $lo = sprintf("%u", $lo); |
338 | 360 | |
339 | 361 | // x32, bcmath |
340 | - if (function_exists("bcmul")) |
|
341 | - return $neg.bcadd(bcadd($lo, bcmul($hi, "4294967296")), $c); |
|
362 | + if (function_exists("bcmul")) { |
|
363 | + return $neg.bcadd(bcadd($lo, bcmul($hi, "4294967296")), $c); |
|
364 | + } |
|
342 | 365 | |
343 | 366 | // x32, no-bcmath |
344 | 367 | $hi = (float) $hi; |
@@ -358,8 +381,9 @@ discard block |
||
358 | 381 | |
359 | 382 | $h = sprintf("%.0f", $h); |
360 | 383 | $l = sprintf("%07.0f", $l); |
361 | - if ($h == "0") |
|
362 | - return $neg.sprintf("%.0f", (float) $l); |
|
384 | + if ($h == "0") { |
|
385 | + return $neg.sprintf("%.0f", (float) $l); |
|
386 | + } |
|
363 | 387 | return $neg.$h.$l; |
364 | 388 | } |
365 | 389 | |
@@ -369,10 +393,11 @@ discard block |
||
369 | 393 | if (PHP_INT_SIZE >= 8) |
370 | 394 | { |
371 | 395 | // x64 route, workaround broken unpack() in 5.2.2+ |
372 | - if ($value < 0) $value += (1 << 32); |
|
396 | + if ($value < 0) { |
|
397 | + $value += (1 << 32); |
|
398 | + } |
|
373 | 399 | return $value; |
374 | - } |
|
375 | - else |
|
400 | + } else |
|
376 | 401 | { |
377 | 402 | // x32 route, workaround php signed/unsigned braindamage |
378 | 403 | return sprintf("%u", $value); |
@@ -470,8 +495,9 @@ discard block |
||
470 | 495 | |
471 | 496 | public function __destruct() |
472 | 497 | { |
473 | - if ($this->_socket !== false) |
|
474 | - fclose($this->_socket); |
|
498 | + if ($this->_socket !== false) { |
|
499 | + fclose($this->_socket); |
|
500 | + } |
|
475 | 501 | } |
476 | 502 | |
477 | 503 | /// get last error message (string) |
@@ -549,8 +575,9 @@ discard block |
||
549 | 575 | /// leave mbstring workaround mode |
550 | 576 | public function _MBPop() |
551 | 577 | { |
552 | - if ($this->_mbenc) |
|
553 | - mb_internal_encoding($this->_mbenc); |
|
578 | + if ($this->_mbenc) { |
|
579 | + mb_internal_encoding($this->_mbenc); |
|
580 | + } |
|
554 | 581 | } |
555 | 582 | |
556 | 583 | /// connect to searchd server |
@@ -560,8 +587,9 @@ discard block |
||
560 | 587 | { |
561 | 588 | // we are in persistent connection mode, so we have a socket |
562 | 589 | // however, need to check whether it's still alive |
563 | - if (!@feof($this->_socket)) |
|
564 | - return $this->_socket; |
|
590 | + if (!@feof($this->_socket)) { |
|
591 | + return $this->_socket; |
|
592 | + } |
|
565 | 593 | |
566 | 594 | // force reopen |
567 | 595 | $this->_socket = false; |
@@ -575,24 +603,25 @@ discard block |
||
575 | 603 | { |
576 | 604 | $host = $this->_path; |
577 | 605 | $port = 0; |
578 | - } |
|
579 | - else |
|
606 | + } else |
|
580 | 607 | { |
581 | 608 | $host = $this->_host; |
582 | 609 | $port = $this->_port; |
583 | 610 | } |
584 | 611 | |
585 | - if ($this->_timeout <= 0) |
|
586 | - $fp = @fsockopen($host, $port, $errno, $errstr); |
|
587 | - else |
|
588 | - $fp = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); |
|
612 | + if ($this->_timeout <= 0) { |
|
613 | + $fp = @fsockopen($host, $port, $errno, $errstr); |
|
614 | + } else { |
|
615 | + $fp = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); |
|
616 | + } |
|
589 | 617 | |
590 | 618 | if (!$fp) |
591 | 619 | { |
592 | - if ($this->_path) |
|
593 | - $location = $this->_path; |
|
594 | - else |
|
595 | - $location = "{$this->_host}:{$this->_port}"; |
|
620 | + if ($this->_path) { |
|
621 | + $location = $this->_path; |
|
622 | + } else { |
|
623 | + $location = "{$this->_host}:{$this->_port}"; |
|
624 | + } |
|
596 | 625 | |
597 | 626 | $errstr = trim($errstr); |
598 | 627 | $this->_error = "connection to $location failed (errno=$errno, msg=$errstr)"; |
@@ -645,8 +674,9 @@ discard block |
||
645 | 674 | } |
646 | 675 | } |
647 | 676 | } |
648 | - if ($this->_socket === false) |
|
649 | - fclose($fp); |
|
677 | + if ($this->_socket === false) { |
|
678 | + fclose($fp); |
|
679 | + } |
|
650 | 680 | |
651 | 681 | // check response |
652 | 682 | $read = strlen($response); |
@@ -706,10 +736,12 @@ discard block |
||
706 | 736 | assert($max >= 0); |
707 | 737 | $this->_offset = $offset; |
708 | 738 | $this->_limit = $limit; |
709 | - if ($max > 0) |
|
710 | - $this->_maxmatches = $max; |
|
711 | - if ($cutoff > 0) |
|
712 | - $this->_cutoff = $cutoff; |
|
739 | + if ($max > 0) { |
|
740 | + $this->_maxmatches = $max; |
|
741 | + } |
|
742 | + if ($cutoff > 0) { |
|
743 | + $this->_cutoff = $cutoff; |
|
744 | + } |
|
713 | 745 | } |
714 | 746 | |
715 | 747 | /// set maximum query time, in milliseconds, per-index |
@@ -763,8 +795,9 @@ discard block |
||
763 | 795 | public function SetWeights($weights) |
764 | 796 | { |
765 | 797 | assert(is_array($weights)); |
766 | - foreach ($weights as $weight) |
|
767 | - assert(is_int($weight)); |
|
798 | + foreach ($weights as $weight) { |
|
799 | + assert(is_int($weight)); |
|
800 | + } |
|
768 | 801 | |
769 | 802 | $this->_weights = $weights; |
770 | 803 | } |
@@ -814,8 +847,9 @@ discard block |
||
814 | 847 | |
815 | 848 | if (is_array($values) && count($values)) |
816 | 849 | { |
817 | - foreach ($values as $value) |
|
818 | - assert(is_numeric($value)); |
|
850 | + foreach ($values as $value) { |
|
851 | + assert(is_numeric($value)); |
|
852 | + } |
|
819 | 853 | |
820 | 854 | $this->_filters[] = array("type"=>SPH_FILTER_VALUES, "attr"=>$attribute, "exclude"=>$exclude, "values"=>$values); |
821 | 855 | } |
@@ -954,15 +988,18 @@ discard block |
||
954 | 988 | $results = $this->RunQueries(); |
955 | 989 | $this->_reqs = array(); // just in case it failed too early |
956 | 990 | |
957 | - if (!is_array($results)) |
|
958 | - return false; // probably network error; error message should be already filled |
|
991 | + if (!is_array($results)) { |
|
992 | + return false; |
|
993 | + } |
|
994 | + // probably network error; error message should be already filled |
|
959 | 995 | |
960 | 996 | $this->_error = $results[0]["error"]; |
961 | 997 | $this->_warning = $results[0]["warning"]; |
962 | - if ($results[0]["status"] == SEARCHD_ERROR) |
|
963 | - return false; |
|
964 | - else |
|
965 | - return $results[0]; |
|
998 | + if ($results[0]["status"] == SEARCHD_ERROR) { |
|
999 | + return false; |
|
1000 | + } else { |
|
1001 | + return $results[0]; |
|
1002 | + } |
|
966 | 1003 | } |
967 | 1004 | |
968 | 1005 | /// helper to pack floats in network byte order |
@@ -989,8 +1026,9 @@ discard block |
||
989 | 1026 | $req .= pack("N", strlen($this->_sortby)).$this->_sortby; |
990 | 1027 | $req .= pack("N", strlen($query)).$query; // query itself |
991 | 1028 | $req .= pack("N", count($this->_weights)); // weights |
992 | - foreach ($this->_weights as $weight) |
|
993 | - $req .= pack("N", (int) $weight); |
|
1029 | + foreach ($this->_weights as $weight) { |
|
1030 | + $req .= pack("N", (int) $weight); |
|
1031 | + } |
|
994 | 1032 | $req .= pack("N", strlen($index)).$index; // indexes |
995 | 1033 | $req .= pack("N", 1); // id64 range marker |
996 | 1034 | $req .= sphPackU64($this->_min_id).sphPackU64($this->_max_id); // id64 range |
@@ -1005,8 +1043,9 @@ discard block |
||
1005 | 1043 | { |
1006 | 1044 | case SPH_FILTER_VALUES: |
1007 | 1045 | $req .= pack("N", count($filter["values"])); |
1008 | - foreach ($filter["values"] as $value) |
|
1009 | - $req .= sphPackI64($value); |
|
1046 | + foreach ($filter["values"] as $value) { |
|
1047 | + $req .= sphPackI64($value); |
|
1048 | + } |
|
1010 | 1049 | break; |
1011 | 1050 | |
1012 | 1051 | case SPH_FILTER_RANGE: |
@@ -1045,16 +1084,18 @@ discard block |
||
1045 | 1084 | |
1046 | 1085 | // per-index weights |
1047 | 1086 | $req .= pack("N", count($this->_indexweights)); |
1048 | - foreach ($this->_indexweights as $idx=>$weight) |
|
1049 | - $req .= pack("N", strlen($idx)).$idx.pack("N", $weight); |
|
1087 | + foreach ($this->_indexweights as $idx=>$weight) { |
|
1088 | + $req .= pack("N", strlen($idx)).$idx.pack("N", $weight); |
|
1089 | + } |
|
1050 | 1090 | |
1051 | 1091 | // max query time |
1052 | 1092 | $req .= pack("N", $this->_maxquerytime); |
1053 | 1093 | |
1054 | 1094 | // per-field weights |
1055 | 1095 | $req .= pack("N", count($this->_fieldweights)); |
1056 | - foreach ($this->_fieldweights as $field=>$weight) |
|
1057 | - $req .= pack("N", strlen($field)).$field.pack("N", $weight); |
|
1096 | + foreach ($this->_fieldweights as $field=>$weight) { |
|
1097 | + $req .= pack("N", strlen($field)).$field.pack("N", $weight); |
|
1098 | + } |
|
1058 | 1099 | |
1059 | 1100 | // comment |
1060 | 1101 | $req .= pack("N", strlen($comment)).$comment; |
@@ -1200,8 +1241,7 @@ discard block |
||
1200 | 1241 | { |
1201 | 1242 | $doc = sphUnpackU64(substr($response, $p, 8)); $p += 8; |
1202 | 1243 | list(,$weight) = unpack("N*", substr($response, $p, 4)); $p += 4; |
1203 | - } |
|
1204 | - else |
|
1244 | + } else |
|
1205 | 1245 | { |
1206 | 1246 | list ($doc, $weight) = array_values(unpack("N*N*", |
1207 | 1247 | substr($response, $p, 8))); |
@@ -1211,10 +1251,11 @@ discard block |
||
1211 | 1251 | $weight = sprintf("%u", $weight); |
1212 | 1252 | |
1213 | 1253 | // create match entry |
1214 | - if ($this->_arrayresult) |
|
1215 | - $result["matches"][$idx] = array("id"=>$doc, "weight"=>$weight); |
|
1216 | - else |
|
1217 | - $result["matches"][$doc]["weight"] = $weight; |
|
1254 | + if ($this->_arrayresult) { |
|
1255 | + $result["matches"][$idx] = array("id"=>$doc, "weight"=>$weight); |
|
1256 | + } else { |
|
1257 | + $result["matches"][$doc]["weight"] = $weight; |
|
1258 | + } |
|
1218 | 1259 | |
1219 | 1260 | // parse and create attributes |
1220 | 1261 | $attrvals = array(); |
@@ -1257,10 +1298,11 @@ discard block |
||
1257 | 1298 | } |
1258 | 1299 | } |
1259 | 1300 | |
1260 | - if ($this->_arrayresult) |
|
1261 | - $result["matches"][$idx]["attrs"] = $attrvals; |
|
1262 | - else |
|
1263 | - $result["matches"][$doc]["attrs"] = $attrvals; |
|
1301 | + if ($this->_arrayresult) { |
|
1302 | + $result["matches"][$idx]["attrs"] = $attrvals; |
|
1303 | + } else { |
|
1304 | + $result["matches"][$doc]["attrs"] = $attrvals; |
|
1305 | + } |
|
1264 | 1306 | } |
1265 | 1307 | |
1266 | 1308 | list ($total, $total_found, $msecs, $words) = |
@@ -1311,25 +1353,63 @@ discard block |
||
1311 | 1353 | // fixup options |
1312 | 1354 | ///////////////// |
1313 | 1355 | |
1314 | - if (!isset($opts["before_match"])) $opts["before_match"] = "<b>"; |
|
1315 | - if (!isset($opts["after_match"])) $opts["after_match"] = "</b>"; |
|
1316 | - if (!isset($opts["chunk_separator"])) $opts["chunk_separator"] = " ... "; |
|
1317 | - if (!isset($opts["limit"])) $opts["limit"] = 256; |
|
1318 | - if (!isset($opts["limit_passages"])) $opts["limit_passages"] = 0; |
|
1319 | - if (!isset($opts["limit_words"])) $opts["limit_words"] = 0; |
|
1320 | - if (!isset($opts["around"])) $opts["around"] = 5; |
|
1321 | - if (!isset($opts["exact_phrase"])) $opts["exact_phrase"] = false; |
|
1322 | - if (!isset($opts["single_passage"])) $opts["single_passage"] = false; |
|
1323 | - if (!isset($opts["use_boundaries"])) $opts["use_boundaries"] = false; |
|
1324 | - if (!isset($opts["weight_order"])) $opts["weight_order"] = false; |
|
1325 | - if (!isset($opts["query_mode"])) $opts["query_mode"] = false; |
|
1326 | - if (!isset($opts["force_all_words"])) $opts["force_all_words"] = false; |
|
1327 | - if (!isset($opts["start_passage_id"])) $opts["start_passage_id"] = 1; |
|
1328 | - if (!isset($opts["load_files"])) $opts["load_files"] = false; |
|
1329 | - if (!isset($opts["html_strip_mode"])) $opts["html_strip_mode"] = "index"; |
|
1330 | - if (!isset($opts["allow_empty"])) $opts["allow_empty"] = false; |
|
1331 | - if (!isset($opts["passage_boundary"])) $opts["passage_boundary"] = "none"; |
|
1332 | - if (!isset($opts["emit_zones"])) $opts["emit_zones"] = false; |
|
1356 | + if (!isset($opts["before_match"])) { |
|
1357 | + $opts["before_match"] = "<b>"; |
|
1358 | + } |
|
1359 | + if (!isset($opts["after_match"])) { |
|
1360 | + $opts["after_match"] = "</b>"; |
|
1361 | + } |
|
1362 | + if (!isset($opts["chunk_separator"])) { |
|
1363 | + $opts["chunk_separator"] = " ... "; |
|
1364 | + } |
|
1365 | + if (!isset($opts["limit"])) { |
|
1366 | + $opts["limit"] = 256; |
|
1367 | + } |
|
1368 | + if (!isset($opts["limit_passages"])) { |
|
1369 | + $opts["limit_passages"] = 0; |
|
1370 | + } |
|
1371 | + if (!isset($opts["limit_words"])) { |
|
1372 | + $opts["limit_words"] = 0; |
|
1373 | + } |
|
1374 | + if (!isset($opts["around"])) { |
|
1375 | + $opts["around"] = 5; |
|
1376 | + } |
|
1377 | + if (!isset($opts["exact_phrase"])) { |
|
1378 | + $opts["exact_phrase"] = false; |
|
1379 | + } |
|
1380 | + if (!isset($opts["single_passage"])) { |
|
1381 | + $opts["single_passage"] = false; |
|
1382 | + } |
|
1383 | + if (!isset($opts["use_boundaries"])) { |
|
1384 | + $opts["use_boundaries"] = false; |
|
1385 | + } |
|
1386 | + if (!isset($opts["weight_order"])) { |
|
1387 | + $opts["weight_order"] = false; |
|
1388 | + } |
|
1389 | + if (!isset($opts["query_mode"])) { |
|
1390 | + $opts["query_mode"] = false; |
|
1391 | + } |
|
1392 | + if (!isset($opts["force_all_words"])) { |
|
1393 | + $opts["force_all_words"] = false; |
|
1394 | + } |
|
1395 | + if (!isset($opts["start_passage_id"])) { |
|
1396 | + $opts["start_passage_id"] = 1; |
|
1397 | + } |
|
1398 | + if (!isset($opts["load_files"])) { |
|
1399 | + $opts["load_files"] = false; |
|
1400 | + } |
|
1401 | + if (!isset($opts["html_strip_mode"])) { |
|
1402 | + $opts["html_strip_mode"] = "index"; |
|
1403 | + } |
|
1404 | + if (!isset($opts["allow_empty"])) { |
|
1405 | + $opts["allow_empty"] = false; |
|
1406 | + } |
|
1407 | + if (!isset($opts["passage_boundary"])) { |
|
1408 | + $opts["passage_boundary"] = "none"; |
|
1409 | + } |
|
1410 | + if (!isset($opts["emit_zones"])) { |
|
1411 | + $opts["emit_zones"] = false; |
|
1412 | + } |
|
1333 | 1413 | |
1334 | 1414 | ///////////////// |
1335 | 1415 | // build request |
@@ -1337,15 +1417,33 @@ discard block |
||
1337 | 1417 | |
1338 | 1418 | // v.1.2 req |
1339 | 1419 | $flags = 1; // remove spaces |
1340 | - if ($opts["exact_phrase"]) $flags |= 2; |
|
1341 | - if ($opts["single_passage"]) $flags |= 4; |
|
1342 | - if ($opts["use_boundaries"]) $flags |= 8; |
|
1343 | - if ($opts["weight_order"]) $flags |= 16; |
|
1344 | - if ($opts["query_mode"]) $flags |= 32; |
|
1345 | - if ($opts["force_all_words"]) $flags |= 64; |
|
1346 | - if ($opts["load_files"]) $flags |= 128; |
|
1347 | - if ($opts["allow_empty"]) $flags |= 256; |
|
1348 | - if ($opts["emit_zones"]) $flags |= 512; |
|
1420 | + if ($opts["exact_phrase"]) { |
|
1421 | + $flags |= 2; |
|
1422 | + } |
|
1423 | + if ($opts["single_passage"]) { |
|
1424 | + $flags |= 4; |
|
1425 | + } |
|
1426 | + if ($opts["use_boundaries"]) { |
|
1427 | + $flags |= 8; |
|
1428 | + } |
|
1429 | + if ($opts["weight_order"]) { |
|
1430 | + $flags |= 16; |
|
1431 | + } |
|
1432 | + if ($opts["query_mode"]) { |
|
1433 | + $flags |= 32; |
|
1434 | + } |
|
1435 | + if ($opts["force_all_words"]) { |
|
1436 | + $flags |= 64; |
|
1437 | + } |
|
1438 | + if ($opts["load_files"]) { |
|
1439 | + $flags |= 128; |
|
1440 | + } |
|
1441 | + if ($opts["allow_empty"]) { |
|
1442 | + $flags |= 256; |
|
1443 | + } |
|
1444 | + if ($opts["emit_zones"]) { |
|
1445 | + $flags |= 512; |
|
1446 | + } |
|
1349 | 1447 | $req = pack("NN", 0, $flags); // mode=0, flags=$flags |
1350 | 1448 | $req .= pack("N", strlen($index)).$index; // req index |
1351 | 1449 | $req .= pack("N", strlen($words)).$words; // req words |
@@ -1512,8 +1610,9 @@ discard block |
||
1512 | 1610 | assert(is_bool($mva)); |
1513 | 1611 | |
1514 | 1612 | assert(is_array($attrs)); |
1515 | - foreach ($attrs as $attr) |
|
1516 | - assert(is_string($attr)); |
|
1613 | + foreach ($attrs as $attr) { |
|
1614 | + assert(is_string($attr)); |
|
1615 | + } |
|
1517 | 1616 | |
1518 | 1617 | assert(is_array($values)); |
1519 | 1618 | foreach ($values as $id=>$entry) |
@@ -1526,10 +1625,12 @@ discard block |
||
1526 | 1625 | if ($mva) |
1527 | 1626 | { |
1528 | 1627 | assert(is_array($v)); |
1529 | - foreach ($v as $vv) |
|
1530 | - assert(is_int($vv)); |
|
1531 | - } else |
|
1532 | - assert(is_int($v)); |
|
1628 | + foreach ($v as $vv) { |
|
1629 | + assert(is_int($vv)); |
|
1630 | + } |
|
1631 | + } else { |
|
1632 | + assert(is_int($v)); |
|
1633 | + } |
|
1533 | 1634 | } |
1534 | 1635 | } |
1535 | 1636 | |
@@ -1551,9 +1652,10 @@ discard block |
||
1551 | 1652 | foreach ($entry as $v) |
1552 | 1653 | { |
1553 | 1654 | $req .= pack("N", $mva ? count($v) : $v); |
1554 | - if ($mva) |
|
1555 | - foreach ($v as $vv) |
|
1655 | + if ($mva) { |
|
1656 | + foreach ($v as $vv) |
|
1556 | 1657 | $req .= pack("N", $vv); |
1658 | + } |
|
1557 | 1659 | } |
1558 | 1660 | } |
1559 | 1661 | |
@@ -1595,13 +1697,15 @@ discard block |
||
1595 | 1697 | $this->_error = 'already connected'; |
1596 | 1698 | return false; |
1597 | 1699 | } |
1598 | - if (!$fp = $this->_Connect()) |
|
1599 | - return false; |
|
1700 | + if (!$fp = $this->_Connect()) { |
|
1701 | + return false; |
|
1702 | + } |
|
1600 | 1703 | |
1601 | 1704 | // command, command version = 0, body length = 4, body = 1 |
1602 | 1705 | $req = pack("nnNN", SEARCHD_COMMAND_PERSIST, 0, 4, 1); |
1603 | - if (!$this->_Send($fp, $req, 12)) |
|
1604 | - return false; |
|
1706 | + if (!$this->_Send($fp, $req, 12)) { |
|
1707 | + return false; |
|
1708 | + } |
|
1605 | 1709 | |
1606 | 1710 | $this->_socket = $fp; |
1607 | 1711 | return true; |
@@ -1647,8 +1751,10 @@ discard block |
||
1647 | 1751 | list ($rows, $cols) = array_values(unpack("N*N*", substr($response, $p, 8))); $p += 8; |
1648 | 1752 | |
1649 | 1753 | $res = array(); |
1650 | - for ($i = 0; $i < $rows; $i++) |
|
1651 | - for ($j = 0; $j < $cols; $j++) |
|
1754 | + for ($i = 0; $i < $rows; $i++) { |
|
1755 | + for ($j = 0; |
|
1756 | + } |
|
1757 | + $j < $cols; $j++) |
|
1652 | 1758 | { |
1653 | 1759 | list(,$len) = unpack("N*", substr($response, $p, 4)); $p += 4; |
1654 | 1760 | $res[$i][] = substr($response, $p, $len); $p += $len; |
@@ -1680,10 +1786,11 @@ discard block |
||
1680 | 1786 | } |
1681 | 1787 | |
1682 | 1788 | $tag = -1; |
1683 | - if (strlen($response) == 4) |
|
1684 | - list(,$tag) = unpack("N*", $response); |
|
1685 | - else |
|
1686 | - $this->_error = "unexpected response length"; |
|
1789 | + if (strlen($response) == 4) { |
|
1790 | + list(,$tag) = unpack("N*", $response); |
|
1791 | + } else { |
|
1792 | + $this->_error = "unexpected response length"; |
|
1793 | + } |
|
1687 | 1794 | |
1688 | 1795 | $this->_MBPop(); |
1689 | 1796 | return $tag; |
@@ -1,223 +1,223 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | class Af_Proxy_Http extends Plugin { |
3 | 3 | |
4 | - /* @var PluginHost $host */ |
|
5 | - private $host; |
|
4 | + /* @var PluginHost $host */ |
|
5 | + private $host; |
|
6 | 6 | |
7 | - /* @var DiskCache $cache */ |
|
8 | - private $cache; |
|
7 | + /* @var DiskCache $cache */ |
|
8 | + private $cache; |
|
9 | 9 | |
10 | - public function about() { |
|
11 | - return array(1.0, |
|
12 | - "Loads media served over plain HTTP via built-in secure proxy", |
|
13 | - "fox"); |
|
14 | - } |
|
10 | + public function about() { |
|
11 | + return array(1.0, |
|
12 | + "Loads media served over plain HTTP via built-in secure proxy", |
|
13 | + "fox"); |
|
14 | + } |
|
15 | 15 | |
16 | - private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com"; |
|
16 | + private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com"; |
|
17 | 17 | |
18 | - public function is_public_method($method) { |
|
19 | - return $method === "imgproxy"; |
|
20 | - } |
|
18 | + public function is_public_method($method) { |
|
19 | + return $method === "imgproxy"; |
|
20 | + } |
|
21 | 21 | |
22 | - public function init($host) { |
|
23 | - $this->host = $host; |
|
24 | - $this->cache = new DiskCache("images"); |
|
22 | + public function init($host) { |
|
23 | + $this->host = $host; |
|
24 | + $this->cache = new DiskCache("images"); |
|
25 | 25 | |
26 | - $host->add_hook($host::HOOK_RENDER_ARTICLE, $this, 150); |
|
27 | - $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this, 150); |
|
28 | - $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this); |
|
26 | + $host->add_hook($host::HOOK_RENDER_ARTICLE, $this, 150); |
|
27 | + $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this, 150); |
|
28 | + $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this); |
|
29 | 29 | |
30 | - $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
31 | - } |
|
30 | + $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
31 | + } |
|
32 | 32 | |
33 | - public function hook_enclosure_entry($enc) { |
|
34 | - if (preg_match("/image/", $enc["content_type"])) { |
|
35 | - $proxy_all = $this->host->get($this, "proxy_all"); |
|
33 | + public function hook_enclosure_entry($enc) { |
|
34 | + if (preg_match("/image/", $enc["content_type"])) { |
|
35 | + $proxy_all = $this->host->get($this, "proxy_all"); |
|
36 | 36 | |
37 | - $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all); |
|
38 | - } |
|
37 | + $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all); |
|
38 | + } |
|
39 | 39 | |
40 | - return $enc; |
|
41 | - } |
|
40 | + return $enc; |
|
41 | + } |
|
42 | 42 | |
43 | - public function hook_render_article($article) { |
|
44 | - return $this->hook_render_article_cdm($article); |
|
45 | - } |
|
43 | + public function hook_render_article($article) { |
|
44 | + return $this->hook_render_article_cdm($article); |
|
45 | + } |
|
46 | 46 | |
47 | - public function imgproxy() { |
|
47 | + public function imgproxy() { |
|
48 | 48 | |
49 | - $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]); |
|
49 | + $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]); |
|
50 | 50 | |
51 | - // called without user context, let's just redirect to original URL |
|
52 | - if (!$_SESSION["uid"]) { |
|
53 | - header("Location: $url"); |
|
54 | - return; |
|
55 | - } |
|
51 | + // called without user context, let's just redirect to original URL |
|
52 | + if (!$_SESSION["uid"]) { |
|
53 | + header("Location: $url"); |
|
54 | + return; |
|
55 | + } |
|
56 | 56 | |
57 | - $local_filename = sha1($url); |
|
57 | + $local_filename = sha1($url); |
|
58 | 58 | |
59 | - if ($this->cache->exists($local_filename)) { |
|
60 | - header("Location: ".$this->cache->getUrl($local_filename)); |
|
61 | - return; |
|
62 | - //$this->cache->send($local_filename); |
|
63 | - } else { |
|
64 | - $data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]); |
|
59 | + if ($this->cache->exists($local_filename)) { |
|
60 | + header("Location: ".$this->cache->getUrl($local_filename)); |
|
61 | + return; |
|
62 | + //$this->cache->send($local_filename); |
|
63 | + } else { |
|
64 | + $data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]); |
|
65 | 65 | |
66 | - if ($data) { |
|
66 | + if ($data) { |
|
67 | 67 | |
68 | - $disable_cache = $this->host->get($this, "disable_cache"); |
|
68 | + $disable_cache = $this->host->get($this, "disable_cache"); |
|
69 | 69 | |
70 | - if (!$disable_cache) { |
|
71 | - if ($this->cache->put($local_filename, $data)) { |
|
72 | - header("Location: ".$this->cache->getUrl($local_filename)); |
|
73 | - return; |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - print $data; |
|
78 | - } else { |
|
79 | - global $fetch_last_error; |
|
80 | - global $fetch_last_error_code; |
|
81 | - global $fetch_last_error_content; |
|
82 | - |
|
83 | - if (function_exists("imagecreate") && !isset($_REQUEST["text"])) { |
|
84 | - $img = imagecreate(450, 75); |
|
85 | - |
|
86 | - /*$bg =*/ imagecolorallocate($img, 255, 255, 255); |
|
87 | - $textcolor = imagecolorallocate($img, 255, 0, 0); |
|
70 | + if (!$disable_cache) { |
|
71 | + if ($this->cache->put($local_filename, $data)) { |
|
72 | + header("Location: ".$this->cache->getUrl($local_filename)); |
|
73 | + return; |
|
74 | + } |
|
75 | + } |
|
88 | 76 | |
89 | - imagerectangle($img, 0, 0, 450 - 1, 75 - 1, $textcolor); |
|
77 | + print $data; |
|
78 | + } else { |
|
79 | + global $fetch_last_error; |
|
80 | + global $fetch_last_error_code; |
|
81 | + global $fetch_last_error_content; |
|
90 | 82 | |
91 | - imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor); |
|
92 | - imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor); |
|
93 | - imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor); |
|
83 | + if (function_exists("imagecreate") && !isset($_REQUEST["text"])) { |
|
84 | + $img = imagecreate(450, 75); |
|
94 | 85 | |
95 | - header("Content-type: image/png"); |
|
96 | - print imagepng($img); |
|
97 | - imagedestroy($img); |
|
86 | + /*$bg =*/ imagecolorallocate($img, 255, 255, 255); |
|
87 | + $textcolor = imagecolorallocate($img, 255, 0, 0); |
|
98 | 88 | |
99 | - } else { |
|
100 | - header("Content-type: text/html"); |
|
101 | - |
|
102 | - http_response_code(400); |
|
103 | - |
|
104 | - print "<h1>Proxy request failed.</h1>"; |
|
105 | - print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>"; |
|
106 | - print "<p>URL: $url</p>"; |
|
107 | - print "<textarea cols='80' rows='25'>".htmlspecialchars($fetch_last_error_content)."</textarea>"; |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - } |
|
89 | + imagerectangle($img, 0, 0, 450 - 1, 75 - 1, $textcolor); |
|
112 | 90 | |
113 | - private function rewrite_url_if_needed($url, $all_remote = false) { |
|
114 | - /* we don't need to handle URLs where local cache already exists, tt-rss rewrites those automatically */ |
|
115 | - if (!$this->cache->exists(sha1($url))) { |
|
91 | + imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor); |
|
92 | + imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor); |
|
93 | + imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor); |
|
116 | 94 | |
117 | - $scheme = parse_url($url, PHP_URL_SCHEME); |
|
95 | + header("Content-type: image/png"); |
|
96 | + print imagepng($img); |
|
97 | + imagedestroy($img); |
|
118 | 98 | |
119 | - if ($all_remote) { |
|
120 | - $host = parse_url($url, PHP_URL_HOST); |
|
121 | - $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST); |
|
99 | + } else { |
|
100 | + header("Content-type: text/html"); |
|
122 | 101 | |
123 | - $is_remote = $host != $self_host; |
|
124 | - } else { |
|
125 | - $is_remote = false; |
|
126 | - } |
|
102 | + http_response_code(400); |
|
127 | 103 | |
128 | - if (($scheme != 'https' && $scheme != "") || $is_remote) { |
|
129 | - if (strpos($url, "data:") !== 0) { |
|
130 | - $parts = parse_url($url); |
|
131 | - |
|
132 | - foreach (explode(" ", $this->ssl_known_whitelist) as $host) { |
|
133 | - if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) { |
|
134 | - $parts['scheme'] = 'https'; |
|
135 | - $url = build_url($parts); |
|
136 | - if ($all_remote && $is_remote) { |
|
137 | - break; |
|
138 | - } else { |
|
139 | - return $url; |
|
140 | - } |
|
141 | - } |
|
142 | - } |
|
104 | + print "<h1>Proxy request failed.</h1>"; |
|
105 | + print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>"; |
|
106 | + print "<p>URL: $url</p>"; |
|
107 | + print "<textarea cols='80' rows='25'>".htmlspecialchars($fetch_last_error_content)."</textarea>"; |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + } |
|
143 | 112 | |
144 | - return $this->host->get_public_method_url($this, "imgproxy", ["url" => $url]); |
|
145 | - } |
|
146 | - } |
|
147 | - } |
|
113 | + private function rewrite_url_if_needed($url, $all_remote = false) { |
|
114 | + /* we don't need to handle URLs where local cache already exists, tt-rss rewrites those automatically */ |
|
115 | + if (!$this->cache->exists(sha1($url))) { |
|
148 | 116 | |
149 | - return $url; |
|
150 | - } |
|
117 | + $scheme = parse_url($url, PHP_URL_SCHEME); |
|
151 | 118 | |
152 | - /** |
|
153 | - * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
154 | - */ |
|
155 | - public function hook_render_article_cdm($article, $api_mode = false) { |
|
119 | + if ($all_remote) { |
|
120 | + $host = parse_url($url, PHP_URL_HOST); |
|
121 | + $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST); |
|
156 | 122 | |
157 | - $need_saving = false; |
|
158 | - $proxy_all = $this->host->get($this, "proxy_all"); |
|
123 | + $is_remote = $host != $self_host; |
|
124 | + } else { |
|
125 | + $is_remote = false; |
|
126 | + } |
|
159 | 127 | |
160 | - $doc = new DOMDocument(); |
|
161 | - if (@$doc->loadHTML('<?xml encoding="UTF-8">'.$article["content"])) { |
|
162 | - $xpath = new DOMXPath($doc); |
|
163 | - $imgs = $xpath->query("//img[@src]"); |
|
128 | + if (($scheme != 'https' && $scheme != "") || $is_remote) { |
|
129 | + if (strpos($url, "data:") !== 0) { |
|
130 | + $parts = parse_url($url); |
|
164 | 131 | |
165 | - foreach ($imgs as $img) { |
|
166 | - $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all); |
|
132 | + foreach (explode(" ", $this->ssl_known_whitelist) as $host) { |
|
133 | + if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) { |
|
134 | + $parts['scheme'] = 'https'; |
|
135 | + $url = build_url($parts); |
|
136 | + if ($all_remote && $is_remote) { |
|
137 | + break; |
|
138 | + } else { |
|
139 | + return $url; |
|
140 | + } |
|
141 | + } |
|
142 | + } |
|
167 | 143 | |
168 | - if ($new_src != $img->getAttribute("src")) { |
|
169 | - $img->setAttribute("src", $new_src); |
|
170 | - $img->removeAttribute("srcset"); |
|
144 | + return $this->host->get_public_method_url($this, "imgproxy", ["url" => $url]); |
|
145 | + } |
|
146 | + } |
|
147 | + } |
|
171 | 148 | |
172 | - $need_saving = true; |
|
173 | - } |
|
174 | - } |
|
149 | + return $url; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
154 | + */ |
|
155 | + public function hook_render_article_cdm($article, $api_mode = false) { |
|
175 | 156 | |
176 | - $vids = $xpath->query("(//video|//picture)"); |
|
157 | + $need_saving = false; |
|
158 | + $proxy_all = $this->host->get($this, "proxy_all"); |
|
159 | + |
|
160 | + $doc = new DOMDocument(); |
|
161 | + if (@$doc->loadHTML('<?xml encoding="UTF-8">'.$article["content"])) { |
|
162 | + $xpath = new DOMXPath($doc); |
|
163 | + $imgs = $xpath->query("//img[@src]"); |
|
177 | 164 | |
178 | - foreach ($vids as $vid) { |
|
179 | - if ($vid->hasAttribute("poster")) { |
|
180 | - $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all); |
|
165 | + foreach ($imgs as $img) { |
|
166 | + $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all); |
|
181 | 167 | |
182 | - if ($new_src != $vid->getAttribute("poster")) { |
|
183 | - $vid->setAttribute("poster", $new_src); |
|
168 | + if ($new_src != $img->getAttribute("src")) { |
|
169 | + $img->setAttribute("src", $new_src); |
|
170 | + $img->removeAttribute("srcset"); |
|
171 | + |
|
172 | + $need_saving = true; |
|
173 | + } |
|
174 | + } |
|
184 | 175 | |
185 | - $need_saving = true; |
|
186 | - } |
|
187 | - } |
|
176 | + $vids = $xpath->query("(//video|//picture)"); |
|
188 | 177 | |
189 | - $vsrcs = $xpath->query("source", $vid); |
|
178 | + foreach ($vids as $vid) { |
|
179 | + if ($vid->hasAttribute("poster")) { |
|
180 | + $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all); |
|
190 | 181 | |
191 | - foreach ($vsrcs as $vsrc) { |
|
192 | - $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all); |
|
182 | + if ($new_src != $vid->getAttribute("poster")) { |
|
183 | + $vid->setAttribute("poster", $new_src); |
|
193 | 184 | |
194 | - if ($new_src != $vsrc->getAttribute("src")) { |
|
195 | - $vid->setAttribute("src", $new_src); |
|
185 | + $need_saving = true; |
|
186 | + } |
|
187 | + } |
|
196 | 188 | |
197 | - $need_saving = true; |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
189 | + $vsrcs = $xpath->query("source", $vid); |
|
202 | 190 | |
203 | - if ($need_saving) { |
|
204 | - $article["content"] = $doc->saveHTML(); |
|
205 | - } |
|
191 | + foreach ($vsrcs as $vsrc) { |
|
192 | + $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all); |
|
206 | 193 | |
207 | - return $article; |
|
208 | - } |
|
194 | + if ($new_src != $vsrc->getAttribute("src")) { |
|
195 | + $vid->setAttribute("src", $new_src); |
|
196 | + |
|
197 | + $need_saving = true; |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + if ($need_saving) { |
|
204 | + $article["content"] = $doc->saveHTML(); |
|
205 | + } |
|
206 | + |
|
207 | + return $article; |
|
208 | + } |
|
209 | 209 | |
210 | - public function hook_prefs_tab($args) { |
|
211 | - if ($args != "prefFeeds") { |
|
212 | - return; |
|
213 | - } |
|
210 | + public function hook_prefs_tab($args) { |
|
211 | + if ($args != "prefFeeds") { |
|
212 | + return; |
|
213 | + } |
|
214 | 214 | |
215 | - print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
215 | + print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
216 | 216 | title=\"<i class='material-icons'>extension</i> ".__('Image proxy settings (af_proxy_http)')."\">"; |
217 | 217 | |
218 | - print "<form dojoType=\"dijit.form.Form\">"; |
|
218 | + print "<form dojoType=\"dijit.form.Form\">"; |
|
219 | 219 | |
220 | - print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> |
|
220 | + print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> |
|
221 | 221 | evt.preventDefault(); |
222 | 222 | if (this.validate()) { |
223 | 223 | console.log(dojo.objectToQuery(this.getValues())); |
@@ -231,36 +231,36 @@ discard block |
||
231 | 231 | } |
232 | 232 | </script>"; |
233 | 233 | |
234 | - print_hidden("op", "pluginhandler"); |
|
235 | - print_hidden("method", "save"); |
|
236 | - print_hidden("plugin", "af_proxy_http"); |
|
234 | + print_hidden("op", "pluginhandler"); |
|
235 | + print_hidden("method", "save"); |
|
236 | + print_hidden("plugin", "af_proxy_http"); |
|
237 | 237 | |
238 | - $proxy_all = $this->host->get($this, "proxy_all"); |
|
239 | - print_checkbox("proxy_all", $proxy_all); |
|
240 | - print " <label for=\"proxy_all\">".__("Enable proxy for all remote images.")."</label><br/>"; |
|
238 | + $proxy_all = $this->host->get($this, "proxy_all"); |
|
239 | + print_checkbox("proxy_all", $proxy_all); |
|
240 | + print " <label for=\"proxy_all\">".__("Enable proxy for all remote images.")."</label><br/>"; |
|
241 | 241 | |
242 | - $disable_cache = $this->host->get($this, "disable_cache"); |
|
243 | - print_checkbox("disable_cache", $disable_cache); |
|
244 | - print " <label for=\"disable_cache\">".__("Don't cache files locally.")."</label>"; |
|
242 | + $disable_cache = $this->host->get($this, "disable_cache"); |
|
243 | + print_checkbox("disable_cache", $disable_cache); |
|
244 | + print " <label for=\"disable_cache\">".__("Don't cache files locally.")."</label>"; |
|
245 | 245 | |
246 | - print "<p>"; print_button("submit", __("Save")); |
|
246 | + print "<p>"; print_button("submit", __("Save")); |
|
247 | 247 | |
248 | - print "</form>"; |
|
248 | + print "</form>"; |
|
249 | 249 | |
250 | - print "</div>"; |
|
251 | - } |
|
250 | + print "</div>"; |
|
251 | + } |
|
252 | 252 | |
253 | - public function save() { |
|
254 | - $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]); |
|
255 | - $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]); |
|
253 | + public function save() { |
|
254 | + $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]); |
|
255 | + $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]); |
|
256 | 256 | |
257 | - $this->host->set($this, "proxy_all", $proxy_all, false); |
|
258 | - $this->host->set($this, "disable_cache", $disable_cache); |
|
257 | + $this->host->set($this, "proxy_all", $proxy_all, false); |
|
258 | + $this->host->set($this, "disable_cache", $disable_cache); |
|
259 | 259 | |
260 | - echo __("Configuration saved"); |
|
261 | - } |
|
260 | + echo __("Configuration saved"); |
|
261 | + } |
|
262 | 262 | |
263 | - public function api_version() { |
|
264 | - return 2; |
|
265 | - } |
|
263 | + public function api_version() { |
|
264 | + return 2; |
|
265 | + } |
|
266 | 266 | } |
@@ -1,87 +1,87 @@ |
||
1 | 1 | <?php |
2 | 2 | class Af_Unburn extends Plugin { |
3 | - private $host; |
|
3 | + private $host; |
|
4 | 4 | |
5 | - public function about() { |
|
6 | - return array(1.0, |
|
7 | - "Resolves feedburner and similar feed redirector URLs (requires CURL)", |
|
8 | - "fox"); |
|
9 | - } |
|
5 | + public function about() { |
|
6 | + return array(1.0, |
|
7 | + "Resolves feedburner and similar feed redirector URLs (requires CURL)", |
|
8 | + "fox"); |
|
9 | + } |
|
10 | 10 | |
11 | - public function flags() { |
|
12 | - return array("needs_curl" => true); |
|
13 | - } |
|
11 | + public function flags() { |
|
12 | + return array("needs_curl" => true); |
|
13 | + } |
|
14 | 14 | |
15 | - public function init($host) { |
|
16 | - $this->host = $host; |
|
15 | + public function init($host) { |
|
16 | + $this->host = $host; |
|
17 | 17 | |
18 | - $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
19 | - } |
|
18 | + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
19 | + } |
|
20 | 20 | |
21 | - public function hook_article_filter($article) { |
|
22 | - $owner_uid = $article["owner_uid"]; |
|
21 | + public function hook_article_filter($article) { |
|
22 | + $owner_uid = $article["owner_uid"]; |
|
23 | 23 | |
24 | - if (defined('NO_CURL') || !function_exists("curl_init") || ini_get("open_basedir")) { |
|
25 | - return $article; |
|
26 | - } |
|
24 | + if (defined('NO_CURL') || !function_exists("curl_init") || ini_get("open_basedir")) { |
|
25 | + return $article; |
|
26 | + } |
|
27 | 27 | |
28 | - if ((strpos($article["link"], "feedproxy.google.com") !== false || |
|
29 | - strpos($article["link"], "/~r/") !== false || |
|
30 | - strpos($article["link"], "feedsportal.com") !== false)) { |
|
28 | + if ((strpos($article["link"], "feedproxy.google.com") !== false || |
|
29 | + strpos($article["link"], "/~r/") !== false || |
|
30 | + strpos($article["link"], "feedsportal.com") !== false)) { |
|
31 | 31 | |
32 | - $ch = curl_init($article["link"]); |
|
32 | + $ch = curl_init($article["link"]); |
|
33 | 33 | |
34 | - curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
35 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
36 | - curl_setopt($ch, CURLOPT_HEADER, true); |
|
37 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
38 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
39 | - curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); |
|
34 | + curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
35 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
36 | + curl_setopt($ch, CURLOPT_HEADER, true); |
|
37 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
38 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
39 | + curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); |
|
40 | 40 | |
41 | - if (defined('_CURL_HTTP_PROXY')) { |
|
42 | - curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY); |
|
43 | - } |
|
41 | + if (defined('_CURL_HTTP_PROXY')) { |
|
42 | + curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY); |
|
43 | + } |
|
44 | 44 | |
45 | - @curl_exec($ch); |
|
45 | + @curl_exec($ch); |
|
46 | 46 | |
47 | - $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); |
|
47 | + $real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); |
|
48 | 48 | |
49 | - curl_close($ch); |
|
49 | + curl_close($ch); |
|
50 | 50 | |
51 | - if ($real_url) { |
|
52 | - /* remove the rest of it */ |
|
51 | + if ($real_url) { |
|
52 | + /* remove the rest of it */ |
|
53 | 53 | |
54 | - $query = parse_url($real_url, PHP_URL_QUERY); |
|
54 | + $query = parse_url($real_url, PHP_URL_QUERY); |
|
55 | 55 | |
56 | - if ($query && strpos($query, "utm_source") !== false) { |
|
57 | - $args = array(); |
|
58 | - parse_str($query, $args); |
|
56 | + if ($query && strpos($query, "utm_source") !== false) { |
|
57 | + $args = array(); |
|
58 | + parse_str($query, $args); |
|
59 | 59 | |
60 | - foreach (array("utm_source", "utm_medium", "utm_campaign") as $param) { |
|
61 | - if (isset($args[$param])) { |
|
62 | - unset($args[$param]); |
|
63 | - } |
|
64 | - } |
|
60 | + foreach (array("utm_source", "utm_medium", "utm_campaign") as $param) { |
|
61 | + if (isset($args[$param])) { |
|
62 | + unset($args[$param]); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - $new_query = http_build_query($args); |
|
66 | + $new_query = http_build_query($args); |
|
67 | 67 | |
68 | - if ($new_query != $query) { |
|
69 | - $real_url = str_replace("?$query", "?$new_query", $real_url); |
|
70 | - } |
|
71 | - } |
|
68 | + if ($new_query != $query) { |
|
69 | + $real_url = str_replace("?$query", "?$new_query", $real_url); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - $real_url = preg_replace("/\?$/", "", $real_url); |
|
73 | + $real_url = preg_replace("/\?$/", "", $real_url); |
|
74 | 74 | |
75 | - $article["plugin_data"] = "unburn,$owner_uid:".$article["plugin_data"]; |
|
76 | - $article["link"] = $real_url; |
|
77 | - } |
|
78 | - } |
|
75 | + $article["plugin_data"] = "unburn,$owner_uid:".$article["plugin_data"]; |
|
76 | + $article["link"] = $real_url; |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - return $article; |
|
81 | - } |
|
80 | + return $article; |
|
81 | + } |
|
82 | 82 | |
83 | - public function api_version() { |
|
84 | - return 2; |
|
85 | - } |
|
83 | + public function api_version() { |
|
84 | + return 2; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | } |
@@ -1,78 +1,78 @@ |
||
1 | 1 | <?php |
2 | 2 | class Note extends Plugin { |
3 | 3 | |
4 | - /* @var PluginHost $host */ |
|
5 | - private $host; |
|
4 | + /* @var PluginHost $host */ |
|
5 | + private $host; |
|
6 | 6 | |
7 | - public function about() { |
|
8 | - return array(1.0, |
|
9 | - "Adds support for setting article notes", |
|
10 | - "fox"); |
|
11 | - } |
|
7 | + public function about() { |
|
8 | + return array(1.0, |
|
9 | + "Adds support for setting article notes", |
|
10 | + "fox"); |
|
11 | + } |
|
12 | 12 | |
13 | - public function init($host) { |
|
14 | - $this->host = $host; |
|
13 | + public function init($host) { |
|
14 | + $this->host = $host; |
|
15 | 15 | |
16 | - $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); |
|
17 | - } |
|
16 | + $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); |
|
17 | + } |
|
18 | 18 | |
19 | - public function get_js() { |
|
20 | - return file_get_contents(dirname(__FILE__)."/note.js"); |
|
21 | - } |
|
19 | + public function get_js() { |
|
20 | + return file_get_contents(dirname(__FILE__)."/note.js"); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | |
24 | - public function hook_article_button($line) { |
|
25 | - return "<i class='material-icons' onclick=\"Plugins.Note.edit(".$line["id"].")\" |
|
24 | + public function hook_article_button($line) { |
|
25 | + return "<i class='material-icons' onclick=\"Plugins.Note.edit(".$line["id"].")\" |
|
26 | 26 | style='cursor : pointer' title='".__('Edit article note')."'>note</i>"; |
27 | - } |
|
27 | + } |
|
28 | 28 | |
29 | - public function edit() { |
|
30 | - $param = $_REQUEST['param']; |
|
29 | + public function edit() { |
|
30 | + $param = $_REQUEST['param']; |
|
31 | 31 | |
32 | - $sth = $this->pdo->prepare("SELECT note FROM ttrss_user_entries WHERE |
|
32 | + $sth = $this->pdo->prepare("SELECT note FROM ttrss_user_entries WHERE |
|
33 | 33 | ref_id = ? AND owner_uid = ?"); |
34 | - $sth->execute([$param, $_SESSION['uid']]); |
|
34 | + $sth->execute([$param, $_SESSION['uid']]); |
|
35 | 35 | |
36 | - if ($row = $sth->fetch()) { |
|
36 | + if ($row = $sth->fetch()) { |
|
37 | 37 | |
38 | - $note = $row['note']; |
|
38 | + $note = $row['note']; |
|
39 | 39 | |
40 | - print_hidden("id", "$param"); |
|
41 | - print_hidden("op", "pluginhandler"); |
|
42 | - print_hidden("method", "setNote"); |
|
43 | - print_hidden("plugin", "note"); |
|
40 | + print_hidden("id", "$param"); |
|
41 | + print_hidden("op", "pluginhandler"); |
|
42 | + print_hidden("method", "setNote"); |
|
43 | + print_hidden("plugin", "note"); |
|
44 | 44 | |
45 | - print "<textarea dojoType='dijit.form.SimpleTextarea' |
|
45 | + print "<textarea dojoType='dijit.form.SimpleTextarea' |
|
46 | 46 | style='font-size : 12px; width : 98%; height: 100px;' |
47 | 47 | name='note'>$note</textarea>"; |
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - print "<footer class='text-center'>"; |
|
52 | - print "<button dojoType=\"dijit.form.Button\" |
|
51 | + print "<footer class='text-center'>"; |
|
52 | + print "<button dojoType=\"dijit.form.Button\" |
|
53 | 53 | onclick=\"dijit.byId('editNoteDlg').execute()\">".__('Save')."</button> "; |
54 | - print "<button dojoType=\"dijit.form.Button\" |
|
54 | + print "<button dojoType=\"dijit.form.Button\" |
|
55 | 55 | onclick=\"dijit.byId('editNoteDlg').hide()\">".__('Cancel')."</button>"; |
56 | - print "</footer>"; |
|
56 | + print "</footer>"; |
|
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - public function setNote() { |
|
61 | - $id = $_REQUEST["id"]; |
|
62 | - $note = trim(strip_tags($_REQUEST["note"])); |
|
60 | + public function setNote() { |
|
61 | + $id = $_REQUEST["id"]; |
|
62 | + $note = trim(strip_tags($_REQUEST["note"])); |
|
63 | 63 | |
64 | - $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET note = ? |
|
64 | + $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET note = ? |
|
65 | 65 | WHERE ref_id = ? AND owner_uid = ?"); |
66 | - $sth->execute([$note, $id, $_SESSION['uid']]); |
|
66 | + $sth->execute([$note, $id, $_SESSION['uid']]); |
|
67 | 67 | |
68 | - $formatted_note = Article::format_article_note($id, $note); |
|
68 | + $formatted_note = Article::format_article_note($id, $note); |
|
69 | 69 | |
70 | - print json_encode(array("note" => $formatted_note, |
|
71 | - "raw_length" => mb_strlen($note))); |
|
72 | - } |
|
70 | + print json_encode(array("note" => $formatted_note, |
|
71 | + "raw_length" => mb_strlen($note))); |
|
72 | + } |
|
73 | 73 | |
74 | - public function api_version() { |
|
75 | - return 2; |
|
76 | - } |
|
74 | + public function api_version() { |
|
75 | + return 2; |
|
76 | + } |
|
77 | 77 | |
78 | 78 | } |
@@ -1,40 +1,40 @@ |
||
1 | 1 | <?php |
2 | 2 | class GoogleReaderKeys extends Plugin { |
3 | - private $host; |
|
4 | - |
|
5 | - public function about() { |
|
6 | - return array(1.0, |
|
7 | - "Keyboard hotkeys emulate Google Reader", |
|
8 | - "markwaters"); |
|
9 | - } |
|
10 | - |
|
11 | - public function init($host) { |
|
12 | - $this->host = $host; |
|
13 | - |
|
14 | - $host->add_hook($host::HOOK_HOTKEY_MAP, $this); |
|
15 | - } |
|
16 | - |
|
17 | - public function hook_hotkey_map($hotkeys) { |
|
18 | - |
|
19 | - $hotkeys["j"] = "next_article_noscroll"; |
|
20 | - $hotkeys["k"] = "prev_article_noscroll"; |
|
21 | - $hotkeys["N"] = "next_feed"; |
|
22 | - $hotkeys["P"] = "prev_feed"; |
|
23 | - $hotkeys["v"] = "open_in_new_window"; |
|
24 | - $hotkeys["r"] = "feed_refresh"; |
|
25 | - $hotkeys["m"] = "toggle_unread"; |
|
26 | - $hotkeys["o"] = "toggle_expand"; |
|
27 | - $hotkeys["\r|Enter"] = "toggle_expand"; |
|
28 | - $hotkeys["?"] = "help_dialog"; |
|
29 | - $hotkeys[" |Space"] = "next_article"; |
|
30 | - $hotkeys["(38)|Up"] = "article_scroll_up"; |
|
31 | - $hotkeys["(40)|Down"] = "article_scroll_down"; |
|
32 | - |
|
33 | - return $hotkeys; |
|
34 | - } |
|
35 | - |
|
36 | - public function api_version() { |
|
37 | - return 2; |
|
38 | - } |
|
3 | + private $host; |
|
4 | + |
|
5 | + public function about() { |
|
6 | + return array(1.0, |
|
7 | + "Keyboard hotkeys emulate Google Reader", |
|
8 | + "markwaters"); |
|
9 | + } |
|
10 | + |
|
11 | + public function init($host) { |
|
12 | + $this->host = $host; |
|
13 | + |
|
14 | + $host->add_hook($host::HOOK_HOTKEY_MAP, $this); |
|
15 | + } |
|
16 | + |
|
17 | + public function hook_hotkey_map($hotkeys) { |
|
18 | + |
|
19 | + $hotkeys["j"] = "next_article_noscroll"; |
|
20 | + $hotkeys["k"] = "prev_article_noscroll"; |
|
21 | + $hotkeys["N"] = "next_feed"; |
|
22 | + $hotkeys["P"] = "prev_feed"; |
|
23 | + $hotkeys["v"] = "open_in_new_window"; |
|
24 | + $hotkeys["r"] = "feed_refresh"; |
|
25 | + $hotkeys["m"] = "toggle_unread"; |
|
26 | + $hotkeys["o"] = "toggle_expand"; |
|
27 | + $hotkeys["\r|Enter"] = "toggle_expand"; |
|
28 | + $hotkeys["?"] = "help_dialog"; |
|
29 | + $hotkeys[" |Space"] = "next_article"; |
|
30 | + $hotkeys["(38)|Up"] = "article_scroll_up"; |
|
31 | + $hotkeys["(40)|Down"] = "article_scroll_down"; |
|
32 | + |
|
33 | + return $hotkeys; |
|
34 | + } |
|
35 | + |
|
36 | + public function api_version() { |
|
37 | + return 2; |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
@@ -1,70 +1,70 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | class Af_Psql_Trgm extends Plugin { |
3 | 3 | |
4 | - /* @var PluginHost $host */ |
|
5 | - private $host; |
|
4 | + /* @var PluginHost $host */ |
|
5 | + private $host; |
|
6 | 6 | |
7 | - public function about() { |
|
8 | - return array(1.0, |
|
9 | - "Marks similar articles as read (requires pg_trgm)", |
|
10 | - "fox"); |
|
11 | - } |
|
7 | + public function about() { |
|
8 | + return array(1.0, |
|
9 | + "Marks similar articles as read (requires pg_trgm)", |
|
10 | + "fox"); |
|
11 | + } |
|
12 | 12 | |
13 | - public function save() { |
|
14 | - $similarity = (float) $_POST["similarity"]; |
|
15 | - $min_title_length = (int) $_POST["min_title_length"]; |
|
16 | - $enable_globally = checkbox_to_sql_bool($_POST["enable_globally"]); |
|
13 | + public function save() { |
|
14 | + $similarity = (float) $_POST["similarity"]; |
|
15 | + $min_title_length = (int) $_POST["min_title_length"]; |
|
16 | + $enable_globally = checkbox_to_sql_bool($_POST["enable_globally"]); |
|
17 | 17 | |
18 | - if ($similarity < 0) { |
|
19 | - $similarity = 0; |
|
20 | - } |
|
21 | - if ($similarity > 1) { |
|
22 | - $similarity = 1; |
|
23 | - } |
|
18 | + if ($similarity < 0) { |
|
19 | + $similarity = 0; |
|
20 | + } |
|
21 | + if ($similarity > 1) { |
|
22 | + $similarity = 1; |
|
23 | + } |
|
24 | 24 | |
25 | - if ($min_title_length < 0) { |
|
26 | - $min_title_length = 0; |
|
27 | - } |
|
25 | + if ($min_title_length < 0) { |
|
26 | + $min_title_length = 0; |
|
27 | + } |
|
28 | 28 | |
29 | - $similarity = sprintf("%.2f", $similarity); |
|
29 | + $similarity = sprintf("%.2f", $similarity); |
|
30 | 30 | |
31 | - $this->host->set($this, "similarity", $similarity); |
|
32 | - $this->host->set($this, "min_title_length", $min_title_length); |
|
33 | - $this->host->set($this, "enable_globally", $enable_globally); |
|
31 | + $this->host->set($this, "similarity", $similarity); |
|
32 | + $this->host->set($this, "min_title_length", $min_title_length); |
|
33 | + $this->host->set($this, "enable_globally", $enable_globally); |
|
34 | 34 | |
35 | - echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally); |
|
36 | - } |
|
35 | + echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally); |
|
36 | + } |
|
37 | 37 | |
38 | - public function init($host) { |
|
39 | - $this->host = $host; |
|
38 | + public function init($host) { |
|
39 | + $this->host = $host; |
|
40 | 40 | |
41 | - $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
42 | - $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
43 | - $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); |
|
44 | - $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); |
|
45 | - $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); |
|
41 | + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
42 | + $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
43 | + $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); |
|
44 | + $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); |
|
45 | + $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); |
|
46 | 46 | |
47 | - } |
|
47 | + } |
|
48 | 48 | |
49 | - public function get_js() { |
|
50 | - return file_get_contents(__DIR__."/init.js"); |
|
51 | - } |
|
49 | + public function get_js() { |
|
50 | + return file_get_contents(__DIR__."/init.js"); |
|
51 | + } |
|
52 | 52 | |
53 | - public function showrelated() { |
|
54 | - $id = (int) $_REQUEST['param']; |
|
55 | - $owner_uid = $_SESSION["uid"]; |
|
53 | + public function showrelated() { |
|
54 | + $id = (int) $_REQUEST['param']; |
|
55 | + $owner_uid = $_SESSION["uid"]; |
|
56 | 56 | |
57 | - $sth = $this->pdo->prepare("SELECT title FROM ttrss_entries, ttrss_user_entries |
|
57 | + $sth = $this->pdo->prepare("SELECT title FROM ttrss_entries, ttrss_user_entries |
|
58 | 58 | WHERE ref_id = id AND id = ? AND owner_uid = ?"); |
59 | - $sth->execute([$id, $owner_uid]); |
|
59 | + $sth->execute([$id, $owner_uid]); |
|
60 | 60 | |
61 | - if ($row = $sth->fetch()) { |
|
61 | + if ($row = $sth->fetch()) { |
|
62 | 62 | |
63 | - $title = $row['title']; |
|
63 | + $title = $row['title']; |
|
64 | 64 | |
65 | - print "<p>$title</p>"; |
|
65 | + print "<p>$title</p>"; |
|
66 | 66 | |
67 | - $sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id, |
|
67 | + $sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id, |
|
68 | 68 | feed_id, |
69 | 69 | ttrss_entries.title AS title, |
70 | 70 | updated, link, |
@@ -81,83 +81,83 @@ discard block |
||
81 | 81 | sm DESC, date_entered DESC |
82 | 82 | LIMIT 10"); |
83 | 83 | |
84 | - $sth->execute([$title, $owner_uid, $id]); |
|
84 | + $sth->execute([$title, $owner_uid, $id]); |
|
85 | 85 | |
86 | - print "<ul class='panel panel-scrollable'>"; |
|
86 | + print "<ul class='panel panel-scrollable'>"; |
|
87 | 87 | |
88 | - while ($line = $sth->fetch()) { |
|
89 | - print "<li style='display : flex'>"; |
|
90 | - print "<i class='material-icons'>bookmark_outline</i>"; |
|
88 | + while ($line = $sth->fetch()) { |
|
89 | + print "<li style='display : flex'>"; |
|
90 | + print "<i class='material-icons'>bookmark_outline</i>"; |
|
91 | 91 | |
92 | - $sm = sprintf("%.2f", $line['sm']); |
|
93 | - $article_link = htmlspecialchars($line["link"]); |
|
92 | + $sm = sprintf("%.2f", $line['sm']); |
|
93 | + $article_link = htmlspecialchars($line["link"]); |
|
94 | 94 | |
95 | - print "<div style='flex-grow : 2'>"; |
|
95 | + print "<div style='flex-grow : 2'>"; |
|
96 | 96 | |
97 | - print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">". |
|
98 | - $line["title"]."</a>"; |
|
97 | + print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">". |
|
98 | + $line["title"]."</a>"; |
|
99 | 99 | |
100 | - print " (<a href=\"#\" onclick=\"Feeds.open({feed:".$line["feed_id"]."})\">". |
|
101 | - htmlspecialchars($line["feed_title"])."</a>)"; |
|
100 | + print " (<a href=\"#\" onclick=\"Feeds.open({feed:".$line["feed_id"]."})\">". |
|
101 | + htmlspecialchars($line["feed_title"])."</a>)"; |
|
102 | 102 | |
103 | - print " — $sm"; |
|
103 | + print " — $sm"; |
|
104 | 104 | |
105 | - print "</div>"; |
|
105 | + print "</div>"; |
|
106 | 106 | |
107 | - print "<div style='text-align : right' class='text-muted'>".smart_date_time(strtotime($line["updated"]))."</div>"; |
|
107 | + print "<div style='text-align : right' class='text-muted'>".smart_date_time(strtotime($line["updated"]))."</div>"; |
|
108 | 108 | |
109 | - print "</li>"; |
|
110 | - } |
|
109 | + print "</li>"; |
|
110 | + } |
|
111 | 111 | |
112 | - print "</ul>"; |
|
112 | + print "</ul>"; |
|
113 | 113 | |
114 | - } |
|
114 | + } |
|
115 | 115 | |
116 | - print "<footer class='text-center'>"; |
|
117 | - print "<button dojoType='dijit.form.Button' onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>"; |
|
118 | - print "</footer>"; |
|
116 | + print "<footer class='text-center'>"; |
|
117 | + print "<button dojoType='dijit.form.Button' onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>"; |
|
118 | + print "</footer>"; |
|
119 | 119 | |
120 | 120 | |
121 | - } |
|
121 | + } |
|
122 | 122 | |
123 | - public function hook_article_button($line) { |
|
124 | - return "<i style=\"cursor : pointer\" class='material-icons' |
|
123 | + public function hook_article_button($line) { |
|
124 | + return "<i style=\"cursor : pointer\" class='material-icons' |
|
125 | 125 | onclick=\"Plugins.Psql_Trgm.showRelated(".$line["id"].")\" |
126 | 126 | title='".__('Show related articles')."'>bookmark_outline</i>"; |
127 | - } |
|
127 | + } |
|
128 | 128 | |
129 | - public function hook_prefs_tab($args) { |
|
130 | - if ($args != "prefFeeds") { |
|
131 | - return; |
|
132 | - } |
|
129 | + public function hook_prefs_tab($args) { |
|
130 | + if ($args != "prefFeeds") { |
|
131 | + return; |
|
132 | + } |
|
133 | 133 | |
134 | - print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
134 | + print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
135 | 135 | title=\"<i class='material-icons'>extension</i> ".__('Mark similar articles as read')."\">"; |
136 | 136 | |
137 | - if (DB_TYPE != "pgsql") { |
|
138 | - print_error("Database type not supported."); |
|
139 | - } else { |
|
137 | + if (DB_TYPE != "pgsql") { |
|
138 | + print_error("Database type not supported."); |
|
139 | + } else { |
|
140 | 140 | |
141 | - $res = $this->pdo->query("select 'similarity'::regproc"); |
|
141 | + $res = $this->pdo->query("select 'similarity'::regproc"); |
|
142 | 142 | |
143 | - if (!$res->fetch()) { |
|
144 | - print_error("pg_trgm extension not found."); |
|
145 | - } |
|
143 | + if (!$res->fetch()) { |
|
144 | + print_error("pg_trgm extension not found."); |
|
145 | + } |
|
146 | 146 | |
147 | - $similarity = $this->host->get($this, "similarity"); |
|
148 | - $min_title_length = $this->host->get($this, "min_title_length"); |
|
149 | - $enable_globally = $this->host->get($this, "enable_globally"); |
|
147 | + $similarity = $this->host->get($this, "similarity"); |
|
148 | + $min_title_length = $this->host->get($this, "min_title_length"); |
|
149 | + $enable_globally = $this->host->get($this, "enable_globally"); |
|
150 | 150 | |
151 | - if (!$similarity) { |
|
152 | - $similarity = '0.75'; |
|
153 | - } |
|
154 | - if (!$min_title_length) { |
|
155 | - $min_title_length = '32'; |
|
156 | - } |
|
151 | + if (!$similarity) { |
|
152 | + $similarity = '0.75'; |
|
153 | + } |
|
154 | + if (!$min_title_length) { |
|
155 | + $min_title_length = '32'; |
|
156 | + } |
|
157 | 157 | |
158 | - print "<form dojoType=\"dijit.form.Form\">"; |
|
158 | + print "<form dojoType=\"dijit.form.Form\">"; |
|
159 | 159 | |
160 | - print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> |
|
160 | + print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> |
|
161 | 161 | evt.preventDefault(); |
162 | 162 | if (this.validate()) { |
163 | 163 | console.log(dojo.objectToQuery(this.getValues())); |
@@ -171,206 +171,206 @@ discard block |
||
171 | 171 | } |
172 | 172 | </script>"; |
173 | 173 | |
174 | - print_hidden("op", "pluginhandler"); |
|
175 | - print_hidden("method", "save"); |
|
176 | - print_hidden("plugin", "af_psql_trgm"); |
|
174 | + print_hidden("op", "pluginhandler"); |
|
175 | + print_hidden("method", "save"); |
|
176 | + print_hidden("plugin", "af_psql_trgm"); |
|
177 | 177 | |
178 | - print "<h2>".__("Global settings")."</h2>"; |
|
178 | + print "<h2>".__("Global settings")."</h2>"; |
|
179 | 179 | |
180 | - print_notice("Enable for specific feeds in the feed editor."); |
|
180 | + print_notice("Enable for specific feeds in the feed editor."); |
|
181 | 181 | |
182 | - print "<fieldset>"; |
|
182 | + print "<fieldset>"; |
|
183 | 183 | |
184 | - print "<label>".__("Minimum similarity:")."</label> "; |
|
185 | - print "<input dojoType=\"dijit.form.NumberSpinner\" |
|
184 | + print "<label>".__("Minimum similarity:")."</label> "; |
|
185 | + print "<input dojoType=\"dijit.form.NumberSpinner\" |
|
186 | 186 | placeholder=\"0.75\" id='psql_trgm_similarity' |
187 | 187 | required=\"1\" name=\"similarity\" value=\"$similarity\">"; |
188 | 188 | |
189 | - print "<div dojoType='dijit.Tooltip' connectId='psql_trgm_similarity' position='below'>". |
|
190 | - __("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking."). |
|
191 | - "</div>"; |
|
189 | + print "<div dojoType='dijit.Tooltip' connectId='psql_trgm_similarity' position='below'>". |
|
190 | + __("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking."). |
|
191 | + "</div>"; |
|
192 | 192 | |
193 | - print "</fieldset><fieldset>"; |
|
193 | + print "</fieldset><fieldset>"; |
|
194 | 194 | |
195 | - print "<label>".__("Minimum title length:")."</label> "; |
|
196 | - print "<input dojoType=\"dijit.form.NumberSpinner\" |
|
195 | + print "<label>".__("Minimum title length:")."</label> "; |
|
196 | + print "<input dojoType=\"dijit.form.NumberSpinner\" |
|
197 | 197 | placeholder=\"32\" |
198 | 198 | required=\"1\" name=\"min_title_length\" value=\"$min_title_length\">"; |
199 | 199 | |
200 | - print "</fieldset><fieldset>"; |
|
200 | + print "</fieldset><fieldset>"; |
|
201 | 201 | |
202 | - print "<label class='checkbox'>"; |
|
203 | - print_checkbox("enable_globally", $enable_globally); |
|
204 | - print " ".__("Enable for all feeds:"); |
|
205 | - print "</label>"; |
|
202 | + print "<label class='checkbox'>"; |
|
203 | + print_checkbox("enable_globally", $enable_globally); |
|
204 | + print " ".__("Enable for all feeds:"); |
|
205 | + print "</label>"; |
|
206 | 206 | |
207 | - print "</fieldset>"; |
|
207 | + print "</fieldset>"; |
|
208 | 208 | |
209 | - print_button("submit", __("Save"), "class='alt-primary'"); |
|
210 | - print "</form>"; |
|
209 | + print_button("submit", __("Save"), "class='alt-primary'"); |
|
210 | + print "</form>"; |
|
211 | 211 | |
212 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
213 | - if (!array($enabled_feeds)) { |
|
214 | - $enabled_feeds = array(); |
|
215 | - } |
|
212 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
213 | + if (!array($enabled_feeds)) { |
|
214 | + $enabled_feeds = array(); |
|
215 | + } |
|
216 | 216 | |
217 | - $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); |
|
218 | - $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
217 | + $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); |
|
218 | + $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
219 | 219 | |
220 | - if (count($enabled_feeds) > 0) { |
|
221 | - print "<h3>".__("Currently enabled for (click to edit):")."</h3>"; |
|
220 | + if (count($enabled_feeds) > 0) { |
|
221 | + print "<h3>".__("Currently enabled for (click to edit):")."</h3>"; |
|
222 | 222 | |
223 | - print "<ul class=\"panel panel-scrollable list list-unstyled\">"; |
|
224 | - foreach ($enabled_feeds as $f) { |
|
225 | - print "<li>". |
|
226 | - "<i class='material-icons'>rss_feed</i> <a href='#' |
|
223 | + print "<ul class=\"panel panel-scrollable list list-unstyled\">"; |
|
224 | + foreach ($enabled_feeds as $f) { |
|
225 | + print "<li>". |
|
226 | + "<i class='material-icons'>rss_feed</i> <a href='#' |
|
227 | 227 | onclick='CommonDialogs.editFeed($f)'>". |
228 | - Feeds::getFeedTitle($f)."</a></li>"; |
|
229 | - } |
|
230 | - print "</ul>"; |
|
231 | - } |
|
232 | - } |
|
228 | + Feeds::getFeedTitle($f)."</a></li>"; |
|
229 | + } |
|
230 | + print "</ul>"; |
|
231 | + } |
|
232 | + } |
|
233 | 233 | |
234 | - print "</div>"; |
|
235 | - } |
|
234 | + print "</div>"; |
|
235 | + } |
|
236 | 236 | |
237 | - public function hook_prefs_edit_feed($feed_id) { |
|
238 | - print "<header>".__("Similarity (pg_trgm)")."</header>"; |
|
239 | - print "<section>"; |
|
237 | + public function hook_prefs_edit_feed($feed_id) { |
|
238 | + print "<header>".__("Similarity (pg_trgm)")."</header>"; |
|
239 | + print "<section>"; |
|
240 | 240 | |
241 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
242 | - if (!array($enabled_feeds)) { |
|
243 | - $enabled_feeds = array(); |
|
244 | - } |
|
241 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
242 | + if (!array($enabled_feeds)) { |
|
243 | + $enabled_feeds = array(); |
|
244 | + } |
|
245 | 245 | |
246 | - $key = array_search($feed_id, $enabled_feeds); |
|
247 | - $checked = $key !== false ? "checked" : ""; |
|
246 | + $key = array_search($feed_id, $enabled_feeds); |
|
247 | + $checked = $key !== false ? "checked" : ""; |
|
248 | 248 | |
249 | - print "<fieldset>"; |
|
249 | + print "<fieldset>"; |
|
250 | 250 | |
251 | - print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='trgm_similarity_enabled' |
|
251 | + print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='trgm_similarity_enabled' |
|
252 | 252 | name='trgm_similarity_enabled' $checked> ".__('Mark similar articles as read')."</label>"; |
253 | 253 | |
254 | - print "</fieldset>"; |
|
254 | + print "</fieldset>"; |
|
255 | 255 | |
256 | - print "</section>"; |
|
257 | - } |
|
256 | + print "</section>"; |
|
257 | + } |
|
258 | 258 | |
259 | - public function hook_prefs_save_feed($feed_id) { |
|
260 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
261 | - if (!is_array($enabled_feeds)) { |
|
262 | - $enabled_feeds = array(); |
|
263 | - } |
|
259 | + public function hook_prefs_save_feed($feed_id) { |
|
260 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
261 | + if (!is_array($enabled_feeds)) { |
|
262 | + $enabled_feeds = array(); |
|
263 | + } |
|
264 | 264 | |
265 | - $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]); |
|
266 | - $key = array_search($feed_id, $enabled_feeds); |
|
265 | + $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]); |
|
266 | + $key = array_search($feed_id, $enabled_feeds); |
|
267 | 267 | |
268 | - if ($enable) { |
|
269 | - if ($key === false) { |
|
270 | - array_push($enabled_feeds, $feed_id); |
|
271 | - } |
|
272 | - } else { |
|
273 | - if ($key !== false) { |
|
274 | - unset($enabled_feeds[$key]); |
|
275 | - } |
|
276 | - } |
|
268 | + if ($enable) { |
|
269 | + if ($key === false) { |
|
270 | + array_push($enabled_feeds, $feed_id); |
|
271 | + } |
|
272 | + } else { |
|
273 | + if ($key !== false) { |
|
274 | + unset($enabled_feeds[$key]); |
|
275 | + } |
|
276 | + } |
|
277 | 277 | |
278 | - $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
279 | - } |
|
278 | + $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
279 | + } |
|
280 | 280 | |
281 | - public function hook_article_filter($article) { |
|
281 | + public function hook_article_filter($article) { |
|
282 | 282 | |
283 | - if (DB_TYPE != "pgsql") { |
|
284 | - return $article; |
|
285 | - } |
|
283 | + if (DB_TYPE != "pgsql") { |
|
284 | + return $article; |
|
285 | + } |
|
286 | 286 | |
287 | - $res = $this->pdo->query("select 'similarity'::regproc"); |
|
288 | - if (!$res->fetch()) { |
|
289 | - return $article; |
|
290 | - } |
|
287 | + $res = $this->pdo->query("select 'similarity'::regproc"); |
|
288 | + if (!$res->fetch()) { |
|
289 | + return $article; |
|
290 | + } |
|
291 | 291 | |
292 | - $enable_globally = $this->host->get($this, "enable_globally"); |
|
292 | + $enable_globally = $this->host->get($this, "enable_globally"); |
|
293 | 293 | |
294 | - if (!$enable_globally) { |
|
295 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
296 | - $key = array_search($article["feed"]["id"], $enabled_feeds); |
|
297 | - if ($key === false) { |
|
298 | - return $article; |
|
299 | - } |
|
300 | - } |
|
294 | + if (!$enable_globally) { |
|
295 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
296 | + $key = array_search($article["feed"]["id"], $enabled_feeds); |
|
297 | + if ($key === false) { |
|
298 | + return $article; |
|
299 | + } |
|
300 | + } |
|
301 | 301 | |
302 | - $similarity = (float) $this->host->get($this, "similarity"); |
|
303 | - if ($similarity < 0.01) { |
|
304 | - return $article; |
|
305 | - } |
|
302 | + $similarity = (float) $this->host->get($this, "similarity"); |
|
303 | + if ($similarity < 0.01) { |
|
304 | + return $article; |
|
305 | + } |
|
306 | 306 | |
307 | - $min_title_length = (int) $this->host->get($this, "min_title_length"); |
|
308 | - if (mb_strlen($article["title"]) < $min_title_length) { |
|
309 | - return $article; |
|
310 | - } |
|
307 | + $min_title_length = (int) $this->host->get($this, "min_title_length"); |
|
308 | + if (mb_strlen($article["title"]) < $min_title_length) { |
|
309 | + return $article; |
|
310 | + } |
|
311 | 311 | |
312 | - $owner_uid = $article["owner_uid"]; |
|
313 | - $entry_guid = $article["guid_hashed"]; |
|
314 | - $title_escaped = $article["title"]; |
|
312 | + $owner_uid = $article["owner_uid"]; |
|
313 | + $entry_guid = $article["guid_hashed"]; |
|
314 | + $title_escaped = $article["title"]; |
|
315 | 315 | |
316 | - // trgm does not return similarity=1 for completely equal strings |
|
316 | + // trgm does not return similarity=1 for completely equal strings |
|
317 | 317 | |
318 | - $sth = $this->pdo->prepare("SELECT COUNT(id) AS nequal |
|
318 | + $sth = $this->pdo->prepare("SELECT COUNT(id) AS nequal |
|
319 | 319 | FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND |
320 | 320 | date_entered >= NOW() - interval '3 days' AND |
321 | 321 | title = ? AND |
322 | 322 | guid != ? AND |
323 | 323 | owner_uid = ?"); |
324 | - $sth->execute([$title_escaped, $entry_guid, $owner_uid]); |
|
324 | + $sth->execute([$title_escaped, $entry_guid, $owner_uid]); |
|
325 | 325 | |
326 | - $row = $sth->fetch(); |
|
327 | - $nequal = $row['nequal']; |
|
326 | + $row = $sth->fetch(); |
|
327 | + $nequal = $row['nequal']; |
|
328 | 328 | |
329 | - Debug::log("af_psql_trgm: num equals: $nequal", Debug::$LOG_EXTENDED); |
|
329 | + Debug::log("af_psql_trgm: num equals: $nequal", Debug::$LOG_EXTENDED); |
|
330 | 330 | |
331 | - if ($nequal != 0) { |
|
332 | - $article["force_catchup"] = true; |
|
333 | - return $article; |
|
334 | - } |
|
331 | + if ($nequal != 0) { |
|
332 | + $article["force_catchup"] = true; |
|
333 | + return $article; |
|
334 | + } |
|
335 | 335 | |
336 | - $sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, ?)) AS ms |
|
336 | + $sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, ?)) AS ms |
|
337 | 337 | FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND |
338 | 338 | date_entered >= NOW() - interval '1 day' AND |
339 | 339 | guid != ? AND |
340 | 340 | owner_uid = ?"); |
341 | - $sth->execute([$title_escaped, $entry_guid, $owner_uid]); |
|
341 | + $sth->execute([$title_escaped, $entry_guid, $owner_uid]); |
|
342 | 342 | |
343 | - $row = $sth->fetch(); |
|
344 | - $similarity_result = $row['ms']; |
|
343 | + $row = $sth->fetch(); |
|
344 | + $similarity_result = $row['ms']; |
|
345 | 345 | |
346 | - Debug::log("af_psql_trgm: similarity result: $similarity_result", Debug::$LOG_EXTENDED); |
|
346 | + Debug::log("af_psql_trgm: similarity result: $similarity_result", Debug::$LOG_EXTENDED); |
|
347 | 347 | |
348 | - if ($similarity_result >= $similarity) { |
|
349 | - $article["force_catchup"] = true; |
|
350 | - } |
|
348 | + if ($similarity_result >= $similarity) { |
|
349 | + $article["force_catchup"] = true; |
|
350 | + } |
|
351 | 351 | |
352 | - return $article; |
|
352 | + return $article; |
|
353 | 353 | |
354 | - } |
|
354 | + } |
|
355 | 355 | |
356 | - public function api_version() { |
|
357 | - return 2; |
|
358 | - } |
|
356 | + public function api_version() { |
|
357 | + return 2; |
|
358 | + } |
|
359 | 359 | |
360 | - private function filter_unknown_feeds($enabled_feeds) { |
|
361 | - $tmp = array(); |
|
360 | + private function filter_unknown_feeds($enabled_feeds) { |
|
361 | + $tmp = array(); |
|
362 | 362 | |
363 | - foreach ($enabled_feeds as $feed) { |
|
363 | + foreach ($enabled_feeds as $feed) { |
|
364 | 364 | |
365 | - $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); |
|
366 | - $sth->execute([$feed, $_SESSION['uid']]); |
|
365 | + $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); |
|
366 | + $sth->execute([$feed, $_SESSION['uid']]); |
|
367 | 367 | |
368 | - if ($row = $sth->fetch()) { |
|
369 | - array_push($tmp, $feed); |
|
370 | - } |
|
371 | - } |
|
368 | + if ($row = $sth->fetch()) { |
|
369 | + array_push($tmp, $feed); |
|
370 | + } |
|
371 | + } |
|
372 | 372 | |
373 | - return $tmp; |
|
374 | - } |
|
373 | + return $tmp; |
|
374 | + } |
|
375 | 375 | |
376 | 376 | } |
@@ -4,67 +4,67 @@ discard block |
||
4 | 4 | |
5 | 5 | class Af_Readability extends Plugin { |
6 | 6 | |
7 | - /* @var PluginHost $host */ |
|
8 | - private $host; |
|
7 | + /* @var PluginHost $host */ |
|
8 | + private $host; |
|
9 | 9 | |
10 | - public function about() { |
|
11 | - return array(1.0, |
|
12 | - "Try to inline article content using Readability", |
|
13 | - "fox"); |
|
14 | - } |
|
10 | + public function about() { |
|
11 | + return array(1.0, |
|
12 | + "Try to inline article content using Readability", |
|
13 | + "fox"); |
|
14 | + } |
|
15 | 15 | |
16 | - public function flags() { |
|
17 | - return array("needs_curl" => true); |
|
18 | - } |
|
16 | + public function flags() { |
|
17 | + return array("needs_curl" => true); |
|
18 | + } |
|
19 | 19 | |
20 | - public function save() { |
|
21 | - $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]); |
|
20 | + public function save() { |
|
21 | + $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]); |
|
22 | 22 | |
23 | - $this->host->set($this, "enable_share_anything", $enable_share_anything); |
|
23 | + $this->host->set($this, "enable_share_anything", $enable_share_anything); |
|
24 | 24 | |
25 | - echo __("Data saved."); |
|
26 | - } |
|
25 | + echo __("Data saved."); |
|
26 | + } |
|
27 | 27 | |
28 | - public function init($host) |
|
29 | - { |
|
30 | - $this->host = $host; |
|
28 | + public function init($host) |
|
29 | + { |
|
30 | + $this->host = $host; |
|
31 | 31 | |
32 | - if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
|
33 | - user_error("af_readability requires PHP 7.0", E_USER_WARNING); |
|
34 | - return; |
|
35 | - } |
|
32 | + if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
|
33 | + user_error("af_readability requires PHP 7.0", E_USER_WARNING); |
|
34 | + return; |
|
35 | + } |
|
36 | 36 | |
37 | - $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
38 | - $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
39 | - $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); |
|
40 | - $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); |
|
37 | + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
38 | + $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
39 | + $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); |
|
40 | + $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); |
|
41 | 41 | |
42 | - // Note: we have to install the hook even if disabled because init() is being run before plugin data has loaded |
|
43 | - // so we can't check for our storage-set options here |
|
44 | - $host->add_hook($host::HOOK_GET_FULL_TEXT, $this); |
|
42 | + // Note: we have to install the hook even if disabled because init() is being run before plugin data has loaded |
|
43 | + // so we can't check for our storage-set options here |
|
44 | + $host->add_hook($host::HOOK_GET_FULL_TEXT, $this); |
|
45 | 45 | |
46 | - $host->add_filter_action($this, "action_inline", __("Inline content")); |
|
47 | - } |
|
46 | + $host->add_filter_action($this, "action_inline", __("Inline content")); |
|
47 | + } |
|
48 | 48 | |
49 | - public function hook_prefs_tab($args) { |
|
50 | - if ($args != "prefFeeds") { |
|
51 | - return; |
|
52 | - } |
|
49 | + public function hook_prefs_tab($args) { |
|
50 | + if ($args != "prefFeeds") { |
|
51 | + return; |
|
52 | + } |
|
53 | 53 | |
54 | - print "<div dojoType='dijit.layout.AccordionPane' |
|
54 | + print "<div dojoType='dijit.layout.AccordionPane' |
|
55 | 55 | title=\"<i class='material-icons'>extension</i> ".__('Readability settings (af_readability)')."\">"; |
56 | 56 | |
57 | - if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
|
58 | - print_error("This plugin requires PHP 7.0."); |
|
59 | - } else { |
|
57 | + if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
|
58 | + print_error("This plugin requires PHP 7.0."); |
|
59 | + } else { |
|
60 | 60 | |
61 | - print "<h2>".__("Global settings")."</h2>"; |
|
61 | + print "<h2>".__("Global settings")."</h2>"; |
|
62 | 62 | |
63 | - print_notice("Enable for specific feeds in the feed editor."); |
|
63 | + print_notice("Enable for specific feeds in the feed editor."); |
|
64 | 64 | |
65 | - print "<form dojoType='dijit.form.Form'>"; |
|
65 | + print "<form dojoType='dijit.form.Form'>"; |
|
66 | 66 | |
67 | - print "<script type='dojo/method' event='onSubmit' args='evt'> |
|
67 | + print "<script type='dojo/method' event='onSubmit' args='evt'> |
|
68 | 68 | evt.preventDefault(); |
69 | 69 | if (this.validate()) { |
70 | 70 | console.log(dojo.objectToQuery(this.getValues())); |
@@ -78,223 +78,223 @@ discard block |
||
78 | 78 | } |
79 | 79 | </script>"; |
80 | 80 | |
81 | - print_hidden("op", "pluginhandler"); |
|
82 | - print_hidden("method", "save"); |
|
83 | - print_hidden("plugin", "af_readability"); |
|
81 | + print_hidden("op", "pluginhandler"); |
|
82 | + print_hidden("method", "save"); |
|
83 | + print_hidden("plugin", "af_readability"); |
|
84 | 84 | |
85 | - $enable_share_anything = $this->host->get($this, "enable_share_anything"); |
|
85 | + $enable_share_anything = $this->host->get($this, "enable_share_anything"); |
|
86 | 86 | |
87 | - print "<fieldset>"; |
|
88 | - print "<label class='checkbox'> "; |
|
89 | - print_checkbox("enable_share_anything", $enable_share_anything); |
|
90 | - print " ".__("Provide full-text services to core code (bookmarklets) and other plugins"); |
|
91 | - print "</label>"; |
|
92 | - print "</fieldset>"; |
|
87 | + print "<fieldset>"; |
|
88 | + print "<label class='checkbox'> "; |
|
89 | + print_checkbox("enable_share_anything", $enable_share_anything); |
|
90 | + print " ".__("Provide full-text services to core code (bookmarklets) and other plugins"); |
|
91 | + print "</label>"; |
|
92 | + print "</fieldset>"; |
|
93 | 93 | |
94 | - print_button("submit", __("Save"), "class='alt-primary'"); |
|
95 | - print "</form>"; |
|
94 | + print_button("submit", __("Save"), "class='alt-primary'"); |
|
95 | + print "</form>"; |
|
96 | 96 | |
97 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
98 | - if (!is_array($enabled_feeds)) { |
|
99 | - $enabled_feeds = array(); |
|
100 | - } |
|
97 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
98 | + if (!is_array($enabled_feeds)) { |
|
99 | + $enabled_feeds = array(); |
|
100 | + } |
|
101 | 101 | |
102 | - $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); |
|
103 | - $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
102 | + $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds); |
|
103 | + $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
104 | 104 | |
105 | - if (count($enabled_feeds) > 0) { |
|
106 | - print "<h3>".__("Currently enabled for (click to edit):")."</h3>"; |
|
105 | + if (count($enabled_feeds) > 0) { |
|
106 | + print "<h3>".__("Currently enabled for (click to edit):")."</h3>"; |
|
107 | 107 | |
108 | - print "<ul class='panel panel-scrollable list list-unstyled'>"; |
|
109 | - foreach ($enabled_feeds as $f) { |
|
110 | - print "<li><i class='material-icons'>rss_feed</i> <a href='#' |
|
108 | + print "<ul class='panel panel-scrollable list list-unstyled'>"; |
|
109 | + foreach ($enabled_feeds as $f) { |
|
110 | + print "<li><i class='material-icons'>rss_feed</i> <a href='#' |
|
111 | 111 | onclick='CommonDialogs.editFeed($f)'>". |
112 | - Feeds::getFeedTitle($f)."</a></li>"; |
|
113 | - } |
|
114 | - print "</ul>"; |
|
115 | - } |
|
112 | + Feeds::getFeedTitle($f)."</a></li>"; |
|
113 | + } |
|
114 | + print "</ul>"; |
|
115 | + } |
|
116 | 116 | |
117 | - } |
|
117 | + } |
|
118 | 118 | |
119 | - print "</div>"; |
|
120 | - } |
|
119 | + print "</div>"; |
|
120 | + } |
|
121 | 121 | |
122 | - public function hook_prefs_edit_feed($feed_id) { |
|
123 | - print "<header>".__("Readability")."</header>"; |
|
124 | - print "<section>"; |
|
122 | + public function hook_prefs_edit_feed($feed_id) { |
|
123 | + print "<header>".__("Readability")."</header>"; |
|
124 | + print "<section>"; |
|
125 | 125 | |
126 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
127 | - if (!is_array($enabled_feeds)) { |
|
128 | - $enabled_feeds = array(); |
|
129 | - } |
|
126 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
127 | + if (!is_array($enabled_feeds)) { |
|
128 | + $enabled_feeds = array(); |
|
129 | + } |
|
130 | 130 | |
131 | - $key = array_search($feed_id, $enabled_feeds); |
|
132 | - $checked = $key !== false ? "checked" : ""; |
|
131 | + $key = array_search($feed_id, $enabled_feeds); |
|
132 | + $checked = $key !== false ? "checked" : ""; |
|
133 | 133 | |
134 | - print "<fieldset>"; |
|
134 | + print "<fieldset>"; |
|
135 | 135 | |
136 | - print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_readability_enabled' |
|
136 | + print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_readability_enabled' |
|
137 | 137 | name='af_readability_enabled' $checked> ".__('Inline article content')."</label>"; |
138 | 138 | |
139 | - print "</fieldset>"; |
|
139 | + print "</fieldset>"; |
|
140 | 140 | |
141 | - print "</section>"; |
|
142 | - } |
|
141 | + print "</section>"; |
|
142 | + } |
|
143 | 143 | |
144 | - public function hook_prefs_save_feed($feed_id) { |
|
145 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
146 | - if (!is_array($enabled_feeds)) { |
|
147 | - $enabled_feeds = array(); |
|
148 | - } |
|
144 | + public function hook_prefs_save_feed($feed_id) { |
|
145 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
146 | + if (!is_array($enabled_feeds)) { |
|
147 | + $enabled_feeds = array(); |
|
148 | + } |
|
149 | 149 | |
150 | - $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]); |
|
151 | - $key = array_search($feed_id, $enabled_feeds); |
|
150 | + $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]); |
|
151 | + $key = array_search($feed_id, $enabled_feeds); |
|
152 | 152 | |
153 | - if ($enable) { |
|
154 | - if ($key === false) { |
|
155 | - array_push($enabled_feeds, $feed_id); |
|
156 | - } |
|
157 | - } else { |
|
158 | - if ($key !== false) { |
|
159 | - unset($enabled_feeds[$key]); |
|
160 | - } |
|
161 | - } |
|
153 | + if ($enable) { |
|
154 | + if ($key === false) { |
|
155 | + array_push($enabled_feeds, $feed_id); |
|
156 | + } |
|
157 | + } else { |
|
158 | + if ($key !== false) { |
|
159 | + unset($enabled_feeds[$key]); |
|
160 | + } |
|
161 | + } |
|
162 | 162 | |
163 | - $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
164 | - } |
|
163 | + $this->host->set($this, "enabled_feeds", $enabled_feeds); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
168 | - */ |
|
169 | - public function hook_article_filter_action($article, $action) { |
|
170 | - return $this->process_article($article); |
|
171 | - } |
|
166 | + /** |
|
167 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
168 | + */ |
|
169 | + public function hook_article_filter_action($article, $action) { |
|
170 | + return $this->process_article($article); |
|
171 | + } |
|
172 | 172 | |
173 | - public function extract_content($url) { |
|
173 | + public function extract_content($url) { |
|
174 | 174 | |
175 | - global $fetch_effective_url; |
|
175 | + global $fetch_effective_url; |
|
176 | 176 | |
177 | - $tmp = fetch_file_contents([ |
|
178 | - "url" => $url, |
|
179 | - "http_accept" => "text/*", |
|
180 | - "type" => "text/html"]); |
|
177 | + $tmp = fetch_file_contents([ |
|
178 | + "url" => $url, |
|
179 | + "http_accept" => "text/*", |
|
180 | + "type" => "text/html"]); |
|
181 | 181 | |
182 | - if ($tmp && mb_strlen($tmp) < 1024 * 500) { |
|
183 | - $tmpdoc = new DOMDocument("1.0", "UTF-8"); |
|
182 | + if ($tmp && mb_strlen($tmp) < 1024 * 500) { |
|
183 | + $tmpdoc = new DOMDocument("1.0", "UTF-8"); |
|
184 | 184 | |
185 | - if (!@$tmpdoc->loadHTML($tmp)) { |
|
186 | - return false; |
|
187 | - } |
|
185 | + if (!@$tmpdoc->loadHTML($tmp)) { |
|
186 | + return false; |
|
187 | + } |
|
188 | 188 | |
189 | - // this is the worst hack yet :( |
|
190 | - if (strtolower($tmpdoc->encoding) != 'utf-8') { |
|
191 | - $tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp); |
|
192 | - if (empty($tmpdoc->encoding)) { |
|
193 | - $tmp = mb_convert_encoding($tmp, 'utf-8'); |
|
194 | - } else { |
|
195 | - $tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding); |
|
196 | - } |
|
197 | - } |
|
189 | + // this is the worst hack yet :( |
|
190 | + if (strtolower($tmpdoc->encoding) != 'utf-8') { |
|
191 | + $tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp); |
|
192 | + if (empty($tmpdoc->encoding)) { |
|
193 | + $tmp = mb_convert_encoding($tmp, 'utf-8'); |
|
194 | + } else { |
|
195 | + $tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding); |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | - try { |
|
200 | - $r = new Readability(new Configuration()); |
|
199 | + try { |
|
200 | + $r = new Readability(new Configuration()); |
|
201 | 201 | |
202 | - if ($r->parse($tmp)) { |
|
202 | + if ($r->parse($tmp)) { |
|
203 | 203 | |
204 | - $tmpxpath = new DOMXPath($r->getDOMDOcument()); |
|
205 | - $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); |
|
204 | + $tmpxpath = new DOMXPath($r->getDOMDOcument()); |
|
205 | + $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); |
|
206 | 206 | |
207 | - foreach ($entries as $entry) { |
|
208 | - if ($entry->hasAttribute("href")) { |
|
209 | - $entry->setAttribute("href", |
|
210 | - rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href"))); |
|
207 | + foreach ($entries as $entry) { |
|
208 | + if ($entry->hasAttribute("href")) { |
|
209 | + $entry->setAttribute("href", |
|
210 | + rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href"))); |
|
211 | 211 | |
212 | - } |
|
212 | + } |
|
213 | 213 | |
214 | - if ($entry->hasAttribute("src")) { |
|
215 | - $entry->setAttribute("src", |
|
216 | - rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src"))); |
|
214 | + if ($entry->hasAttribute("src")) { |
|
215 | + $entry->setAttribute("src", |
|
216 | + rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src"))); |
|
217 | 217 | |
218 | - } |
|
219 | - } |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - return $r->getContent(); |
|
222 | - } |
|
221 | + return $r->getContent(); |
|
222 | + } |
|
223 | 223 | |
224 | - } catch (Exception $e) { |
|
225 | - return false; |
|
226 | - } |
|
227 | - } |
|
224 | + } catch (Exception $e) { |
|
225 | + return false; |
|
226 | + } |
|
227 | + } |
|
228 | 228 | |
229 | - return false; |
|
230 | - } |
|
229 | + return false; |
|
230 | + } |
|
231 | 231 | |
232 | - public function process_article($article) { |
|
232 | + public function process_article($article) { |
|
233 | 233 | |
234 | - $extracted_content = $this->extract_content($article["link"]); |
|
234 | + $extracted_content = $this->extract_content($article["link"]); |
|
235 | 235 | |
236 | - # let's see if there's anything of value in there |
|
237 | - $content_test = trim(strip_tags(sanitize($extracted_content))); |
|
236 | + # let's see if there's anything of value in there |
|
237 | + $content_test = trim(strip_tags(sanitize($extracted_content))); |
|
238 | 238 | |
239 | - if ($content_test) { |
|
240 | - $article["content"] = $extracted_content; |
|
241 | - } |
|
239 | + if ($content_test) { |
|
240 | + $article["content"] = $extracted_content; |
|
241 | + } |
|
242 | 242 | |
243 | - return $article; |
|
244 | - } |
|
243 | + return $article; |
|
244 | + } |
|
245 | 245 | |
246 | - public function hook_article_filter($article) { |
|
246 | + public function hook_article_filter($article) { |
|
247 | 247 | |
248 | - $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
249 | - if (!is_array($enabled_feeds)) { |
|
250 | - return $article; |
|
251 | - } |
|
248 | + $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
|
249 | + if (!is_array($enabled_feeds)) { |
|
250 | + return $article; |
|
251 | + } |
|
252 | 252 | |
253 | - $key = array_search($article["feed"]["id"], $enabled_feeds); |
|
254 | - if ($key === false) { |
|
255 | - return $article; |
|
256 | - } |
|
253 | + $key = array_search($article["feed"]["id"], $enabled_feeds); |
|
254 | + if ($key === false) { |
|
255 | + return $article; |
|
256 | + } |
|
257 | 257 | |
258 | - return $this->process_article($article); |
|
258 | + return $this->process_article($article); |
|
259 | 259 | |
260 | - } |
|
260 | + } |
|
261 | 261 | |
262 | - public function hook_get_full_text($link) |
|
263 | - { |
|
264 | - $enable_share_anything = $this->host->get($this, "enable_share_anything"); |
|
262 | + public function hook_get_full_text($link) |
|
263 | + { |
|
264 | + $enable_share_anything = $this->host->get($this, "enable_share_anything"); |
|
265 | 265 | |
266 | - if ($enable_share_anything) { |
|
267 | - $extracted_content = $this->extract_content($link); |
|
266 | + if ($enable_share_anything) { |
|
267 | + $extracted_content = $this->extract_content($link); |
|
268 | 268 | |
269 | - # let's see if there's anything of value in there |
|
270 | - $content_test = trim(strip_tags(sanitize($extracted_content))); |
|
269 | + # let's see if there's anything of value in there |
|
270 | + $content_test = trim(strip_tags(sanitize($extracted_content))); |
|
271 | 271 | |
272 | - if ($content_test) { |
|
273 | - return $extracted_content; |
|
274 | - } |
|
275 | - } |
|
272 | + if ($content_test) { |
|
273 | + return $extracted_content; |
|
274 | + } |
|
275 | + } |
|
276 | 276 | |
277 | - return false; |
|
278 | - } |
|
277 | + return false; |
|
278 | + } |
|
279 | 279 | |
280 | - public function api_version() { |
|
281 | - return 2; |
|
282 | - } |
|
280 | + public function api_version() { |
|
281 | + return 2; |
|
282 | + } |
|
283 | 283 | |
284 | - private function filter_unknown_feeds($enabled_feeds) { |
|
285 | - $tmp = array(); |
|
284 | + private function filter_unknown_feeds($enabled_feeds) { |
|
285 | + $tmp = array(); |
|
286 | 286 | |
287 | - foreach ($enabled_feeds as $feed) { |
|
287 | + foreach ($enabled_feeds as $feed) { |
|
288 | 288 | |
289 | - $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); |
|
290 | - $sth->execute([$feed, $_SESSION['uid']]); |
|
289 | + $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); |
|
290 | + $sth->execute([$feed, $_SESSION['uid']]); |
|
291 | 291 | |
292 | - if ($row = $sth->fetch()) { |
|
293 | - array_push($tmp, $feed); |
|
294 | - } |
|
295 | - } |
|
292 | + if ($row = $sth->fetch()) { |
|
293 | + array_push($tmp, $feed); |
|
294 | + } |
|
295 | + } |
|
296 | 296 | |
297 | - return $tmp; |
|
298 | - } |
|
297 | + return $tmp; |
|
298 | + } |
|
299 | 299 | |
300 | 300 | } |
@@ -1,44 +1,44 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | class Af_RedditImgur extends Plugin { |
3 | 3 | |
4 | - /* @var PluginHost $host */ |
|
5 | - private $host; |
|
4 | + /* @var PluginHost $host */ |
|
5 | + private $host; |
|
6 | 6 | |
7 | - public function about() { |
|
8 | - return array(1.0, |
|
9 | - "Inline images (and other content) in Reddit RSS feeds", |
|
10 | - "fox"); |
|
11 | - } |
|
7 | + public function about() { |
|
8 | + return array(1.0, |
|
9 | + "Inline images (and other content) in Reddit RSS feeds", |
|
10 | + "fox"); |
|
11 | + } |
|
12 | 12 | |
13 | - public function flags() { |
|
14 | - return array("needs_curl" => true); |
|
15 | - } |
|
13 | + public function flags() { |
|
14 | + return array("needs_curl" => true); |
|
15 | + } |
|
16 | 16 | |
17 | - public function init($host) { |
|
18 | - $this->host = $host; |
|
17 | + public function init($host) { |
|
18 | + $this->host = $host; |
|
19 | 19 | |
20 | - $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
21 | - $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
22 | - } |
|
20 | + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); |
|
21 | + $host->add_hook($host::HOOK_PREFS_TAB, $this); |
|
22 | + } |
|
23 | 23 | |
24 | - public function hook_prefs_tab($args) { |
|
25 | - if ($args != "prefFeeds") { |
|
26 | - return; |
|
27 | - } |
|
24 | + public function hook_prefs_tab($args) { |
|
25 | + if ($args != "prefFeeds") { |
|
26 | + return; |
|
27 | + } |
|
28 | 28 | |
29 | - print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
29 | + print "<div dojoType=\"dijit.layout.AccordionPane\" |
|
30 | 30 | title=\"<i class='material-icons'>extension</i> ".__('Reddit content settings (af_redditimgur)')."\">"; |
31 | 31 | |
32 | - $enable_readability = $this->host->get($this, "enable_readability"); |
|
33 | - $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck"); |
|
32 | + $enable_readability = $this->host->get($this, "enable_readability"); |
|
33 | + $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck"); |
|
34 | 34 | |
35 | - if (version_compare(PHP_VERSION, '5.6.0', '<')) { |
|
36 | - print_error("Readability requires PHP version 5.6."); |
|
37 | - } |
|
35 | + if (version_compare(PHP_VERSION, '5.6.0', '<')) { |
|
36 | + print_error("Readability requires PHP version 5.6."); |
|
37 | + } |
|
38 | 38 | |
39 | - print "<form dojoType='dijit.form.Form'>"; |
|
39 | + print "<form dojoType='dijit.form.Form'>"; |
|
40 | 40 | |
41 | - print "<script type='dojo/method' event='onSubmit' args='evt'> |
|
41 | + print "<script type='dojo/method' event='onSubmit' args='evt'> |
|
42 | 42 | evt.preventDefault(); |
43 | 43 | if (this.validate()) { |
44 | 44 | console.log(dojo.objectToQuery(this.getValues())); |
@@ -52,235 +52,235 @@ discard block |
||
52 | 52 | } |
53 | 53 | </script>"; |
54 | 54 | |
55 | - print_hidden("op", "pluginhandler"); |
|
56 | - print_hidden("method", "save"); |
|
57 | - print_hidden("plugin", "af_redditimgur"); |
|
55 | + print_hidden("op", "pluginhandler"); |
|
56 | + print_hidden("method", "save"); |
|
57 | + print_hidden("plugin", "af_redditimgur"); |
|
58 | 58 | |
59 | - print "<fieldset class='narrow'>"; |
|
60 | - print "<label class='checkbox'>"; |
|
61 | - print_checkbox("enable_readability", $enable_readability); |
|
62 | - print " ".__("Extract missing content using Readability (requires af_readability)")."</label>"; |
|
63 | - print "</fieldset>"; |
|
59 | + print "<fieldset class='narrow'>"; |
|
60 | + print "<label class='checkbox'>"; |
|
61 | + print_checkbox("enable_readability", $enable_readability); |
|
62 | + print " ".__("Extract missing content using Readability (requires af_readability)")."</label>"; |
|
63 | + print "</fieldset>"; |
|
64 | 64 | |
65 | - print "<fieldset class='narrow'>"; |
|
66 | - print "<label class='checkbox'>"; |
|
67 | - print_checkbox("enable_content_dupcheck", $enable_content_dupcheck); |
|
68 | - print " ".__("Enable additional duplicate checking")."</label>"; |
|
69 | - print "</fieldset>"; |
|
65 | + print "<fieldset class='narrow'>"; |
|
66 | + print "<label class='checkbox'>"; |
|
67 | + print_checkbox("enable_content_dupcheck", $enable_content_dupcheck); |
|
68 | + print " ".__("Enable additional duplicate checking")."</label>"; |
|
69 | + print "</fieldset>"; |
|
70 | 70 | |
71 | - print_button("submit", __("Save"), 'class="alt-primary"'); |
|
72 | - print "</form>"; |
|
71 | + print_button("submit", __("Save"), 'class="alt-primary"'); |
|
72 | + print "</form>"; |
|
73 | 73 | |
74 | - print "</div>"; |
|
75 | - } |
|
74 | + print "</div>"; |
|
75 | + } |
|
76 | 76 | |
77 | - public function save() { |
|
78 | - $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]); |
|
79 | - $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]); |
|
77 | + public function save() { |
|
78 | + $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]); |
|
79 | + $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]); |
|
80 | 80 | |
81 | - $this->host->set($this, "enable_readability", $enable_readability, false); |
|
82 | - $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck); |
|
81 | + $this->host->set($this, "enable_readability", $enable_readability, false); |
|
82 | + $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck); |
|
83 | 83 | |
84 | - echo __("Configuration saved"); |
|
85 | - } |
|
84 | + echo __("Configuration saved"); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
89 | - */ |
|
90 | - private function inline_stuff($article, &$doc, $xpath) { |
|
87 | + /** |
|
88 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
89 | + */ |
|
90 | + private function inline_stuff($article, &$doc, $xpath) { |
|
91 | 91 | |
92 | - $entries = $xpath->query('(//a[@href]|//img[@src])'); |
|
93 | - $img_entries = $xpath->query("(//img[@src])"); |
|
92 | + $entries = $xpath->query('(//a[@href]|//img[@src])'); |
|
93 | + $img_entries = $xpath->query("(//img[@src])"); |
|
94 | 94 | |
95 | - $found = false; |
|
96 | - //$debug = 1; |
|
95 | + $found = false; |
|
96 | + //$debug = 1; |
|
97 | 97 | |
98 | - foreach ($entries as $entry) { |
|
99 | - if ($entry->hasAttribute("href") && strpos($entry->getAttribute("href"), "reddit.com") === false) { |
|
98 | + foreach ($entries as $entry) { |
|
99 | + if ($entry->hasAttribute("href") && strpos($entry->getAttribute("href"), "reddit.com") === false) { |
|
100 | 100 | |
101 | - Debug::log("processing href: ".$entry->getAttribute("href"), Debug::$LOG_VERBOSE); |
|
101 | + Debug::log("processing href: ".$entry->getAttribute("href"), Debug::$LOG_VERBOSE); |
|
102 | 102 | |
103 | - $matches = array(); |
|
103 | + $matches = array(); |
|
104 | 104 | |
105 | - if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) { |
|
106 | - Debug::log("handling as twitter: ".$matches[1]." ".$matches[2], Debug::$LOG_VERBOSE); |
|
105 | + if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) { |
|
106 | + Debug::log("handling as twitter: ".$matches[1]." ".$matches[2], Debug::$LOG_VERBOSE); |
|
107 | 107 | |
108 | - $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=".urlencode($entry->getAttribute("href"))); |
|
108 | + $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=".urlencode($entry->getAttribute("href"))); |
|
109 | 109 | |
110 | - if ($oembed_result) { |
|
111 | - $oembed_result = json_decode($oembed_result, true); |
|
110 | + if ($oembed_result) { |
|
111 | + $oembed_result = json_decode($oembed_result, true); |
|
112 | 112 | |
113 | - if ($oembed_result && isset($oembed_result["html"])) { |
|
113 | + if ($oembed_result && isset($oembed_result["html"])) { |
|
114 | 114 | |
115 | - $tmp = new DOMDocument(); |
|
116 | - if ($tmp->loadHTML('<?xml encoding="utf-8" ?>'.$oembed_result["html"])) { |
|
117 | - $p = $doc->createElement("p"); |
|
115 | + $tmp = new DOMDocument(); |
|
116 | + if ($tmp->loadHTML('<?xml encoding="utf-8" ?>'.$oembed_result["html"])) { |
|
117 | + $p = $doc->createElement("p"); |
|
118 | 118 | |
119 | - $p->appendChild($doc->importNode( |
|
120 | - $tmp->getElementsByTagName("blockquote")->item(0), true)); |
|
119 | + $p->appendChild($doc->importNode( |
|
120 | + $tmp->getElementsByTagName("blockquote")->item(0), true)); |
|
121 | 121 | |
122 | - $br = $doc->createElement('br'); |
|
123 | - $entry->parentNode->insertBefore($p, $entry); |
|
124 | - $entry->parentNode->insertBefore($br, $entry); |
|
122 | + $br = $doc->createElement('br'); |
|
123 | + $entry->parentNode->insertBefore($p, $entry); |
|
124 | + $entry->parentNode->insertBefore($br, $entry); |
|
125 | 125 | |
126 | - $found = 1; |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
126 | + $found = 1; |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | 131 | |
132 | - if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
|
133 | - $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); |
|
134 | - } |
|
132 | + if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
|
133 | + $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); |
|
134 | + } |
|
135 | 135 | |
136 | - if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
|
136 | + if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
|
137 | 137 | |
138 | - Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE); |
|
138 | + Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE); |
|
139 | 139 | |
140 | - $source_stream = 'https://giant.gfycat.com/'.$matches[2].'.mp4'; |
|
141 | - $poster_url = 'https://thumbs.gfycat.com/'.$matches[2].'-mobile.jpg'; |
|
140 | + $source_stream = 'https://giant.gfycat.com/'.$matches[2].'.mp4'; |
|
141 | + $poster_url = 'https://thumbs.gfycat.com/'.$matches[2].'-mobile.jpg'; |
|
142 | 142 | |
143 | - $content_type = $this->get_content_type($source_stream); |
|
143 | + $content_type = $this->get_content_type($source_stream); |
|
144 | 144 | |
145 | - if (strpos($content_type, "video/") !== false) { |
|
146 | - $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
147 | - $found = 1; |
|
148 | - } |
|
149 | - } |
|
145 | + if (strpos($content_type, "video/") !== false) { |
|
146 | + $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
147 | + $found = 1; |
|
148 | + } |
|
149 | + } |
|
150 | 150 | |
151 | - if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry->getAttribute("href"), $matches)) { |
|
151 | + if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry->getAttribute("href"), $matches)) { |
|
152 | 152 | |
153 | - Debug::log("Handling as reddit inline video", Debug::$LOG_VERBOSE); |
|
153 | + Debug::log("Handling as reddit inline video", Debug::$LOG_VERBOSE); |
|
154 | 154 | |
155 | - $img = $img_entries->item(0); |
|
155 | + $img = $img_entries->item(0); |
|
156 | 156 | |
157 | - if ($img) { |
|
158 | - $poster_url = $img->getAttribute("src"); |
|
159 | - } else { |
|
160 | - $poster_url = false; |
|
161 | - } |
|
157 | + if ($img) { |
|
158 | + $poster_url = $img->getAttribute("src"); |
|
159 | + } else { |
|
160 | + $poster_url = false; |
|
161 | + } |
|
162 | 162 | |
163 | - // Get original article URL from v.redd.it redirects |
|
164 | - $source_article_url = $this->get_location($matches[0]); |
|
165 | - Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE); |
|
166 | - |
|
167 | - $source_stream = false; |
|
168 | - |
|
169 | - if ($source_article_url) { |
|
170 | - $j = json_decode(fetch_file_contents($source_article_url.".json"), true); |
|
171 | - |
|
172 | - if ($j) { |
|
173 | - foreach ($j as $listing) { |
|
174 | - foreach ($listing["data"]["children"] as $child) { |
|
175 | - if ($child["data"]["url"] == $matches[0]) { |
|
176 | - try { |
|
177 | - $source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"]; |
|
178 | - } catch (Exception $e) { |
|
179 | - } |
|
180 | - break 2; |
|
181 | - } |
|
182 | - } |
|
183 | - } |
|
184 | - } |
|
185 | - } |
|
163 | + // Get original article URL from v.redd.it redirects |
|
164 | + $source_article_url = $this->get_location($matches[0]); |
|
165 | + Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE); |
|
166 | + |
|
167 | + $source_stream = false; |
|
168 | + |
|
169 | + if ($source_article_url) { |
|
170 | + $j = json_decode(fetch_file_contents($source_article_url.".json"), true); |
|
171 | + |
|
172 | + if ($j) { |
|
173 | + foreach ($j as $listing) { |
|
174 | + foreach ($listing["data"]["children"] as $child) { |
|
175 | + if ($child["data"]["url"] == $matches[0]) { |
|
176 | + try { |
|
177 | + $source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"]; |
|
178 | + } catch (Exception $e) { |
|
179 | + } |
|
180 | + break 2; |
|
181 | + } |
|
182 | + } |
|
183 | + } |
|
184 | + } |
|
185 | + } |
|
186 | 186 | |
187 | - if (!$source_stream) { |
|
188 | - $source_stream = "https://v.redd.it/".$matches[1]."/DASH_600_K"; |
|
189 | - } |
|
187 | + if (!$source_stream) { |
|
188 | + $source_stream = "https://v.redd.it/".$matches[1]."/DASH_600_K"; |
|
189 | + } |
|
190 | 190 | |
191 | - $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
192 | - $found = 1; |
|
193 | - } |
|
191 | + $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
192 | + $found = 1; |
|
193 | + } |
|
194 | 194 | |
195 | - if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) { |
|
195 | + if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) { |
|
196 | 196 | |
197 | - Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE); |
|
197 | + Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE); |
|
198 | 198 | |
199 | - $tmp = fetch_file_contents($entry->getAttribute("href")); |
|
199 | + $tmp = fetch_file_contents($entry->getAttribute("href")); |
|
200 | 200 | |
201 | - if ($tmp) { |
|
202 | - $tmpdoc = new DOMDocument(); |
|
201 | + if ($tmp) { |
|
202 | + $tmpdoc = new DOMDocument(); |
|
203 | 203 | |
204 | - if (@$tmpdoc->loadHTML($tmp)) { |
|
205 | - $tmpxpath = new DOMXPath($tmpdoc); |
|
204 | + if (@$tmpdoc->loadHTML($tmp)) { |
|
205 | + $tmpxpath = new DOMXPath($tmpdoc); |
|
206 | 206 | |
207 | - $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0); |
|
208 | - $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0); |
|
207 | + $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0); |
|
208 | + $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0); |
|
209 | 209 | |
210 | - if ($source_node && $poster_node) { |
|
211 | - $source_stream = $source_node->getAttribute("src"); |
|
212 | - $poster_url = $poster_node->getAttribute("poster"); |
|
210 | + if ($source_node && $poster_node) { |
|
211 | + $source_stream = $source_node->getAttribute("src"); |
|
212 | + $poster_url = $poster_node->getAttribute("poster"); |
|
213 | 213 | |
214 | - $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
215 | - $found = 1; |
|
216 | - } |
|
217 | - } |
|
218 | - } |
|
219 | - } |
|
214 | + $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
215 | + $found = 1; |
|
216 | + } |
|
217 | + } |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - // imgur .gif -> .gifv |
|
222 | - if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { |
|
223 | - Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE); |
|
221 | + // imgur .gif -> .gifv |
|
222 | + if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { |
|
223 | + Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE); |
|
224 | 224 | |
225 | - $entry->setAttribute("href", |
|
226 | - str_replace(".gif", ".gifv", $entry->getAttribute("href"))); |
|
227 | - } |
|
225 | + $entry->setAttribute("href", |
|
226 | + str_replace(".gif", ".gifv", $entry->getAttribute("href"))); |
|
227 | + } |
|
228 | 228 | |
229 | - if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) { |
|
230 | - Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE); |
|
229 | + if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) { |
|
230 | + Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE); |
|
231 | 231 | |
232 | - $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
|
232 | + $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
|
233 | 233 | |
234 | - if (strpos($source_stream, "imgur.com") !== false) { |
|
235 | - $poster_url = str_replace(".mp4", "h.jpg", $source_stream); |
|
236 | - } |
|
234 | + if (strpos($source_stream, "imgur.com") !== false) { |
|
235 | + $poster_url = str_replace(".mp4", "h.jpg", $source_stream); |
|
236 | + } |
|
237 | 237 | |
238 | - $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
238 | + $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
239 | 239 | |
240 | - $found = true; |
|
241 | - } |
|
240 | + $found = true; |
|
241 | + } |
|
242 | 242 | |
243 | - $matches = array(); |
|
244 | - if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
245 | - preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
246 | - preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
247 | - preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
|
243 | + $matches = array(); |
|
244 | + if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
245 | + preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
246 | + preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
|
247 | + preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
|
248 | 248 | |
249 | - $vid_id = $matches[1]; |
|
249 | + $vid_id = $matches[1]; |
|
250 | 250 | |
251 | - Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE); |
|
251 | + Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE); |
|
252 | 252 | |
253 | - $iframe = $doc->createElement("iframe"); |
|
254 | - $iframe->setAttribute("class", "youtube-player"); |
|
255 | - $iframe->setAttribute("type", "text/html"); |
|
256 | - $iframe->setAttribute("width", "640"); |
|
257 | - $iframe->setAttribute("height", "385"); |
|
258 | - $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); |
|
259 | - $iframe->setAttribute("allowfullscreen", "1"); |
|
260 | - $iframe->setAttribute("frameborder", "0"); |
|
253 | + $iframe = $doc->createElement("iframe"); |
|
254 | + $iframe->setAttribute("class", "youtube-player"); |
|
255 | + $iframe->setAttribute("type", "text/html"); |
|
256 | + $iframe->setAttribute("width", "640"); |
|
257 | + $iframe->setAttribute("height", "385"); |
|
258 | + $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); |
|
259 | + $iframe->setAttribute("allowfullscreen", "1"); |
|
260 | + $iframe->setAttribute("frameborder", "0"); |
|
261 | 261 | |
262 | - $br = $doc->createElement('br'); |
|
263 | - $entry->parentNode->insertBefore($iframe, $entry); |
|
264 | - $entry->parentNode->insertBefore($br, $entry); |
|
262 | + $br = $doc->createElement('br'); |
|
263 | + $entry->parentNode->insertBefore($iframe, $entry); |
|
264 | + $entry->parentNode->insertBefore($br, $entry); |
|
265 | 265 | |
266 | - $found = true; |
|
267 | - } |
|
266 | + $found = true; |
|
267 | + } |
|
268 | 268 | |
269 | - if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) || |
|
270 | - mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== false || |
|
271 | - mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== false) { |
|
269 | + if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) || |
|
270 | + mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== false || |
|
271 | + mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== false) { |
|
272 | 272 | |
273 | - Debug::log("Handling as a picture", Debug::$LOG_VERBOSE); |
|
273 | + Debug::log("Handling as a picture", Debug::$LOG_VERBOSE); |
|
274 | 274 | |
275 | - $img = $doc->createElement('img'); |
|
276 | - $img->setAttribute("src", $entry->getAttribute("href")); |
|
275 | + $img = $doc->createElement('img'); |
|
276 | + $img->setAttribute("src", $entry->getAttribute("href")); |
|
277 | 277 | |
278 | - $br = $doc->createElement('br'); |
|
279 | - $entry->parentNode->insertBefore($img, $entry); |
|
280 | - $entry->parentNode->insertBefore($br, $entry); |
|
278 | + $br = $doc->createElement('br'); |
|
279 | + $entry->parentNode->insertBefore($img, $entry); |
|
280 | + $entry->parentNode->insertBefore($br, $entry); |
|
281 | 281 | |
282 | - $found = true; |
|
283 | - } |
|
282 | + $found = true; |
|
283 | + } |
|
284 | 284 | |
285 | 285 | // imgur via link rel="image_src" href="..." |
286 | 286 | if (!$found && preg_match("/imgur/", $entry->getAttribute("href"))) { |
@@ -313,109 +313,109 @@ discard block |
||
313 | 313 | } |
314 | 314 | } |
315 | 315 | |
316 | - // wtf is this even |
|
317 | - if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
|
318 | - $img_id = $matches[1]; |
|
316 | + // wtf is this even |
|
317 | + if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
|
318 | + $img_id = $matches[1]; |
|
319 | 319 | |
320 | - Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE); |
|
320 | + Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE); |
|
321 | 321 | |
322 | - $img = $doc->createElement('img'); |
|
323 | - $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); |
|
322 | + $img = $doc->createElement('img'); |
|
323 | + $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); |
|
324 | 324 | |
325 | - $br = $doc->createElement('br'); |
|
326 | - $entry->parentNode->insertBefore($img, $entry); |
|
327 | - $entry->parentNode->insertBefore($br, $entry); |
|
325 | + $br = $doc->createElement('br'); |
|
326 | + $entry->parentNode->insertBefore($img, $entry); |
|
327 | + $entry->parentNode->insertBefore($br, $entry); |
|
328 | 328 | |
329 | - $found = true; |
|
330 | - } |
|
329 | + $found = true; |
|
330 | + } |
|
331 | 331 | |
332 | - // let's try meta properties |
|
333 | - if (!$found) { |
|
334 | - Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE); |
|
332 | + // let's try meta properties |
|
333 | + if (!$found) { |
|
334 | + Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE); |
|
335 | 335 | |
336 | - $content = fetch_file_contents(["url" => $entry->getAttribute("href"), |
|
337 | - "http_accept" => "text/*"]); |
|
336 | + $content = fetch_file_contents(["url" => $entry->getAttribute("href"), |
|
337 | + "http_accept" => "text/*"]); |
|
338 | 338 | |
339 | - if ($content) { |
|
340 | - $cdoc = new DOMDocument(); |
|
339 | + if ($content) { |
|
340 | + $cdoc = new DOMDocument(); |
|
341 | 341 | |
342 | - if (@$cdoc->loadHTML($content)) { |
|
343 | - $cxpath = new DOMXPath($cdoc); |
|
342 | + if (@$cdoc->loadHTML($content)) { |
|
343 | + $cxpath = new DOMXPath($cdoc); |
|
344 | 344 | |
345 | - $og_image = $cxpath->query("//meta[@property='og:image']")->item(0); |
|
346 | - $og_video = $cxpath->query("//meta[@property='og:video']")->item(0); |
|
345 | + $og_image = $cxpath->query("//meta[@property='og:image']")->item(0); |
|
346 | + $og_video = $cxpath->query("//meta[@property='og:video']")->item(0); |
|
347 | 347 | |
348 | - if ($og_video) { |
|
348 | + if ($og_video) { |
|
349 | 349 | |
350 | - $source_stream = $og_video->getAttribute("content"); |
|
350 | + $source_stream = $og_video->getAttribute("content"); |
|
351 | 351 | |
352 | - if ($source_stream) { |
|
352 | + if ($source_stream) { |
|
353 | 353 | |
354 | - if ($og_image) { |
|
355 | - $poster_url = $og_image->getAttribute("content"); |
|
356 | - } else { |
|
357 | - $poster_url = false; |
|
358 | - } |
|
354 | + if ($og_image) { |
|
355 | + $poster_url = $og_image->getAttribute("content"); |
|
356 | + } else { |
|
357 | + $poster_url = false; |
|
358 | + } |
|
359 | 359 | |
360 | - $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
361 | - $found = true; |
|
362 | - } |
|
360 | + $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
|
361 | + $found = true; |
|
362 | + } |
|
363 | 363 | |
364 | - } else if ($og_image) { |
|
364 | + } else if ($og_image) { |
|
365 | 365 | |
366 | - $og_src = $og_image->getAttribute("content"); |
|
366 | + $og_src = $og_image->getAttribute("content"); |
|
367 | 367 | |
368 | - if ($og_src) { |
|
369 | - $img = $doc->createElement('img'); |
|
370 | - $img->setAttribute("src", $og_src); |
|
368 | + if ($og_src) { |
|
369 | + $img = $doc->createElement('img'); |
|
370 | + $img->setAttribute("src", $og_src); |
|
371 | 371 | |
372 | - $br = $doc->createElement('br'); |
|
373 | - $entry->parentNode->insertBefore($img, $entry); |
|
374 | - $entry->parentNode->insertBefore($br, $entry); |
|
372 | + $br = $doc->createElement('br'); |
|
373 | + $entry->parentNode->insertBefore($img, $entry); |
|
374 | + $entry->parentNode->insertBefore($br, $entry); |
|
375 | 375 | |
376 | - $found = true; |
|
377 | - } |
|
378 | - } |
|
379 | - } |
|
380 | - } |
|
381 | - } |
|
376 | + $found = true; |
|
377 | + } |
|
378 | + } |
|
379 | + } |
|
380 | + } |
|
381 | + } |
|
382 | 382 | |
383 | - } |
|
383 | + } |
|
384 | 384 | |
385 | - // remove tiny thumbnails |
|
386 | - if ($entry->hasAttribute("src")) { |
|
387 | - if ($entry->parentNode && $entry->parentNode->parentNode) { |
|
388 | - $entry->parentNode->parentNode->removeChild($entry->parentNode); |
|
389 | - } |
|
390 | - } |
|
391 | - } |
|
385 | + // remove tiny thumbnails |
|
386 | + if ($entry->hasAttribute("src")) { |
|
387 | + if ($entry->parentNode && $entry->parentNode->parentNode) { |
|
388 | + $entry->parentNode->parentNode->removeChild($entry->parentNode); |
|
389 | + } |
|
390 | + } |
|
391 | + } |
|
392 | 392 | |
393 | - return $found; |
|
394 | - } |
|
393 | + return $found; |
|
394 | + } |
|
395 | 395 | |
396 | - public function hook_article_filter($article) { |
|
396 | + public function hook_article_filter($article) { |
|
397 | 397 | |
398 | - if (strpos($article["link"], "reddit.com/r/") !== false) { |
|
399 | - $doc = new DOMDocument(); |
|
400 | - @$doc->loadHTML($article["content"]); |
|
401 | - $xpath = new DOMXPath($doc); |
|
398 | + if (strpos($article["link"], "reddit.com/r/") !== false) { |
|
399 | + $doc = new DOMDocument(); |
|
400 | + @$doc->loadHTML($article["content"]); |
|
401 | + $xpath = new DOMXPath($doc); |
|
402 | 402 | |
403 | - $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
|
403 | + $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
|
404 | 404 | |
405 | - if ($this->host->get($this, "enable_content_dupcheck")) { |
|
405 | + if ($this->host->get($this, "enable_content_dupcheck")) { |
|
406 | 406 | |
407 | - if ($content_link) { |
|
408 | - $content_href = $content_link->getAttribute("href"); |
|
409 | - $entry_guid = $article["guid_hashed"]; |
|
410 | - $owner_uid = $article["owner_uid"]; |
|
407 | + if ($content_link) { |
|
408 | + $content_href = $content_link->getAttribute("href"); |
|
409 | + $entry_guid = $article["guid_hashed"]; |
|
410 | + $owner_uid = $article["owner_uid"]; |
|
411 | 411 | |
412 | - if (DB_TYPE == "pgsql") { |
|
413 | - $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'"; |
|
414 | - } else { |
|
415 | - $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)"; |
|
416 | - } |
|
412 | + if (DB_TYPE == "pgsql") { |
|
413 | + $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'"; |
|
414 | + } else { |
|
415 | + $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)"; |
|
416 | + } |
|
417 | 417 | |
418 | - $sth = $this->pdo->prepare("SELECT COUNT(id) AS cid |
|
418 | + $sth = $this->pdo->prepare("SELECT COUNT(id) AS cid |
|
419 | 419 | FROM ttrss_entries, ttrss_user_entries WHERE |
420 | 420 | ref_id = id AND |
421 | 421 | $interval_qpart AND |
@@ -423,153 +423,153 @@ discard block |
||
423 | 423 | owner_uid = ? AND |
424 | 424 | content LIKE ?"); |
425 | 425 | |
426 | - $sth->execute([$entry_guid, $owner_uid, "%href=\"$content_href\">[link]%"]); |
|
426 | + $sth->execute([$entry_guid, $owner_uid, "%href=\"$content_href\">[link]%"]); |
|
427 | 427 | |
428 | - if ($row = $sth->fetch()) { |
|
429 | - $num_found = $row['cid']; |
|
428 | + if ($row = $sth->fetch()) { |
|
429 | + $num_found = $row['cid']; |
|
430 | 430 | |
431 | - if ($num_found > 0) { |
|
432 | - $article["force_catchup"] = true; |
|
433 | - } |
|
434 | - } |
|
435 | - } |
|
436 | - } |
|
431 | + if ($num_found > 0) { |
|
432 | + $article["force_catchup"] = true; |
|
433 | + } |
|
434 | + } |
|
435 | + } |
|
436 | + } |
|
437 | 437 | |
438 | - $found = $this->inline_stuff($article, $doc, $xpath); |
|
438 | + $found = $this->inline_stuff($article, $doc, $xpath); |
|
439 | 439 | |
440 | - $node = $doc->getElementsByTagName('body')->item(0); |
|
440 | + $node = $doc->getElementsByTagName('body')->item(0); |
|
441 | 441 | |
442 | - if ($node && $found) { |
|
443 | - $article["content"] = $doc->saveHTML($node); |
|
444 | - } else if ($content_link) { |
|
445 | - $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath); |
|
446 | - } |
|
447 | - } |
|
442 | + if ($node && $found) { |
|
443 | + $article["content"] = $doc->saveHTML($node); |
|
444 | + } else if ($content_link) { |
|
445 | + $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath); |
|
446 | + } |
|
447 | + } |
|
448 | 448 | |
449 | - return $article; |
|
450 | - } |
|
449 | + return $article; |
|
450 | + } |
|
451 | 451 | |
452 | - public function api_version() { |
|
453 | - return 2; |
|
454 | - } |
|
452 | + public function api_version() { |
|
453 | + return 2; |
|
454 | + } |
|
455 | 455 | |
456 | - private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) { |
|
456 | + private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) { |
|
457 | 457 | |
458 | - Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE); |
|
458 | + Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE); |
|
459 | 459 | |
460 | - $video = $doc->createElement('video'); |
|
461 | - $video->setAttribute("autoplay", "1"); |
|
462 | - $video->setAttribute("controls", "1"); |
|
463 | - $video->setAttribute("loop", "1"); |
|
460 | + $video = $doc->createElement('video'); |
|
461 | + $video->setAttribute("autoplay", "1"); |
|
462 | + $video->setAttribute("controls", "1"); |
|
463 | + $video->setAttribute("loop", "1"); |
|
464 | 464 | |
465 | - if ($poster_url) { |
|
466 | - $video->setAttribute("poster", $poster_url); |
|
467 | - } |
|
465 | + if ($poster_url) { |
|
466 | + $video->setAttribute("poster", $poster_url); |
|
467 | + } |
|
468 | 468 | |
469 | - $source = $doc->createElement('source'); |
|
470 | - $source->setAttribute("src", $source_stream); |
|
471 | - $source->setAttribute("type", "video/mp4"); |
|
469 | + $source = $doc->createElement('source'); |
|
470 | + $source->setAttribute("src", $source_stream); |
|
471 | + $source->setAttribute("type", "video/mp4"); |
|
472 | 472 | |
473 | - $video->appendChild($source); |
|
473 | + $video->appendChild($source); |
|
474 | 474 | |
475 | - $br = $doc->createElement('br'); |
|
476 | - $entry->parentNode->insertBefore($video, $entry); |
|
477 | - $entry->parentNode->insertBefore($br, $entry); |
|
475 | + $br = $doc->createElement('br'); |
|
476 | + $entry->parentNode->insertBefore($video, $entry); |
|
477 | + $entry->parentNode->insertBefore($br, $entry); |
|
478 | 478 | |
479 | - $img = $doc->createElement('img'); |
|
480 | - $img->setAttribute("src", |
|
481 | - "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); |
|
479 | + $img = $doc->createElement('img'); |
|
480 | + $img->setAttribute("src", |
|
481 | + "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); |
|
482 | 482 | |
483 | - $entry->parentNode->insertBefore($img, $entry); |
|
484 | - } |
|
483 | + $entry->parentNode->insertBefore($img, $entry); |
|
484 | + } |
|
485 | 485 | |
486 | - public function testurl() { |
|
487 | - $url = htmlspecialchars($_REQUEST["url"]); |
|
486 | + public function testurl() { |
|
487 | + $url = htmlspecialchars($_REQUEST["url"]); |
|
488 | 488 | |
489 | - header("Content-type: text/plain"); |
|
489 | + header("Content-type: text/plain"); |
|
490 | 490 | |
491 | - print "URL: $url\n"; |
|
491 | + print "URL: $url\n"; |
|
492 | 492 | |
493 | - $doc = new DOMDocument(); |
|
494 | - @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>"); |
|
495 | - $xpath = new DOMXPath($doc); |
|
493 | + $doc = new DOMDocument(); |
|
494 | + @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>"); |
|
495 | + $xpath = new DOMXPath($doc); |
|
496 | 496 | |
497 | - $found = $this->inline_stuff([], $doc, $xpath); |
|
497 | + $found = $this->inline_stuff([], $doc, $xpath); |
|
498 | 498 | |
499 | - print "Inline result: $found\n"; |
|
499 | + print "Inline result: $found\n"; |
|
500 | 500 | |
501 | - if (!$found) { |
|
502 | - print "\nReadability result:\n"; |
|
501 | + if (!$found) { |
|
502 | + print "\nReadability result:\n"; |
|
503 | 503 | |
504 | - $article = $this->readability([], $url, $doc, $xpath); |
|
504 | + $article = $this->readability([], $url, $doc, $xpath); |
|
505 | 505 | |
506 | - print_r($article); |
|
507 | - } else { |
|
508 | - print "\nResulting HTML:\n"; |
|
506 | + print_r($article); |
|
507 | + } else { |
|
508 | + print "\nResulting HTML:\n"; |
|
509 | 509 | |
510 | - print $doc->saveHTML(); |
|
511 | - } |
|
512 | - } |
|
510 | + print $doc->saveHTML(); |
|
511 | + } |
|
512 | + } |
|
513 | 513 | |
514 | - private function get_header($url, $useragent = SELF_USER_AGENT, $header) { |
|
515 | - $ret = false; |
|
514 | + private function get_header($url, $useragent = SELF_USER_AGENT, $header) { |
|
515 | + $ret = false; |
|
516 | 516 | |
517 | - if (function_exists("curl_init") && !defined("NO_CURL")) { |
|
518 | - $ch = curl_init($url); |
|
519 | - curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
520 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
521 | - curl_setopt($ch, CURLOPT_HEADER, true); |
|
522 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
523 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); |
|
524 | - curl_setopt($ch, CURLOPT_USERAGENT, $useragent); |
|
517 | + if (function_exists("curl_init") && !defined("NO_CURL")) { |
|
518 | + $ch = curl_init($url); |
|
519 | + curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
|
520 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
521 | + curl_setopt($ch, CURLOPT_HEADER, true); |
|
522 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
523 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); |
|
524 | + curl_setopt($ch, CURLOPT_USERAGENT, $useragent); |
|
525 | 525 | |
526 | - @curl_exec($ch); |
|
527 | - $ret = curl_getinfo($ch, $header); |
|
528 | - } |
|
526 | + @curl_exec($ch); |
|
527 | + $ret = curl_getinfo($ch, $header); |
|
528 | + } |
|
529 | 529 | |
530 | - return $ret; |
|
531 | - } |
|
530 | + return $ret; |
|
531 | + } |
|
532 | 532 | |
533 | - private function get_content_type($url, $useragent = SELF_USER_AGENT) { |
|
534 | - return $this->get_header($url, $useragent, CURLINFO_CONTENT_TYPE); |
|
535 | - } |
|
533 | + private function get_content_type($url, $useragent = SELF_USER_AGENT) { |
|
534 | + return $this->get_header($url, $useragent, CURLINFO_CONTENT_TYPE); |
|
535 | + } |
|
536 | 536 | |
537 | - private function get_location($url, $useragent = SELF_USER_AGENT) { |
|
538 | - return $this->get_header($url, $useragent, CURLINFO_EFFECTIVE_URL); |
|
539 | - } |
|
537 | + private function get_location($url, $useragent = SELF_USER_AGENT) { |
|
538 | + return $this->get_header($url, $useragent, CURLINFO_EFFECTIVE_URL); |
|
539 | + } |
|
540 | 540 | |
541 | - /** |
|
542 | - * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
543 | - */ |
|
544 | - private function readability($article, $url, $doc, $xpath, $debug = false) { |
|
541 | + /** |
|
542 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
543 | + */ |
|
544 | + private function readability($article, $url, $doc, $xpath, $debug = false) { |
|
545 | 545 | |
546 | - if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") && |
|
547 | - mb_strlen(strip_tags($article["content"])) <= 150) { |
|
546 | + if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") && |
|
547 | + mb_strlen(strip_tags($article["content"])) <= 150) { |
|
548 | 548 | |
549 | - // do not try to embed posts linking back to other reddit posts |
|
550 | - // readability.php requires PHP 5.6 |
|
551 | - if ($url && strpos($url, "reddit.com") === false && version_compare(PHP_VERSION, '5.6.0', '>=')) { |
|
549 | + // do not try to embed posts linking back to other reddit posts |
|
550 | + // readability.php requires PHP 5.6 |
|
551 | + if ($url && strpos($url, "reddit.com") === false && version_compare(PHP_VERSION, '5.6.0', '>=')) { |
|
552 | 552 | |
553 | - /* link may lead to a huge video file or whatever, we need to check content type before trying to |
|
553 | + /* link may lead to a huge video file or whatever, we need to check content type before trying to |
|
554 | 554 | parse it which p much requires curl */ |
555 | 555 | |
556 | - $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; |
|
557 | - $content_type = $this->get_content_type($url, $useragent_compat); |
|
556 | + $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; |
|
557 | + $content_type = $this->get_content_type($url, $useragent_compat); |
|
558 | 558 | |
559 | - if ($content_type && strpos($content_type, "text/html") !== false) { |
|
559 | + if ($content_type && strpos($content_type, "text/html") !== false) { |
|
560 | 560 | |
561 | - foreach ($this->host->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) { |
|
562 | - $extracted_content = $p->hook_get_full_text($url); |
|
561 | + foreach ($this->host->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) { |
|
562 | + $extracted_content = $p->hook_get_full_text($url); |
|
563 | 563 | |
564 | - if ($extracted_content) { |
|
565 | - $article["content"] = $extracted_content; |
|
566 | - break; |
|
567 | - } |
|
568 | - } |
|
569 | - } |
|
570 | - } |
|
571 | - } |
|
564 | + if ($extracted_content) { |
|
565 | + $article["content"] = $extracted_content; |
|
566 | + break; |
|
567 | + } |
|
568 | + } |
|
569 | + } |
|
570 | + } |
|
571 | + } |
|
572 | 572 | |
573 | - return $article; |
|
574 | - } |
|
573 | + return $article; |
|
574 | + } |
|
575 | 575 | } |