Project

General

Profile

« Previous | Next » 

Revision 1420

Added by Dietmar almost 14 years ago

YGN Ethical Hacker Group (2.8.2 / 2.9.0)

View differences:

search_modext.php
1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, 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 5.2.2 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
{
22
	$link = "";
23
	if ($search_match != 'exact')
24
    {
25
		$str = implode(" ", $search_url_array);
26
		$link = "?searchresult=1&amp;sstring=".urlencode($str);
27
	} else {
28
		$str = str_replace(' ', '_', $search_url_array[0]);
29
		$link = "?searchresult=2&amp;sstring=".urlencode($str);
30
	}
31
	return $link;
32
}
33

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

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

  
59
	$username = $users[$page_modified_by]['username'];
60
	$displayname = $users[$page_modified_by]['display_name'];
61
	return array($username, $displayname);
62
}
63

  
64
// checks if _all_ searchwords matches
65
function is_all_matched($text, $search_words)
66
{
67
	$all_matched = true;
68
	foreach ($search_words AS $word)
69
    {
70
		if(!preg_match('/'.$word.'/ui', $text))
71
        {
72
			$all_matched = false;
73
			break;
74
		}
75
	}
76
	return $all_matched;
77
}
78

  
79
// checks if _any_ of the searchwords matches
80
function is_any_matched($text, $search_words)
81
{
82
	$any_matched = false;
83
	$word = '('.implode('|', $search_words).')';
84
	if(preg_match('/'.$word.'/ui', $text))
85
    {
86
		$any_matched = true;
87
	}
88
	return $any_matched;
89
}
90

  
91
// collects the matches from text in excerpt_array
92
function get_excerpts($text, $search_words, $max_excerpt_num)
93
{
94
	$match_array = array();
95
	$excerpt_array = array();
96
	$word = '('.implode('|', $search_words).')';
97

  
98
	//Filter droplets from the page data
99
	preg_match_all('~\[\[(.*?)\]\]~', $text, $matches);
100
	foreach ($matches[1] as $match)
101
    {
102
		$text = str_replace('[['.$match.']]', '', $text);					
103
	}
104

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

  
151
			}
152
		}
153
	}
154
	return $excerpt_array;
155
}
156

  
157
// makes excerpt_array a string ready to print out
158
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num)
159
{
160
	// excerpts: text before and after a single excerpt, html-tag for markup
161
	$EXCERPT_BEFORE =       '...&nbsp;';
162
	$EXCERPT_AFTER =        '&nbsp;...<br />';
163
	$EXCERPT_MARKUP_START = '<b>';
164
	$EXCERPT_MARKUP_END =   '</b>';
165
	// remove duplicate matches from $excerpt_array, if any.
166
	$excerpt_array = array_unique($excerpt_array);
167
	// use the first $max_excerpt_num excerpts only
168
	if(count($excerpt_array) > $max_excerpt_num)
169
    {
170
		$excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
171
	}
172
	// prepare search-string
173
	$string = "(".implode("|", $search_words).")";
174
	// we want markup on search-results page,
175
	// but we need some 'magic' to prevent <br />, <b>... from being highlighted
176
	$excerpt = '';
177
	foreach($excerpt_array as $str)
178
    {
179
		$excerpt .= '#,,#'.preg_replace("/($string)/iu","#,,,,#$1#,,,,,#",$str).'#,,,#';
180
	}
181
	$excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&amp;','&lt;','&gt;','&quot;','&#039;','&nbsp;'), $excerpt);
182
	$excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
183
	$excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
184
	// prepare to write out
185
	if(DEFAULT_CHARSET != 'utf-8')
186
    {
187
		$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
188
	}
189
	return $excerpt;
190
}
191

  
192
// work out what the link-anchor should be
193
function make_url_target($page_link_target, $text, $search_words)
194
{
195
	// 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
196
	// 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
197
	// 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
198
	// 4. $page_link_target=="" - do nothing
199
	if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_')
200
    {
201
		$word = '('.implode('|', $search_words).')';
202
		preg_match('/'.$word.'/ui', $text, $match, PREG_OFFSET_CAPTURE);
203
		if($match && is_array($match[0]))
204
        {
205
			$x=$match[0][1]; // position of first match
206
			// is there an anchor nearby?
207
			if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/iU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE))
208
            {
209
				$anchor='';
210
				foreach($match[1] AS $array)
211
                {
212
					if($array[1] > $x)
213
                    {
214
						break;
215
					}
216
					$anchor = $array[0];
217
				}
218
				if($anchor != '')
219
                {
220
					$page_link_target = '#'.$anchor;
221
				}
222
			}
223
		}
224
	} elseif(substr($page_link_target,0,13)=='#!wb_section_') {
225
		$page_link_target = '#'.substr($page_link_target, 2);
226
	}
227
	
228
	// since wb 2.7.1 the section-anchor is configurable - SEC_ANCHOR holds the anchor name
229
	if(substr($page_link_target,0,12)=='#wb_section_')
230
    {
231
		if(defined('SEC_ANCHOR') && SEC_ANCHOR!='')
232
        {
233
			$sec_id = substr($page_link_target, 12);
234
			$page_link_target = '#'.SEC_ANCHOR.$sec_id;
235
		} else { // section-anchors are disabled
236
			$page_link_target = '';
237
		}
238
	}
239
	
240
	return $page_link_target;
241
}
242

  
243
// wrapper for compatibility with old print_excerpt()
244
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="")
245
{
246
	$mod_vars = array(
247
		'page_link' => $page_link,
248
		'page_link_target' => $page_link_target,
249
		'page_title' => $page_title,
250
		'page_description' => $page_description,
251
		'page_modified_when' => $page_modified_when,
252
		'page_modified_by' => $page_modified_by,
253
		'text' => $text,
254
		'max_excerpt_num' => $max_excerpt_num,
255
		'pic_link' => $pic_link
256
	);
257
	print_excerpt2($mod_vars, $func_vars);
258
}
259

  
260
/* These functions can be used in module-supplied search_funcs
261
 * -----------------------------------------------------------
262
 * print_excerpt2() - the main-function to use in all search_funcs
263
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
264
 * list_files_dirs() - lists all files and dirs below a given directory
265
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
266
 */
267

  
268
// prints the excerpts for one section
269
function print_excerpt2($mod_vars, $func_vars)
270
{
271
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
272
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
273
	global $TEXT;
274
	// check $mod_...vars
275
	if(!isset($mod_page_link))          { $mod_page_link = $func_page_link; }
276
	if(!isset($mod_page_link_target))   { $mod_page_link_target = ''; }
277
	if(!isset($mod_page_title))         { $mod_page_title = $func_page_title; }
278
	if(!isset($mod_page_description))   { $mod_page_description = $func_page_description; }
279
	if(!isset($mod_page_modified_when)) { $mod_page_modified_when = $func_page_modified_when; }
280
	if(!isset($mod_page_modified_by))   { $mod_page_modified_by = $func_page_modified_by; }
281
	if(!isset($mod_text))               { $mod_text = ''; }
282
	if(!isset($mod_max_excerpt_num))    { $mod_max_excerpt_num = $func_default_max_excerpt; }
283
	if(!isset($mod_pic_link))           { $mod_pic_link = ''; }
284
	if(!isset($mod_no_highlight))       { $mod_no_highlight = false; }
285
	if(!isset($func_enable_flush))      { $func_enable_flush = false; } // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
286
	if(isset($mod_ext_charset))
287
    {
288
      $mod_ext_charset = strtolower($mod_ext_charset);
289
    } else {
290
      $mod_ext_charset = '';
291
    }
292

  
293
	if($mod_text == "") // nothing to do
294
		{ return false; }
295

  
296
	if($mod_no_highlight) // no highlighting
297
		{ $mod_page_link_target = "&amp;nohighlight=1".$mod_page_link_target; }
298
	// clean the text:
299
	$mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#iU', ' ', $mod_text);
300
	$mod_text = preg_replace('#<(br( /)?|dt|/dd|/?(h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#i', '.', $mod_text);
301
	// $mod_text = preg_replace('/(\v\s?|\s\s)+/', ' ', $mod_text);
302
	$mod_text = preg_replace('/\s\./', '.', $mod_text);
303
	if($mod_ext_charset!='') { // data from external database may have a different charset
304
		require_once(WB_PATH.'/framework/functions-utf8.php');
305
		switch($mod_ext_charset) {
306
		case 'latin1':
307
		case 'cp1252':
308
			$mod_text = charset_to_utf8($mod_text, 'CP1252');
309
			break;
310
		case 'cp1251':
311
			$mod_text = charset_to_utf8($mod_text, 'CP1251');
312
			break;
313
		case 'latin2':
314
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-2');
315
			break;
316
		case 'hebrew':
317
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-8');
318
			break;
319
		case 'greek':
320
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-7');
321
			break;
322
		case 'latin5':
323
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-9');
324
			break;
325
		case 'latin7':
326
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-13');
327
			break;
328
		case 'utf8':
329
		default:
330
			$mod_text = charset_to_utf8($mod_text, 'UTF-8');
331
		}
332
	} else {
333
	$mod_text = entities_to_umlauts($mod_text, 'UTF-8');
334
	}
335
	$anchor_text = $mod_text; // make an copy containing html-tags
336
	$mod_text = strip_tags($mod_text);
337
	$mod_text = str_replace(array('&gt;','&lt;','&amp;','&quot;','&#039;','&apos;','&nbsp;'), array('>','<','&','"','\'','\'',"\xC2\xA0"), $mod_text);
338
	$mod_text = '.'.trim($mod_text).'.';
339
	// Do a fast scan over $mod_text first. This will speedup things a lot.
340
	if($func_search_match == 'all') {
341
		if(!is_all_matched($mod_text, $func_search_words))
342
			return false;
343
	}
344
	elseif(!is_any_matched($mod_text, $func_search_words)) {
345
		return false;
346
	}
347
	// search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
348
	$anchor =  make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
349

  
350
	// make the link from $mod_page_link, add anchor
351
	$link = "";
352
	$link = page_link($mod_page_link);
353
	if(strpos($mod_page_link, 'http:')===FALSE)
354
		$link .= make_url_searchstring($func_search_match, $func_search_url_array);
355
	$link .= $anchor;
356

  
357
	// now get the excerpt
358
	$excerpt = "";
359
	$excerpt_array = array();
360
	if($mod_max_excerpt_num > 0) {
361
		if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
362
			return false;
363
		}
364
		$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
365
	}
366

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

  
372
	// print-out the excerpt
373
	$vars = array();
374
	$values = array();
375
	list($date, $time) = get_page_modified($mod_page_modified_when);
376
	list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
377
	$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
378
	$values = array(
379
		$link,
380
		$mod_page_title,
381
		$mod_page_description,
382
		$username,
383
		$displayname,
384
		$date,
385
		$time,
386
		$TEXT['LAST_UPDATED_BY'],
387
		$TEXT['ON'],
388
		$excerpt
389
	);
390
	echo str_replace($vars, $values, $func_results_loop_string);
391
	if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
392
		ob_flush();flush();
393
	}
394
	return true;
395
}
396

  
397
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
398
// returns an array of two arrays ($files[] and $dirs[]).
399
// usage: list($files,$dirs) = list_files_dirs($directory);
400
//        $depth: get subdirs (true/false)
401
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
402
	$dh=opendir($dir);
403
	while(($file = readdir($dh)) !== false) {
404
		if($file{0} == '.' || $file == '..') {
405
			continue;
406
		}
407
		if(is_dir($dir.'/'.$file)) {
408
			if($depth) {
409
				$dirs[] = $dir.'/'.$file;
410
				list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
411
			}
412
		} else {
413
			$files[] = $dir.'/'.$file;
414
		}
415
	}
416
	closedir($dh);
417
	natcasesort($files);
418
	natcasesort($dirs);
419
	return(array($files, $dirs));
420
}
421

  
422
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
423
/*
424
function clear_filelist($files, $str, $keep=true)
425
{
426
	// options: $keep = true  : remove all non-matching entries
427
	//          $keep = false : remove all matching entries
428
	$c_filelist = array();
429
	if($str == '')
430
		return $files;
431
	foreach($files as $file)
432
    {
433
		if($keep)
434
        {
435
			if(eregi($str, $file))
436
            {
437
				$c_filelist[] = $file;
438
			}
439
		} else {
440
			if(!eregi($str, $file))
441
            {
442
				$c_filelist[] = $file;
443
			}
444
		}
445
	}
446
	return($c_filelist);
447
}
448
*/
1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, 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 5.2.2 and higher
13
 * @version         $Id$
14
 * @filesource		$HeadURL$
15
 * @lastmodified    $Date$
16
 *
17
 */
18

  
19
// Must include code to stop this file being access directly
20
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
21

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

  
37
// make date and time for "last modified by... on ..."-string
38
function get_page_modified($page_modified_when)
39
{
40
	global $TEXT;
41
	if($page_modified_when > 0)
42
    {
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
{
55
	global $TEXT;
56
	// check for existing user-id
57
	if(!isset($users[$page_modified_by]))
58
    {
59
        $page_modified_by = 0;
60
    }
61

  
62
	$username = $users[$page_modified_by]['username'];
63
	$displayname = $users[$page_modified_by]['display_name'];
64
	return array($username, $displayname);
65
}
66

  
67
// checks if _all_ searchwords matches
68
function is_all_matched($text, $search_words)
69
{
70
	$all_matched = true;
71
	foreach ($search_words AS $word)
72
    {
73
		if(!preg_match('/'.$word.'/ui', $text))
74
        {
75
			$all_matched = false;
76
			break;
77
		}
78
	}
79
	return $all_matched;
80
}
81

  
82
// checks if _any_ of the searchwords matches
83
function is_any_matched($text, $search_words)
84
{
85
	$any_matched = false;
86
	$word = '('.implode('|', $search_words).')';
87
	if(preg_match('/'.$word.'/ui', $text))
88
    {
89
		$any_matched = true;
90
	}
91
	return $any_matched;
92
}
93

  
94
// collects the matches from text in excerpt_array
95
function get_excerpts($text, $search_words, $max_excerpt_num)
96
{
97
	$match_array = array();
98
	$excerpt_array = array();
99
	$word = '('.implode('|', $search_words).')';
100

  
101
	//Filter droplets from the page data
102
	preg_match_all('~\[\[(.*?)\]\]~', $text, $matches);
103
	foreach ($matches[1] as $match)
104
    {
105
		$text = str_replace('[['.$match.']]', '', $text);					
106
	}
107

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

  
154
			}
155
		}
156
	}
157
	return $excerpt_array;
158
}
159

  
160
// makes excerpt_array a string ready to print out
161
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num)
162
{
163
	// excerpts: text before and after a single excerpt, html-tag for markup
164
	$EXCERPT_BEFORE =       '...&nbsp;';
165
	$EXCERPT_AFTER =        '&nbsp;...<br />';
166
	$EXCERPT_MARKUP_START = '<b>';
167
	$EXCERPT_MARKUP_END =   '</b>';
168
	// remove duplicate matches from $excerpt_array, if any.
169
	$excerpt_array = array_unique($excerpt_array);
170
	// use the first $max_excerpt_num excerpts only
171
	if(count($excerpt_array) > $max_excerpt_num)
172
    {
173
		$excerpt_array = array_slice($excerpt_array, 0, $max_excerpt_num);
174
	}
175
	// prepare search-string
176
	$string = "(".implode("|", $search_words).")";
177
	// we want markup on search-results page,
178
	// but we need some 'magic' to prevent <br />, <b>... from being highlighted
179
	$excerpt = '';
180
	foreach($excerpt_array as $str)
181
    {
182
		$excerpt .= '#,,#'.preg_replace("/($string)/iu","#,,,,#$1#,,,,,#",$str).'#,,,#';
183
	}
184
	$excerpt = str_replace(array('&','<','>','"','\'',"\xC2\xA0"), array('&amp;','&lt;','&gt;','&quot;','&#039;','&nbsp;'), $excerpt);
185
	$excerpt = str_replace(array('#,,,,#','#,,,,,#'), array($EXCERPT_MARKUP_START,$EXCERPT_MARKUP_END), $excerpt);
186
	$excerpt = str_replace(array('#,,#','#,,,#'), array($EXCERPT_BEFORE,$EXCERPT_AFTER), $excerpt);
187
	// prepare to write out
188
	if(DEFAULT_CHARSET != 'utf-8')
189
    {
190
		$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
191
	}
192
	return $excerpt;
193
}
194

  
195
// work out what the link-anchor should be
196
function make_url_target($page_link_target, $text, $search_words)
197
{
198
	// 1. e.g. $page_link_target=="&monthno=5&year=2007" - module-dependent target. Do nothing.
199
	// 2. $page_link_target=="#!wb_section_..." - the user wants the section-target, so do nothing.
200
	// 3. $page_link_target=="#wb_section_..." - try to find a better target, use the section-target as fallback.
201
	// 4. $page_link_target=="" - do nothing
202
	if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_')
203
    {
204
		$word = '('.implode('|', $search_words).')';
205
		preg_match('/'.$word.'/ui', $text, $match, PREG_OFFSET_CAPTURE);
206
		if($match && is_array($match[0]))
207
        {
208
			$x=$match[0][1]; // position of first match
209
			// is there an anchor nearby?
210
			if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/iU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE))
211
            {
212
				$anchor='';
213
				foreach($match[1] AS $array)
214
                {
215
					if($array[1] > $x)
216
                    {
217
						break;
218
					}
219
					$anchor = $array[0];
220
				}
221
				if($anchor != '')
222
                {
223
					$page_link_target = '#'.$anchor;
224
				}
225
			}
226
		}
227
	} elseif(substr($page_link_target,0,13)=='#!wb_section_') {
228
		$page_link_target = '#'.substr($page_link_target, 2);
229
	}
230
	
231
	// since wb 2.7.1 the section-anchor is configurable - SEC_ANCHOR holds the anchor name
232
	if(substr($page_link_target,0,12)=='#wb_section_')
233
    {
234
		if(defined('SEC_ANCHOR') && SEC_ANCHOR!='')
235
        {
236
			$sec_id = substr($page_link_target, 12);
237
			$page_link_target = '#'.SEC_ANCHOR.$sec_id;
238
		} else { // section-anchors are disabled
239
			$page_link_target = '';
240
		}
241
	}
242
	
243
	return $page_link_target;
244
}
245

  
246
// wrapper for compatibility with old print_excerpt()
247
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="")
248
{
249
	$mod_vars = array(
250
		'page_link' => $page_link,
251
		'page_link_target' => $page_link_target,
252
		'page_title' => $page_title,
253
		'page_description' => $page_description,
254
		'page_modified_when' => $page_modified_when,
255
		'page_modified_by' => $page_modified_by,
256
		'text' => $text,
257
		'max_excerpt_num' => $max_excerpt_num,
258
		'pic_link' => $pic_link
259
	);
260
	print_excerpt2($mod_vars, $func_vars);
261
}
262

  
263
/* These functions can be used in module-supplied search_funcs
264
 * -----------------------------------------------------------
265
 * print_excerpt2() - the main-function to use in all search_funcs
266
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
267
 * list_files_dirs() - lists all files and dirs below a given directory
268
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
269
 */
270

  
271
// prints the excerpts for one section
272
function print_excerpt2($mod_vars, $func_vars)
273
{
274
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
275
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
276
	global $TEXT;
277
	// check $mod_...vars
278
	if(!isset($mod_page_link))          { $mod_page_link = $func_page_link; }
279
	if(!isset($mod_page_link_target))   { $mod_page_link_target = ''; }
280
	if(!isset($mod_page_title))         { $mod_page_title = $func_page_title; }
281
	if(!isset($mod_page_description))   { $mod_page_description = $func_page_description; }
282
	if(!isset($mod_page_modified_when)) { $mod_page_modified_when = $func_page_modified_when; }
283
	if(!isset($mod_page_modified_by))   { $mod_page_modified_by = $func_page_modified_by; }
284
	if(!isset($mod_text))               { $mod_text = ''; }
285
	if(!isset($mod_max_excerpt_num))    { $mod_max_excerpt_num = $func_default_max_excerpt; }
286
	if(!isset($mod_pic_link))           { $mod_pic_link = ''; }
287
	if(!isset($mod_no_highlight))       { $mod_no_highlight = false; }
288
	if(!isset($func_enable_flush))      { $func_enable_flush = false; } // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
289
	if(isset($mod_ext_charset))
290
    {
291
      $mod_ext_charset = strtolower($mod_ext_charset);
292
    } else {
293
      $mod_ext_charset = '';
294
    }
295

  
296
	if($mod_text == "") // nothing to do
297
		{ return false; }
298

  
299
	if($mod_no_highlight) // no highlighting
300
		{ $mod_page_link_target = "&amp;nohighlight=1".$mod_page_link_target; }
301
	// clean the text:
302
	$mod_text = preg_replace('#<(!--.*--|style.*</style|script.*</script)>#iU', ' ', $mod_text);
303
	$mod_text = preg_replace('#<(br( /)?|dt|/dd|/?(h[1-6]|tr|table|p|li|ul|pre|code|div|hr))[^>]*>#i', '.', $mod_text);
304
	// $mod_text = preg_replace('/(\v\s?|\s\s)+/', ' ', $mod_text);
305
	$mod_text = preg_replace('/\s\./', '.', $mod_text);
306
	if($mod_ext_charset!='') { // data from external database may have a different charset
307
		require_once(WB_PATH.'/framework/functions-utf8.php');
308
		switch($mod_ext_charset) {
309
		case 'latin1':
310
		case 'cp1252':
311
			$mod_text = charset_to_utf8($mod_text, 'CP1252');
312
			break;
313
		case 'cp1251':
314
			$mod_text = charset_to_utf8($mod_text, 'CP1251');
315
			break;
316
		case 'latin2':
317
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-2');
318
			break;
319
		case 'hebrew':
320
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-8');
321
			break;
322
		case 'greek':
323
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-7');
324
			break;
325
		case 'latin5':
326
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-9');
327
			break;
328
		case 'latin7':
329
			$mod_text = charset_to_utf8($mod_text, 'ISO-8859-13');
330
			break;
331
		case 'utf8':
332
		default:
333
			$mod_text = charset_to_utf8($mod_text, 'UTF-8');
334
		}
335
	} else {
336
	$mod_text = entities_to_umlauts($mod_text, 'UTF-8');
337
	}
338
	$anchor_text = $mod_text; // make an copy containing html-tags
339
	$mod_text = strip_tags($mod_text);
340
	$mod_text = str_replace(array('&gt;','&lt;','&amp;','&quot;','&#039;','&apos;','&nbsp;'), array('>','<','&','"','\'','\'',"\xC2\xA0"), $mod_text);
341
	$mod_text = '.'.trim($mod_text).'.';
342
	// Do a fast scan over $mod_text first. This will speedup things a lot.
343
	if($func_search_match == 'all') {
344
		if(!is_all_matched($mod_text, $func_search_words))
345
			return false;
346
	}
347
	elseif(!is_any_matched($mod_text, $func_search_words)) {
348
		return false;
349
	}
350
	// search for an better anchor - this have to be done before strip_tags() (may fail if search-string contains <, &, amp, gt, lt, ...)
351
	$anchor =  make_url_target($mod_page_link_target, $anchor_text, $func_search_words);
352

  
353
	// make the link from $mod_page_link, add anchor
354
	$link = "";
355
	$link = page_link($mod_page_link);
356
	if(strpos($mod_page_link, 'http:')===FALSE)
357
		$link .= make_url_searchstring($func_search_match, $func_search_url_array);
358
	$link .= $anchor;
359

  
360
	// now get the excerpt
361
	$excerpt = "";
362
	$excerpt_array = array();
363
	if($mod_max_excerpt_num > 0) {
364
		if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
365
			return false;
366
		}
367
		$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
368
	}
369

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

  
375
	// print-out the excerpt
376
	$vars = array();
377
	$values = array();
378
	list($date, $time) = get_page_modified($mod_page_modified_when);
379
	list($username, $displayname) = get_page_modified_by($mod_page_modified_by, $func_users);
380
	$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
381
	$values = array(
382
		$link,
383
		$mod_page_title,
384
		$mod_page_description,
385
		$username,
386
		$displayname,
387
		$date,
388
		$time,
389
		$TEXT['LAST_UPDATED_BY'],
390
		$TEXT['ON'],
391
		$excerpt
392
	);
393
	echo str_replace($vars, $values, $func_results_loop_string);
394
	if($func_enable_flush) { // ATTN: this will bypass output-filters and may break template-layout or -filters
395
		ob_flush();flush();
396
	}
397
	return true;
398
}
399

  
400
// list all files and dirs in $dir (recursive), omits '.', '..', and hidden files/dirs
401
// returns an array of two arrays ($files[] and $dirs[]).
402
// usage: list($files,$dirs) = list_files_dirs($directory);
403
//        $depth: get subdirs (true/false)
404
function list_files_dirs($dir, $depth=true, $files=array(), $dirs=array()) {
405
	$dh=opendir($dir);
406
	while(($file = readdir($dh)) !== false) {
407
		if($file{0} == '.' || $file == '..') {
408
			continue;
409
		}
410
		if(is_dir($dir.'/'.$file)) {
411
			if($depth) {
412
				$dirs[] = $dir.'/'.$file;
413
				list($files, $dirs) = list_files_dirs($dir.'/'.$file, $depth, $files, $dirs);
414
			}
415
		} else {
416
			$files[] = $dir.'/'.$file;
417
		}
418
	}
419
	closedir($dh);
420
	natcasesort($files);
421
	natcasesort($dirs);
422
	return(array($files, $dirs));
423
}
424

  
425
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
426
/*
427
function clear_filelist($files, $str, $keep=true)
428
{
429
	// options: $keep = true  : remove all non-matching entries
430
	//          $keep = false : remove all matching entries
431
	$c_filelist = array();
432
	if($str == '')
433
		return $files;
434
	foreach($files as $file)
435
    {
436
		if($keep)
437
        {
438
			if(eregi($str, $file))
439
            {
440
				$c_filelist[] = $file;
441
			}
442
		} else {
443
			if(!eregi($str, $file))
444
            {
445
				$c_filelist[] = $file;
446
			}
447
		}
448
	}
449
	return($c_filelist);
450
}
451
*/
449 452
?>

Also available in: Unified diff