Project

General

Profile

« Previous | Next » 

Revision 437

Added by Matthias over 17 years ago

Added new function for highlighting search results in the content area; Fixed some issues in search with special chars; (Special thanks to "thorn" and "Funky_MF")

View differences:

search.php
39 39

  
40 40
	// Get the search type
41 41
	$match = 'all';
42
    if(isset($_REQUEST['match'])) {
43
        $match = $_REQUEST['match'];
44
    }
42
	if(isset($_REQUEST['match'])) {
43
		$match = $_REQUEST['match'];
44
	}
45 45

  
46 46
	// Get search string
47 47
	if(isset($_REQUEST['string'])) {
......
54 54
		$original_string=$wb->strip_slashes($string);
55 55
		// Double backslashes (mySQL needs doubly escaped backslashes in LIKE comparisons)
56 56
		$string = addslashes($wb->escape_backslashes($original_string));
57
		// then escape for mySQL query
58
		$search_string = htmlspecialchars($original_string,ENT_QUOTES);
57
		// convert a copy of $string to HTML-ENTITIES
58
		$string_entities=mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
59
		$string_entities=htmlspecialchars($string_entities,ENT_QUOTES);
60
		$string_entities=mb_convert_encoding($string_entities, 'HTML-ENTITIES', 'UTF-8');
61
		// and do some convertion to both
62
		require(WB_PATH.'/search/search_convert.php');
63
		$string=strtr($string,$string_conv);
64
		$string_entities=strtr($string_entities,$string_entities_conv);
65
		$search_string = $string_entities;
59 66
	} else {
60 67
		$string = '';
61 68
		$search_string = '';
......
75 82
				$string[] = $each_exploded_string;
76 83
			}
77 84
		}
85
		// Split $string_entities, too
86
		$exploded_string = explode(' ', $string_entities);
87
		// Make sure there is no blank values in the array
88
		$string_entities = array();
89
		foreach($exploded_string AS $each_exploded_string) {
90
			if($each_exploded_string != '') {
91
				$string_entities[] = $each_exploded_string;
92
			}
93
		}
78 94
		if ($match == 'any') {
79 95
			$any_checked = ' checked="checked"';
80 96
			$logical_operator = ' OR';
......
87 103
		$exact_string=$string;
88 104
		$string=array();
89 105
		$string[]=$exact_string;
106
		$exact_string=$string_entities;
107
		$string_entities=array();
108
		$string_entities[]=$exact_string;
90 109
	}	
91 110
	// Get list of usernames and display names
92 111
	$query_users = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
......
131 150
		// Show search results_header
132 151
		echo $search_results_header;
133 152
		// Search page details only, such as description, keywords, etc.
134
			$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
135
			$count = 0;
136
			foreach($string AS $each_string) {
137
				if($count != 0) { $query_pages .= $logical_operator; }
138
				$query_pages .= " visibility != 'none' AND page_title LIKE '%$each_string%' AND searching = '1'".
139
				" OR visibility != 'none' AND visibility != 'deleted' AND menu_title LIKE '%$each_string%' AND searching = '1'".
140
				" OR visibility != 'none' AND visibility != 'deleted' AND description LIKE '%$each_string%' AND searching = '1'".
141
				" OR visibility != 'none' AND visibility != 'deleted' AND keywords LIKE '%$each_string%' AND searching = '1'";
142
				$count = $count+1;
153
		$query_pages = "SELECT page_id, page_title, menu_title, link, description, modified_when, modified_by FROM ".TABLE_PREFIX."pages WHERE ";
154
		$count = 0;
155
		foreach($string AS $each_string) {
156
			if($count != 0) { 
157
				$query_pages .= $logical_operator;
143 158
			}
144
			$query_pages = $database->query($query_pages);
159
			$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
160
			" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
161
			$count = $count+1;
162
		}
163
		$count = 0;
164
		$query_pages .= ' OR';
165
		foreach($string_entities AS $each_string) {
166
			if($count != 0) { 
167
				$query_pages .= $logical_operator;
168
			}
169
			$query_pages .= " visibility != 'none' AND visibility != 'deleted' AND searching = '1'".
170
			" AND (page_title LIKE '%$each_string%' OR menu_title LIKE '%$each_string%' OR description LIKE '%$each_string%' OR keywords LIKE '%$each_string%')";
171
			$count = $count+1;
172
		}
173
		$query_pages = $database->query($query_pages);
145 174
		// Loop through pages
146 175
		if($query_pages->numRows() > 0) {
147 176
			while($page = $query_pages->fetchRow()) {
148 177
				// Get page link
149 178
				$link = page_link($page['link']);
179
				
180
				//Add search string for highlighting
181
				$sstring = implode(" ", array_merge($string,$string_entities));
182
				//$link = $link."?searchresult=1&sstring=".$sstring;
183
				$link = $link."?searchresult=1&sstring=".urlencode($sstring);
184
				
150 185
				// Set vars to be replaced by values
151 186
				$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
152 187
				if($page['modified_when'] > 0) {
......
201 236
							$prepared_query = $query_start;
202 237
							$count = 0;
203 238
							foreach($string AS $each_string) {
204
								if($count != 0) { $prepared_query .= $logical_operator; }
239
								if($count != 0) {
240
									$prepared_query .= $logical_operator;
241
								}
205 242
								$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
206 243
								$count = $count+1;
207 244
							}
245
							$count=0;
246
							$prepared_query .= ' OR ';
247
							foreach($string_entities AS $each_string) {
248
								if($count != 0) {
249
									$prepared_query .= $logical_operator;
250
								}
251
								$prepared_query .= str_replace('[STRING]', $each_string, $query_body);
252
								$count = $count+1;
253
							}
254
							
208 255
							$prepared_query .= $query_end;
256
							
209 257
							// Execute query
210 258
							$query = $database->query($prepared_query);
211 259
							// Loop though queried items
......
215 263
									if(!isset($fields['page_id']) OR !isset($pages_listed[$page[$fields['page_id']]])) {
216 264
										// Get page link
217 265
										$link = page_link($page[$fields['link']]);
266
										
267
										//Add search string for highlighting
268
										$sstring = implode(" ", array_merge($string,$string_entities));
269
										//$link = $link."?searchresult=1&sstring=".$sstring;
270
										$link = $link."?searchresult=1&sstring=".urlencode($sstring);
271
										
218 272
										// Set vars to be replaced by values
219 273
										$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]');
220 274
										if($page[$fields['modified_when']] > 0) {
......
236 290
									}
237 291
								}
238 292
							}
239
						
240 293
						}
241 294
					}
242 295
				}
......
247 300
			
248 301
		}
249 302
	
250
	// Say no items found if we should
251
	if($pages_listed == array() AND $items_listed == array()) {
252
		echo $fetch_no_results['value'];
253
	}
303
		// Say no items found if we should
304
		if($pages_listed == array() AND $items_listed == array()) {
305
			echo $fetch_no_results['value'];
306
		}
254 307
		
255 308
	}
256 309
	

Also available in: Unified diff