1
|
<?php
|
2
|
|
3
|
// $Id: search.php 773 2008-03-26 17:41:44Z 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
|
'page_id' => 0,
|
324
|
'section_id' => 0,
|
325
|
'page_title' => '',
|
326
|
'page_menu_title' => '',
|
327
|
'page_description' => '',
|
328
|
'page_keywords' => '',
|
329
|
'page_link' => '',
|
330
|
'page_modified_when' => 0,
|
331
|
'page_modified_by' => 0,
|
332
|
'users' => $users, // array of known user-id/user-name
|
333
|
'search_words' => $search_words, // search-string, prepared for regex
|
334
|
'search_match' => $match, // match-type
|
335
|
'search_url_array' => $search_url_array, // original search-string. ATTN: string is not quoted!
|
336
|
'results_loop_string' => $fetch_results_loop['value'],
|
337
|
'default_max_excerpt' => $search_max_excerpt,
|
338
|
'time_limit' => $search_time_limit, // time-limit in secs
|
339
|
'search_path' => $search_path // see docu
|
340
|
);
|
341
|
foreach($search_funcs['__before'] as $func) {
|
342
|
$uf_res = call_user_func($func, $search_func_vars);
|
343
|
}
|
344
|
// now call module-based $search_funcs[]
|
345
|
$seen_pages = array(); // seen pages per module.
|
346
|
$pages_listed = array(); // seen pages.
|
347
|
foreach($sorted_modules AS $module_name) {
|
348
|
$start_time = time(); // get start-time to check time-limit; not very accurate, but ok
|
349
|
$seen_pages[$module_name] = array();
|
350
|
if(!isset($search_funcs[$module_name])) {
|
351
|
continue; // there is no search_func for this module
|
352
|
}
|
353
|
// get each section for $module_name
|
354
|
$table_s = TABLE_PREFIX."sections";
|
355
|
$table_p = TABLE_PREFIX."pages";
|
356
|
$sections_query = $database->query("
|
357
|
SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
|
358
|
p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
|
359
|
p.visibility, p.viewing_groups, p.viewing_users
|
360
|
FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
|
361
|
WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL
|
362
|
ORDER BY s.section_id, s.position ASC
|
363
|
");
|
364
|
if($sections_query->numRows() > 0) {
|
365
|
while($res = $sections_query->fetchRow()) {
|
366
|
// check if time-limit is exceeded for this module
|
367
|
if($search_time_limit > 0 && (time()-$start_time > $search_time_limit)) {
|
368
|
break;
|
369
|
}
|
370
|
// Only show this section if it is not "out of publication-date"
|
371
|
$now = time();
|
372
|
if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
|
373
|
$now>$res['publ_start'] && $res['publ_end']==0) ) {
|
374
|
continue;
|
375
|
}
|
376
|
$search_func_vars = array(
|
377
|
'database' => $database,
|
378
|
'page_id' => $res['page_id'],
|
379
|
'section_id' => $res['section_id'],
|
380
|
'page_title' => $res['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
|
381
|
'page_menu_title' => $res['page_title'],
|
382
|
'page_description' => ($cfg_show_description?$res['description']:""),
|
383
|
'page_keywords' => $res['keywords'],
|
384
|
'page_link' => $res['link'],
|
385
|
'page_modified_when' => $res['modified_when'],
|
386
|
'page_modified_by' => $res['modified_by'],
|
387
|
'users' => $users,
|
388
|
'search_words' => $search_words, // needed for preg_match_all
|
389
|
'search_match' => $match,
|
390
|
'search_url_array' => $search_url_array, // needed for url-string only
|
391
|
'results_loop_string' => $fetch_results_loop['value'],
|
392
|
'default_max_excerpt' => $search_max_excerpt,
|
393
|
'enable_flush' => $cfg_enable_flush
|
394
|
);
|
395
|
// Only show this page if we are allowed to see it
|
396
|
if($admin->page_is_visible($res) == false) {
|
397
|
if($res['visibility'] == 'registered') { // don't show excerpt
|
398
|
$search_func_vars['default_max_excerpt'] = 0;
|
399
|
$search_func_vars['page_description'] = $TEXT['REGISTERED'];
|
400
|
} else { // private
|
401
|
continue;
|
402
|
}
|
403
|
}
|
404
|
$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
|
405
|
if($uf_res) {
|
406
|
$pages_listed[$res['page_id']] = true;
|
407
|
$seen_pages[$module_name][$res['page_id']] = true;
|
408
|
} else {
|
409
|
$seen_pages[$module_name][$res['page_id']] = true;
|
410
|
}
|
411
|
}
|
412
|
}
|
413
|
}
|
414
|
// now call $search_funcs['__after']
|
415
|
$search_func_vars = array(
|
416
|
'database' => $database, // database-handle
|
417
|
'page_id' => 0,
|
418
|
'section_id' => 0,
|
419
|
'page_title' => '',
|
420
|
'page_menu_title' => '',
|
421
|
'page_description' => '',
|
422
|
'page_keywords' => '',
|
423
|
'page_link' => '',
|
424
|
'page_modified_when' => 0,
|
425
|
'page_modified_by' => 0,
|
426
|
'users' => $users, // array of known user-id/user-name
|
427
|
'search_words' => $search_words, // search-string, prepared for regex
|
428
|
'search_match' => $match, // match-type
|
429
|
'search_url_array' => $search_url_array, // original search-string. ATTN: string is not quoted!
|
430
|
'results_loop_string' => $fetch_results_loop['value'],
|
431
|
'default_max_excerpt' => $search_max_excerpt,
|
432
|
'time_limit' => $search_time_limit, // time-limit in secs
|
433
|
'search_path' => $search_path // see docu
|
434
|
);
|
435
|
foreach($search_funcs['__after'] as $func) {
|
436
|
$uf_res = call_user_func($func, $search_func_vars);
|
437
|
}
|
438
|
|
439
|
|
440
|
// Search page details only, such as description, keywords, etc, but only of unseen pages.
|
441
|
$max_excerpt_num = 0; // we don't want excerpt here
|
442
|
$divider = ".";
|
443
|
$table = TABLE_PREFIX."pages";
|
444
|
$query_pages = $database->query("
|
445
|
SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
|
446
|
visibility, viewing_groups, viewing_users
|
447
|
FROM $table
|
448
|
WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL"
|
449
|
);
|
450
|
if($query_pages->numRows() > 0) {
|
451
|
while($page = $query_pages->fetchRow()) {
|
452
|
if (isset($pages_listed[$page['page_id']])) {
|
453
|
continue;
|
454
|
}
|
455
|
$func_vars = array(
|
456
|
'database' => $database,
|
457
|
'page_id' => $page['page_id'],
|
458
|
'page_title' => $page['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
|
459
|
'page_menu_title' => $page['page_title'],
|
460
|
'page_description' => ($cfg_show_description?$page['description']:""),
|
461
|
'page_keywords' => $page['keywords'],
|
462
|
'page_link' => $page['link'],
|
463
|
'page_modified_when' => $page['modified_when'],
|
464
|
'page_modified_by' => $page['modified_by'],
|
465
|
'users' => $users,
|
466
|
'search_words' => $search_words, // needed for preg_match_all
|
467
|
'search_match' => $match,
|
468
|
'search_url_array' => $search_url_array, // needed for url-string only
|
469
|
'results_loop_string' => $fetch_results_loop['value'],
|
470
|
'default_max_excerpt' => $max_excerpt_num,
|
471
|
'enable_flush' => $cfg_enable_flush
|
472
|
);
|
473
|
// Only show this page if we are allowed to see it
|
474
|
if($admin->page_is_visible($page) == false) {
|
475
|
if($page['visibility'] != 'registered') {
|
476
|
continue;
|
477
|
} else { // page: registered, user: access denied
|
478
|
$func_vars['page_description'] = 'registered';
|
479
|
}
|
480
|
}
|
481
|
if($admin->page_is_active($page) == false) {
|
482
|
continue;
|
483
|
}
|
484
|
$text = $func_vars['page_title'].$divider
|
485
|
.$func_vars['page_menu_title'].$divider
|
486
|
.($cfg_search_description?$func_vars['page_description']:"").$divider
|
487
|
.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
|
488
|
$mod_vars = array(
|
489
|
'page_link' => $func_vars['page_link'],
|
490
|
'page_link_target' => "",
|
491
|
'page_title' => $func_vars['page_title'],
|
492
|
'page_description' => $func_vars['page_description'],
|
493
|
'page_modified_when' => $func_vars['page_modified_when'],
|
494
|
'page_modified_by' => $func_vars['page_modified_by'],
|
495
|
'text' => $text,
|
496
|
'max_excerpt_num' => $func_vars['default_max_excerpt']
|
497
|
);
|
498
|
if(print_excerpt2($mod_vars, $func_vars)) {
|
499
|
$pages_listed[$page['page_id']] = true;
|
500
|
}
|
501
|
}
|
502
|
}
|
503
|
|
504
|
// Now use the old method for pages not displayed by the new method above
|
505
|
// in case someone has old modules without search.php.
|
506
|
|
507
|
// Get modules
|
508
|
$table_search = TABLE_PREFIX."search";
|
509
|
$table_sections = TABLE_PREFIX."sections";
|
510
|
$get_modules = $database->query("
|
511
|
SELECT DISTINCT s.value, s.extra
|
512
|
FROM $table_search AS s INNER JOIN $table_sections AS sec
|
513
|
ON s.value = sec.module
|
514
|
WHERE s.name = 'module'
|
515
|
");
|
516
|
$modules = array();
|
517
|
if($get_modules->numRows() > 0) {
|
518
|
while($module = $get_modules->fetchRow()) {
|
519
|
$modules[] = $module; // $modules in an array of arrays
|
520
|
}
|
521
|
}
|
522
|
// sort module search-order
|
523
|
// get the modules from $search_module_order first ...
|
524
|
$sorted_modules = array();
|
525
|
$m = count($modules);
|
526
|
$search_modules = explode(',', $search_module_order);
|
527
|
foreach($search_modules AS $item) {
|
528
|
$item = trim($item);
|
529
|
for($i=0; $i < $m; $i++) {
|
530
|
if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
|
531
|
$sorted_modules[] = $modules[$i];
|
532
|
unset($modules[$i]);
|
533
|
break;
|
534
|
}
|
535
|
}
|
536
|
}
|
537
|
// ... then add the rest
|
538
|
foreach($modules AS $item) {
|
539
|
$sorted_modules[] = $item;
|
540
|
}
|
541
|
|
542
|
if($cfg_enable_old_search) {
|
543
|
$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
|
544
|
foreach($sorted_modules AS $module) {
|
545
|
$query_start = '';
|
546
|
$query_body = '';
|
547
|
$query_end = '';
|
548
|
$prepared_query = '';
|
549
|
// Get module name
|
550
|
$module_name = $module['value'];
|
551
|
if(!isset($seen_pages[$module_name])) {
|
552
|
$seen_pages[$module_name]=array();
|
553
|
}
|
554
|
// skip module 'code' - it doesn't make sense to search in a code section
|
555
|
if($module_name=="code")
|
556
|
continue;
|
557
|
// Get fields to use for title, link, etc.
|
558
|
$fields = unserialize($module['extra']);
|
559
|
// Get query start
|
560
|
$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
|
561
|
if($get_query_start->numRows() > 0) {
|
562
|
// Fetch query start
|
563
|
$fetch_query_start = $get_query_start->fetchRow();
|
564
|
// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
|
565
|
$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
|
566
|
}
|
567
|
// Get query end
|
568
|
$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
|
569
|
if($get_query_end->numRows() > 0) {
|
570
|
// Fetch query end
|
571
|
$fetch_query_end = $get_query_end->fetchRow();
|
572
|
// Set query end
|
573
|
$query_end = ($fetch_query_end['value']);
|
574
|
}
|
575
|
// Get query body
|
576
|
$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
|
577
|
if($get_query_body->numRows() > 0) {
|
578
|
// Fetch query body
|
579
|
$fetch_query_body = $get_query_body->fetchRow();
|
580
|
// Prepare query body for execution by replacing {STRING} with the correct one
|
581
|
$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
|
582
|
// Loop through query body for each string, then combine with start and end
|
583
|
$prepared_query = $query_start." ( ( ( ";
|
584
|
$count = 0;
|
585
|
foreach($search_normal_array AS $string) {
|
586
|
if($count != 0) {
|
587
|
$prepared_query .= " ) ".$logical_operator." ( ";
|
588
|
}
|
589
|
$prepared_query .= str_replace('[STRING]', $string, $query_body);
|
590
|
$count = $count+1;
|
591
|
}
|
592
|
$count=0;
|
593
|
$prepared_query .= ' ) ) OR ( ( ';
|
594
|
foreach($search_entities_array AS $string) {
|
595
|
if($count != 0) {
|
596
|
$prepared_query .= " ) ".$logical_operator." ( ";
|
597
|
}
|
598
|
$prepared_query .= str_replace('[STRING]', $string, $query_body);
|
599
|
$count = $count+1;
|
600
|
}
|
601
|
$prepared_query .= " ) ) ) ".$query_end;
|
602
|
|
603
|
// Execute query
|
604
|
$page_query = $database->query($prepared_query." ".$search_path_SQL);
|
605
|
|
606
|
// Loop through queried items
|
607
|
if($page_query->numRows() > 0) {
|
608
|
while($page = $page_query->fetchRow()) {
|
609
|
// Only show this page if it hasn't already been listed
|
610
|
if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
|
611
|
continue;
|
612
|
}
|
613
|
|
614
|
// don't list pages with visibility == none|deleted and check if user is allowed to see the page
|
615
|
$p_table = TABLE_PREFIX."pages";
|
616
|
$viewquery = $database->query("
|
617
|
SELECT visibility, viewing_groups, viewing_users
|
618
|
FROM $p_table
|
619
|
WHERE page_id='{$page['page_id']}'
|
620
|
");
|
621
|
$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
|
622
|
if($viewquery->numRows() > 0) {
|
623
|
if($res = $viewquery->fetchRow()) {
|
624
|
$visibility = $res['visibility'];
|
625
|
$viewing_groups = $res['viewing_groups'];
|
626
|
$viewing_users = $res['viewing_users'];
|
627
|
if($visibility == 'deleted' || $visibility == 'none') {
|
628
|
continue;
|
629
|
}
|
630
|
if($visibility == 'private') {
|
631
|
if($admin->page_is_visible(array(
|
632
|
'page_id'=>$page[$fields['page_id']],
|
633
|
'visibility' =>$visibility,
|
634
|
'viewing_groups'=>$viewing_groups,
|
635
|
'viewing_users'=>$viewing_users
|
636
|
)) == false) {
|
637
|
continue;
|
638
|
}
|
639
|
}
|
640
|
if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
|
641
|
continue;
|
642
|
}
|
643
|
}
|
644
|
}
|
645
|
|
646
|
// Get page link
|
647
|
$link = page_link($page['link']);
|
648
|
// Add search string for highlighting
|
649
|
if ($match!='exact') {
|
650
|
$sstring = implode(" ", $search_normal_array);
|
651
|
$link = $link."?searchresult=1&sstring=".urlencode($sstring);
|
652
|
} else {
|
653
|
$sstring = strtr($search_normal_array[0], " ", "_");
|
654
|
$link = $link."?searchresult=2&sstring=".urlencode($sstring);
|
655
|
}
|
656
|
// Set vars to be replaced by values
|
657
|
if(!isset($page['description'])) { $page['description'] = ""; }
|
658
|
if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
|
659
|
if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
|
660
|
$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
|
661
|
if($page['modified_when'] > 0) {
|
662
|
$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
|
663
|
$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
|
664
|
} else {
|
665
|
$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
|
666
|
$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
|
667
|
}
|
668
|
$excerpt="";
|
669
|
if($cfg_show_description == 0) {
|
670
|
$page['description'] = "";
|
671
|
}
|
672
|
if(isset($page['menu_title'])) $rep_title = $page['menu_title'];
|
673
|
else $rep_title = $page['page_title'];
|
674
|
$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);
|
675
|
// Show loop code with vars replaced by values
|
676
|
echo str_replace($vars, $values, ($fetch_results_loop['value']));
|
677
|
// Say that this page has been listed
|
678
|
$seen_pages[$module_name][$page['page_id']] = true;
|
679
|
$pages_listed[$page['page_id']] = true;
|
680
|
}
|
681
|
}
|
682
|
}
|
683
|
}
|
684
|
}
|
685
|
|
686
|
// Say no items found if we should
|
687
|
if(count($pages_listed) == 0) {
|
688
|
echo $search_no_results;
|
689
|
}
|
690
|
} else {
|
691
|
echo $search_no_results;
|
692
|
}
|
693
|
|
694
|
// Show search results_footer
|
695
|
echo $search_results_footer;
|
696
|
// Show search footer
|
697
|
echo $search_footer;
|
698
|
|
699
|
?>
|