X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mod%2Fsearch.php;h=4a911b4fd9828a51aaf05459bdad0dfc67174c15;hb=b2d685482928363ce86c3c0519c8ff39d0af43ca;hp=8f3d897ae4a815d6a559a2c58f27df64c67a108a;hpb=4f2ae8868431952bd3acda1537ee899740cbf205;p=friendica.git diff --git a/mod/search.php b/mod/search.php index 8f3d897ae4..4a911b4fd9 100644 --- a/mod/search.php +++ b/mod/search.php @@ -4,33 +4,28 @@ */ use Friendica\App; -use Friendica\Content\Feature; use Friendica\Content\Nav; +use Friendica\Content\Pager; +use Friendica\Content\Text\HTML; use Friendica\Core\Cache; use Friendica\Core\Config; use Friendica\Core\L10n; -use Friendica\Core\System; +use Friendica\Core\Logger; +use Friendica\Core\Renderer; use Friendica\Database\DBA; -use Friendica\Database\DBM; use Friendica\Model\Item; - -require_once 'include/security.php'; -require_once 'include/conversation.php'; -require_once 'mod/dirfind.php'; +use Friendica\Module\BaseSearchModule; +use Friendica\Util\Strings; function search_saved_searches() { - $o = ''; - $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : ''); - - if (!Feature::isEnabled(local_user(),'savedsearch')) - return $o; + $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); $r = q("SELECT `id`,`term` FROM `search` WHERE `uid` = %d", intval(local_user()) ); - if (DBM::is_result($r)) { + if (DBA::isResult($r)) { $saved = []; foreach ($r as $rr) { $saved[] = [ @@ -43,9 +38,9 @@ function search_saved_searches() { } - $tpl = get_markup_template("saved_searches_aside.tpl"); + $tpl = Renderer::getMarkupTemplate("saved_searches_aside.tpl"); - $o .= replace_macros($tpl, [ + $o .= Renderer::replaceMacros($tpl, [ '$title' => L10n::t('Saved Searches'), '$add' => '', '$searchbox' => '', @@ -54,25 +49,23 @@ function search_saved_searches() { } return $o; - } function search_init(App $a) { - - $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : ''); + $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : ''); if (local_user()) { - if (x($_GET,'save') && $search) { + if (!empty($_GET['save']) && $search) { $r = q("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1", intval(local_user()), - dbesc($search) + DBA::escape($search) ); - if (!DBM::is_result($r)) { + if (!DBA::isResult($r)) { DBA::insert('search', ['uid' => local_user(), 'term' => $search]); } } - if (x($_GET,'remove') && $search) { + if (!empty($_GET['remove']) && $search) { DBA::delete('search', ['uid' => local_user(), 'term' => $search]); } @@ -87,33 +80,18 @@ function search_init(App $a) { unset($_SESSION['theme']); unset($_SESSION['mobile-theme']); } - - - -} - - - -function search_post(App $a) { - if (x($_POST,'search')) - $a->data['search'] = $_POST['search']; } - function search_content(App $a) { - if (Config::get('system','block_public') && !local_user() && !remote_user()) { notice(L10n::t('Public access denied.') . EOL); return; } if (Config::get('system','local_search') && !local_user() && !remote_user()) { - System::httpExit(403, - ["title" => L10n::t("Public access denied."), - "description" => L10n::t("Only logged in users are permitted to perform a search.")]); - killme(); - //notice(L10n::t('Public access denied.').EOL); - //return; + $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search.")); + $e->httpdesc = L10n::t("Public access denied."); + throw $e; } if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) { @@ -133,36 +111,29 @@ function search_content(App $a) { if (!is_null($result)) { $resultdata = json_decode($result); if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) { - System::httpExit(429, - ["title" => L10n::t("Too Many Requests"), - "description" => L10n::t("Only one search per minute is permitted for not logged in users.")]); - killme(); + throw new \Friendica\Network\HTTPException\TooManyRequestsException(L10n::t("Only one search per minute is permitted for not logged in users.")); } - Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), CACHE_HOUR); + Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => $resultdata->accesses + 1]), Cache::HOUR); } else - Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), CACHE_HOUR); + Cache::set("remote_search:".$remote, json_encode(["time" => time(), "accesses" => 1]), Cache::HOUR); } Nav::setSelected('search'); - $search = ''; - if (x($a->data,'search')) - $search = notags(trim($a->data['search'])); - else - $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : ''); + $search = (!empty($_REQUEST['search']) ? Strings::escapeTags(trim(rawurldecode($_REQUEST['search']))) : ''); $tag = false; - if (x($_GET,'tag')) { + if (!empty($_GET['tag'])) { $tag = true; - $search = (x($_GET,'tag') ? '#' . notags(trim(rawurldecode($_GET['tag']))) : ''); + $search = (!empty($_GET['tag']) ? '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))) : ''); } // contruct a wrapper for the search header - $o = replace_macros(get_markup_template("content_wrapper.tpl"),[ + $o = Renderer::replaceMacros(Renderer::getMarkupTemplate("content_wrapper.tpl"),[ 'name' => "search-header", '$title' => L10n::t("Search"), '$title_size' => 3, - '$content' => search($search,'search-box','search',((local_user()) ? true : false), false) + '$content' => HTML::search($search,'search-box','search', false) ]); if (strpos($search,'#') === 0) { @@ -170,13 +141,23 @@ function search_content(App $a) { $search = substr($search,1); } if (strpos($search,'@') === 0) { - return dirfind_content($a); + return BaseSearchModule::performSearch(); } if (strpos($search,'!') === 0) { - return dirfind_content($a); + return BaseSearchModule::performSearch(); } - if (x($_GET,'search-option')) + if (parse_url($search, PHP_URL_SCHEME) != '') { + $id = Item::fetchByLink($search); + if (!empty($id)) { + $item = Item::selectFirst(['guid'], ['id' => $id]); + if (DBA::isResult($item)) { + $a->internalRedirect('display/' . $item['guid']); + } + } + } + + if (!empty($_GET['search-option'])) switch($_GET['search-option']) { case 'fulltext': break; @@ -184,11 +165,9 @@ function search_content(App $a) { $tag = true; break; case 'contacts': - return dirfind_content($a, "@"); - break; + return BaseSearchModule::performSearch('@'); case 'forums': - return dirfind_content($a, "!"); - break; + return BaseSearchModule::performSearch('!'); } if (!$search) @@ -202,14 +181,16 @@ function search_content(App $a) { // OR your own posts if you are a logged in member // No items will be shown if the member has a blocked profile wall. + $pager = new Pager($a->query_string); + if ($tag) { - logger("Start tag search for '".$search."'", LOGGER_DEBUG); + Logger::log("Start tag search for '".$search."'", Logger::DEBUG); $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `otype` = ? AND `type` = ? AND `term` = ?", local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search]; - $params = ['order' => ['created' => true], - 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; + $params = ['order' => ['received' => true], + 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; $terms = DBA::select('term', ['oid'], $condition, $params); $itemids = []; @@ -226,18 +207,18 @@ function search_content(App $a) { $r = []; } } else { - logger("Start fulltext search for '".$search."'", LOGGER_DEBUG); + Logger::log("Start fulltext search for '".$search."'", Logger::DEBUG); $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`)) AND `body` LIKE CONCAT('%',?,'%')", local_user(), $search]; $params = ['order' => ['id' => true], - 'limit' => [$a->pager['start'], $a->pager['itemspage']]]; + 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; $items = Item::selectForUser(local_user(), [], $condition, $params); $r = Item::inArray($items); } - if (!DBM::is_result($r)) { + if (!DBA::isResult($r)) { info(L10n::t('No results.') . EOL); return $o; } @@ -249,16 +230,16 @@ function search_content(App $a) { $title = L10n::t('Results for: %s', $search); } - $o .= replace_macros(get_markup_template("section_title.tpl"),[ + $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate("section_title.tpl"),[ '$title' => $title ]); - logger("Start Conversation for '".$search."'", LOGGER_DEBUG); - $o .= conversation($a, $r, 'search', false, false, 'commented', local_user()); + Logger::log("Start Conversation for '".$search."'", Logger::DEBUG); + $o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user()); - $o .= alt_pager($a,count($r)); + $o .= $pager->renderMinimal(count($r)); - logger("Done '".$search."'", LOGGER_DEBUG); + Logger::log("Done '".$search."'", Logger::DEBUG); return $o; }