PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/xforum/search.php

https://github.com/severnaya99/Sg-2010
PHP | 222 lines | 168 code | 23 blank | 31 comment | 41 complexity | f8cfe9f56949a0ff5f414c5edfdf2996 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. // $Id: search.php,v 4.04 2008/06/05 15:35:59 wishcraft Exp $
  3. // ------------------------------------------------------------------------ //
  4. // XOOPS - PHP Content Management System //
  5. // Copyright (c) 2000 XOOPS.org //
  6. // <http://www.xoops.org/> //
  7. // ------------------------------------------------------------------------ //
  8. // This program is free software; you can redistribute it and/or modify //
  9. // it under the terms of the GNU General Public License 2.0 as published by //
  10. // the Free Software Foundation; either version 2 of the License, or //
  11. // (at your option) any later version. //
  12. // //
  13. // You may not change or alter any portion of this comment or credits //
  14. // of supporting developers from this source code or any supporting //
  15. // source code which is considered copyrighted (c) material of the //
  16. // original comment or credit authors. //
  17. // //
  18. // This program is distributed in the hope that it will be useful, //
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  21. // GNU General Public License for more details. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program; if not, write to the Free Software //
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
  26. // ------------------------------------------------------------------------ //
  27. // Author: wishcraft (S.F.C., sales@chronolabs.org.au) //
  28. // URL: http://www.chronolabs.org.au/forums/X-Forum/0,17,0,0,100,0,DESC,0 //
  29. // Project: X-Forum 4 //
  30. // ------------------------------------------------------------------------ //
  31. include 'header.php';
  32. forum_load_lang_file("search");
  33. $config_handler =& xoops_gethandler('config');
  34. $xoopsConfigSearch =& $config_handler->getConfigsByCat(XOOPS_CONF_SEARCH);
  35. if ($xoopsConfigSearch['enable_search'] != 1) {
  36. header('Location: '.XOOPS_URL.'/modules/xforum/index.php');
  37. exit();
  38. }
  39. $xoopsConfig['module_cache'][$xoopsModule->getVar('mid')] = 0;
  40. $xoopsOption['template_main']= 'xforum_search.html';
  41. include XOOPS_ROOT_PATH.'/header.php';
  42. include_once XOOPS_ROOT_PATH.'/modules/xforum/include/search.inc.php';
  43. $limit = $xoopsModuleConfig['topics_per_page'];
  44. $queries = array();
  45. $andor = "";
  46. $start = 0;
  47. $uid = 0;
  48. $xforum = 0;
  49. $sortby = 'p.post_time DESC';
  50. $subquery = "";
  51. $searchin = "both";
  52. $sort = "";
  53. $since = isset($_POST['since']) ? $_POST['since'] : (isset($_GET['since']) ? $_GET['since'] : null);
  54. $next_search['since'] = $since;
  55. $term = isset($_POST['term']) ? $_POST['term'] : (isset($_GET['term']) ? $_GET['term'] : null);
  56. $uname = isset($_POST['uname']) ? $_POST['uname'] : (isset($_GET['uname']) ? $_GET['uname'] : null);
  57. if ($xoopsModuleConfig['wol_enabled']){
  58. $online_handler =& xoops_getmodulehandler('online', 'xforum');
  59. $online_handler->init(0);
  60. }
  61. $xoopsTpl->assign("forumindex", sprintf(_MD_FORUMINDEX, htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES)));
  62. $xoopsTpl->assign("img_folder", forum_displayImage($xforumImage['folder_topic']));
  63. if ( !empty($_POST['submit']) || !empty($_GET['submit']) || !empty($uname) || !empty($term)) {
  64. $start = isset($_GET['start']) ? $_GET['start'] : 0;
  65. $xforum = isset($_POST['forum']) ? $_POST['forum'] : (isset($_GET['forum']) ? $_GET['forum'] : null);
  66. if (empty($xforum) or $xforum == 'all' or (is_array($xforum) and in_array('all', $xforum))) {
  67. $xforum = array();
  68. } elseif(!is_array($xforum)){
  69. $xforum = array_map("intval",explode(",", $xforum));
  70. }
  71. $next_search['forum'] = implode(",", $xforum);
  72. $addterms = isset($_POST['andor']) ? $_POST['andor'] : (isset($_GET['andor']) ? $_GET['andor'] : "");
  73. $next_search['andor'] = $addterms;
  74. if ( !in_array(strtolower($addterms), array("or", "and", "exact"))){
  75. $andor = "AND";
  76. }else{
  77. $andor = strtoupper($addterms);
  78. }
  79. $uname_required = false;
  80. $search_username = $uname;
  81. $search_username = trim($search_username);
  82. $next_search['uname'] = $search_username;
  83. if ( !empty($search_username) ) {
  84. $uname_required = true;
  85. $search_username = $myts->addSlashes($search_username);
  86. if ( !$result = $xoopsDB->query("SELECT uid FROM ".$xoopsDB->prefix("users")." WHERE uname LIKE '%$search_username%'") ) {
  87. redirect_header('search.php',1,_MD_ERROROCCURED);
  88. exit();
  89. }
  90. $uid = array();
  91. while ($row = $xoopsDB->fetchArray($result)) {
  92. $uid[] = $row['uid'];
  93. }
  94. }
  95. else {
  96. $uid = 0;
  97. }
  98. $next_search['term'] = $term;
  99. $query = trim($term);
  100. if ( $andor != "EXACT" ) {
  101. $ignored_queries = array(); // holds kewords that are shorter than allowed minmum length
  102. $temp_queries = preg_split('/[\s,]+/', $query);
  103. foreach ($temp_queries as $q) {
  104. $q = trim($q);
  105. if (strlen($q) >= $xoopsConfigSearch['keyword_min']) {
  106. $queries[] = $myts->addSlashes($q);
  107. } else {
  108. $ignored_queries[] = $myts->addSlashes($q);
  109. }
  110. }
  111. if (!$uname_required && count($queries) == 0) {
  112. redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
  113. exit();
  114. }
  115. } else {
  116. //$query = trim($query);
  117. if (!$uname_required && (strlen($query) < $xoopsConfigSearch['keyword_min'])) {
  118. redirect_header('search.php', 2, sprintf(_SR_KEYTOOSHORT, $xoopsConfigSearch['keyword_min']));
  119. exit();
  120. }
  121. $queries = array($myts->addSlashes($query));
  122. }
  123. // entries must be lowercase
  124. $allowed = array('p.post_time desc', 't.topic_title', 't.topic_views', 't.topic_replies', 'f.forum_name', 'u.uname');
  125. $sortby = isset($_POST['sortby']) ? $_POST['sortby'] : (isset($_GET['sortby']) ? $_GET['sortby'] : null);
  126. $next_search['sortby'] = $sortby;
  127. $sortby = (in_array(strtolower($sortby), $allowed)) ? $sortby : 'p.post_time DESC';
  128. $searchin = isset($_POST['searchin']) ? $_POST['searchin'] : (isset($_GET['searchin']) ? $_GET['searchin'] : 'both');
  129. $next_search['searchin'] = $searchin;
  130. if (!empty($since)) {
  131. $subquery = ' AND p.post_time >= ' . (time() - forum_getSinceTime($since));
  132. }
  133. if($uname_required&&(!$uid||count($uid)<1)) $result = false;
  134. else $results =& forum_search($queries, $andor, $limit, $start, $uid, $xforum, $sortby, $searchin, $subquery);
  135. if ( count($results) < 1 ) {
  136. $xoopsTpl->assign("lang_nomatch", _SR_NOMATCH);
  137. }
  138. else {
  139. foreach ($results as $row) {
  140. $xoopsTpl->append('results', array('forum_name' => $myts->htmlSpecialChars($row['forum_name']), 'forum_link' => $row['forum_link'], 'link' => $row['link'], 'title' => $row['title'], 'poster' => $row['poster'], 'post_time' => formatTimestamp($row['time'], "m")));
  141. }
  142. unset($results);
  143. if(count($next_search)>0){
  144. $items = array();
  145. foreach($next_search as $para => $val){
  146. if(!empty($val)) $items[] = "$para=$val";
  147. }
  148. if(count($items)>0) $paras = implode("&",$items);
  149. unset($next_search);
  150. unset($items);
  151. }
  152. $search_url = XOOPS_URL.'/modules/'.$xoopsModule->getVar('dirname')."/search.php?".$paras;
  153. $next_results = forum_search($queries, $andor, 1, $start + $limit, $uid, $xforum, $sortby, $searchin, $subquery);
  154. $next_count = count($next_results);
  155. $has_next = false;
  156. if (is_array($next_results) && $next_count >0) {
  157. $has_next = true;
  158. }
  159. if (false != $has_next) {
  160. $next = $start + $limit;
  161. $queries = implode(',',$queries);
  162. $search_url_next = $search_url."&start=$next";
  163. $search_next = '<a href="'.htmlspecialchars($search_url_next).'">'._SR_NEXT.'</a>';
  164. $xoopsTpl->assign("search_next", $search_next);
  165. }
  166. if ( $start > 0 ) {
  167. $prev = $start - $limit;
  168. $search_url_prev = $search_url."&start=$prev";
  169. $search_prev = '<a href="'.htmlspecialchars($search_url_prev).'">'._SR_PREVIOUS.'</a>';
  170. $xoopsTpl->assign("search_prev", $search_prev);
  171. }
  172. }
  173. $search_info = _SR_KEYWORDS.": ".$myts->htmlSpecialChars($term);
  174. if($uname_required){
  175. if($search_info) $search_info .= "<br />";
  176. $search_info .= _MD_USERNAME.": ".$myts->htmlSpecialChars($search_username);
  177. }
  178. $xoopsTpl->assign("search_info", $search_info);
  179. }
  180. $xforumperms =& xoops_getmodulehandler('permission', 'xforum');
  181. $allowed_forums = $xforumperms->getPermissions('forum');
  182. $forum_handler =& xoops_getmodulehandler('forum', 'xforum');
  183. $forum_array = $forum_handler->getForums();
  184. $select_forum = '<select name="forum[]" size="5" multiple="multiple">';
  185. $select_forum .= '<option value="all">'._MD_SEARCHALLFORUMS.'</option>';
  186. foreach ($forum_array as $key => $xforum) {
  187. if (in_array($xforum->getVar('forum_id'), array_keys($allowed_forums))) {
  188. $select_forum .= '<option value="'.$xforum->getVar('forum_id').'">'.$xforum->getVar('forum_name').'</option>';
  189. }
  190. }
  191. $select_forum .= '</select>';
  192. $xoopsTpl->assign_by_ref("forum_selection_box", $select_forum);
  193. $select_since = forum_sinceSelectBox($xoopsModuleConfig['since_default']);
  194. $xoopsTpl->assign_by_ref("since_selection_box", $select_since);
  195. if ($xoopsConfigSearch['keyword_min'] > 0) {
  196. $xoopsTpl->assign("search_rule", sprintf(_SR_KEYIGNORE, $xoopsConfigSearch['keyword_min']));
  197. }
  198. include XOOPS_ROOT_PATH.'/footer.php';
  199. ?>