Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 36 2005-09-06 23:31:52Z stefan $
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(addslashes(str_replace(',', '', $_POST['string'])));
40
		$search_string = htmlspecialchars($this->stripslashes(str_replace(',', '', $_POST['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, $this->stripslashes($fetch_footer['value']));
113
	$search_results_header = str_replace($vars, $values, $this->stripslashes($fetch_results_header['value']));
114
	$search_results_footer = str_replace($vars, $values, $this->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, $this->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
		// Search page details only, such as description, keywords, etc.
142
		if($match == 'all' OR $match == 'exact') {
143
			$query_pages = $database->query("SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages".
144
			" WHERE visibility != 'none' AND visibility != 'deleted' AND page_title $operator '$wildcard$string$wildcard' AND searching = '1' ".
145
			" OR visibility != 'none' AND visibility != 'deleted' AND menu_title $operator '$wildcard$string$wildcard' AND searching = '1'".
146
			" OR visibility != 'none' AND visibility != 'deleted' AND description $operator '$wildcard$string$wildcard' AND searching = '1'".
147
			" OR visibility != 'none' AND visibility != 'deleted' AND keywords $operator '$wildcard$string$wildcard' AND searching = '1'");
148
		} elseif($match == 'any') {
149
			$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
150
			$count = 0;
151
			foreach($string AS $each_string) {
152
				if($count != 0) { $query_pages .= ' OR'; }
153
				$query_pages .= " visibility != 'none' AND page_title $operator '$wildcard$each_string$wildcard' AND searching = '1'".
154
				" OR visibility != 'none' AND visibility != 'deleted' AND menu_title $operator '$wildcard$each_string$wildcard' AND searching = '1'".
155
				" OR visibility != 'none' AND visibility != 'deleted' AND description $operator '$wildcard$each_string$wildcard' AND searching = '1'".
156
				" OR visibility != 'none' AND visibility != 'deleted' AND keywords $operator '$wildcard$each_string$wildcard' AND searching = '1'";
157
				$count = $count+1;
158
			}
159
			$query_pages = $database->query($query_pages);
160
		}
161
		// Loop through pages
162
		if($query_pages->numRows() > 0) {
163
			while($page = $query_pages->fetchRow()) {
164
				// Get page link
165
				$link = page_link($page['link']);
166
				// Set vars to be replaced by values
167
				$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
168
				if($page['modified_when'] > 0) {
169
					$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
170
					$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
171
				} else {
172
					$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
173
					$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
174
				}
175
				$values = array($link, $this->stripslashes($page['page_title']),$this->stripslashes($page['description']), $users[$page['modified_by']]['username'], $users[$page['modified_by']]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']));
176
				// Show loop code with vars replaced by values
177
				if($values != array()) {
178
					echo str_replace($vars, $values, $this->stripslashes($fetch_results_loop['value']));
179
				}
180
				// Say that we have already listed this page id
181
				$pages_listed[$page['page_id']] = true;
182
				// Set values to blank
183
				$value = array();
184
			}
185
		}
186
		// Get modules that have registered for custom query's to be conducted
187
		$get_modules = $database->query("SELECT value,extra FROM ".TABLE_PREFIX."search WHERE name = 'module'");
188
		// Loop through each module
189
		if($get_modules->numRows() > 0) {
190
			while($module = $get_modules->fetchRow()) {
191
				// Get module name
192
				$module_name = $module['value'];
193
				// Get fields to use for title, link, etc.
194
				$fields = unserialize($module['extra']);
195
				// Get query start
196
				$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
197
				if($get_query_start->numRows() > 0) {
198
					// Fetch query start
199
					$fetch_query_start = $get_query_start->fetchRow();
200
					// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
201
					$query_start = str_replace('[TP]', TABLE_PREFIX, $this->stripslashes($fetch_query_start['value']));
202
					// Get query end
203
					$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
204
					if($get_query_end->numRows() > 0) {
205
						// Fetch query start
206
						$fetch_query_end = $get_query_end->fetchRow();
207
						// Set query end
208
						$query_end = $this->stripslashes($fetch_query_end['value']);
209
						// Get query body
210
						$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
211
						if($get_query_body->numRows() > 0) {
212
							// Fetch query start
213
							$fetch_query_body = $get_query_body->fetchRow();
214
							// Prepare query body for execution by replacing {STRING} with the correct one
215
							$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,$operator,$wildcard), $this->stripslashes($fetch_query_body['value']));
216
							// 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
217
							if($match == 'any') {
218
								// Loop through query body for each string, then combine with start and end
219
								$prepared_query = $query_start;
220
								$count = 0;
221
								foreach($string AS $each_string) {
222
									if($count != 0) { $prepared_query .= 'OR'; }
223
									$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
224
									$count = $count+1;
225
								}
226
								$prepared_query .= $query_end;
227
							} else {
228
								// Replace {STRING} with $string, then combine with start and end
229
								$prepared_query = $query_start.str_replace('[STRING]', $string, $query_body).$query_end;
230
							}
231
							// Execute query
232
							$query = $database->query($prepared_query);
233
							// Loop though queried items
234
							if($query->numRows() > 0) {
235
								while($page = $query->fetchRow()) {
236
									// Only show this page if it hasn't already been list
237
									if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
238
										// Get page link
239
										$link = page_link($page[$fields['link']]);
240
										// Set vars to be replaced by values
241
										$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
242
										if($page[$fields['modified_when']] > 0) {
243
											$date = gmdate(DATE_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
244
											$time = gmdate(TIME_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
245
										} else {
246
											$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
247
											$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
248
										}
249
										$values = array($link, $this->stripslashes($page[$fields['title']]), $this->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']));
250
										// Show loop code with vars replaced by values
251
										echo str_replace($vars, $values, $this->stripslashes($fetch_results_loop['value']));
252
										// Say that this page or item has been listed if we can
253
										if(isset($fields['page_id'])) {
254
											$pages_listed[$page[$fields['page_id']]] = true;
255
										} elseif(isset($fields['item_id'])) {
256
											$items_listed[$page[$fields['item_id']]] = true;
257
										}
258
									}
259
								}
260
							}
261
						
262
						}
263
					}
264
				}
265
			}
266
			
267
			// Show search results_footer
268
			echo $search_results_footer;
269
			
270
		}
271
	
272
	// Say no items found if we should
273
	if($pages_listed == array() AND $items_listed == array()) {
274
		echo $fetch_no_results['value'];
275
	}
276
		
277
	}
278
	
279
	// Show search footer
280
	echo $search_footer;
281
	
282
}
283

    
284
?>
(2-2/2)