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

/phpmyfaq/search.php

https://github.com/cyrke/phpMyFAQ
PHP | 176 lines | 118 code | 25 blank | 33 comment | 22 complexity | 44815285add93011375768f31cf4fedf MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * The fulltext search page
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Frontend
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @copyright 2002-2012 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2002-09-16
  18. */
  19. if (!defined('IS_VALID_PHPMYFAQ')) {
  20. header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']));
  21. exit();
  22. }
  23. $faqsession->userTracking('fulltext_search', 0);
  24. // Get possible user input
  25. $inputLanguage = PMF_Filter::filterInput(INPUT_GET, 'langs', FILTER_SANITIZE_STRING);
  26. $inputCategory = PMF_Filter::filterInput(INPUT_GET, 'searchcategory', FILTER_VALIDATE_INT, '%');
  27. $inputTag = PMF_Filter::filterInput(INPUT_GET, 'tagging_id', FILTER_VALIDATE_INT);
  28. $inputSearchTerm = PMF_Filter::filterInput(INPUT_GET, 'suchbegriff', FILTER_SANITIZE_STRIPPED);
  29. $search = PMF_Filter::filterInput(INPUT_GET, 'search', FILTER_SANITIZE_STRIPPED);
  30. $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1);
  31. // Search only on current language (default)
  32. if (!is_null($inputLanguage)) {
  33. $allLanguages = true;
  34. $languages = '&amp;langs=all';
  35. } else {
  36. $allLanguages = false;
  37. $languages = '';
  38. }
  39. // HACK: (re)evaluate the Category object w/o passing the user language
  40. // so the result set of a Search will have the Category Path
  41. // for any of the multilanguage faq records and the Category list
  42. // on the left pane will not be affected
  43. if ($allLanguages) {
  44. $category = new PMF_Category($faqConfig);
  45. $category->transform(0);
  46. }
  47. if (is_null($user)) {
  48. $user = new PMF_User_CurrentUser($faqConfig);
  49. }
  50. $faqSearch = new PMF_Search($faqConfig);
  51. $faqSearchResult = new PMF_Search_Resultset($user, $faq, $faqConfig);
  52. $tagSearch = false;
  53. //
  54. // Handle the Tagging ID
  55. //
  56. if (!is_null($inputTag)) {
  57. $tagSearch = true;
  58. $tagging = new PMF_Tags($faqConfig);
  59. $recordIds = $tagging->getRecordsByTagId($inputTag);
  60. $searchResult = $faq->showAllRecordsByIds($recordIds);
  61. } else {
  62. $searchResult = '';
  63. }
  64. //
  65. // Handle the full text search stuff
  66. //
  67. if (!is_null($inputSearchTerm) || !is_null($search)) {
  68. if (!is_null($inputSearchTerm)) {
  69. $inputSearchTerm = $faqConfig->getDb()->escape(strip_tags($inputSearchTerm));
  70. }
  71. if (!is_null($search)) {
  72. $inputSearchTerm = $faqConfig->getDb()->escape(strip_tags($search));
  73. }
  74. $faqSearch->setCategory($category);
  75. $faqSearch->setCategoryId($inputCategory);
  76. $searchResults = $faqSearch->search($inputSearchTerm, $allLanguages);
  77. $faqSearchResult->reviewResultset($searchResults);
  78. $inputSearchTerm = stripslashes($inputSearchTerm);
  79. $faqSearch->logSearchTerm($inputSearchTerm);
  80. }
  81. // Change a little bit the $searchCategory value;
  82. $inputCategory = ('%' == $inputCategory) ? 0 : $inputCategory;
  83. $faqsession->userTracking('fulltext_search', $inputSearchTerm);
  84. if (is_numeric($inputSearchTerm) && PMF_SOLUTION_ID_START_VALUE <= $inputSearchTerm &&
  85. 0 < $faqSearchResult->getNumberOfResults()) {
  86. // Before a redirection we must force the PHP session update for preventing data loss
  87. session_write_close();
  88. if ($faqConfig->get('main.enableRewriteRules')) {
  89. header('Location: ' . $faqConfig->get('main.referenceURL') . '/solution_id_' . $inputSearchTerm . '.html');
  90. } else {
  91. header('Location: ' . $faqConfig->get('main.referenceURL') . '/index.php?solution_id=' . $inputSearchTerm);
  92. }
  93. exit();
  94. }
  95. $category->buildTree();
  96. $mostPopularSearchData = $faqSearch->getMostPopularSearches($faqConfig->get('search.numberSearchTerms'));
  97. // Set base URL scheme
  98. if ($faqConfig->get('main.enableRewriteRules')) {
  99. $baseUrl = sprintf("%ssearch.html?search=%s&amp;seite=%d%s&amp;searchcategory=%d",
  100. PMF_Link::getSystemRelativeUri('index.php'),
  101. urlencode($inputSearchTerm),
  102. $page,
  103. $languages,
  104. $inputCategory
  105. );
  106. } else {
  107. $baseUrl = sprintf('%s?%saction=search&amp;search=%s&amp;seite=%d%s&amp;searchcategory=%d',
  108. PMF_Link::getSystemRelativeUri(),
  109. empty($sids) ? '' : '$sids&amp;',
  110. urlencode($inputSearchTerm),
  111. $page,
  112. $languages,
  113. $inputCategory
  114. );
  115. }
  116. // Pagination options
  117. $options = array(
  118. 'baseUrl' => $baseUrl,
  119. 'total' => $faqSearchResult->getNumberOfResults(),
  120. 'perPage' => $faqConfig->get('records.numberOfRecordsPerPage'),
  121. 'pageParamName' => 'seite',
  122. 'layoutTpl' => '<div align="center" class="pagination"><ul>{LAYOUT_CONTENT}</ul></div>'
  123. );
  124. $faqPagination = new PMF_Pagination($faqConfig, $options);
  125. $categoryHelper = new PMF_Helper_Category();
  126. $categoryHelper->setCategory($category);
  127. $searchHelper = new PMF_Helper_Search($faqConfig);
  128. $searchHelper->setSearchterm($inputSearchTerm);
  129. $searchHelper->setCategory($category);
  130. $searchHelper->setPagination($faqPagination);
  131. $searchHelper->setPlurals($plr);
  132. $searchHelper->setSessionId($sids);
  133. if ('' == $searchResult && !is_null($inputSearchTerm)) {
  134. $searchResult = $searchHelper->renderSearchResult($faqSearchResult, $page);
  135. }
  136. $tpl->parse('writeContent', array(
  137. 'msgAdvancedSearch' => ($tagSearch ? $PMF_LANG['msgTagSearch'] : $PMF_LANG['msgAdvancedSearch']),
  138. 'msgSearch' => $PMF_LANG['msgSearch'],
  139. 'searchString' => PMF_String::htmlspecialchars($inputSearchTerm, ENT_QUOTES, 'utf-8'),
  140. 'searchOnAllLanguages' => $PMF_LANG['msgSearchOnAllLanguages'],
  141. 'checkedAllLanguages' => $allLanguages ? ' checked="checked"' : '',
  142. 'selectCategories' => $PMF_LANG['msgSelectCategories'],
  143. 'allCategories' => $PMF_LANG['msgAllCategories'],
  144. 'printCategoryOptions' => $categoryHelper->renderOptions($inputCategory),
  145. 'writeSendAdress' => '?'.$sids.'action=search',
  146. 'msgSearchWord' => $PMF_LANG['msgSearchWord'],
  147. 'printResult' => $searchResult,
  148. 'openSearchLink' => $searchHelper->renderOpenSearchLink(),
  149. 'msgMostPopularSearches' => $PMF_LANG['msgMostPopularSearches'],
  150. 'printMostPopularSearches' => $searchHelper->renderMostPopularSearches($mostPopularSearchData)));
  151. $tpl->merge('writeContent', 'index');