[ Index ] |
PHP Cross Reference of phpBB 3.0 Beta 3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * @package phpBB3 5 * @version $Id: search.php,v 1.166 2006/11/12 15:35:43 acydburn Exp $ 6 * @copyright (c) 2005 phpBB Group 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License 8 * 9 */ 10 11 /** 12 * @ignore 13 */ 14 define('IN_PHPBB', true); 15 $phpbb_root_path = './'; 16 $phpEx = substr(strrchr(__FILE__, '.'), 1); 17 include($phpbb_root_path . 'common.' . $phpEx); 18 19 // Start session management 20 $user->session_begin(); 21 $auth->acl($user->data); 22 $user->setup('search'); 23 24 // Define initial vars 25 $mode = request_var('mode', ''); 26 $search_id = request_var('search_id', ''); 27 $start = max(request_var('start', 0), 0); 28 $post_id = request_var('p', 0); 29 $topic_id = request_var('t', 0); 30 $view = request_var('view', ''); 31 32 $submit = request_var('submit', false); 33 $keywords = request_var('keywords', '', true); 34 $add_keywords = request_var('add_keywords', '', true); 35 $author = request_var('author', ''); 36 $author_id = request_var('author_id', 0); 37 $show_results = ($topic_id) ? 'posts' : request_var('sr', 'posts'); 38 $show_results = ($show_results == 'posts') ? 'posts' : 'topics'; 39 $search_terms = request_var('terms', 'all'); 40 $search_fields = request_var('sf', 'all'); 41 $search_child = request_var('sc', true); 42 43 $sort_days = request_var('st', 0); 44 $sort_key = request_var('sk', 't'); 45 $sort_dir = request_var('sd', 'd'); 46 47 $return_chars = request_var('ch', ($topic_id) ? -1 : 200); 48 $search_forum = request_var('fid', array(0)); 49 50 // Is user able to search? Has search been disabled? 51 if (!$auth->acl_get('u_search') || !$auth->acl_getf_global('f_search') || !$config['load_search']) 52 { 53 trigger_error($user->lang['NO_SEARCH']); 54 } 55 56 // Check search load limit 57 if ($user->load && $config['limit_search_load'] && ($user->load > doubleval($config['limit_search_load']))) 58 { 59 trigger_error($user->lang['NO_SEARCH_TIME']); 60 } 61 62 // Check flood limit ... if applicable 63 $interval = ($user->data['user_id'] == ANONYMOUS) ? $config['search_anonymous_interval'] : $config['search_interval']; 64 if ($interval && !$auth->acl_get('u_ignoreflood')) 65 { 66 if ($user->data['user_last_search'] > time() - $interval) 67 { 68 trigger_error($user->lang['NO_SEARCH_TIME']); 69 } 70 } 71 72 // Define some vars 73 $limit_days = array(0 => $user->lang['ALL_RESULTS'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']); 74 $sort_by_text = array('a' => $user->lang['SORT_AUTHOR'], 't' => $user->lang['SORT_TIME'], 'f' => $user->lang['SORT_FORUM'], 'i' => $user->lang['SORT_TOPIC_TITLE'], 's' => $user->lang['SORT_POST_SUBJECT']); 75 76 $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; 77 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); 78 79 if ($keywords || $author || $author_id || $search_id || $submit) 80 { 81 // clear arrays 82 $id_ary = array(); 83 84 // Which forums should not be searched? 85 $ex_fid_ary = array_unique(array_merge(array_keys($auth->acl_getf('!f_read', true)), array_keys($auth->acl_getf('!f_search', true)))); 86 87 $not_in_fid = (sizeof($ex_fid_ary)) ? 'WHERE ' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . " OR (f.forum_password <> '' AND fa.user_id <> " . (int) $user->data['user_id'] . ')' : ""; 88 89 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.right_id, f.forum_password, fa.user_id 90 FROM ' . FORUMS_TABLE . ' f 91 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id 92 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "') 93 $not_in_fid 94 ORDER BY f.left_id"; 95 $result = $db->sql_query($sql); 96 97 $right_id = 0; 98 $reset_search_forum = true; 99 while ($row = $db->sql_fetchrow($result)) 100 { 101 if ($row['forum_password'] && $row['user_id'] != $user->data['user_id']) 102 { 103 $ex_fid_ary[] = (int) $row['forum_id']; 104 continue; 105 } 106 107 if (sizeof($search_forum)) 108 { 109 if ($search_child) 110 { 111 if (in_array($row['forum_id'], $search_forum) && $row['right_id'] > $right_id) 112 { 113 $right_id = (int) $row['right_id']; 114 } 115 else if ($row['right_id'] < $right_id) 116 { 117 continue; 118 } 119 } 120 121 if (!in_array($row['forum_id'], $search_forum)) 122 { 123 $ex_fid_ary[] = (int) $row['forum_id']; 124 $reset_search_forum = false; 125 } 126 } 127 } 128 $db->sql_freeresult($result); 129 130 // find out in which forums the user is allowed to view approved posts 131 if ($auth->acl_get('m_approve')) 132 { 133 $m_approve_fid_ary = array(-1); 134 $m_approve_fid_sql = ''; 135 } 136 else if ($auth->acl_getf_global('m_approve')) 137 { 138 $m_approve_fid_ary = array_diff(array_keys($auth->acl_getf('!m_approve', true)), $ex_fid_ary); 139 $m_approve_fid_sql = ' AND (p.post_approved = 1' . ((sizeof($m_approve_fid_ary)) ? ' OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) : '') . ')'; 140 } 141 else 142 { 143 $m_approve_fid_ary = array(); 144 $m_approve_fid_sql = ' AND p.post_approved = 1'; 145 } 146 147 if ($reset_search_forum) 148 { 149 $search_forum = array(); 150 } 151 152 // egosearch is an author search 153 if ($search_id == 'egosearch') 154 { 155 $author = $user->data['username']; 156 } 157 158 // If we are looking for authors get their ids 159 $author_id_ary = array(); 160 if ($author_id) 161 { 162 $author_id_ary[] = $author_id; 163 } 164 else if ($author) 165 { 166 if ((strpos($author, '*') !== false) && (str_replace(array('*', '%'), '', $author) < $config['min_search_author_chars'])) 167 { 168 trigger_error(sprintf($user->lang['TOO_FEW_AUTHOR_CHARS'], $config['min_search_author_chars'])); 169 } 170 171 $sql_where = (strpos($author, '*') !== false) ? ' LIKE ' : ' = '; 172 $sql = 'SELECT user_id 173 FROM ' . USERS_TABLE . " 174 WHERE username $sql_where '" . $db->sql_escape(preg_replace('#\*+#', '%', $author)) . "' 175 AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')'; 176 $result = $db->sql_query_limit($sql, 100); 177 178 while ($row = $db->sql_fetchrow($result)) 179 { 180 $author_id_ary[] = (int) $row['user_id']; 181 } 182 $db->sql_freeresult($result); 183 184 if (!sizeof($author_id_ary)) 185 { 186 trigger_error($user->lang['NO_SEARCH_RESULTS']); 187 } 188 } 189 190 // if we search in an existing search result just add the additional keywords. But we need to use "all search terms"-mode 191 // so we can keep the old keywords in their old mode, but add the new ones as required words 192 if ($add_keywords) 193 { 194 if ($search_terms == 'all') 195 { 196 $keywords .= ' ' . $add_keywords; 197 } 198 else 199 { 200 $search_terms = 'all'; 201 $keywords = implode(' |', explode(' ', preg_replace('#\s+#', ' ', $keywords))) . ' ' .$add_keywords; 202 } 203 } 204 205 // Select which method we'll use to obtain the post_id or topic_id information 206 $search_type = basename($config['search_type']); 207 208 if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx)) 209 { 210 trigger_error('NO_SUCH_SEARCH_MODULE'); 211 } 212 213 require("{$phpbb_root_path}includes/search/$search_type.$phpEx"); 214 215 // We do some additional checks in the module to ensure it can actually be utilised 216 $error = false; 217 $search = new $search_type($error); 218 219 if ($error) 220 { 221 trigger_error($error); 222 } 223 224 // let the search module split up the keywords 225 if ($keywords) 226 { 227 $search->split_keywords($keywords, $search_terms); 228 if (empty($search->search_query) && !sizeof($author_id_ary) && !$search_id) 229 { 230 $ignored = (sizeof($search->common_words)) ? sprintf($user->lang['IGNORED_TERMS_EXPLAIN'], htmlspecialchars(implode(' ', $search->common_words))) . '<br />' : ''; 231 trigger_error($ignored . sprintf($user->lang['NO_KEYWORDS'], $search->word_length['min'], $search->word_length['max'])); 232 } 233 } 234 235 if (!$keywords && sizeof($author_id_ary)) 236 { 237 // if it is an author search we want to show topics by default 238 $show_results = ($topic_id) ? 'posts' : request_var('sr', ($search_id == 'egosearch') ? 'topics' : 'posts'); 239 $show_results = ($show_results == 'posts') ? 'posts' : 'topics'; 240 } 241 242 // define some variables needed for retrieving post_id/topic_id information 243 $sort_by_sql = array('a' => 'u.username', 't' => (($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'), 'f' => 'f.forum_id', 'i' => 't.topic_title', 's' => (($show_results == 'posts') ? 'p.post_subject' : 't.topic_title')); 244 245 // pre-made searches 246 $sql = $field = ''; 247 if ($search_id) 248 { 249 switch ($search_id) 250 { 251 // Oh holy Bob, bring us some activity... 252 case 'active_topics': 253 $show_results = 'topics'; 254 $sort_key = 't'; 255 $sort_dir = 'd'; 256 $sort_by_sql['t'] = 't.topic_last_post_time'; 257 258 if (!$sort_days) 259 { 260 $sort_days = 3; 261 } 262 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); 263 $s_sort_key = $s_sort_dir = ''; 264 $u_sort_param = 'st=' . $sort_days; 265 266 $last_post_time = (time() - ($sort_days * 24 * 3600)); 267 268 $sql = 'SELECT t.topic_last_post_time, t.topic_id 269 FROM ' . TOPICS_TABLE . " t 270 WHERE t.topic_last_post_time > $last_post_time 271 AND t.topic_moved_id = 0 272 " . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . ' 273 ' . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . ' 274 ORDER BY t.topic_last_post_time DESC'; 275 $field = 'topic_id'; 276 break; 277 278 case 'unanswered': 279 $show_results = request_var('sr', 'topics'); 280 $show_results = ($show_results == 'posts') ? 'posts' : 'topics'; 281 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'; 282 $sort_by_sql['s'] = ($show_results == 'posts') ? 'p.post_subject' : 't.topic_title'; 283 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); 284 285 $sort_join = ($sort_key == 'f') ? FORUMS_TABLE . ' f, ' : ''; 286 $sql_sort = ($sort_key == 'f') ? ' AND f.forum_id = p.forum_id ' . $sql_sort : $sql_sort; 287 288 if ($sort_days) 289 { 290 $last_post_time = 'AND p.post_time > ' . (time() - ($sort_days * 24 * 3600)); 291 } 292 else 293 { 294 $last_post_time = ''; 295 } 296 297 if ($show_results == 'posts') 298 { 299 if ($sort_key == 'a') 300 { 301 $sort_join = USERS_TABLE . ' u, '; 302 $sql_sort = ' AND u.user_id = p.poster_id ' . $sql_sort; 303 } 304 305 $sql = "SELECT p.post_id 306 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t 307 WHERE t.topic_replies = 0 308 AND p.topic_id = t.topic_id 309 $last_post_time 310 $m_approve_fid_sql 311 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . " 312 $sql_sort"; 313 $field = 'post_id'; 314 } 315 else 316 { 317 $sql = 'SELECT DISTINCT ' . $sort_by_sql[$sort_key] . ", p.topic_id 318 FROM $sort_join" . POSTS_TABLE . ' p, ' . TOPICS_TABLE . " t 319 WHERE t.topic_replies = 0 320 AND t.topic_moved_id = 0 321 AND p.topic_id = t.topic_id 322 $last_post_time 323 $m_approve_fid_sql 324 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . " 325 $sql_sort"; 326 $field = 'topic_id'; 327 } 328 break; 329 330 case 'newposts': 331 // force sorting 332 $show_results = (request_var('sr', 'topics') == 'posts') ? 'posts' : 'topics'; 333 $sort_key = 't'; 334 $sort_dir = 'd'; 335 $sort_by_sql['t'] = ($show_results == 'posts') ? 'p.post_time' : 't.topic_last_post_time'; 336 $sql_sort = 'ORDER BY ' . $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); 337 338 gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); 339 $s_sort_key = $s_sort_dir = $u_sort_param = $s_limit_days = ''; 340 341 if ($show_results == 'posts') 342 { 343 $sql = 'SELECT p.post_id 344 FROM ' . POSTS_TABLE . ' p 345 WHERE p.post_time > ' . $user->data['user_lastvisit'] . " 346 $m_approve_fid_sql 347 " . ((sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '') . " 348 $sql_sort"; 349 $field = 'post_id'; 350 } 351 else 352 { 353 $sql = 'SELECT t.topic_id 354 FROM ' . TOPICS_TABLE . ' t 355 WHERE t.topic_last_post_time > ' . $user->data['user_lastvisit'] . ' 356 AND t.topic_moved_id = 0 357 ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . ' 358 ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $db->sql_in_set('t.forum_id', $ex_fid_ary, true) : '') . " 359 $sql_sort"; 360 $field = 'topic_id'; 361 } 362 break; 363 } 364 } 365 366 // show_results should not change after this 367 $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page']; 368 369 if ($search_id) 370 { 371 if ($sql) 372 { 373 // only return up to 1000 ids (the last one will be removed later) 374 $result = $db->sql_query_limit($sql, 1001 - $start, $start); 375 376 while ($row = $db->sql_fetchrow($result)) 377 { 378 $id_ary[] = $row[$field]; 379 } 380 $db->sql_freeresult($result); 381 382 $total_match_count = sizeof($id_ary) + $start; 383 $id_ary = array_slice($id_ary, 0, $per_page); 384 } 385 else 386 { 387 $search_id = ''; 388 } 389 } 390 391 // make sure that some arrays are always in the same order 392 sort($ex_fid_ary); 393 sort($m_approve_fid_ary); 394 sort($author_id_ary); 395 396 if (!empty($search->search_query)) 397 { 398 $total_match_count = $search->keyword_search($show_results, $search_fields, $search_terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page); 399 } 400 else if (sizeof($author_id_ary)) 401 { 402 $total_match_count = $search->author_search($show_results, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $m_approve_fid_ary, $topic_id, $author_id_ary, $id_ary, $start, $per_page); 403 } 404 405 if (!sizeof($id_ary)) 406 { 407 trigger_error($user->lang['NO_SEARCH_RESULTS']); 408 } 409 410 $sql_where = $db->sql_in_set(($show_results == 'posts') ? 'p.post_id' : 't.topic_id', $id_ary); 411 $sql_where .= (sizeof($ex_fid_ary)) ? ' AND (' . $db->sql_in_set('f.forum_id', $ex_fid_ary, true) . ' OR f.forum_id IS NULL)' : ''; 412 $sql_where .= ($show_results == 'posts') ? $m_approve_fid_sql : str_replace(array('p.post_approved', 'p.forum_id'), array('t.topic_approved', 't.forum_id'), $m_approve_fid_sql); 413 414 if ($show_results == 'posts') 415 { 416 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx); 417 } 418 else 419 { 420 include($phpbb_root_path . 'includes/functions_display.' . $phpEx); 421 } 422 423 // Grab icons 424 $icons = $cache->obtain_icons(); 425 426 // Output header 427 if ($search_id && ($total_match_count > 1000)) 428 { 429 // limit the number to 1000 for pre-made searches 430 $total_match_count--; 431 $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count); 432 } 433 else 434 { 435 $l_search_matches = ($total_match_count == 1) ? sprintf($user->lang['FOUND_SEARCH_MATCH'], $total_match_count) : sprintf($user->lang['FOUND_SEARCH_MATCHES'], $total_match_count); 436 } 437 438 // define some vars for urls 439 $hilit = preg_replace('#&(\#[0-9]+;)#', '&$1', htmlspecialchars(implode('|', explode(' ', preg_replace('#\s+#', ' ', str_replace(array('+', '-', '|', '(', ')'), ' ', $keywords)))))); 440 $u_hilit = urlencode($keywords); 441 $u_show_results = ($show_results != 'posts') ? '&sr=' . $show_results : ''; 442 $u_search_forum = implode('&fid%5B%5D=', $search_forum); 443 444 $u_search = append_sid("{$phpbb_root_path}search.$phpEx", $u_sort_param . $u_show_results); 445 $u_search .= ($search_id) ? '&search_id=' . $search_id : ''; 446 $u_search .= ($u_hilit) ? '&keywords=' . $u_hilit : ''; 447 $u_search .= ($topic_id) ? '&t=' . $topic_id : ''; 448 $u_search .= ($author) ? '&author=' . urlencode($author) : ''; 449 $u_search .= ($author_id) ? '&author_id=' . $author_id : ''; 450 $u_search .= ($u_search_forum) ? '&fid%5B%5D=' . $u_search_forum : ''; 451 $u_search .= (!$search_child) ? '&sc=0' : ''; 452 $u_search .= ($search_fields != 'all') ? '&sf=' . $search_fields : ''; 453 $u_search .= ($return_chars != 200) ? '&ch=' . $return_chars : ''; 454 455 $template->assign_vars(array( 456 'SEARCH_MATCHES' => $l_search_matches, 457 'SEARCH_WORDS' => preg_replace('#&(\#[0-9]+;)#', '&$1', htmlspecialchars($search->search_query)), 458 'IGNORED_WORDS' => (sizeof($search->common_words)) ? htmlspecialchars(implode(' ', $search->common_words)) : '', 459 'PAGINATION' => generate_pagination($u_search, $total_match_count, $per_page, $start), 460 'PAGE_NUMBER' => on_page($total_match_count, $per_page, $start), 461 'TOTAL_MATCHES' => $total_match_count, 462 'SEARCH_IN_RESULTS' => ($search_id) ? false : true, 463 464 'S_SELECT_SORT_DIR' => $s_sort_dir, 465 'S_SELECT_SORT_KEY' => $s_sort_key, 466 'S_SELECT_SORT_DAYS' => $s_limit_days, 467 'S_SEARCH_ACTION' => $u_search, 468 'S_SHOW_TOPICS' => ($show_results == 'posts') ? false : true, 469 470 'GOTO_PAGE_IMG' => $user->img('icon_post_target', 'GOTO_PAGE'), 471 'NEWEST_POST_IMG' => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'), 472 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED'), 473 'UNAPPROVED_IMG' => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'), 474 475 'U_SEARCH_WORDS' => $u_search) 476 ); 477 478 if ($sql_where) 479 { 480 if ($show_results == 'posts') 481 { 482 /** 483 * @todo Joining this query to the one below? 484 */ 485 $sql = 'SELECT zebra_id, friend, foe 486 FROM ' . ZEBRA_TABLE . ' 487 WHERE user_id = ' . $user->data['user_id']; 488 $result = $db->sql_query($sql); 489 490 $zebra = array(); 491 while ($row = $db->sql_fetchrow($result)) 492 { 493 $zebra[($row['friend']) ? 'friend' : 'foe'][] = $row['zebra_id']; 494 } 495 $db->sql_freeresult($result); 496 497 $sql = 'SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_sig, u.user_sig_bbcode_uid, u.user_colour 498 FROM ' . POSTS_TABLE . ' p 499 LEFT JOIN ' . TOPICS_TABLE . ' t ON (p.topic_id = t.topic_id) 500 LEFT JOIN ' . FORUMS_TABLE . ' f ON (p.forum_id = f.forum_id) 501 LEFT JOIN ' . USERS_TABLE . " u ON (p.poster_id = u.user_id) 502 WHERE $sql_where"; 503 } 504 else 505 { 506 $sql_from = TOPICS_TABLE . ' t 507 LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = t.forum_id) 508 ' . (($sort_key == 'a') ? ' LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = t.topic_poster) ' : ''); 509 $sql_select = 't.*, f.forum_id, f.forum_name'; 510 511 if ($user->data['is_registered']) 512 { 513 if ($config['load_db_track']) 514 { 515 $sql_from .= ' LEFT JOIN ' . TOPICS_POSTED_TABLE . ' tp ON (tp.user_id = ' . $user->data['user_id'] . ' 516 AND t.topic_id = tp.topic_id)'; 517 $sql_select .= ', tp.topic_posted'; 518 } 519 520 if ($config['load_db_lastread']) 521 { 522 $sql_from .= ' LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . ' 523 AND t.topic_id = tt.topic_id) 524 LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . ' 525 AND ft.forum_id = f.forum_id)'; 526 $sql_select .= ', tt.mark_time, ft.mark_time as f_mark_time'; 527 } 528 } 529 530 if ($config['load_anon_lastread'] || ($user->data['is_registered'] && !$config['load_db_lastread'])) 531 { 532 $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_track'])) ? ((STRIP) ? stripslashes($_COOKIE[$config['cookie_name'] . '_track']) : $_COOKIE[$config['cookie_name'] . '_track']) : ''; 533 $tracking_topics = ($tracking_topics) ? unserialize($tracking_topics) : array(); 534 } 535 536 $sql = "SELECT $sql_select 537 FROM $sql_from 538 WHERE $sql_where"; 539 } 540 $sql .= ' ORDER BY ' . $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC'); 541 $result = $db->sql_query($sql); 542 $result_topic_id = 0; 543 544 $rowset = array(); 545 546 if ($show_results == 'topics') 547 { 548 $forums = $rowset = $shadow_topic_list = array(); 549 while ($row = $db->sql_fetchrow($result)) 550 { 551 if ($row['topic_status'] == ITEM_MOVED) 552 { 553 $shadow_topic_list[$row['topic_moved_id']] = $row['topic_id']; 554 } 555 556 $rowset[$row['topic_id']] = $row; 557 558 if (!isset($forums[$row['forum_id']]) && $user->data['is_registered'] && $config['load_db_lastread']) 559 { 560 $forums[$row['forum_id']]['mark_time'] = $row['f_mark_time']; 561 } 562 $forums[$row['forum_id']]['topic_list'][] = $row['topic_id']; 563 $forums[$row['forum_id']]['rowset'][$row['topic_id']] = &$rowset[$row['topic_id']]; 564 } 565 $db->sql_freeresult($result); 566 567 // If we have some shadow topics, update the rowset to reflect their topic informations 568 if (sizeof($shadow_topic_list)) 569 { 570 $sql = 'SELECT * 571 FROM ' . TOPICS_TABLE . ' 572 WHERE ' . $db->sql_in_set('topic_id', array_keys($shadow_topic_list)); 573 $result = $db->sql_query($sql); 574 575 while ($row = $db->sql_fetchrow($result)) 576 { 577 $orig_topic_id = $shadow_topic_list[$row['topic_id']]; 578 579 // We want to retain some values 580 $row = array_merge($row, array( 581 'topic_moved_id' => $rowset[$orig_topic_id]['topic_moved_id'], 582 'topic_status' => $rowset[$orig_topic_id]['topic_status'], 583 'forum_name' => $rowset[$orig_topic_id]['forum_name']) 584 ); 585 586 $rowset[$orig_topic_id] = $row; 587 } 588 $db->sql_freeresult($result); 589 } 590 unset($shadow_topic_list); 591 592 foreach ($forums as $forum_id => $forum) 593 { 594 if ($user->data['is_registered'] && $config['load_db_lastread']) 595 { 596 $topic_tracking_info[$forum_id] = get_topic_tracking($forum_id, $forum['topic_list'], $forum['rowset'], array($forum_id => $forum['mark_time']), ($forum_id) ? false : $forum['topic_list']); 597 } 598 else if ($config['load_anon_lastread'] || $user->data['is_registered']) 599 { 600 $topic_tracking_info[$forum_id] = get_complete_topic_tracking($forum_id, $forum['topic_list'], ($forum_id) ? false : $forum['topic_list']); 601 602 if (!$user->data['is_registered']) 603 { 604 $user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0; 605 } 606 } 607 } 608 unset($forums); 609 } 610 else 611 { 612 $bbcode_bitfield = ''; 613 $attach_list = array(); 614 615 while ($row = $db->sql_fetchrow($result)) 616 { 617 $rowset[] = $row; 618 if (($return_chars == -1) || (utf8_strlen($row['post_text']) < $return_chars + 3)) 619 { 620 $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']); 621 622 // Does this post have an attachment? If so, add it to the list 623 if ($row['post_attachment'] && $config['allow_attachments']) 624 { 625 $attach_list[$row['forum_id']][] = $row['post_id']; 626 } 627 } 628 } 629 $db->sql_freeresult($result); 630 631 // Instantiate BBCode if needed 632 if ($bbcode_bitfield !== '') 633 { 634 include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx); 635 $bbcode = new bbcode(base64_encode($bbcode_bitfield)); 636 } 637 638 // Pull attachment data 639 if (sizeof($attach_list)) 640 { 641 $use_attach_list = $attach_list; 642 $attach_list = array(); 643 644 foreach ($use_attach_list as $forum_id => $_list) 645 { 646 if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id)) 647 { 648 $attach_list = array_merge($attach_list, $_list); 649 } 650 } 651 } 652 653 if (sizeof($attach_list)) 654 { 655 $sql = 'SELECT * 656 FROM ' . ATTACHMENTS_TABLE . ' 657 WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . ' 658 AND in_message = 0 659 ORDER BY filetime ' . ((!$config['display_order']) ? 'DESC' : 'ASC') . ', post_msg_id ASC'; 660 $result = $db->sql_query($sql); 661 662 while ($row = $db->sql_fetchrow($result)) 663 { 664 $attachments[$row['post_msg_id']][] = $row; 665 } 666 $db->sql_freeresult($result); 667 } 668 } 669 670 if ($hilit) 671 { 672 // Remove bad highlights 673 $hilit_array = array_filter(explode('|', $hilit), 'strlen'); 674 foreach ($hilit_array as $key => $value) 675 { 676 $hilit_array[$key] = str_replace('\*', '\w*?', preg_quote($value, '#')); 677 } 678 $hilit = implode('|', $hilit_array); 679 } 680 681 foreach ($rowset as $row) 682 { 683 $forum_id = $row['forum_id']; 684 $result_topic_id = $row['topic_id']; 685 $topic_title = censor_text($row['topic_title']); 686 687 // we need to select a forum id for this global topic 688 if (!$forum_id) 689 { 690 if (!isset($g_forum_id)) 691 { 692 // Get a list of forums the user cannot read 693 $forum_ary = array_unique(array_keys($auth->acl_getf('!f_read', true))); 694 695 // Determine first forum the user is able to read (must not be a category) 696 $sql = 'SELECT forum_id 697 FROM ' . FORUMS_TABLE . ' 698 WHERE forum_type = ' . FORUM_POST; 699 700 if (sizeof($forum_ary)) 701 { 702 $sql .= ' AND ' . $db->sql_in_set('forum_id', $forum_ary, true); 703 } 704 705 $result = $db->sql_query_limit($sql, 1); 706 $g_forum_id = (int) $db->sql_fetchfield('forum_id'); 707 } 708 $u_forum_id = $g_forum_id; 709 } 710 else 711 { 712 $u_forum_id = $forum_id; 713 } 714 715 $view_topic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$u_forum_id&t=$result_topic_id&hilit=$u_hilit"); 716 717 $replies = ($auth->acl_get('m_approve', $forum_id)) ? $row['topic_replies_real'] : $row['topic_replies']; 718 719 if ($show_results == 'topics') 720 { 721 $folder_img = $folder_alt = $topic_type = ''; 722 topic_status($row, $replies, (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false, $folder_img, $folder_alt, $topic_type); 723 724 $unread_topic = (isset($topic_tracking_info[$forum_id][$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$forum_id][$row['topic_id']]) ? true : false; 725 726 $topic_unapproved = (!$row['topic_approved'] && $auth->acl_get('m_approve', $forum_id)) ? true : false; 727 $posts_unapproved = ($row['topic_approved'] && $row['topic_replies'] < $row['topic_replies_real'] && $auth->acl_get('m_approve', $forum_id)) ? true : false; 728 $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . "&t=$result_topic_id", true, $user->session_id) : ''; 729 730 $tpl_ary = array( 731 'TOPIC_AUTHOR' => ($row['topic_first_poster_name']) ? $row['topic_first_poster_name'] : $user->lang['GUEST'], 732 'TOPIC_AUTHOR_COLOUR' => ($row['topic_first_poster_colour']) ? '#' . $row['topic_first_poster_colour'] : '', 733 'FIRST_POST_TIME' => $user->format_date($row['topic_time']), 734 'LAST_POST_SUBJECT' => $row['topic_last_post_subject'], 735 'LAST_POST_TIME' => $user->format_date($row['topic_last_post_time']), 736 'LAST_VIEW_TIME' => $user->format_date($row['topic_last_view_time']), 737 'LAST_POST_AUTHOR' => ($row['topic_last_poster_name'] != '') ? $row['topic_last_poster_name'] : $user->lang['GUEST'], 738 'LAST_POST_AUTHOR_COLOUR' => ($row['topic_last_poster_colour']) ? '#' . $row['topic_last_poster_colour'] : '', 739 740 'PAGINATION' => topic_generate_pagination($replies, $view_topic_url), 741 'TOPIC_TYPE' => $topic_type, 742 743 'LAST_POST_IMG' => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'), 744 'TOPIC_FOLDER_IMG' => $user->img($folder_img, $folder_alt), 745 'TOPIC_FOLDER_IMG_SRC' => $user->img($folder_img, $folder_alt, false, '', 'src'), 746 'TOPIC_ICON_IMG' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['img'] : '', 747 'TOPIC_ICON_IMG_WIDTH' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['width'] : '', 748 'TOPIC_ICON_IMG_HEIGHT' => (!empty($icons[$row['icon_id']])) ? $icons[$row['icon_id']]['height'] : '', 749 'ATTACH_ICON_IMG' => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) && $row['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '', 750 'UNAPPROVED_IMG' => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '', 751 752 'S_TOPIC_GLOBAL' => (!$forum_id) ? true : false, 753 'S_TOPIC_TYPE' => $row['topic_type'], 754 'S_USER_POSTED' => (!empty($row['mark_type'])) ? true : false, 755 'S_UNREAD_TOPIC' => $unread_topic, 756 757 'S_TOPIC_REPORTED' => (!empty($row['topic_reported']) && $auth->acl_get('m_report', $forum_id)) ? true : false, 758 'S_TOPIC_UNAPPROVED' => $topic_unapproved, 759 'S_POSTS_UNAPPROVED' => $posts_unapproved, 760 761 'U_LAST_POST' => $view_topic_url . '&p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id'], 762 'U_LAST_POST_AUTHOR' => ($row['topic_last_poster_id'] != ANONYMOUS && $row['topic_last_poster_id']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['topic_last_poster_id']) : '', 763 'U_TOPIC_AUTHOR' => ($row['topic_poster'] != ANONYMOUS && $row['topic_poster']) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['topic_poster']) : '', 764 'U_NEWEST_POST' => $view_topic_url . '&view=unread#unread', 765 'U_MCP_REPORT' => append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&mode=reports&t=' . $result_topic_id, true, $user->session_id), 766 'U_MCP_QUEUE' => $u_mcp_queue, 767 ); 768 } 769 else 770 { 771 if ((isset($zebra['foe']) && in_array($row['poster_id'], $zebra['foe'])) && (!$view || $view != 'show' || $post_id != $row['post_id'])) 772 { 773 $template->assign_block_vars('searchresults', array( 774 'S_IGNORE_POST' => true, 775 776 'L_IGNORE_POST' => sprintf($user->lang['POST_BY_FOE'], $row['username'], "<a href=\"$u_search&p=" . $row['post_id'] . '&view=show#p' . $row['post_id'] . '">', '</a>')) 777 ); 778 779 continue; 780 } 781 782 // Replace naughty words such as farty pants 783 $row['post_subject'] = censor_text($row['post_subject']); 784 $message = $row['post_text']; 785 786 if ($return_chars != -1 && utf8_strlen($message) >= ($return_chars + 3)) 787 { 788 $message = censor_text($message); 789 790 // make list items visible as such 791 $message = str_replace('[*:' . $row['bbcode_uid'] . ']', '⋅ ', $message); 792 793 // do not display raw bbcode 794 strip_bbcode($message, $row['bbcode_uid']); 795 796 // now find context for the searched words 797 $message = get_context($message, array_filter(explode('|', $hilit), 'strlen'), $return_chars); 798 799 $message = str_replace("\n", '<br />', $message); 800 } 801 else 802 { 803 $message = censor_text($message); 804 $message = str_replace("\n", '<br />', $message); 805 806 // Second parse bbcode here 807 if ($row['bbcode_bitfield']) 808 { 809 $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']); 810 } 811 812 if (isset($attachments[$row['post_id']]) && sizeof($attachments[$row['post_id']])) 813 { 814 parse_inline_attachments($message, $attachments[$row['post_id']], $update_count, $forum_id); 815 816 // we only display inline attachments 817 unset($attachments[$row['post_id']]); 818 } 819 820 // Always process smilies after parsing bbcodes 821 $message = smiley_text($message); 822 } 823 824 if ($hilit) 825 { 826 // post highlighting 827 $message = preg_replace('#(?!<.*)(?<!\w)(' . $hilit . ')(?!\w|[^<>]*(?:</s(?:cript|tyle))?>)#is', '<span class="posthilit">$1</span>', $message); 828 } 829 830 $tpl_ary = array( 831 'POSTER_NAME' => ($row['poster_id'] == ANONYMOUS) ? ((!empty($row['post_username'])) ? $row['post_username'] : $user->lang['GUEST']) : $row['username'], 832 'POSTER_COLOUR' => ($row['user_colour']) ? '#' . $row['user_colour'] : '', 833 'U_PROFILE' => ($row['poster_id'] != ANONYMOUS) ? append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&u=' . $row['poster_id']) : '', 834 'POST_SUBJECT' => $row['post_subject'], 835 'POST_DATE' => (!empty($row['post_time'])) ? $user->format_date($row['post_time']) : '', 836 'MESSAGE' => $message 837 ); 838 } 839 840 $template->assign_block_vars('searchresults', array_merge($tpl_ary, array( 841 'FORUM_ID' => $forum_id, 842 'TOPIC_ID' => $result_topic_id, 843 'POST_ID' => ($show_results == 'posts') ? $row['post_id'] : false, 844 845 'FORUM_TITLE' => $row['forum_name'], 846 'TOPIC_TITLE' => $topic_title, 847 'TOPIC_REPLIES' => $replies, 848 'TOPIC_VIEWS' => $row['topic_views'], 849 850 'U_VIEW_TOPIC' => $view_topic_url, 851 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id), 852 'U_VIEW_POST' => (!empty($row['post_id'])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=" . $row['topic_id'] . '&p=' . $row['post_id'] . '&hilit=' . $u_hilit) . '#p' . $row['post_id'] : '') 853 )); 854 } 855 856 if ($topic_id && ($topic_id == $result_topic_id)) 857 { 858 $template->assign_vars(array( 859 'SEARCH_TOPIC' => $topic_title, 860 'U_SEARCH_TOPIC' => $view_topic_url 861 )); 862 } 863 } 864 unset($rowset); 865 866 page_header($user->lang['SEARCH']); 867 868 $template->set_filenames(array( 869 'body' => 'search_results.html') 870 ); 871 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx")); 872 873 page_footer(); 874 } 875 876 877 // Search forum 878 $s_forums = ''; 879 $sql = 'SELECT f.forum_id, f.forum_name, f.parent_id, f.forum_type, f.left_id, f.right_id, f.forum_password, fa.user_id 880 FROM ' . FORUMS_TABLE . ' f 881 LEFT JOIN ' . FORUMS_ACCESS_TABLE . " fa ON (fa.forum_id = f.forum_id 882 AND fa.session_id = '" . $db->sql_escape($user->session_id) . "') 883 ORDER BY f.left_id ASC"; 884 $result = $db->sql_query($sql); 885 886 $right = $cat_right = $padding_inc = 0; 887 $padding = $forum_list = $holding = ''; 888 $pad_store = array('0' => ''); 889 890 while ($row = $db->sql_fetchrow($result)) 891 { 892 if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id'])) 893 { 894 // Non-postable forum with no subforums, don't display 895 continue; 896 } 897 898 if (!$auth->acl_get('f_list', $row['forum_id']) || $row['forum_type'] == FORUM_LINK || ($row['forum_password'] && !$row['user_id'])) 899 { 900 // if the user does not have permissions to list this forum skip to the next branch 901 continue; 902 } 903 904 if ($row['left_id'] < $right) 905 { 906 $padding .= ' '; 907 $pad_store[$row['parent_id']] = $padding; 908 } 909 else if ($row['left_id'] > $right + 1) 910 { 911 if (isset($pad_store[$row['parent_id']])) 912 { 913 $padding = $pad_store[$row['parent_id']]; 914 } 915 else 916 { 917 continue; 918 } 919 } 920 921 $right = $row['right_id']; 922 923 if (!$auth->acl_get('f_search', $row['forum_id'])) 924 { 925 // if the user does not have permissions to search this forum skip only this forum/category 926 continue; 927 } 928 929 $selected = (in_array($row['forum_id'], $search_forum)) ? ' selected="selected"' : ''; 930 931 if ($row['left_id'] > $cat_right) 932 { 933 $holding = ''; 934 } 935 936 if ($row['right_id'] - $row['left_id'] > 1) 937 { 938 $cat_right = max($cat_right, $row['right_id']); 939 940 $holding .= '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>'; 941 } 942 else 943 { 944 $s_forums .= $holding . '<option value="' . $row['forum_id'] . '"' . $selected . '>' . $padding . $row['forum_name'] . '</option>'; 945 $holding = ''; 946 } 947 } 948 $db->sql_freeresult($result); 949 unset($pad_store); 950 951 if (!$s_forums) 952 { 953 trigger_error($user->lang['NO_SEARCH']); 954 } 955 956 // Number of chars returned 957 $s_characters = '<option value="-1">' . $user->lang['ALL_AVAILABLE'] . '</option>'; 958 $s_characters .= '<option value="0">0</option>'; 959 $s_characters .= '<option value="25">25</option>'; 960 $s_characters .= '<option value="50">50</option>'; 961 962 for ($i = 100; $i <= 1000 ; $i += 100) 963 { 964 $selected = ($i == 200) ? ' selected="selected"' : ''; 965 $s_characters .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>'; 966 } 967 968 $template->assign_vars(array( 969 'S_SEARCH_ACTION' => "{$phpbb_root_path}search.$phpEx", 970 'S_HIDDEN_FIELDS' => build_hidden_fields(array('sid' => $user->session_id, 't' => $topic_id)), 971 'S_CHARACTER_OPTIONS' => $s_characters, 972 'S_FORUM_OPTIONS' => $s_forums, 973 'S_SELECT_SORT_DIR' => $s_sort_dir, 974 'S_SELECT_SORT_KEY' => $s_sort_key, 975 'S_SELECT_SORT_DAYS' => $s_limit_days) 976 ); 977 978 // Can't do comparisons w/ TEXT on MSSQL, CAST is good enough 979 switch ($db->sql_layer) 980 { 981 case 'mssql': 982 case 'mssql_odbc': 983 $sql = 'SELECT search_time, search_keywords 984 FROM ' . SEARCH_RESULTS_TABLE . ' 985 WHERE CAST(search_keywords AS varchar) <> \'\' 986 ORDER BY search_time DESC'; 987 break; 988 989 default: 990 $sql = 'SELECT search_time, search_keywords 991 FROM ' . SEARCH_RESULTS_TABLE . ' 992 WHERE search_keywords <> \'\' 993 ORDER BY search_time DESC'; 994 break; 995 } 996 $result = $db->sql_query_limit($sql, 5); 997 998 while ($row = $db->sql_fetchrow($result)) 999 { 1000 $keywords = htmlspecialchars($row['search_keywords']); 1001 1002 $template->assign_block_vars('recentsearch', array( 1003 'KEYWORDS' => $keywords, 1004 'TIME' => $user->format_date($row['search_time']), 1005 1006 'U_KEYWORDS' => append_sid("{$phpbb_root_path}search.$phpEx", 'keywords=' . urlencode($keywords))) 1007 ); 1008 } 1009 $db->sql_freeresult($result); 1010 1011 // Output the basic page 1012 page_header($user->lang['SEARCH']); 1013 1014 $template->set_filenames(array( 1015 'body' => 'search_body.html') 1016 ); 1017 make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx")); 1018 1019 page_footer(); 1020 1021 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 22 00:35:05 2006 | Cross-referenced by PHPXref 0.6 |