1
|
<?php
|
2
|
|
3
|
// $Id: search.php 437 2007-03-03 13:24:20Z Ruebenwurzel $
|
4
|
|
5
|
/*
|
6
|
|
7
|
Website Baker Project <http://www.websitebaker.org/>
|
8
|
Copyright (C) 2004-2007, Ryan Djurovich
|
9
|
|
10
|
Website Baker is free software; you can redistribute it and/or modify
|
11
|
it under the terms of the GNU General Public License as published by
|
12
|
the Free Software Foundation; either version 2 of the License, or
|
13
|
(at your option) any later version.
|
14
|
|
15
|
Website Baker is distributed in the hope that it will be useful,
|
16
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
GNU General Public License for more details.
|
19
|
|
20
|
You should have received a copy of the GNU General Public License
|
21
|
along with Website Baker; if not, write to the Free Software
|
22
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
|
24
|
*/
|
25
|
|
26
|
if(!defined('WB_URL')) {
|
27
|
header('Location: index.php');
|
28
|
exit(0);
|
29
|
}
|
30
|
|
31
|
// Check if search is enabled
|
32
|
if(SHOW_SEARCH != true) {
|
33
|
echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
|
34
|
} else {
|
35
|
|
36
|
// Make pages_listed and items_listed blank arrays
|
37
|
$pages_listed = array();
|
38
|
$items_listed = array();
|
39
|
|
40
|
// Get the search type
|
41
|
$match = 'all';
|
42
|
if(isset($_REQUEST['match'])) {
|
43
|
$match = $_REQUEST['match'];
|
44
|
}
|
45
|
|
46
|
// Get search string
|
47
|
if(isset($_REQUEST['string'])) {
|
48
|
if ($match!='exact') {
|
49
|
$string=str_replace(',', '', $_REQUEST['string']);
|
50
|
} else {
|
51
|
$string=$_REQUEST['string'];
|
52
|
}
|
53
|
// reverse potential magic_quotes action
|
54
|
$original_string=$wb->strip_slashes($string);
|
55
|
// Double backslashes (mySQL needs doubly escaped backslashes in LIKE comparisons)
|
56
|
$string = addslashes($wb->escape_backslashes($original_string));
|
57
|
// convert a copy of $string to HTML-ENTITIES
|
58
|
$string_entities=mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
|
59
|
$string_entities=htmlspecialchars($string_entities,ENT_QUOTES);
|
60
|
$string_entities=mb_convert_encoding($string_entities, 'HTML-ENTITIES', 'UTF-8');
|
61
|
// and do some convertion to both
|
62
|
require(WB_PATH.'/search/search_convert.php');
|
63
|
$string=strtr($string,$string_conv);
|
64
|
$string_entities=strtr($string_entities,$string_entities_conv);
|
65
|
$search_string = $string_entities;
|
66
|
} else {
|
67
|
$string = '';
|
68
|
$search_string = '';
|
69
|
}
|
70
|
|
71
|
// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
|
72
|
$all_checked = '';
|
73
|
$any_checked = '';
|
74
|
$exact_checked = '';
|
75
|
if($match != 'exact') {
|
76
|
// Split string into array with explode() function
|
77
|
$exploded_string = explode(' ', $string);
|
78
|
// Make sure there is no blank values in the array
|
79
|
$string = array();
|
80
|
foreach($exploded_string AS $each_exploded_string) {
|
81
|
if($each_exploded_string != '') {
|
82
|
$string[] = $each_exploded_string;
|
83
|
}
|
84
|
}
|
85
|
// Split $string_entities, too
|
86
|
$exploded_string = explode(' ', $string_entities);
|
87
|
// Make sure there is no blank values in the array
|
88
|
$string_entities = array();
|
89
|
foreach($exploded_string AS $each_exploded_string) {
|
90
|
if($each_exploded_string != '') {
|
91
|
$string_entities[] = $each_exploded_string;
|
92
|
}
|
93
|
}
|
94
|
if ($match == 'any') {
|
95
|
$any_checked = ' checked="checked"';
|
96
|
$logical_operator = ' OR';
|
97
|
} else {
|
98
|
$all_checked = ' checked="checked"';
|
99
|
$logical_operator = ' AND';
|
100
|
}
|
101
|
} else {
|
102
|
$exact_checked = ' checked="checked"';
|
103
|
$exact_string=$string;
|
104
|
$string=array();
|
105
|
$string[]=$exact_string;
|
106
|
$exact_string=$string_entities;
|
107
|
$string_entities=array();
|
108
|
$string_entities[]=$exact_string;
|
109
|
}
|
110
|
// Get list of usernames and display names
|
111
|
$query_users = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
|
112
|
$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
|
113
|
if($query_users->numRows() > 0) {
|
114
|
while($user = $query_users->fetchRow()) {
|
115
|
$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
// Get search settings
|
120
|
$query_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'header' LIMIT 1");
|
121
|
$fetch_header = $query_header->fetchRow();
|
122
|
$query_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'footer' LIMIT 1");
|
123
|
$fetch_footer = $query_footer->fetchRow();
|
124
|
$query_results_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_header' LIMIT 1");
|
125
|
$fetch_results_header = $query_results_header->fetchRow();
|
126
|
$query_results_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_footer' LIMIT 1");
|
127
|
$fetch_results_footer = $query_results_footer->fetchRow();
|
128
|
$query_results_loop = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_loop' LIMIT 1");
|
129
|
$fetch_results_loop = $query_results_loop->fetchRow();
|
130
|
$query_no_results = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'no_results' LIMIT 1");
|
131
|
$fetch_no_results = $query_no_results->fetchRow();
|
132
|
|
133
|
// Replace vars in search settings with values
|
134
|
$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
|
135
|
$values = array($search_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
|
136
|
$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
|
137
|
$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
|
138
|
$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
|
139
|
// Do extra vars/values replacement
|
140
|
$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]');
|
141
|
$values = 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);
|
142
|
$search_header = str_replace($vars, $values, ($fetch_header['value']));
|
143
|
|
144
|
// Show search header
|
145
|
echo $search_header;
|
146
|
|
147
|
// Work-out if the user has already entered their details or not
|
148
|
if($string != '' AND $string != ' ' AND $string != ' ' AND $string != array()) {
|
149
|
|
150
|
// Show search results_header
|
151
|
echo $search_results_header;
|
152
|
// Search page details only, such as description, keywords, etc.
|
153
|
$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
|
154
|
$count = 0;
|
155
|
foreach($string AS $each_string) {
|
156
|
if($count != 0) {
|
157
|
$query_pages .= $logical_operator;
|
158
|
}
|
159
|
$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
|
160
|
" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
|
161
|
$count = $count+1;
|
162
|
}
|
163
|
$count = 0;
|
164
|
$query_pages .= ' OR';
|
165
|
foreach($string_entities AS $each_string) {
|
166
|
if($count != 0) {
|
167
|
$query_pages .= $logical_operator;
|
168
|
}
|
169
|
$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
|
170
|
" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
|
171
|
$count = $count+1;
|
172
|
}
|
173
|
$query_pages = $database->query($query_pages);
|
174
|
// Loop through pages
|
175
|
if($query_pages->numRows() > 0) {
|
176
|
while($page = $query_pages->fetchRow()) {
|
177
|
// Get page link
|
178
|
$link = page_link($page['link']);
|
179
|
|
180
|
//Add search string for highlighting
|
181
|
$sstring = implode(" ", array_merge($string,$string_entities));
|
182
|
//$link = $link."?searchresult=1&sstring=".$sstring;
|
183
|
$link = $link."?searchresult=1&sstring=".urlencode($sstring);
|
184
|
|
185
|
// Set vars to be replaced by values
|
186
|
$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
|
187
|
if($page['modified_when'] > 0) {
|
188
|
$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
|
189
|
$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
|
190
|
} else {
|
191
|
$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
|
192
|
$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
|
193
|
}
|
194
|
$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']));
|
195
|
// Show loop code with vars replaced by values
|
196
|
if($values != array()) {
|
197
|
echo str_replace($vars, $values, ($fetch_results_loop['value']));
|
198
|
}
|
199
|
// Say that we have already listed this page id
|
200
|
$pages_listed[$page['page_id']] = true;
|
201
|
// Set values to blank
|
202
|
$value = array();
|
203
|
}
|
204
|
}
|
205
|
// Get modules that have registered for custom query's to be conducted
|
206
|
$get_modules = $database->query("SELECT value,extra FROM ".TABLE_PREFIX."search WHERE name = 'module'");
|
207
|
// Loop through each module
|
208
|
if($get_modules->numRows() > 0) {
|
209
|
while($module = $get_modules->fetchRow()) {
|
210
|
// Get module name
|
211
|
$module_name = $module['value'];
|
212
|
// Get fields to use for title, link, etc.
|
213
|
$fields = unserialize($module['extra']);
|
214
|
// Get query start
|
215
|
$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
|
216
|
if($get_query_start->numRows() > 0) {
|
217
|
// Fetch query start
|
218
|
$fetch_query_start = $get_query_start->fetchRow();
|
219
|
// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
|
220
|
$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
|
221
|
// Get query end
|
222
|
$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
|
223
|
if($get_query_end->numRows() > 0) {
|
224
|
// Fetch query start
|
225
|
$fetch_query_end = $get_query_end->fetchRow();
|
226
|
// Set query end
|
227
|
$query_end = ($fetch_query_end['value']);
|
228
|
// Get query body
|
229
|
$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
|
230
|
if($get_query_body->numRows() > 0) {
|
231
|
// Fetch query start
|
232
|
$fetch_query_body = $get_query_body->fetchRow();
|
233
|
// Prepare query body for execution by replacing {STRING} with the correct one
|
234
|
$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
|
235
|
// Loop through query body for each string, then combine with start and end
|
236
|
$prepared_query = $query_start;
|
237
|
$count = 0;
|
238
|
foreach($string AS $each_string) {
|
239
|
if($count != 0) {
|
240
|
$prepared_query .= $logical_operator;
|
241
|
}
|
242
|
$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
|
243
|
$count = $count+1;
|
244
|
}
|
245
|
$count=0;
|
246
|
$prepared_query .= ' OR ';
|
247
|
foreach($string_entities AS $each_string) {
|
248
|
if($count != 0) {
|
249
|
$prepared_query .= $logical_operator;
|
250
|
}
|
251
|
$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
|
252
|
$count = $count+1;
|
253
|
}
|
254
|
|
255
|
$prepared_query .= $query_end;
|
256
|
|
257
|
// Execute query
|
258
|
$query = $database->query($prepared_query);
|
259
|
// Loop though queried items
|
260
|
if($query->numRows() > 0) {
|
261
|
while($page = $query->fetchRow()) {
|
262
|
// Only show this page if it hasn't already been list
|
263
|
if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
|
264
|
// Get page link
|
265
|
$link = page_link($page[$fields['link']]);
|
266
|
|
267
|
//Add search string for highlighting
|
268
|
$sstring = implode(" ", array_merge($string,$string_entities));
|
269
|
//$link = $link."?searchresult=1&sstring=".$sstring;
|
270
|
$link = $link."?searchresult=1&sstring=".urlencode($sstring);
|
271
|
|
272
|
// Set vars to be replaced by values
|
273
|
$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
|
274
|
if($page[$fields['modified_when']] > 0) {
|
275
|
$date = gmdate(DATE_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
|
276
|
$time = gmdate(TIME_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
|
277
|
} else {
|
278
|
$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
|
279
|
$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
|
280
|
}
|
281
|
$values = array($link, ($page[$fields['title']]), ($page[$fields['description']]), $users[$page[$fields['modified_by']]]['username'], $users[$page[$fields['modified_by']]]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']));
|
282
|
// Show loop code with vars replaced by values
|
283
|
echo str_replace($vars, $values, ($fetch_results_loop['value']));
|
284
|
// Say that this page or item has been listed if we can
|
285
|
if(isset($fields['page_id'])) {
|
286
|
$pages_listed[$page[$fields['page_id']]] = true;
|
287
|
} elseif(isset($fields['item_id'])) {
|
288
|
$items_listed[$page[$fields['item_id']]] = true;
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
}
|
293
|
}
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
|
298
|
// Show search results_footer
|
299
|
echo $search_results_footer;
|
300
|
|
301
|
}
|
302
|
|
303
|
// Say no items found if we should
|
304
|
if($pages_listed == array() AND $items_listed == array()) {
|
305
|
echo $fetch_no_results['value'];
|
306
|
}
|
307
|
|
308
|
}
|
309
|
|
310
|
// Show search footer
|
311
|
echo $search_footer;
|
312
|
|
313
|
}
|
314
|
|
315
|
?>
|