Project

General

Profile

1
<?php
2

    
3
// $Id: search.php 240 2005-11-23 15:17:50Z 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($_REQUEST['string'])) {
39
		if ($_REQUEST['match']!='exact') {
40
			$string=str_replace(',', '', $_REQUEST['string']);
41
		} else {
42
			$string=$_REQUEST['string'];
43
		}
44
		// reverse potential magic_quotes action
45
		$original_string=$wb->strip_slashes($string);
46
		// Double backslashes (mySQL needs doubly escaped backslashes in LIKE comparisons)
47
		$string = addslashes($wb->escape_backslashes($original_string));
48
		// then escape for mySQL query
49
		$search_string = htmlspecialchars($original_string,ENT_QUOTES);
50
	} else {
51
		$string = '';
52
		$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
	$exact_checked = '';
59
	if($_REQUEST['match'] != 'exact') {
60
		// 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
		if ($_REQUEST['match'] == 'any') {
70
			$any_checked = ' checked';
71
			$logical_operator = ' OR';
72
		} else {
73
			$all_checked = ' checked';
74
			$logical_operator = ' AND';
75
		}
76
	} else {
77
		$exact_checked = ' checked';
78
		$exact_string=$string;
79
		$string=array();
80
		$string[]=$exact_string;
81
	}	
82
	// 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
	// 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
	$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
	// 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
	$search_header = str_replace($vars, $values, ($fetch_header['value']));
115
	
116
	// Show search header
117
	echo $search_header;
118
	
119
	// Work-out if the user has already entered their details or not
120
	if($string != '' AND $string != ' ' AND $string != '  ' AND $string != array()) {
121
		
122
		// Show search results_header
123
		echo $search_results_header;
124
		// Search page details only, such as description, keywords, etc.
125
			$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
126
			$count = 0;
127
			foreach($string AS $each_string) {
128
				if($count != 0) { $query_pages .= $logical_operator; }
129
				$query_pages .= " visibility != 'none' AND page_title LIKE '%$each_string%' AND searching = '1'".
130
				" OR visibility != 'none' AND visibility != 'deleted' AND menu_title LIKE '%$each_string%' AND searching = '1'".
131
				" OR visibility != 'none' AND visibility != 'deleted' AND description LIKE '%$each_string%' AND searching = '1'".
132
				" OR visibility != 'none' AND visibility != 'deleted' AND keywords LIKE '%$each_string%' AND searching = '1'";
133
				$count = $count+1;
134
			}
135
			$query_pages = $database->query($query_pages);
136
		// Loop through pages
137
		if($query_pages->numRows() > 0) {
138
			while($page = $query_pages->fetchRow()) {
139
				// Get page link
140
				$link = page_link($page['link']);
141
				// Set vars to be replaced by values
142
				$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
143
				if($page['modified_when'] > 0) {
144
					$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
145
					$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
146
				} else {
147
					$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
148
					$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
149
				}
150
				$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']));
151
				// Show loop code with vars replaced by values
152
				if($values != array()) {
153
					echo str_replace($vars, $values, ($fetch_results_loop['value']));
154
				}
155
				// Say that we have already listed this page id
156
				$pages_listed[$page['page_id']] = true;
157
				// Set values to blank
158
				$value = array();
159
			}
160
		}
161
		// Get modules that have registered for custom query's to be conducted
162
		$get_modules = $database->query("SELECT value,extra FROM ".TABLE_PREFIX."search WHERE name = 'module'");
163
		// Loop through each module
164
		if($get_modules->numRows() > 0) {
165
			while($module = $get_modules->fetchRow()) {
166
				// Get module name
167
				$module_name = $module['value'];
168
				// Get fields to use for title, link, etc.
169
				$fields = unserialize($module['extra']);
170
				// Get query start
171
				$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
172
				if($get_query_start->numRows() > 0) {
173
					// Fetch query start
174
					$fetch_query_start = $get_query_start->fetchRow();
175
					// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
176
					$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
177
					// Get query end
178
					$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
179
					if($get_query_end->numRows() > 0) {
180
						// Fetch query start
181
						$fetch_query_end = $get_query_end->fetchRow();
182
						// Set query end
183
						$query_end = ($fetch_query_end['value']);
184
						// Get query body
185
						$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
186
						if($get_query_body->numRows() > 0) {
187
							// Fetch query start
188
							$fetch_query_body = $get_query_body->fetchRow();
189
							// Prepare query body for execution by replacing {STRING} with the correct one
190
							$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
191
							// Loop through query body for each string, then combine with start and end
192
							$prepared_query = $query_start;
193
							$count = 0;
194
							foreach($string AS $each_string) {
195
								if($count != 0) { $prepared_query .= $logical_operator; }
196
								$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
197
								$count = $count+1;
198
							}
199
							$prepared_query .= $query_end;
200
							// Execute query
201
							$query = $database->query($prepared_query);
202
							// Loop though queried items
203
							if($query->numRows() > 0) {
204
								while($page = $query->fetchRow()) {
205
									// Only show this page if it hasn't already been list
206
									if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
207
										// Get page link
208
										$link = page_link($page[$fields['link']]);
209
										// Set vars to be replaced by values
210
										$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
211
										if($page[$fields['modified_when']] > 0) {
212
											$date = gmdate(DATE_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
213
											$time = gmdate(TIME_FORMAT, $page[$fields['modified_when']]+TIMEZONE);
214
										} else {
215
											$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
216
											$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
217
										}
218
										$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']));
219
										// Show loop code with vars replaced by values
220
										echo str_replace($vars, $values, ($fetch_results_loop['value']));
221
										// Say that this page or item has been listed if we can
222
										if(isset($fields['page_id'])) {
223
											$pages_listed[$page[$fields['page_id']]] = true;
224
										} elseif(isset($fields['item_id'])) {
225
											$items_listed[$page[$fields['item_id']]] = true;
226
										}
227
									}
228
								}
229
							}
230
						
231
						}
232
					}
233
				}
234
			}
235
			
236
			// Show search results_footer
237
			echo $search_results_footer;
238
			
239
		}
240
	
241
	// Say no items found if we should
242
	if($pages_listed == array() AND $items_listed == array()) {
243
		echo $fetch_no_results['value'];
244
	}
245
		
246
	}
247
	
248
	// Show search footer
249
	echo $search_footer;
250
	
251
}
252

    
253
?>
(2-2/2)