Project

General

Profile

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: search_modext.php 1420 2011-01-26 17:43:56Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/search/search_modext.php $
15
 * @lastmodified    $Date: 2011-01-26 18:43:56 +0100 (Wed, 26 Jan 2011) $
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
*/
452
?>
(4-4/4)