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 452 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 452 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 487 Ruebenwurz
			$string=str_replace(',', '', $_REQUEST['string']);
53 239 stefan
		} else {
54 487 Ruebenwurz
			$string=$_REQUEST['string'];
55 239 stefan
		}
56 487 Ruebenwurz
		// remove some bad chars like _single_ '"', '&'. '!", ...
57
		$string = preg_replace("/(^|\s+)([-=+_&!;#]|\\\\\"|\\\\')+(?=\s+|$)/", "", $string);
58
		$string = my_htmlspecialchars($string);
59 239 stefan
		// reverse potential magic_quotes action
60
		$original_string=$wb->strip_slashes($string);
61
		// Double backslashes (mySQL needs doubly escaped backslashes in LIKE comparisons)
62
		$string = addslashes($wb->escape_backslashes($original_string));
63 452 Ruebenwurz
		// convert a copy of $string to HTML-ENTITIES
64
		$string_entities = umlauts_to_entities($string);
65
		// and do some convertion to both
66
		require(WB_PATH.'/search/search_convert.php');
67
		$search_string = $string_entities;
68 239 stefan
	} else {
69
		$string = '';
70
		$search_string = '';
71
	}
72
73
	// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
74
	$all_checked = '';
75
	$any_checked = '';
76
	$exact_checked = '';
77 369 Ruebenwurz
	if($match != 'exact') {
78 239 stefan
		// Split string into array with explode() function
79
		$exploded_string = explode(' ', $string);
80
		// Make sure there is no blank values in the array
81
		$string = array();
82
		foreach($exploded_string AS $each_exploded_string) {
83
			if($each_exploded_string != '') {
84
				$string[] = $each_exploded_string;
85
			}
86
		}
87 452 Ruebenwurz
		// Split $string_entities, too
88
		$exploded_string = explode(' ', $string_entities);
89
		// Make sure there is no blank values in the array
90
		$string_entities = array();
91
		foreach($exploded_string AS $each_exploded_string) {
92
			if($each_exploded_string != '') {
93
				$string_entities[] = $each_exploded_string;
94
			}
95
		}
96 369 Ruebenwurz
		if ($match == 'any') {
97
			$any_checked = ' checked="checked"';
98 239 stefan
			$logical_operator = ' OR';
99
		} else {
100 369 Ruebenwurz
			$all_checked = ' checked="checked"';
101 239 stefan
			$logical_operator = ' AND';
102
		}
103
	} else {
104 369 Ruebenwurz
		$exact_checked = ' checked="checked"';
105 239 stefan
		$exact_string=$string;
106
		$string=array();
107
		$string[]=$exact_string;
108 452 Ruebenwurz
		$exact_string=$string_entities;
109
		$string_entities=array();
110
		$string_entities[]=$exact_string;
111 239 stefan
	}
112
	// Get list of usernames and display names
113
	$query_users = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
114
	$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
115
	if($query_users->numRows() > 0) {
116
		while($user = $query_users->fetchRow()) {
117
			$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
118
		}
119
	}
120
121
	// Get search settings
122
	$query_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'header' LIMIT 1");
123
	$fetch_header = $query_header->fetchRow();
124
	$query_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'footer' LIMIT 1");
125
	$fetch_footer = $query_footer->fetchRow();
126
	$query_results_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_header' LIMIT 1");
127
	$fetch_results_header = $query_results_header->fetchRow();
128
	$query_results_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_footer' LIMIT 1");
129
	$fetch_results_footer = $query_results_footer->fetchRow();
130
	$query_results_loop = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_loop' LIMIT 1");
131
	$fetch_results_loop = $query_results_loop->fetchRow();
132
	$query_no_results = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'no_results' LIMIT 1");
133
	$fetch_no_results = $query_no_results->fetchRow();
134
135
	// Replace vars in search settings with values
136
	$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
137
	$values = array($search_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
138
	$search_footer = str_replace($vars, $values, ($fetch_footer['value']));
139
	$search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
140
	$search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
141
	// Do extra vars/values replacement
142 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]');
143
	$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);
144 239 stefan
	$search_header = str_replace($vars, $values, ($fetch_header['value']));
145 452 Ruebenwurz
	$vars = array('[TEXT_NO_RESULTS]');
146
	$values = array($TEXT['NO_RESULTS']);
147
	$search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
148 239 stefan
149
	// Show search header
150
	echo $search_header;
151
152
	// Work-out if the user has already entered their details or not
153
	if($string != '' AND $string != ' ' AND $string != '  ' AND $string != array()) {
154
155
		// Show search results_header
156
		echo $search_results_header;
157
		// Search page details only, such as description, keywords, etc.
158 481 Ruebenwurz
		$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by, visibility FROM ".TABLE_PREFIX."pages WHERE ";
159 452 Ruebenwurz
		$count = 0;
160
		foreach($string AS $each_string) {
161
			if($count != 0) {
162
				$query_pages .= $logical_operator;
163 239 stefan
			}
164 452 Ruebenwurz
			$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
165
			" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
166
			$count = $count+1;
167
		}
168
		$count = 0;
169
		$query_pages .= ' OR';
170
		foreach($string_entities AS $each_string) {
171
			if($count != 0) {
172
				$query_pages .= $logical_operator;
173
			}
174
			$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
175
			" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
176
			$count = $count+1;
177
		}
178
		$query_pages = $database->query($query_pages);
179 239 stefan
		// Loop through pages
180
		if($query_pages->numRows() > 0) {
181
			while($page = $query_pages->fetchRow()) {
182 481 Ruebenwurz
183
				// check if user is allowed to see the page (for private-pages)
184
				$visibility = $page['visibility'];
185
				if($visibility == 'private') {
186
					$access_denied = true;
187
					$rightsquery = $database->query("SELECT ".
188
						TABLE_PREFIX."pages.viewing_groups, ".
189
						TABLE_PREFIX."pages.viewing_users
190
						FROM ".TABLE_PREFIX."pages
191
						WHERE ".TABLE_PREFIX."pages.page_id='".$page['page_id']."' LIMIT 1 "
192
					);
193
					$viewing_groups=array() ; $viewing_users=array();
194
					if($rightsquery->numRows() > 0) {
195
						if($res = $rightsquery->fetchRow()) {
196
							$viewing_groups = explode(',', $res['viewing_groups']);
197
							$viewing_users = explode(',', $res['viewing_users']);
198
						}
199
					}
200
					if($wb->is_authenticated() == true) {
201
						if(in_array($wb->get_group_id(), $viewing_groups) || (in_array($wb->get_user_id(), $viewing_users))) {
202
							$access_denied = false;
203
						}
204
					}
205
					if($access_denied) {
206
						continue;
207
					}
208
				}
209
210 239 stefan
				// Get page link
211
				$link = page_link($page['link']);
212 452 Ruebenwurz
213
				//Add search string for highlighting
214
				if ($match!='exact') {
215
					$sstring = implode(" ", $string);
216
					$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
217
				}
218
				else {
219
					$sstring = strtr($string[0], " ", "_");
220
					$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
221
				}
222
223 239 stefan
				// Set vars to be replaced by values
224
				$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
225
				if($page['modified_when'] > 0) {
226
					$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
227
					$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
228
				} else {
229
					$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
230
					$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
231
				}
232
				$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']));
233
				// Show loop code with vars replaced by values
234
				if($values != array()) {
235
					echo str_replace($vars, $values, ($fetch_results_loop['value']));
236
				}
237
				// Say that we have already listed this page id
238
				$pages_listed[$page['page_id']] = true;
239
				// Set values to blank
240
				$value = array();
241
			}
242
		}
243
		// Get modules that have registered for custom query's to be conducted
244
		$get_modules = $database->query("SELECT value,extra FROM ".TABLE_PREFIX."search WHERE name = 'module'");
245
		// Loop through each module
246
		if($get_modules->numRows() > 0) {
247
			while($module = $get_modules->fetchRow()) {
248
				// Get module name
249
				$module_name = $module['value'];
250
				// Get fields to use for title, link, etc.
251
				$fields = unserialize($module['extra']);
252
				// Get query start
253
				$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
254
				if($get_query_start->numRows() > 0) {
255
					// Fetch query start
256
					$fetch_query_start = $get_query_start->fetchRow();
257
					// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
258
					$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
259
					// Get query end
260
					$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
261
					if($get_query_end->numRows() > 0) {
262
						// Fetch query start
263
						$fetch_query_end = $get_query_end->fetchRow();
264
						// Set query end
265
						$query_end = ($fetch_query_end['value']);
266
						// Get query body
267
						$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
268
						if($get_query_body->numRows() > 0) {
269
							// Fetch query start
270
							$fetch_query_body = $get_query_body->fetchRow();
271
							// Prepare query body for execution by replacing {STRING} with the correct one
272
							$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
273
							// Loop through query body for each string, then combine with start and end
274
							$prepared_query = $query_start;
275
							$count = 0;
276
							foreach($string AS $each_string) {
277 452 Ruebenwurz
								if($count != 0) {
278
									$prepared_query .= $logical_operator;
279
								}
280 239 stefan
								$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
281
								$count = $count+1;
282
							}
283 452 Ruebenwurz
							$count=0;
284
							$prepared_query .= ' OR ';
285
							foreach($string_entities AS $each_string) {
286
								if($count != 0) {
287
									$prepared_query .= $logical_operator;
288
								}
289
								$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
290
								$count = $count+1;
291
							}
292
293 239 stefan
							$prepared_query .= $query_end;
294 452 Ruebenwurz
295 239 stefan
							// Execute query
296
							$query = $database->query($prepared_query);
297
							// Loop though queried items
298
							if($query->numRows() > 0) {
299
								while($page = $query->fetchRow()) {
300
									// Only show this page if it hasn't already been list
301
									if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
302 481 Ruebenwurz
303
304
										// don't list pages with visibility == none|deleted
305 487 Ruebenwurz
										$viewquery = $database->query("SELECT ".
306 481 Ruebenwurz
											TABLE_PREFIX."pages.visibility
307
											FROM ".TABLE_PREFIX."pages
308
											WHERE ".TABLE_PREFIX."pages.page_id='".$page[$fields['page_id']]."' LIMIT 1 "
309
										);
310
										$visibility = 'public';
311 487 Ruebenwurz
										if($viewquery->numRows() > 0) {
312
											if($res = $viewquery->fetchRow()) {
313 481 Ruebenwurz
												$visibility = $res['visibility'];
314
											}
315
										}
316
										if($visibility == 'deleted' || $visibility == 'none') {
317
											continue;
318
										}
319
										// check if user is allowed to see the page (for private-pages)
320
										if($visibility == 'private') {
321
											$access_denied = true;
322
											$rightsquery = $database->query("SELECT ".
323
												TABLE_PREFIX."pages.viewing_groups, ".
324
												TABLE_PREFIX."pages.viewing_users
325
												FROM ".TABLE_PREFIX."pages
326
												WHERE ".TABLE_PREFIX."pages.page_id='".$page[$fields['page_id']]."' LIMIT 1 "
327
											);
328
											$viewing_groups=array() ; $viewing_users=array();
329
											if($rightsquery->numRows() > 0) {
330
												if($res = $rightsquery->fetchRow()) {
331
													$viewing_groups = explode(',', $res['viewing_groups']);
332
													$viewing_users = explode(',', $res['viewing_users']);
333
												}
334
											}
335
											if($wb->is_authenticated() == true) {
336
												if(in_array($wb->get_group_id(), $viewing_groups) || (in_array($wb->get_user_id(), $viewing_users))) {
337
													$access_denied = false;
338
												}
339
											}
340
											if($access_denied) {
341
												continue;
342
											}
343
										}
344
345 239 stefan
										// Get page link
346
										$link = page_link($page[$fields['link']]);
347 452 Ruebenwurz
348
										//Add search string for highlighting
349
										if ($match!='exact') {
350
											$sstring = implode(" ", $string);
351
											$link = $link."?searchresult=1&amp;sstring=".urlencode($sstring);
352
										}
353
										else {
354
											$sstring = strtr($string[0], " ", "_");
355
											$link = $link."?searchresult=2&amp;sstring=".urlencode($sstring);
356
										}
357
358 239 stefan
										// Set vars to be replaced by values
359
										$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
360
										if($page[$fields['modified_when']] > 0) {
361
											$date = gmdate(DATE_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
362
											$time = gmdate(TIME_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
363
										} else {
364
											$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
365
											$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
366
										}
367
										$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']));
368
										// Show loop code with vars replaced by values
369
										echo str_replace($vars, $values, ($fetch_results_loop['value']));
370
										// Say that this page or item has been listed if we can
371
										if(isset($fields['page_id'])) {
372
											$pages_listed[$page[$fields['page_id']]] = true;
373
										} elseif(isset($fields['item_id'])) {
374
											$items_listed[$page[$fields['item_id']]] = true;
375
										}
376
									}
377
								}
378
							}
379
						}
380
					}
381
				}
382
			}
383
384
			// Show search results_footer
385
			echo $search_results_footer;
386
387
		}
388
389 452 Ruebenwurz
		// Say no items found if we should
390
		if($pages_listed == array() AND $items_listed == array()) {
391
			echo $search_no_results;
392
		}
393 239 stefan
394
	}
395
396
	// Show search footer
397
	echo $search_footer;
398
399
}
400
401
?>