1
|
<?php
|
2
|
|
3
|
// $Id: search_modext.php 777 2008-03-27 20:02:43Z thorn $
|
4
|
|
5
|
/*
|
6
|
|
7
|
Website Baker Project <http://www.websitebaker.org/>
|
8
|
Copyright (C) 2004-2008, Ryan Djurovich
|
9
|
|
10
|
Website Baker is free software; you can redistribute it and/or modify
|
11
|
it under the terms of the GNU General Public License as published by
|
12
|
the Free Software Foundation; either version 2 of the License, or
|
13
|
(at your option) any later version.
|
14
|
|
15
|
Website Baker is distributed in the hope that it will be useful,
|
16
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
GNU General Public License for more details.
|
19
|
|
20
|
You should have received a copy of the GNU General Public License
|
21
|
along with Website Baker; if not, write to the Free Software
|
22
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
|
24
|
*/
|
25
|
|
26
|
// make the url-string for highlighting
|
27
|
function make_url_searchstring($search_match, $search_url_array) {
|
28
|
$link = "";
|
29
|
if ($search_match != 'exact') {
|
30
|
$str = implode(" ", $search_url_array);
|
31
|
$link = "?searchresult=1&sstring=".urlencode($str);
|
32
|
} else {
|
33
|
$str = str_replace(' ', '_', $search_url_array[0]);
|
34
|
$link = "?searchresult=2&sstring=".urlencode($str);
|
35
|
}
|
36
|
return $link;
|
37
|
}
|
38
|
|
39
|
// make date and time for "last modified by... on ..."-string
|
40
|
function get_page_modified($page_modified_when) {
|
41
|
global $TEXT;
|
42
|
if($page_modified_when > 0) {
|
43
|
$date = gmdate(DATE_FORMAT, $page_modified_when+TIMEZONE);
|
44
|
$time = gmdate(TIME_FORMAT, $page_modified_when+TIMEZONE);
|
45
|
} else {
|
46
|
$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
|
47
|
$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
|
48
|
}
|
49
|
return array($date, $time);
|
50
|
}
|
51
|
|
52
|
// make username and displayname for "last modified by... on ..."-string
|
53
|
function get_page_modified_by($page_modified_by, $users) {
|
54
|
global $TEXT;
|
55
|
if($page_modified_by > 0) {
|
56
|
$username = $users[$page_modified_by]['username'];
|
57
|
$displayname = $users[$page_modified_by]['display_name'];
|
58
|
} else {
|
59
|
$username = "";
|
60
|
$displayname = $TEXT['UNKNOWN'];
|
61
|
}
|
62
|
return array($username, $displayname);
|
63
|
}
|
64
|
|
65
|
// checks if _all_ searchwords matches
|
66
|
function is_all_matched($text, $search_words) {
|
67
|
$all_matched = true;
|
68
|
foreach ($search_words AS $word) {
|
69
|
if(!preg_match('/'.$word.'/i', $text)) {
|
70
|
$all_matched = false;
|
71
|
break;
|
72
|
}
|
73
|
}
|
74
|
return $all_matched;
|
75
|
}
|
76
|
|
77
|
// checks if _any_ of the searchwords matches
|
78
|
function is_any_matched($text, $search_words) {
|
79
|
$any_matched = false;
|
80
|
$word = '('.implode('|', $search_words).')';
|
81
|
if(preg_match('/'.$word.'/i', $text)) {
|
82
|
$any_matched = true;
|
83
|
}
|
84
|
return $any_matched;
|
85
|
}
|
86
|
|
87
|
// collects the matches from text in excerpt_array
|
88
|
function get_excerpts($text, $search_words, $max_excerpt_num) {
|
89
|
$match_array = array();
|
90
|
$excerpt_array = array();
|
91
|
$word = '('.implode('|', $search_words).')';
|
92
|
// Build the regex-string
|
93
|
// start-sign: .!?; + INVERTED EXCLAMATION MARK - INVERTED QUESTION MARK - DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
|
94
|
$str1=".!?;"."\xC2\xA1"."\xC2\xBF"."\xE2\x80\xBC"."\xE2\x80\xBD"."\xE2\x81\x89"."\xE2\x81\x88"."\xE2\x81\x87"."\xEF\xBD\xA1"."\xE3\x80\x82"."\xE3\x80\x81";
|
95
|
// stop-sign: .!?; + DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
|
96
|
$str2=".!?;"."\xE2\x80\xBC"."\xE2\x80\xBD"."\xE2\x81\x89"."\xE2\x81\x88"."\xE2\x81\x87"."\xEF\xBD\xA1"."\xE3\x80\x82"."\xE3\x80\x81";
|
97
|
$regex='/(?:^|\b|['.$str1.'])([^'.$str1.']{0,200}?'.$word.'[^'.$str2.']{0,200}(?:['.$str2.']|\b|$))/Sisu';
|
98
|
if(version_compare(PHP_VERSION, '4.3.3', '>=')) {
|
99
|
// jump from match to match, get excerpt, stop if $max_excerpt_num is reached
|
100
|
$last_end = 0; $offset = 0;
|
101
|
while(preg_match('/'.$word.'/Sisu', $text, $match_array, PREG_OFFSET_CAPTURE, $last_end)) {
|
102
|
$offset = ($match_array[0][1]-206 < $last_end)?$last_end:$match_array[0][1]-206;
|
103
|
if(preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
104
|
$last_end = $matches[1][1]+strlen($matches[1][0])-1;
|
105
|
if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./S', $matches[1][0])) // skip excerpts with email-addresses
|
106
|
$excerpt_array[] = trim($matches[1][0]);
|
107
|
if(count($excerpt_array)>=$max_excerpt_num) {
|
108
|
$excerpt_array = array_unique($excerpt_array);
|
109
|
if(count($excerpt_array) >= $max_excerpt_num)
|
110
|
break;
|
111
|
}
|
112
|
} else { // problem - preg_match failed: can't find a start- or stop-sign
|
113
|
$last_end += 201; // jump forward and try again
|
114
|
}
|
115
|
}
|
116
|
} else { // compatile, but may be very slow with many large pages
|
117
|
if(preg_match_all($regex, $text, $match_array)) {
|
118
|
foreach($match_array[1] AS $string) {
|
119
|
if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./S', $string)) // skip excerpts with email-addresses
|
120
|
$excerpt_array[] = trim($string);
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
return $excerpt_array;
|
125
|
}
|
126
|
|
127
|
// makes excerpt_array a string ready to print out
|
128
|
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num) {
|
129
|
// excerpts: text before and after a single excerpt, html-tag for markup
|
130
|
$EXCERPT_BEFORE = '... ';
|
131
|
$EXCERPT_AFTER = ' ...<br />';
|
132
|
$EXCERPT_MARKUP_START = '<b>';
|
133
|
$EXCERPT_MARKUP_END = '</b>';
|
134
|
// remove duplicate matches from $excerpt_array, if any.
|
135
|
$excerpt_array = array_unique($excerpt_array);
|
136
|
// use the first $max_excerpt_num excerpts only
|
137
|
if(count($excerpt_array) > $max_excerpt_num) {
|
138
|
$excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
|
139
|
}
|
140
|
// prepare search-string
|
141
|
$string = "(".implode("|", $search_words).")";
|
142
|
// we want markup on search-results page,
|
143
|
// but we need some 'magic' to prevent <br />, <b>... from being highlighted
|
144
|
$excerpt = '';
|
145
|
foreach($excerpt_array as $str) {
|
146
|
$excerpt .= '#,,#'.preg_replace("/($string)/iu","#,,,,#$1#,,,,,#",$str).'#,,,#';
|
147
|
}
|
148
|
$excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&','<','>','"',''',' '), $excerpt);
|
149
|
$excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
|
150
|
$excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
|
151
|
// prepare to write out
|
152
|
if(DEFAULT_CHARSET != 'utf-8') {
|
153
|
$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
|
154
|
}
|
155
|
return $excerpt;
|
156
|
}
|
157
|
|
158
|
// work out what the link-anchor should be
|
159
|
function make_url_target($page_link_target, $text, $search_words) {
|
160
|
// 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
|
161
|
// 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
|
162
|
// 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
|
163
|
// 4. $page_link_target=="" - do nothing
|
164
|
if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_') {
|
165
|
$word = '('.implode('|', $search_words).')';
|
166
|
preg_match('/'.$word.'/i', $text, $match, PREG_OFFSET_CAPTURE);
|
167
|
if($match && is_array($match[0])) {
|
168
|
$x=$match[0][1]; // position of first match
|
169
|
// is there an anchor nearby?
|
170
|
if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/SiU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE)) {
|
171
|
$anchor='';
|
172
|
foreach($match[1] AS $array) {
|
173
|
if($array[1] > $x) {
|
174
|
break;
|
175
|
}
|
176
|
$anchor = $array[0];
|
177
|
}
|
178
|
if($anchor != '') {
|
179
|
$page_link_target = '#'.$anchor;
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
elseif(substr($page_link_target,0,13)=='#!wb_section_') {
|
185
|
$page_link_target = '#'.substr($page_link_target, 2);
|
186
|
}
|
187
|
return $page_link_target;
|
188
|
}
|
189
|
|
190
|
// wrapper for compatibility with old print_excerpt()
|
191
|
function print_excerpt($page_link, $page_link_target, $page_title, $page_description, $page_modified_when, $page_modified_by, $text, $max_excerpt_num, $func_vars, $pic_link="") {
|
192
|
$mod_vars = array(
|
193
|
'page_link' => $page_link,
|
194
|
'page_link_target' => $page_link_target,
|
195
|
'page_title' => $page_title,
|
196
|
'page_description' => $page_description,
|
197
|
'page_modified_when' => $page_modified_when,
|
198
|
'page_modified_by' => $page_modified_by,
|
199
|
'text' => $text,
|
200
|
'max_excerpt_num' => $max_excerpt_num,
|
201
|
'pic_link' => $pic_link
|
202
|
);
|
203
|
print_excerpt2($mod_vars, $func_vars);
|
204
|
}
|
205
|
|
206
|
/* These functions can be used in module-supplied search_funcs
|
207
|
* -----------------------------------------------------------
|
208
|
* print_excerpt2() - the main-function to use in all search_funcs
|
209
|
* print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
|
210
|
* list_files_dirs() - lists all files and dirs below a given directory
|
211
|
* clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
|
212
|
*/
|
213
|
|
214
|
// prints the excerpts for one section
|
215
|
function print_excerpt2($mod_vars, $func_vars) {
|
216
|
extract($func_vars, EXTR_PREFIX_ALL, 'func');
|
217
|
extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
|
218
|
global $TEXT;
|
219
|
// check $mod_...vars
|
220
|
if(!isset($mod_page_link)) $mod_page_link = $func_page_link;
|
221
|
if(!isset($mod_page_link_target)) $mod_page_link_target = "";
|
222
|
if(!isset($mod_page_title)) $mod_page_title = $func_page_title;
|
223
|
if(!isset($mod_page_description)) $mod_page_description = $func_page_description;
|
224
|
if(!isset($mod_page_modified_when)) $mod_page_modified_when = $func_page_modified_when;
|
225
|
if(!isset($mod_page_modified_by)) $mod_page_modified_by = $func_page_modified_by;
|
226
|
if(!isset($mod_text)) $mod_text = "";
|
227
|
if(!isset($mod_max_excerpt_num)) $mod_max_excerpt_num = $func_default_max_excerpt;
|
228
|
if(!isset($mod_pic_link)) $mod_pic_link = "";
|
229
|
if(!isset($mod_no_highlight)) $mod_no_highlight = false;
|
230
|
if(!isset($func_enable_flush)) $func_enable_flush = false; // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
|
231
|
if($mod_text == "") // nothing to do
|
232
|
{ return false; }
|
233
|
if($mod_no_highlight) // no highlighting
|
234
|
{ $mod_page_link_target = "&nohighlight=1".$mod_page_link_target; }
|
235
|
// clean the text:
|
236
|
$mod_text = str_replace(array("\x0D","\x0A"), ' ', $mod_text);
|
237
|
$mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#SiU', ' ', $mod_text);
|
238
|
$mod_text = preg_replace('#<(br( /)?|dt|/dd|/?(h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#Si', '.', $mod_text);
|
239
|
$mod_text = entities_to_umlauts($mod_text, 'UTF-8');
|
240
|
$anchor_text = $mod_text; // make an copy containing html-tags
|
241
|
$mod_text = strip_tags($mod_text);
|
242
|
$mod_text = str_replace(array('>','<','&','"',''',''',' '), array('>','<','&','"','\'','\'',"\xC2\xA0"), $mod_text);
|
243
|
// Do a fast scan over $mod_text first. This will speedup things a lot.
|
244
|
if($func_search_match == 'all') {
|
245
|
if(!is_all_matched($mod_text, $func_search_words))
|
246
|
return false;
|
247
|
}
|
248
|
elseif(!is_any_matched($mod_text, $func_search_words)) {
|
249
|
return false;
|
250
|
}
|
251
|
// search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
|
252
|
$anchor = make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
|
253
|
|
254
|
// make the link from $mod_page_link, add anchor
|
255
|
$link = "";
|
256
|
$link = page_link($mod_page_link);
|
257
|
$link .= make_url_searchstring($func_search_match, $func_search_url_array);
|
258
|
$link .= $anchor;
|
259
|
|
260
|
// now get the excerpt
|
261
|
$excerpt = "";
|
262
|
$excerpt_array = array();
|
263
|
if($mod_max_excerpt_num > 0) {
|
264
|
if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
|
265
|
return false;
|
266
|
}
|
267
|
$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
|
268
|
}
|
269
|
|
270
|
// handle thumbs - to deactivate this look in the module's search.php: $show_thumb (or maybe in the module's settings-page)
|
271
|
if($mod_pic_link != "") {
|
272
|
$excerpt = '<table class="excerpt_thumb" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody><tr><td width="110" valign="top"><a href="'.$link.'"><img src="'.WB_URL.'/'.MEDIA_DIRECTORY.$mod_pic_link.'" alt="" /></a></td><td>'.$excerpt.'</td></tr></tbody></table>';
|
273
|
}
|
274
|
|
275
|
// print-out the excerpt
|
276
|
$vars = array();
|
277
|
$values = array();
|
278
|
list($date, $time) = get_page_modified($mod_page_modified_when);
|
279
|
list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
|
280
|
$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
|
281
|
$values = array(
|
282
|
$link,
|
283
|
$mod_page_title,
|
284
|
$mod_page_description,
|
285
|
$username,
|
286
|
$displayname,
|
287
|
$date,
|
288
|
$time,
|
289
|
$TEXT['LAST_UPDATED_BY'],
|
290
|
$TEXT['ON'],
|
291
|
$excerpt
|
292
|
);
|
293
|
echo str_replace($vars, $values, $func_results_loop_string);
|
294
|
if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
|
295
|
ob_flush();flush();
|
296
|
}
|
297
|
return true;
|
298
|
}
|
299
|
|
300
|
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
|
301
|
// returns an array of two arrays ($files[] and $dirs[]).
|
302
|
// usage: list($files,$dirs) = list_files_dirs($directory);
|
303
|
// $depth: get subdirs (true/false)
|
304
|
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
|
305
|
$dh=opendir($dir);
|
306
|
while(($file = readdir($dh)) !== false) {
|
307
|
if($file{0} == '.' || $file == '..') {
|
308
|
continue;
|
309
|
}
|
310
|
if(is_dir($dir.'/'.$file)) {
|
311
|
if($depth) {
|
312
|
$dirs[] = $dir.'/'.$file;
|
313
|
list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
|
314
|
}
|
315
|
} else {
|
316
|
$files[] = $dir.'/'.$file;
|
317
|
}
|
318
|
}
|
319
|
closedir($dh);
|
320
|
natcasesort($files);
|
321
|
natcasesort($dirs);
|
322
|
return(array($files, $dirs));
|
323
|
}
|
324
|
|
325
|
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
|
326
|
function clear_filelist($files, $str, $keep=true) {
|
327
|
// options: $keep = true : remove all non-matching entries
|
328
|
// $keep = false : remove all matching entries
|
329
|
$c_filelist = array();
|
330
|
if($str == '')
|
331
|
return $files;
|
332
|
foreach($files as $file) {
|
333
|
if($keep) {
|
334
|
if(eregi($str, $file)) {
|
335
|
$c_filelist[] = $file;
|
336
|
}
|
337
|
} else {
|
338
|
if(!eregi($str, $file)) {
|
339
|
$c_filelist[] = $file;
|
340
|
}
|
341
|
}
|
342
|
}
|
343
|
return($c_filelist);
|
344
|
}
|
345
|
|
346
|
?>
|