Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 761 2008-03-24 14:54:24Z 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
// this is for testing only
29
$overall_start_time = microtime(true);
30
//xdebug_start_trace();
31
//xdebug_enable();
32

    
33
if(!defined('WB_URL')) { 
34
	header('Location: index.php');
35
	exit(0);
36
}
37

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

    
41
// Check if search is enabled
42
if(SHOW_SEARCH != true) {
43
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
44
	return;
45
}
46

    
47
// search-module-extension: get helper-functions
48
require_once(WB_PATH.'/search/search_modext.php');
49
// search-module-extension: Get "search.php" for each module, if present
50
// looks in modules/module/ and modules/module_searchext/
51
$search_funcs = array();
52
$query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
53
if($query->numRows() > 0) {
54
	while($module = $query->fetchRow()) {
55
		$func_name = $module['directory']."_search";
56
		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
57
		if(!file_exists($file)) {
58
			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
59
			if(!file_exists($file)) {
60
				$file='';
61
			}
62
		}
63
		if($file!='') {
64
			include_once($file);
65
			if(function_exists($module['directory']."_search")) {
66
				$search_funcs[$module['directory']] = $func_name;
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
// Replace vars in search settings with values
202
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
203
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
204
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
205
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
206
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
207
$search_module_order = $fetch_module_order['value'];
208

    
209
// check $search_max_excerpt
210
if(!is_numeric($search_max_excerpt)) {
211
	$search_max_excerpt = 15;
212
}
213

    
214
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
215
$all_checked = '';
216
$any_checked = '';
217
$exact_checked = '';
218
$search_normal_array = array();
219
$search_entities_array = array();
220
if($match != 'exact') {
221
	// Split string into array with explode() function
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
	// Split $string_entities, too
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
	if ($match == 'any') {
238
		$any_checked = ' checked="checked"';
239
		$logical_operator = ' OR';
240
	} else {
241
		$all_checked = ' checked="checked"';
242
		$logical_operator = ' AND';
243
	}
244
} else {
245
	$exact_checked = ' checked="checked"';
246
	$exact_string=$search_normal_string;
247
	$search_normal_array[]=$exact_string;
248
	$exact_string=$search_entities_string;
249
	$search_entities_array[]=$exact_string;
250
}	
251
// make an extra copy of search-string for use in a regex and another one for url
252
require_once(WB_PATH.'/search/search_convert.php');
253
$search_words = array();
254
foreach ($search_normal_array AS $str) {
255
	$str = strtr($str, $string_ul_umlauts);
256
	// special-feature: '|' means word-boundary (\b). Searching for 'the|' will find the, but not thema.
257
	// this doesn't work correctly for unicode-chars: '|test' will work, but '|über' not.
258
	$str = strtr($str, array('\\|'=>'\b'));
259
	$search_words[] = $str;
260
}
261
$search_url_array=explode(' ', $search_url_string);
262

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

    
271
// Show search header
272
echo $search_header;
273
// Show search results_header
274
echo $search_results_header;
275

    
276
// Work-out if the user has already entered their details or not
277
if($search_normal_string != '') {
278

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

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

    
309
	// First, use an alternative search-method, without sql's 'LIKE'.
310
	// 'LIKE' won't find upper/lower-variants of umlauts, cyrillic or greek chars without propperly set setlocale();
311
	// and even if setlocale() is set, it won't work for multi-linguale sites.
312
	// Use the search-module-extension instead.
313
	// This is somewhat slower than the orginial method.
314
	$seen_pages = array(); // seen pages per module.
315
	$pages_listed = array(); // seen pages.
316
	foreach($sorted_modules AS $module_name) {
317
		$seen_pages[$module_name] = array();
318
		if(!isset($search_funcs[$module_name])) {
319
			continue; // there is no search_func for this module
320
		}
321
		// get each section for $module_name
322
		$table_s = TABLE_PREFIX."sections";	
323
		$table_p = TABLE_PREFIX."pages";
324
		$sections_query = $database->query("
325
			SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
326
			       p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
327
			       p.visibility, p.viewing_groups, p.viewing_users
328
			FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
329
			WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL
330
			ORDER BY s.section_id, s.position ASC
331
		");
332
		if($sections_query->numRows() > 0) {
333
			while($res = $sections_query->fetchRow()) {
334
				// Only show this section if it is not "out of publication-date"
335
				$now = time();
336
				if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
337
					$now>$res['publ_start'] && $res['publ_end']==0) ) {
338
					continue;
339
				}
340
				$search_func_vars = array(
341
					'database' => $database,
342
					'page_id' => $res['page_id'],
343
					'section_id' => $res['section_id'],
344
					'page_title' => $res['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
345
					'page_menu_title' => $res['page_title'],
346
					'page_description' => ($cfg_show_description?$res['description']:""),
347
					'page_keywords' => $res['keywords'],
348
					'page_link' => $res['link'],
349
					'page_modified_when' => $res['modified_when'],
350
					'page_modified_by' => $res['modified_by'],
351
					'users' => $users,
352
					'search_words' => $search_words, // needed for preg_match_all
353
					'search_match' => $match,
354
					'search_url_array' => $search_url_array, // needed for url-string only
355
					'results_loop_string' => $fetch_results_loop['value'],
356
					'default_max_excerpt' => $search_max_excerpt,
357
					'enable_flush' => $cfg_enable_flush
358
				);
359
				// Only show this page if we are allowed to see it
360
				if($admin->page_is_visible($res) == false) {
361
					if($res['visibility'] == 'registered') { // don't show excerpt
362
						$search_func_vars['default_max_excerpt'] = 0;
363
						$search_func_vars['page_description'] = $TEXT['REGISTERED'];
364
					} else { // private
365
						continue;
366
					}
367
				}
368
				$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
369
				if($uf_res) {
370
					$pages_listed[$res['page_id']] = true;
371
					$seen_pages[$module_name][$res['page_id']] = true;
372
				} else {
373
					$seen_pages[$module_name][$res['page_id']] = true;
374
				}
375
			}
376
		}
377
	}
378

    
379
	// Search page details only, such as description, keywords, etc, but only of unseen pages.
380
	$max_excerpt_num = 0; // we don't want excerpt here
381
	$divider = ".";
382
	$table = TABLE_PREFIX."pages";
383
	$query_pages = $database->query("
384
		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
385
		       visibility, viewing_groups, viewing_users
386
		FROM $table
387
		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL"
388
	);
389
	if($query_pages->numRows() > 0) {
390
		while($page = $query_pages->fetchRow()) {
391
			if (isset($pages_listed[$page['page_id']])) {
392
				continue;
393
			}
394
			$func_vars = array(
395
				'database' => $database,
396
				'page_id' => $page['page_id'],
397
				'page_title' => $page['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
398
				'page_menu_title' => $page['page_title'],
399
				'page_description' => ($cfg_show_description?$page['description']:""),
400
				'page_keywords' => $page['keywords'],
401
				'page_link' => $page['link'],
402
				'page_modified_when' => $page['modified_when'],
403
				'page_modified_by' => $page['modified_by'],
404
				'users' => $users,
405
				'search_words' => $search_words, // needed for preg_match_all
406
				'search_match' => $match,
407
				'search_url_array' => $search_url_array, // needed for url-string only
408
				'results_loop_string' => $fetch_results_loop['value'],
409
				'default_max_excerpt' => $max_excerpt_num,
410
				'enable_flush' => $cfg_enable_flush
411
			);
412
			// Only show this page if we are allowed to see it
413
			if($admin->page_is_visible($page) == false) {
414
				if($page['visibility'] != 'registered') {
415
					continue;
416
				} else { // page: registered, user: access denied
417
					$func_vars['page_description'] = 'registered';
418
				}
419
			}
420
			if($admin->page_is_active($page) == false) {
421
				continue;
422
			}
423
			$text = $func_vars['page_title'].$divider
424
				.$func_vars['page_menu_title'].$divider
425
				.($cfg_search_description?$func_vars['page_description']:"").$divider
426
				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
427
			$mod_vars = array(
428
				'page_link' => $func_vars['page_link'],
429
				'page_link_target' => "",
430
				'page_title' => $func_vars['page_title'],
431
				'page_description' => $func_vars['page_description'],
432
				'page_modified_when' => $func_vars['page_modified_when'],
433
				'page_modified_by' => $func_vars['page_modified_by'],
434
				'text' => $text,
435
				'max_excerpt_num' => $func_vars['default_max_excerpt']
436
			);
437
			if(print_excerpt2($mod_vars, $func_vars)) {
438
				$pages_listed[$page['page_id']] = true;
439
			}
440
		}
441
	}
442

    
443
	// Now use the old method for pages not displayed by the new method above
444
	// in case someone has old modules without search.php.
445

    
446
	// Get modules
447
	$table_search = TABLE_PREFIX."search";
448
	$table_sections = TABLE_PREFIX."sections";
449
	$get_modules = $database->query("
450
		SELECT DISTINCT s.value, s.extra
451
		FROM $table_search AS s INNER JOIN $table_sections AS sec
452
			ON s.value = sec.module
453
		WHERE s.name = 'module'
454
	");
455
	$modules = array();
456
	if($get_modules->numRows() > 0) {
457
		while($module = $get_modules->fetchRow()) {
458
			$modules[] = $module; // $modules in an array of arrays
459
		}
460
	}
461
	// sort module search-order
462
	// get the modules from $search_module_order first ...
463
	$sorted_modules = array();
464
	$m = count($modules);
465
	$search_modules = explode(',', $search_module_order);
466
	foreach($search_modules AS $item) {
467
		$item = trim($item);
468
		for($i=0; $i < $m; $i++) {
469
			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
470
				$sorted_modules[] = $modules[$i];
471
				unset($modules[$i]);
472
				break;
473
			}
474
		}
475
	}
476
	// ... then add the rest
477
	foreach($modules AS $item) {
478
		$sorted_modules[] = $item;
479
	}
480

    
481
	if($cfg_enable_old_search) {
482
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
483
		foreach($sorted_modules AS $module) {
484
			$query_start = '';
485
			$query_body = '';
486
			$query_end = '';
487
			$prepared_query = '';
488
			// Get module name
489
			$module_name = $module['value'];
490
			if(!isset($seen_pages[$module_name])) {
491
				$seen_pages[$module_name]=array();
492
			}
493
			// skip module 'code' - it doesn't make sense to search in a code section
494
			if($module_name=="code")
495
				continue;
496
			// Get fields to use for title, link, etc.
497
			$fields = unserialize($module['extra']);
498
			// Get query start
499
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
500
			if($get_query_start->numRows() > 0) {
501
				// Fetch query start
502
				$fetch_query_start = $get_query_start->fetchRow();
503
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
504
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
505
			}
506
			// Get query end
507
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
508
			if($get_query_end->numRows() > 0) {
509
				// Fetch query end
510
				$fetch_query_end = $get_query_end->fetchRow();
511
				// Set query end
512
				$query_end = ($fetch_query_end['value']);
513
			}
514
			// Get query body
515
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
516
			if($get_query_body->numRows() > 0) {
517
				// Fetch query body
518
				$fetch_query_body = $get_query_body->fetchRow();
519
				// Prepare query body for execution by replacing {STRING} with the correct one
520
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
521
				// Loop through query body for each string, then combine with start and end
522
				$prepared_query = $query_start." ( ( ( ";
523
				$count = 0;
524
				foreach($search_normal_array AS $string) {
525
					if($count != 0) {
526
						$prepared_query .= " ) ".$logical_operator." ( ";
527
					}
528
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
529
					$count = $count+1;
530
				}
531
				$count=0;
532
				$prepared_query .= ' ) ) OR ( ( ';
533
				foreach($search_entities_array AS $string) {
534
					if($count != 0) {
535
						$prepared_query .= " ) ".$logical_operator." ( ";
536
					}
537
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
538
					$count = $count+1;
539
				}
540
				$prepared_query .= " ) ) ) ".$query_end;
541
	
542
				// Execute query
543
				$page_query = $database->query($prepared_query." ".$search_path_SQL);
544

    
545
				// Loop through queried items
546
				if($page_query->numRows() > 0) {
547
					while($page = $page_query->fetchRow()) {
548
						// Only show this page if it hasn't already been listed
549
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
550
							continue;
551
						}
552
						
553
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
554
						$p_table = TABLE_PREFIX."pages";
555
						$viewquery = $database->query("
556
							SELECT visibility, viewing_groups, viewing_users
557
							FROM $p_table
558
							WHERE page_id='{$page['page_id']}'
559
						");
560
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
561
						if($viewquery->numRows() > 0) {
562
							if($res = $viewquery->fetchRow()) {
563
								$visibility = $res['visibility'];
564
								$viewing_groups = $res['viewing_groups'];
565
								$viewing_users = $res['viewing_users'];
566
								if($visibility == 'deleted' || $visibility == 'none') {
567
									continue;
568
								}
569
								if($visibility == 'private') {
570
									if($admin->page_is_visible(array(
571
										'page_id'=>$page[$fields['page_id']],
572
										'visibility' =>$visibility,
573
										'viewing_groups'=>$viewing_groups,
574
										'viewing_users'=>$viewing_users
575
									)) == false) {
576
										continue;
577
									}
578
								}
579
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
580
									continue;
581
								}
582
							}
583
						}
584
	
585
						// Get page link
586
						$link = page_link($page['link']);
587
						// Add search string for highlighting
588
						if ($match!='exact') {
589
							$sstring = implode(" ", $search_normal_array);
590
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
591
						} else {
592
							$sstring = strtr($search_normal_array[0], " ", "_");
593
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
594
						}
595
						// Set vars to be replaced by values
596
						if(!isset($page['description'])) { $page['description'] = ""; }
597
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
598
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
599
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
600
						if($page['modified_when'] > 0) {
601
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
602
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
603
						} else {
604
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
605
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
606
						}
607
						$excerpt="";
608
						if($cfg_show_description == 0) {
609
							$page['description'] = "";
610
						}
611
						if(isset($page['menu_title'])) $rep_title = $page['menu_title'];
612
						else $rep_title = $page['page_title'];
613
						$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);
614
						// Show loop code with vars replaced by values
615
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
616
						// Say that this page has been listed
617
						$seen_pages[$module_name][$page['page_id']] = true;
618
						$pages_listed[$page['page_id']] = true;
619
					}
620
				}
621
			}
622
		}
623
	}
624

    
625
	// Say no items found if we should
626
	if(count($pages_listed) == 0) {
627
		echo $search_no_results;
628
	}
629
} else {
630
	echo $search_no_results;
631
}
632

    
633
// Show search results_footer
634
echo $search_results_footer;
635
// Show search footer
636
echo $search_footer;
637

    
638
$overall_end_time = microtime(true); // for testing only
639
$time=$overall_end_time-$overall_start_time; print "<br />Timings - Overall: $time<br />";
640
//xdebug_stop_trace();
641
//echo "<hr />"."peak memory-usage: ".xdebug_peak_memory_usage()."<br />"."time: ".xdebug_time_index()."<br />";
642

    
643
?>
(2-2/4)