PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/pafiledb/modules/pa_search.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 546 lines | 441 code | 69 blank | 36 comment | 82 complexity | c4dc1b99a5dfba1380800b3cf19e2f7b MD5 | raw file
Possible License(s): AGPL-1.0
  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. * @Extra credits for this file
  13. * Todd - (todd@phparena.net) - (http://www.phparena.net)
  14. *
  15. */
  16. class pafiledb_search extends pafiledb_public
  17. {
  18. function main($action)
  19. {
  20. global $template, $lang, $config, $pafiledb_config, $db, $images, $user;
  21. if(!$this->auth_global['auth_search'])
  22. {
  23. if (!$user->data['session_logged_in'])
  24. {
  25. redirect(append_sid(CMS_PAGE_LOGIN . '?redirect=dload.' . PHP_EXT . '&action=stats', true));
  26. }
  27. $message = sprintf($lang['Sorry_auth_search'], $this->auth_global['auth_search_type']);
  28. message_die(GENERAL_MESSAGE, $message);
  29. }
  30. include(IP_ROOT_PATH . 'includes/functions_search.' . PHP_EXT);
  31. $search_keywords = request_var('search_keywords', '', true);
  32. $search_keywords = htmlspecialchars_decode($search_keywords, ENT_COMPAT);
  33. $search_author = request_var('search_author', '', true);
  34. $search_author = htmlspecialchars_decode($search_author, ENT_COMPAT);
  35. $search_id = request_var('search_id', 0);
  36. $search_terms = request_var('search_terms', '');
  37. $search_terms = ($search_terms == 'all') ? 1 : 0;
  38. $cat_id = request_var('cat_id', 0);
  39. $comments_search = request_var('comments_search', '');
  40. $comments_search = ($comments_search == 'YES') ? 1 : 0;
  41. $start = request_var('start', 0);
  42. $start = ($start < 0) ? 0 : $start;
  43. $sort_method = request_var('sort_method', $pafiledb_config['sort_method']);
  44. $sort_method = check_var_value($sort_method, array('file_name', 'file_time', 'file_dls', 'file_rating', 'file_update_time'));
  45. $sort_method = ($sort_method == 'file_rating') ? 'rating' : $sort_method;
  46. $sort_order = request_var('order', $pafiledb_config['sort_order']);
  47. $sort_order = check_var_value($sort_order, array('DESC', 'ASC'));
  48. $limit_sql = ($start == 0) ? $pafiledb_config['settings_file_page'] : $start . ',' . $pafiledb_config['settings_file_page'];
  49. // encoding match for workaround
  50. $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
  51. if (isset($_POST['submit']) || ($search_author != '') || ($search_keywords != '') || $search_id)
  52. {
  53. $store_vars = array('search_results', 'total_match_count', 'split_search', 'sort_method', 'sort_order');
  54. if(($search_author != '') || ($search_keywords != ''))
  55. {
  56. if (($search_author != '') && ($search_keywords == ''))
  57. {
  58. $search_author = str_replace('*', '%', trim($search_author));
  59. $sql = get_users_sql($search_author, true, false, true, false);
  60. $result = $db->sql_query($sql);
  61. $matching_userids = '';
  62. if ($row = $db->sql_fetchrow($result))
  63. {
  64. do
  65. {
  66. $matching_userids .= (($matching_userids != '') ? ', ' : '') . $row['user_id'];
  67. }
  68. while($row = $db->sql_fetchrow($result));
  69. }
  70. else
  71. {
  72. message_die(GENERAL_MESSAGE, $lang['No_search_match']);
  73. }
  74. $sql = "SELECT *
  75. FROM " . PA_FILES_TABLE . "
  76. WHERE user_id IN ($matching_userids)";
  77. $result = $db->sql_query($sql);
  78. $search_ids = array();
  79. while($row = $db->sql_fetchrow($result))
  80. {
  81. if($this->auth[$row['file_catid']]['auth_view'])
  82. {
  83. $search_ids[] = $row['file_id'];
  84. }
  85. }
  86. $db->sql_freeresult($result);
  87. $total_match_count = sizeof($search_ids);
  88. }
  89. elseif ($search_keywords != '')
  90. {
  91. stopwords_synonyms_init();
  92. $split_search = array();
  93. $split_search = (!strstr($multibyte_charset, $lang['ENCODING'])) ? split_words(clean_words('search', stripslashes($search_keywords), $stopwords_array, $synonyms_array), 'search') : explode(' ', $search_keywords);
  94. $word_count = 0;
  95. $current_match_type = 'or';
  96. $word_match = array();
  97. $result_list = array();
  98. for($i = 0; $i < sizeof($split_search); $i++)
  99. {
  100. switch ($split_search[$i])
  101. {
  102. case 'and':
  103. $current_match_type = 'and';
  104. break;
  105. case 'or':
  106. $current_match_type = 'or';
  107. break;
  108. case 'not':
  109. $current_match_type = 'not';
  110. break;
  111. default:
  112. if (!empty($search_terms))
  113. {
  114. $current_match_type = 'and';
  115. }
  116. $match_word = addslashes('%' . str_replace('*', '', $split_search[$i]) . '%');
  117. $sql = "SELECT file_id
  118. FROM " . PA_FILES_TABLE . "
  119. WHERE (file_name LIKE '$match_word'
  120. OR file_creator LIKE '$match_word'
  121. OR file_desc LIKE '$match_word'
  122. OR file_longdesc LIKE '$match_word')";
  123. $result = $db->sql_query($sql);
  124. $row = array();
  125. while($temp_row = $db->sql_fetchrow($result))
  126. {
  127. $row[$temp_row['file_id']] = 1;
  128. if (!$word_count)
  129. {
  130. $result_list[$temp_row['file_id']] = 1;
  131. }
  132. elseif ($current_match_type == 'or')
  133. {
  134. $result_list[$temp_row['file_id']] = 1;
  135. }
  136. elseif ($current_match_type == 'not')
  137. {
  138. $result_list[$temp_row['file_id']] = 0;
  139. }
  140. }
  141. if ($current_match_type == 'and' && $word_count)
  142. {
  143. @reset($result_list);
  144. while(list($file_id, $match_count) = @each($result_list))
  145. {
  146. if (!$row[$file_id])
  147. {
  148. $result_list[$file_id] = 0;
  149. }
  150. }
  151. }
  152. if($comments_search)
  153. {
  154. $sql = "SELECT file_id
  155. FROM " . PA_COMMENTS_TABLE . "
  156. WHERE (comments_title LIKE '$match_word'
  157. OR comments_text LIKE '$match_word')";
  158. $result = $db->sql_query($sql);
  159. $row = array();
  160. while($temp_row = $db->sql_fetchrow($result))
  161. {
  162. $row[$temp_row['file_id']] = 1;
  163. if (!$word_count)
  164. {
  165. $result_list[$temp_row['file_id']] = 1;
  166. }
  167. else if ($current_match_type == 'or')
  168. {
  169. $result_list[$temp_row['file_id']] = 1;
  170. }
  171. else if ($current_match_type == 'not')
  172. {
  173. $result_list[$temp_row['file_id']] = 0;
  174. }
  175. }
  176. if ($current_match_type == 'and' && $word_count)
  177. {
  178. @reset($result_list);
  179. while(list($file_id, $match_count) = @each($result_list))
  180. {
  181. if (!$row[$file_id])
  182. {
  183. $result_list[$file_id] = 0;
  184. }
  185. }
  186. }
  187. }
  188. $word_count++;
  189. $db->sql_freeresult($result);
  190. }
  191. }
  192. @reset($result_list);
  193. $search_ids = array();
  194. while(list($file_id, $matches) = each($result_list))
  195. {
  196. if ($matches)
  197. {
  198. $search_ids[] = $file_id;
  199. }
  200. }
  201. unset($result_list);
  202. $total_match_count = sizeof($search_ids);
  203. }
  204. // Author name search
  205. if ($search_author != '')
  206. {
  207. $search_author = str_replace('*', '%', trim($db->sql_escape($search_author)));
  208. }
  209. if ($total_match_count)
  210. {
  211. $where_sql = ($cat_id) ? 'AND file_catid IN (' . $this->gen_cat_ids($cat_id, '') . ')' : '';
  212. if ($search_author == '')
  213. {
  214. $sql = "SELECT file_id, file_catid
  215. FROM " . PA_FILES_TABLE . "
  216. WHERE file_id IN (" . implode(", ", $search_ids) . ")
  217. $where_sql
  218. GROUP BY file_id";
  219. }
  220. else
  221. {
  222. $from_sql = PA_FILES_TABLE . " f";
  223. if ($search_author != '')
  224. {
  225. $from_sql .= ", " . USERS_TABLE . " u";
  226. $where_sql .= " AND u.user_id = f.user_id AND u.username LIKE '$search_author' ";
  227. }
  228. $where_sql .= ($cat_id) ? 'AND file_catid IN (' . $this->gen_cat_ids($cat_id, '') . ')' : '';
  229. $sql = "SELECT f.file_id, f.file_catid
  230. FROM $from_sql
  231. WHERE f.file_id IN (" . implode(", ", $search_ids) . ")
  232. $where_sql
  233. GROUP BY f.file_id";
  234. }
  235. $result = $db->sql_query($sql);
  236. $search_ids = array();
  237. while($row = $db->sql_fetchrow($result))
  238. {
  239. if($this->auth[$row['file_catid']]['auth_view'])
  240. {
  241. $search_ids[] = $row['file_id'];
  242. }
  243. }
  244. $db->sql_freeresult($result);
  245. $total_match_count = sizeof($search_ids);
  246. }
  247. else
  248. {
  249. message_die(GENERAL_MESSAGE, $lang['No_search_match']);
  250. }
  251. //
  252. // Finish building query (for all combinations)
  253. // and run it ...
  254. //
  255. $expiry_time = $current_time - $config['session_length'];
  256. $sql = "SELECT session_id
  257. FROM " . SESSIONS_TABLE ."
  258. WHERE session_time > $expiry_time";
  259. $db->sql_return_on_error(true);
  260. $result = $db->sql_query($sql);
  261. $db->sql_return_on_error(false);
  262. if ($result)
  263. {
  264. $delete_search_ids = array();
  265. while($row = $db->sql_fetchrow($result))
  266. {
  267. $delete_search_ids[] = "'" . $row['session_id'] . "'";
  268. }
  269. if (sizeof($delete_search_ids))
  270. {
  271. $sql = "DELETE FROM " . SEARCH_TABLE . "
  272. WHERE session_id NOT IN (" . implode(", ", $delete_search_ids) . ")";
  273. $result = $db->sql_query($sql);
  274. }
  275. }
  276. // Store new result data
  277. $search_results = implode(', ', $search_ids);
  278. $store_search_data = array();
  279. for($i = 0; $i < sizeof($store_vars); $i++)
  280. {
  281. $store_search_data[$store_vars[$i]] = ${$store_vars[$i]};
  282. }
  283. $result_array = serialize($store_search_data);
  284. unset($store_search_data);
  285. mt_srand ((double) microtime() * 1000000);
  286. $search_id = mt_rand();
  287. $sql = "UPDATE " . SEARCH_TABLE . "
  288. SET search_id = $search_id, search_array = '" . $db->sql_escape($result_array) . "'
  289. WHERE session_id = '" . $user->data['session_id'] . "'";
  290. $db->sql_return_on_error(true);
  291. $result = $db->sql_query($sql);
  292. $db->sql_return_on_error(false);
  293. if (!$result || !$db->sql_affectedrows())
  294. {
  295. $sql = "INSERT INTO " . SEARCH_TABLE . " (search_id, session_id, search_array)
  296. VALUES($search_id, '" . $user->data['session_id'] . "', '" . $db->sql_escape($result_array) . "')";
  297. $result = $db->sql_query($sql);
  298. }
  299. }
  300. else
  301. {
  302. $search_id = intval($search_id);
  303. if ($search_id)
  304. {
  305. $sql = "SELECT search_array
  306. FROM " . SEARCH_TABLE . "
  307. WHERE search_id = $search_id
  308. AND session_id = '" . $user->data['session_id'] . "'";
  309. $result = $db->sql_query($sql);
  310. if ($row = $db->sql_fetchrow($result))
  311. {
  312. $search_data = unserialize($row['search_array']);
  313. for($i = 0; $i < sizeof($store_vars); $i++)
  314. {
  315. ${$store_vars[$i]} = $search_data[$store_vars[$i]];
  316. }
  317. }
  318. }
  319. }
  320. if ($search_results != '')
  321. {
  322. $sql = "SELECT f1.*, AVG(r.rate_point) AS rating, COUNT(r.votes_file) AS total_votes, u.user_id, u.username, u.user_active, u.user_color, c.cat_id, c.cat_name, COUNT(cm.comments_id) AS total_comments
  323. FROM (" . PA_FILES_TABLE . " AS f1, " . PA_CATEGORY_TABLE . " AS c)
  324. LEFT JOIN " . PA_VOTES_TABLE . " AS r ON f1.file_id = r.votes_file
  325. LEFT JOIN ". USERS_TABLE ." AS u ON f1.user_id = u.user_id
  326. LEFT JOIN " . PA_COMMENTS_TABLE . " AS cm ON f1.file_id = cm.file_id
  327. WHERE f1.file_id IN ($search_results)
  328. AND c.cat_id = f1.file_catid
  329. AND f1.file_approved = '1'
  330. GROUP BY f1.file_id
  331. ORDER BY $sort_method $sort_order
  332. LIMIT $limit_sql";
  333. $result = $db->sql_query($sql);
  334. $searchset = array();
  335. while($row = $db->sql_fetchrow($result))
  336. {
  337. $searchset[] = $row;
  338. }
  339. $db->sql_freeresult($result);
  340. $l_search_matches = ($total_match_count == 1) ? sprintf($lang['Found_search_match'], $total_match_count) : sprintf($lang['Found_search_matches'], $total_match_count);
  341. $template->assign_vars(array(
  342. 'L_SEARCH_MATCHES' => $l_search_matches
  343. )
  344. );
  345. for($i = 0; $i < sizeof($searchset); $i++)
  346. {
  347. $cat_url = append_sid('dload.' . PHP_EXT . '?action=category&amp;cat_id=' . $searchset[$i]['cat_id']);
  348. $file_url = append_sid('dload.' . PHP_EXT . '?action=file&amp;file_id=' . $searchset[$i]['file_id']);
  349. //===================================================
  350. // Format the date for the given file
  351. //===================================================
  352. $date = create_date_ip($config['default_dateformat'], $searchset[$i]['file_time'], $config['board_timezone']);
  353. //===================================================
  354. // Get rating for the file and format it
  355. //===================================================
  356. //$rating = ($searchset[$i]['rating'] != 0) ? round($searchset[$i]['rating'], 2) . ' / 10' : $lang['Not_rated'];
  357. //$rating2 = ($searchset[$i]['rating'] != 0) ? sprintf("%.1f", round(($searchset[$i]['rating']), 2) / 2) : '0.0';
  358. $rating2 = ($searchset[$i]['rating'] != 0) ? sprintf("%.1f", round(($searchset[$i]['rating']), 0) / 2) : '0.0';
  359. //===================================================
  360. // If the file is new then put a new image in front of it
  361. //===================================================
  362. $is_new = false;
  363. if (time() - ($pafiledb_config['settings_newdays'] * 24 * 60 * 60) < $searchset[$i]['file_time'])
  364. {
  365. $is_new = true;
  366. }
  367. $xs_new = ($is_new) ? '-new' : '';
  368. //===================================================
  369. // Get the post icon fot this file
  370. //===================================================
  371. if ($searchset[$i]['file_pin'] != FILE_PINNED)
  372. {
  373. if (($searchset[$i]['file_posticon'] == 'none') || ($searchset[$i]['file_posticon'] == 'none.gif'))
  374. {
  375. $posticon = '<img src="' . IP_ROOT_PATH . FILES_ICONS_DIR . 'default.png" alt="" />';
  376. //$posticon = '&nbsp;';
  377. }
  378. else
  379. {
  380. $posticon = '<img src="' . FILES_ICONS_DIR . $searchset[$i]['file_posticon'] . '" alt="" />';
  381. }
  382. }
  383. else
  384. {
  385. $posticon = '<img src="' . $images['forum_link'] . '" alt="" />';
  386. }
  387. $poster = ($searchset[$i]['user_id'] == ANONYMOUS) ? $lang['Guest'] : colorize_username($searchset[$i]['user_id'], $searchset[$i]['username'], $searchset[$i]['user_color'], $searchset[$i]['user_active']);
  388. $template->assign_block_vars('searchresults', array(
  389. 'CAT_NAME' => $searchset[$i]['cat_name'],
  390. 'FILE_NEW_IMAGE' => $images['pa_file_new'],
  391. 'PIN_IMAGE' => $posticon,
  392. 'L_HOME' => $lang['Home'],
  393. 'CURRENT_TIME' => sprintf($lang['Current_time'], create_date($config['default_dateformat'], time(), $config['board_timezone'])),
  394. 'XS_NEW' => $xs_new,
  395. 'IS_NEW_FILE' => $is_new,
  396. 'FILE_NAME' => $searchset[$i]['file_name'],
  397. 'FILE_DESC' => $searchset[$i]['file_desc'],
  398. 'FILE_SUBMITER' => $poster,
  399. 'DATE' => $date,
  400. 'RATING' => $rating2,
  401. 'DOWNLOADS' => $searchset[$i]['file_dls'],
  402. 'U_FILE' => $file_url,
  403. 'U_CAT' => $cat_url)
  404. );
  405. }
  406. $base_url = append_sid('dload.' . PHP_EXT . '?action=search&amp;search_id=' . $search_id);
  407. $template->assign_vars(array(
  408. 'PAGINATION' => generate_pagination($base_url, $total_match_count, $pafiledb_config['settings_file_page'], $start),
  409. 'PAGE_NUMBER' => sprintf($lang['Page_of'], (floor($start / $pafiledb_config['settings_file_page']) + 1), ceil($total_match_count / $pafiledb_config['settings_file_page'])),
  410. 'DOWNLOAD' => $pafiledb_config['settings_dbname'],
  411. 'L_HOME' => $lang['Home'],
  412. 'U_INDEX_HOME' => append_sid(CMS_PAGE_HOME),
  413. 'U_DOWNLOAD' => append_sid('dload.' . PHP_EXT),
  414. 'L_HOME' => $lang['Home'],
  415. 'CURRENT_TIME' => sprintf($lang['Current_time'], create_date($config['default_dateformat'], time(), $config['board_timezone'])),
  416. 'XS_NEW' => $xs_new,
  417. 'L_INDEX' => sprintf($lang['Forum_Index'], $config['sitename']),
  418. 'L_RATE' => $lang['DlRating'],
  419. 'L_DOWNLOADS' => $lang['Dls'],
  420. 'L_DATE' => $lang['Date'],
  421. 'L_NAME' => $lang['Name'],
  422. 'L_FILE' => $lang['File'],
  423. 'L_SUBMITER' => $lang['Submiter'],
  424. 'L_CATEGORY' => $lang['Category'],
  425. 'L_NEW_FILE' => $lang['New_file']
  426. )
  427. );
  428. $this->display($lang['Download'], 'pa_search_result.tpl');
  429. }
  430. else
  431. {
  432. message_die(GENERAL_MESSAGE, $lang['No_search_match']);
  433. }
  434. }
  435. if (!isset($_POST['submit']) || (($search_author == '') && ($search_keywords == '') && !$search_id) )
  436. {
  437. $dropmenu = $this->jumpmenu_option();
  438. $template->assign_vars(array(
  439. 'S_SEARCH_ACTION' => append_sid('dload.php'),
  440. 'S_CAT_MENU' => $dropmenu,
  441. 'DOWNLOAD' => $pafiledb_config['settings_dbname'],
  442. 'U_INDEX_HOME' => append_sid(CMS_PAGE_HOME),
  443. 'U_DOWNLOAD' => append_sid('dload.' . PHP_EXT),
  444. 'L_HOME' => $lang['Home'],
  445. 'CURRENT_TIME' => sprintf($lang['Current_time'], create_date($config['default_dateformat'], time(), $config['board_timezone'])),
  446. 'XS_NEW' => $xs_new,
  447. 'L_YES' => $lang['Yes'],
  448. 'L_NO' => $lang['No'],
  449. 'L_SEARCH_OPTIONS' => $lang['Search_options'],
  450. 'L_SEARCH_KEYWORDS' => $lang['Search_keywords'],
  451. 'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'],
  452. 'L_SEARCH_AUTHOR' => $lang['Search_author'],
  453. 'L_SEARCH_AUTHOR_EXPLAIN' => $lang['Search_author_explain'],
  454. 'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
  455. 'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'],
  456. 'L_INCLUDE_COMMENTS' => $lang['Include_comments'],
  457. 'L_SORT_BY' => $lang['Select_sort_method'],
  458. 'L_SORT_DIR' => $lang['Order'],
  459. 'L_SORT_ASCENDING' => $lang['Sort_Ascending'],
  460. 'L_SORT_DESCENDING' => $lang['Sort_Descending'],
  461. 'L_INDEX' => sprintf($lang['Forum_Index'], $config['sitename']),
  462. 'L_RATING' => $lang['DlRating'],
  463. 'L_DOWNLOADS' => $lang['Dls'],
  464. 'L_DATE' => $lang['Date'],
  465. 'L_NAME' => $lang['Name'],
  466. 'L_UPDATE_TIME' => $lang['Update_time'],
  467. 'L_SEARCH' => $lang['Search'],
  468. 'L_SEARCH_FOR' => $lang['Search_for'],
  469. 'L_ALL' => $lang['All'],
  470. 'L_CHOOSE_CAT' => $lang['Choose_cat']
  471. )
  472. );
  473. $this->display($lang['Download'], 'pa_search_body.tpl');
  474. }
  475. }
  476. }
  477. ?>