PageRenderTime 75ms CodeModel.GetById 43ms RepoModel.GetById 1ms app.codeStats 0ms

/campsite/src/include/phorum/search.php

https://github.com/joechrysler/Campsite
PHP | 224 lines | 145 code | 50 blank | 29 comment | 26 complexity | 1826d38f0b1fd19e9d59349e741d517d MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2006 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // along with this program. //
  17. ////////////////////////////////////////////////////////////////////////////////
  18. define('phorum_page','search');
  19. include_once("./common.php");
  20. if(!phorum_check_read_common()) {
  21. return;
  22. }
  23. include_once("./include/format_functions.php");
  24. // set all our URL's
  25. phorum_build_common_urls();
  26. $PHORUM["DATA"]["SEARCH"]["noresults"] = false;
  27. $PHORUM["DATA"]["SEARCH"]["showresults"] = false;
  28. $PHORUM["DATA"]["SEARCH"]["safe_search"] = "";
  29. function phorum_search_check_valid_vars() {
  30. $PHORUM=$GLOBALS['PHORUM'];
  31. $retval=true;
  32. // these are valid values for some args
  33. $valid_match_types=array("ALL","ANY","PHRASE","AUTHOR");
  34. $valid_match_forum=array("THISONE","ALL");
  35. if(!in_array($PHORUM["args"]["match_type"],$valid_match_types)) {
  36. $retval=false;
  37. } elseif(!in_array($PHORUM["args"]["match_forum"],$valid_match_forum)) {
  38. $retval=false;
  39. } elseif(!is_numeric($PHORUM["args"]["match_dates"])) {
  40. $retval=false;
  41. }
  42. return $retval;
  43. }
  44. if(!empty($_GET["search"]) && !isset($PHORUM["args"]["page"])){
  45. $search_url = @phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($_GET["search"]), "page=1", "match_type=" . urlencode($_GET['match_type']), "match_dates=" . urlencode($_GET['match_dates']), "match_forum=" . urlencode($_GET['match_forum']));
  46. $PHORUM["DATA"]["MESSAGE"]=$PHORUM["DATA"]["LANG"]["SearchRunning"];
  47. $PHORUM["DATA"]["BACKMSG"]=$PHORUM["DATA"]["LANG"]["BackToSearch"];
  48. $PHORUM["DATA"]["URL"]["REDIRECT"]=$search_url;
  49. $PHORUM["DATA"]["REDIRECT_TIME"]=1;
  50. include phorum_get_template("header");
  51. phorum_hook("after_header");
  52. include phorum_get_template("message");
  53. phorum_hook("before_footer");
  54. include phorum_get_template("footer");
  55. return;
  56. }
  57. if(isset($PHORUM["args"]["search"])){
  58. $phorum_search = $PHORUM["args"]["search"];
  59. }
  60. if(!isset($PHORUM["args"]["match_type"])) $PHORUM["args"]["match_type"]="ALL";
  61. if(!isset($PHORUM["args"]["match_dates"])) $PHORUM["args"]["match_dates"]="30";
  62. if(!isset($PHORUM["args"]["match_forum"])) $PHORUM["args"]["match_forum"]="ALL";
  63. if(!phorum_search_check_valid_vars()) {
  64. $redir_url=phorum_get_url(PHORUM_LIST_URL);
  65. phorum_redirect_by_url($redir_url);
  66. }
  67. // setup some stuff based on the url passed
  68. if(!empty($phorum_search)){
  69. $PHORUM["DATA"]["SEARCH"]["safe_search"] = htmlspecialchars($phorum_search);
  70. include_once("./include/format_functions.php");
  71. $offset = (empty($PHORUM["args"]["page"])) ? 0 : $PHORUM["args"]["page"]-1;
  72. if(empty($PHORUM["list_length"])) $PHORUM["list_length"]=30;
  73. $start = ($offset * $PHORUM["list_length"]);
  74. settype($PHORUM["args"]["match_dates"], "int");
  75. $arr = phorum_db_search($phorum_search, $offset, $PHORUM["list_length"], $PHORUM["args"]["match_type"], $PHORUM["args"]["match_dates"], $PHORUM["args"]["match_forum"]);
  76. if(count($arr["rows"])){
  77. $match_number = $start + 1;
  78. $forums = phorum_db_get_forums();
  79. // For announcements, we will put the current forum_id in the record.
  80. // Else the message cannot be read (Phorum will redirect the user
  81. // back to the index page if the forum id is zero). If the user came
  82. // from the index page, no forum id will be set. In that case we
  83. // use the first available forum id.
  84. $announcement_forum_id = 0;
  85. if ($PHORUM["forum_id"]) {
  86. $announcement_forum_id = $PHORUM["forum_id"];
  87. } elseif (count($forums)) {
  88. list ($f_id, $_data) = each($forums);
  89. $announcement_forum_id = $f_id;
  90. }
  91. foreach($arr["rows"] as $key => $row){
  92. $arr["rows"][$key]["number"] = $match_number;
  93. // Fake a forum_id for folders.
  94. if ($row["forum_id"] == 0) {
  95. $row["forum_id"] = $announcement_forum_id;
  96. }
  97. $arr["rows"][$key]["url"] = phorum_get_url(PHORUM_FOREIGN_READ_URL, $row["forum_id"], $row["thread"], $row["message_id"]);
  98. // strip HTML & BB Code
  99. $body = phorum_strip_body($arr["rows"][$key]["body"]);
  100. $arr["rows"][$key]["short_body"] = substr($body, 0, 200);
  101. $arr["rows"][$key]["datestamp"] = phorum_date($PHORUM["short_date"], $row["datestamp"]);
  102. $arr["rows"][$key]["author"] = htmlspecialchars($row["author"]);
  103. $arr["rows"][$key]["short_body"] = htmlspecialchars($arr["rows"][$key]["short_body"]);
  104. $forum_ids[$row["forum_id"]] = $row["forum_id"];
  105. $match_number++;
  106. }
  107. foreach($arr["rows"] as $key => $row){
  108. // Skip announcements "forum".
  109. if ($row["forum_id"] == 0) continue;
  110. $arr["rows"][$key]["forum_url"] = phorum_get_url(PHORUM_LIST_URL, $row["forum_id"]);
  111. $arr["rows"][$key]["forum_name"] = $forums[$row["forum_id"]]["name"];
  112. }
  113. $PHORUM["DATA"]["RANGE_START"] = $start + 1;
  114. $PHORUM["DATA"]["RANGE_END"] = $start + count($arr["rows"]);
  115. $PHORUM["DATA"]["TOTAL"] = $arr["count"];
  116. $PHORUM["DATA"]["SEARCH"]["showresults"] = true;
  117. // figure out paging
  118. $pages = ceil($arr["count"] / $PHORUM["list_length"]);
  119. $page = $offset + 1;
  120. if ($pages <= 5){
  121. $page_start = 1;
  122. }elseif ($pages - $page < 2){
  123. $page_start = $pages-4;
  124. }elseif ($pages > 5 && $page > 3){
  125. $page_start = $page-2;
  126. }else{
  127. $page_start = 1;
  128. }
  129. $pageno = 1;
  130. for($x = 0;$x < 5 && $x < $pages;$x++){
  131. $pageno = $x + $page_start;
  132. $PHORUM["DATA"]["PAGES"][] = array("pageno" => $pageno,
  133. "url" => phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "page=$pageno", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum={$PHORUM['args']['match_forum']}")
  134. );
  135. }
  136. $PHORUM["DATA"]["CURRENTPAGE"] = $page;
  137. $PHORUM["DATA"]["TOTALPAGES"] = $pages;
  138. if ($page_start > 1){
  139. $PHORUM["DATA"]["URL"]["FIRSTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "page=1", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum={$PHORUM['args']['match_forum']}");
  140. }
  141. if ($pageno < $pages){
  142. $PHORUM["DATA"]["URL"]["LASTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "page=$pages", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum={$PHORUM['args']['match_forum']}");
  143. }
  144. if ($pages > $page){
  145. $nextpage = $page + 1;
  146. $PHORUM["DATA"]["URL"]["NEXTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "page=$nextpage", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum={$PHORUM['args']['match_forum']}");
  147. }
  148. if ($page > 1){
  149. $prevpage = $page-1;
  150. $PHORUM["DATA"]["URL"]["PREVPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "page=$prevpage", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum={$PHORUM['args']['match_forum']}");
  151. }
  152. $arr["rows"] = phorum_hook("search", $arr["rows"]);
  153. $arr["rows"] = phorum_format_messages($arr["rows"]);
  154. $PHORUM["DATA"]["MATCHES"] = $arr["rows"];
  155. }else{
  156. $PHORUM["DATA"]["SEARCH"]["noresults"] = true;
  157. $PHORUM["DATA"]["FOCUS_TO_ID"] = 'phorum_search_message';
  158. }
  159. } else {
  160. // Set cursor focus to message search entry.
  161. $PHORUM["DATA"]["FOCUS_TO_ID"] = 'phorum_search_message';
  162. }
  163. $PHORUM["DATA"]["URL"]["ACTION"] = phorum_get_url(PHORUM_SEARCH_ACTION_URL);
  164. $PHORUM["DATA"]["SEARCH"]["forum_id"] = $PHORUM["forum_id"];
  165. $PHORUM["DATA"]["SEARCH"]["match_type"] = $PHORUM["args"]["match_type"];
  166. $PHORUM["DATA"]["SEARCH"]["match_dates"] = $PHORUM["args"]["match_dates"];
  167. $PHORUM["DATA"]["SEARCH"]["match_forum"] = $PHORUM["args"]["match_forum"];
  168. $PHORUM["DATA"]["SEARCH"]["allow_match_one_forum"] = $PHORUM["forum_id"];
  169. include phorum_get_template("header");
  170. phorum_hook("after_header");
  171. include phorum_get_template("search");
  172. phorum_hook("before_footer");
  173. include phorum_get_template("footer");
  174. ?>