1 <?php
2
3 namespace Icybee\Modules\Search;
4
5 use ICanBoogie\I18n;
6
7 use Brickrouge\Pager;
8
9 if (!defined('PREG_CLASS_SEARCH_EXCLUDE'))
10 {
11 12 13 14 15 16 17 18 19 20 21 22 23
24 define('PREG_CLASS_SEARCH_EXCLUDE',
25 '\x{0}-\x{2f}\x{3a}-\x{40}\x{5b}-\x{60}\x{7b}-\x{bf}\x{d7}\x{f7}\x{2b0}-'.
26 '\x{385}\x{387}\x{3f6}\x{482}-\x{489}\x{559}-\x{55f}\x{589}-\x{5c7}\x{5f3}-'.
27 '\x{61f}\x{640}\x{64b}-\x{65e}\x{66a}-\x{66d}\x{670}\x{6d4}\x{6d6}-\x{6ed}'.
28 '\x{6fd}\x{6fe}\x{700}-\x{70f}\x{711}\x{730}-\x{74a}\x{7a6}-\x{7b0}\x{901}-'.
29 '\x{903}\x{93c}\x{93e}-\x{94d}\x{951}-\x{954}\x{962}-\x{965}\x{970}\x{981}-'.
30 '\x{983}\x{9bc}\x{9be}-\x{9cd}\x{9d7}\x{9e2}\x{9e3}\x{9f2}-\x{a03}\x{a3c}-'.
31 '\x{a4d}\x{a70}\x{a71}\x{a81}-\x{a83}\x{abc}\x{abe}-\x{acd}\x{ae2}\x{ae3}'.
32 '\x{af1}-\x{b03}\x{b3c}\x{b3e}-\x{b57}\x{b70}\x{b82}\x{bbe}-\x{bd7}\x{bf0}-'.
33 '\x{c03}\x{c3e}-\x{c56}\x{c82}\x{c83}\x{cbc}\x{cbe}-\x{cd6}\x{d02}\x{d03}'.
34 '\x{d3e}-\x{d57}\x{d82}\x{d83}\x{dca}-\x{df4}\x{e31}\x{e34}-\x{e3f}\x{e46}-'.
35 '\x{e4f}\x{e5a}\x{e5b}\x{eb1}\x{eb4}-\x{ebc}\x{ec6}-\x{ecd}\x{f01}-\x{f1f}'.
36 '\x{f2a}-\x{f3f}\x{f71}-\x{f87}\x{f90}-\x{fd1}\x{102c}-\x{1039}\x{104a}-'.
37 '\x{104f}\x{1056}-\x{1059}\x{10fb}\x{10fc}\x{135f}-\x{137c}\x{1390}-\x{1399}'.
38 '\x{166d}\x{166e}\x{1680}\x{169b}\x{169c}\x{16eb}-\x{16f0}\x{1712}-\x{1714}'.
39 '\x{1732}-\x{1736}\x{1752}\x{1753}\x{1772}\x{1773}\x{17b4}-\x{17db}\x{17dd}'.
40 '\x{17f0}-\x{180e}\x{1843}\x{18a9}\x{1920}-\x{1945}\x{19b0}-\x{19c0}\x{19c8}'.
41 '\x{19c9}\x{19de}-\x{19ff}\x{1a17}-\x{1a1f}\x{1d2c}-\x{1d61}\x{1d78}\x{1d9b}-'.
42 '\x{1dc3}\x{1fbd}\x{1fbf}-\x{1fc1}\x{1fcd}-\x{1fcf}\x{1fdd}-\x{1fdf}\x{1fed}-'.
43 '\x{1fef}\x{1ffd}-\x{2070}\x{2074}-\x{207e}\x{2080}-\x{2101}\x{2103}-\x{2106}'.
44 '\x{2108}\x{2109}\x{2114}\x{2116}-\x{2118}\x{211e}-\x{2123}\x{2125}\x{2127}'.
45 '\x{2129}\x{212e}\x{2132}\x{213a}\x{213b}\x{2140}-\x{2144}\x{214a}-\x{2b13}'.
46 '\x{2ce5}-\x{2cff}\x{2d6f}\x{2e00}-\x{3005}\x{3007}-\x{303b}\x{303d}-\x{303f}'.
47 '\x{3099}-\x{309e}\x{30a0}\x{30fb}\x{30fd}\x{30fe}\x{3190}-\x{319f}\x{31c0}-'.
48 '\x{31cf}\x{3200}-\x{33ff}\x{4dc0}-\x{4dff}\x{a015}\x{a490}-\x{a716}\x{a802}'.
49 '\x{a806}\x{a80b}\x{a823}-\x{a82b}\x{d800}-\x{f8ff}\x{fb1e}\x{fb29}\x{fd3e}'.
50 '\x{fd3f}\x{fdfc}-\x{fe6b}\x{feff}-\x{ff0f}\x{ff1a}-\x{ff20}\x{ff3b}-\x{ff40}'.
51 '\x{ff5b}-\x{ff65}\x{ff70}\x{ff9e}\x{ff9f}\x{ffe0}-\x{fffd}');
52
53 54 55
56 define('PREG_CLASS_NUMBERS',
57 '\x{30}-\x{39}\x{b2}\x{b3}\x{b9}\x{bc}-\x{be}\x{660}-\x{669}\x{6f0}-\x{6f9}'.
58 '\x{966}-\x{96f}\x{9e6}-\x{9ef}\x{9f4}-\x{9f9}\x{a66}-\x{a6f}\x{ae6}-\x{aef}'.
59 '\x{b66}-\x{b6f}\x{be7}-\x{bf2}\x{c66}-\x{c6f}\x{ce6}-\x{cef}\x{d66}-\x{d6f}'.
60 '\x{e50}-\x{e59}\x{ed0}-\x{ed9}\x{f20}-\x{f33}\x{1040}-\x{1049}\x{1369}-'.
61 '\x{137c}\x{16ee}-\x{16f0}\x{17e0}-\x{17e9}\x{17f0}-\x{17f9}\x{1810}-\x{1819}'.
62 '\x{1946}-\x{194f}\x{2070}\x{2074}-\x{2079}\x{2080}-\x{2089}\x{2153}-\x{2183}'.
63 '\x{2460}-\x{249b}\x{24ea}-\x{24ff}\x{2776}-\x{2793}\x{3007}\x{3021}-\x{3029}'.
64 '\x{3038}-\x{303a}\x{3192}-\x{3195}\x{3220}-\x{3229}\x{3251}-\x{325f}\x{3280}-'.
65 '\x{3289}\x{32b1}-\x{32bf}\x{ff10}-\x{ff19}');
66
67 68 69
70 define('PREG_CLASS_PUNCTUATION',
71 '\x{21}-\x{23}\x{25}-\x{2a}\x{2c}-\x{2f}\x{3a}\x{3b}\x{3f}\x{40}\x{5b}-\x{5d}'.
72 '\x{5f}\x{7b}\x{7d}\x{a1}\x{ab}\x{b7}\x{bb}\x{bf}\x{37e}\x{387}\x{55a}-\x{55f}'.
73 '\x{589}\x{58a}\x{5be}\x{5c0}\x{5c3}\x{5f3}\x{5f4}\x{60c}\x{60d}\x{61b}\x{61f}'.
74 '\x{66a}-\x{66d}\x{6d4}\x{700}-\x{70d}\x{964}\x{965}\x{970}\x{df4}\x{e4f}'.
75 '\x{e5a}\x{e5b}\x{f04}-\x{f12}\x{f3a}-\x{f3d}\x{f85}\x{104a}-\x{104f}\x{10fb}'.
76 '\x{1361}-\x{1368}\x{166d}\x{166e}\x{169b}\x{169c}\x{16eb}-\x{16ed}\x{1735}'.
77 '\x{1736}\x{17d4}-\x{17d6}\x{17d8}-\x{17da}\x{1800}-\x{180a}\x{1944}\x{1945}'.
78 '\x{2010}-\x{2027}\x{2030}-\x{2043}\x{2045}-\x{2051}\x{2053}\x{2054}\x{2057}'.
79 '\x{207d}\x{207e}\x{208d}\x{208e}\x{2329}\x{232a}\x{23b4}-\x{23b6}\x{2768}-'.
80 '\x{2775}\x{27e6}-\x{27eb}\x{2983}-\x{2998}\x{29d8}-\x{29db}\x{29fc}\x{29fd}'.
81 '\x{3001}-\x{3003}\x{3008}-\x{3011}\x{3014}-\x{301f}\x{3030}\x{303d}\x{30a0}'.
82 '\x{30fb}\x{fd3e}\x{fd3f}\x{fe30}-\x{fe52}\x{fe54}-\x{fe61}\x{fe63}\x{fe68}'.
83 '\x{fe6a}\x{fe6b}\x{ff01}-\x{ff03}\x{ff05}-\x{ff0a}\x{ff0c}-\x{ff0f}\x{ff1a}'.
84 '\x{ff1b}\x{ff1f}\x{ff20}\x{ff3b}-\x{ff3d}\x{ff3f}\x{ff5b}\x{ff5d}\x{ff5f}-'.
85 '\x{ff65}');
86
87 88 89 90 91
92 define('PREG_CLASS_CJK', '\x{3041}-\x{30ff}\x{31f0}-\x{31ff}\x{3400}-\x{4db5}'.
93 '\x{4e00}-\x{9fbb}\x{f900}-\x{fad9}');
94 }
95
96 function query_google($search, $position, $limit)
97 {
98 $site = 'atalian.com';
99 $query = 'http://ajax.googleapis.com/ajax/services/search/web?' . http_build_query
100 (
101 array
102 (
103 'q' => $search . ' site:' . $site,
104 'start' => $position * $limit,
105 'v' => '1.0'
106 )
107
108 + array
109 (
110 'gl' => 'fr',
111 'hl' => 'fr',
112 'rsz' => 'large'
113 )
114 );
115
116 $rc = file_get_contents($query);
117
118 $response = json_decode($rc)->responseData;
119
120 $entries = array();
121 $count = $response->cursor->estimatedResultCount;
122
123 if ($response->results)
124 {
125 foreach ($response->results as $result)
126 {
127 $shortUrl = $result->unescapedUrl;
128 $shortUrl = substr($shortUrl, strpos($shortUrl, $site) + strlen($site));
129
130 $entries[] = (object) array
131 (
132 'url' => $shortUrl,
133 'title' => $result->title,
134 'body' => $result->content
135 );
136 }
137 }
138
139 return array($entries, $count);
140 }
141
142 function query_pages($search, $position, $limit)
143 {
144 global $core;
145
146 $model = $core->models['pages'];
147
148 $query_part = 'FROM {self_and_related} INNER JOIN {self}__contents content ON (nid = pageid AND contentid = "body")
149 WHERE is_online = 1 AND siteid = ? AND editor != "view" AND content LIKE ?';
150
151 $query_args = array($core->site_id, '%' . $search . '%');
152
153 $count = $model->query
154 (
155 'SELECT COUNT(nid) ' . $query_part, $query_args
156 )
157 ->fetchColumnAndClose();
158
159 $entries = $model->query
160 (
161 'SELECT node.*, page.*, content.content AS body ' . $query_part . ' ORDER BY created_at DESC LIMIT ' . ($position * $limit) . ', ' . $limit, $query_args
162 )
163 ->fetchAll(\PDO::FETCH_CLASS, 'Icybee\Modules\Pages\Page', array($model));
164
165 return array($entries, $count);
166 }
167
168 function query_contents($constructor, $search, $position, $limit)
169 {
170 global $core;
171
172 if ($constructor == 'contents')
173 {
174 $query_part = 'is_online = 1 AND (siteid = 0 OR siteid = ?)';
175 $query_args = array($core->site_id);
176 }
177 else
178 {
179 $query_part = 'is_online = 1 AND (siteid = 0 OR siteid = ?) AND constructor = ?';
180 $query_args = array($core->site_id, $constructor);
181 }
182
183 $model = $core->models[$constructor];
184
185 $words = explode(' ', $search);
186 $words = array_map(function($str) {
187
188 return '%' . trim($str) . '%';
189
190 }, $words);
191
192 $properties = array('title', 'body');
193 $concat = str_repeat(' AND CONCAT_WS(" ", ' . implode(', ', $properties) . ') LIKE ?', count($words));
194
195 $query_part .= ' AND (' . substr($concat, 4) . ')';
196 $query_args = array_merge($query_args, $words);
197
198 $arq = $model->where($query_part, $query_args);
199
200 $count = $arq->count;
201 $entries = $arq->limit($position * $limit, $limit)->order('date DESC')->all;
202
203 return array($entries, $count);
204 }
205
206
207
208
209
210 function make_set($constructor, $entries, $count, $search, $has_pager=false)
211 {
212 global $core;
213
214 $flat_id = 'module.' . strtr($constructor, '.', '_');
215
216 $rc = '<div class="set">';
217
218 if (empty($_GET['constructor']))
219 {
220 $title = ($constructor == 'google' ? 'Google' : $core->modules->descriptors[$constructor][Module::T_TITLE]);
221 $title = I18n\t(strtr($constructor, '.', '_'), array(), array('scope' => 'module_title', 'default' => $title));
222
223 $rc .= '<h2>' . $title . '</h2>';
224 }
225
226 $rc .= '<p class="count">';
227 $rc .= I18n\t('found', array(':count' => $count, '%search' => $search), array('scope' => array($flat_id, 'search')));
228 $rc .= '</p>';
229
230 if ($entries)
231 {
232 $rc .= '<ol>';
233
234 foreach ($entries as $entry)
235 {
236 $rc .= '<li>';
237 $rc .= '<h3><a href="' . $entry->url . '">' . \ICanBoogie\escape(\ICanBoogie\shorten($entry->title, 80)) . '</a></h3>';
238 $rc .= '<cite>' . \ICanBoogie\shorten($entry->url, 64) . '</cite>';
239
240 $excerpt = search_excerpt($search, html_entity_decode($entry->body, ENT_COMPAT, 'utf-8'));
241
242 $rc .= '<p class="excerpt">' . $excerpt . '</p>';
243 $rc .= '</li>';
244 }
245
246 $rc .= '</ol>';
247
248 if ($count > count($entries))
249 {
250 if ($has_pager)
251 {
252 $rc .= new Pager
253 (
254 'div', array
255 (
256 Pager::T_COUNT => $count,
257 Pager::T_LIMIT => $core->site->metas->get('search.limits.list', 10),
258 Pager::T_POSITION => isset($_GET['page']) ? (int) $_GET['page'] : 0,
259 Pager::T_WITH => 'q,constructor',
260
261 'class' => 'pagination'
262 )
263 );
264 }
265 else
266 {
267 $more_url = '?' . http_build_query(array('q' => $search, 'constructor' => $constructor));
268
269 $rc .= '<p class="more"><a href="' . $more_url . '">';
270 $rc .= I18n\t('more', array(':count' => $count, '%search' => $search), array('scope' => array($flat_id, 'search')));
271 $rc .= '</a></p>';
272 }
273 }
274 }
275
276 $rc .= '</div>';
277
278 return $rc;
279 }
280
281
282
283
284 285 286 287 288 289 290 291 292 293 294 295 296
297 function search_excerpt($keys, $text) {
298
299 $boundary = '(?:(?<=['. PREG_CLASS_SEARCH_EXCLUDE . PREG_CLASS_CJK .'])|(?=['. PREG_CLASS_SEARCH_EXCLUDE . PREG_CLASS_CJK .']))';
300
301
302 preg_match_all('/ ("([^"]+)"|(?!OR)([^" ]+))/', ' '. $keys, $matches);
303 $keys = array_merge($matches[2], $matches[3]);
304
305
306 $text = ' '. strip_tags(str_replace(array('<', '>'), array(' <', '> '), $text)) .' ';
307 array_walk($keys, __NAMESPACE__ . '\_search_excerpt_replace');
308 $workkeys = $keys;
309
310
311
312
313
314 $ranges = array();
315 $included = array();
316 $length = 0;
317 while ($length < 256 && count($workkeys)) {
318 foreach ($workkeys as $k => $key) {
319 if (strlen($key) == 0) {
320 unset($workkeys[$k]);
321 unset($keys[$k]);
322 continue;
323 }
324 if ($length >= 256) {
325 break;
326 }
327
328
329 if (!isset($included[$key])) {
330 $included[$key] = 0;
331 }
332
333
334 if (preg_match('/'. $boundary . $key . $boundary .'/iu', $text, $match, PREG_OFFSET_CAPTURE, $included[$key])) {
335 $p = $match[0][1];
336 if (($q = strpos($text, ' ', max(0, $p - 60))) !== FALSE) {
337 $end = substr($text, $p, 80);
338 if (($s = strrpos($end, ' ')) !== FALSE) {
339 $ranges[$q] = $p + $s;
340 $length += $p + $s - $q;
341 $included[$key] = $p + 1;
342 }
343 else {
344 unset($workkeys[$k]);
345 }
346 }
347 else {
348 unset($workkeys[$k]);
349 }
350 }
351 else {
352 unset($workkeys[$k]);
353 }
354 }
355 }
356
357
358 if (count($ranges) == 0) {
359 return truncate_utf8($text, 256) .' …';
360 }
361
362
363 ksort($ranges);
364
365
366 $newranges = array();
367 foreach ($ranges as $from2 => $to2) {
368 if (!isset($from1)) {
369 $from1 = $from2;
370 $to1 = $to2;
371 continue;
372 }
373 if ($from2 <= $to1) {
374 $to1 = max($to1, $to2);
375 }
376 else {
377 $newranges[$from1] = $to1;
378 $from1 = $from2;
379 $to1 = $to2;
380 }
381 }
382 $newranges[$from1] = $to1;
383
384
385 $out = array();
386 foreach ($newranges as $from => $to) {
387 $out[] = substr($text, $from, $to - $from);
388 }
389 $text = (isset($newranges[0]) ? '' : '… ') . implode(' … ', $out) .' …';
390
391
392 $text = preg_replace('/'. $boundary .'('. implode('|', $keys) .')'. $boundary .'/iu', '<strong>\0</strong>', $text);
393 return $text;
394 }
395
396 397 398
399
400 401 402
403 function _search_excerpt_replace(&$text) {
404 $text = preg_quote($text, '/');
405 }
406
407 function search_forms() {
408 $forms['search_theme_form']= array(
409 'callback' => 'search_box',
410 'callback arguments' => array('search_theme_form'),
411 );
412 $forms['search_block_form']= array(
413 'callback' => 'search_box',
414 'callback arguments' => array('search_block_form'),
415 );
416 return $forms;
417 }
418
419
420
421 422 423 424 425 426 427 428 429 430 431 432 433 434
435 function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
436
437 if (mb_strlen($string) <= $len) {
438 return $string;
439 }
440
441 if ($dots) {
442 $len -= 4;
443 }
444
445 if ($wordsafe) {
446 $string = mb_substr($string, 0, $len + 1);
447 if ($last_space = strrpos($string, ' ')) {
448 $string = substr($string, 0, $last_space);
449 }
450 else {
451 $string = mb_substr($string, 0, $len);
452 }
453 }
454 else {
455 $string = mb_substr($string, 0, $len);
456 }
457
458 if ($dots) {
459 $string .= ' ...';
460 }
461
462 return $string;
463 }