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