Project

General

Profile

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