コード例 #1
0
    /**
     * returns search results XML
     * @param $text		search string
     * @param $type		search type: msgs - messages | tlts - titles
     * @param $forum	forum id to search within
     * @param $u		search posts of this user only
     * @param $disp		display: topics | posts
     * @param $max_res	max number of results
     */
    function getSearchResultsXML($text, $type, $forum, $u, $disp, $max_res = 50)
    {
        $fdb = new DbForum();
        if (!$this->_checkUserPerm('', '', 'search')) {
            return $this->_no_access();
        }
        switch ($type) {
            case 'msgs':
            case 'tlts':
                $a = $fdb->searchMessages($text, $u, $forum, $type, 'posts' == $disp ? 1 : 0, $max_res);
                break;
            default:
                return '<error>Wrong search type</error>';
        }
        $ws = preg_split("/\\s+/", $text);
        reset($a);
        $s = '';
        switch ($type) {
            case 'tlts':
                while (list(, $r) = each($a)) {
                    // search hightlight
                    reset($ws);
                    while (list(, $w) = each($ws)) {
                        if ($w) {
                            $r['topic_title'] = preg_replace("/({$w})/i", "<span style=\"background-color:yellow\">{$w}</span>", $r['topic_title']);
                        }
                    }
                    encode_post_text($r['cat_name']);
                    encode_post_text($r['forum_title']);
                    encode_post_text($r['topic_title'], 0, 1);
                    $s .= <<<EOF
\t\t\t\t\t<sr date="{$r['date']}" user="******">
\t\t\t\t\t\t<c id="{$r['cat_id']}">{$r['cat_name']}</c>
\t\t\t\t\t\t<f id="{$r['forum_id']}">{$r['forum_title']}</f>
\t\t\t\t\t\t<t id="{$r['topic_id']}">{$r['topic_title']}</t>
\t\t\t\t\t</sr>
EOF;
                }
                break;
            case 'msgs':
                while (list(, $r) = each($a)) {
                    // search hightlight
                    reset($ws);
                    while (list(, $w) = each($ws)) {
                        if ($w) {
                            $ind = eregi("([^>]*<)", $r['post_text'], $ind);
                            // html tags?
                            if ($ind) {
                                $r['post_text'] = preg_replace("/({$w})(?=[^>]*<)/i", "<span style=\"background-color:yellow\">{$w}</span>", "<div>{$r['post_text']}</div>");
                            } else {
                                $r['post_text'] = preg_replace("/({$w})/i", "<span style=\"background-color:yellow\">{$w}</span>", $r['post_text']);
                            }
                        }
                    }
                    encode_post_text($r['post_text']);
                    reset($ws);
                    while (list(, $w) = each($ws)) {
                        $r['topic_title'] = preg_replace("/({$w})/i", "<span style=\"background-color:yellow\">{$w}</span>", $r['topic_title']);
                    }
                    encode_post_text($r['cat_name']);
                    encode_post_text($r['forum_title']);
                    encode_post_text($r['topic_title'], 0, 1);
                    $s .= <<<EOF
\t\t\t\t\t<sr date="{$r['date']}" user="******">
\t\t\t\t\t\t<c id="{$r['cat_id']}">{$r['cat_name']}</c>
\t\t\t\t\t\t<f id="{$r['forum_id']}">{$r['forum_title']}</f>
\t\t\t\t\t\t<t id="{$r['topic_id']}">{$r['topic_title']}</t>
\t\t\t\t\t\t<p id="{$r['post_id']}">{$r['post_text']}</p>
\t\t\t\t\t</sr>
EOF;
                }
                break;
        }
        $cu = $this->getUrlsXml();
        encode_post_text($text, 0, 1);
        return "<root>{$cu}<search><search_text>{$text}</search_text>{$s}</search></root>";
    }