Project

General

Profile

1
<?php
2
/*
3
*
4
*                       About WebsiteBaker
5
*
6
* Website Baker is a PHP-based Content Management System (CMS)
7
* designed with one goal in mind: to enable its users to produce websites
8
* with ease.
9
*
10
*                       LICENSE INFORMATION
11
*
12
* WebsiteBaker is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU General Public License
14
* as published by the Free Software Foundation; either version 2
15
* of the License, or (at your option) any later version.
16
*
17
* WebsiteBaker is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
* See the GNU General Public License for more details.
21
*
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
*
26
*                   WebsiteBaker Extra Information
27
*
28
*
29
*/
30
/**
31
 *
32
 * @category        frontend
33
 * @package         search
34
 * @author          WebsiteBaker Project
35
 * @copyright       2004-2009, Ryan Djurovich
36
 * @copyright       2009-2010, Website Baker Org. e.V.
37
 * @link			http://www.websitebaker2.org/
38
 * @license         http://www.gnu.org/licenses/gpl.html
39
 * @platform        WebsiteBaker 2.8.x
40
 * @requirements    PHP 4.3.4 and higher
41
 * @version         $Id: search.php 1268 2010-01-22 17:21:02Z Luisehahne $
42
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/search/search.php $
43
 * @lastmodified    $Date: 2010-01-22 18:21:02 +0100 (Fri, 22 Jan 2010) $
44
 *
45
 */
46

    
47
if(!defined('WB_URL')) {
48
	header('Location: index.php');
49
	exit(0);
50
}
51

    
52
// Check if search is enabled
53
if(SHOW_SEARCH != true) {
54
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
55
	return;
56
}
57

    
58
// Include the WB functions file
59
require_once(WB_PATH.'/framework/functions.php');
60

    
61
// Get search settings
62
$table=TABLE_PREFIX.'search';
63
$query = $database->query("SELECT value FROM $table WHERE name = 'header' LIMIT 1");
64
$fetch_header = $query->fetchRow();
65
$query = $database->query("SELECT value FROM $table WHERE name = 'footer' LIMIT 1");
66
$fetch_footer = $query->fetchRow();
67
$query = $database->query("SELECT value FROM $table WHERE name = 'results_header' LIMIT 1");
68
$fetch_results_header = $query->fetchRow();
69
$query = $database->query("SELECT value FROM $table WHERE name = 'results_footer' LIMIT 1");
70
$fetch_results_footer = $query->fetchRow();
71
$query = $database->query("SELECT value FROM $table WHERE name = 'results_loop' LIMIT 1");
72
$fetch_results_loop = $query->fetchRow();
73
$query = $database->query("SELECT value FROM $table WHERE name = 'no_results' LIMIT 1");
74
$fetch_no_results = $query->fetchRow();
75
$query = $database->query("SELECT value FROM $table WHERE name = 'module_order' LIMIT 1");
76
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value']='faqbaker,manual,wysiwyg'; }
77
$search_module_order = $res['value'];
78
$query = $database->query("SELECT value FROM $table WHERE name = 'max_excerpt' LIMIT 1");
79
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '15'; }
80
$search_max_excerpt = (int)($res['value']);
81
if(!is_numeric($search_max_excerpt)) { $search_max_excerpt = 15; }
82
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_show_description' LIMIT 1");
83
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
84
if($res['value'] == 'false') { $cfg_show_description = false; } else { $cfg_show_description = true; }
85
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_description' LIMIT 1");
86
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
87
if($res['value'] == 'false') { $cfg_search_description = false; } else { $cfg_search_description = true; }
88
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_search_keywords' LIMIT 1");
89
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
90
if($res['value'] == 'false') { $cfg_search_keywords = false; } else { $cfg_search_keywords = true; }
91
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_old_search' LIMIT 1");
92
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'true'; }
93
if($res['value'] == 'false') { $cfg_enable_old_search = false; } else { $cfg_enable_old_search = true; }
94
$query = $database->query("SELECT value FROM $table WHERE name = 'cfg_enable_flush' LIMIT 1");
95
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = 'false'; }
96
if($res['value'] == 'false') { $cfg_enable_flush = false; } else { $cfg_enable_flush = true; }
97
$query = $database->query("SELECT value FROM $table WHERE name = 'time_limit' LIMIT 1"); // time-limit per module
98
if($query->numRows() > 0) { $res = $query->fetchRow(); } else { $res['value'] = '0'; }
99
$search_time_limit = (int)($res['value']);
100
if($search_time_limit < 1) $search_time_limit = 0;
101

    
102
// search-module-extension: get helper-functions
103
require_once(WB_PATH.'/search/search_modext.php');
104
// search-module-extension: Get "search.php" for each module, if present
105
// looks in modules/module/ and modules/module_searchext/
106
$search_funcs = array();$search_funcs['__before'] = array();$search_funcs['__after'] = array();
107
$query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
108
if($query->numRows() > 0) {
109
	while($module = $query->fetchRow()) {
110
		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
111
		if(!file_exists($file)) {
112
			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
113
			if(!file_exists($file)) {
114
				$file='';
115
			}
116
		}
117
		if($file!='') {
118
			include_once($file);
119
			if(function_exists($module['directory']."_search")) {
120
				$search_funcs[$module['directory']] = $module['directory']."_search";
121
			}
122
			if(function_exists($module['directory']."_search_before")) {
123
				$search_funcs['__before'][] = $module['directory']."_search_before";
124
			}
125
			if(function_exists($module['directory']."_search_after")) {
126
				$search_funcs['__after'][] = $module['directory']."_search_after";
127
			}
128
		}
129
	}
130
}
131

    
132
// Get list of usernames and display names
133
$query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
134
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
135
if($query->numRows() > 0) {
136
	while($user = $query->fetchRow()) {
137
		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
138
	}
139
}
140

    
141
// Get search language, used for special umlaut handling (DE: ß=ss, ...)
142
$search_lang = '';
143
if(isset($_REQUEST['search_lang'])) {
144
	$search_lang = $_REQUEST['search_lang'];
145
	if(!preg_match('~^[A-Z]{2}$~', $search_lang))
146
		$search_lang = LANGUAGE;
147
} else {
148
	$search_lang = LANGUAGE;
149
}
150

    
151
// Get the path to search into. Normally left blank
152
// 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"
153
// Add a '%' in front of each path to get SQL: LIKE "%/path%"
154
/* possible values:
155
 * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
156
 * - a single path not to search into: "-/help" - search all, exclude /help...
157
 * - a bunch of alternative pathes: "/en/,%/machinery/,/docs/" - alternatives paths, seperated by comma
158
 * - a bunch of paths to exclude: "-/about,%/info,/jp/,/light" - search all, exclude these.
159
 * These different styles can't be mixed.
160
 */
161
// ATTN: in wb2.7.0 "/en/" matched all links with "/en/" somewhere in the link: "/info/en/intro.php", "/en/info.php", ...
162
// since wb2.7.1 "/en/" matches only links _starting_  with "/en/": "/en/intro/info.php"
163
// use "%/en/" (or "%/en/, %/info", ...) to get the old behavior
164
$search_path_SQL = '';
165
$search_path = '';
166
if(isset($_REQUEST['search_path'])) {
167
	$search_path = addslashes(htmlspecialchars(strip_tags($wb->strip_slashes($_REQUEST['search_path'])), ENT_QUOTES));
168
	if(!preg_match('~^%?[-a-zA-Z0-9_,/ ]+$~', $search_path))
169
		$search_path = '';
170
	if($search_path != '') {
171
		$search_path_SQL = 'AND ( ';
172
		$not = '';
173
		$op = 'OR';
174
		if($search_path[0] == '-') {
175
			$not = 'NOT';
176
			$op = 'AND';
177
			$paths = explode(',', substr($search_path, 1) );
178
		} else {
179
			$paths = explode(',',$search_path);
180
		}
181
		$i=0;
182
		foreach($paths as $p) {
183
			if($i++ > 0) {
184
				$search_path_SQL .= ' $op';
185
			}
186
			$search_path_SQL .= " link $not LIKE '".$p."%'";			
187
		}
188
		$search_path_SQL .= ' )';
189
	}
190
}
191

    
192
// use page_languages?
193
if(PAGE_LANGUAGES) {
194
	$table = TABLE_PREFIX."pages";
195
	$search_language_SQL_t = "AND $table.`language` = '".LANGUAGE."'";
196
	$search_language_SQL = "AND `language` = '".LANGUAGE."'";
197
} else {
198
	$search_language_SQL_t = '';
199
	$search_language_SQL = '';
200
}
201

    
202
// Get the search type
203
$match = '';
204
if(isset($_REQUEST['match'])) {
205
	if($_REQUEST['match']=='any') $match = 'any';
206
	elseif($_REQUEST['match']=='all') $match = 'all';
207
	elseif($_REQUEST['match']=='exact') $match = 'exact';
208
	else $match = 'all';
209
} else {
210
	$match = 'all';
211
}
212

    
213
// Get search string
214
$search_normal_string = '';
215
$search_entities_string = ''; // for SQL's LIKE
216
$search_display_string = ''; // for displaying
217
$search_url_string = ''; // for $_GET -- ATTN: unquoted! Will become urldecoded later
218
$string = '';
219
if(isset($_REQUEST['string']))
220
{
221
	if($match!='exact') // $string will be cleaned below 
222
    {
223
		$string=str_replace(',', '', $_REQUEST['string']);
224
	} else {
225
		$string=$_REQUEST['string'];
226
	}
227
    // redo possible magic quotes
228
    $string = $wb->strip_slashes($string);
229
    $string = preg_replace('/[ \r\n\t]+/', ' ', $string);
230
    $string = trim($string);
231
	// remove some bad chars
232
	$string = str_replace ( array('[[',']]'),'', $string);
233
	$string = preg_replace('/(^|\s+)[|.]+(?=\s+|$)/', '', $string);
234
	$search_display_string = htmlspecialchars($string);
235
	$search_entities_string = addslashes(umlauts_to_entities(htmlspecialchars($string)));
236
	// mySQL needs four backslashes to match one in LIKE comparisons)
237
	$search_entities_string = str_replace('\\\\', '\\\\\\\\', $search_entities_string);
238
	// convert string to utf-8
239
	$string = entities_to_umlauts($string, 'UTF-8');
240
	$search_url_string = $string;
241
	$string = preg_quote($string);
242
	// quote ' " and /  -we need quoted / for regex
243
	$search_normal_string = str_replace(array('\'','"','/'), array('\\\'','\"','\/'), $string);
244
}
245
// make arrays from the search_..._strings above
246
if($match == 'exact')
247
	$search_url_array[] = $search_url_string;
248
else
249
	$search_url_array = explode(' ', $search_url_string);
250
$search_normal_array = array();
251
$search_entities_array = array();
252
if($match == 'exact') {
253
	$search_normal_array[]=$search_normal_string;
254
	$search_entities_array[]=$search_entities_string;
255
} else {
256
	$exploded_string = explode(' ', $search_normal_string);
257
	// Make sure there is no blank values in the array
258
	foreach($exploded_string AS $each_exploded_string) {
259
		if($each_exploded_string != '') {
260
			$search_normal_array[] = $each_exploded_string;
261
		}
262
	}
263
	$exploded_string = explode(' ', $search_entities_string);
264
	// Make sure there is no blank values in the array
265
	foreach($exploded_string AS $each_exploded_string) {
266
		if($each_exploded_string != '') {
267
			$search_entities_array[] = $each_exploded_string;
268
		}
269
	}
270
}
271
// make an extra copy of search_normal_array for use in regex
272
require(WB_PATH.'/search/search_convert.php');
273
$search_words = array();
274
foreach($search_normal_array AS $str) {
275
	$str = str_replace($string_ul_umlaut, $string_ul_regex, $str);
276
	$search_words[] = $str;
277
}
278

    
279
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
280
$all_checked = '';
281
$any_checked = '';
282
$exact_checked = '';
283
if ($match == 'any') {
284
	$any_checked = ' checked="checked"';
285
	$logical_operator = ' OR';
286
} elseif($match == 'all') {
287
	$all_checked = ' checked="checked"';
288
	$logical_operator = ' AND';
289
} else {
290
	$exact_checked = ' checked="checked"';
291
}
292

    
293
// Replace vars in search settings with values
294
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
295
$values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
296
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
297
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
298
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
299

    
300
// Do extra vars/values replacement
301
$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]');
302
$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);
303
$search_header = str_replace($vars, $values, ($fetch_header['value']));
304
$vars = array('[TEXT_NO_RESULTS]');
305
$values = array($TEXT['NO_RESULTS']);
306
$search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
307

    
308
/*
309
 * Start of output
310
 */
311

    
312
// Show search header
313
echo $search_header;
314
// Show search results_header
315
echo $search_results_header;
316

    
317
// Work-out if the user has already entered their details or not
318
if($search_normal_string != '') {
319

    
320
	// Get modules
321
	$table = TABLE_PREFIX."sections";
322
	$get_modules = $database->query("SELECT DISTINCT module FROM $table WHERE module != '' ");
323
	$modules = array();
324
	if($get_modules->numRows() > 0) {
325
		while($module = $get_modules->fetchRow()) {
326
			$modules[] = $module['module'];
327
		}
328
	}
329
	// sort module search-order
330
	// get the modules from $search_module_order first ...
331
	$sorted_modules = array();
332
	$m = count($modules);
333
	$search_modules = explode(',', $search_module_order);
334
	foreach($search_modules AS $item) {
335
		$item = trim($item);
336
		for($i=0; $i < $m; $i++) {
337
			if(isset($modules[$i]) && $modules[$i] == $item) {
338
				$sorted_modules[] = $modules[$i];
339
				unset($modules[$i]);
340
				break;
341
			}
342
		}
343
	}
344
	// ... then add the rest
345
	foreach($modules AS $item) {
346
		$sorted_modules[] = $item;
347
	}
348

    
349

    
350
	// Use the module's search-extensions.
351
	// This is somewhat slower than the orginial method.
352
	
353
	// call $search_funcs['__before'] first
354
	$search_func_vars = array(
355
		'database' => $database, // database-handle
356
		'page_id' => 0,
357
		'section_id' => 0,
358
		'page_title' => '',
359
		'page_menu_title' => '',
360
		'page_description' => '',
361
		'page_keywords' => '',
362
		'page_link' => '',
363
		'page_modified_when' => 0,
364
		'page_modified_by' => 0,
365
		'users' => $users, // array of known user-id/user-name
366
		'search_words' => $search_words, // array of strings, prepared for regex
367
		'search_match' => $match, // match-type
368
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
369
		'results_loop_string' => $fetch_results_loop['value'],
370
		'default_max_excerpt' => $search_max_excerpt,
371
		'time_limit' => $search_time_limit, // time-limit in secs
372
		'search_path' => $search_path // see docu
373
	);
374
	foreach($search_funcs['__before'] as $func) {
375
		$uf_res = call_user_func($func, $search_func_vars);
376
	}
377
	// now call module-based $search_funcs[]
378
	$seen_pages = array(); // seen pages per module.
379
	$pages_listed = array(); // seen pages.
380
	if($search_max_excerpt!=0) { // skip this search if $search_max_excerpt==0
381
		foreach($sorted_modules AS $module_name) {
382
			$start_time = time();	// get start-time to check time-limit; not very accurate, but ok
383
			$seen_pages[$module_name] = array();
384
			if(!isset($search_funcs[$module_name])) {
385
				continue; // there is no search_func for this module
386
			}
387
			// get each section for $module_name
388
			$table_s = TABLE_PREFIX."sections";	
389
			$table_p = TABLE_PREFIX."pages";
390
			$sections_query = $database->query("
391
				SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
392
							 p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
393
							 p.visibility, p.viewing_groups, p.viewing_users
394
				FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
395
				WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL $search_language_SQL
396
				ORDER BY s.page_id, s.position ASC
397
			");
398
			if($sections_query->numRows() > 0) {
399
				while($res = $sections_query->fetchRow()) {
400
					// check if time-limit is exceeded for this module
401
					if($search_time_limit > 0 && (time()-$start_time > $search_time_limit)) {
402
						break;
403
					}
404
					// Only show this section if it is not "out of publication-date"
405
					$now = time();
406
					if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
407
						$now>$res['publ_start'] && $res['publ_end']==0) ) {
408
						continue;
409
					}
410
					$search_func_vars = array(
411
						'database' => $database,
412
						'page_id' => $res['page_id'],
413
						'section_id' => $res['section_id'],
414
						'page_title' => $res['page_title'],
415
						'page_menu_title' => $res['menu_title'],
416
						'page_description' => ($cfg_show_description?$res['description']:""),
417
						'page_keywords' => $res['keywords'],
418
						'page_link' => $res['link'],
419
						'page_modified_when' => $res['modified_when'],
420
						'page_modified_by' => $res['modified_by'],
421
						'users' => $users,
422
						'search_words' => $search_words, // needed for preg_match
423
						'search_match' => $match,
424
						'search_url_array' => $search_url_array, // needed for url-string only
425
						'results_loop_string' => $fetch_results_loop['value'],
426
						'default_max_excerpt' => $search_max_excerpt,
427
						'enable_flush' => $cfg_enable_flush,
428
						'time_limit' => $search_time_limit // time-limit in secs
429
					);
430
					// Only show this page if we are allowed to see it
431
					if($admin->page_is_visible($res) == false) {
432
						if($res['visibility'] == 'registered') { // don't show excerpt
433
							$search_func_vars['default_max_excerpt'] = 0;
434
							$search_func_vars['page_description'] = $TEXT['REGISTERED'];
435
						} else { // private
436
							continue;
437
						}
438
					}
439
					$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
440
					if($uf_res) {
441
						$pages_listed[$res['page_id']] = true;
442
						$seen_pages[$module_name][$res['page_id']] = true;
443
					} else {
444
						$seen_pages[$module_name][$res['page_id']] = true;
445
					}
446
				}
447
			}
448
		}
449
	}
450
	// now call $search_funcs['__after']
451
	$search_func_vars = array(
452
		'database' => $database, // database-handle
453
		'page_id' => 0,
454
		'section_id' => 0,
455
		'page_title' => '',
456
		'page_menu_title' => '',
457
		'page_description' => '',
458
		'page_keywords' => '',
459
		'page_link' => '',
460
		'page_modified_when' => 0,
461
		'page_modified_by' => 0,
462
		'users' => $users, // array of known user-id/user-name
463
		'search_words' => $search_words, // array of strings, prepared for regex
464
		'search_match' => $match, // match-type
465
		'search_url_array' => $search_url_array, // array of strings from the original search-string. ATTN: strings are not quoted!
466
		'results_loop_string' => $fetch_results_loop['value'],
467
		'default_max_excerpt' => $search_max_excerpt,
468
		'time_limit' => $search_time_limit, // time-limit in secs
469
		'search_path' => $search_path // see docu
470
	);
471
	foreach($search_funcs['__after'] as $func) {
472
		$uf_res = call_user_func($func, $search_func_vars);
473
	}
474

    
475

    
476
	// Search page details only, such as description, keywords, etc, but only of unseen pages.
477
	$max_excerpt_num = 0; // we don't want excerpt here
478
	$divider = ".";
479
	$table = TABLE_PREFIX."pages";
480
	$query_pages = $database->query("
481
		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
482
		       visibility, viewing_groups, viewing_users
483
		FROM $table
484
		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL $search_language_SQL
485
	");
486
	if($query_pages->numRows() > 0) {
487
		while($page = $query_pages->fetchRow()) {
488
			if (isset($pages_listed[$page['page_id']])) {
489
				continue;
490
			}
491
			$func_vars = array(
492
				'database' => $database,
493
				'page_id' => $page['page_id'],
494
				'page_title' => $page['page_title'],
495
				'page_menu_title' => $page['menu_title'],
496
				'page_description' => ($cfg_show_description?$page['description']:""),
497
				'page_keywords' => $page['keywords'],
498
				'page_link' => $page['link'],
499
				'page_modified_when' => $page['modified_when'],
500
				'page_modified_by' => $page['modified_by'],
501
				'users' => $users,
502
				'search_words' => $search_words, // needed for preg_match_all
503
				'search_match' => $match,
504
				'search_url_array' => $search_url_array, // needed for url-string only
505
				'results_loop_string' => $fetch_results_loop['value'],
506
				'default_max_excerpt' => $max_excerpt_num,
507
				'enable_flush' => $cfg_enable_flush
508
			);
509
			// Only show this page if we are allowed to see it
510
			if($admin->page_is_visible($page) == false) {
511
				if($page['visibility'] != 'registered') {
512
					continue;
513
				} else { // page: registered, user: access denied
514
					$func_vars['page_description'] = $TEXT['REGISTERED'];
515
				}
516
			}
517
			if($admin->page_is_active($page) == false) {
518
				continue;
519
			}
520
			$text = $func_vars['page_title'].$divider
521
				.$func_vars['page_menu_title'].$divider
522
				.($cfg_search_description?$func_vars['page_description']:"").$divider
523
				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
524
			$mod_vars = array(
525
				'page_link' => $func_vars['page_link'],
526
				'page_link_target' => "",
527
				'page_title' => $func_vars['page_title'],
528
				'page_description' => $func_vars['page_description'],
529
				'page_modified_when' => $func_vars['page_modified_when'],
530
				'page_modified_by' => $func_vars['page_modified_by'],
531
				'text' => $text,
532
				'max_excerpt_num' => $func_vars['default_max_excerpt']
533
			);
534
			if(print_excerpt2($mod_vars, $func_vars)) {
535
				$pages_listed[$page['page_id']] = true;
536
			}
537
		}
538
	}
539

    
540
	// Now use the old method for pages not displayed by the new method above
541
	// in case someone has old modules without search.php.
542

    
543
	// Get modules
544
	$table_search = TABLE_PREFIX."search";
545
	$table_sections = TABLE_PREFIX."sections";
546
	$get_modules = $database->query("
547
		SELECT DISTINCT s.value, s.extra
548
		FROM $table_search AS s INNER JOIN $table_sections AS sec
549
			ON s.value = sec.module
550
		WHERE s.name = 'module'
551
	");
552
	$modules = array();
553
	if($get_modules->numRows() > 0) {
554
		while($module = $get_modules->fetchRow()) {
555
			$modules[] = $module; // $modules in an array of arrays
556
		}
557
	}
558
	// sort module search-order
559
	// get the modules from $search_module_order first ...
560
	$sorted_modules = array();
561
	$m = count($modules);
562
	$search_modules = explode(',', $search_module_order);
563
	foreach($search_modules AS $item) {
564
		$item = trim($item);
565
		for($i=0; $i < $m; $i++) {
566
			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
567
				$sorted_modules[] = $modules[$i];
568
				unset($modules[$i]);
569
				break;
570
			}
571
		}
572
	}
573
	// ... then add the rest
574
	foreach($modules AS $item) {
575
		$sorted_modules[] = $item;
576
	}
577

    
578
	if($cfg_enable_old_search) { // this is the old (wb <= 2.6.7) search-function
579
		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
580
		foreach($sorted_modules AS $module) {
581
			if(isset($seen_pages[$module['value']]) && count($seen_pages[$module['value']])>0) // skip modules handled by new search-func
582
				continue;
583
			$query_start = '';
584
			$query_body = '';
585
			$query_end = '';
586
			$prepared_query = '';
587
			// Get module name
588
			$module_name = $module['value'];
589
			if(!isset($seen_pages[$module_name])) {
590
				$seen_pages[$module_name]=array();
591
			}
592
			// skip module 'code' - it doesn't make sense to search in a code section
593
			if($module_name=="code")
594
				continue;
595
			// Get fields to use for title, link, etc.
596
			$fields = unserialize($module['extra']);
597
			// Get query start
598
			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
599
			if($get_query_start->numRows() > 0) {
600
				// Fetch query start
601
				$fetch_query_start = $get_query_start->fetchRow();
602
				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
603
				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
604
			}
605
			// Get query end
606
			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
607
			if($get_query_end->numRows() > 0) {
608
				// Fetch query end
609
				$fetch_query_end = $get_query_end->fetchRow();
610
				// Set query end
611
				$query_end = ($fetch_query_end['value']);
612
			}
613
			// Get query body
614
			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
615
			if($get_query_body->numRows() > 0) {
616
				// Fetch query body
617
				$fetch_query_body = $get_query_body->fetchRow();
618
				// Prepare query body for execution by replacing {STRING} with the correct one
619
				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
620
				// Loop through query body for each string, then combine with start and end
621
				$prepared_query = $query_start." ( ( ( ";
622
				$count = 0;
623
				foreach($search_normal_array AS $string) {
624
					if($count != 0) {
625
						$prepared_query .= " ) ".$logical_operator." ( ";
626
					}
627
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
628
					$count = $count+1;
629
				}
630
				$count=0;
631
				$prepared_query .= ' ) ) OR ( ( ';
632
				foreach($search_entities_array AS $string) {
633
					if($count != 0) {
634
						$prepared_query .= " ) ".$logical_operator." ( ";
635
					}
636
					$prepared_query .= str_replace('[STRING]', $string, $query_body);
637
					$count = $count+1;
638
				}
639
				$prepared_query .= " ) ) ) ".$query_end;
640
				// Execute query
641
				$page_query = $database->query($prepared_query." ".$search_path_SQL." ".$search_language_SQL_t);
642
				if(!$page_query) continue; // on error, skip the rest of the current loop iteration
643
				// Loop through queried items
644
				if($page_query->numRows() > 0) {
645
					while($page = $page_query->fetchRow()) {
646
						// Only show this page if it hasn't already been listed
647
						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
648
							continue;
649
						}
650
						
651
						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
652
						$p_table = TABLE_PREFIX."pages";
653
						$viewquery = $database->query("
654
							SELECT visibility, viewing_groups, viewing_users
655
							FROM $p_table
656
							WHERE page_id='{$page['page_id']}'
657
						");
658
						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
659
						if($viewquery->numRows() > 0) {
660
							if($res = $viewquery->fetchRow()) {
661
								$visibility = $res['visibility'];
662
								$viewing_groups = $res['viewing_groups'];
663
								$viewing_users = $res['viewing_users'];
664
								if($visibility == 'deleted' || $visibility == 'none') {
665
									continue;
666
								}
667
								if($visibility == 'private') {
668
									if($admin->page_is_visible(array(
669
										'page_id'=>$page[$fields['page_id']],
670
										'visibility' =>$visibility,
671
										'viewing_groups'=>$viewing_groups,
672
										'viewing_users'=>$viewing_users
673
									)) == false) {
674
										continue;
675
									}
676
								}
677
								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
678
									continue;
679
								}
680
							}
681
						}
682
	
683
						// Get page link
684
						$link = page_link($page['link']);
685
						// Add search string for highlighting
686
						if ($match!='exact') {
687
							$sstring = implode(" ", $search_normal_array);
688
							$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
689
						} else {
690
							$sstring = str_replace(" ", "_",$search_normal_array[0]);
691
							$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
692
						}
693
						// Set vars to be replaced by values
694
						if(!isset($page['description'])) { $page['description'] = ""; }
695
						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
696
						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
697
						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
698
						if($page['modified_when'] > 0) {
699
							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
700
							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
701
						} else {
702
							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
703
							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
704
						}
705
						$excerpt="";
706
						if($cfg_show_description == 0) {
707
							$page['description'] = "";
708
						}
709
						$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);
710
						// Show loop code with vars replaced by values
711
						echo str_replace($vars, $values, ($fetch_results_loop['value']));
712
						// Say that this page has been listed
713
						$seen_pages[$module_name][$page['page_id']] = true;
714
						$pages_listed[$page['page_id']] = true;
715
					}
716
				}
717
			}
718
		}
719
	}
720

    
721
	// Say no items found if we should
722
	if(count($pages_listed) == 0) {
723
		echo $search_no_results;
724
	}
725
} else {
726
	echo $search_no_results;
727
}
728

    
729
// Show search results_footer
730
echo $search_results_footer;
731
// Show search footer
732
echo $search_footer;
733

    
734
?>
(2-2/4)