Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 286 2006-01-23 21:15:10Z 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')) { 
27
	header('Location: index.php');
28
	exit(0);
29
}
30

    
31
// Check if search is enabled
32
if(SHOW_SEARCH != true) {
33
	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
34
} else {
35
	
36
	// Make pages_listed and items_listed blank arrays
37
	$pages_listed = array();
38
	$items_listed = array();
39

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

    
256
?>
(2-2/2)