Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6
 * @author          yan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id: search.php 1718 2012-08-29 14:25:15Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/search/search.php $
14
 * @lastmodified    $Date: 2012-08-29 16:25:15 +0200 (Wed, 29 Aug 2012) $
15
 *
16
 */
17

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

    
21
// Check if search is enabled
22
if(SHOW_SEARCH != true) {
23
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
24
	return;
25
}
26

    
27
// Include the WB functions file
28
require_once(WB_PATH.'/framework/functions.php');
29

    
30
// Get search settings
31
$table=TABLE_PREFIX.'search';
32
$query = $database->query("SELECT value FROM $table WHERE name = 'header' LIMIT 1");
33
$fetch_header = $query->fetchRow();
34
$query = $database->query("SELECT value FROM $table WHERE name = 'footer' LIMIT 1");
35
$fetch_footer = $query->fetchRow();
36
$query = $database->query("SELECT value FROM $table WHERE name = 'results_header' LIMIT 1");
37
$fetch_results_header = $query->fetchRow();
38
$query = $database->query("SELECT value FROM $table WHERE name = 'results_footer' LIMIT 1");
39
$fetch_results_footer = $query->fetchRow();
40
$query = $database->query("SELECT value FROM $table WHERE name = 'results_loop' LIMIT 1");
41
$fetch_results_loop = $query->fetchRow();
42
$query = $database->query("SELECT value FROM $table WHERE name = 'no_results' LIMIT 1");
43
$fetch_no_results = $query->fetchRow();
44
$query = $database->query("SELECT value FROM $table WHERE name = 'module_order' LIMIT 1");
45
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value']='faqbaker,manual,wysiwyg'; }
46
$search_module_order = $res['value'];
47
$query = $database->query("SELECT value FROM $table WHERE name = 'max_excerpt' LIMIT 1");
48
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '15'; }
49
$search_max_excerpt = (int)($res['value']);
50
if(!is_numeric($search_max_excerpt)) { $search_max_excerpt = 15; }
51
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_show_description' LIMIT 1");
52
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
53
if($res['value'] == 'false') { $cfg_show_description = false; } else { $cfg_show_description = true; }
54
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_description' LIMIT 1");
55
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
56
if($res['value'] == 'false') { $cfg_search_description = false; } else { $cfg_search_description = true; }
57
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_keywords' LIMIT 1");
58
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
59
if($res['value'] == 'false') { $cfg_search_keywords = false; } else { $cfg_search_keywords = true; }
60
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_old_search' LIMIT 1");
61
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
62
if($res['value'] == 'false') { $cfg_enable_old_search = false; } else { $cfg_enable_old_search = true; }
63
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_flush' LIMIT 1");
64
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'false'; }
65
if($res['value'] == 'false') { $cfg_enable_flush = false; } else { $cfg_enable_flush = true; }
66
$query = $database->query("SELECT value FROM $table WHERE name = 'time_limit' LIMIT 1"); // time-limit per module
67
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '0'; }
68
$search_time_limit = (int)($res['value']);
69
if($search_time_limit < 1) $search_time_limit = 0;
70

    
71
// search-module-extension: get helper-functions
72
require_once(WB_PATH.'/search/search_modext.php');
73
// search-module-extension: Get "search.php" for each module, if present
74
// looks in modules/module/ and modules/module_searchext/
75
$search_funcs = array();$search_funcs['__before'] = array();$search_funcs['__after'] = array();
76
$query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
77
if($query->numRows() > 0) {
78
	while($module = $query->fetchRow()) {
79
		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
80
		if(!file_exists($file)) {
81
			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
82
			if(!file_exists($file)) {
83
				$file='';
84
			}
85
		}
86
		if($file!='') {
87
			include_once($file);
88
			if(function_exists($module['directory']."_search")) {
89
				$search_funcs[$module['directory']] = $module['directory']."_search";
90
			}
91
			if(function_exists($module['directory']."_search_before")) {
92
				$search_funcs['__before'][] = $module['directory']."_search_before";
93
			}
94
			if(function_exists($module['directory']."_search_after")) {
95
				$search_funcs['__after'][] = $module['directory']."_search_after";
96
			}
97
		}
98
	}
99
}
100

    
101
// Get list of usernames and display names
102
$query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
103
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
104
if($query->numRows() > 0) {
105
	while($user = $query->fetchRow()) {
106
		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
107
	}
108
}
109

    
110
// Get search language, used for special umlaut handling (DE: ß=ss, ...)
111
$search_lang = '';
112
if(isset($_REQUEST['search_lang'])) {
113
	$search_lang = $_REQUEST['search_lang'];
114
	if(!preg_match('~^[A-Z]{2}$~', $search_lang))
115
		$search_lang = LANGUAGE;
116
} else {
117
	$search_lang = LANGUAGE;
118
}
119

    
120
// Get the path to search into. Normally left blank
121
// ATTN: since wb2.7.1 the path is evaluated as SQL: LIKE "/path%" - which will find "/path.php", "/path/info.php", ...; But not "/de/path.php"
122
// Add a '%' in front of each path to get SQL: LIKE "%/path%"
123
/* possible values:
124
 * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
125
 * - a single path not to search into: "-/help" - search all, exclude /help...
126
 * - a bunch of alternative pathes: "/en/,%/machinery/,/docs/" - alternatives paths, seperated by comma
127
 * - a bunch of paths to exclude: "-/about,%/info,/jp/,/light" - search all, exclude these.
128
 * These different styles can't be mixed.
129
 */
130
// ATTN: in wb2.7.0 "/en/" matched all links with "/en/" somewhere in the link: "/info/en/intro.php", "/en/info.php", ...
131
// since wb2.7.1 "/en/" matches only links _starting_  with "/en/": "/en/intro/info.php"
132
// use "%/en/" (or "%/en/, %/info", ...) to get the old behavior
133
$search_path_SQL = '';
134
$search_path = '';
135
if(isset($_REQUEST['search_path'])) {
136
	$search_path = addslashes(htmlspecialchars(strip_tags($wb->strip_slashes($_REQUEST['search_path'])), ENT_QUOTES));
137
	if(!preg_match('~^%?[-a-zA-Z0-9_,/ ]+$~', $search_path))
138
		$search_path = '';
139
	if($search_path != '') {
140
		$search_path_SQL = 'AND ( ';
141
		$not = '';
142
		$op = 'OR';
143
		if($search_path[0] == '-') {
144
			$not = 'NOT';
145
			$op = 'AND';
146
			$paths = explode(',', substr($search_path, 1) );
147
		} else {
148
			$paths = explode(',',$search_path);
149
		}
150
		$i=0;
151
		foreach($paths as $p) {
152
			if($i++ > 0) {
153
				$search_path_SQL .= ' $op';
154
			}
155
			$search_path_SQL .= " link $not LIKE '".$p."%'";
156
		}
157
		$search_path_SQL .= ' )';
158
	}
159
}
160

    
161
// use page_languages?
162
if(PAGE_LANGUAGES) {
163
	$table = TABLE_PREFIX."pages";
164
	$search_language_SQL_t = "AND $table.`language` = '".LANGUAGE."'";
165
	$search_language_SQL = "AND `language` = '".LANGUAGE."'";
166
} else {
167
	$search_language_SQL_t = '';
168
	$search_language_SQL = '';
169
}
170

    
171
// Get the search type
172
$match = '';
173
if(isset($_REQUEST['match'])) {
174
	if($_REQUEST['match']=='any') $match = 'any';
175
	elseif($_REQUEST['match']=='all') $match = 'all';
176
	elseif($_REQUEST['match']=='exact') $match = 'exact';
177
	else $match = 'all';
178
} else {
179
	$match = 'all';
180
}
181

    
182
// Get search string
183
$search_normal_string = '';
184
$search_entities_string = ''; // for SQL's LIKE
185
$search_display_string = ''; // for displaying
186
$search_url_string = ''; // for $_GET -- ATTN: unquoted! Will become urldecoded later
187
$string = '';
188
if(isset($_REQUEST['string'])) {
189
	if($match!='exact') { // $string will be cleaned below
190
		$string=str_replace(',', '', $_REQUEST['string']);
191
	} else {
192
		$string=$_REQUEST['string'];
193
	}
194
    // redo possible magic quotes
195
    $string = $wb->strip_slashes($string);
196
    $string = preg_replace('/\s+/', ' ', $string);
197
    $string = trim($string);
198
	// remove some bad chars
199
	$string = str_replace ( array('[[',']]'),'', $string);
200
	$string = preg_replace('/(^|\s+)[|.]+(?=\s+|$)/', '', $string);
201
	$search_display_string = htmlspecialchars($string);
202
	// convert string to utf-8
203
	$string = entities_to_umlauts($string, 'UTF-8');
204
	$search_url_string = $string;
205
	$search_entities_string = addslashes(htmlentities($string, ENT_COMPAT, 'UTF-8'));
206
	// mySQL needs four backslashes to match one in LIKE comparisons)
207
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
208
	$string = preg_quote($string);
209
	// quote ' " and /  -we need quoted / for regex
210
	$search_normal_string = str_replace(array('\'','"','/'), array('\\\'','\"','\/'), $string);
211
}
212
// make arrays from the search_..._strings above
213
if($match == 'exact')
214
	$search_url_array[] = $search_url_string;
215
else
216
	$search_url_array = explode(' ', $search_url_string);
217
$search_normal_array = array();
218
$search_entities_array = array();
219
if($match == 'exact') {
220
	$search_normal_array[]=$search_normal_string;
221
	$search_entities_array[]=$search_entities_string;
222
} else {
223
	$exploded_string = explode(' ', $search_normal_string);
224
	// Make sure there is no blank values in the array
225
	foreach($exploded_string AS $each_exploded_string) {
226
		if($each_exploded_string != '') {
227
			$search_normal_array[] = $each_exploded_string;
228
		}
229
	}
230
	$exploded_string = explode(' ', $search_entities_string);
231
	// Make sure there is no blank values in the array
232
	foreach($exploded_string AS $each_exploded_string) {
233
		if($each_exploded_string != '') {
234
			$search_entities_array[] = $each_exploded_string;
235
		}
236
	}
237
}
238
// make an extra copy of search_normal_array for use in regex
239
$search_words = array();
240
require_once(WB_PATH.'/search/search_convert.php');
241
global $search_table_umlauts_local;
242
require_once(WB_PATH.'/search/search_convert_ul.php');
243
global $search_table_ul_umlauts;
244
foreach($search_normal_array AS $str) {
245
	$str = strtr($str, $search_table_umlauts_local);
246
	$str = strtr($str, $search_table_ul_umlauts);
247
	$search_words[] = $str;
248
}
249

    
250
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
251
$all_checked = '';
252
$any_checked = '';
253
$exact_checked = '';
254
if ($match == 'any') {
255
	$any_checked = ' checked="checked"';
256
	$logical_operator = ' OR';
257
} elseif($match == 'all') {
258
	$all_checked = ' checked="checked"';
259
	$logical_operator = ' AND';
260
} else {
261
	$exact_checked = ' checked="checked"';
262
}
263

    
264
// Replace vars in search settings with values
265
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
266
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
267
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
268
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
269
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
270

    
271
// Do extra vars/values replacement
272
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_SEARCH]', '[TEXT_ALL_WORDS]', '[TEXT_ANY_WORDS]', '[TEXT_EXACT_MATCH]', '[TEXT_MATCH]', '[TEXT_MATCHING]', '[ALL_CHECKED]', '[ANY_CHECKED]', '[EXACT_CHECKED]', '[REFERRER_ID]', '[SEARCH_PATH]');
273
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['SEARCH'], $TEXT['ALL_WORDS'], $TEXT['ANY_WORDS'], $TEXT['EXACT_MATCH'], $TEXT['MATCH'], $TEXT['MATCHING'], $all_checked, $any_checked, $exact_checked, REFERRER_ID, $search_path);
274
$search_header = str_replace($vars, $values, ($fetch_header['value']));
275
$vars = array('[TEXT_NO_RESULTS]');
276
$values = array($TEXT['NO_RESULTS']);
277
$search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
278

    
279
/*
280
 * Start of output
281
 */
282

    
283
// Show search header
284
echo $search_header;
285
// Show search results_header
286
echo $search_results_header;
287

    
288
// Work-out if the user has already entered their details or not
289
if($search_normal_string != '') {
290

    
291
	// Get modules
292
	$table = TABLE_PREFIX."sections";
293
	$get_modules = $database->query("SELECT DISTINCT module FROM $table WHERE module != '' ");
294
	$modules = array();
295
	if($get_modules->numRows() > 0) {
296
		while($module = $get_modules->fetchRow()) {
297
			$modules[] = $module['module'];
298
		}
299
	}
300
	// sort module search-order
301
	// get the modules from $search_module_order first ...
302
	$sorted_modules = array();
303
	$m = count($modules);
304
	$search_modules = explode(',', $search_module_order);
305
	foreach($search_modules AS $item) {
306
		$item = trim($item);
307
		for($i=0; $i < $m; $i++) {
308
			if(isset($modules[$i]) && $modules[$i] == $item) {
309
				$sorted_modules[] = $modules[$i];
310
				unset($modules[$i]);
311
				break;
312
			}
313
		}
314
	}
315
	// ... then add the rest
316
	foreach($modules AS $item) {
317
		$sorted_modules[] = $item;
318
	}
319

    
320

    
321
	// Use the module's search-extensions.
322
	// This is somewhat slower than the orginial method.
323
	// call $search_funcs['__before'] first
324
	$search_func_vars = array(
325
		'database' => $database, // database-handle
326
		'page_id' => 0,
327
		'section_id' => 0,
328
		'page_title' => '',
329
		'page_menu_title' => '',
330
		'page_description' => '',
331
		'page_keywords' => '',
332
		'page_link' => '',
333
		'page_modified_when' => 0,
334
		'page_modified_by' => 0,
335
		'users' => $users, // array of known user-id/user-name
336
		'search_words' => $search_words, // array of strings, prepared for regex
337
		'search_match' => $match, // match-type
338
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
339
		'search_entities_array' => $search_entities_array, // entities
340
		'results_loop_string' => $fetch_results_loop['value'],
341
		'default_max_excerpt' => $search_max_excerpt,
342
		'time_limit' => $search_time_limit, // time-limit in secs
343
		'search_path' => $search_path // see docu
344
	);
345
	foreach($search_funcs['__before'] as $func) {
346
		$uf_res = call_user_func($func, $search_func_vars);
347
	}
348
	// now call module-based $search_funcs[]
349
	$seen_pages = array(); // seen pages per module.
350
	$pages_listed = array(); // seen pages.
351
	if($search_max_excerpt!=0) { // skip this search if $search_max_excerpt==0
352
		foreach($sorted_modules AS $module_name) {
353
			$start_time = time();	// get start-time to check time-limit; not very accurate, but ok
354
			$seen_pages[$module_name] = array();
355
			if(!isset($search_funcs[$module_name])) {
356
				continue; // there is no search_func for this module
357
			}
358
			// get each section for $module_name
359
			$table_s = TABLE_PREFIX."sections";
360
			$table_p = TABLE_PREFIX."pages";
361
			$sections_query = $database->query("
362
				SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
363
							 p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
364
							 p.visibility, p.viewing_groups, p.viewing_users
365
				FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
366
				WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL $search_language_SQL
367
				ORDER BY s.page_id, s.position ASC
368
			");
369
			if($sections_query->numRows() > 0) {
370
				while($res = $sections_query->fetchRow()) {
371
					// check if time-limit is exceeded for this module
372
					if($search_time_limit > 0 && (time()-$start_time > $search_time_limit)) {
373
						break;
374
					}
375
					// Only show this section if it is not "out of publication-date"
376
					$now = time();
377
					if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
378
						$now>$res['publ_start'] && $res['publ_end']==0) ) {
379
						continue;
380
					}
381
					$search_func_vars = array(
382
						'database' => $database,
383
						'page_id' => $res['page_id'],
384
						'section_id' => $res['section_id'],
385
						'page_title' => $res['page_title'],
386
						'page_menu_title' => $res['menu_title'],
387
						'page_description' => ($cfg_show_description?$res['description']:""),
388
						'page_keywords' => $res['keywords'],
389
						'page_link' => $res['link'],
390
						'page_modified_when' => $res['modified_when'],
391
						'page_modified_by' => $res['modified_by'],
392
						'users' => $users,
393
						'search_words' => $search_words, // needed for preg_match
394
						'search_match' => $match,
395
						'search_url_array' => $search_url_array, // needed for url-string only
396
						'search_entities_array' => $search_entities_array, // entities
397
						'results_loop_string' => $fetch_results_loop['value'],
398
						'default_max_excerpt' => $search_max_excerpt,
399
						'enable_flush' => $cfg_enable_flush,
400
						'time_limit' => $search_time_limit // time-limit in secs
401
					);
402
					// Only show this page if we are allowed to see it
403
					if($admin->page_is_visible($res) == false) {
404
						if($res['visibility'] == 'registered') { // don't show excerpt
405
							$search_func_vars['default_max_excerpt'] = 0;
406
							$search_func_vars['page_description'] = $TEXT['REGISTERED'];
407
						} else { // private
408
							continue;
409
						}
410
					}
411
					$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
412
					if($uf_res) {
413
						$pages_listed[$res['page_id']] = true;
414
						$seen_pages[$module_name][$res['page_id']] = true;
415
					} else {
416
						$seen_pages[$module_name][$res['page_id']] = true;
417
					}
418
				}
419
			}
420
		}
421
	}
422
	// now call $search_funcs['__after']
423
	$search_func_vars = array(
424
		'database' => $database, // database-handle
425
		'page_id' => 0,
426
		'section_id' => 0,
427
		'page_title' => '',
428
		'page_menu_title' => '',
429
		'page_description' => '',
430
		'page_keywords' => '',
431
		'page_link' => '',
432
		'page_modified_when' => 0,
433
		'page_modified_by' => 0,
434
		'users' => $users, // array of known user-id/user-name
435
		'search_words' => $search_words, // array of strings, prepared for regex
436
		'search_match' => $match, // match-type
437
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
438
		'search_entities_array' => $search_entities_array, // entities
439
		'results_loop_string' => $fetch_results_loop['value'],
440
		'default_max_excerpt' => $search_max_excerpt,
441
		'time_limit' => $search_time_limit, // time-limit in secs
442
		'search_path' => $search_path // see docu
443
	);
444
	foreach($search_funcs['__after'] as $func) {
445
		$uf_res = call_user_func($func, $search_func_vars);
446
	}
447

    
448

    
449
	// Search page details only, such as description, keywords, etc, but only of unseen pages.
450
	$max_excerpt_num = 0; // we don't want excerpt here
451
	$divider = ".";
452
	$table = TABLE_PREFIX."pages";
453
	$query_pages = $database->query("
454
		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
455
		       visibility, viewing_groups, viewing_users
456
		FROM $table
457
		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL $search_language_SQL
458
	");
459
	if($query_pages->numRows() > 0) {
460
		while($page = $query_pages->fetchRow()) {
461
			if (isset($pages_listed[$page['page_id']])) {
462
				continue;
463
			}
464
			$func_vars = array(
465
				'database' => $database,
466
				'page_id' => $page['page_id'],
467
				'page_title' => $page['page_title'],
468
				'page_menu_title' => $page['menu_title'],
469
				'page_description' => ($cfg_show_description?$page['description']:""),
470
				'page_keywords' => $page['keywords'],
471
				'page_link' => $page['link'],
472
				'page_modified_when' => $page['modified_when'],
473
				'page_modified_by' => $page['modified_by'],
474
				'users' => $users,
475
				'search_words' => $search_words, // needed for preg_match_all
476
				'search_match' => $match,
477
				'search_url_array' => $search_url_array, // needed for url-string only
478
				'search_entities_array' => $search_entities_array, // entities
479
				'results_loop_string' => $fetch_results_loop['value'],
480
				'default_max_excerpt' => $max_excerpt_num,
481
				'enable_flush' => $cfg_enable_flush
482
			);
483
			// Only show this page if we are allowed to see it
484
			if($admin->page_is_visible($page) == false) {
485
				if($page['visibility'] != 'registered') {
486
					continue;
487
				} else { // page: registered, user: access denied
488
					$func_vars['page_description'] = $TEXT['REGISTERED'];
489
				}
490
			}
491
			if($admin->page_is_active($page) == false) {
492
				continue;
493
			}
494
			$text = $func_vars['page_title'].$divider
495
				.$func_vars['page_menu_title'].$divider
496
				.($cfg_search_description?$func_vars['page_description']:"").$divider
497
				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
498
			$mod_vars = array(
499
				'page_link' => $func_vars['page_link'],
500
				'page_link_target' => "",
501
				'page_title' => $func_vars['page_title'],
502
				'page_description' => $func_vars['page_description'],
503
				'page_modified_when' => $func_vars['page_modified_when'],
504
				'page_modified_by' => $func_vars['page_modified_by'],
505
				'text' => $text,
506
				'max_excerpt_num' => $func_vars['default_max_excerpt']
507
			);
508
			if(print_excerpt2($mod_vars, $func_vars)) {
509
				$pages_listed[$page['page_id']] = true;
510
			}
511
		}
512
	}
513

    
514
	// Now use the old method for pages not displayed by the new method above
515
	// in case someone has old modules without search.php.
516

    
517
	// Get modules
518
	$table_search = TABLE_PREFIX."search";
519
	$table_sections = TABLE_PREFIX."sections";
520
	$get_modules = $database->query("
521
		SELECT DISTINCT s.value, s.extra
522
		FROM $table_search AS s INNER JOIN $table_sections AS sec
523
			ON s.value = sec.module
524
		WHERE s.name = 'module'
525
	");
526
	$modules = array();
527
	if($get_modules->numRows() > 0) {
528
		while($module = $get_modules->fetchRow()) {
529
			$modules[] = $module; // $modules in an array of arrays
530
		}
531
	}
532
	// sort module search-order
533
	// get the modules from $search_module_order first ...
534
	$sorted_modules = array();
535
	$m = count($modules);
536
	$search_modules = explode(',', $search_module_order);
537
	foreach($search_modules AS $item) {
538
		$item = trim($item);
539
		for($i=0; $i < $m; $i++) {
540
			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
541
				$sorted_modules[] = $modules[$i];
542
				unset($modules[$i]);
543
				break;
544
			}
545
		}
546
	}
547
	// ... then add the rest
548
	foreach($modules AS $item) {
549
		$sorted_modules[] = $item;
550
	}
551

    
552
	if($cfg_enable_old_search) { // this is the old (wb <= 2.6.7) search-function
553
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
554
		foreach($sorted_modules AS $module) {
555
			if(isset($seen_pages[$module['value']]) && count($seen_pages[$module['value']])>0) // skip modules handled by new search-func
556
				continue;
557
			$query_start = '';
558
			$query_body = '';
559
			$query_end = '';
560
			$prepared_query = '';
561
			// Get module name
562
			$module_name = $module['value'];
563
			if(!isset($seen_pages[$module_name])) {
564
				$seen_pages[$module_name]=array();
565
			}
566
			// skip module 'code' - it doesn't make sense to search in a code section
567
			if($module_name=="code")
568
				continue;
569
			// Get fields to use for title, link, etc.
570
			$fields = unserialize($module['extra']);
571
			// Get query start
572
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
573
			if($get_query_start->numRows() > 0) {
574
				// Fetch query start
575
				$fetch_query_start = $get_query_start->fetchRow();
576
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
577
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
578
			}
579
			// Get query end
580
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
581
			if($get_query_end->numRows() > 0) {
582
				// Fetch query end
583
				$fetch_query_end = $get_query_end->fetchRow();
584
				// Set query end
585
				$query_end = ($fetch_query_end['value']);
586
			}
587
			// Get query body
588
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
589
			if($get_query_body->numRows() > 0) {
590
				// Fetch query body
591
				$fetch_query_body = $get_query_body->fetchRow();
592
				// Prepare query body for execution by replacing {STRING} with the correct one
593
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
594
				// Loop through query body for each string, then combine with start and end
595
				$prepared_query = $query_start." ( ( ( ";
596
				$count = 0;
597
				foreach($search_normal_array AS $string) {
598
					if($count != 0) {
599
						$prepared_query .= " ) ".$logical_operator." ( ";
600
					}
601
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
602
					$count = $count+1;
603
				}
604
				$count=0;
605
				$prepared_query .= ' ) ) OR ( ( ';
606
				foreach($search_entities_array AS $string) {
607
					if($count != 0) {
608
						$prepared_query .= " ) ".$logical_operator." ( ";
609
					}
610
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
611
					$count = $count+1;
612
				}
613
				$prepared_query .= " ) ) ) ".$query_end;
614
				// Execute query
615
				$page_query = $database->query($prepared_query." ".$search_path_SQL." ".$search_language_SQL_t);
616
				if(!$page_query) continue; // on error, skip the rest of the current loop iteration
617
				// Loop through queried items
618
				if($page_query->numRows() > 0) {
619
					while($page = $page_query->fetchRow()) {
620
						// Only show this page if it hasn't already been listed
621
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
622
							continue;
623
						}
624

    
625
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
626
						$p_table = TABLE_PREFIX."pages";
627
						$viewquery = $database->query("
628
							SELECT visibility, viewing_groups, viewing_users
629
							FROM $p_table
630
							WHERE page_id='{$page['page_id']}'
631
						");
632
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
633
						if($viewquery->numRows() > 0) {
634
							if($res = $viewquery->fetchRow()) {
635
								$visibility = $res['visibility'];
636
								$viewing_groups = $res['viewing_groups'];
637
								$viewing_users = $res['viewing_users'];
638
								if($visibility == 'deleted' || $visibility == 'none') {
639
									continue;
640
								}
641
								if($visibility == 'private') {
642
									if($admin->page_is_visible(array(
643
										'page_id'=>$page[$fields['page_id']],
644
										'visibility' =>$visibility,
645
										'viewing_groups'=>$viewing_groups,
646
										'viewing_users'=>$viewing_users
647
									)) == false) {
648
										continue;
649
									}
650
								}
651
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
652
									continue;
653
								}
654
							}
655
						}
656

    
657
						// Get page link
658
						$link = page_link($page['link']);
659
						// Add search string for highlighting
660
						if ($match!='exact') {
661
							$sstring = implode(" ", $search_normal_array);
662
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
663
						} else {
664
							$sstring = str_replace(" ", "_",$search_normal_array[0]);
665
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
666
						}
667
						// Set vars to be replaced by values
668
						if(!isset($page['description'])) { $page['description'] = ""; }
669
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
670
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
671
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
672
						if($page['modified_when'] > 0) {
673
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
674
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
675
						} else {
676
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
677
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
678
						}
679
						$excerpt="";
680
						if($cfg_show_description == 0) {
681
							$page['description'] = "";
682
						}
683
						$values = array($link, $page['page_title'], $page['description'], $users[$page['modified_by']]['username'], $users[$page['modified_by']]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']), $excerpt);
684
						// Show loop code with vars replaced by values
685
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
686
						// Say that this page has been listed
687
						$seen_pages[$module_name][$page['page_id']] = true;
688
						$pages_listed[$page['page_id']] = true;
689
					}
690
				}
691
			}
692
		}
693
	}
694

    
695
	// Say no items found if we should
696
	if(count($pages_listed) == 0) {
697
		echo $search_no_results;
698
	}
699
} else {
700
	echo $search_no_results;
701
}
702

    
703
// Show search results_footer
704
echo $search_results_footer;
705
// Show search footer
706
echo $search_footer;
(2-2/5)