| 1 | <?php
 | 
  
    | 2 | 
 | 
  
    | 3 | // $Id: search.php 670 2008-02-08 16:47:43Z thorn $
 | 
  
    | 4 | 
 | 
  
    | 5 | /*
 | 
  
    | 6 | 
 | 
  
    | 7 |  Website Baker Project <http://www.websitebaker.org/>
 | 
  
    | 8 |  Copyright (C) 2004-2008, 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 | // most of this is still the same code as in 2.6.7, but rearranged heavily
 | 
  
    | 27 | 
 | 
  
    | 28 | if(!defined('WB_URL')) { 
 | 
  
    | 29 | 	header('Location: index.php');
 | 
  
    | 30 | 	exit(0);
 | 
  
    | 31 | }
 | 
  
    | 32 | 
 | 
  
    | 33 | // Include the WB functions file
 | 
  
    | 34 | require_once(WB_PATH.'/framework/functions.php');
 | 
  
    | 35 | 
 | 
  
    | 36 | // Check if search is enabled
 | 
  
    | 37 | if(SHOW_SEARCH != true) {
 | 
  
    | 38 | 	echo $TEXT['SEARCH'].' '.$TEXT['DISABLED'];
 | 
  
    | 39 | 	return;
 | 
  
    | 40 | }
 | 
  
    | 41 | 
 | 
  
    | 42 | // this is for timing-tests only
 | 
  
    | 43 | //$overall_start_time = microtime(true);
 | 
  
    | 44 | 
 | 
  
    | 45 | // search-module-extension: get helper-functions
 | 
  
    | 46 | require_once(WB_PATH.'/search/search_modext.php');
 | 
  
    | 47 | // search-module-extension: Get "search.php" for each module, if present
 | 
  
    | 48 | // looks in modules/module/ and modules/module_searchext/
 | 
  
    | 49 | $search_funcs = array();
 | 
  
    | 50 | $query = $database->query("SELECT DISTINCT directory FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory NOT LIKE '%_searchext'");
 | 
  
    | 51 | if($query->numRows() > 0) {
 | 
  
    | 52 | 	while($module = $query->fetchRow()) {
 | 
  
    | 53 | 		$func_name = $module['directory']."_search";
 | 
  
    | 54 | 		$file = WB_PATH.'/modules/'.$module['directory'].'/search.php';
 | 
  
    | 55 | 		if(!file_exists($file)) {
 | 
  
    | 56 | 			$file = WB_PATH.'/modules/'.$module['directory'].'_searchext/search.php';
 | 
  
    | 57 | 			if(!file_exists($file)) {
 | 
  
    | 58 | 				$file='';
 | 
  
    | 59 | 			}
 | 
  
    | 60 | 		}
 | 
  
    | 61 | 		if($file!='') {
 | 
  
    | 62 | 			include_once($file);
 | 
  
    | 63 | 			if(function_exists($module['directory']."_search")) {
 | 
  
    | 64 | 				$search_funcs[$module['directory']] = $func_name;
 | 
  
    | 65 | 			}
 | 
  
    | 66 | 		}
 | 
  
    | 67 | 	}
 | 
  
    | 68 | }
 | 
  
    | 69 | 
 | 
  
    | 70 | // Get the search type
 | 
  
    | 71 | $match = 'all';
 | 
  
    | 72 | if(isset($_REQUEST['match'])) {
 | 
  
    | 73 | 	$match = $wb->add_slashes(strip_tags($_REQUEST['match']));
 | 
  
    | 74 | }
 | 
  
    | 75 | 
 | 
  
    | 76 | // Get the path to search into. Normally left blank
 | 
  
    | 77 | /* possible values:
 | 
  
    | 78 |  * - a single path: "/en/" - search only pages whose link contains 'path' ("/en/machinery/bender-x09")
 | 
  
    | 79 |  * - a bunch of alternative pathes: "/en/,/machinery/,docs/" - alternatives paths, seperated by comma
 | 
  
    | 80 |  * - a bunch of paths to exclude: "-/about,/info,/jp/,/light" - search all, exclude these.
 | 
  
    | 81 |  * These different styles can't be mixed.
 | 
  
    | 82 |  */
 | 
  
    | 83 | $search_path_SQL = "";
 | 
  
    | 84 | $search_path = "";
 | 
  
    | 85 | if(isset($_REQUEST['search_path'])) {
 | 
  
    | 86 | 	$search_path = $wb->add_slashes(strip_tags($_REQUEST['search_path']));
 | 
  
    | 87 | 	if($search_path != '') {
 | 
  
    | 88 | 		$search_path_SQL = "AND ( ";
 | 
  
    | 89 | 		$not = "";
 | 
  
    | 90 | 		$op = "OR";
 | 
  
    | 91 | 		if($search_path[0] == '-') {
 | 
  
    | 92 | 			$not = "NOT";
 | 
  
    | 93 | 			$op = "AND";
 | 
  
    | 94 | 			$paths = explode(',', substr($search_path, 1) );
 | 
  
    | 95 | 		} else {
 | 
  
    | 96 | 			$paths = explode(',',$search_path);
 | 
  
    | 97 | 		}
 | 
  
    | 98 | 		$i=0;
 | 
  
    | 99 | 		foreach($paths as $p) {
 | 
  
    | 100 | 			if($i++ > 0) {
 | 
  
    | 101 | 				$search_path_SQL .= " $op";
 | 
  
    | 102 | 			}
 | 
  
    | 103 | 			$search_path_SQL .= " link $not LIKE '%$p%'";			
 | 
  
    | 104 | 		}
 | 
  
    | 105 | 		$search_path_SQL .= " )";
 | 
  
    | 106 | 	}
 | 
  
    | 107 | }
 | 
  
    | 108 | 
 | 
  
    | 109 | // TODO: with the new method, there is no need for search_entities_string anymore.
 | 
  
    | 110 | //   When the old method disappears, it can be removed, too.
 | 
  
    | 111 | //   BTW: in this case, there is no need for 
 | 
  
    | 112 | //   $text = umlauts_to_entities(strip_tags($content), strtoupper(DEFAULT_CHARSET), 0);
 | 
  
    | 113 | //   in wb/modules/wysiwyg/save.php anymore, too. Change that back to $text=strip_tags($content);
 | 
  
    | 114 | 
 | 
  
    | 115 | // Get search string
 | 
  
    | 116 | $search_normal_string = 'unset';
 | 
  
    | 117 | $search_entities_string = 'unset';
 | 
  
    | 118 | $search_display_string = '';
 | 
  
    | 119 | $string = '';
 | 
  
    | 120 | if(isset($_REQUEST['string'])) {
 | 
  
    | 121 | 	if ($match!='exact') {
 | 
  
    | 122 | 		$string=str_replace(',', '', $_REQUEST['string']);
 | 
  
    | 123 | 	} else {
 | 
  
    | 124 | 		$string=$_REQUEST['string']; // $string will be cleaned below
 | 
  
    | 125 | 	}
 | 
  
    | 126 | 	// redo possible magic quotes
 | 
  
    | 127 | 	$string = $wb->strip_slashes($string);
 | 
  
    | 128 | 	$string = htmlspecialchars($string);
 | 
  
    | 129 | 	$search_display_string = $string;
 | 
  
    | 130 | 	$string = addslashes($string);
 | 
  
    | 131 | 	// remove some bad chars
 | 
  
    | 132 | 	$string = preg_replace("/(^|\s+)([.])+(?=\s+|$)/", "", $string);
 | 
  
    | 133 | 	// mySQL needs four backslashes to match one in LIKE comparisons)
 | 
  
    | 134 | 	$string = str_replace('\\\\', '\\\\\\\\', $string);
 | 
  
    | 135 | 	$string = trim($string);
 | 
  
    | 136 | 	// convert a copy of $string to HTML-ENTITIES
 | 
  
    | 137 | 	$string_entities = umlauts_to_entities($string);
 | 
  
    | 138 | 	$search_normal_string = $string;
 | 
  
    | 139 | 	$search_entities_string = $string_entities;
 | 
  
    | 140 | }
 | 
  
    | 141 | 
 | 
  
    | 142 | // Get list of usernames and display names
 | 
  
    | 143 | $query = $database->query("SELECT user_id,username,display_name FROM ".TABLE_PREFIX."users");
 | 
  
    | 144 | $users = array('0' => array('display_name' => $TEXT['UNKNOWN'], 'username' => strtolower($TEXT['UNKNOWN'])));
 | 
  
    | 145 | if($query->numRows() > 0) {
 | 
  
    | 146 | 	while($user = $query->fetchRow()) {
 | 
  
    | 147 | 		$users[$user['user_id']] = array('display_name' => $user['display_name'], 'username' => $user['username']);
 | 
  
    | 148 | 	}
 | 
  
    | 149 | }
 | 
  
    | 150 | 
 | 
  
    | 151 | // Get search settings
 | 
  
    | 152 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'header' LIMIT 1");
 | 
  
    | 153 | $fetch_header = $query->fetchRow();
 | 
  
    | 154 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'footer' LIMIT 1");
 | 
  
    | 155 | $fetch_footer = $query->fetchRow();
 | 
  
    | 156 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_header' LIMIT 1");
 | 
  
    | 157 | $fetch_results_header = $query->fetchRow();
 | 
  
    | 158 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_footer' LIMIT 1");
 | 
  
    | 159 | $fetch_results_footer = $query->fetchRow();
 | 
  
    | 160 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'results_loop' LIMIT 1");
 | 
  
    | 161 | $fetch_results_loop = $query->fetchRow();
 | 
  
    | 162 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'no_results' LIMIT 1");
 | 
  
    | 163 | $fetch_no_results = $query->fetchRow();
 | 
  
    | 164 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'module_order' LIMIT 1");
 | 
  
    | 165 | if($query->numRows() > 0) { $fetch_module_order = $query->fetchRow();
 | 
  
    | 166 | } else { $fetch_module_order['value'] = ""; }
 | 
  
    | 167 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'max_excerpt' LIMIT 1");
 | 
  
    | 168 | if($query->numRows() > 0) { $fetch_max_excerpt = $query->fetchRow();
 | 
  
    | 169 | } else { $fetch_max_excerpt['value'] = '15'; }
 | 
  
    | 170 | $search_max_excerpt = (int)$fetch_max_excerpt['value'];
 | 
  
    | 171 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_show_description' LIMIT 1");
 | 
  
    | 172 | if($query->numRows() > 0) { $fetch_cfg_show_description = $query->fetchRow();
 | 
  
    | 173 | } else { $fetch_cfg_show_description['value'] = 'true'; }
 | 
  
    | 174 | if($fetch_cfg_show_description['value'] == 'false') { $cfg_show_description = false;
 | 
  
    | 175 | } else { $cfg_show_description = true; }
 | 
  
    | 176 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_search_description' LIMIT 1");
 | 
  
    | 177 | if($query->numRows() > 0) { $fetch_cfg_search_description = $query->fetchRow();
 | 
  
    | 178 | } else { $fetch_cfg_search_description['value'] = 'true'; }
 | 
  
    | 179 | if($fetch_cfg_search_description['value'] == 'false') { $cfg_search_description = false;
 | 
  
    | 180 | } else { $cfg_search_description = true; }
 | 
  
    | 181 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_search_keywords' LIMIT 1");
 | 
  
    | 182 | if($query->numRows() > 0) { $fetch_cfg_search_keywords = $query->fetchRow();
 | 
  
    | 183 | } else { $fetch_cfg_search_keywords['value'] = 'true'; }
 | 
  
    | 184 | if($fetch_cfg_search_keywords['value'] == 'false') { $cfg_search_keywords = false;
 | 
  
    | 185 | } else { $cfg_search_keywords = true; }
 | 
  
    | 186 | $query = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'cfg_enable_old_search' LIMIT 1");
 | 
  
    | 187 | if($query->numRows() > 0) { $fetch_cfg_enable_old_search = $query->fetchRow();
 | 
  
    | 188 | } else { $fetch_cfg_enable_old_search['value'] = 'true'; }
 | 
  
    | 189 | if($fetch_cfg_enable_old_search['value'] == 'false') { $cfg_enable_old_search = false;
 | 
  
    | 190 | } else { $cfg_enable_old_search = true; }
 | 
  
    | 191 | // Replace vars in search settings with values
 | 
  
    | 192 | $vars = array('[SEARCH_STRING]', '[WB_URL]', '[PAGE_EXTENSION]', '[TEXT_RESULTS_FOR]');
 | 
  
    | 193 | $values = array($search_display_string, WB_URL, PAGE_EXTENSION, $TEXT['RESULTS_FOR']);
 | 
  
    | 194 | $search_footer = str_replace($vars, $values, ($fetch_footer['value']));
 | 
  
    | 195 | $search_results_header = str_replace($vars, $values, ($fetch_results_header['value']));
 | 
  
    | 196 | $search_results_footer = str_replace($vars, $values, ($fetch_results_footer['value']));
 | 
  
    | 197 | $search_module_order = $fetch_module_order['value'];
 | 
  
    | 198 | 
 | 
  
    | 199 | // check $search_max_excerpt
 | 
  
    | 200 | if(!is_numeric($search_max_excerpt)) {
 | 
  
    | 201 | 	$search_max_excerpt = 15;
 | 
  
    | 202 | }
 | 
  
    | 203 | 
 | 
  
    | 204 | // Work-out what to do (match all words, any words, or do exact match), and do relevant with query settings
 | 
  
    | 205 | $all_checked = '';
 | 
  
    | 206 | $any_checked = '';
 | 
  
    | 207 | $exact_checked = '';
 | 
  
    | 208 | $search_normal_array = array();
 | 
  
    | 209 | $search_entities_array = array();
 | 
  
    | 210 | if($match != 'exact') {
 | 
  
    | 211 | 	// Split string into array with explode() function
 | 
  
    | 212 | 	$exploded_string = explode(' ', $search_normal_string);
 | 
  
    | 213 | 	// Make sure there is no blank values in the array
 | 
  
    | 214 | 	foreach($exploded_string AS $each_exploded_string) {
 | 
  
    | 215 | 		if($each_exploded_string != '') {
 | 
  
    | 216 | 			$search_normal_array[] = $each_exploded_string;
 | 
  
    | 217 | 		}
 | 
  
    | 218 | 	}
 | 
  
    | 219 | 	// Split $string_entities, too
 | 
  
    | 220 | 	$exploded_string = explode(' ', $search_entities_string);
 | 
  
    | 221 | 	// Make sure there is no blank values in the array
 | 
  
    | 222 | 	foreach($exploded_string AS $each_exploded_string) {
 | 
  
    | 223 | 		if($each_exploded_string != '') {
 | 
  
    | 224 | 			$search_entities_array[] = $each_exploded_string;
 | 
  
    | 225 | 		}
 | 
  
    | 226 | 	}
 | 
  
    | 227 | 	if ($match == 'any') {
 | 
  
    | 228 | 		$any_checked = ' checked="checked"';
 | 
  
    | 229 | 		$logical_operator = ' OR';
 | 
  
    | 230 | 	} else {
 | 
  
    | 231 | 		$all_checked = ' checked="checked"';
 | 
  
    | 232 | 		$logical_operator = ' AND';
 | 
  
    | 233 | 	}
 | 
  
    | 234 | } else {
 | 
  
    | 235 | 	$exact_checked = ' checked="checked"';
 | 
  
    | 236 | 	$exact_string=$search_normal_string;
 | 
  
    | 237 | 	$search_normal_array[]=$exact_string;
 | 
  
    | 238 | 	$exact_string=$search_entities_string;
 | 
  
    | 239 | 	$search_entities_array[]=$exact_string;
 | 
  
    | 240 | }	
 | 
  
    | 241 | // make an extra copy of $string_entities for use in a regex
 | 
  
    | 242 | require_once(WB_PATH.'/search/search_convert.php');
 | 
  
    | 243 | $search_words = array();
 | 
  
    | 244 | foreach ($search_entities_array AS $str) {
 | 
  
    | 245 | 	$str = entities_to_umlauts($str, 'UTF-8');
 | 
  
    | 246 | 	$str = preg_quote($str, '/');
 | 
  
    | 247 | 	$str = strtr($str, $string_ul_umlauts);
 | 
  
    | 248 | 	// special-feature: '|' means word-boundary (\b). Searching for 'the|' will find the, but not thema.
 | 
  
    | 249 | 	// this doesn't work correctly for unicode-chars: '|test' will work, but '|über' not.
 | 
  
    | 250 | 	$str = strtr($str, array('\\|'=>'\b'));
 | 
  
    | 251 | 	$search_words[] = $str;
 | 
  
    | 252 | }
 | 
  
    | 253 | 
 | 
  
    | 254 | // Do extra vars/values replacement
 | 
  
    | 255 | $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]', '[REFERRER_ID]', '[SEARCH_PATH]');
 | 
  
    | 256 | $values = array($search_display_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, REFERRER_ID, $search_path);
 | 
  
    | 257 | $search_header = str_replace($vars, $values, ($fetch_header['value']));
 | 
  
    | 258 | $vars = array('[TEXT_NO_RESULTS]');
 | 
  
    | 259 | $values = array($TEXT['NO_RESULTS']);
 | 
  
    | 260 | $search_no_results = str_replace($vars, $values, ($fetch_no_results['value']));
 | 
  
    | 261 | 
 | 
  
    | 262 | // Show search header
 | 
  
    | 263 | echo $search_header;
 | 
  
    | 264 | // Show search results_header
 | 
  
    | 265 | echo $search_results_header;
 | 
  
    | 266 | 
 | 
  
    | 267 | // Work-out if the user has already entered their details or not
 | 
  
    | 268 | if($search_normal_string != '') {
 | 
  
    | 269 | 
 | 
  
    | 270 | 	// Get modules
 | 
  
    | 271 | 	$table = TABLE_PREFIX."sections";
 | 
  
    | 272 | 	$get_modules = $database->query("SELECT DISTINCT module FROM $table WHERE module != '' ");
 | 
  
    | 273 | 	$modules = array();
 | 
  
    | 274 | 	if($get_modules->numRows() > 0) {
 | 
  
    | 275 | 		while($module = $get_modules->fetchRow()) {
 | 
  
    | 276 | 			$modules[] = $module['module']; // $modules is an array of strings
 | 
  
    | 277 | 		}
 | 
  
    | 278 | 	}
 | 
  
    | 279 | 
 | 
  
    | 280 | 	// sort module search-order
 | 
  
    | 281 | 	// get the modules from $search_module_order first ...
 | 
  
    | 282 | 	$sorted_modules = array();
 | 
  
    | 283 | 	$m = count($modules);
 | 
  
    | 284 | 	$search_modules = explode(',', $search_module_order);
 | 
  
    | 285 | 	foreach($search_modules AS $item) {
 | 
  
    | 286 | 		$item = trim($item);
 | 
  
    | 287 | 		for($i=0; $i < $m; $i++) {
 | 
  
    | 288 | 			if(isset($modules[$i]) && $modules[$i] == $item) {
 | 
  
    | 289 | 				$sorted_modules[] = $modules[$i];
 | 
  
    | 290 | 				unset($modules[$i]);
 | 
  
    | 291 | 				break;
 | 
  
    | 292 | 			}
 | 
  
    | 293 | 		}
 | 
  
    | 294 | 	}
 | 
  
    | 295 | 	// ... then add the rest
 | 
  
    | 296 | 	foreach($modules AS $item) {
 | 
  
    | 297 | 		$sorted_modules[] = $item;
 | 
  
    | 298 | 	}
 | 
  
    | 299 | 
 | 
  
    | 300 | 	// First, use an alternative search-method, without sql's 'LIKE'.
 | 
  
    | 301 | 	// 'LIKE' won't find upper/lower-variants of umlauts, cyrillic or greek chars without propperly set setlocale();
 | 
  
    | 302 | 	// and even if setlocale() is set, it won't work for multi-linguale sites.
 | 
  
    | 303 | 	// Use the search-module-extension instead.
 | 
  
    | 304 | 	// This is somewhat slower than the orginial method.
 | 
  
    | 305 | 	// CHANGES WITH V1.3: before V1.3 we called [module]-search() for a given page only once and searched in all [module]-sections on this page;
 | 
  
    | 306 | 	// since V1.3 we call [module]-search() for very single section.
 | 
  
    | 307 | 	$seen_pages = array(); // seen pages per module.
 | 
  
    | 308 | 	$pages_listed = array(); // seen pages.
 | 
  
    | 309 | 	foreach($sorted_modules AS $module_name) {
 | 
  
    | 310 | 		$seen_pages[$module_name] = array();
 | 
  
    | 311 | 		if(!isset($search_funcs[$module_name])) {
 | 
  
    | 312 | 			continue; // there is no search_func for this module
 | 
  
    | 313 | 		}
 | 
  
    | 314 | 		// get each section for $module_name
 | 
  
    | 315 | 		$table_s = TABLE_PREFIX."sections";	
 | 
  
    | 316 | 		$table_p = TABLE_PREFIX."pages";
 | 
  
    | 317 | 		$sections_query = $database->query("
 | 
  
    | 318 | 			SELECT s.section_id, s.page_id, s.module, s.publ_start, s.publ_end,
 | 
  
    | 319 | 			       p.page_title, p.menu_title, p.link, p.description, p.keywords, p.modified_when, p.modified_by,
 | 
  
    | 320 | 			       p.visibility, p.viewing_groups, p.viewing_users
 | 
  
    | 321 | 			FROM $table_s AS s INNER JOIN $table_p AS p ON s.page_id = p.page_id
 | 
  
    | 322 | 			WHERE s.module = '$module_name' AND p.visibility NOT IN ('none','deleted') AND p.searching = '1' $search_path_SQL
 | 
  
    | 323 | 			ORDER BY s.section_id, s.position ASC
 | 
  
    | 324 | 		");
 | 
  
    | 325 | 		if($sections_query->numRows() > 0) {
 | 
  
    | 326 | 			while($res = $sections_query->fetchRow()) {
 | 
  
    | 327 | 				// TODO: add a "section is searchable: yes/no" config-element into "Manage Sections" - ???
 | 
  
    | 328 | 				//       and show this section only if section is searchable
 | 
  
    | 329 | 				// Only show this section if it is not "out of publication-date"
 | 
  
    | 330 | 				$now = time();
 | 
  
    | 331 | 				if( !( $now<$res['publ_end'] && ($now>$res['publ_start'] || $res['publ_start']==0) ||
 | 
  
    | 332 | 					$now>$res['publ_start'] && $res['publ_end']==0) ) {
 | 
  
    | 333 | 					continue;
 | 
  
    | 334 | 				}
 | 
  
    | 335 | 				$search_func_vars = array(
 | 
  
    | 336 | 					'database' => $database,
 | 
  
    | 337 | 					'page_id' => $res['page_id'],
 | 
  
    | 338 | 					'section_id' => $res['section_id'],
 | 
  
    | 339 | 					'page_title' => $res['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
 | 
  
    | 340 | 					'page_menu_title' => $res['page_title'],
 | 
  
    | 341 | 					'page_description' => ($cfg_show_description?$res['description']:""),
 | 
  
    | 342 | 					'page_keywords' => $res['keywords'],
 | 
  
    | 343 | 					'page_link' => $res['link'],
 | 
  
    | 344 | 					'page_modified_when' => $res['modified_when'],
 | 
  
    | 345 | 					'page_modified_by' => $res['modified_by'],
 | 
  
    | 346 | 					'users' => $users,
 | 
  
    | 347 | 					'search_words' => $search_words, // needed for preg_match_all
 | 
  
    | 348 | 					'search_match' => $match,
 | 
  
    | 349 | 					'search_url_array' => $search_normal_array, // needed for url-string only
 | 
  
    | 350 | 					'results_loop_string' => $fetch_results_loop['value'],
 | 
  
    | 351 | 					'default_max_excerpt' => $search_max_excerpt
 | 
  
    | 352 | 				);
 | 
  
    | 353 | 				// Only show this page if we are allowed to see it
 | 
  
    | 354 | 				if($admin->page_is_visible($res) == false) {
 | 
  
    | 355 | 					if($res['visibility'] == 'registered') { // don't show excerpt
 | 
  
    | 356 | 						$search_func_vars['default_max_excerpt'] = 0;
 | 
  
    | 357 | 						$search_func_vars['page_description'] = $TEXT['REGISTERED'];
 | 
  
    | 358 | 					} else { // private
 | 
  
    | 359 | 						continue;
 | 
  
    | 360 | 					}
 | 
  
    | 361 | 				}
 | 
  
    | 362 | 				$uf_res = call_user_func($search_funcs[$module_name], $search_func_vars);
 | 
  
    | 363 | 				if($uf_res) {
 | 
  
    | 364 | 					$pages_listed[$res['page_id']] = true;
 | 
  
    | 365 | 					$seen_pages[$module_name][$res['page_id']] = true;
 | 
  
    | 366 | 				} else {
 | 
  
    | 367 | 					$seen_pages[$module_name][$res['page_id']] = true;
 | 
  
    | 368 | 				}
 | 
  
    | 369 | 			}
 | 
  
    | 370 | 		}
 | 
  
    | 371 | 	}
 | 
  
    | 372 | 
 | 
  
    | 373 | 	// Search page details only, such as description, keywords, etc, but only of unseen pages.
 | 
  
    | 374 | 	$max_excerpt_num = 0; // we don't want excerpt here
 | 
  
    | 375 | 	$divider = ".";
 | 
  
    | 376 | 	$table = TABLE_PREFIX."pages";
 | 
  
    | 377 | 	$query_pages = $database->query("
 | 
  
    | 378 | 		SELECT page_id, page_title, menu_title, link, description, keywords, modified_when, modified_by,
 | 
  
    | 379 | 		       visibility, viewing_groups, viewing_users
 | 
  
    | 380 | 		FROM $table
 | 
  
    | 381 | 		WHERE visibility NOT IN ('none','deleted') AND searching = '1' $search_path_SQL"
 | 
  
    | 382 | 	);
 | 
  
    | 383 | 	if($query_pages->numRows() > 0) {
 | 
  
    | 384 | 		while($page = $query_pages->fetchRow()) {
 | 
  
    | 385 | 			if (isset($pages_listed[$page['page_id']])) {
 | 
  
    | 386 | 				continue;
 | 
  
    | 387 | 			}
 | 
  
    | 388 | 			$func_vars = array(
 | 
  
    | 389 | 				'database' => $database,
 | 
  
    | 390 | 				'page_id' => $page['page_id'],
 | 
  
    | 391 | 				'page_title' => $page['menu_title'], // had to change this, since the link is build from page_title (changeset #593)
 | 
  
    | 392 | 				'page_menu_title' => $page['page_title'],
 | 
  
    | 393 | 				'page_description' => ($cfg_show_description?$page['description']:""),
 | 
  
    | 394 | 				'page_keywords' => $page['keywords'],
 | 
  
    | 395 | 				'page_link' => $page['link'],
 | 
  
    | 396 | 				'page_modified_when' => $page['modified_when'],
 | 
  
    | 397 | 				'page_modified_by' => $page['modified_by'],
 | 
  
    | 398 | 				'users' => $users,
 | 
  
    | 399 | 				'search_words' => $search_words, // needed for preg_match_all
 | 
  
    | 400 | 				'search_match' => $match,
 | 
  
    | 401 | 				'search_url_array' => $search_normal_array, // needed for url-string only
 | 
  
    | 402 | 				'results_loop_string' => $fetch_results_loop['value'],
 | 
  
    | 403 | 				'default_max_excerpt' => $max_excerpt_num
 | 
  
    | 404 | 			);
 | 
  
    | 405 | 			// Only show this page if we are allowed to see it
 | 
  
    | 406 | 			if($admin->page_is_visible($page) == false) {
 | 
  
    | 407 | 				if($page['visibility'] != 'registered') {
 | 
  
    | 408 | 					continue;
 | 
  
    | 409 | 				} else { // page: registered, user: access denied
 | 
  
    | 410 | 					$func_vars['page_description'] = 'registered';
 | 
  
    | 411 | 				}
 | 
  
    | 412 | 			}
 | 
  
    | 413 | 			if($admin->page_is_active($page) == false) {
 | 
  
    | 414 | 				continue;
 | 
  
    | 415 | 			}
 | 
  
    | 416 | 			$text = $func_vars['page_title'].$divider
 | 
  
    | 417 | 				.$func_vars['page_menu_title'].$divider
 | 
  
    | 418 | 				.($cfg_search_description?$func_vars['page_description']:"").$divider
 | 
  
    | 419 | 				.($cfg_search_keywords?$func_vars['page_keywords']:"").$divider;
 | 
  
    | 420 | 			$mod_vars = array(
 | 
  
    | 421 | 				'page_link' => $func_vars['page_link'],
 | 
  
    | 422 | 				'page_link_target' => "",
 | 
  
    | 423 | 				'page_title' => $func_vars['page_title'],
 | 
  
    | 424 | 				'page_description' => $func_vars['page_description'],
 | 
  
    | 425 | 				'page_modified_when' => $func_vars['page_modified_when'],
 | 
  
    | 426 | 				'page_modified_by' => $func_vars['page_modified_by'],
 | 
  
    | 427 | 				'text' => $text,
 | 
  
    | 428 | 				'max_excerpt_num' => $func_vars['default_max_excerpt']
 | 
  
    | 429 | 			);
 | 
  
    | 430 | 			if(print_excerpt2($mod_vars, $func_vars)) {
 | 
  
    | 431 | 				$pages_listed[$page['page_id']] = true;
 | 
  
    | 432 | 			}
 | 
  
    | 433 | 		}
 | 
  
    | 434 | 	}
 | 
  
    | 435 | 
 | 
  
    | 436 | 	// Now use the old method for pages not displayed by the new method above
 | 
  
    | 437 | 	// in case someone has old modules without search.php.
 | 
  
    | 438 | 
 | 
  
    | 439 | 	// Get modules
 | 
  
    | 440 | 	$table_search = TABLE_PREFIX."search";
 | 
  
    | 441 | 	$table_sections = TABLE_PREFIX."sections";
 | 
  
    | 442 | 	$get_modules = $database->query("
 | 
  
    | 443 | 		SELECT DISTINCT s.value, s.extra
 | 
  
    | 444 | 		FROM $table_search AS s INNER JOIN $table_sections AS sec
 | 
  
    | 445 | 			ON s.value = sec.module
 | 
  
    | 446 | 		WHERE s.name = 'module'
 | 
  
    | 447 | 	");
 | 
  
    | 448 | 	$modules = array();
 | 
  
    | 449 | 	if($get_modules->numRows() > 0) {
 | 
  
    | 450 | 		while($module = $get_modules->fetchRow()) {
 | 
  
    | 451 | 			$modules[] = $module; // $modules in an array of arrays
 | 
  
    | 452 | 		}
 | 
  
    | 453 | 	}
 | 
  
    | 454 | 	// sort module search-order
 | 
  
    | 455 | 	// get the modules from $search_module_order first ...
 | 
  
    | 456 | 	$sorted_modules = array();
 | 
  
    | 457 | 	$m = count($modules);
 | 
  
    | 458 | 	$search_modules = explode(',', $search_module_order);
 | 
  
    | 459 | 	foreach($search_modules AS $item) {
 | 
  
    | 460 | 		$item = trim($item);
 | 
  
    | 461 | 		for($i=0; $i < $m; $i++) {
 | 
  
    | 462 | 			if(isset($modules[$i]) && $modules[$i]['value'] == $item) {
 | 
  
    | 463 | 				$sorted_modules[] = $modules[$i];
 | 
  
    | 464 | 				unset($modules[$i]);
 | 
  
    | 465 | 				break;
 | 
  
    | 466 | 			}
 | 
  
    | 467 | 		}
 | 
  
    | 468 | 	}
 | 
  
    | 469 | 	// ... then add the rest
 | 
  
    | 470 | 	foreach($modules AS $item) {
 | 
  
    | 471 | 		$sorted_modules[] = $item;
 | 
  
    | 472 | 	}
 | 
  
    | 473 | 
 | 
  
    | 474 | 	if($cfg_enable_old_search) {
 | 
  
    | 475 | 		$search_path_SQL = str_replace(' link ', ' '.TABLE_PREFIX.'pages.link ', $search_path_SQL);
 | 
  
    | 476 | 		foreach($sorted_modules AS $module) {
 | 
  
    | 477 | 			$query_start = '';
 | 
  
    | 478 | 			$query_body = '';
 | 
  
    | 479 | 			$query_end = '';
 | 
  
    | 480 | 			$prepared_query = '';
 | 
  
    | 481 | 			// Get module name
 | 
  
    | 482 | 			$module_name = $module['value'];
 | 
  
    | 483 | 			if(!isset($seen_pages[$module_name])) {
 | 
  
    | 484 | 				$seen_pages[$module_name]=array();
 | 
  
    | 485 | 			}
 | 
  
    | 486 | 			// skip module 'code' - it doesn't make sense to search in a code section
 | 
  
    | 487 | 			if($module_name=="code")
 | 
  
    | 488 | 				continue;
 | 
  
    | 489 | 			// Get fields to use for title, link, etc.
 | 
  
    | 490 | 			$fields = unserialize($module['extra']);
 | 
  
    | 491 | 			// Get query start
 | 
  
    | 492 | 			$get_query_start = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_start' AND extra = '$module_name' LIMIT 1");
 | 
  
    | 493 | 			if($get_query_start->numRows() > 0) {
 | 
  
    | 494 | 				// Fetch query start
 | 
  
    | 495 | 				$fetch_query_start = $get_query_start->fetchRow();
 | 
  
    | 496 | 				// Prepare query start for execution by replacing {TP} with the TABLE_PREFIX
 | 
  
    | 497 | 				$query_start = str_replace('[TP]', TABLE_PREFIX, ($fetch_query_start['value']));
 | 
  
    | 498 | 			}
 | 
  
    | 499 | 			// Get query end
 | 
  
    | 500 | 			$get_query_end = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_end' AND extra = '$module_name' LIMIT 1");
 | 
  
    | 501 | 			if($get_query_end->numRows() > 0) {
 | 
  
    | 502 | 				// Fetch query end
 | 
  
    | 503 | 				$fetch_query_end = $get_query_end->fetchRow();
 | 
  
    | 504 | 				// Set query end
 | 
  
    | 505 | 				$query_end = ($fetch_query_end['value']);
 | 
  
    | 506 | 			}
 | 
  
    | 507 | 			// Get query body
 | 
  
    | 508 | 			$get_query_body = $database->query("SELECT value FROM ".TABLE_PREFIX."search WHERE name = 'query_body' AND extra = '$module_name' LIMIT 1");
 | 
  
    | 509 | 			if($get_query_body->numRows() > 0) {
 | 
  
    | 510 | 				// Fetch query body
 | 
  
    | 511 | 				$fetch_query_body = $get_query_body->fetchRow();
 | 
  
    | 512 | 				// Prepare query body for execution by replacing {STRING} with the correct one
 | 
  
    | 513 | 				$query_body = str_replace(array('[TP]','[O]','[W]'), array(TABLE_PREFIX,'LIKE','%'), ($fetch_query_body['value']));
 | 
  
    | 514 | 				// Loop through query body for each string, then combine with start and end
 | 
  
    | 515 | 				$prepared_query = $query_start." ( ( ( ";
 | 
  
    | 516 | 				$count = 0;
 | 
  
    | 517 | 				foreach($search_normal_array AS $string) {
 | 
  
    | 518 | 					if($count != 0) {
 | 
  
    | 519 | 						$prepared_query .= " ) ".$logical_operator." ( ";
 | 
  
    | 520 | 					}
 | 
  
    | 521 | 					$prepared_query .= str_replace('[STRING]', $string, $query_body);
 | 
  
    | 522 | 					$count = $count+1;
 | 
  
    | 523 | 				}
 | 
  
    | 524 | 				$count=0;
 | 
  
    | 525 | 				$prepared_query .= ' ) ) OR ( ( ';
 | 
  
    | 526 | 				foreach($search_entities_array AS $string) {
 | 
  
    | 527 | 					if($count != 0) {
 | 
  
    | 528 | 						$prepared_query .= " ) ".$logical_operator." ( ";
 | 
  
    | 529 | 					}
 | 
  
    | 530 | 					$prepared_query .= str_replace('[STRING]', $string, $query_body);
 | 
  
    | 531 | 					$count = $count+1;
 | 
  
    | 532 | 				}
 | 
  
    | 533 | 				$prepared_query .= " ) ) ) ".$query_end;
 | 
  
    | 534 | 	
 | 
  
    | 535 | 				// Execute query
 | 
  
    | 536 | 				$page_query = $database->query($prepared_query." ".$search_path_SQL);
 | 
  
    | 537 | 
 | 
  
    | 538 | 				// Loop through queried items
 | 
  
    | 539 | 				if($page_query->numRows() > 0) {
 | 
  
    | 540 | 					while($page = $page_query->fetchRow()) {
 | 
  
    | 541 | 						// Only show this page if it hasn't already been listed
 | 
  
    | 542 | 						if(isset($seen_pages[$module_name][$page['page_id']]) || isset($pages_listed[$page['page_id']])) {
 | 
  
    | 543 | 							continue;
 | 
  
    | 544 | 						}
 | 
  
    | 545 | 						
 | 
  
    | 546 | 						// don't list pages with visibility == none|deleted and check if user is allowed to see the page
 | 
  
    | 547 | 						$p_table = TABLE_PREFIX."pages";
 | 
  
    | 548 | 						$viewquery = $database->query("
 | 
  
    | 549 | 							SELECT visibility, viewing_groups, viewing_users
 | 
  
    | 550 | 							FROM $p_table
 | 
  
    | 551 | 							WHERE page_id='{$page['page_id']}'
 | 
  
    | 552 | 						");
 | 
  
    | 553 | 						$visibility = 'none'; $viewing_groups="" ; $viewing_users="";
 | 
  
    | 554 | 						if($viewquery->numRows() > 0) {
 | 
  
    | 555 | 							if($res = $viewquery->fetchRow()) {
 | 
  
    | 556 | 								$visibility = $res['visibility'];
 | 
  
    | 557 | 								$viewing_groups = $res['viewing_groups'];
 | 
  
    | 558 | 								$viewing_users = $res['viewing_users'];
 | 
  
    | 559 | 								if($visibility == 'deleted' || $visibility == 'none') {
 | 
  
    | 560 | 									continue;
 | 
  
    | 561 | 								}
 | 
  
    | 562 | 								if($visibility == 'private') {
 | 
  
    | 563 | 									if($admin->page_is_visible(array(
 | 
  
    | 564 | 										'page_id'=>$page[$fields['page_id']],
 | 
  
    | 565 | 										'visibility' =>$visibility,
 | 
  
    | 566 | 										'viewing_groups'=>$viewing_groups,
 | 
  
    | 567 | 										'viewing_users'=>$viewing_users
 | 
  
    | 568 | 									)) == false) {
 | 
  
    | 569 | 										continue;
 | 
  
    | 570 | 									}
 | 
  
    | 571 | 								}
 | 
  
    | 572 | 								if($admin->page_is_active(array('page_id'=>$page[$fields['page_id']]))==false) {
 | 
  
    | 573 | 									continue;
 | 
  
    | 574 | 								}
 | 
  
    | 575 | 							}
 | 
  
    | 576 | 						}
 | 
  
    | 577 | 	
 | 
  
    | 578 | 						// Get page link
 | 
  
    | 579 | 						$link = page_link($page['link']);
 | 
  
    | 580 | 						// Add search string for highlighting
 | 
  
    | 581 | 						if ($match!='exact') {
 | 
  
    | 582 | 							$sstring = implode(" ", $search_normal_array);
 | 
  
    | 583 | 							$link = $link."?searchresult=1&sstring=".urlencode($sstring);
 | 
  
    | 584 | 						} else {
 | 
  
    | 585 | 							$sstring = strtr($search_normal_array[0], " ", "_");
 | 
  
    | 586 | 							$link = $link."?searchresult=2&sstring=".urlencode($sstring);
 | 
  
    | 587 | 						}
 | 
  
    | 588 | 						// Set vars to be replaced by values
 | 
  
    | 589 | 						if(!isset($page['description'])) { $page['description'] = ""; }
 | 
  
    | 590 | 						if(!isset($page['modified_when'])) { $page['modified_when'] = 0; }
 | 
  
    | 591 | 						if(!isset($page['modified_by'])) { $page['modified_by'] = 0; }
 | 
  
    | 592 | 						$vars = array('[LINK]', '[TITLE]', '[DESCRIPTION]', '[USERNAME]','[DISPLAY_NAME]','[DATE]','[TIME]','[TEXT_LAST_UPDATED_BY]','[TEXT_ON]','[EXCERPT]');
 | 
  
    | 593 | 						if($page['modified_when'] > 0) {
 | 
  
    | 594 | 							$date = gmdate(DATE_FORMAT, $page['modified_when']+TIMEZONE);
 | 
  
    | 595 | 							$time = gmdate(TIME_FORMAT, $page['modified_when']+TIMEZONE);
 | 
  
    | 596 | 						} else {
 | 
  
    | 597 | 							$date = $TEXT['UNKNOWN'].' '.$TEXT['DATE'];
 | 
  
    | 598 | 							$time = $TEXT['UNKNOWN'].' '.$TEXT['TIME'];
 | 
  
    | 599 | 						}
 | 
  
    | 600 | 						$excerpt="";
 | 
  
    | 601 | 						if($cfg_show_description == 0) {
 | 
  
    | 602 | 							$page['description'] = "";
 | 
  
    | 603 | 						}
 | 
  
    | 604 | 						if(isset($page['menu_title'])) $rep_title = $page['menu_title'];
 | 
  
    | 605 | 						else $rep_title = $page['page_title'];
 | 
  
    | 606 | 						$values = array($link, $rep_title, $page['description'], $users[$page['modified_by']]['username'], $users[$page['modified_by']]['display_name'], $date, $time, $TEXT['LAST_UPDATED_BY'], strtolower($TEXT['ON']), $excerpt);
 | 
  
    | 607 | 						// Show loop code with vars replaced by values
 | 
  
    | 608 | 						echo str_replace($vars, $values, ($fetch_results_loop['value']));
 | 
  
    | 609 | 						// Say that this page has been listed
 | 
  
    | 610 | 						$seen_pages[$module_name][$page['page_id']] = true;
 | 
  
    | 611 | 						$pages_listed[$page['page_id']] = true;
 | 
  
    | 612 | 					}
 | 
  
    | 613 | 				}
 | 
  
    | 614 | 			}
 | 
  
    | 615 | 		}
 | 
  
    | 616 | 	}
 | 
  
    | 617 | 
 | 
  
    | 618 | 	// Say no items found if we should
 | 
  
    | 619 | 	if(count($pages_listed) == 0) {
 | 
  
    | 620 | 		echo $search_no_results;
 | 
  
    | 621 | 	}
 | 
  
    | 622 | } else {
 | 
  
    | 623 | 	echo $search_no_results;
 | 
  
    | 624 | }
 | 
  
    | 625 | 
 | 
  
    | 626 | // Show search results_footer
 | 
  
    | 627 | echo $search_results_footer;
 | 
  
    | 628 | // Show search footer
 | 
  
    | 629 | echo $search_footer;
 | 
  
    | 630 | 
 | 
  
    | 631 | //$overall_end_time = microtime(true); // for testing only
 | 
  
    | 632 | //$time=$overall_end_time-$overall_start_time; print "<br />Timings - Overall: $time<br />";
 | 
  
    | 633 | 
 | 
  
    | 634 | ?>
 |