Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 772 2008-03-25 22:34:27Z 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
// we have to do some cleanup here, ASAP!
27

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

    
33
// Include the WB functions file
34
require_once(WB_PATH.'/framework/functions.php');
35

    
36
// Check if search is enabled
37
if(SHOW_SEARCH != true) {
38
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
39
	return;
40
}
41

    
42
// search-module-extension: get helper-functions
43
require_once(WB_PATH.'/search/search_modext.php');
44
// search-module-extension: Get "search.php" for each module, if present
45
// looks in modules/module/ and modules/module_searchext/
46
$search_funcs = array();$search_funcs['__before'] = array();$search_funcs['__after'] = array();
47
$query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
48
if($query->numRows() > 0) {
49
	while($module = $query->fetchRow()) {
50
		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
51
		if(!file_exists($file)) {
52
			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
53
			if(!file_exists($file)) {
54
				$file='';
55
			}
56
		}
57
		if($file!='') {
58
			include_once($file);
59
			if(function_exists($module['directory']."_search")) {
60
				$search_funcs[$module['directory']] = $module['directory']."_search";
61
			}
62
			if(function_exists($module['directory']."_search_before")) {
63
				$search_funcs['__before'][] = $module['directory']."_search_before";
64
			}
65
			if(function_exists($module['directory']."_search_after")) {
66
				$search_funcs['__after'][] = $module['directory']."_search_after";
67
			}
68
		}
69
	}
70
}
71

    
72
// Get the search type
73
$match = 'all';
74
if(isset($_REQUEST['match'])) {
75
	$match = $wb->add_slashes(strip_tags($_REQUEST['match']));
76
}
77

    
78
// Get the path to search into. Normally left blank
79
/* possible values:
80
 * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
81
 * - a bunch of alternative pathes: "/en/,/machinery/,docs/" - alternatives paths, seperated by comma
82
 * - a bunch of paths to exclude: "-/about,/info,/jp/,/light" - search all, exclude these.
83
 * These different styles can't be mixed.
84
 */
85
$search_path_SQL = "";
86
$search_path = "";
87
if(isset($_REQUEST['search_path'])) {
88
	$search_path = $wb->add_slashes($_REQUEST['search_path']);
89
	if(!preg_match('~^[-a-zA-Z0-9_,/ ]+$~', $search_path))
90
		$search_path = '';
91
	if($search_path != '') {
92
		$search_path_SQL = "AND ( ";
93
		$not = "";
94
		$op = "OR";
95
		if($search_path[0] == '-') {
96
			$not = "NOT";
97
			$op = "AND";
98
			$paths = explode(',', substr($search_path, 1) );
99
		} else {
100
			$paths = explode(',',$search_path);
101
		}
102
		$i=0;
103
		foreach($paths as $p) {
104
			if($i++ > 0) {
105
				$search_path_SQL .= " $op";
106
			}
107
			$search_path_SQL .= " link $not LIKE '%$p%'";			
108
		}
109
		$search_path_SQL .= " )";
110
	}
111
}
112

    
113
// TODO: with the new method, there is no need for search_entities_string anymore.
114
//   When the old method disappears, it can be removed, too.
115
//   BTW: in this case, there is no need for 
116
//   $text = umlauts_to_entities(strip_tags($content), strtoupper(DEFAULT_CHARSET), 0);
117
//   in wb/modules/wysiwyg/save.php anymore, too. Change that back to $text=strip_tags($content);
118

    
119
// Get search string
120
$search_normal_string = 'unset'; // for regex
121
$search_entities_string = 'unset'; // for SQL's LIKE
122
$search_display_string = ''; // for displaying
123
$string = '';
124
if(isset($_REQUEST['string'])) {
125
	if ($match!='exact') {
126
		$string=str_replace(',', '', $_REQUEST['string']);
127
	} else {
128
		$string=$_REQUEST['string']; // $string will be cleaned below
129
	}
130
	// redo possible magic quotes
131
	$string = $wb->strip_slashes($string);
132
	$string = trim($string);
133
	// remove some bad chars
134
	$string = preg_replace('/(^|\s+)[|.]+(?=\s+|$)/', '', $string);
135
	$search_display_string = htmlspecialchars($string);
136
	// convert string to utf-8
137
	$string = entities_to_umlauts($string, 'UTF-8');
138
	$search_entities_string = addslashes(umlauts_to_entities(htmlspecialchars($string)));
139
	// mySQL needs four backslashes to match one in LIKE comparisons)
140
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
141
	// quote ' " and /  -we need quoted / for regex
142
	$search_url_string = $string;
143
	$string = preg_quote($string);
144
	$search_normal_string = str_replace(array('\'','"','/'), array('\\\'','\"','\/'), $string);
145
}
146

    
147
// Get list of usernames and display names
148
$query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
149
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
150
if($query->numRows() > 0) {
151
	while($user = $query->fetchRow()) {
152
		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
153
	}
154
}
155

    
156
// Get search settings
157
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'header' LIMIT 1");
158
$fetch_header = $query->fetchRow();
159
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'footer' LIMIT 1");
160
$fetch_footer = $query->fetchRow();
161
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_header' LIMIT 1");
162
$fetch_results_header = $query->fetchRow();
163
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_footer' LIMIT 1");
164
$fetch_results_footer = $query->fetchRow();
165
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_loop' LIMIT 1");
166
$fetch_results_loop = $query->fetchRow();
167
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'no_results' LIMIT 1");
168
$fetch_no_results = $query->fetchRow();
169
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'module_order' LIMIT 1");
170
if($query->numRows() > 0) { $fetch_module_order = $query->fetchRow();
171
} else { $fetch_module_order['value'] = ""; }
172
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'max_excerpt' LIMIT 1");
173
if($query->numRows() > 0) { $fetch_max_excerpt = $query->fetchRow();
174
} else { $fetch_max_excerpt['value'] = '15'; }
175
$search_max_excerpt = (int)$fetch_max_excerpt['value'];
176
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_show_description' LIMIT 1");
177
if($query->numRows() > 0) { $fetch_cfg_show_description = $query->fetchRow();
178
} else { $fetch_cfg_show_description['value'] = 'true'; }
179
if($fetch_cfg_show_description['value'] == 'false') { $cfg_show_description = false;
180
} else { $cfg_show_description = true; }
181
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_search_description' LIMIT 1");
182
if($query->numRows() > 0) { $fetch_cfg_search_description = $query->fetchRow();
183
} else { $fetch_cfg_search_description['value'] = 'true'; }
184
if($fetch_cfg_search_description['value'] == 'false') { $cfg_search_description = false;
185
} else { $cfg_search_description = true; }
186
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_search_keywords' LIMIT 1");
187
if($query->numRows() > 0) { $fetch_cfg_search_keywords = $query->fetchRow();
188
} else { $fetch_cfg_search_keywords['value'] = 'true'; }
189
if($fetch_cfg_search_keywords['value'] == 'false') { $cfg_search_keywords = false;
190
} else { $cfg_search_keywords = true; }
191
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_enable_old_search' LIMIT 1");
192
if($query->numRows() > 0) { $fetch_cfg_enable_old_search = $query->fetchRow();
193
} else { $fetch_cfg_enable_old_search['value'] = 'true'; }
194
if($fetch_cfg_enable_old_search['value'] == 'false') { $cfg_enable_old_search = false;
195
} else { $cfg_enable_old_search = true; }
196
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_enable_flush' LIMIT 1");
197
if($query->numRows() > 0) { $fetch_cfg_enable_flush = $query->fetchRow();
198
} else { $fetch_cfg_enable_flush['value'] = 'false'; }
199
if($fetch_cfg_enable_flush['value'] == 'false') { $cfg_enable_flush = false;
200
} else { $cfg_enable_flush = true; }
201
$query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'time_limit' LIMIT 1"); // time-limit per module
202
if($query->numRows() > 0) { $fetch_search_time_limit = $query->fetchRow();
203
} else { $fetch_search_time_limit['value'] = 'false'; }
204
$search_time_limit = (int)($fetch_search_time_limit['value']);
205
if($search_time_limit < 1) $search_time_limit = 0;
206
// Replace vars in search settings with values
207
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
208
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
209
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
210
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
211
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
212
$search_module_order = $fetch_module_order['value'];
213

    
214
// check $search_max_excerpt
215
if(!is_numeric($search_max_excerpt)) {
216
	$search_max_excerpt = 15;
217
}
218

    
219
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
220
$all_checked = '';
221
$any_checked = '';
222
$exact_checked = '';
223
$search_normal_array = array();
224
$search_entities_array = array();
225
if($match != 'exact') {
226
	// Split string into array with explode() function
227
	$exploded_string = explode(' ', $search_normal_string);
228
	// Make sure there is no blank values in the array
229
	foreach($exploded_string AS $each_exploded_string) {
230
		if($each_exploded_string != '') {
231
			$search_normal_array[] = $each_exploded_string;
232
		}
233
	}
234
	// Split $string_entities, too
235
	$exploded_string = explode(' ', $search_entities_string);
236
	// Make sure there is no blank values in the array
237
	foreach($exploded_string AS $each_exploded_string) {
238
		if($each_exploded_string != '') {
239
			$search_entities_array[] = $each_exploded_string;
240
		}
241
	}
242
	if ($match == 'any') {
243
		$any_checked = ' checked="checked"';
244
		$logical_operator = ' OR';
245
	} else {
246
		$all_checked = ' checked="checked"';
247
		$logical_operator = ' AND';
248
	}
249
} else {
250
	$exact_checked = ' checked="checked"';
251
	$exact_string=$search_normal_string;
252
	$search_normal_array[]=$exact_string;
253
	$exact_string=$search_entities_string;
254
	$search_entities_array[]=$exact_string;
255
}	
256
// make an extra copy of search-string for use in a regex and another one for url
257
require_once(WB_PATH.'/search/search_convert.php');
258
$search_words = array();
259
foreach ($search_normal_array AS $str) {
260
	$str = strtr($str, $string_ul_umlauts);
261
	// special-feature: '|' means word-boundary (\b). Searching for 'the|' will find the, but not thema.
262
	// this doesn't work correctly for unicode-chars: '|test' will work, but '|über' not.
263
	$str = strtr($str, array('\\|'=>'\b'));
264
	$search_words[] = $str;
265
}
266
$search_url_array=explode(' ', $search_url_string);
267

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

    
276
// Show search header
277
echo $search_header;
278
// Show search results_header
279
echo $search_results_header;
280

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

    
284
	// Get modules
285
	$table = TABLE_PREFIX."sections";
286
	$get_modules = $database->query("SELECT DISTINCT module FROM $table WHERE module != '' ");
287
	$modules = array();
288
	if($get_modules->numRows() > 0) {
289
		while($module = $get_modules->fetchRow()) {
290
			$modules[] = $module['module']; // $modules is an array of strings
291
		}
292
	}
293

    
294
	// sort module search-order
295
	// get the modules from $search_module_order first ...
296
	$sorted_modules = array();
297
	$m = count($modules);
298
	$search_modules = explode(',', $search_module_order);
299
	foreach($search_modules AS $item) {
300
		$item = trim($item);
301
		for($i=0; $i < $m; $i++) {
302
			if(isset($modules[$i]) && $modules[$i] == $item) {
303
				$sorted_modules[] = $modules[$i];
304
				unset($modules[$i]);
305
				break;
306
			}
307
		}
308
	}
309
	// ... then add the rest
310
	foreach($modules AS $item) {
311
		$sorted_modules[] = $item;
312
	}
313

    
314
	// First, use an alternative search-method, without sql's 'LIKE'.
315
	// 'LIKE' won't find upper/lower-variants of umlauts, cyrillic or greek chars without propperly set setlocale();
316
	// and even if setlocale() is set, it won't work for multi-linguale sites.
317
	// Use the search-module-extension instead.
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
		'users' => $users, // array of known user-id/user-name
324
		'search_words' => $search_words, // search-string, prepared for regex
325
		'search_match' => $match, // match-type
326
		'search_url_array' => $search_url_array, // original search-string. ATTN: string is not quoted!
327
		'results_loop_string' => $fetch_results_loop['value'],
328
		'default_max_excerpt' => $search_max_excerpt,
329
		'time_limit' => $search_time_limit // time-limit in secs
330
	);
331
	foreach($search_funcs['__before'] as $func) {
332
		$uf_res = call_user_func($func, $search_func_vars);
333
	}
334
	// now call module-based $search_funcs[]
335
	$seen_pages = array(); // seen pages per module.
336
	$pages_listed = array(); // seen pages.
337
	foreach($sorted_modules AS $module_name) {
338
		$start_time = time();	// get start-time to check time-limit; not very accurate, but ok
339
		$seen_pages[$module_name] = array();
340
		if(!isset($search_funcs[$module_name])) {
341
			continue; // there is no search_func for this module
342
		}
343
		// get each section for $module_name
344
		$table_s = TABLE_PREFIX."sections";	
345
		$table_p = TABLE_PREFIX."pages";
346
		$sections_query = $database->query("
347
			SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
348
			       p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
349
			       p.visibility, p.viewing_groups, p.viewing_users
350
			FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
351
			WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL
352
			ORDER BY s.section_id, s.position ASC
353
		");
354
		if($sections_query->numRows() > 0) {
355
			while($res = $sections_query->fetchRow()) {
356
				// check if time-limit is exceeded for this module
357
				if($search_time_limit > 0 && (time()-$start_time > $search_time_limit)) {
358
					break;
359
				}
360
				// Only show this section if it is not "out of publication-date"
361
				$now = time();
362
				if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
363
					$now>$res['publ_start'] && $res['publ_end']==0) ) {
364
					continue;
365
				}
366
				$search_func_vars = array(
367
					'database' => $database,
368
					'page_id' => $res['page_id'],
369
					'section_id' => $res['section_id'],
370
					'page_title' => $res['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
371
					'page_menu_title' => $res['page_title'],
372
					'page_description' => ($cfg_show_description?$res['description']:""),
373
					'page_keywords' => $res['keywords'],
374
					'page_link' => $res['link'],
375
					'page_modified_when' => $res['modified_when'],
376
					'page_modified_by' => $res['modified_by'],
377
					'users' => $users,
378
					'search_words' => $search_words, // needed for preg_match_all
379
					'search_match' => $match,
380
					'search_url_array' => $search_url_array, // needed for url-string only
381
					'results_loop_string' => $fetch_results_loop['value'],
382
					'default_max_excerpt' => $search_max_excerpt,
383
					'enable_flush' => $cfg_enable_flush
384
				);
385
				// Only show this page if we are allowed to see it
386
				if($admin->page_is_visible($res) == false) {
387
					if($res['visibility'] == 'registered') { // don't show excerpt
388
						$search_func_vars['default_max_excerpt'] = 0;
389
						$search_func_vars['page_description'] = $TEXT['REGISTERED'];
390
					} else { // private
391
						continue;
392
					}
393
				}
394
				$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
395
				if($uf_res) {
396
					$pages_listed[$res['page_id']] = true;
397
					$seen_pages[$module_name][$res['page_id']] = true;
398
				} else {
399
					$seen_pages[$module_name][$res['page_id']] = true;
400
				}
401
			}
402
		}
403
	}
404
	// now call $search_funcs['__after']
405
	$search_func_vars = array(
406
		'database' => $database, // database-handle
407
		'users' => $users, // array of known user-id/user-name
408
		'search_words' => $search_words, // search-string, prepared for regex
409
		'search_match' => $match, // match-type
410
		'search_url_array' => $search_url_array, // original search-string. ATTN: string is not quoted!
411
		'results_loop_string' => $fetch_results_loop['value'],
412
		'default_max_excerpt' => $search_max_excerpt,
413
		'time_limit' => $search_time_limit // time-limit in secs
414
	);
415
	foreach($search_funcs['__after'] as $func) {
416
		$uf_res = call_user_func($func, $search_func_vars);
417
	}
418

    
419

    
420
	// Search page details only, such as description, keywords, etc, but only of unseen pages.
421
	$max_excerpt_num = 0; // we don't want excerpt here
422
	$divider = ".";
423
	$table = TABLE_PREFIX."pages";
424
	$query_pages = $database->query("
425
		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
426
		       visibility, viewing_groups, viewing_users
427
		FROM $table
428
		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL"
429
	);
430
	if($query_pages->numRows() > 0) {
431
		while($page = $query_pages->fetchRow()) {
432
			if (isset($pages_listed[$page['page_id']])) {
433
				continue;
434
			}
435
			$func_vars = array(
436
				'database' => $database,
437
				'page_id' => $page['page_id'],
438
				'page_title' => $page['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
439
				'page_menu_title' => $page['page_title'],
440
				'page_description' => ($cfg_show_description?$page['description']:""),
441
				'page_keywords' => $page['keywords'],
442
				'page_link' => $page['link'],
443
				'page_modified_when' => $page['modified_when'],
444
				'page_modified_by' => $page['modified_by'],
445
				'users' => $users,
446
				'search_words' => $search_words, // needed for preg_match_all
447
				'search_match' => $match,
448
				'search_url_array' => $search_url_array, // needed for url-string only
449
				'results_loop_string' => $fetch_results_loop['value'],
450
				'default_max_excerpt' => $max_excerpt_num,
451
				'enable_flush' => $cfg_enable_flush
452
			);
453
			// Only show this page if we are allowed to see it
454
			if($admin->page_is_visible($page) == false) {
455
				if($page['visibility'] != 'registered') {
456
					continue;
457
				} else { // page: registered, user: access denied
458
					$func_vars['page_description'] = 'registered';
459
				}
460
			}
461
			if($admin->page_is_active($page) == false) {
462
				continue;
463
			}
464
			$text = $func_vars['page_title'].$divider
465
				.$func_vars['page_menu_title'].$divider
466
				.($cfg_search_description?$func_vars['page_description']:"").$divider
467
				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
468
			$mod_vars = array(
469
				'page_link' => $func_vars['page_link'],
470
				'page_link_target' => "",
471
				'page_title' => $func_vars['page_title'],
472
				'page_description' => $func_vars['page_description'],
473
				'page_modified_when' => $func_vars['page_modified_when'],
474
				'page_modified_by' => $func_vars['page_modified_by'],
475
				'text' => $text,
476
				'max_excerpt_num' => $func_vars['default_max_excerpt']
477
			);
478
			if(print_excerpt2($mod_vars, $func_vars)) {
479
				$pages_listed[$page['page_id']] = true;
480
			}
481
		}
482
	}
483

    
484
	// Now use the old method for pages not displayed by the new method above
485
	// in case someone has old modules without search.php.
486

    
487
	// Get modules
488
	$table_search = TABLE_PREFIX."search";
489
	$table_sections = TABLE_PREFIX."sections";
490
	$get_modules = $database->query("
491
		SELECT DISTINCT s.value, s.extra
492
		FROM $table_search AS s INNER JOIN $table_sections AS sec
493
			ON s.value = sec.module
494
		WHERE s.name = 'module'
495
	");
496
	$modules = array();
497
	if($get_modules->numRows() > 0) {
498
		while($module = $get_modules->fetchRow()) {
499
			$modules[] = $module; // $modules in an array of arrays
500
		}
501
	}
502
	// sort module search-order
503
	// get the modules from $search_module_order first ...
504
	$sorted_modules = array();
505
	$m = count($modules);
506
	$search_modules = explode(',', $search_module_order);
507
	foreach($search_modules AS $item) {
508
		$item = trim($item);
509
		for($i=0; $i < $m; $i++) {
510
			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
511
				$sorted_modules[] = $modules[$i];
512
				unset($modules[$i]);
513
				break;
514
			}
515
		}
516
	}
517
	// ... then add the rest
518
	foreach($modules AS $item) {
519
		$sorted_modules[] = $item;
520
	}
521

    
522
	if($cfg_enable_old_search) {
523
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
524
		foreach($sorted_modules AS $module) {
525
			$query_start = '';
526
			$query_body = '';
527
			$query_end = '';
528
			$prepared_query = '';
529
			// Get module name
530
			$module_name = $module['value'];
531
			if(!isset($seen_pages[$module_name])) {
532
				$seen_pages[$module_name]=array();
533
			}
534
			// skip module 'code' - it doesn't make sense to search in a code section
535
			if($module_name=="code")
536
				continue;
537
			// Get fields to use for title, link, etc.
538
			$fields = unserialize($module['extra']);
539
			// Get query start
540
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
541
			if($get_query_start->numRows() > 0) {
542
				// Fetch query start
543
				$fetch_query_start = $get_query_start->fetchRow();
544
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
545
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
546
			}
547
			// Get query end
548
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
549
			if($get_query_end->numRows() > 0) {
550
				// Fetch query end
551
				$fetch_query_end = $get_query_end->fetchRow();
552
				// Set query end
553
				$query_end = ($fetch_query_end['value']);
554
			}
555
			// Get query body
556
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
557
			if($get_query_body->numRows() > 0) {
558
				// Fetch query body
559
				$fetch_query_body = $get_query_body->fetchRow();
560
				// Prepare query body for execution by replacing {STRING} with the correct one
561
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
562
				// Loop through query body for each string, then combine with start and end
563
				$prepared_query = $query_start." ( ( ( ";
564
				$count = 0;
565
				foreach($search_normal_array AS $string) {
566
					if($count != 0) {
567
						$prepared_query .= " ) ".$logical_operator." ( ";
568
					}
569
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
570
					$count = $count+1;
571
				}
572
				$count=0;
573
				$prepared_query .= ' ) ) OR ( ( ';
574
				foreach($search_entities_array AS $string) {
575
					if($count != 0) {
576
						$prepared_query .= " ) ".$logical_operator." ( ";
577
					}
578
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
579
					$count = $count+1;
580
				}
581
				$prepared_query .= " ) ) ) ".$query_end;
582
	
583
				// Execute query
584
				$page_query = $database->query($prepared_query." ".$search_path_SQL);
585

    
586
				// Loop through queried items
587
				if($page_query->numRows() > 0) {
588
					while($page = $page_query->fetchRow()) {
589
						// Only show this page if it hasn't already been listed
590
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
591
							continue;
592
						}
593
						
594
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
595
						$p_table = TABLE_PREFIX."pages";
596
						$viewquery = $database->query("
597
							SELECT visibility, viewing_groups, viewing_users
598
							FROM $p_table
599
							WHERE page_id='{$page['page_id']}'
600
						");
601
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
602
						if($viewquery->numRows() > 0) {
603
							if($res = $viewquery->fetchRow()) {
604
								$visibility = $res['visibility'];
605
								$viewing_groups = $res['viewing_groups'];
606
								$viewing_users = $res['viewing_users'];
607
								if($visibility == 'deleted' || $visibility == 'none') {
608
									continue;
609
								}
610
								if($visibility == 'private') {
611
									if($admin->page_is_visible(array(
612
										'page_id'=>$page[$fields['page_id']],
613
										'visibility' =>$visibility,
614
										'viewing_groups'=>$viewing_groups,
615
										'viewing_users'=>$viewing_users
616
									)) == false) {
617
										continue;
618
									}
619
								}
620
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
621
									continue;
622
								}
623
							}
624
						}
625
	
626
						// Get page link
627
						$link = page_link($page['link']);
628
						// Add search string for highlighting
629
						if ($match!='exact') {
630
							$sstring = implode(" ", $search_normal_array);
631
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
632
						} else {
633
							$sstring = strtr($search_normal_array[0], " ", "_");
634
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
635
						}
636
						// Set vars to be replaced by values
637
						if(!isset($page['description'])) { $page['description'] = ""; }
638
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
639
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
640
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
641
						if($page['modified_when'] > 0) {
642
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
643
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
644
						} else {
645
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
646
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
647
						}
648
						$excerpt="";
649
						if($cfg_show_description == 0) {
650
							$page['description'] = "";
651
						}
652
						if(isset($page['menu_title'])) $rep_title = $page['menu_title'];
653
						else $rep_title = $page['page_title'];
654
						$values = array($link, $rep_title, $page['description'], $users[$page['modified_by']]['username'], $users[$page['modified_by']]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']), $excerpt);
655
						// Show loop code with vars replaced by values
656
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
657
						// Say that this page has been listed
658
						$seen_pages[$module_name][$page['page_id']] = true;
659
						$pages_listed[$page['page_id']] = true;
660
					}
661
				}
662
			}
663
		}
664
	}
665

    
666
	// Say no items found if we should
667
	if(count($pages_listed) == 0) {
668
		echo $search_no_results;
669
	}
670
} else {
671
	echo $search_no_results;
672
}
673

    
674
// Show search results_footer
675
echo $search_results_footer;
676
// Show search footer
677
echo $search_footer;
678

    
679
?>
(2-2/4)