Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 989 2009-06-15 13:12:39Z aldus $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
if(!defined('WB_URL')) { 
27
	header('Location: index.php');
28
	exit(0);
29
}
30

    
31
// Check if search is enabled
32
if(SHOW_SEARCH != true) {
33
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
34
	return;
35
}
36

    
37
// Include the WB functions file
38
require_once(WB_PATH.'/framework/functions.php');
39

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

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

    
111
// Get list of usernames and display names
112
$query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
113
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
114
if($query->numRows() > 0) {
115
	while($user = $query->fetchRow()) {
116
		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
117
	}
118
}
119

    
120
// Get search language
121
$search_lang = '';
122
if(isset($_REQUEST['search_lang'])) {
123
	$search_lang = $_REQUEST['search_lang'];
124
	if(!preg_match('~^[A-Z]{2}$~', $search_lang))
125
		$search_lang = LANGUAGE;
126
} else {
127
	$search_lang = LANGUAGE;
128
}
129

    
130
// Get the path to search into. Normally left blank
131
// 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"
132
// Add a '%' in front of each path to get SQL: LIKE "%/path%"
133
/* possible values:
134
 * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
135
 * - a single path not to search into: "-/help" - search all, exclude /help...
136
 * - a bunch of alternative pathes: "/en/,%/machinery/,/docs/" - alternatives paths, seperated by comma
137
 * - a bunch of paths to exclude: "-/about,%/info,/jp/,/light" - search all, exclude these.
138
 * These different styles can't be mixed.
139
 */
140
// ATTN: in wb2.7.0 "/en/" matched all links with "/en/" somewhere in the link: "/info/en/intro.php", "/en/info.php", ...
141
// since wb2.7.1 "/en/" matches only links _starting_  with "/en/": "/en/intro/info.php"
142
// use "%/en/" (or "%/en/, %/info", ...) to get the old behavior
143
$search_path_SQL = '';
144
$search_path = '';
145
if(isset($_REQUEST['search_path'])) {
146
	$search_path = addslashes(htmlspecialchars(strip_tags($wb->strip_slashes($_REQUEST['search_path']))));
147
	if(!preg_match('~^%?[-a-zA-Z0-9_,/ ]+$~', $search_path))
148
		$search_path = '';
149
	if($search_path != '') {
150
		$search_path_SQL = 'AND ( ';
151
		$not = '';
152
		$op = 'OR';
153
		if($search_path[0] == '-') {
154
			$not = 'NOT';
155
			$op = 'AND';
156
			$paths = explode(',', substr($search_path, 1) );
157
		} else {
158
			$paths = explode(',',$search_path);
159
		}
160
		$i=0;
161
		foreach($paths as $p) {
162
			if($i++ > 0) {
163
				$search_path_SQL .= ' $op';
164
			}
165
			$search_path_SQL .= " link $not LIKE '".$p."%'";			
166
		}
167
		$search_path_SQL .= ' )';
168
	}
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
	$search_entities_string = addslashes(umlauts_to_entities(htmlspecialchars($string)));
203
	// mySQL needs four backslashes to match one in LIKE comparisons)
204
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
205
	// convert string to utf-8
206
	$string = entities_to_umlauts($string, 'UTF-8');
207
	$search_url_string = $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
require_once(WB_PATH.'/search/search_convert.php');
240
$search_words = array();
241
foreach($search_normal_array AS $str) {
242
	$str = str_replace($string_ul_umlaut, $string_ul_regex, $str);
243
	$search_words[] = $str;
244
}
245

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

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

    
267
// Do extra vars/values replacement
268
$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]');
269
$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);
270
$search_header = str_replace($vars, $values, ($fetch_header['value']));
271
$vars = array('[TEXT_NO_RESULTS]');
272
$values = array($TEXT['NO_RESULTS']);
273
$search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
274

    
275
/*
276
 * Start of output
277
 */
278

    
279
// Show search header
280
echo $search_header;
281
// Show search results_header
282
echo $search_results_header;
283

    
284
// Work-out if the user has already entered their details or not
285
if($search_normal_string != '') {
286

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

    
316

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

    
439

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

    
504
	// Now use the old method for pages not displayed by the new method above
505
	// in case someone has old modules without search.php.
506

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

    
542
	if($cfg_enable_old_search) { // this is the old (wb <= 2.6.7) search-function
543
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
544
		foreach($sorted_modules AS $module) {
545
			if(isset($seen_pages[$module['value']]) && count($seen_pages[$module['value']])>0) // skip modules handled by new search-func
546
				continue;
547
			$query_start = '';
548
			$query_body = '';
549
			$query_end = '';
550
			$prepared_query = '';
551
			// Get module name
552
			$module_name = $module['value'];
553
			if(!isset($seen_pages[$module_name])) {
554
				$seen_pages[$module_name]=array();
555
			}
556
			// skip module 'code' - it doesn't make sense to search in a code section
557
			if($module_name=="code")
558
				continue;
559
			// Get fields to use for title, link, etc.
560
			$fields = unserialize($module['extra']);
561
			// Get query start
562
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
563
			if($get_query_start->numRows() > 0) {
564
				// Fetch query start
565
				$fetch_query_start = $get_query_start->fetchRow();
566
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
567
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
568
			}
569
			// Get query end
570
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
571
			if($get_query_end->numRows() > 0) {
572
				// Fetch query end
573
				$fetch_query_end = $get_query_end->fetchRow();
574
				// Set query end
575
				$query_end = ($fetch_query_end['value']);
576
			}
577
			// Get query body
578
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
579
			if($get_query_body->numRows() > 0) {
580
				// Fetch query body
581
				$fetch_query_body = $get_query_body->fetchRow();
582
				// Prepare query body for execution by replacing {STRING} with the correct one
583
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
584
				// Loop through query body for each string, then combine with start and end
585
				$prepared_query = $query_start." ( ( ( ";
586
				$count = 0;
587
				foreach($search_normal_array AS $string) {
588
					if($count != 0) {
589
						$prepared_query .= " ) ".$logical_operator." ( ";
590
					}
591
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
592
					$count = $count+1;
593
				}
594
				$count=0;
595
				$prepared_query .= ' ) ) OR ( ( ';
596
				foreach($search_entities_array AS $string) {
597
					if($count != 0) {
598
						$prepared_query .= " ) ".$logical_operator." ( ";
599
					}
600
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
601
					$count = $count+1;
602
				}
603
				$prepared_query .= " ) ) ) ".$query_end;
604
				// Execute query
605
				$page_query = $database->query($prepared_query." ".$search_path_SQL);
606

    
607
				// Loop through queried items
608
				if($page_query->numRows() > 0) {
609
					while($page = $page_query->fetchRow()) {
610
						// Only show this page if it hasn't already been listed
611
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
612
							continue;
613
						}
614
						
615
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
616
						$p_table = TABLE_PREFIX."pages";
617
						$viewquery = $database->query("
618
							SELECT visibility, viewing_groups, viewing_users
619
							FROM $p_table
620
							WHERE page_id='{$page['page_id']}'
621
						");
622
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
623
						if($viewquery->numRows() > 0) {
624
							if($res = $viewquery->fetchRow()) {
625
								$visibility = $res['visibility'];
626
								$viewing_groups = $res['viewing_groups'];
627
								$viewing_users = $res['viewing_users'];
628
								if($visibility == 'deleted' || $visibility == 'none') {
629
									continue;
630
								}
631
								if($visibility == 'private') {
632
									if($admin->page_is_visible(array(
633
										'page_id'=>$page[$fields['page_id']],
634
										'visibility' =>$visibility,
635
										'viewing_groups'=>$viewing_groups,
636
										'viewing_users'=>$viewing_users
637
									)) == false) {
638
										continue;
639
									}
640
								}
641
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
642
									continue;
643
								}
644
							}
645
						}
646
	
647
						// Get page link
648
						$link = page_link($page['link']);
649
						// Add search string for highlighting
650
						if ($match!='exact') {
651
							$sstring = implode(" ", $search_normal_array);
652
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
653
						} else {
654
							$sstring = str_replace(" ", "_",$search_normal_array[0]);
655
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
656
						}
657
						// Set vars to be replaced by values
658
						if(!isset($page['description'])) { $page['description'] = ""; }
659
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
660
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
661
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
662
						if($page['modified_when'] > 0) {
663
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
664
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
665
						} else {
666
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
667
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
668
						}
669
						$excerpt="";
670
						if($cfg_show_description == 0) {
671
							$page['description'] = "";
672
						}
673
						$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);
674
						// Show loop code with vars replaced by values
675
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
676
						// Say that this page has been listed
677
						$seen_pages[$module_name][$page['page_id']] = true;
678
						$pages_listed[$page['page_id']] = true;
679
					}
680
				}
681
			}
682
		}
683
	}
684

    
685
	// Say no items found if we should
686
	if(count($pages_listed) == 0) {
687
		echo $search_no_results;
688
	}
689
} else {
690
	echo $search_no_results;
691
}
692

    
693
// Show search results_footer
694
echo $search_results_footer;
695
// Show search footer
696
echo $search_footer;
697

    
698
?>
(2-2/4)