Project

General

Profile

1 4 ryan
<?php
2
3
// $Id: search.php,v 1.9 2005/04/07 07:53:15 rdjurovich Exp $
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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')) { header('Location: index.php'); }
27
28
// Check if search is enabled
29
if(SHOW_SEARCH != true) {
30
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
31
} else {
32
33
	// Make pages_listed and items_listed blank arrays
34
	$pages_listed = array();
35
	$items_listed = array();
36
37
	// Get search string
38
	if(isset($_POST['string'])) {
39
		$string = addslashes(str_replace(',', '', $_POST['string']));
40
		$search_string = htmlspecialchars(stripslashes($string),ENT_QUOTES);
41
	} else {
42
		$string = '';
43
		$search_string = '';
44
	}
45
46
	// Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
47
	$all_checked = '';
48
	$any_checked = '';
49
	$exact_checked = '';
50
	if(!isset($_POST['match'])) {
51
		$match = 'all';
52
		$operator = 'LIKE';
53
		$wildcard = '%';
54
		$all_checked = ' checked';
55
	} elseif($_POST['match'] == 'all') {
56
		$match = 'all';
57
		$operator = 'LIKE';
58
		$wildcard = '%';
59
		$all_checked = ' checked';
60
	} elseif($_POST['match'] == 'any') {
61
		$match = 'any';
62
		$operator = 'LIKE';
63
		$wildcard = '%';
64
		$any_checked = ' checked';
65
		// Split string into array with explode() function
66
		$exploded_string = explode(' ', $string);
67
		// Make sure there is no blank values in the array
68
		$string = array();
69
		foreach($exploded_string AS $each_exploded_string) {
70
			if($each_exploded_string != '') {
71
				$string[] = $each_exploded_string;
72
			}
73
		}
74
	} elseif($_POST['match'] == 'exact') {
75
		$match = 'exact';
76
		$operator = '=';
77
		$wildcard = '';
78
		$exact_checked = ' checked';
79
	} else {
80
		$match = 'all';
81
		$operator = 'LIKE';
82
		$wildcard = '%';
83
		$all_checked = ' checked';
84
	}
85
86
	// Get list of usernames and display names
87
	$query_users = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
88
	$users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
89
	if($query_users->numRows() > 0) {
90
		while($user = $query_users->fetchRow()) {
91
			$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
92
		}
93
	}
94
95
	// Get search settings
96
	$query_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'header' LIMIT 1");
97
	$fetch_header = $query_header->fetchRow();
98
	$query_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'footer' LIMIT 1");
99
	$fetch_footer = $query_footer->fetchRow();
100
	$query_results_header = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_header' LIMIT 1");
101
	$fetch_results_header = $query_results_header->fetchRow();
102
	$query_results_footer = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_footer' LIMIT 1");
103
	$fetch_results_footer = $query_results_footer->fetchRow();
104
	$query_results_loop = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_loop' LIMIT 1");
105
	$fetch_results_loop = $query_results_loop->fetchRow();
106
	$query_no_results = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'no_results' LIMIT 1");
107
	$fetch_no_results = $query_no_results->fetchRow();
108
109
	// Replace vars in search settings with values
110
	$vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
111
	$values = array($search_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
112
	$search_footer = str_replace($vars, $values, stripslashes($fetch_footer['value']));
113
	$search_results_header = str_replace($vars, $values, stripslashes($fetch_results_header['value']));
114
	$search_results_footer = str_replace($vars, $values, stripslashes($fetch_results_footer['value']));
115
	// Do extra vars/values replacement
116
	$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]');
117
	$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);
118
	$search_header = str_replace($vars, $values, stripslashes($fetch_header['value']));
119
120
	// Insert js code
121
	?>
122
	<script language="javascript" type="text/javascript">
123
	function toggle_radio(checkbox_id) {
124
		if(document.getElementById(checkbox_id).checked == true) {
125
			document.getElementById(checkbox_id).checked = false;
126
		} else {
127
			document.getElementById(checkbox_id).checked = true;
128
		}
129
	}
130
	</script>
131
	<?php
132
133
	// Show search header
134
	echo $search_header;
135
136
	// Work-out if the user has already entered their details or not
137
	if($string != '' AND $string != ' ' AND $string != '  ' AND $string != array()) {
138
139
		// Show search results_header
140
		echo $search_results_header;
141
142
		// Search page details only, such as description, keywords, etc.
143
		if($match == 'all' OR $match == 'exact') {
144
			$query_pages = $database->query("SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages".
145
			" WHERE visibility != 'none' AND visibility != 'deleted' AND page_title $operator '$wildcard$string$wildcard' AND searching = '1' ".
146
			" OR visibility != 'none' AND visibility != 'deleted' AND menu_title $operator '$wildcard$string$wildcard' AND searching = '1'".
147
			" OR visibility != 'none' AND visibility != 'deleted' AND description $operator '$wildcard$string$wildcard' AND searching = '1'".
148
			" OR visibility != 'none' AND visibility != 'deleted' AND keywords $operator '$wildcard$string$wildcard' AND searching = '1'");
149
		} elseif($match == 'any') {
150
			$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
151
			$count = 0;
152
			foreach($string AS $each_string) {
153
				if($count != 0) { $query_pages .= ' OR'; }
154
				$query_pages .= " visibility != 'none' AND page_title $operator '$wildcard$each_string$wildcard' AND searching = '1'".
155
				" OR visibility != 'none' AND visibility != 'deleted' AND menu_title $operator '$wildcard$each_string$wildcard' AND searching = '1'".
156
				" OR visibility != 'none' AND visibility != 'deleted' AND description $operator '$wildcard$each_string$wildcard' AND searching = '1'".
157
				" OR visibility != 'none' AND visibility != 'deleted' AND keywords $operator '$wildcard$each_string$wildcard' AND searching = '1'";
158
				$count = $count+1;
159
			}
160
			$query_pages = $database->query($query_pages);
161
		}
162
		// Loop through pages
163
		if($query_pages->numRows() > 0) {
164
			while($page = $query_pages->fetchRow()) {
165
				// Get page link
166
				$link = page_link($page['link']);
167
				// Set vars to be replaced by values
168
				$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
169
				if($page['modified_when'] > 0) {
170
					$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
171
					$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
172
				} else {
173
					$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
174
					$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
175
				}
176
				$values = array($link, stripslashes($page['page_title']),stripslashes($page['description']), $users[$page['modified_by']]['username'], $users[$page['modified_by']]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']));
177
				// Show loop code with vars replaced by values
178
				if($values != array()) {
179
					echo str_replace($vars, $values, stripslashes($fetch_results_loop['value']));
180
				}
181
				// Say that we have already listed this page id
182
				$pages_listed[$page['page_id']] = true;
183
				// Set values to blank
184
				$value = array();
185
			}
186
		}
187
		// Get modules that have registered for custom query's to be conducted
188
		$get_modules = $database->query("SELECT value,extra FROM ".TABLE_PREFIX."search WHERE name = 'module'");
189
		// Loop through each module
190
		if($get_modules->numRows() > 0) {
191
			while($module = $get_modules->fetchRow()) {
192
				// Get module name
193
				$module_name = $module['value'];
194
				// Get fields to use for title, link, etc.
195
				$fields = unserialize($module['extra']);
196
				// Get query start
197
				$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
198
				if($get_query_start->numRows() > 0) {
199
					// Fetch query start
200
					$fetch_query_start = $get_query_start->fetchRow();
201
					// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
202
					$query_start = str_replace('[TP]', TABLE_PREFIX, stripslashes($fetch_query_start['value']));
203
					// Get query end
204
					$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
205
					if($get_query_end->numRows() > 0) {
206
						// Fetch query start
207
						$fetch_query_end = $get_query_end->fetchRow();
208
						// Set query end
209
						$query_end = stripslashes($fetch_query_end['value']);
210
						// Get query body
211
						$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
212
						if($get_query_body->numRows() > 0) {
213
							// Fetch query start
214
							$fetch_query_body = $get_query_body->fetchRow();
215
							// Prepare query body for execution by replacing {STRING} with the correct one
216
							$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,$operator,$wildcard), stripslashes($fetch_query_body['value']));
217
							// If we need to match any of the words, loop through the body for each one then combine with start and end, otherwise just combine without looping
218
							if($match == 'any') {
219
								// Loop through query body for each string, then combine with start and end
220
								$prepared_query = $query_start;
221
								$count = 0;
222
								foreach($string AS $each_string) {
223
									if($count != 0) { $prepared_query .= 'OR'; }
224
									$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
225
									$count = $count+1;
226
								}
227
								$prepared_query .= $query_end;
228
							} else {
229
								// Replace {STRING} with $string, then combine with start and end
230
								$prepared_query = $query_start.str_replace('[STRING]', $string, $query_body).$query_end;
231
							}
232
							// Execute query
233
							$query = $database->query($prepared_query);
234
							// Loop though queried items
235
							if($query->numRows() > 0) {
236
								while($page = $query->fetchRow()) {
237
									// Only show this page if it hasn't already been list
238
									if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
239
										// Get page link
240
										$link = page_link($page[$fields['link']]);
241
										// Set vars to be replaced by values
242
										$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
243
										if($page[$fields['modified_when']] > 0) {
244
											$date = gmdate(DATE_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
245
											$time = gmdate(TIME_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
246
										} else {
247
											$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
248
											$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
249
										}
250
										$values = array($link, stripslashes($page[$fields['title']]), stripslashes($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']));
251
										// Show loop code with vars replaced by values
252
										echo str_replace($vars, $values, stripslashes($fetch_results_loop['value']));
253
										// Say that this page or item has been listed if we can
254
										if(isset($fields['page_id'])) {
255
											$pages_listed[$page[$fields['page_id']]] = true;
256
										} elseif(isset($fields['item_id'])) {
257
											$items_listed[$page[$fields['item_id']]] = true;
258
										}
259
									}
260
								}
261
							}
262
263
						}
264
					}
265
				}
266
			}
267
268
			// Show search results_footer
269
			echo $search_results_footer;
270
271
		}
272
273
	// Say no items found if we should
274
	if($pages_listed == array() AND $items_listed == array()) {
275
		echo $fetch_no_results['value'];
276
	}
277
278
	}
279
280
	// Show search footer
281
	echo $search_footer;
282
283
}
284
285
?>