PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/search.php

https://github.com/MightyGorgon/icy_phoenix
PHP | 1988 lines | 1582 code | 216 blank | 190 comment | 441 complexity | 31d6dfcd2ef259a2acf0ba7f4ad6ebdd MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Icy Phoenix is based on phpBB
  13. * @copyright (c) 2008 phpBB Group
  14. *
  15. */
  16. define('IN_SEARCH', true);
  17. // Added to optimize memory for attachments
  18. define('ATTACH_DISPLAY', true);
  19. define('IN_ICYPHOENIX', true);
  20. if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './');
  21. if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
  22. include(IP_ROOT_PATH . 'common.' . PHP_EXT);
  23. include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  24. include_once(IP_ROOT_PATH . 'includes/functions_search.' . PHP_EXT);
  25. include_once(IP_ROOT_PATH . 'includes/functions_topics.' . PHP_EXT);
  26. include_once(IP_ROOT_PATH . 'includes/functions_calendar.' . PHP_EXT);
  27. // Event Registration - BEGIN
  28. include_once(IP_ROOT_PATH . 'includes/functions_events_reg.' . PHP_EXT);
  29. // Event Registration - END
  30. @include_once(IP_ROOT_PATH . 'includes/class_topics.' . PHP_EXT);
  31. $class_topics = new class_topics();
  32. // Adding CPL_NAV only if needed
  33. define('PARSE_CPL_NAV', true);
  34. // Start session management
  35. $user->session_begin();
  36. $auth->acl($user->data);
  37. $user->setup();
  38. // End session management
  39. $search_id = request_var('search_id', '');
  40. $search_mode = request_var('search_mode', '');
  41. $search_mode = !empty($search_mode) ? $search_mode : $search_id;
  42. if (($search_id != 'unanswered') && !$user->data['session_logged_in'] && $config['gsearch_guests'])
  43. {
  44. $google_q = request_var('search_keywords', '', true);
  45. $google_sitesearch = preg_replace('#^\/?(.*?)\/?$#', '\1', trim($config['server_name']));
  46. $google_cof = 'FORID:9';
  47. //$google_ie = 'ISO-8859-1';
  48. $google_ie = 'UTF-8';
  49. $google_url_append = '?q=' . urlencode($google_q) . '&sitesearch=' . $google_sitesearch . '&cof=' . $google_cof . '&ie=' . $google_ie;
  50. redirect(append_sid('gsearch.' . PHP_EXT . (!empty($google_q) ? $google_url_append : ''), true));
  51. }
  52. // CrackerTracker v5.x
  53. if (check_http_var_exists('mode', false) || check_http_var_exists('search_id', false) || check_http_var_exists('show_results', false) || isset($_GET['search_keywords']))
  54. {
  55. include_once(IP_ROOT_PATH . 'includes/ctracker/classes/class_ct_userfunctions.' . PHP_EXT);
  56. $search_system = new ct_userfunctions();
  57. $search_system->search_handler();
  58. unset($search_system);
  59. }
  60. // CrackerTracker v5.x
  61. // SELF AUTH
  62. // MG Added for an indepth auth check and SELF posts - BEGIN
  63. $is_auth_ary = array();
  64. $is_auth_ary = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  65. // MG Added for an indepth auth check and SELF posts - END
  66. // UPI2DB - BEGIN
  67. if($user->data['upi2db_access'])
  68. {
  69. $params = array(
  70. POST_FORUM_URL => POST_FORUM_URL,
  71. POST_TOPIC_URL => POST_TOPIC_URL,
  72. POST_POST_URL => POST_POST_URL,
  73. 'mar' => 'mar',
  74. );
  75. while(list($var, $param) = @each($params))
  76. {
  77. ${$var} = request_var($param, 0);
  78. }
  79. $params = array(
  80. 'always_read' => 'always_read',
  81. 's2' => 's2',
  82. 'do' => 'do',
  83. 'tt' => 'tt'
  84. );
  85. while(list($var, $param) = @each($params))
  86. {
  87. ${$var} = request_var($param, '');
  88. }
  89. $mar_topic_id = request_var('mar_topic_id', array(0));
  90. if (!defined('UPI2DB_UNREAD'))
  91. {
  92. $user->data['upi2db_unread'] = upi2db_unread();
  93. }
  94. if($always_read || $do || ($mar && !empty($mar_topic_id)))
  95. {
  96. if($always_read)
  97. {
  98. $mark_read_text = always_read($t, $always_read, $user->data['upi2db_unread']);
  99. }
  100. if($do)
  101. {
  102. $mark_read_text = set_unread($t, $f, $p, $user->data['upi2db_unread'], $do, $tt);
  103. }
  104. if($mar && !empty($mar_topic_id))
  105. {
  106. search_mark_as_read($mar_topic_id);
  107. $mark_read_text = $lang['upi2db_submit_topic_mark_read'];
  108. }
  109. $redirect_url = append_sid(CMS_PAGE_SEARCH . '?search_id=' . $search_id . (isset($s2) ? ('&amp;s2=' . $s2) : ''));
  110. meta_refresh(3, $redirect_url);
  111. $message = $mark_read_text . '<br /><br />' . sprintf($lang['Click_return_search'], '<a href="' . append_sid(CMS_PAGE_SEARCH . '?search_id=' . $search_id . (isset($s2) ? ('&amp;s2=' . $s2) : '')) . '">', '</a>');
  112. message_die(GENERAL_MESSAGE, $message);
  113. }
  114. $count_new_posts = sizeof($user->data['upi2db_unread']['new_posts']);
  115. $count_edit_posts = sizeof($user->data['upi2db_unread']['edit_posts']);
  116. $count_always_read = sizeof($user->data['upi2db_unread']['always_read']['topics']);
  117. $count_mark_unread = sizeof($user->data['upi2db_unread']['mark_posts']);
  118. }
  119. // UPI2DB - END
  120. $cms_page['page_id'] = 'search';
  121. $cms_page['page_nav'] = (!empty($cms_config_layouts[$cms_page['page_id']]['page_nav']) ? true : false);
  122. $cms_page['global_blocks'] = (!empty($cms_config_layouts[$cms_page['page_id']]['global_blocks']) ? true : false);
  123. $cms_auth_level = (isset($cms_config_layouts[$cms_page['page_id']]['view']) ? $cms_config_layouts[$cms_page['page_id']]['view'] : AUTH_ALL);
  124. check_page_auth($cms_page['page_id'], $cms_auth_level);
  125. $mode = request_var('mode', '');
  126. $only_bluecards = (!empty($_POST['only_bluecards']) ? 1 : 0);
  127. $search_keywords = request_var('search_keywords', '', true);
  128. $is_newposts = false;
  129. $search_author = request_var('search_author', '', true);
  130. if (!empty($search_author))
  131. {
  132. $search_author = phpbb_clean_username($search_author);
  133. $search_topic_starter = check_http_var_exists('search_topic_starter', true);
  134. }
  135. else
  136. {
  137. $search_author = '';
  138. $search_topic_starter = false;
  139. }
  140. if (isset($search_mode) && ($search_mode == 'bookmarks'))
  141. {
  142. // TO DO: force to false, and decide if we would like to overwrite it with Profile Global Blocks settings...
  143. //$cms_page['global_blocks'] = (!empty($cms_config_layouts['profile']['global_blocks']) ? true : false);
  144. $cms_page['global_blocks'] = false;
  145. }
  146. $search_terms = request_var('search_terms', '');
  147. $search_terms = ($search_terms == 'all') ? 1 : 0;
  148. $search_fields_types = array('all', 'titleonly', 'msgonly');
  149. $search_fields = request_var('search_fields', '');
  150. $search_fields = check_var_value($search_fields, $search_fields_types);
  151. $search_cat = request_var('search_cat', -1);
  152. $search_forum = request_var('search_forum', -1);
  153. $search_thanks = request_var('search_thanks', 0);
  154. $search_thanks = (($search_thanks >= '2') && empty($config['disable_likes_posts'])) ? $search_thanks : false;
  155. $search_where = request_post_var('search_where', 'Root');
  156. $search_where_topic = request_post_var('search_where_topic', 'Root');
  157. $search_where_topic = (!empty($search_where_topic) ? (str_replace(POST_TOPIC_URL, '', $search_where_topic)) : false);
  158. $search_where_topic = !empty($search_where_topic) ? intval($search_where_topic) : false;
  159. $search_where_topic = ($search_where_topic > 0) ? $search_where_topic : false;
  160. $sort_by = request_var('sort_by', 0);
  161. $sort_dir = request_var('sort_dir', 'DESC');
  162. $sort_dir = check_var_value($sort_dir, array('DESC', 'ASC'));
  163. $psort_types = array('time', 'cat');
  164. $psort = request_var('psort', 'time');
  165. $psort = check_var_value($psort, $psort_types);
  166. $topic_days = request_var('search_time', 0);
  167. if (!empty($topic_days))
  168. {
  169. $search_time = time() - ($topic_days * 86400);
  170. }
  171. else
  172. {
  173. $search_time = 0;
  174. $topic_days = 0;
  175. }
  176. $search_date = request_var('d', 0);
  177. $show_results = request_var('show_results', 'posts');
  178. $show_results = check_var_value($show_results, array('posts', 'topics'));
  179. // $sr is used to allow users to override the default result displaying for new posts
  180. $sr_cn = $config['cookie_name'] . '_sr';
  181. if(isset($_GET['sr']))
  182. {
  183. $sr_get = (isset($_GET['sr']) && ($_GET['sr'] == 't')) ? 't' : 'p';
  184. $user->set_cookie('sr', $sr_get, $user->cookie_expire);
  185. $_COOKIE[$sr_cn] = $sr_get;
  186. }
  187. $sr_cookie = (isset($_COOKIE[$sr_cn]) && ($_COOKIE[$sr_cn] == 't')) ? 't' : 'p';
  188. $sr = $sr_cookie;
  189. $return_chars = request_var('return_chars', 200);
  190. $return_chars = ($return_chars >= -1) ? $return_chars : 200;
  191. // MG: if the users chooses to show no chars from posts, then we force topics view.
  192. $show_results = ($return_chars == 0) ? 'topics' : $show_results;
  193. $is_ajax = request_var('is_ajax', 0);
  194. $start = request_var('start', 0);
  195. $start = ($start < 0) ? 0 : $start;
  196. $page_number = request_var('page_number', 0);
  197. $page_number = ($page_number < 1) ? 0 : $page_number;
  198. $start = (empty($page_number) ? $start : (($page_number * $config['topics_per_page']) - $config['topics_per_page']));
  199. $sort_by_types = array($lang['Sort_Time'], $lang['Sort_Post_Subject'], $lang['Sort_Topic_Title'], $lang['Sort_Author'], $lang['Sort_Forum']);
  200. // Start Advanced IP Tools Pack MOD
  201. // For security reasons, we need to make sure the IP lookup is coming from an admin or mod.
  202. $search_ip = '';
  203. $ip_display_auth = ip_display_auth($user->data, false);
  204. if (!empty($ip_display_auth))
  205. {
  206. $ip_address = request_var('search_ip', '');
  207. if (!empty($ip_address))
  208. {
  209. $ip_address = $db->sql_escape($ip_address);
  210. $search_ip = str_replace('*', '%', $ip_address);
  211. }
  212. else
  213. {
  214. $search_ip = '';
  215. }
  216. }
  217. // End Advanced IP Tools Pack MOD
  218. // encoding match for workaround
  219. $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
  220. // Begin core code
  221. if (($search_mode == 'bookmarks') && !$user->data['session_logged_in'])
  222. {
  223. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . CMS_PAGE_SEARCH . '?search_id=bookmarks&amp;search_mode=bookmarks', true));
  224. }
  225. if (($search_mode == 'bookmarks') && ($mode == 'removebm'))
  226. {
  227. // Delete Bookmarks
  228. $delete = (isset($_POST['delete'])) ? true : false;
  229. if ($delete && isset($_POST['topic_id_list']))
  230. {
  231. $topics = request_post_var('topic_id_list', array(0));
  232. $topic_list = implode(',', $topics);
  233. if ($user->data['session_logged_in'])
  234. {
  235. remove_bookmark($topic_list);
  236. redirect(append_sid(CMS_PAGE_SEARCH . '?search_id=bookmarks&amp;search_mode=bookmarks' . (!empty($start) ? ('&amp;start=' . $start) : ''), true));
  237. }
  238. }
  239. // Reset settings
  240. $mode = '';
  241. }
  242. if ($mode == 'searchuser')
  243. {
  244. // This handles the simple windowed user search functions called from various other scripts
  245. $search_username = request_var('search_username', '', true);
  246. $search_username = htmlspecialchars_decode($search_username, ENT_COMPAT);
  247. username_search($search_username);
  248. exit;
  249. }
  250. elseif (($search_keywords != '') || ($search_author != '') || $search_id || ($search_ip != '') || ($search_thanks != false))
  251. {
  252. $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_by', 'psort', 'sort_dir', 'show_results', 'return_chars');
  253. $search_results = '';
  254. // Search ID Limiter, decrease this value if you experience further timeout problems with searching forums
  255. $limiter = 5000;
  256. $current_time = time();
  257. // Cycle through options ...
  258. $search_id_filter_array = array('newposts', 'upi2db', 'egosearch', 'unanswered', 'bookmarks', 'mini_cal', 'mini_cal_events');
  259. if (in_array($search_id, $search_id_filter_array) || ($search_keywords != '') || ($search_author != '') || ($search_ip != '') || ($search_thanks != false))
  260. {
  261. if (($search_id == 'newposts') || ($search_id == 'upi2db') || ($search_id == 'egosearch') || ($search_id == 'mini_cal') || ($search_id == 'mini_cal_events') || (($search_author != '') && ($search_keywords == '')) || ($search_ip != ''))
  262. {
  263. if (($search_id == 'newposts') || ($search_id == 'upi2db'))
  264. {
  265. // UPI2DB REPLACE -------------------------------------------------
  266. /*
  267. if ($user->data['session_logged_in'])
  268. {
  269. #$sql = "SELECT post_id
  270. #FROM " . POSTS_TABLE . "
  271. #WHERE post_time >= " . $user->data['user_lastvisit'];
  272. }
  273. else
  274. {
  275. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . CMS_PAGE_SEARCH . '&search_id=newposts', true));
  276. }
  277. $show_results = 'topics';
  278. */
  279. // -------------------------------------------------
  280. if ($user->data['session_logged_in'])
  281. {
  282. // UPI2DB - BEGIN
  283. if(!$user->data['upi2db_access'] || ($search_id == 'newposts'))
  284. {
  285. $sql = "SELECT post_id
  286. FROM " . POSTS_TABLE . "
  287. WHERE post_time >= " . $user->data['user_lastvisit'] . "
  288. AND poster_id != " . $user->data['user_id'];
  289. }
  290. else
  291. {
  292. if($search_id == 'upi2db')
  293. {
  294. switch($s2)
  295. {
  296. case 'perm':
  297. $sql_where = (sizeof($user->data['upi2db_unread']['always_read']['topics']) == 0) ? 0 : implode(',', $user->data['upi2db_unread']['always_read']['topics']);
  298. break;
  299. case 'new':
  300. $sql_where = (sizeof($user->data['upi2db_unread']['new_posts']) == 0) ? 0 : implode(',', $user->data['upi2db_unread']['new_posts']);
  301. $sql_where2 = (sizeof($user->data['upi2db_unread']['edit_posts']) == 0) ? 0 : implode(',', $user->data['upi2db_unread']['edit_posts']);
  302. break;
  303. case 'mark':
  304. $sql_where = (sizeof($user->data['upi2db_unread']['mark_posts']) == 0) ? 0 : implode(',', $user->data['upi2db_unread']['mark_posts']);
  305. $sql_where2 = 0;
  306. break;
  307. }
  308. if(($search_id == 'upi2db') && ($s2 == 'perm'))
  309. {
  310. $sql = "SELECT post_id
  311. FROM " . POSTS_TABLE . "
  312. WHERE topic_id IN (" . $sql_where . ")";
  313. }
  314. if(($search_id == 'upi2db') && ($s2 != 'perm'))
  315. {
  316. $sql = "SELECT post_id
  317. FROM " . POSTS_TABLE . "
  318. WHERE (post_id IN (" . $sql_where . ") OR post_id IN (" . $sql_where2 . "))";
  319. }
  320. if(empty($sql_where) && empty($sql_where2))
  321. {
  322. redirect(append_sid(CMS_PAGE_FORUM));
  323. }
  324. }
  325. }
  326. // UPI2DB - END
  327. }
  328. else
  329. {
  330. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . CMS_PAGE_SEARCH . '&search_id=newposts', true));
  331. }
  332. // UPI2DB - BEGIN
  333. if($search_id == 'newposts')
  334. {
  335. $is_newposts = true;
  336. }
  337. if((($search_id == 'newposts') && ($sr != 't')) || (($search_id == 'upi2db') && ($s2 == 'mark')))
  338. {
  339. $show_results = 'posts';
  340. }
  341. else
  342. {
  343. $show_results = 'topics';
  344. }
  345. // UPI2DB - END
  346. $sort_by = 0;
  347. $sort_dir = 'DESC';
  348. }
  349. // Start Advanced IP Tools Pack MOD
  350. elseif ($search_ip != '')
  351. {
  352. $sql = "SELECT post_id FROM " . POSTS_TABLE . " WHERE poster_ip LIKE '$search_ip'";
  353. $show_results = 'posts';
  354. $sort_by = 0;
  355. $sort_dir = 'DESC';
  356. }
  357. //End Advanced IP Tools Pack MOD
  358. elseif ($search_cat != -1)
  359. {
  360. $sql = "SELECT post_id FROM " . POSTS_TABLE . " p, " . FORUMS_TABLE . " f WHERE p.forum_id = f.forum_id AND f.parent_id = $search_cat";
  361. $show_results = 'posts';
  362. $sort_by = 0;
  363. $sort_dir = 'DESC';
  364. }
  365. elseif ($search_forum != -1)
  366. {
  367. $sql = "SELECT post_id FROM " . POSTS_TABLE . " WHERE forum_id = $search_forum";
  368. $show_results = 'posts';
  369. $sort_by = 0;
  370. $sort_dir = 'DESC';
  371. }
  372. elseif ($search_id == 'egosearch')
  373. {
  374. if ($user->data['session_logged_in'])
  375. {
  376. $sql = "SELECT post_id
  377. FROM " . POSTS_TABLE . "
  378. WHERE poster_id = " . $user->data['user_id'];
  379. }
  380. else
  381. {
  382. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . CMS_PAGE_SEARCH . '&search_id=egosearch', true));
  383. }
  384. $show_results = 'topics';
  385. $sort_by = 0;
  386. $sort_dir = 'DESC';
  387. }
  388. elseif ($is_ajax)
  389. {
  390. $result_ar = array(
  391. 'search_id' => 0,
  392. 'results' => 0,
  393. 'keywords' => ''
  394. );
  395. AJAX_message_die($result_ar);
  396. }
  397. elseif ((MINI_CAL_CALENDAR_VERSION != 'NONE') && ($search_id == 'mini_cal'))
  398. {
  399. $nix_tomorrow = gmmktime (0, 0, 0, gmdate('m', $search_date), gmdate('d', $search_date) + 1, gmdate('Y', $search_date));
  400. $sql = "SELECT post_id
  401. FROM " . POSTS_TABLE . "
  402. WHERE post_time >= $search_date
  403. AND post_time < $nix_tomorrow";
  404. $show_results = 'posts';
  405. $sort_by = 0;
  406. $sort_dir = 'DESC';
  407. }
  408. elseif ((MINI_CAL_CALENDAR_VERSION != 'NONE') && ($search_id == 'mini_cal_events'))
  409. {
  410. // include the required events calendar support
  411. define('IN_MINI_CAL', 1);
  412. include_once(IP_ROOT_PATH . 'includes/mini_cal/mini_cal_config.' . PHP_EXT);
  413. $mini_cal_inc = 'mini_cal_' . MINI_CAL_CALENDAR_VERSION;
  414. include_once(IP_ROOT_PATH . 'includes/mini_cal/' . $mini_cal_inc . '.' . PHP_EXT);
  415. $sql = getMiniCalSearchSql($search_date);
  416. $show_results = 'posts';
  417. $sort_by = 0;
  418. $sort_dir = 'DESC';
  419. }
  420. else
  421. {
  422. //$search_author = str_replace('*', '%', trim($db->sql_escape($search_author)));
  423. $search_author = str_replace('*', '%', trim($db->sql_escape(utf8_clean_string($search_author))));
  424. if(!$only_bluecards && (strpos($search_author, '%') !== false) && (strlen(str_replace('%', '', $search_author)) < $config['search_min_chars']))
  425. {
  426. $search_author = '';
  427. message_die(GENERAL_MESSAGE, sprintf($lang['SEARCH_MIN_CHARS'], $config['search_min_chars']));
  428. }
  429. $sql = get_users_sql($search_author, true, false, false, false);
  430. $result = $db->sql_query($sql);
  431. $matching_userids = '';
  432. if ($row = $db->sql_fetchrow($result))
  433. {
  434. do
  435. {
  436. $matching_userids .= (($matching_userids != '') ? ', ' : '') . $row['user_id'];
  437. }
  438. while($row = $db->sql_fetchrow($result));
  439. }
  440. else
  441. {
  442. message_die(GENERAL_MESSAGE, $lang['No_search_match']);
  443. }
  444. /*
  445. $sql = "SELECT post_id
  446. FROM " . POSTS_TABLE . "
  447. WHERE poster_id IN ($matching_userids)";
  448. $sql .= ($only_bluecards) ? " AND post_bluecard > 0 " : "";
  449. if ($search_time)
  450. {
  451. $sql .= " AND post_time >= " . $search_time;
  452. }
  453. */
  454. $sql_from_ts = '';
  455. $sql_where_ts = '';
  456. if ($search_topic_starter)
  457. {
  458. $sql_from_ts = ", " . TOPICS_TABLE . " t";
  459. $sql_where_ts = " AND p.post_id = t.topic_first_post_id";
  460. }
  461. $sql = "SELECT p.post_id
  462. FROM " . POSTS_TABLE . " p" . $sql_from_ts . "
  463. WHERE p.poster_id IN (" . $matching_userids . ")" . $sql_where_ts;
  464. $sql .= ($only_bluecards) ? " AND p.post_bluecard > 0 " : "";
  465. if ($search_time)
  466. {
  467. $sql .= " AND p.post_time >= " . $search_time;
  468. }
  469. }
  470. //die($sql);
  471. $result = $db->sql_query($sql);
  472. $search_ids = array();
  473. while($row = $db->sql_fetchrow($result))
  474. {
  475. $search_ids[] = $row['post_id'];
  476. }
  477. $db->sql_freeresult($result);
  478. $total_match_count = sizeof($search_ids);
  479. }
  480. elseif ($search_keywords != '')
  481. {
  482. stopwords_synonyms_init();
  483. $split_search = array();
  484. $stripped_keywords = stripslashes($search_keywords);
  485. $split_search = (!strstr($multibyte_charset, $lang['ENCODING'])) ? split_words(clean_words('search', $stripped_keywords, $stopwords_array, $synonyms_array), 'search') : explode(' ', $search_keywords);
  486. unset($stripped_keywords);
  487. $word_count = 0;
  488. $current_match_type = 'or';
  489. $word_match = array();
  490. $result_list = array();
  491. for($i = 0; $i < sizeof($split_search); $i++)
  492. {
  493. if (!$only_bluecards && strlen(str_replace(array('*', '%'), '', trim($split_search[$i]))) < $config['search_min_chars'])
  494. {
  495. $split_search[$i] = '';
  496. continue;
  497. }
  498. switch ($split_search[$i])
  499. {
  500. case 'and':
  501. $current_match_type = 'and';
  502. break;
  503. case 'or':
  504. $current_match_type = 'or';
  505. break;
  506. case 'not':
  507. $current_match_type = 'not';
  508. break;
  509. default:
  510. if (!empty($search_terms))
  511. {
  512. $current_match_type = 'and';
  513. }
  514. if (!strstr($multibyte_charset, $lang['ENCODING']))
  515. {
  516. $match_word = str_replace('*', '%', $split_search[$i]);
  517. $search_add_sql = '';
  518. if (($search_fields == 'msgonly') || ($search_fields == 'titleonly'))
  519. {
  520. $search_add_sql = 'AND m.title_match = ' . (($search_fields == 'msgonly') ? '0' : '1');
  521. }
  522. $search_add_sql .= ($only_bluecards) ? " AND p.post_bluecard > 0 AND m.post_id = p.post_id " : '';
  523. $sql = "SELECT m.post_id
  524. FROM " . SEARCH_WORD_TABLE . " w, " . SEARCH_MATCH_TABLE . " m " . (($only_bluecards) ? ','.POSTS_TABLE . ' p ' : '') . "
  525. WHERE w.word_text LIKE '" . $db->sql_escape($match_word) . "'
  526. AND m.word_id = w.word_id
  527. AND w.word_common <> 1
  528. " . $search_add_sql;
  529. }
  530. else
  531. {
  532. $match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
  533. $search_add_sql = '';
  534. if ($search_fields == 'msgonly')
  535. {
  536. $search_add_sql = "p.post_text LIKE '" . $db->sql_escape($match_word) . "'";
  537. }
  538. elseif ($search_fields == 'titleonly')
  539. {
  540. $search_add_sql = "p.post_subject LIKE '" . $db->sql_escape($match_word) . "'";
  541. }
  542. else
  543. {
  544. $search_add_sql = "p.post_text LIKE '" . $db->sql_escape($match_word) . "' OR p.post_subject LIKE '" . $db->sql_escape($match_word) . "'";
  545. }
  546. $search_add_sql .= ($only_bluecards) ? " AND p.post_bluecard > 0" : '';
  547. $sql = "SELECT p.post_id
  548. FROM " . POSTS_TABLE . " p
  549. WHERE " . $search_add_sql;
  550. }
  551. $result = $db->sql_query($sql);
  552. $row = array();
  553. while($temp_row = $db->sql_fetchrow($result))
  554. {
  555. $row[$temp_row['post_id']] = 1;
  556. if (!$word_count)
  557. {
  558. $result_list[$temp_row['post_id']] = 1;
  559. }
  560. elseif ($current_match_type == 'or')
  561. {
  562. $result_list[$temp_row['post_id']] = 1;
  563. }
  564. elseif ($current_match_type == 'not')
  565. {
  566. $result_list[$temp_row['post_id']] = 0;
  567. }
  568. }
  569. if ($current_match_type == 'and' && $word_count)
  570. {
  571. @reset($result_list);
  572. while(list($post_id, $match_count) = @each($result_list))
  573. {
  574. if (!$row[$post_id])
  575. {
  576. $result_list[$post_id] = 0;
  577. }
  578. }
  579. }
  580. $word_count++;
  581. $db->sql_freeresult($result);
  582. }
  583. }
  584. @reset($result_list);
  585. $search_ids = array();
  586. while(list($post_id, $matches) = each($result_list))
  587. {
  588. if ($matches)
  589. {
  590. $search_ids[] = $post_id;
  591. }
  592. }
  593. unset($result_list);
  594. $total_match_count = sizeof($search_ids);
  595. }
  596. //
  597. // If user is logged in then we'll check to see which (if any) private
  598. // forums they are allowed to view and include them in the search.
  599. //
  600. // If not logged in we explicitly prevent searching of private forums
  601. //
  602. $auth_sql = '';
  603. // get the object list
  604. $keys = array();
  605. $keys = get_auth_keys($search_where, true, -1, -1, 'auth_read');
  606. $keys = get_auth_keys($search_where, true);
  607. $s_flist = '';
  608. for ($i = 0; $i < sizeof($keys['id']); $i++)
  609. {
  610. if (isset($tree['type'][$keys['idx'][$i]]) && ($tree['type'][$keys['idx'][$i]] == POST_FORUM_URL) && isset($tree['auth'][$keys['id'][$i]]['auth_read']) && $tree['auth'][$keys['id'][$i]]['auth_read'])
  611. {
  612. $s_flist .= (($s_flist != '') ? ', ' : '') . $tree['id'][$keys['idx'][$i]];
  613. }
  614. }
  615. if ($s_flist != '')
  616. {
  617. $auth_sql .= (($auth_sql != '') ? " AND" : '') . " f.forum_id IN ($s_flist) ";
  618. $auth_sql .= ($search_where_topic ? (" AND p.topic_id = " . $search_where_topic) : '');
  619. }
  620. else
  621. {
  622. message_die(GENERAL_MESSAGE, $lang['No_searchable_forums']);
  623. }
  624. // Author name search
  625. if ($search_author != '')
  626. {
  627. if (preg_match('#^[\*%]+$#', trim($search_author)) || preg_match('#^[^\*]{1,2}$#', str_replace(array('*', '%'), '', trim($search_author))))
  628. {
  629. $search_author = '';
  630. }
  631. $search_author = str_replace('*', '%', trim($db->sql_escape($search_author)));
  632. }
  633. if ($total_match_count)
  634. {
  635. if ($show_results == 'topics')
  636. {
  637. // This one is a beast, try to seperate it a bit (workaround for connection timeouts)
  638. $search_id_chunks = array();
  639. $count = 0;
  640. $chunk = 0;
  641. if (sizeof($search_ids) > $limiter)
  642. {
  643. for ($i = 0; $i < sizeof($search_ids); $i++)
  644. {
  645. if ($count == $limiter)
  646. {
  647. $chunk++;
  648. $count = 0;
  649. }
  650. $search_id_chunks[$chunk][$count] = $search_ids[$i];
  651. $count++;
  652. }
  653. }
  654. else
  655. {
  656. $search_id_chunks[0] = $search_ids;
  657. }
  658. $search_ids = array();
  659. for ($i = 0; $i < sizeof($search_id_chunks); $i++)
  660. {
  661. $where_sql = '';
  662. if ($search_time)
  663. {
  664. $where_sql .= (($search_author == '') && ($auth_sql == '')) ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time ";
  665. }
  666. if (($search_author == '') && ($auth_sql == ''))
  667. {
  668. $sql = "SELECT topic_id
  669. FROM " . POSTS_TABLE . "
  670. WHERE post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
  671. $where_sql
  672. GROUP BY topic_id";
  673. }
  674. else
  675. {
  676. $from_sql = POSTS_TABLE . " p";
  677. if ($search_author != '')
  678. {
  679. if ($search_topic_starter)
  680. {
  681. $from_sql .= ", " . USERS_TABLE . " u, " . TOPICS_TABLE . " t";
  682. $where_sql .= " AND u.user_id = p.poster_id AND LOWER(u.username) LIKE '" . strtolower($search_author) . "' AND p.post_id = t.topic_first_post_id ";
  683. }
  684. else
  685. {
  686. $from_sql .= ", " . USERS_TABLE . " u";
  687. $where_sql .= " AND u.user_id = p.poster_id AND LOWER(u.username) LIKE '" . strtolower($search_author) . "' ";
  688. }
  689. }
  690. if ($auth_sql != '')
  691. {
  692. $from_sql .= ", " . FORUMS_TABLE . " f";
  693. $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
  694. }
  695. $sql = "SELECT p.topic_id
  696. FROM $from_sql
  697. WHERE p.post_id IN (" . implode(", ", $search_id_chunks[$i]) . ")
  698. $where_sql
  699. GROUP BY p.topic_id";
  700. }
  701. //die($sql);
  702. $result = $db->sql_query($sql);
  703. while ($row = $db->sql_fetchrow($result))
  704. {
  705. $search_ids[] = $row['topic_id'];
  706. }
  707. $db->sql_freeresult($result);
  708. }
  709. $total_match_count = sizeof($search_ids);
  710. }
  711. elseif (($search_author != '') || $search_time || ($auth_sql != ''))
  712. {
  713. $search_id_chunks = array();
  714. $count = 0;
  715. $chunk = 0;
  716. if (sizeof($search_ids) > $limiter)
  717. {
  718. for ($i = 0; $i < sizeof($search_ids); $i++)
  719. {
  720. if ($count == $limiter)
  721. {
  722. $chunk++;
  723. $count = 0;
  724. }
  725. $search_id_chunks[$chunk][$count] = $search_ids[$i];
  726. $count++;
  727. }
  728. }
  729. else
  730. {
  731. $search_id_chunks[0] = $search_ids;
  732. }
  733. $search_ids = array();
  734. for ($i = 0; $i < sizeof($search_id_chunks); $i++)
  735. {
  736. $where_sql = ($search_author == '' && $auth_sql == '') ? 'post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')' : 'p.post_id IN (' . implode(', ', $search_id_chunks[$i]) . ')';
  737. $select_sql = ($search_author == '' && $auth_sql == '') ? 'post_id' : 'p.post_id';
  738. $from_sql = ($search_author == '' && $auth_sql == '') ? POSTS_TABLE : POSTS_TABLE . ' p';
  739. if ($search_time)
  740. {
  741. $where_sql .= ($search_author == '' && $auth_sql == '') ? " AND post_time >= $search_time " : " AND p.post_time >= $search_time";
  742. }
  743. if ($auth_sql != '')
  744. {
  745. $from_sql .= ", " . FORUMS_TABLE . " f";
  746. $where_sql .= " AND f.forum_id = p.forum_id AND $auth_sql";
  747. }
  748. if ($search_author != '')
  749. {
  750. $from_sql .= ", " . USERS_TABLE . " u";
  751. $where_sql .= " AND u.user_id = p.poster_id AND LOWER(u.username) LIKE '" . strtolower($search_author) . "'";
  752. }
  753. $sql = "SELECT " . $select_sql . "
  754. FROM $from_sql
  755. WHERE $where_sql";
  756. $result = $db->sql_query($sql);
  757. while($row = $db->sql_fetchrow($result))
  758. {
  759. $search_ids[] = $row['post_id'];
  760. }
  761. $db->sql_freeresult($result);
  762. }
  763. $total_match_count = sizeof($search_ids);
  764. }
  765. }
  766. elseif ($search_thanks != false)
  767. {
  768. if ($user->data['session_logged_in'])
  769. {
  770. if ($auth_sql != '')
  771. {
  772. $sql = "SELECT DISTINCT(t.topic_id), f.forum_id
  773. FROM " . POSTS_LIKES_TABLE . " th, " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
  774. WHERE t.topic_poster = '" . $search_thanks . "'
  775. AND t.topic_id = th.topic_id
  776. AND t.forum_id = f.forum_id
  777. AND th.post_id = t.topic_first_post_id
  778. AND $auth_sql";
  779. }
  780. else
  781. {
  782. $sql = "SELECT DISTINCT(t.topic_id)
  783. FROM " . POSTS_LIKES_TABLE . " th, " . TOPICS_TABLE . " t
  784. WHERE t.topic_poster = '" . $search_thanks . "'
  785. AND t.topic_id = th.topic_id
  786. AND th.post_id = t.topic_first_post_id";
  787. }
  788. }
  789. else
  790. {
  791. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=' . CMS_PAGE_SEARCH . '&search_thanks=' . $search_thanks, true));
  792. }
  793. $result = $db->sql_query($sql);
  794. $search_ids = array();
  795. while($row = $db->sql_fetchrow($result))
  796. {
  797. $search_ids[] = $row['topic_id'];
  798. }
  799. $db->sql_freeresult($result);
  800. $total_match_count = sizeof($search_ids);
  801. if ($total_match_count <= $start) // No results for the selected page
  802. {
  803. $start = $total_match_count - 1;
  804. $start = intval($start / $config['topics_per_page']) * $config['topics_per_page'];
  805. }
  806. $show_results = 'topics';
  807. $sort_by = 0;
  808. $sort_dir = 'DESC';
  809. }
  810. elseif ($search_id == 'unanswered')
  811. {
  812. if ($auth_sql != '')
  813. {
  814. $sql = "SELECT t.topic_id, f.forum_id
  815. FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
  816. WHERE t.topic_replies = 0
  817. AND t.forum_id = f.forum_id
  818. AND t.topic_moved_id = 0
  819. AND $auth_sql";
  820. }
  821. else
  822. {
  823. $sql = "SELECT topic_id
  824. FROM " . TOPICS_TABLE . "
  825. WHERE topic_replies = 0
  826. AND topic_moved_id = 0";
  827. }
  828. $result = $db->sql_query($sql);
  829. $search_ids = array();
  830. while($row = $db->sql_fetchrow($result))
  831. {
  832. $search_ids[] = $row['topic_id'];
  833. }
  834. $db->sql_freeresult($result);
  835. $total_match_count = sizeof($search_ids);
  836. // Basic requirements
  837. $show_results = 'topics';
  838. $sort_by = 0;
  839. $sort_dir = 'DESC';
  840. }
  841. elseif ($search_id == 'bookmarks')
  842. {
  843. if ($user->data['session_logged_in'])
  844. {
  845. if ($auth_sql != '')
  846. {
  847. $sql = "SELECT t.topic_id, f.forum_id
  848. FROM " . TOPICS_TABLE . " t, " . BOOKMARK_TABLE . " b, " . FORUMS_TABLE . " f
  849. WHERE t.topic_id = b.topic_id
  850. AND t.forum_id = f.forum_id
  851. AND b.user_id = " . $user->data['user_id'] . "
  852. AND $auth_sql";
  853. }
  854. else
  855. {
  856. $sql = "SELECT t.topic_id
  857. FROM " . TOPICS_TABLE . " t, " . BOOKMARK_TABLE . " b
  858. WHERE t.topic_id = b.topic_id
  859. AND b.user_id = " . $user->data['user_id'];
  860. }
  861. }
  862. else
  863. {
  864. redirect(append_sid(CMS_PAGE_LOGIN. '?redirect=' . CMS_PAGE_SEARCH . '?search_id=bookmarks&amp;search_mode=bookmarks', true));
  865. }
  866. $result = $db->sql_query($sql);
  867. $search_ids = array();
  868. while($row = $db->sql_fetchrow($result))
  869. {
  870. $search_ids[] = $row['topic_id'];
  871. }
  872. $db->sql_freeresult($result);
  873. $total_match_count = sizeof($search_ids);
  874. if ($total_match_count <= $start) // No results for the selected page
  875. {
  876. $start = $total_match_count - 1;
  877. $start = intval($start / $config['topics_per_page']) * $config['topics_per_page'];
  878. }
  879. // Basic requirements
  880. $show_results = 'bookmarks';
  881. $sort_by = 0;
  882. $sort_dir = 'DESC';
  883. }
  884. else
  885. {
  886. message_die(GENERAL_MESSAGE, $lang['No_search_match']);
  887. }
  888. //0 = post_time, 1; 2 = title, 3 = author, 4 = forum
  889. $sort_by = request_var('sort_by', 0);
  890. $sort_dir = request_var('sort_dir', '');
  891. $sort_dir = ($sort_dir == 'ASC') ? $sort_dir : 'DESC';
  892. // Delete old data from the search result table
  893. $sql = 'DELETE FROM ' . SEARCH_TABLE . ' WHERE search_time < ' . ($current_time - (int) $config['session_length']);
  894. $result = $db->sql_query($sql);
  895. // Store new result data
  896. $search_results = implode(', ', $search_ids);
  897. $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
  898. //
  899. // Combine both results and search data (apart from original query)
  900. // so we can serialize it and place it in the DB
  901. //
  902. $store_search_data = array();
  903. //
  904. // Limit the character length (and with this the results displayed at all following pages) to prevent
  905. // truncated result arrays. Normally, search results above 12000 are affected.
  906. // - to include or not to include
  907. /*
  908. $max_result_length = 60000;
  909. if (strlen($search_results) > $max_result_length)
  910. {
  911. $search_results = substr($search_results, 0, $max_result_length);
  912. $search_results = substr($search_results, 0, strrpos($search_results, ','));
  913. $total_match_count = count(explode(', ', $search_results));
  914. }
  915. */
  916. for($i = 0; $i < sizeof($store_vars); $i++)
  917. {
  918. $store_search_data[$store_vars[$i]] = !empty(${$store_vars[$i]}) ? ${$store_vars[$i]} : '';
  919. }
  920. $result_array = serialize($store_search_data);
  921. unset($store_search_data);
  922. mt_srand ((double) microtime() * 1000000);
  923. $search_type = $search_id; //create a save
  924. $search_id = mt_rand();
  925. $sql = "UPDATE " . SEARCH_TABLE . "
  926. SET search_id = $search_id, search_time = $current_time, search_array = '" . $db->sql_escape($result_array) . "'
  927. WHERE session_id = '" . $user->data['session_id'] . "'";
  928. $db->sql_return_on_error(true);
  929. $result = $db->sql_query($sql);
  930. $db->sql_return_on_error(false);
  931. if (!$result || !$db->sql_affectedrows())
  932. {
  933. $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_time, search_array)
  934. VALUES($search_id, '" . $user->data['session_id'] . "', $current_time, '" . $db->sql_escape($result_array) . "')";
  935. $result = $db->sql_query($sql);
  936. }
  937. }
  938. else
  939. {
  940. $search_id = intval($search_id);
  941. if ($search_id)
  942. {
  943. $sql = "SELECT search_array
  944. FROM " . SEARCH_TABLE . "
  945. WHERE search_id = " . $search_id . "
  946. AND session_id = '" . $user->data['session_id'] . "'";
  947. $result = $db->sql_query($sql);
  948. if ($row = $db->sql_fetchrow($result))
  949. {
  950. $search_data = unserialize($row['search_array']);
  951. $psort_main = $psort;
  952. for($i = 0; $i < sizeof($store_vars); $i++)
  953. {
  954. ${$store_vars[$i]} = $search_data[$store_vars[$i]];
  955. }
  956. $psort = $psort_main;
  957. }
  958. }
  959. }
  960. // Look up data ...
  961. if ($search_results != '')
  962. {
  963. //$this_auth = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  964. if ($show_results == 'posts')
  965. {
  966. $sql = "SELECT p.*, f.forum_id, f.forum_name, t.*, u.username, u.user_id, u.user_active, u.user_mask, u.user_color, u.user_sig
  967. FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p
  968. WHERE p.post_id IN ($search_results)
  969. AND f.forum_id = p.forum_id
  970. AND p.topic_id = t.topic_id
  971. AND p.poster_id = u.user_id";
  972. }
  973. else
  974. {
  975. $sql = "SELECT t.*, f.forum_id, f.forum_name, u.username, u.user_id, u.user_active, u.user_mask, u.user_color, u2.username as user2, u2.user_id as id2, u2.user_active as user_active2, u2.user_mask as user_mask2, u2.user_color as user_color2, p.post_username, p2.post_username AS post_username2, p2.post_time
  976. FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
  977. WHERE t.topic_id IN ($search_results)
  978. AND t.topic_poster = u.user_id
  979. AND f.forum_id = t.forum_id
  980. AND p.post_id = t.topic_first_post_id
  981. AND p2.post_id = t.topic_last_post_id
  982. AND u2.user_id = p2.poster_id";
  983. }
  984. $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page'];
  985. $sql .= " ORDER BY ";
  986. if ($psort == 'cat')
  987. {
  988. $sql .= 'f.forum_id ASC, ';
  989. }
  990. switch ($sort_by)
  991. {
  992. case 1:
  993. if ($show_results == 'posts')
  994. {
  995. $sql .= 'p.post_subject';
  996. break;
  997. }
  998. case 2:
  999. $sql .= 't.topic_title';
  1000. $sort_by = 2;
  1001. break;
  1002. case 3:
  1003. $sql .= 'u.username';
  1004. break;
  1005. case 4:
  1006. $sql .= 'f.forum_id';
  1007. break;
  1008. default:
  1009. $sql .= ($show_results == 'posts') ? 'p.post_time' : 'p2.post_time';
  1010. $sort_by = 0;
  1011. break;
  1012. }
  1013. $template->assign_vars(array(
  1014. 'U_SELF' => CMS_PAGE_SEARCH . '?search_id=' . $search_type . '&amp;s2=' . $s2,
  1015. 'U_SELF_SORT' => CMS_PAGE_SEARCH . '?search_id=' . $search_type . '&amp;s2=' . $s2 . '&amp;sort_by=' . $sort_by,
  1016. )
  1017. );
  1018. $sql .= " $sort_dir LIMIT $start, " . $per_page;
  1019. $result = $db->sql_query($sql);
  1020. /* UPI2DB REPLACE
  1021. $searchset = array();
  1022. while($row = $db->sql_fetchrow($result))
  1023. {
  1024. $searchset[] = $row;
  1025. }
  1026. */
  1027. // UPI2DB - BEGIN
  1028. $searchset = array();
  1029. $searchset_gae = array();
  1030. $searchset_gan = array();
  1031. $searchset_ae = array();
  1032. $searchset_an = array();
  1033. $searchset_se = array();
  1034. $searchset_sn = array();
  1035. $searchset_e = array();
  1036. $searchset_n = array();
  1037. while($row = $db->sql_fetchrow($result))
  1038. {
  1039. if($user->data['upi2db_access'])
  1040. {
  1041. if($config['upi2db_edit_topic_first'])
  1042. {
  1043. if(isset($user->data['upi2db_unread']['edit_topics']) && in_array($row['topic_id'], $user->data['upi2db_unread']['edit_topics']) && $row['topic_type'] == POST_GLOBAL_ANNOUNCE)
  1044. {
  1045. $searchset_gae[] = $row;
  1046. }
  1047. elseif($row['topic_type'] == POST_GLOBAL_ANNOUNCE)
  1048. {
  1049. $searchset_gan[] = $row;
  1050. }
  1051. elseif(isset($user->data['upi2db_unread']['edit_topics']) && in_array($row['topic_id'], $user->data['upi2db_unread']['edit_topics']) && $row['topic_type'] == POST_ANNOUNCE)
  1052. {
  1053. $searchset_ae[] = $row;
  1054. }
  1055. elseif($row['topic_type'] == POST_ANNOUNCE)
  1056. {
  1057. $searchset_an[] = $row;
  1058. }
  1059. elseif(isset($user->data['upi2db_unread']['edit_topics']) && in_array($row['topic_id'], $user->data['upi2db_unread']['edit_topics']) && $row['topic_type'] == POST_STICKY)
  1060. {
  1061. $searchset_se[] = $row;
  1062. }
  1063. elseif($row['topic_type'] == POST_STICKY)
  1064. {
  1065. $searchset_sn[] = $row;
  1066. }
  1067. elseif(isset($user->data['upi2db_unread']['edit_topics']) && in_array($row['topic_id'], $user->data['upi2db_unread']['edit_topics']) && $row['topic_type'] != POST_GLOBAL_ANNOUNCE && $row['topic_type'] != POST_ANNOUNCE && $row['topic_type'] != POST_STICKY)
  1068. {
  1069. $searchset_e[] = $row;
  1070. }
  1071. elseif($row['topic_type'] != POST_GLOBAL_ANNOUNCE && $row['topic_type'] != POST_ANNOUNCE && $row['topic_type'] != POST_STICKY)
  1072. {
  1073. $searchset_n[] = $row;
  1074. }
  1075. }
  1076. else
  1077. {
  1078. if($row['topic_type'] == POST_GLOBAL_ANNOUNCE)
  1079. {
  1080. $searchset_gan[] = $row;
  1081. }
  1082. elseif($row['topic_type'] == POST_ANNOUNCE)
  1083. {
  1084. $searchset_an[] = $row;
  1085. }
  1086. elseif($row['topic_type'] == POST_STICKY)
  1087. {
  1088. $searchset_sn[] = $row;
  1089. }
  1090. elseif($row['topic_type'] != POST_GLOBAL_ANNOUNCE && $row['topic_type'] != POST_ANNOUNCE && $row['topic_type'] != POST_STICKY)
  1091. {
  1092. $searchset_n[] = $row;
  1093. }
  1094. }
  1095. }
  1096. else
  1097. {
  1098. $searchset[] = $row;
  1099. }
  1100. }
  1101. if($user->data['upi2db_access'])
  1102. {
  1103. if($config['upi2db_edit_topic_first'])
  1104. {
  1105. $searchset = array_merge($searchset_gae, $searchset_gan, $searchset_ae, $searchset_an, $searchset_se, $searchset_sn, $searchset_e, $searchset_n);
  1106. }
  1107. else
  1108. {
  1109. $searchset = array_merge($searchset_gan, $searchset_an, $searchset_sn, $searchset_n);
  1110. }
  1111. }
  1112. // UPI2DB - END
  1113. $db->sql_freeresult($result);
  1114. if ($is_ajax == 1)
  1115. {
  1116. $result_ar = array(
  1117. 'search_id' => ($total_match_count == 1) ? $searchset[0]['topic_id'] : $search_id,
  1118. 'results' => $total_match_count,
  1119. 'keywords' => $search_keywords
  1120. );
  1121. AJAX_message_die($result_ar);
  1122. }
  1123. elseif ($is_ajax == 2)
  1124. {
  1125. $result_title = '';
  1126. $max_results = ($total_match_count < 10) ? $total_match_count : 10;
  1127. $search_result_text = ($total_match_count == 1) ? $lang['AJAX_quick_search_result'] : sprintf($lang['AJAX_quick_search_results'], $total_match_count);
  1128. for ($sr = 0; $sr < $max_results; $sr++)
  1129. {
  1130. $result_title .= '<a href="' . append_sid(CMS_PAGE_VIEWTOPIC . '?' . POST_TOPIC_URL . '=' . $searchset[$sr]['topic_id'] . '&highlight=' . $search_keywords) . '">' . $searchset[$sr]['topic_title'] . '</a><br />';
  1131. }
  1132. $result_ar = array(
  1133. 'results' => $result_title,
  1134. 'error_msg' => $search_result_text
  1135. );
  1136. AJAX_message_die($result_ar);
  1137. }
  1138. include_once(IP_ROOT_PATH . 'includes/users_zebra_block.' . PHP_EXT);
  1139. if ($show_results == 'bookmarks')
  1140. {
  1141. $template_to_parse = 'search_results_bookmarks.tpl';
  1142. }
  1143. elseif ($show_results == 'posts')
  1144. {
  1145. $template_to_parse = 'search_results_posts.tpl';
  1146. }
  1147. else
  1148. {
  1149. $template_to_parse = 'search_results_topics.tpl';
  1150. }
  1151. make_jumpbox(CMS_PAGE_VIEWFORUM);
  1152. if ($show_results == 'bookmarks')
  1153. {
  1154. // Send variables for bookmarks
  1155. //$s_hidden_fields = '<input type="hidden" name="mode" value="removebm" />';
  1156. $template->assign_vars(array(
  1157. 'L_DELETE' => $lang['Delete'],
  1158. 'S_BM_ACTION' => append_sid(CMS_PAGE_SEARCH . '?search_id=bookmarks&amp;search_mode=bookmarks&amp;mode=removebm' . (!empty($start) ? ('&amp;start=' . $start) : '')),
  1159. 'S_HIDDEN_FIELDS' => $s_hidden_fields
  1160. )
  1161. );
  1162. }
  1163. $template->assign_vars(array(
  1164. 'L_SEARCH_MATCHES' => $l_search_matches,
  1165. 'L_TOPIC' => $lang['Topic']
  1166. )
  1167. );
  1168. // Added by MG: creation of $highlight_match_string
  1169. $words = array();
  1170. $highlight_match_string = '';
  1171. $highlight_active = '';
  1172. $highlight_match = array();
  1173. if (!empty($split_search))
  1174. {
  1175. for($j = 0; $j < sizeof($split_search); $j++)
  1176. {
  1177. $split_word = $split_search[$j];
  1178. if (($split_word != 'and') && ($split_word != 'or') && ($split_word != 'not'))
  1179. {
  1180. $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $split_word) . ')\b#is';
  1181. // Added by MG: creation of $highlight_match_string
  1182. $words[] = $split_word;
  1183. $highlight_active .= " " . $split_word;
  1184. for ($k = 0; $k < sizeof($synonyms_array); $k++)
  1185. {
  1186. list($replace_synonym, $match_synonym) = explode(' ', trim(strtolower($synonyms_array[$k])));
  1187. if ($replace_synonym == $split_word)
  1188. {
  1189. $highlight_match[] = '#\b(' . str_replace("*", "([\w]+)?", $replace_synonym) . ')\b#is';
  1190. // Added by MG: creation of $highlight_match_string
  1191. $words[] = $replace_synonym;
  1192. $highlight_active .= ' ' . $match_synonym;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. // Added by MG: creation of $highlight_match_string
  1199. for($i = 0; $i < sizeof($words); $i++)
  1200. {
  1201. $highlight_match_string .= (($highlight_match_string != '') ? '|' : '') . str_replace('*', '\w*', preg_quote($words[$i], '#'));
  1202. }
  1203. $highlight_match_string = rtrim($highlight_match_string, "\\");
  1204. $highlight_active = urlencode(trim($highlight_active));
  1205. $tracking_forums = (isset($_COOKIE[$config['cookie_name'] . '_f'])) ? unserialize($_COOKIE[$config['cookie_name'] . '_f']) : array();
  1206. $tracking_topics = (isset($_COOKIE[$config['cookie_name'] . '_t'])) ? unserialize($_COOKIE[$config['cookie_name'] . '_t']) : array();
  1207. if ($show_results == 'posts')
  1208. {
  1209. if ($search_where == -1)
  1210. {
  1211. $is_auth_ary = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  1212. }
  1213. else
  1214. {
  1215. $is_auth = auth(AUTH_ALL, $search_where, $user->data);
  1216. }
  1217. }
  1218. // MG User Replied - BEGIN
  1219. // check if user replied to the topic
  1220. define('USER_REPLIED_ICON', true);
  1221. $user_topics = $class_topics->user_replied_array($searchset);
  1222. // MG User Replied - END
  1223. $valid_results = 0;
  1224. for($i = 0; $i < sizeof($searchset); $i++)
  1225. {
  1226. // CrackerTracker v5.x
  1227. $sucheck = strtolower($highlight_active);
  1228. $sucheck = str_replace($ct_rules, '*', $sucheck);
  1229. if($sucheck != $highlight_active)
  1230. {
  1231. $highlight_active = '';
  1232. }
  1233. // CrackerTracker v5.x
  1234. $forum_id = !empty($searchset[$i]['forum_id']) ? $searchset[$i]['forum_id'] : 0;
  1235. $topic_id = !empty($searchset[$i]['topic_id']) ? $searchset[$i]['topic_id'] : 0;
  1236. $post_id = !empty($searchset[$i]['post_id']) ? $searchset[$i]['post_id'] : 0;
  1237. $forum_id_append = (!empty($forum_id) ? (POST_FORUM_URL . '=' . $forum_id) : '');
  1238. $topic_id_append = (!empty($topic_id) ? (POST_TOPIC_URL . '=' . $topic_id) : '');
  1239. $post_id_append = (!empty($post_id) ? (POST_POST_URL . '=' . $post_id) : '');
  1240. $post_id_append_url = (!empty($post_id) ? ('#p' . $post_id) : '');
  1241. $forum_url = append_sid(CMS_PAGE_VIEWFORUM . '?' . $forum_id_append);
  1242. $topic_url = append_sid(CMS_PAGE_VIEWTOPIC . '?' . $forum_id_append . '&amp;' . $topic_id_append . '&amp;highlight=' . $highlight_active);
  1243. $post_url = append_sid(CMS_PAGE_VIEWTOPIC . '?' . $forum_id_append . '&amp;' . $topic_id_append . '&amp;' . $post_id_append . '&amp;highlight=' . $highlight_active) . $post_id_append_url;
  1244. $user_replied = (!empty($user_topics) && isset($user_topics[$topic_id]));
  1245. $post_date = create_date_ip($config['default_dateformat'], $searchset[$i]['post_time'], $config['board_timezone']);
  1246. $message = !empty($searchset[$i]['post_text']) ? $searchset[$i]['post_text'] : '';
  1247. $message_compiled = (empty($searchset[$i]['post_text_compiled']) || !empty($user->data['session_logged_in']) || !empty($config['posts_precompiled'])) ? false : $searchset[$i]['post_text_compiled'];
  1248. $topic_title_data = $class_topics->generate_topic_title($topic_id, $searchset[$i], 255);
  1249. $topic_title = $topic_title_data['title'];
  1250. $topic_title_clean = $topic_title_data['title_clean'];
  1251. $topic_title_plain = $topic_title_data['title_plain'];
  1252. $topic_title_label = $topic_title_data['title_label'];
  1253. $topic_title_short = $topic_title_data['title_short'];
  1254. if ($show_results == 'posts')
  1255. {
  1256. if ($search_where == -1)
  1257. {
  1258. $is_auth = $is_auth_ary[$forum_id];
  1259. }
  1260. $clean_tags = false;
  1261. if ($return_chars != -1)
  1262. {
  1263. $clean_tags = true;
  1264. }
  1265. if($message_compiled === false)
  1266. {
  1267. $bbcode->allow_html = $config['allow_html'] && $searchset[$i]['enable_html'];
  1268. $bbcode->allow_bbcode = $config['allow_bbcode'] && $searchset[$i]['enable_bbcode'];
  1269. $bbcode->allow_smilies = $config['allow_smilies'] && $searchset[$i]['enable_smilies'];
  1270. $bbcode->code_post_id = $searchset[$i]['post_id'];
  1271. $message = $bbcode->parse($message, '', false, $clean_tags);
  1272. $bbcode->code_post_id = 0;
  1273. }
  1274. else
  1275. {
  1276. $message = $message_compiled;
  1277. }
  1278. if ($return_chars != -1)
  1279. {
  1280. //$message = (strlen($message) > $return_chars) ? substr($message, 0, $return_chars) . ' ...' : $message;
  1281. $message = truncate_html_string($message, $return_chars);
  1282. }
  1283. if ($highlight_active)
  1284. {
  1285. // Replaced by MG: creation of $highlight_match_string
  1286. $message = preg_replace('#(?!<.*)(?<!\w)(' . $highlight_match_string . ')(?!\w|[^<>]*>)#i', '<span class="highlight-w"><b>\1</b></span>', $message);
  1287. }
  1288. $post_subject = !empty($searchset[$i]['post_subject']) ? censor_text($searchset[$i]['post_subject']) : $topic_title;
  1289. $message = censor_text($message);
  1290. $poster = ($searchset[$i]['user_id'] != ANONYMOUS) ? colorize_username($searchset[$i]['user_id'], $searchset[$i]['username'], $searchset[$i]['user_color'], $searchset[$i]['user_active']) : (($searchset[$i]['post_username'] != '') ? $searchset[$i]['post_username'] : $lang['Guest']);
  1291. //$poster .= ($searchset[$i]['user_id'] != ANONYMOUS) ? $searchset[$i]['username'] : (($searchset[$i]['post_username'] != "") ? $searchset[$i]['post_username'] : $lang['Guest']);
  1292. if (($user->data['user_level'] != ADMIN) && !empty($searchset[$i]['user_mask']) && empty($searchset[$i]['user_active']))
  1293. {
  1294. $poster = $lang['INACTIVE_USER'];
  1295. }
  1296. // UPI2DB - BEGIN
  1297. if(!$user->data['upi2db_access'])
  1298. {
  1299. // UPI2DB - END
  1300. if ($user->data['session_logged_in'] && ($searchset[$i]['post_time'] > $user->data['user_lastvisit']))
  1301. {
  1302. if (!empty($tracking_topics[$topic_id]) && !empty($tracking_forums[$forum_id]))
  1303. {
  1304. $topic_last_read = ($tracking_topics[$topic_id] > $tracking_forums[$forum_id]) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
  1305. }
  1306. elseif (!empty($tracking_topics[$topic_id]) || !empty($tracking_forums[$forum_id]))
  1307. {
  1308. $topic_last_read = (!empty($tracking_topics[$topic_id])) ? $tracking_topics[$topic_id] : $tracking_forums[$forum_id];
  1309. }
  1310. if ($searchset[$i]['post_time'] > $topic_last_read)
  1311. {
  1312. $mini_post_img = $images['icon_minipost_new'];
  1313. $mini_post_alt = $lang['New_post'];
  1314. }
  1315. else
  1316. {
  1317. $mini_post_img = $images['icon_minipost'];
  1318. $mini_post_alt = $lang['Post'];
  1319. }
  1320. }
  1321. else
  1322. {
  1323. $mini_post_img = $images['icon_minipost'];
  1324. $mini_post_alt = $lang['Post'];
  1325. }
  1326. if (!empty($searchset[$i]['topic_calendar_time']) && ($searchset[$i]['post_id'] == $searchset[$i]['topic_first_post_id']))
  1327. {
  1328. $post_subject .= '</a></b>' . get_calendar_title($searchset[$i]['topic_calendar_time'], $searchset[$i]['topic_calendar_duration']);
  1329. }
  1330. // UPI2DB - BEGIN
  1331. $folder_image = $images['topic_nor_read'];
  1332. $folder_alt = ($searchset[$i]['topic_status'] == TOPIC_LOCKED) ? $lang['Topic_locked'] : $lang['No_new_posts'];
  1333. }
  1334. else
  1335. {
  1336. search_calc_unread_ip($user->data['upi2db_unread'], $topic_id, $searchset, $i, $mini_post_img, $mini_post_alt, $unread_color, $folder_image, $folder_alt);
  1337. }
  1338. $mark_topic_unread_array['unmark_post'] = 0;
  1339. if($user->data['upi2db_access'])
  1340. {
  1341. if($s2 == 'mark')
  1342. {
  1343. $post_id = $searchset[$i]['post_id'];
  1344. $mark_topic_unread_array['unmark_post'] = 1;
  1345. }
  1346. }
  1347. // UPI2DB - END
  1348. // SELF AUTH - BEGIN
  1349. // Comment the lines below if you wish to show RESERVED topics for AUTH_SELF.
  1350. $is_topic_reserved = false;
  1351. if (((($user->data['user_level'] != ADMIN) && ($user->data['user_level'] != MOD)) || (($user->data['user_level'] == MOD) && ($config['allow_mods_view_self'] == false))) && (intval($is_auth_ary[$searchset[$i]['forum_id']]['auth_read']) == AUTH_SELF) && ($searchset[$i]['user_id'] != $user->data['user_id']))
  1352. {
  1353. $is_topic_reserved = true;
  1354. continue;
  1355. /*
  1356. $poster = $lang['Reserved_Author'];
  1357. $topic_title = $lang['Reserved_Topic'];
  1358. $message = $lang['Reserved_Post'];
  1359. */
  1360. }
  1361. // SELF AUTH - END
  1362. $template->assign_block_vars('searchresults', array(
  1363. 'TOPIC_TITLE' => $topic_title,
  1364. 'TOPIC_TITLE_PLAIN' => $topic_title_plain,
  1365. 'FORUM_NAME' => get_object_lang(POST_FORUM_URL . $searchset[$i]['forum_id'], 'name'),
  1366. //'POST_SUBJECT' => $post_subject,
  1367. 'POST_DATE' => $post_date,
  1368. 'POSTER_NAME' => $poster,
  1369. 'TOPIC_REPLIES' => $searchset[$i]['topic_replies'],
  1370. 'TOPIC_VIEWS' => $searchset[$i]['topic_views'],
  1371. 'MESSAGE' => $message,
  1372. 'MINI_POST_IMG' => $mini_post_img,
  1373. 'L_MINI_POST_ALT' => $mini_post_alt,
  1374. // UPI2DB - BEGIN
  1375. 'L_TOPIC_FOLDER_ALT' => $folder_alt,
  1376. 'TOPIC_FOLDER_IMG' => $folder_image,
  1377. 'UNREAD_COLOR' => $unread_color,
  1378. 'UPI2DB_UNMARK_POST' => !empty($mark_topic_unread_array

Large files files are truncated, but you can click here to view the full file