PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/search.php

https://bitbucket.org/webop/webop-forum
PHP | 425 lines | 264 code | 71 blank | 90 comment | 57 complexity | b0ab55efbc0cbb56b98e6e3ee90f5faa MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2010 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. ////////////////////////////////////////////////////////////////////////////////
  19. define('phorum_page','search');
  20. include_once("./common.php");
  21. include_once("./include/forum_functions.php");
  22. if(!phorum_check_read_common()) {
  23. return;
  24. }
  25. include_once("./include/format_functions.php");
  26. // set all our URL's
  27. phorum_build_common_urls();
  28. // fill the breadcrumbs-info
  29. $PHORUM['DATA']['BREADCRUMBS'][]=array(
  30. 'URL'=>$PHORUM['DATA']['URL']['SEARCH'],
  31. 'TEXT'=>$PHORUM['DATA']['LANG']['Search'],
  32. 'TYPE'=>'search'
  33. );
  34. // A pointer for the portable code that the search page is used.
  35. $PHORUM["DATA"]["POST_VARS"] .=
  36. '<input type="hidden" name="phorum_page" value="search" />';
  37. $PHORUM["DATA"]["SEARCH"]["noresults"] = false;
  38. $PHORUM["DATA"]["SEARCH"]["showresults"] = false;
  39. $PHORUM["DATA"]["SEARCH"]["safe_search"] = "";
  40. $PHORUM["DATA"]["SEARCH"]["safe_author"] = "";
  41. function phorum_search_check_valid_vars() {
  42. $PHORUM=$GLOBALS['PHORUM'];
  43. $retval=true;
  44. // Check the match_type.
  45. $valid_match_types=array("ALL","ANY","PHRASE","USER_ID");
  46. if(!in_array($PHORUM["args"]["match_type"],$valid_match_types)) {
  47. $retval=false;
  48. }
  49. // Check the match_dates.
  50. elseif(!is_numeric($PHORUM["args"]["match_dates"])) {
  51. $retval=false;
  52. }
  53. return $retval;
  54. }
  55. if(!empty($_GET["search"]) || !empty($_GET["author"])) {
  56. $match_forum = "ALL";
  57. if(!empty($_GET["match_forum"])){
  58. if(is_array($_GET["match_forum"])){
  59. $match_forum = array();
  60. foreach($_GET["match_forum"] as $forum_id){
  61. if(is_numeric($forum_id)){
  62. $match_forum[] = $forum_id;
  63. } elseif($forum_id=="ALL") {
  64. $match_forum="ALL";
  65. break;
  66. }
  67. }
  68. if(is_array($match_forum)){
  69. $match_forum = implode(",", $match_forum);
  70. }
  71. } else {
  72. if(is_numeric($_GET["match_forum"])){
  73. $match_forum = $_GET["match_forum"];
  74. } elseif($_GET["match_forum"]=="ALL") {
  75. $match_forum="ALL";
  76. }
  77. }
  78. }
  79. $url_parameters = array(
  80. PHORUM_SEARCH_URL,
  81. "search=" .
  82. (isset($_GET['search']) ? urlencode($_GET['search']):''),
  83. "author=" .
  84. (isset($_GET['author']) ? urlencode($_GET['author']):''),
  85. "page=1",
  86. "match_type=" .
  87. (isset($_GET['match_type']) ? urlencode($_GET['match_type']):''),
  88. "match_dates=" .
  89. (isset($_GET['match_dates']) ? urlencode($_GET['match_dates']):''),
  90. "match_forum=" . urlencode($match_forum),
  91. "match_threads=" .
  92. (isset($_GET['match_threads']) ? urlencode($_GET['match_threads']):'')
  93. );
  94. /**
  95. * [hook]
  96. * search_redirect
  97. *
  98. * [description]
  99. * Phorum does not jump to the search results page directly after
  100. * posting the search form. Instead, it will first do a redirect
  101. * to a secondary URL. This system is used, so Phorum can show an
  102. * intermediate "Please wait while searching" page before doing the
  103. * redirect. This is useful in case searching is taking a while, in
  104. * which case users might otherwise repeatedly start hitting the
  105. * search button when results don't show up immediately.<br>
  106. * <br>
  107. * This hook can be used to modify the parameters that are used
  108. * for building the redirect URL. This can be useful in case a
  109. * search page is implemented that uses more fields than the standard
  110. * search page.
  111. *
  112. * [category]
  113. * Message search
  114. *
  115. * [when]
  116. * Right before the primary search redirect (for showing the
  117. * "Please wait while searching" intermediate page) is done.
  118. *
  119. * [input]
  120. * An array of phorum_get_url() parameters that will be used for
  121. * building the redirect URL.
  122. *
  123. * [output]
  124. * The possibly updated array of parameters.
  125. */
  126. if (isset($PHORUM["hooks"]["search_redirect"])) {
  127. $url_parameters = phorum_hook("search_redirect", $url_parameters);
  128. }
  129. $search_url = call_user_func_array('phorum_get_url', $url_parameters);
  130. if (!empty($PHORUM["skip_intermediate_search_page"])) {
  131. phorum_redirect_by_url($search_url);
  132. exit(0);
  133. } else {
  134. $PHORUM["DATA"]["OKMSG"]=$PHORUM["DATA"]["LANG"]["SearchRunning"];
  135. $PHORUM["DATA"]["BACKMSG"]=$PHORUM["DATA"]["LANG"]["BackToSearch"];
  136. $PHORUM["DATA"]["URL"]["REDIRECT"]=$search_url;
  137. $PHORUM["DATA"]["REDIRECT_TIME"]=1;
  138. phorum_output("message");
  139. return;
  140. }
  141. }
  142. if(isset($PHORUM["args"]["search"])){
  143. $phorum_search = $PHORUM["args"]["search"];
  144. } else {
  145. $phorum_search = "";
  146. }
  147. if(isset($PHORUM["args"]["author"])){
  148. $phorum_author = $PHORUM["args"]["author"];
  149. } else {
  150. $phorum_author = "";
  151. }
  152. if(!isset($PHORUM["args"]["match_type"])) $PHORUM["args"]["match_type"]="ALL";
  153. if(!isset($PHORUM["args"]["match_dates"])) $PHORUM["args"]["match_dates"]="30";
  154. if(!isset($PHORUM["args"]["match_forum"])) $PHORUM["args"]["match_forum"]="ALL";
  155. if(!isset($PHORUM["args"]["match_threads"])) $PHORUM["args"]["match_threads"]=FALSE;
  156. settype($PHORUM["args"]["match_threads"], "bool");
  157. if(!phorum_search_check_valid_vars()) {
  158. $redir_url=phorum_get_url(PHORUM_LIST_URL);
  159. phorum_redirect_by_url($redir_url);
  160. }
  161. // Check what forums the current user can read.
  162. $allowed_forums = phorum_api_user_check_access(
  163. PHORUM_USER_ALLOW_READ, PHORUM_ACCESS_LIST
  164. );
  165. // setup some stuff based on the url passed
  166. if(!empty($phorum_search) || !empty($phorum_author)){
  167. $PHORUM["DATA"]["SEARCH"]["safe_search"] = htmlspecialchars($phorum_search, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  168. $PHORUM["DATA"]["SEARCH"]["safe_author"] = htmlspecialchars($phorum_author, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  169. if(isset($PHORUM["args"]["page"])){
  170. $PHORUM["args"]["page"] = (int)$PHORUM["args"]["page"];
  171. } else {
  172. $PHORUM["args"]["page"] = 1;
  173. }
  174. $offset = (empty($PHORUM["args"]["page"])) ? 0 : $PHORUM["args"]["page"]-1;
  175. if($offset < 0)
  176. $offset = 0;
  177. if(empty($PHORUM["list_length"])) $PHORUM["list_length"]=30;
  178. $start = ($offset * $PHORUM["list_length"]);
  179. settype($PHORUM["args"]["match_dates"], "int");
  180. // setup the needed data for an alternate search backend
  181. // needs to get fed by posted messages
  182. $search_request_data = array(
  183. 'search' => $phorum_search,
  184. 'author' => $phorum_author,
  185. 'offset' => $start,
  186. 'length' => $PHORUM["list_length"],
  187. 'match_type' => $PHORUM["args"]["match_type"],
  188. 'match_dates' => $PHORUM["args"]["match_dates"],
  189. 'match_forum' => $PHORUM["args"]["match_forum"],
  190. 'match_threads' => $PHORUM["args"]["match_threads"],
  191. 'results' => array(),
  192. 'raw_body' => 0,
  193. 'totals' => 0,
  194. 'continue' => 1
  195. );
  196. if (isset($PHORUM["hooks"]["search_action"]))
  197. $search_request_data = phorum_hook('search_action',$search_request_data);
  198. // only continue if our hook was either not run or didn't return a stop request
  199. if($search_request_data['continue']) {
  200. $arr = phorum_db_search($phorum_search, $phorum_author, $PHORUM["args"]["match_threads"], $offset, $PHORUM["list_length"], $PHORUM["args"]["match_type"], $PHORUM["args"]["match_dates"], $PHORUM["args"]["match_forum"]);
  201. $raw_body = 0;
  202. } else {
  203. $arr['rows'] = $search_request_data['results'];
  204. $arr['count']= $search_request_data['totals'];
  205. $raw_body = $search_request_data['raw_body'];
  206. }
  207. if(count($arr["rows"])){
  208. $match_number = $start + 1;
  209. $forums = phorum_db_get_forums(0, NULL, $PHORUM["vroot"]);
  210. if (!$raw_body)
  211. $arr["rows"] = phorum_format_messages($arr["rows"]);
  212. foreach($arr["rows"] as $key => $row){
  213. $arr["rows"][$key]["number"] = $match_number;
  214. $arr["rows"][$key]["URL"]["READ"] = phorum_get_url(PHORUM_FOREIGN_READ_URL, $row["forum_id"], $row["thread"], $row["message_id"]);
  215. // strip HTML & BB Code
  216. if(!$raw_body) {
  217. $body = phorum_strip_body($arr["rows"][$key]["body"]);
  218. $arr["rows"][$key]["short_body"] = mb_substr($body, 0, 400, $PHORUM["DATA"]["HCHARSET"]);
  219. }
  220. $arr["rows"][$key]["raw_datestamp"] = $row["datestamp"];
  221. $arr["rows"][$key]["datestamp"] = phorum_relative_date($row["datestamp"]);
  222. $forum_ids[$row["forum_id"]] = $row["forum_id"];
  223. $match_number++;
  224. }
  225. foreach($arr["rows"] as $key => $row){
  226. $arr["rows"][$key]["URL"]["LIST"] = phorum_get_url(PHORUM_LIST_URL, $row["forum_id"]);
  227. $arr["rows"][$key]["forum_name"] = $forums[$row["forum_id"]]["name"];
  228. }
  229. $PHORUM["DATA"]["RANGE_START"] = $start + 1;
  230. $PHORUM["DATA"]["RANGE_END"] = $start + count($arr["rows"]);
  231. $PHORUM["DATA"]["TOTAL"] = $arr["count"];
  232. $PHORUM["DATA"]["SEARCH"]["showresults"] = true;
  233. // figure out paging
  234. $pages = ceil($arr["count"] / $PHORUM["list_length"]);
  235. $page = $offset + 1;
  236. $pages_shown = (isset($PHORUM["TMP"]["search_pages_shown"])) ? $PHORUM["TMP"]["search_pages_shown"] : 5;
  237. // first $pages_shown pages
  238. if($page - floor($pages_shown/2) <= 0 || $page < $pages_shown){
  239. $page_start=1;
  240. // last $pages_shown pages
  241. } elseif($page > $pages - floor($pages_shown/2)) {
  242. $page_start = $pages - $pages_shown + 1;
  243. // all others
  244. } else {
  245. $page_start = $page - floor($pages_shown/2);
  246. }
  247. $pageno = 1;
  248. for($x = 0;$x < $pages_shown && $x < $pages;$x++){
  249. $pageno = $x + $page_start;
  250. $PHORUM["DATA"]["PAGES"][] = array("pageno" => $pageno,
  251. "url" => phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=$pageno", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]))
  252. );
  253. }
  254. $PHORUM["DATA"]["CURRENTPAGE"] = $page;
  255. $PHORUM["DATA"]["TOTALPAGES"] = $pages;
  256. $PHORUM["DATA"]["URL"]["PAGING_TEMPLATE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=%page_num%", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]));
  257. if ($page_start > 1){
  258. $PHORUM["DATA"]["URL"]["FIRSTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=1", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]));
  259. }
  260. if ($pageno < $pages){
  261. $PHORUM["DATA"]["URL"]["LASTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=$pages", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]));
  262. }
  263. if ($pages > $page){
  264. $nextpage = $page + 1;
  265. $PHORUM["DATA"]["URL"]["NEXTPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=$nextpage", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]));
  266. }
  267. if ($page > 1){
  268. $prevpage = $page-1;
  269. $PHORUM["DATA"]["URL"]["PREVPAGE"] = phorum_get_url(PHORUM_SEARCH_URL, "search=" . urlencode($phorum_search), "author=" . urlencode($phorum_author), "page=$prevpage", "match_type={$PHORUM['args']['match_type']}", "match_dates={$PHORUM['args']['match_dates']}", "match_forum=".urlencode($PHORUM['args']['match_forum']), "match_threads=".urlencode($PHORUM["args"]["match_threads"]));
  270. }
  271. if (isset($PHORUM["hooks"]["search"]))
  272. $arr["rows"] = phorum_hook("search", $arr["rows"]);
  273. $PHORUM["DATA"]["MATCHES"] = $arr["rows"];
  274. }else{
  275. $PHORUM["DATA"]["SEARCH"]["noresults"] = true;
  276. $PHORUM["DATA"]["FOCUS_TO_ID"] = 'phorum_search_message';
  277. }
  278. } else {
  279. // Set cursor focus to message search entry.
  280. $PHORUM["DATA"]["FOCUS_TO_ID"] = 'phorum_search_message';
  281. if (isset($PHORUM["hooks"]["search_start"]))
  282. $PHORUM['args'] = phorum_hook('search_start',$PHORUM['args']);
  283. }
  284. $PHORUM["DATA"]["URL"]["ACTION"] = phorum_get_url(PHORUM_SEARCH_ACTION_URL);
  285. $PHORUM["DATA"]["SEARCH"]["forum_id"] = $PHORUM["forum_id"];
  286. $PHORUM["DATA"]["SEARCH"]["match_type"] = $PHORUM["args"]["match_type"];
  287. $PHORUM["DATA"]["SEARCH"]["match_dates"] = $PHORUM["args"]["match_dates"];
  288. $PHORUM["DATA"]["SEARCH"]["match_forum"] = $PHORUM["args"]["match_forum"];
  289. $PHORUM["DATA"]["SEARCH"]["match_threads"] = (int)$PHORUM["args"]["match_threads"];
  290. $PHORUM["DATA"]["SEARCH"]["forum_list"] = phorum_build_forum_list();
  291. if(isset($PHORUM["args"]["match_forum"])){
  292. $match_forum = is_array($PHORUM['args']['match_forum'])
  293. ? $PHORUM['args']['match_forum']
  294. : explode(",", $PHORUM["args"]["match_forum"]);
  295. foreach($PHORUM["DATA"]["SEARCH"]["forum_list"] as $key=>$list_item){
  296. if(in_array($list_item["forum_id"], $match_forum)){
  297. $PHORUM["DATA"]["SEARCH"]["forum_list"][$key]["selected"] = true;
  298. }
  299. }
  300. }
  301. $PHORUM["DATA"]["SEARCH"]["forum_list_length"] = min(10, count($PHORUM["DATA"]["SEARCH"]["forum_list"])+1);
  302. if ($PHORUM["args"]["match_type"] == "USER_ID")
  303. {
  304. $search_user = phorum_api_user_get((int)$phorum_author);
  305. if (!$search_user) {
  306. $search_name = $PHORUM["DATA"]["LANG"]["AnonymousUser"];
  307. } else {
  308. $search_name = $search_user["display_name"];
  309. if (empty($PHORUM['custom_display_name'])) {
  310. $search_name = htmlspecialchars($search_name, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]);
  311. }
  312. }
  313. $PHORUM["DATA"]["HEADING"] = $PHORUM["DATA"]["LANG"]["SearchAllPosts"];
  314. $PHORUM["DATA"]["HTML_TITLE"] = $PHORUM["DATA"]["LANG"]["SearchAllPosts"];
  315. } else {
  316. $PHORUM["DATA"]["HEADING"] = $PHORUM["DATA"]["LANG"]["Search"];
  317. $PHORUM["DATA"]["HTML_TITLE"].= PHORUM_SEPARATOR.$PHORUM["DATA"]["LANG"]["Search"];
  318. if(!empty($phorum_search)){
  319. $PHORUM["DATA"]["HTML_TITLE"] .= " - ".htmlspecialchars($phorum_search);
  320. }
  321. }
  322. $PHORUM["DATA"]["DESCRIPTION"] = "";
  323. /**
  324. * [hook]
  325. * search_output
  326. *
  327. * [description]
  328. * This hook can be used to override the standard output for the
  329. * search page. This can be useful for search modules that implement
  330. * a different search backend which does not support the same options
  331. * as Phorum's standard search backend.
  332. *
  333. * [category]
  334. * Message search
  335. *
  336. * [when]
  337. * At the end of the search script, just before it loads the
  338. * output template.
  339. *
  340. * [input]
  341. * The name of the template to use for displaying the search page,
  342. * which is "search" by default.
  343. *
  344. * [output]
  345. * The possibly updated template name to load or NULL if the module
  346. * handled the output on its own already.
  347. */
  348. $template = 'search';
  349. if (isset($PHORUM["hooks"]["search_output"])) {
  350. $template = phorum_hook("search_output", $template);
  351. if ($template === NULL) return;
  352. }
  353. phorum_output($template);
  354. ?>