Project

General

Profile

1 1262 Luisehahne
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6 1268 Luisehahne
 * @author          WebsiteBaker Project
7 1262 Luisehahne
 * @copyright       2004-2009, Ryan Djurovich
8 1349 Luisehahne
 * @copyright       2009-2011, Website Baker Org. e.V.
9 1262 Luisehahne
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12 1349 Luisehahne
 * @requirements    PHP 5.2.2 and higher
13 1268 Luisehahne
 * @version         $Id$
14
 * @filesource		$HeadURL$
15 1262 Luisehahne
 * @lastmodified    $Date$
16
 *
17
 */
18
19
// make the url-string for highlighting
20 1301 Luisehahne
function make_url_searchstring($search_match, $search_url_array)
21
{
22 1262 Luisehahne
	$link = "";
23 1301 Luisehahne
	if ($search_match != 'exact')
24
    {
25 1262 Luisehahne
		$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 1301 Luisehahne
function get_page_modified($page_modified_when)
36
{
37 1262 Luisehahne
	global $TEXT;
38 1301 Luisehahne
	if($page_modified_when > 0)
39
    {
40 1262 Luisehahne
		$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 1301 Luisehahne
function get_page_modified_by($page_modified_by, $users)
51
{
52 1262 Luisehahne
	global $TEXT;
53
	// check for existing user-id
54
	if(!isset($users[$page_modified_by]))
55 1301 Luisehahne
    {
56
        $page_modified_by = 0;
57
    }
58
59 1262 Luisehahne
	$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 1301 Luisehahne
function is_all_matched($text, $search_words)
66
{
67 1262 Luisehahne
	$all_matched = true;
68 1301 Luisehahne
	foreach ($search_words AS $word)
69
    {
70
		if(!preg_match('/'.$word.'/ui', $text))
71
        {
72 1262 Luisehahne
			$all_matched = false;
73
			break;
74
		}
75
	}
76
	return $all_matched;
77
}
78
79
// checks if _any_ of the searchwords matches
80 1301 Luisehahne
function is_any_matched($text, $search_words)
81
{
82 1262 Luisehahne
	$any_matched = false;
83
	$word = '('.implode('|', $search_words).')';
84 1301 Luisehahne
	if(preg_match('/'.$word.'/ui', $text))
85
    {
86 1262 Luisehahne
		$any_matched = true;
87
	}
88
	return $any_matched;
89
}
90
91
// collects the matches from text in excerpt_array
92 1301 Luisehahne
function get_excerpts($text, $search_words, $max_excerpt_num)
93
{
94 1262 Luisehahne
	$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 1301 Luisehahne
	foreach ($matches[1] as $match)
101
    {
102 1262 Luisehahne
		$text = str_replace('[['.$match.']]', '', $text);
103
	}
104
105
	// Build the regex-string
106 1301 Luisehahne
	if(strpos(strtoupper(PHP_OS), 'WIN')===0)  // windows -> see below
107
    {
108 1262 Luisehahne
		$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 1301 Luisehahne
		while(preg_match('/'.$word.'/uis', $text, $match_array, PREG_OFFSET_CAPTURE, $last_end))
123
        {
124 1262 Luisehahne
			$offset = ($match_array[0][1]-206 < $last_end)?$last_end:$match_array[0][1]-206;
125 1301 Luisehahne
			if(preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE, $offset))
126
            {
127 1262 Luisehahne
				$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 1301 Luisehahne
				{
130
				  $excerpt_array[] = trim($matches[1][0]);
131
                }
132
				if(count($excerpt_array)>=$max_excerpt_num)
133
                {
134 1262 Luisehahne
					$excerpt_array = array_unique($excerpt_array);
135 1301 Luisehahne
					if(count($excerpt_array) >= $max_excerpt_num) { break; }
136 1262 Luisehahne
				}
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 1301 Luisehahne
		if(preg_match_all($regex, $text, $match_array))
143
        {
144
			foreach($match_array[1] AS $string)
145
            {
146 1262 Luisehahne
				if(!preg_match('/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\./', $string))  // skip excerpts with email-addresses
147 1301 Luisehahne
				{
148
				  $excerpt_array[] = trim($string);
149
                }
150
151 1262 Luisehahne
			}
152
		}
153
	}
154
	return $excerpt_array;
155
}
156
157
// makes excerpt_array a string ready to print out
158 1301 Luisehahne
function prepare_excerpts($excerpt_array, $search_words, $max_excerpt_num)
159
{
160 1262 Luisehahne
	// 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 1301 Luisehahne
	if(count($excerpt_array) > $max_excerpt_num)
169
    {
170 1262 Luisehahne
		$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 1301 Luisehahne
	foreach($excerpt_array as $str)
178
    {
179 1262 Luisehahne
		$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 1301 Luisehahne
	if(DEFAULT_CHARSET != 'utf-8')
186
    {
187 1262 Luisehahne
		$excerpt = umlauts_to_entities($excerpt, 'UTF-8');
188
	}
189
	return $excerpt;
190
}
191
192
// work out what the link-anchor should be
193 1301 Luisehahne
function make_url_target($page_link_target, $text, $search_words)
194
{
195 1262 Luisehahne
	// 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 1301 Luisehahne
	if(version_compare(PHP_VERSION, '4.3.3', ">=") && substr($page_link_target,0,12)=='#wb_section_')
200
    {
201 1262 Luisehahne
		$word = '('.implode('|', $search_words).')';
202
		preg_match('/'.$word.'/ui', $text, $match, PREG_OFFSET_CAPTURE);
203 1301 Luisehahne
		if($match && is_array($match[0]))
204
        {
205 1262 Luisehahne
			$x=$match[0][1]; // position of first match
206
			// is there an anchor nearby?
207 1301 Luisehahne
			if(preg_match_all('/<(?:[^>]+id|\s*a[^>]+name)\s*=\s*"(.*)"/iU', substr($text,0,$x), $match, PREG_OFFSET_CAPTURE))
208
            {
209 1262 Luisehahne
				$anchor='';
210 1301 Luisehahne
				foreach($match[1] AS $array)
211
                {
212
					if($array[1] > $x)
213
                    {
214 1262 Luisehahne
						break;
215
					}
216
					$anchor = $array[0];
217
				}
218 1301 Luisehahne
				if($anchor != '')
219
                {
220 1262 Luisehahne
					$page_link_target = '#'.$anchor;
221
				}
222
			}
223
		}
224 1301 Luisehahne
	} elseif(substr($page_link_target,0,13)=='#!wb_section_') {
225 1262 Luisehahne
		$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 1301 Luisehahne
	if(substr($page_link_target,0,12)=='#wb_section_')
230
    {
231
		if(defined('SEC_ANCHOR') && SEC_ANCHOR!='')
232
        {
233 1262 Luisehahne
			$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 1301 Luisehahne
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 1262 Luisehahne
	$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 1301 Luisehahne
268 1262 Luisehahne
// prints the excerpts for one section
269 1301 Luisehahne
function print_excerpt2($mod_vars, $func_vars)
270
{
271 1262 Luisehahne
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
272
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
273
	global $TEXT;
274
	// check $mod_...vars
275 1301 Luisehahne
	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 1262 Luisehahne
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 1302 Luisehahne
	// $mod_text = preg_replace('/(\v\s?|\s\s)+/', ' ', $mod_text);
302 1262 Luisehahne
	$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 1301 Luisehahne
/*
424
function clear_filelist($files, $str, $keep=true)
425
{
426 1262 Luisehahne
	// 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 1301 Luisehahne
	foreach($files as $file)
432
    {
433
		if($keep)
434
        {
435
			if(eregi($str, $file))
436
            {
437 1262 Luisehahne
				$c_filelist[] = $file;
438
			}
439
		} else {
440 1301 Luisehahne
			if(!eregi($str, $file))
441
            {
442 1262 Luisehahne
				$c_filelist[] = $file;
443
			}
444
		}
445
	}
446
	return($c_filelist);
447
}
448 1301 Luisehahne
*/
449
?>