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

/Quản lý website đọc truyện online PHP/forum/search.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 138 lines | 99 code | 8 blank | 31 comment | 21 complexity | f7743d9d99eb17f95299af5790141883 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JohnCMS
  4. * @link http://johncms.com
  5. * @copyright Copyright (C) 2008-2011 JohnCMS Community
  6. * @license LICENSE.txt (see attached file)
  7. * @version VERSION.txt (see attached file)
  8. * @author http://johncms.com/about
  9. */
  10. define('_IN_JOHNCMS', 1);
  11. $headmod = 'forumsearch';
  12. require_once('../incfiles/core.php');
  13. if(!$website)
  14. require(''.$_SERVER['DOCUMENT_ROOT'].'/incfiles/websiteb.php');
  15. $lng_forum = core::load_lng('forum');
  16. $textl = $lng_forum['search_forum'];
  17. require('../incfiles/head.php');
  18. /*
  19. -----------------------------------------------------------------
  20. Функция подсветки результатов запроса
  21. -----------------------------------------------------------------
  22. */
  23. function ReplaceKeywords($search, $text) {
  24. $search = str_replace('*', '', $search);
  25. return mb_strlen($search) < 3 ? $text : preg_replace('|('.preg_quote($search, '/').')|siu','<span style="background-color: #FFFF33">$1</span>',$text);
  26. }
  27. /*
  28. -----------------------------------------------------------------
  29. Принимаем данные, выводим форму поиска
  30. -----------------------------------------------------------------
  31. */
  32. $search_post = isset($_POST['search']) ? trim($_POST['search']) : false;
  33. $search_get = isset($_GET['search']) ? rawurldecode(trim($_GET['search'])) : false;
  34. $search = $search_post ? $search_post : $search_get;
  35. //$search = preg_replace("/[^\w\x7F-\xFF\s]/", " ", $search);
  36. $search_t = isset($_REQUEST['t']);
  37. echo '<div class="phdr"><a href="index.php"><b>' . $lng['forum'] . '</b></a> | ' . $lng['search'] . '</div>' .
  38. '<div class="gmenu"><form action="search.php" method="post"><p>' .
  39. '<input type="text" value="' . ($search ? functions::checkout($search) : '') . '" name="search" />' .
  40. '<input type="submit" value="' . $lng['search'] . '" name="submit" /><br />' .
  41. '<input name="t" type="checkbox" value="1" ' . ($search_t ? 'checked="checked"' : '') . ' />&nbsp;' . $lng_forum['search_topic_name'] .
  42. '</p></form></div>';
  43. /*
  44. -----------------------------------------------------------------
  45. Проверям на ошибки
  46. -----------------------------------------------------------------
  47. */
  48. $error = false;
  49. if ($search && (mb_strlen($search) < 2 || mb_strlen($search) > 64))
  50. $error = $lng['error_search_length'];
  51. if ($search && !$error) {
  52. /*
  53. -----------------------------------------------------------------
  54. Выводим результаты запроса
  55. -----------------------------------------------------------------
  56. */
  57. $array = explode(' ', $search);
  58. $count = count($array);
  59. $query = mysql_real_escape_string($search);
  60. $total = mysql_result(mysql_query("
  61. SELECT COUNT(*) FROM `forum`
  62. WHERE MATCH (`text`) AGAINST ('$query' IN BOOLEAN MODE)
  63. AND `type` = '" . ($search_t ? 't' : 'm') . "'" . ($rights >= 7 ? "" : " AND `close` != '1'
  64. AND `website` = '$website'")), 0);
  65. echo '<div class="phdr">' . $lng['search_results'] . '</div>';
  66. if ($total > $kmess)
  67. echo '<div class="topmenu">' . functions::display_pagination('search.php?' . ($search_t ? 't=1&amp;' : '') . 'search=' . urlencode($search) . '&amp;', $start, $total, $kmess) . '</div>';
  68. if ($total) {
  69. $req = mysql_query("
  70. SELECT *, MATCH (`text`) AGAINST ('$query' IN BOOLEAN MODE) as `rel`
  71. FROM `forum`
  72. WHERE MATCH (`text`) AGAINST ('$query' IN BOOLEAN MODE)
  73. AND `type` = '" . ($search_t ? 't' : 'm') . "' AND `website` = '$website'
  74. ORDER BY `rel` DESC
  75. LIMIT $start, $kmess
  76. ");
  77. $i = 0;
  78. while (($res = mysql_fetch_assoc($req)) !== false) {
  79. echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
  80. if (!$search_t) {
  81. // Поиск только в тексте
  82. $req_t = mysql_query("SELECT `id`,`text` FROM `forum` WHERE `id` = '" . $res['refid'] . "' AND `website` = '$website'");
  83. $res_t = mysql_fetch_assoc($req_t);
  84. echo '<b>' . $res_t['text'] . '</b><br />';
  85. } else {
  86. // Поиск в названиях тем
  87. $req_p = mysql_query("SELECT `text` FROM `forum` WHERE `refid` = '" . $res['id'] . "' AND `website` = '$website' ORDER BY `id` ASC LIMIT 1");
  88. $res_p = mysql_fetch_assoc($req_p);
  89. foreach($array as $val){
  90. $res['text'] = ReplaceKeywords($val, $res['text']);
  91. }
  92. echo '<b>' . $res['text'] . '</b><br />';
  93. }
  94. echo '<a href="../users/profile.php?user=' . $res['user_id'] . '">' . $res['from'] . '</a> ';
  95. echo ' <span class="gray">(' . functions::display_date($res['time']) . ')</span><br/>';
  96. $text = $search_t ? $res_p['text'] : $res['text'];
  97. foreach ($array as $srch) if (($pos = mb_strpos(strtolower($res['text']), strtolower(str_replace('*', '', $srch)))) !== false) break;
  98. if(!isset($pos) || $pos < 100) $pos = 100;
  99. $text = preg_replace('#\[c\](.*?)\[/c\]#si', '<div class="quote">\1</div>', $text);
  100. $text = functions::checkout(mb_substr($text, ($pos - 100), 400), 1);
  101. if (!$search_t) {
  102. foreach($array as $val){
  103. $text = ReplaceKeywords($val, $text);
  104. }
  105. }
  106. echo $text;
  107. if (mb_strlen($res['text']) > 500)
  108. echo '...<a href="index.php?act=post&amp;id=' . $res['id'] . '">' . $lng_forum['read_all'] . ' &gt;&gt;</a>';
  109. echo '<br /><a href="index.php?id=' . ($search_t ? $res['id'] : $res_t['id']) . '">' . $lng_forum['to_topic'] . '</a>' . ($search_t ? '' : ' | <a href="index.php?act=post&amp;id=' . $res['id'] . '">' . $lng_forum['to_post'] . '</a>');
  110. echo '</div>';
  111. ++$i;
  112. }
  113. } else {
  114. echo '<div class="rmenu"><p>' . $lng['search_results_empty'] . '</p></div>';
  115. }
  116. echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
  117. if ($total > $kmess) {
  118. echo '<div class="topmenu">' . functions::display_pagination('search.php?' . ($search_t ? 't=1&amp;' : '') . 'search=' . urlencode($search) . '&amp;', $start, $total, $kmess) . '</div>' .
  119. '<p><form action="search.php?' . ($search_t ? 't=1&amp;' : '') . 'search=' . urlencode($search) . '" method="post">' .
  120. '<input type="text" name="page" size="2"/>' .
  121. '<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/>' .
  122. '</form></p>';
  123. }
  124. } else {
  125. if ($error) echo functions::display_error($error);
  126. echo '<div class="phdr"><small>' . $lng['search_help'] . '</small></div>';
  127. }
  128. echo '<p>' . ($search ? '<a href="search.php">' . $lng['search_new'] . '</a><br />' : '') . '<a href="index.php">' . $lng['forum'] . '</a></p>';
  129. require('../incfiles/end.php');
  130. ?>