Project

General

Profile

1 1420 Luisehahne
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         search
6 1718 Luisehahne
 * @author          yan Djurovich, WebsiteBaker Project
7
 * @copyright       2009-2012, WebsiteBaker Org. e.V.
8 1420 Luisehahne
 * @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$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15
 *
16
 */
17
18 1832 Luisehahne
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_PATH')) {
21
	require_once(dirname(dirname(__FILE__)).'/framework/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24
/* -------------------------------------------------------- */
25 1420 Luisehahne
26
// Check if search is enabled
27
if(SHOW_SEARCH != true) {
28
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
29
	return;
30
}
31
32
// Include the WB functions file
33
require_once(WB_PATH.'/framework/functions.php');
34
35
// Get search settings
36
$table=TABLE_PREFIX.'search';
37
$query = $database->query("SELECT value FROM $table WHERE name = 'header' LIMIT 1");
38
$fetch_header = $query->fetchRow();
39
$query = $database->query("SELECT value FROM $table WHERE name = 'footer' LIMIT 1");
40
$fetch_footer = $query->fetchRow();
41
$query = $database->query("SELECT value FROM $table WHERE name = 'results_header' LIMIT 1");
42
$fetch_results_header = $query->fetchRow();
43
$query = $database->query("SELECT value FROM $table WHERE name = 'results_footer' LIMIT 1");
44
$fetch_results_footer = $query->fetchRow();
45
$query = $database->query("SELECT value FROM $table WHERE name = 'results_loop' LIMIT 1");
46
$fetch_results_loop = $query->fetchRow();
47
$query = $database->query("SELECT value FROM $table WHERE name = 'no_results' LIMIT 1");
48
$fetch_no_results = $query->fetchRow();
49
$query = $database->query("SELECT value FROM $table WHERE name = 'module_order' LIMIT 1");
50
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value']='faqbaker,manual,wysiwyg'; }
51
$search_module_order = $res['value'];
52
$query = $database->query("SELECT value FROM $table WHERE name = 'max_excerpt' LIMIT 1");
53
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '15'; }
54
$search_max_excerpt = (int)($res['value']);
55
if(!is_numeric($search_max_excerpt)) { $search_max_excerpt = 15; }
56
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_show_description' LIMIT 1");
57
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
58
if($res['value'] == 'false') { $cfg_show_description = false; } else { $cfg_show_description = true; }
59
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_description' LIMIT 1");
60
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
61
if($res['value'] == 'false') { $cfg_search_description = false; } else { $cfg_search_description = true; }
62
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_keywords' LIMIT 1");
63
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
64
if($res['value'] == 'false') { $cfg_search_keywords = false; } else { $cfg_search_keywords = true; }
65
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_old_search' LIMIT 1");
66
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
67
if($res['value'] == 'false') { $cfg_enable_old_search = false; } else { $cfg_enable_old_search = true; }
68
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_flush' LIMIT 1");
69
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'false'; }
70
if($res['value'] == 'false') { $cfg_enable_flush = false; } else { $cfg_enable_flush = true; }
71
$query = $database->query("SELECT value FROM $table WHERE name = 'time_limit' LIMIT 1"); // time-limit per module
72
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '0'; }
73
$search_time_limit = (int)($res['value']);
74
if($search_time_limit < 1) $search_time_limit = 0;
75
76
// search-module-extension: get helper-functions
77
require_once(WB_PATH.'/search/search_modext.php');
78
// search-module-extension: Get "search.php" for each module, if present
79
// looks in modules/module/ and modules/module_searchext/
80
$search_funcs = array();$search_funcs['__before'] = array();$search_funcs['__after'] = array();
81
$query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
82
if($query->numRows() > 0) {
83
	while($module = $query->fetchRow()) {
84
		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
85
		if(!file_exists($file)) {
86
			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
87
			if(!file_exists($file)) {
88
				$file='';
89
			}
90
		}
91
		if($file!='') {
92
			include_once($file);
93
			if(function_exists($module['directory']."_search")) {
94
				$search_funcs[$module['directory']] = $module['directory']."_search";
95
			}
96
			if(function_exists($module['directory']."_search_before")) {
97
				$search_funcs['__before'][] = $module['directory']."_search_before";
98
			}
99
			if(function_exists($module['directory']."_search_after")) {
100
				$search_funcs['__after'][] = $module['directory']."_search_after";
101
			}
102
		}
103
	}
104
}
105
106
// Get list of usernames and display names
107
$query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
108
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
109
if($query->numRows() > 0) {
110
	while($user = $query->fetchRow()) {
111
		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
112
	}
113
}
114
115
// Get search language, used for special umlaut handling (DE: ß=ss, ...)
116
$search_lang = '';
117
if(isset($_REQUEST['search_lang'])) {
118
	$search_lang = $_REQUEST['search_lang'];
119
	if(!preg_match('~^[A-Z]{2}$~', $search_lang))
120
		$search_lang = LANGUAGE;
121
} else {
122
	$search_lang = LANGUAGE;
123
}
124
125
// Get the path to search into. Normally left blank
126
// 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"
127
// Add a '%' in front of each path to get SQL: LIKE "%/path%"
128
/* possible values:
129
 * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
130
 * - a single path not to search into: "-/help" - search all, exclude /help...
131
 * - a bunch of alternative pathes: "/en/,%/machinery/,/docs/" - alternatives paths, seperated by comma
132
 * - a bunch of paths to exclude: "-/about,%/info,/jp/,/light" - search all, exclude these.
133
 * These different styles can't be mixed.
134
 */
135
// ATTN: in wb2.7.0 "/en/" matched all links with "/en/" somewhere in the link: "/info/en/intro.php", "/en/info.php", ...
136
// since wb2.7.1 "/en/" matches only links _starting_  with "/en/": "/en/intro/info.php"
137
// use "%/en/" (or "%/en/, %/info", ...) to get the old behavior
138
$search_path_SQL = '';
139
$search_path = '';
140 1832 Luisehahne
// solve $_REQUEST['search_path' to be string
141
if(isset($_REQUEST['search_path']) && is_array($_REQUEST['search_path'])) {
142
    $_REQUEST['search_path'] = implode(",", $_REQUEST['search_path']);
143
}
144 1420 Luisehahne
if(isset($_REQUEST['search_path'])) {
145
	$search_path = addslashes(htmlspecialchars(strip_tags($wb->strip_slashes($_REQUEST['search_path'])), ENT_QUOTES));
146
	if(!preg_match('~^%?[-a-zA-Z0-9_,/ ]+$~', $search_path))
147
		$search_path = '';
148
	if($search_path != '') {
149
		$search_path_SQL = 'AND ( ';
150
		$not = '';
151
		$op = 'OR';
152
		if($search_path[0] == '-') {
153
			$not = 'NOT';
154
			$op = 'AND';
155
			$paths = explode(',', substr($search_path, 1) );
156
		} else {
157
			$paths = explode(',',$search_path);
158
		}
159
		$i=0;
160
		foreach($paths as $p) {
161
			if($i++ > 0) {
162
				$search_path_SQL .= ' $op';
163
			}
164 1718 Luisehahne
			$search_path_SQL .= " link $not LIKE '".$p."%'";
165 1420 Luisehahne
		}
166
		$search_path_SQL .= ' )';
167
	}
168
}
169
170
// use page_languages?
171
if(PAGE_LANGUAGES) {
172
	$table = TABLE_PREFIX."pages";
173
	$search_language_SQL_t = "AND $table.`language` = '".LANGUAGE."'";
174
	$search_language_SQL = "AND `language` = '".LANGUAGE."'";
175
} else {
176
	$search_language_SQL_t = '';
177
	$search_language_SQL = '';
178
}
179
180
// Get the search type
181
$match = '';
182
if(isset($_REQUEST['match'])) {
183
	if($_REQUEST['match']=='any') $match = 'any';
184
	elseif($_REQUEST['match']=='all') $match = 'all';
185
	elseif($_REQUEST['match']=='exact') $match = 'exact';
186
	else $match = 'all';
187
} else {
188
	$match = 'all';
189
}
190
191
// Get search string
192
$search_normal_string = '';
193
$search_entities_string = ''; // for SQL's LIKE
194
$search_display_string = ''; // for displaying
195
$search_url_string = ''; // for $_GET -- ATTN: unquoted! Will become urldecoded later
196
$string = '';
197 1442 Luisehahne
if(isset($_REQUEST['string'])) {
198 1718 Luisehahne
	if($match!='exact') { // $string will be cleaned below
199 1420 Luisehahne
		$string=str_replace(',', '', $_REQUEST['string']);
200
	} else {
201
		$string=$_REQUEST['string'];
202
	}
203
    // redo possible magic quotes
204
    $string = $wb->strip_slashes($string);
205 1442 Luisehahne
    $string = preg_replace('/\s+/', ' ', $string);
206 1420 Luisehahne
    $string = trim($string);
207
	// remove some bad chars
208
	$string = str_replace ( array('[[',']]'),'', $string);
209
	$string = preg_replace('/(^|\s+)[|.]+(?=\s+|$)/', '', $string);
210
	$search_display_string = htmlspecialchars($string);
211
	// convert string to utf-8
212
	$string = entities_to_umlauts($string, 'UTF-8');
213
	$search_url_string = $string;
214 1442 Luisehahne
	$search_entities_string = addslashes(htmlentities($string, ENT_COMPAT, 'UTF-8'));
215
	// mySQL needs four backslashes to match one in LIKE comparisons)
216
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
217 1420 Luisehahne
	$string = preg_quote($string);
218
	// quote ' " and /  -we need quoted / for regex
219
	$search_normal_string = str_replace(array('\'','"','/'), array('\\\'','\"','\/'), $string);
220
}
221
// make arrays from the search_..._strings above
222
if($match == 'exact')
223
	$search_url_array[] = $search_url_string;
224
else
225
	$search_url_array = explode(' ', $search_url_string);
226
$search_normal_array = array();
227
$search_entities_array = array();
228
if($match == 'exact') {
229
	$search_normal_array[]=$search_normal_string;
230
	$search_entities_array[]=$search_entities_string;
231
} else {
232
	$exploded_string = explode(' ', $search_normal_string);
233
	// Make sure there is no blank values in the array
234
	foreach($exploded_string AS $each_exploded_string) {
235
		if($each_exploded_string != '') {
236
			$search_normal_array[] = $each_exploded_string;
237
		}
238
	}
239
	$exploded_string = explode(' ', $search_entities_string);
240
	// Make sure there is no blank values in the array
241
	foreach($exploded_string AS $each_exploded_string) {
242
		if($each_exploded_string != '') {
243
			$search_entities_array[] = $each_exploded_string;
244
		}
245
	}
246
}
247
// make an extra copy of search_normal_array for use in regex
248
$search_words = array();
249 1442 Luisehahne
require_once(WB_PATH.'/search/search_convert.php');
250
global $search_table_umlauts_local;
251
require_once(WB_PATH.'/search/search_convert_ul.php');
252
global $search_table_ul_umlauts;
253 1420 Luisehahne
foreach($search_normal_array AS $str) {
254 1442 Luisehahne
	$str = strtr($str, $search_table_umlauts_local);
255
	$str = strtr($str, $search_table_ul_umlauts);
256 1420 Luisehahne
	$search_words[] = $str;
257
}
258
259
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
260
$all_checked = '';
261
$any_checked = '';
262
$exact_checked = '';
263
if ($match == 'any') {
264
	$any_checked = ' checked="checked"';
265
	$logical_operator = ' OR';
266
} elseif($match == 'all') {
267
	$all_checked = ' checked="checked"';
268
	$logical_operator = ' AND';
269
} else {
270
	$exact_checked = ' checked="checked"';
271
}
272
273
// Replace vars in search settings with values
274
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
275
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
276
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
277
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
278
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
279
280
// Do extra vars/values replacement
281 1969 Luisehahne
$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]');
282 1420 Luisehahne
$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);
283
$search_header = str_replace($vars, $values, ($fetch_header['value']));
284
$vars = array('[TEXT_NO_RESULTS]');
285
$values = array($TEXT['NO_RESULTS']);
286
$search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
287
288
/*
289
 * Start of output
290
 */
291
292
// Show search header
293
echo $search_header;
294
// Show search results_header
295
echo $search_results_header;
296
297
// Work-out if the user has already entered their details or not
298
if($search_normal_string != '') {
299
300
	// Get modules
301
	$table = TABLE_PREFIX."sections";
302
	$get_modules = $database->query("SELECT DISTINCT module FROM $table WHERE module != '' ");
303
	$modules = array();
304
	if($get_modules->numRows() > 0) {
305
		while($module = $get_modules->fetchRow()) {
306
			$modules[] = $module['module'];
307
		}
308
	}
309
	// sort module search-order
310
	// get the modules from $search_module_order first ...
311
	$sorted_modules = array();
312
	$m = count($modules);
313
	$search_modules = explode(',', $search_module_order);
314
	foreach($search_modules AS $item) {
315
		$item = trim($item);
316
		for($i=0; $i < $m; $i++) {
317
			if(isset($modules[$i]) && $modules[$i] == $item) {
318
				$sorted_modules[] = $modules[$i];
319
				unset($modules[$i]);
320
				break;
321
			}
322
		}
323
	}
324
	// ... then add the rest
325
	foreach($modules AS $item) {
326
		$sorted_modules[] = $item;
327
	}
328
329
330
	// Use the module's search-extensions.
331
	// This is somewhat slower than the orginial method.
332
	// call $search_funcs['__before'] first
333
	$search_func_vars = array(
334
		'database' => $database, // database-handle
335
		'page_id' => 0,
336
		'section_id' => 0,
337
		'page_title' => '',
338
		'page_menu_title' => '',
339
		'page_description' => '',
340
		'page_keywords' => '',
341
		'page_link' => '',
342
		'page_modified_when' => 0,
343
		'page_modified_by' => 0,
344
		'users' => $users, // array of known user-id/user-name
345
		'search_words' => $search_words, // array of strings, prepared for regex
346
		'search_match' => $match, // match-type
347
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
348 1442 Luisehahne
		'search_entities_array' => $search_entities_array, // entities
349 1420 Luisehahne
		'results_loop_string' => $fetch_results_loop['value'],
350
		'default_max_excerpt' => $search_max_excerpt,
351
		'time_limit' => $search_time_limit, // time-limit in secs
352
		'search_path' => $search_path // see docu
353
	);
354
	foreach($search_funcs['__before'] as $func) {
355
		$uf_res = call_user_func($func, $search_func_vars);
356
	}
357
	// now call module-based $search_funcs[]
358
	$seen_pages = array(); // seen pages per module.
359
	$pages_listed = array(); // seen pages.
360
	if($search_max_excerpt!=0) { // skip this search if $search_max_excerpt==0
361
		foreach($sorted_modules AS $module_name) {
362
			$start_time = time();	// get start-time to check time-limit; not very accurate, but ok
363
			$seen_pages[$module_name] = array();
364
			if(!isset($search_funcs[$module_name])) {
365
				continue; // there is no search_func for this module
366
			}
367
			// get each section for $module_name
368 1718 Luisehahne
			$table_s = TABLE_PREFIX."sections";
369 1420 Luisehahne
			$table_p = TABLE_PREFIX."pages";
370
			$sections_query = $database->query("
371
				SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
372
							 p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
373
							 p.visibility, p.viewing_groups, p.viewing_users
374
				FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
375
				WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL $search_language_SQL
376
				ORDER BY s.page_id, s.position ASC
377
			");
378
			if($sections_query->numRows() > 0) {
379
				while($res = $sections_query->fetchRow()) {
380
					// check if time-limit is exceeded for this module
381
					if($search_time_limit > 0 && (time()-$start_time > $search_time_limit)) {
382
						break;
383
					}
384
					// Only show this section if it is not "out of publication-date"
385
					$now = time();
386
					if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
387
						$now>$res['publ_start'] && $res['publ_end']==0) ) {
388
						continue;
389
					}
390
					$search_func_vars = array(
391
						'database' => $database,
392
						'page_id' => $res['page_id'],
393
						'section_id' => $res['section_id'],
394
						'page_title' => $res['page_title'],
395
						'page_menu_title' => $res['menu_title'],
396
						'page_description' => ($cfg_show_description?$res['description']:""),
397
						'page_keywords' => $res['keywords'],
398
						'page_link' => $res['link'],
399
						'page_modified_when' => $res['modified_when'],
400
						'page_modified_by' => $res['modified_by'],
401
						'users' => $users,
402
						'search_words' => $search_words, // needed for preg_match
403
						'search_match' => $match,
404
						'search_url_array' => $search_url_array, // needed for url-string only
405 1442 Luisehahne
						'search_entities_array' => $search_entities_array, // entities
406 1420 Luisehahne
						'results_loop_string' => $fetch_results_loop['value'],
407
						'default_max_excerpt' => $search_max_excerpt,
408
						'enable_flush' => $cfg_enable_flush,
409
						'time_limit' => $search_time_limit // time-limit in secs
410
					);
411
					// Only show this page if we are allowed to see it
412
					if($admin->page_is_visible($res) == false) {
413
						if($res['visibility'] == 'registered') { // don't show excerpt
414
							$search_func_vars['default_max_excerpt'] = 0;
415
							$search_func_vars['page_description'] = $TEXT['REGISTERED'];
416
						} else { // private
417
							continue;
418
						}
419
					}
420
					$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
421
					if($uf_res) {
422
						$pages_listed[$res['page_id']] = true;
423
						$seen_pages[$module_name][$res['page_id']] = true;
424
					} else {
425
						$seen_pages[$module_name][$res['page_id']] = true;
426
					}
427
				}
428
			}
429
		}
430
	}
431
	// now call $search_funcs['__after']
432
	$search_func_vars = array(
433
		'database' => $database, // database-handle
434
		'page_id' => 0,
435
		'section_id' => 0,
436
		'page_title' => '',
437
		'page_menu_title' => '',
438
		'page_description' => '',
439
		'page_keywords' => '',
440
		'page_link' => '',
441
		'page_modified_when' => 0,
442
		'page_modified_by' => 0,
443
		'users' => $users, // array of known user-id/user-name
444
		'search_words' => $search_words, // array of strings, prepared for regex
445
		'search_match' => $match, // match-type
446
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
447 1442 Luisehahne
		'search_entities_array' => $search_entities_array, // entities
448 1420 Luisehahne
		'results_loop_string' => $fetch_results_loop['value'],
449
		'default_max_excerpt' => $search_max_excerpt,
450
		'time_limit' => $search_time_limit, // time-limit in secs
451
		'search_path' => $search_path // see docu
452
	);
453
	foreach($search_funcs['__after'] as $func) {
454
		$uf_res = call_user_func($func, $search_func_vars);
455
	}
456
457
458
	// Search page details only, such as description, keywords, etc, but only of unseen pages.
459
	$max_excerpt_num = 0; // we don't want excerpt here
460
	$divider = ".";
461
	$table = TABLE_PREFIX."pages";
462
	$query_pages = $database->query("
463
		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
464
		       visibility, viewing_groups, viewing_users
465
		FROM $table
466
		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL $search_language_SQL
467
	");
468
	if($query_pages->numRows() > 0) {
469
		while($page = $query_pages->fetchRow()) {
470
			if (isset($pages_listed[$page['page_id']])) {
471
				continue;
472
			}
473
			$func_vars = array(
474
				'database' => $database,
475
				'page_id' => $page['page_id'],
476
				'page_title' => $page['page_title'],
477
				'page_menu_title' => $page['menu_title'],
478
				'page_description' => ($cfg_show_description?$page['description']:""),
479
				'page_keywords' => $page['keywords'],
480
				'page_link' => $page['link'],
481
				'page_modified_when' => $page['modified_when'],
482
				'page_modified_by' => $page['modified_by'],
483
				'users' => $users,
484
				'search_words' => $search_words, // needed for preg_match_all
485
				'search_match' => $match,
486
				'search_url_array' => $search_url_array, // needed for url-string only
487 1442 Luisehahne
				'search_entities_array' => $search_entities_array, // entities
488 1420 Luisehahne
				'results_loop_string' => $fetch_results_loop['value'],
489
				'default_max_excerpt' => $max_excerpt_num,
490
				'enable_flush' => $cfg_enable_flush
491
			);
492
			// Only show this page if we are allowed to see it
493
			if($admin->page_is_visible($page) == false) {
494
				if($page['visibility'] != 'registered') {
495
					continue;
496
				} else { // page: registered, user: access denied
497
					$func_vars['page_description'] = $TEXT['REGISTERED'];
498
				}
499
			}
500
			if($admin->page_is_active($page) == false) {
501
				continue;
502
			}
503
			$text = $func_vars['page_title'].$divider
504
				.$func_vars['page_menu_title'].$divider
505
				.($cfg_search_description?$func_vars['page_description']:"").$divider
506
				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
507
			$mod_vars = array(
508
				'page_link' => $func_vars['page_link'],
509
				'page_link_target' => "",
510
				'page_title' => $func_vars['page_title'],
511
				'page_description' => $func_vars['page_description'],
512
				'page_modified_when' => $func_vars['page_modified_when'],
513
				'page_modified_by' => $func_vars['page_modified_by'],
514
				'text' => $text,
515
				'max_excerpt_num' => $func_vars['default_max_excerpt']
516
			);
517
			if(print_excerpt2($mod_vars, $func_vars)) {
518
				$pages_listed[$page['page_id']] = true;
519
			}
520
		}
521
	}
522
523
	// Now use the old method for pages not displayed by the new method above
524
	// in case someone has old modules without search.php.
525
526
	// Get modules
527
	$table_search = TABLE_PREFIX."search";
528
	$table_sections = TABLE_PREFIX."sections";
529
	$get_modules = $database->query("
530
		SELECT DISTINCT s.value, s.extra
531
		FROM $table_search AS s INNER JOIN $table_sections AS sec
532
			ON s.value = sec.module
533
		WHERE s.name = 'module'
534
	");
535
	$modules = array();
536
	if($get_modules->numRows() > 0) {
537
		while($module = $get_modules->fetchRow()) {
538
			$modules[] = $module; // $modules in an array of arrays
539
		}
540
	}
541
	// sort module search-order
542
	// get the modules from $search_module_order first ...
543
	$sorted_modules = array();
544
	$m = count($modules);
545
	$search_modules = explode(',', $search_module_order);
546
	foreach($search_modules AS $item) {
547
		$item = trim($item);
548
		for($i=0; $i < $m; $i++) {
549
			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
550
				$sorted_modules[] = $modules[$i];
551
				unset($modules[$i]);
552
				break;
553
			}
554
		}
555
	}
556
	// ... then add the rest
557
	foreach($modules AS $item) {
558
		$sorted_modules[] = $item;
559
	}
560
561
	if($cfg_enable_old_search) { // this is the old (wb <= 2.6.7) search-function
562
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
563
		foreach($sorted_modules AS $module) {
564
			if(isset($seen_pages[$module['value']]) && count($seen_pages[$module['value']])>0) // skip modules handled by new search-func
565
				continue;
566
			$query_start = '';
567
			$query_body = '';
568
			$query_end = '';
569
			$prepared_query = '';
570
			// Get module name
571
			$module_name = $module['value'];
572
			if(!isset($seen_pages[$module_name])) {
573
				$seen_pages[$module_name]=array();
574
			}
575
			// skip module 'code' - it doesn't make sense to search in a code section
576
			if($module_name=="code")
577
				continue;
578
			// Get fields to use for title, link, etc.
579
			$fields = unserialize($module['extra']);
580
			// Get query start
581
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
582
			if($get_query_start->numRows() > 0) {
583
				// Fetch query start
584
				$fetch_query_start = $get_query_start->fetchRow();
585
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
586
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
587
			}
588
			// Get query end
589
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
590
			if($get_query_end->numRows() > 0) {
591
				// Fetch query end
592
				$fetch_query_end = $get_query_end->fetchRow();
593
				// Set query end
594
				$query_end = ($fetch_query_end['value']);
595
			}
596
			// Get query body
597
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
598
			if($get_query_body->numRows() > 0) {
599
				// Fetch query body
600
				$fetch_query_body = $get_query_body->fetchRow();
601
				// Prepare query body for execution by replacing {STRING} with the correct one
602
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
603
				// Loop through query body for each string, then combine with start and end
604
				$prepared_query = $query_start." ( ( ( ";
605
				$count = 0;
606
				foreach($search_normal_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
				$count=0;
614
				$prepared_query .= ' ) ) OR ( ( ';
615
				foreach($search_entities_array AS $string) {
616
					if($count != 0) {
617
						$prepared_query .= " ) ".$logical_operator." ( ";
618
					}
619
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
620
					$count = $count+1;
621
				}
622
				$prepared_query .= " ) ) ) ".$query_end;
623
				// Execute query
624
				$page_query = $database->query($prepared_query." ".$search_path_SQL." ".$search_language_SQL_t);
625
				if(!$page_query) continue; // on error, skip the rest of the current loop iteration
626
				// Loop through queried items
627
				if($page_query->numRows() > 0) {
628
					while($page = $page_query->fetchRow()) {
629
						// Only show this page if it hasn't already been listed
630
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
631
							continue;
632
						}
633 1718 Luisehahne
634 1420 Luisehahne
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
635
						$p_table = TABLE_PREFIX."pages";
636
						$viewquery = $database->query("
637
							SELECT visibility, viewing_groups, viewing_users
638
							FROM $p_table
639
							WHERE page_id='{$page['page_id']}'
640
						");
641
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
642
						if($viewquery->numRows() > 0) {
643
							if($res = $viewquery->fetchRow()) {
644
								$visibility = $res['visibility'];
645
								$viewing_groups = $res['viewing_groups'];
646
								$viewing_users = $res['viewing_users'];
647
								if($visibility == 'deleted' || $visibility == 'none') {
648
									continue;
649
								}
650
								if($visibility == 'private') {
651
									if($admin->page_is_visible(array(
652
										'page_id'=>$page[$fields['page_id']],
653
										'visibility' =>$visibility,
654
										'viewing_groups'=>$viewing_groups,
655
										'viewing_users'=>$viewing_users
656
									)) == false) {
657
										continue;
658
									}
659
								}
660
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
661
									continue;
662
								}
663
							}
664
						}
665 1718 Luisehahne
666 1420 Luisehahne
						// Get page link
667
						$link = page_link($page['link']);
668
						// Add search string for highlighting
669
						if ($match!='exact') {
670
							$sstring = implode(" ", $search_normal_array);
671
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
672
						} else {
673
							$sstring = str_replace(" ", "_",$search_normal_array[0]);
674
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
675
						}
676
						// Set vars to be replaced by values
677
						if(!isset($page['description'])) { $page['description'] = ""; }
678
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
679
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
680
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
681
						if($page['modified_when'] > 0) {
682
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
683
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
684
						} else {
685
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
686
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
687
						}
688
						$excerpt="";
689
						if($cfg_show_description == 0) {
690
							$page['description'] = "";
691
						}
692
						$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);
693
						// Show loop code with vars replaced by values
694
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
695
						// Say that this page has been listed
696
						$seen_pages[$module_name][$page['page_id']] = true;
697
						$pages_listed[$page['page_id']] = true;
698
					}
699
				}
700
			}
701
		}
702
	}
703
704
	// Say no items found if we should
705
	if(count($pages_listed) == 0) {
706
		echo $search_no_results;
707
	}
708
} else {
709
	echo $search_no_results;
710
}
711
712
// Show search results_footer
713
echo $search_results_footer;
714
// Show search footer
715
echo $search_footer;