Project

General

Profile

1
<?php
2
/*
3
*
4
*                       About WebsiteBaker
5
*
6
* Website Baker is a PHP-based Content Management System (CMS)
7
* designed with one goal in mind: to enable its users to produce websites
8
* with ease.
9
*
10
*                       LICENSE INFORMATION
11
*
12
* WebsiteBaker is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU General Public License
14
* as published by the Free Software Foundation; either version 2
15
* of the License, or (at your option) any later version.
16
*
17
* WebsiteBaker is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
* See the GNU General Public License for more details.
21
*
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
*
26
*                   WebsiteBaker Extra Information
27
*
28
*
29
*/
30
/**
31
 *
32
 * @category        frontend
33
 * @package         search
34
 * @author          Ryan Djurovich
35
 * @copyright       2004-2009, Ryan Djurovich
36
 * @copyright       2009-2010, Website Baker Org. e.V.
37
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/search/search_modext.php $
38
 * @author          Ryan Djurovich
39
 * @copyright       2004-2009, Ryan Djurovich
40
 *
41
 * @author          WebsiteBaker Project
42
 * @link			http://www.websitebaker2.org/
43
 * @copyright       2009-2010, Website Baker Org. e.V.
44
 * @link			http://start.websitebaker2.org/impressum-datenschutz.php
45
 * @license         http://www.gnu.org/licenses/gpl.html
46
 * @version         $Id: search_modext.php 1262 2010-01-21 08:24:34Z Luisehahne $
47
 * @platform        WebsiteBaker 2.8.x
48
 * @requirements    PHP 4.3.4 and higher
49
 * @lastmodified    $Date: 2010-01-21 09:24:34 +0100 (Thu, 21 Jan 2010) $
50
 *
51
 */
52

    
53
// make the url-string for highlighting
54
function make_url_searchstring($search_match, $search_url_array) {
55
	$link = "";
56
	if ($search_match != 'exact') {
57
		$str = implode(" ", $search_url_array);
58
		$link = "?searchresult=1&amp;sstring=".urlencode($str);
59
	} else {
60
		$str = str_replace(' ', '_', $search_url_array[0]);
61
		$link = "?searchresult=2&amp;sstring=".urlencode($str);
62
	}
63
	return $link;
64
}
65

    
66
// make date and time for "last modified by... on ..."-string
67
function get_page_modified($page_modified_when) {
68
	global $TEXT;
69
	if($page_modified_when > 0) {
70
		$date = gmdate(DATE_FORMAT, $page_modified_when+TIMEZONE);
71
		$time = gmdate(TIME_FORMAT, $page_modified_when+TIMEZONE);
72
	} else {
73
		$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
74
		$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
75
	}
76
	return array($date, $time);
77
}
78

    
79
// make username and displayname for "last modified by... on ..."-string
80
function get_page_modified_by($page_modified_by, $users) {
81
	global $TEXT;
82
	// check for existing user-id
83
	if(!isset($users[$page_modified_by]))
84
		$page_modified_by = 0;
85
	
86
	$username = $users[$page_modified_by]['username'];
87
	$displayname = $users[$page_modified_by]['display_name'];
88
	return array($username, $displayname);
89
}
90

    
91
// checks if _all_ searchwords matches
92
function is_all_matched($text, $search_words) {
93
	$all_matched = true;
94
	foreach ($search_words AS $word) {
95
		if(!preg_match('/'.$word.'/ui', $text)) {
96
			$all_matched = false;
97
			break;
98
		}
99
	}
100
	return $all_matched;
101
}
102

    
103
// checks if _any_ of the searchwords matches
104
function is_any_matched($text, $search_words) {
105
	$any_matched = false;
106
	$word = '('.implode('|', $search_words).')';
107
	if(preg_match('/'.$word.'/ui', $text)) {
108
		$any_matched = true;
109
	}
110
	return $any_matched;
111
}
112

    
113
// collects the matches from text in excerpt_array
114
function get_excerpts($text, $search_words, $max_excerpt_num) {
115
	$match_array = array();
116
	$excerpt_array = array();
117
	$word = '('.implode('|', $search_words).')';
118

    
119
	//Filter droplets from the page data
120
	preg_match_all('~\[\[(.*?)\]\]~', $text, $matches);
121
	foreach ($matches[1] as $match) {
122
		$text = str_replace('[['.$match.']]', '', $text);					
123
	}
124

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

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

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

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

    
258
/* These functions can be used in module-supplied search_funcs
259
 * -----------------------------------------------------------
260
 * print_excerpt2() - the main-function to use in all search_funcs
261
 * print_excerpt() - wrapper for compatibility-reason. Use print_excerpt2() instead.
262
 * list_files_dirs() - lists all files and dirs below a given directory
263
 * clear_filelist() - keeps only wanted or removes unwanted entries in file-list.
264
 */
265
 
266
// prints the excerpts for one section
267
function print_excerpt2($mod_vars, $func_vars) {
268
	extract($func_vars, EXTR_PREFIX_ALL, 'func');
269
	extract($mod_vars, EXTR_PREFIX_ALL, 'mod');
270
	global $TEXT;
271
	// check $mod_...vars
272
	if(!isset($mod_page_link))          $mod_page_link = $func_page_link;
273
	if(!isset($mod_page_link_target))   $mod_page_link_target = "";
274
	if(!isset($mod_page_title))         $mod_page_title = $func_page_title;
275
	if(!isset($mod_page_description))   $mod_page_description = $func_page_description;
276
	if(!isset($mod_page_modified_when)) $mod_page_modified_when = $func_page_modified_when;
277
	if(!isset($mod_page_modified_by))   $mod_page_modified_by = $func_page_modified_by;
278
	if(!isset($mod_text))               $mod_text = "";
279
	if(!isset($mod_max_excerpt_num))    $mod_max_excerpt_num = $func_default_max_excerpt;
280
	if(!isset($mod_pic_link))           $mod_pic_link = "";
281
	if(!isset($mod_no_highlight))       $mod_no_highlight = false;
282
	if(!isset($func_enable_flush))      $func_enable_flush = false; // set this in db: wb_search.cfg_enable_flush [READ THE DOC BEFORE]
283
	if(isset($mod_ext_charset)) $mod_ext_charset = strtolower($mod_ext_charset);
284
	else $mod_ext_charset = '';
285

    
286
	if($mod_text == "") // nothing to do
287
		{ return false; }
288

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

    
343
	// make the link from $mod_page_link, add anchor
344
	$link = "";
345
	$link = page_link($mod_page_link);
346
	if(strpos($mod_page_link, 'http:')===FALSE)
347
		$link .= make_url_searchstring($func_search_match, $func_search_url_array);
348
	$link .= $anchor;
349

    
350
	// now get the excerpt
351
	$excerpt = "";
352
	$excerpt_array = array();
353
	if($mod_max_excerpt_num > 0) {
354
		if(!$excerpt_array = get_excerpts($mod_text, $func_search_words, $mod_max_excerpt_num)) {
355
			return false;
356
		}
357
		$excerpt = prepare_excerpts($excerpt_array, $func_search_words, $mod_max_excerpt_num);
358
	}
359

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

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

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

    
415
// keeps only wanted entries in array $files. $str have to be an eregi()-compatible regex
416
function clear_filelist($files, $str, $keep=true) {
417
	// options: $keep = true  : remove all non-matching entries
418
	//          $keep = false : remove all matching entries
419
	$c_filelist = array();
420
	if($str == '')
421
		return $files;
422
	foreach($files as $file) {
423
		if($keep) {
424
			if(eregi($str, $file)) {
425
				$c_filelist[] = $file;
426
			}
427
		} else {
428
			if(!eregi($str, $file)) {
429
				$c_filelist[] = $file;
430
			}
431
		}
432
	}
433
	return($c_filelist);
434
}
435

    
436
?>
(4-4/4)