Project

General

Profile

« Previous | Next » 

Revision 1289

Added by kweitzel over 14 years ago

Branch 2.8.1 merged back into Trunk

View differences:

search_modext.php
1
<?php
2

  
3
// $Id$
4

  
5
/*
6

  
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, 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&amp;sstring=".urlencode($str);
32
	} else {
33
		$str = str_replace(' ', '_', $search_url_array[0]);
34
		$link = "?searchresult=2&amp;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.'/ui', $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.'/ui', $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

  
93
	//Filter droplets from the page data
94
	preg_match_all('~\[\[(.*?)\]\]~', $text, $matches);
95
	foreach ($matches[1] as $match) {
96
		$text = str_replace('[['.$match.']]', '', $text);					
97
	}
98

  
99
	// Build the regex-string
100
	if(strpos(strtoupper(PHP_OS), 'WIN')===0) { // windows -> see below
101
		$str1=".!?;";
102
		$str2=".!?;";
103
	} else { // linux & Co.
104
		// 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
105
		$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";
106
		// stop-sign: .!?; + DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
107
		$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";
108
	}
109
	$regex='/(?:^|\b|['.$str1.'])([^'.$str1.']{0,200}?'.$word.'[^'.$str2.']{0,200}(?:['.$str2.']|\b|$))/uis';
110
	if(version_compare(PHP_VERSION, '4.3.3', '>=') &&
111
	   strpos(strtoupper(PHP_OS), 'WIN')!==0
112
	) { // this may crash windows server, so skip if on windows
113
		// jump from match to match, get excerpt, stop if $max_excerpt_num is reached
114
		$last_end = 0; $offset = 0;
115
		while(preg_match('/'.$word.'/uis', $text, $match_array, PREG_OFFSET_CAPTURE, $last_end)) {
116
			$offset = ($match_array[0][1]-206 < $last_end)?$last_end:$match_array[0][1]-206;
117
			if(preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
118
				$last_end = $matches[1][1]+strlen($matches[1][0])-1;
119
				if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $matches[1][0])) // skip excerpts with email-addresses
120
					$excerpt_array[] = trim($matches[1][0]);
121
				if(count($excerpt_array)>=$max_excerpt_num) {
122
					$excerpt_array = array_unique($excerpt_array);
123
					if(count($excerpt_array) >= $max_excerpt_num)
124
						break;
125
				}
126
			} else { // problem: preg_match failed - can't find a start- or stop-sign
127
				$last_end += 201; // jump forward and try again
128
			}
129
		}
130
	} else { // compatible, but may be very slow with large pages
131
		if(preg_match_all($regex, $text, $match_array)) {
132
			foreach($match_array[1] AS $string) {
133
				if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $string))  // skip excerpts with email-addresses
134
					$excerpt_array[] = trim($string);
135
				
136
			}
137
		}
138
	}
139
	return $excerpt_array;
140
}
141

  
142
// makes excerpt_array a string ready to print out
143
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num) {
144
	// excerpts: text before and after a single excerpt, html-tag for markup
145
	$EXCERPT_BEFORE =       '...&nbsp;';
146
	$EXCERPT_AFTER =        '&nbsp;...<br />';
147
	$EXCERPT_MARKUP_START = '<b>';
148
	$EXCERPT_MARKUP_END =   '</b>';
149
	// remove duplicate matches from $excerpt_array, if any.
150
	$excerpt_array = array_unique($excerpt_array);
151
	// use the first $max_excerpt_num excerpts only
152
	if(count($excerpt_array) > $max_excerpt_num) {
153
		$excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
154
	}
155
	// prepare search-string
156
	$string = "(".implode("|", $search_words).")";
157
	// we want markup on search-results page,
158
	// but we need some 'magic' to prevent <br />, <b>... from being highlighted
159
	$excerpt = '';
160
	foreach($excerpt_array as $str) {
161
		$excerpt .= '#,,#'.preg_replace("/($string)/iu","#,,,,#$1#,,,,,#",$str).'#,,,#';
162
	}
163
	$excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&amp;','&lt;','&gt;','&quot;','&#039;','&nbsp;'), $excerpt);
164
	$excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
165
	$excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
166
	// prepare to write out
167
	if(DEFAULT_CHARSET != 'utf-8') {
168
		$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
169
	}
170
	return $excerpt;
171
}
172

  
173
// work out what the link-anchor should be
174
function make_url_target($page_link_target, $text, $search_words) {
175
	// 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
176
	// 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
177
	// 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
178
	// 4. $page_link_target=="" - do nothing
179
	if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_') {
180
		$word = '('.implode('|', $search_words).')';
181
		preg_match('/'.$word.'/ui', $text, $match, PREG_OFFSET_CAPTURE);
182
		if($match && is_array($match[0])) {
183
			$x=$match[0][1]; // position of first match
184
			// is there an anchor nearby?
185
			if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/iU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE)) {
186
				$anchor='';
187
				foreach($match[1] AS $array) {
188
					if($array[1] > $x) {
189
						break;
190
					}
191
					$anchor = $array[0];
192
				}
193
				if($anchor != '') {
194
					$page_link_target = '#'.$anchor;
195
				}
196
			}
197
		}
198
	}
199
	elseif(substr($page_link_target,0,13)=='#!wb_section_') {
200
		$page_link_target = '#'.substr($page_link_target, 2);
201
	}
202
	
203
	// since wb 2.7.1 the section-anchor is configurable - SEC_ANCHOR holds the anchor name
204
	if(substr($page_link_target,0,12)=='#wb_section_') {
205
		if(defined('SEC_ANCHOR') && SEC_ANCHOR!='') {
206
			$sec_id = substr($page_link_target, 12);
207
			$page_link_target = '#'.SEC_ANCHOR.$sec_id;
208
		} else { // section-anchors are disabled
209
			$page_link_target = '';
210
		}
211
	}
212
	
213
	return $page_link_target;
214
}
215

  
216
// wrapper for compatibility with old print_excerpt()
217
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="") {
218
	$mod_vars = array(
219
		'page_link' => $page_link,
220
		'page_link_target' => $page_link_target,
221
		'page_title' => $page_title,
222
		'page_description' => $page_description,
223
		'page_modified_when' => $page_modified_when,
224
		'page_modified_by' => $page_modified_by,
225
		'text' => $text,
226
		'max_excerpt_num' => $max_excerpt_num,
227
		'pic_link' => $pic_link
228
	);
229
	print_excerpt2($mod_vars, $func_vars);
230
}
231

  
232
/* These functions can be used in module-supplied search_funcs
233
 * -----------------------------------------------------------
234
 * print_excerpt2() - the main-function to use in all search_funcs
235
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
236
 * list_files_dirs() - lists all files and dirs below a given directory
237
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
238
 */
239
 
240
// prints the excerpts for one section
241
function print_excerpt2($mod_vars, $func_vars) {
242
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
243
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
244
	global $TEXT;
245
	// check $mod_...vars
246
	if(!isset($mod_page_link))          $mod_page_link = $func_page_link;
247
	if(!isset($mod_page_link_target))   $mod_page_link_target = "";
248
	if(!isset($mod_page_title))         $mod_page_title = $func_page_title;
249
	if(!isset($mod_page_description))   $mod_page_description = $func_page_description;
250
	if(!isset($mod_page_modified_when)) $mod_page_modified_when = $func_page_modified_when;
251
	if(!isset($mod_page_modified_by))   $mod_page_modified_by = $func_page_modified_by;
252
	if(!isset($mod_text))               $mod_text = "";
253
	if(!isset($mod_max_excerpt_num))    $mod_max_excerpt_num = $func_default_max_excerpt;
254
	if(!isset($mod_pic_link))           $mod_pic_link = "";
255
	if(!isset($mod_no_highlight))       $mod_no_highlight = false;
256
	if(!isset($func_enable_flush))      $func_enable_flush = false; // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
257
	if(isset($mod_ext_charset)) $mod_ext_charset = strtolower($mod_ext_charset);
258
	else $mod_ext_charset = '';
259

  
260
	if($mod_text == "") // nothing to do
261
		{ return false; }
262

  
263
	if($mod_no_highlight) // no highlighting
264
		{ $mod_page_link_target = "&amp;nohighlight=1".$mod_page_link_target; }
265
	// clean the text:
266
	$mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#iU', ' ', $mod_text);
267
	$mod_text = preg_replace('#<(br( /)?|dt|/dd|/?(h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#i', '.', $mod_text);
268
	$mod_text = preg_replace('/(\v\s?|\s\s)+/', ' ', $mod_text);
269
	$mod_text = preg_replace('/\s\./', '.', $mod_text);
270
	if($mod_ext_charset!='') { // data from external database may have a different charset
271
		require_once(WB_PATH.'/framework/functions-utf8.php');
272
		switch($mod_ext_charset) {
273
		case 'latin1':
274
		case 'cp1252':
275
			$mod_text = charset_to_utf8($mod_text, 'CP1252');
276
			break;
277
		case 'cp1251':
278
			$mod_text = charset_to_utf8($mod_text, 'CP1251');
279
			break;
280
		case 'latin2':
281
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-2');
282
			break;
283
		case 'hebrew':
284
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-8');
285
			break;
286
		case 'greek':
287
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-7');
288
			break;
289
		case 'latin5':
290
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-9');
291
			break;
292
		case 'latin7':
293
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-13');
294
			break;
295
		case 'utf8':
296
		default:
297
			$mod_text = charset_to_utf8($mod_text, 'UTF-8');
298
		}
299
	} else {
300
	$mod_text = entities_to_umlauts($mod_text, 'UTF-8');
301
	}
302
	$anchor_text = $mod_text; // make an copy containing html-tags
303
	$mod_text = strip_tags($mod_text);
304
	$mod_text = str_replace(array('&gt;','&lt;','&amp;','&quot;','&#039;','&apos;','&nbsp;'), array('>','<','&','"','\'','\'',"\xC2\xA0"), $mod_text);
305
	$mod_text = '.'.trim($mod_text).'.';
306
	// Do a fast scan over $mod_text first. This will speedup things a lot.
307
	if($func_search_match == 'all') {
308
		if(!is_all_matched($mod_text, $func_search_words))
309
			return false;
310
	}
311
	elseif(!is_any_matched($mod_text, $func_search_words)) {
312
		return false;
313
	}
314
	// search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
315
	$anchor =  make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
316

  
317
	// make the link from $mod_page_link, add anchor
318
	$link = "";
319
	$link = page_link($mod_page_link);
320
	if(strpos($mod_page_link, 'http:')===FALSE)
321
		$link .= make_url_searchstring($func_search_match, $func_search_url_array);
322
	$link .= $anchor;
323

  
324
	// now get the excerpt
325
	$excerpt = "";
326
	$excerpt_array = array();
327
	if($mod_max_excerpt_num > 0) {
328
		if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
329
			return false;
330
		}
331
		$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
332
	}
333

  
334
	// handle thumbs - to deactivate this look in the module's search.php: $show_thumb (or maybe in the module's settings-page)
335
	if($mod_pic_link != "") {
336
		$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>';
337
	}
338

  
339
	// print-out the excerpt
340
	$vars = array();
341
	$values = array();
342
	list($date, $time) = get_page_modified($mod_page_modified_when);
343
	list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
344
	$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
345
	$values = array(
346
		$link,
347
		$mod_page_title,
348
		$mod_page_description,
349
		$username,
350
		$displayname,
351
		$date,
352
		$time,
353
		$TEXT['LAST_UPDATED_BY'],
354
		$TEXT['ON'],
355
		$excerpt
356
	);
357
	echo str_replace($vars, $values, $func_results_loop_string);
358
	if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
359
		ob_flush();flush();
360
	}
361
	return true;
362
}
363

  
364
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
365
// returns an array of two arrays ($files[] and $dirs[]).
366
// usage: list($files,$dirs) = list_files_dirs($directory);
367
//        $depth: get subdirs (true/false)
368
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
369
	$dh=opendir($dir);
370
	while(($file = readdir($dh)) !== false) {
371
		if($file{0} == '.' || $file == '..') {
372
			continue;
373
		}
374
		if(is_dir($dir.'/'.$file)) {
375
			if($depth) {
376
				$dirs[] = $dir.'/'.$file;
377
				list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
378
			}
379
		} else {
380
			$files[] = $dir.'/'.$file;
381
		}
382
	}
383
	closedir($dh);
384
	natcasesort($files);
385
	natcasesort($dirs);
386
	return(array($files, $dirs));
387
}
388

  
389
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
390
function clear_filelist($files, $str, $keep=true) {
391
	// options: $keep = true  : remove all non-matching entries
392
	//          $keep = false : remove all matching entries
393
	$c_filelist = array();
394
	if($str == '')
395
		return $files;
396
	foreach($files as $file) {
397
		if($keep) {
398
			if(eregi($str, $file)) {
399
				$c_filelist[] = $file;
400
			}
401
		} else {
402
			if(!eregi($str, $file)) {
403
				$c_filelist[] = $file;
404
			}
405
		}
406
	}
407
	return($c_filelist);
408
}
409

  
410
?>
1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2010, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 4.3.4 and higher
13
 * @version         $Id$
14
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16
 *
17
 */
18

  
19
// make the url-string for highlighting
20
function make_url_searchstring($search_match, $search_url_array) {
21
	$link = "";
22
	if ($search_match != 'exact') {
23
		$str = implode(" ", $search_url_array);
24
		$link = "?searchresult=1&amp;sstring=".urlencode($str);
25
	} else {
26
		$str = str_replace(' ', '_', $search_url_array[0]);
27
		$link = "?searchresult=2&amp;sstring=".urlencode($str);
28
	}
29
	return $link;
30
}
31

  
32
// make date and time for "last modified by... on ..."-string
33
function get_page_modified($page_modified_when) {
34
	global $TEXT;
35
	if($page_modified_when > 0) {
36
		$date = gmdate(DATE_FORMAT, $page_modified_when+TIMEZONE);
37
		$time = gmdate(TIME_FORMAT, $page_modified_when+TIMEZONE);
38
	} else {
39
		$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
40
		$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
41
	}
42
	return array($date, $time);
43
}
44

  
45
// make username and displayname for "last modified by... on ..."-string
46
function get_page_modified_by($page_modified_by, $users) {
47
	global $TEXT;
48
	// check for existing user-id
49
	if(!isset($users[$page_modified_by]))
50
		$page_modified_by = 0;
51
	
52
	$username = $users[$page_modified_by]['username'];
53
	$displayname = $users[$page_modified_by]['display_name'];
54
	return array($username, $displayname);
55
}
56

  
57
// checks if _all_ searchwords matches
58
function is_all_matched($text, $search_words) {
59
	$all_matched = true;
60
	foreach ($search_words AS $word) {
61
		if(!preg_match('/'.$word.'/ui', $text)) {
62
			$all_matched = false;
63
			break;
64
		}
65
	}
66
	return $all_matched;
67
}
68

  
69
// checks if _any_ of the searchwords matches
70
function is_any_matched($text, $search_words) {
71
	$any_matched = false;
72
	$word = '('.implode('|', $search_words).')';
73
	if(preg_match('/'.$word.'/ui', $text)) {
74
		$any_matched = true;
75
	}
76
	return $any_matched;
77
}
78

  
79
// collects the matches from text in excerpt_array
80
function get_excerpts($text, $search_words, $max_excerpt_num) {
81
	$match_array = array();
82
	$excerpt_array = array();
83
	$word = '('.implode('|', $search_words).')';
84

  
85
	//Filter droplets from the page data
86
	preg_match_all('~\[\[(.*?)\]\]~', $text, $matches);
87
	foreach ($matches[1] as $match) {
88
		$text = str_replace('[['.$match.']]', '', $text);					
89
	}
90

  
91
	// Build the regex-string
92
	if(strpos(strtoupper(PHP_OS), 'WIN')===0) { // windows -> see below
93
		$str1=".!?;";
94
		$str2=".!?;";
95
	} else { // linux & Co.
96
		// 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
97
		$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";
98
		// stop-sign: .!?; + DOUBLE EXCLAMATION MARK - INTERROBANG - EXCLAMATION QUESTION MARK - QUESTION EXCLAMATION MARK - DOUBLE QUESTION MARK - HALFWIDTH IDEOGRAPHIC FULL STOP - IDEOGRAPHIC FULL STOP - IDEOGRAPHIC COMMA
99
		$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";
100
	}
101
	$regex='/(?:^|\b|['.$str1.'])([^'.$str1.']{0,200}?'.$word.'[^'.$str2.']{0,200}(?:['.$str2.']|\b|$))/uis';
102
	if(version_compare(PHP_VERSION, '4.3.3', '>=') &&
103
	   strpos(strtoupper(PHP_OS), 'WIN')!==0
104
	) { // this may crash windows server, so skip if on windows
105
		// jump from match to match, get excerpt, stop if $max_excerpt_num is reached
106
		$last_end = 0; $offset = 0;
107
		while(preg_match('/'.$word.'/uis', $text, $match_array, PREG_OFFSET_CAPTURE, $last_end)) {
108
			$offset = ($match_array[0][1]-206 < $last_end)?$last_end:$match_array[0][1]-206;
109
			if(preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
110
				$last_end = $matches[1][1]+strlen($matches[1][0])-1;
111
				if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $matches[1][0])) // skip excerpts with email-addresses
112
					$excerpt_array[] = trim($matches[1][0]);
113
				if(count($excerpt_array)>=$max_excerpt_num) {
114
					$excerpt_array = array_unique($excerpt_array);
115
					if(count($excerpt_array) >= $max_excerpt_num)
116
						break;
117
				}
118
			} else { // problem: preg_match failed - can't find a start- or stop-sign
119
				$last_end += 201; // jump forward and try again
120
			}
121
		}
122
	} else { // compatible, but may be very slow with large pages
123
		if(preg_match_all($regex, $text, $match_array)) {
124
			foreach($match_array[1] AS $string) {
125
				if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $string))  // skip excerpts with email-addresses
126
					$excerpt_array[] = trim($string);
127
				
128
			}
129
		}
130
	}
131
	return $excerpt_array;
132
}
133

  
134
// makes excerpt_array a string ready to print out
135
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num) {
136
	// excerpts: text before and after a single excerpt, html-tag for markup
137
	$EXCERPT_BEFORE =       '...&nbsp;';
138
	$EXCERPT_AFTER =        '&nbsp;...<br />';
139
	$EXCERPT_MARKUP_START = '<b>';
140
	$EXCERPT_MARKUP_END =   '</b>';
141
	// remove duplicate matches from $excerpt_array, if any.
142
	$excerpt_array = array_unique($excerpt_array);
143
	// use the first $max_excerpt_num excerpts only
144
	if(count($excerpt_array) > $max_excerpt_num) {
145
		$excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
146
	}
147
	// prepare search-string
148
	$string = "(".implode("|", $search_words).")";
149
	// we want markup on search-results page,
150
	// but we need some 'magic' to prevent <br />, <b>... from being highlighted
151
	$excerpt = '';
152
	foreach($excerpt_array as $str) {
153
		$excerpt .= '#,,#'.preg_replace("/($string)/iu","#,,,,#$1#,,,,,#",$str).'#,,,#';
154
	}
155
	$excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&amp;','&lt;','&gt;','&quot;','&#039;','&nbsp;'), $excerpt);
156
	$excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
157
	$excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
158
	// prepare to write out
159
	if(DEFAULT_CHARSET != 'utf-8') {
160
		$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
161
	}
162
	return $excerpt;
163
}
164

  
165
// work out what the link-anchor should be
166
function make_url_target($page_link_target, $text, $search_words) {
167
	// 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
168
	// 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
169
	// 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
170
	// 4. $page_link_target=="" - do nothing
171
	if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_') {
172
		$word = '('.implode('|', $search_words).')';
173
		preg_match('/'.$word.'/ui', $text, $match, PREG_OFFSET_CAPTURE);
174
		if($match && is_array($match[0])) {
175
			$x=$match[0][1]; // position of first match
176
			// is there an anchor nearby?
177
			if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/iU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE)) {
178
				$anchor='';
179
				foreach($match[1] AS $array) {
180
					if($array[1] > $x) {
181
						break;
182
					}
183
					$anchor = $array[0];
184
				}
185
				if($anchor != '') {
186
					$page_link_target = '#'.$anchor;
187
				}
188
			}
189
		}
190
	}
191
	elseif(substr($page_link_target,0,13)=='#!wb_section_') {
192
		$page_link_target = '#'.substr($page_link_target, 2);
193
	}
194
	
195
	// since wb 2.7.1 the section-anchor is configurable - SEC_ANCHOR holds the anchor name
196
	if(substr($page_link_target,0,12)=='#wb_section_') {
197
		if(defined('SEC_ANCHOR') && SEC_ANCHOR!='') {
198
			$sec_id = substr($page_link_target, 12);
199
			$page_link_target = '#'.SEC_ANCHOR.$sec_id;
200
		} else { // section-anchors are disabled
201
			$page_link_target = '';
202
		}
203
	}
204
	
205
	return $page_link_target;
206
}
207

  
208
// wrapper for compatibility with old print_excerpt()
209
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="") {
210
	$mod_vars = array(
211
		'page_link' => $page_link,
212
		'page_link_target' => $page_link_target,
213
		'page_title' => $page_title,
214
		'page_description' => $page_description,
215
		'page_modified_when' => $page_modified_when,
216
		'page_modified_by' => $page_modified_by,
217
		'text' => $text,
218
		'max_excerpt_num' => $max_excerpt_num,
219
		'pic_link' => $pic_link
220
	);
221
	print_excerpt2($mod_vars, $func_vars);
222
}
223

  
224
/* These functions can be used in module-supplied search_funcs
225
 * -----------------------------------------------------------
226
 * print_excerpt2() - the main-function to use in all search_funcs
227
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
228
 * list_files_dirs() - lists all files and dirs below a given directory
229
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
230
 */
231
 
232
// prints the excerpts for one section
233
function print_excerpt2($mod_vars, $func_vars) {
234
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
235
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
236
	global $TEXT;
237
	// check $mod_...vars
238
	if(!isset($mod_page_link))          $mod_page_link = $func_page_link;
239
	if(!isset($mod_page_link_target))   $mod_page_link_target = "";
240
	if(!isset($mod_page_title))         $mod_page_title = $func_page_title;
241
	if(!isset($mod_page_description))   $mod_page_description = $func_page_description;
242
	if(!isset($mod_page_modified_when)) $mod_page_modified_when = $func_page_modified_when;
243
	if(!isset($mod_page_modified_by))   $mod_page_modified_by = $func_page_modified_by;
244
	if(!isset($mod_text))               $mod_text = "";
245
	if(!isset($mod_max_excerpt_num))    $mod_max_excerpt_num = $func_default_max_excerpt;
246
	if(!isset($mod_pic_link))           $mod_pic_link = "";
247
	if(!isset($mod_no_highlight))       $mod_no_highlight = false;
248
	if(!isset($func_enable_flush))      $func_enable_flush = false; // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
249
	if(isset($mod_ext_charset)) $mod_ext_charset = strtolower($mod_ext_charset);
250
	else $mod_ext_charset = '';
251

  
252
	if($mod_text == "") // nothing to do
253
		{ return false; }
254

  
255
	if($mod_no_highlight) // no highlighting
256
		{ $mod_page_link_target = "&amp;nohighlight=1".$mod_page_link_target; }
257
	// clean the text:
258
	$mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#iU', ' ', $mod_text);
259
	$mod_text = preg_replace('#<(br( /)?|dt|/dd|/?(h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#i', '.', $mod_text);
260
	$mod_text = preg_replace('/(\v\s?|\s\s)+/', ' ', $mod_text);
261
	$mod_text = preg_replace('/\s\./', '.', $mod_text);
262
	if($mod_ext_charset!='') { // data from external database may have a different charset
263
		require_once(WB_PATH.'/framework/functions-utf8.php');
264
		switch($mod_ext_charset) {
265
		case 'latin1':
266
		case 'cp1252':
267
			$mod_text = charset_to_utf8($mod_text, 'CP1252');
268
			break;
269
		case 'cp1251':
270
			$mod_text = charset_to_utf8($mod_text, 'CP1251');
271
			break;
272
		case 'latin2':
273
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-2');
274
			break;
275
		case 'hebrew':
276
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-8');
277
			break;
278
		case 'greek':
279
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-7');
280
			break;
281
		case 'latin5':
282
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-9');
283
			break;
284
		case 'latin7':
285
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-13');
286
			break;
287
		case 'utf8':
288
		default:
289
			$mod_text = charset_to_utf8($mod_text, 'UTF-8');
290
		}
291
	} else {
292
	$mod_text = entities_to_umlauts($mod_text, 'UTF-8');
293
	}
294
	$anchor_text = $mod_text; // make an copy containing html-tags
295
	$mod_text = strip_tags($mod_text);
296
	$mod_text = str_replace(array('&gt;','&lt;','&amp;','&quot;','&#039;','&apos;','&nbsp;'), array('>','<','&','"','\'','\'',"\xC2\xA0"), $mod_text);
297
	$mod_text = '.'.trim($mod_text).'.';
298
	// Do a fast scan over $mod_text first. This will speedup things a lot.
299
	if($func_search_match == 'all') {
300
		if(!is_all_matched($mod_text, $func_search_words))
301
			return false;
302
	}
303
	elseif(!is_any_matched($mod_text, $func_search_words)) {
304
		return false;
305
	}
306
	// search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
307
	$anchor =  make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
308

  
309
	// make the link from $mod_page_link, add anchor
310
	$link = "";
311
	$link = page_link($mod_page_link);
312
	if(strpos($mod_page_link, 'http:')===FALSE)
313
		$link .= make_url_searchstring($func_search_match, $func_search_url_array);
314
	$link .= $anchor;
315

  
316
	// now get the excerpt
317
	$excerpt = "";
318
	$excerpt_array = array();
319
	if($mod_max_excerpt_num > 0) {
320
		if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
321
			return false;
322
		}
323
		$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
324
	}
325

  
326
	// handle thumbs - to deactivate this look in the module's search.php: $show_thumb (or maybe in the module's settings-page)
327
	if($mod_pic_link != "") {
328
		$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>';
329
	}
330

  
331
	// print-out the excerpt
332
	$vars = array();
333
	$values = array();
334
	list($date, $time) = get_page_modified($mod_page_modified_when);
335
	list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
336
	$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
337
	$values = array(
338
		$link,
339
		$mod_page_title,
340
		$mod_page_description,
341
		$username,
342
		$displayname,
343
		$date,
344
		$time,
345
		$TEXT['LAST_UPDATED_BY'],
346
		$TEXT['ON'],
347
		$excerpt
348
	);
349
	echo str_replace($vars, $values, $func_results_loop_string);
350
	if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
351
		ob_flush();flush();
352
	}
353
	return true;
354
}
355

  
356
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
357
// returns an array of two arrays ($files[] and $dirs[]).
358
// usage: list($files,$dirs) = list_files_dirs($directory);
359
//        $depth: get subdirs (true/false)
360
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
361
	$dh=opendir($dir);
362
	while(($file = readdir($dh)) !== false) {
363
		if($file{0} == '.' || $file == '..') {
364
			continue;
365
		}
366
		if(is_dir($dir.'/'.$file)) {
367
			if($depth) {
368
				$dirs[] = $dir.'/'.$file;
369
				list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
370
			}
371
		} else {
372
			$files[] = $dir.'/'.$file;
373
		}
374
	}
375
	closedir($dh);
376
	natcasesort($files);
377
	natcasesort($dirs);
378
	return(array($files, $dirs));
379
}
380

  
381
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
382
function clear_filelist($files, $str, $keep=true) {
383
	// options: $keep = true  : remove all non-matching entries
384
	//          $keep = false : remove all matching entries
385
	$c_filelist = array();
386
	if($str == '')
387
		return $files;
388
	foreach($files as $file) {
389
		if($keep) {
390
			if(eregi($str, $file)) {
391
				$c_filelist[] = $file;
392
			}
393
		} else {
394
			if(!eregi($str, $file)) {
395
				$c_filelist[] = $file;
396
			}
397
		}
398
	}
399
	return($c_filelist);
400
}
401

  
402
?>
411 403

  

Also available in: Unified diff