Esempio n. 1
0
function viewSearch()
{
    global $mainframe, $mosConfig_absolute_path, $mosConfig_lang, $my;
    global $Itemid, $database, $_MAMBOTS;
    $gid = $my->gid;
    // Adds parameter handling
    if ($Itemid > 0) {
        $menu =& new mosMenu($database);
        $menu->load($Itemid);
        $params =& new mosParameters($menu->params);
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', $menu->name, T_('Search'));
        $params->def('back_button', $mainframe->getCfg('back_button'));
    } else {
        $params =& new mosParameters('');
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', T_('Search'));
        $params->def('back_button', $mainframe->getCfg('back_button'));
    }
    // html output
    search_html::openhtml($params);
    $searchword = mosGetParam($_REQUEST, 'searchword', '');
    $searchword = $database->getEscaped(trim($searchword));
    $search_ignore = array();
    @(include "{$mosConfig_absolute_path}/language/{$mosConfig_lang}.ignore.php");
    $orders = array();
    $orders[] = mosHTML::makeOption('newest', T_('Newest first'));
    $orders[] = mosHTML::makeOption('oldest', T_('Oldest first'));
    $orders[] = mosHTML::makeOption('popular', T_('Most popular'));
    $orders[] = mosHTML::makeOption('alpha', T_('Alphabetical'));
    $orders[] = mosHTML::makeOption('category', T_('Section/Category'));
    $ordering = mosGetParam($_REQUEST, 'ordering', 'newest');
    if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) {
        $ordering = 'newest';
    }
    $lists = array();
    $lists['ordering'] = mosHTML::selectList($orders, 'ordering', 'class="inputbox"', 'value', 'text', $ordering);
    $searchphrase = mosGetParam($_REQUEST, 'searchphrase', 'any');
    if (!in_array($searchphrase, array('any', 'all', 'exact'))) {
        $searchphrase = 'any';
    }
    $searchphrases = array();
    $phrase = new stdClass();
    $phrase->value = 'any';
    $phrase->text = T_('Any words');
    $searchphrases[] = $phrase;
    $phrase = new stdClass();
    $phrase->value = 'all';
    $phrase->text = T_('All words');
    $searchphrases[] = $phrase;
    $phrase = new stdClass();
    $phrase->value = 'exact';
    $phrase->text = T_('Exact phrase');
    $searchphrases[] = $phrase;
    $lists['searchphrase'] = mosHTML::radioList($searchphrases, 'searchphrase', '', $searchphrase);
    // html output
    search_html::searchbox(htmlspecialchars($searchword), $lists, $params);
    if (!$searchword) {
        if (count($_POST)) {
            // html output
            // no matches found
            search_html::message(T_('No results were found'), $params);
        }
    } else {
        foreach ($search_ignore as $ignore_word) {
            $searchword = preg_replace("/(^|\\W){$ignore_word}(\$|\\W)/i", '$1$2', $searchword);
        }
        $searchword = trim($searchword);
        if (!$searchword) {
            search_html::message(T_('One or more common words were ignored in the search'), $params);
        }
    }
    if ($searchword) {
        // html output
        search_html::searchintro(htmlspecialchars($searchword), $params);
        mosLogSearch($searchword);
        $phrase = mosGetParam($_REQUEST, 'searchphrase', '');
        if (!in_array($phrase, array('any', 'all', 'exact'))) {
            $phrase = 'any';
        }
        $ordering = mosGetParam($_REQUEST, 'ordering', '');
        if (!in_array($ordering, array('newest', 'oldest', 'popular', 'alpha', 'category'))) {
            $ordering = 'newest';
        }
        $_MAMBOTS->loadBotGroup('search');
        $results = $_MAMBOTS->trigger('onSearch', array($searchword, $phrase, $ordering));
        $rows = array();
        foreach ($results as $result) {
            if ($result) {
                $rows = array_merge($rows, $result);
            }
        }
        $totalRows = count($rows);
        for ($i = 0; $i < $totalRows; $i++) {
            $row =& $rows[$i]->text;
            if ($phrase == 'exact') {
                $searchwords = array($searchword);
                $needle = $searchword;
            } else {
                $searchwords = explode(' ', $searchword);
                $needle = $searchwords[0];
            }
            $row = mosPrepareSearchContent($row, 200, $needle);
            foreach ($searchwords as $hlword) {
                $row = preg_replace('/' . preg_quote($hlword, '/') . '/i', "<span class=\"highlight\">\\0</span>", $row);
            }
            if (!eregi('^http', $rows[$i]->href)) {
                // determines Itemid for Content items
                if (strstr($rows[$i]->href, 'view')) {
                    // tests to see if itemid has already been included - this occurs for typed content items
                    if (!strstr($rows[$i]->href, 'Itemid')) {
                        $temp = explode('id=', $rows[$i]->href);
                        $rows[$i]->href = $rows[$i]->href . '&amp;Itemid=' . $mainframe->getItemid($temp[1]);
                    }
                }
            }
        }
        $mainframe->setPageTitle(T_('Search'));
        if ($totalRows) {
            // html output
            search_html::display($rows, $params);
        } else {
            // html output
            search_html::displaynoresult();
        }
        // html output
        search_html::conclusion($totalRows, htmlspecialchars($searchword));
    }
    // displays back button
    echo '<br />';
    mosHTML::BackButton($params, 0);
}
Esempio n. 2
0
function viewSearch()
{
    global $mainframe, $mosConfig_absolute_path, $mosConfig_lang, $my;
    global $Itemid, $database, $_MAMBOTS;
    global $mosConfig_list_limit;
    $restriction = 0;
    // try to find search component's Itemid
    // Only search if we don't have a valid Itemid (e.g. from module)
    if (!intval($Itemid) || intval($Itemid) == 99999999) {
        $query = "SELECT id" . "\n FROM #__menu" . "\n WHERE type = 'components'" . "\n AND published = 1" . "\n AND link = 'index.php?option=com_search'";
        $database->setQuery($query);
        $_Itemid = $database->loadResult();
        if ($_Itemid != "") {
            $Itemid = $_Itemid;
        }
    }
    $gid = $my->gid;
    // Adds parameter handling
    if ($Itemid > 0 && $Itemid != 99999999) {
        $menu = $mainframe->get('menu');
        $params = new mosParameters($menu->params);
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', $menu->name);
        $params->def('back_button', $mainframe->getCfg('back_button'));
    } else {
        $params = new mosParameters('');
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', _SEARCH_TITLE);
        $params->def('back_button', $mainframe->getCfg('back_button'));
    }
    // html output
    search_html::openhtml($params);
    $searchphrase = mosGetParam($_REQUEST, 'searchphrase', 'any');
    $searchphrase = preg_replace('/[^a-z]/', '', strtolower($searchphrase));
    $searchword = strval(mosGetParam($_REQUEST, 'searchword', ''));
    $searchword = trim(stripslashes($searchword));
    // limit searchword to 20 characters
    if (strlen($searchword) > 20) {
        $searchword = substr($searchword, 0, 19);
        $restriction = 1;
    }
    // searchword must contain a minimum of 3 characters
    if ($searchword && strlen($searchword) < 3) {
        $searchword = '';
        $restriction = 1;
    }
    if ($searchphrase != 'exact') {
        $aterms = explode(' ', strtolower($searchword));
        $search_ignore = array();
        // filter out search terms that are too small
        foreach ($aterms as $aterm) {
            if (strlen($aterm) < 3) {
                $search_ignore[] = $aterm;
            }
        }
        $pruned = array_diff($aterms, $search_ignore);
        $pruned = array_unique($pruned);
        $searchword = implode(' ', $pruned);
        if (trim($searchword) == '') {
            $restriction = 1;
        }
    }
    @(include "{$mosConfig_absolute_path}/language/{$mosConfig_lang}.ignore.php");
    $orders = array();
    $orders[] = mosHTML::makeOption('newest', _SEARCH_NEWEST);
    $orders[] = mosHTML::makeOption('oldest', _SEARCH_OLDEST);
    $orders[] = mosHTML::makeOption('popular', _SEARCH_POPULAR);
    $orders[] = mosHTML::makeOption('alpha', _SEARCH_ALPHABETICAL);
    $orders[] = mosHTML::makeOption('category', _SEARCH_CATEGORY);
    $ordering = mosGetParam($_REQUEST, 'ordering', 'newest');
    $ordering = preg_replace('/[^a-z]/', '', strtolower($ordering));
    $lists = array();
    $lists['ordering'] = mosHTML::selectList($orders, 'ordering', 'id="search_ordering" class="inputbox"', 'value', 'text', $ordering);
    $searchphrases = array();
    $phrase = new stdClass();
    $phrase->value = 'any';
    $phrase->text = _SEARCH_ANYWORDS;
    $searchphrases[] = $phrase;
    $phrase = new stdClass();
    $phrase->value = 'all';
    $phrase->text = _SEARCH_ALLWORDS;
    $searchphrases[] = $phrase;
    $phrase = new stdClass();
    $phrase->value = 'exact';
    $phrase->text = _SEARCH_PHRASE;
    $searchphrases[] = $phrase;
    $lists['searchphrase'] = mosHTML::radioList($searchphrases, 'searchphrase', '', $searchphrase);
    // html output
    search_html::searchbox(htmlspecialchars($searchword), $lists, $params);
    if (!$searchword) {
        if (count($_POST)) {
            // html output
            // no matches found
            search_html::message(_NOKEYWORD, $params);
        } else {
            if ($restriction) {
                // html output
                search_html::message(_SEARCH_MESSAGE, $params);
            }
        }
    } else {
        if (in_array($searchword, $search_ignore)) {
            // html output
            search_html::message(_IGNOREKEYWORD, $params);
        } else {
            // html output
            if ($restriction) {
                // html output
                search_html::message(_SEARCH_MESSAGE, $params);
            }
            $searchword_clean = htmlspecialchars($searchword);
            search_html::searchintro($searchword_clean, $params);
            mosLogSearch($searchword);
            $_MAMBOTS->loadBotGroup('search');
            $results = $_MAMBOTS->trigger('onSearch', array($database->getEscaped($searchword, true), $searchphrase, $ordering));
            $totalRows = 0;
            $rows = array();
            for ($i = 0, $n = count($results); $i < $n; $i++) {
                $rows = array_merge((array) $rows, (array) $results[$i]);
            }
            $totalRows = count($rows);
            for ($i = 0; $i < $totalRows; $i++) {
                $text =& $rows[$i]->text;
                if ($searchphrase == 'exact') {
                    $searchwords = array($searchword);
                    $needle = $searchword;
                } else {
                    $searchwords = explode(' ', $searchword);
                    $needle = $searchwords[0];
                }
                $text = mosPrepareSearchContent($text, 200, $needle);
                foreach ($searchwords as $k => $hlword) {
                    $searchwords[$k] = htmlspecialchars(stripslashes($hlword));
                }
                $searchRegex = implode('|', $searchwords);
                $text = eregi_replace('(' . $searchRegex . ')', '<span class="highlight">\\0</span>', $text);
                if (strpos($rows[$i]->href, 'http') == false) {
                    $url = parse_url($rows[$i]->href);
                    parse_str(@$url['query'], $link);
                    // determines Itemid for Content items where itemid has not been included
                    if (isset($rows[$i]->type) && @$link['task'] == 'view' && isset($link['id']) && !isset($link['Itemid'])) {
                        $itemid = '';
                        $_itemid = $mainframe->getItemid($link['id'], 0);
                        if ($_itemid) {
                            $itemid = '&amp;Itemid=' . $_itemid;
                        }
                        $rows[$i]->href = $rows[$i]->href . $itemid;
                    }
                }
            }
            $mainframe->setPageTitle(_SEARCH_TITLE);
            $total = $totalRows;
            $limit = intval(mosGetParam($_GET, 'limit', $mosConfig_list_limit));
            $limit = $limit ? $limit : $mosConfig_list_limit;
            $limitstart = intval(mosGetParam($_GET, 'limitstart', 0));
            // prepares searchword for proper display in url
            $searchword_clean = urlencode($searchword_clean);
            if ($n) {
                // html output
                require_once $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php';
                $pageNav = new mosPageNav($total, $limitstart, $limit);
                search_html::display($rows, $params, $pageNav, $limitstart, $limit, $total, $totalRows, $searchword_clean);
            } else {
                // html output
                search_html::displaynoresult();
            }
            // html output
            search_html::conclusion($searchword_clean, $pageNav);
        }
    }
    // displays back button
    echo '<br/>';
    mosHTML::BackButton($params, 0);
}