Project

General

Profile

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