Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 481 2007-06-11 16:40:46Z 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
// Include the WB functions file
32
require_once(WB_PATH.'/framework/functions.php');
33

    
34
// 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
	// Get the search type
44
	$match = 'all';
45
	if(isset($_REQUEST['match'])) {
46
		$match = $_REQUEST['match'];
47
	}
48

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

    
400
?>
(2-2/3)