Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 874 2008-11-06 21:35:43Z thorn $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, 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 = preg_replace('/(^|\s+)[|.]+(?=\s+|$)/', '', $string);
200
	$search_display_string = htmlspecialchars($string);
201
	$search_entities_string = addslashes(umlauts_to_entities(htmlspecialchars($string)));
202
	// mySQL needs four backslashes to match one in LIKE comparisons)
203
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
204
	// convert string to utf-8
205
	$string = entities_to_umlauts($string, 'UTF-8');
206
	$search_url_string = $string;
207
	$string = preg_quote($string);
208
	// quote ' " and /  -we need quoted / for regex
209
	$search_normal_string = str_replace(array('\'','"','/'), array('\\\'','\"','\/'), $string);
210
}
211
// make arrays from the search_..._strings above
212
if($match == 'exact')
213
	$search_url_array[] = $search_url_string;
214
else
215
	$search_url_array = explode(' ', $search_url_string);
216
$search_normal_array = array();
217
$search_entities_array = array();
218
if($match == 'exact') {
219
	$search_normal_array[]=$search_normal_string;
220
	$search_entities_array[]=$search_entities_string;
221
} else {
222
	$exploded_string = explode(' ', $search_normal_string);
223
	// Make sure there is no blank values in the array
224
	foreach($exploded_string AS $each_exploded_string) {
225
		if($each_exploded_string != '') {
226
			$search_normal_array[] = $each_exploded_string;
227
		}
228
	}
229
	$exploded_string = explode(' ', $search_entities_string);
230
	// Make sure there is no blank values in the array
231
	foreach($exploded_string AS $each_exploded_string) {
232
		if($each_exploded_string != '') {
233
			$search_entities_array[] = $each_exploded_string;
234
		}
235
	}
236
}
237
// make an extra copy of search_normal_array for use in regex
238
require_once(WB_PATH.'/search/search_convert.php');
239
$search_words = array();
240
foreach($search_normal_array AS $str) {
241
	$str = strtr($str, $string_ul_umlauts);
242
	$search_words[] = $str;
243
}
244

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

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

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

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

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

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

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

    
315

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

    
438

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

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

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

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

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

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

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

    
697
?>
(2-2/4)