<?php
/**
 * @package Flatnux_module_search
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2011
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 * 
 */
defined('_FNEXEC') or die('Restricted access');
global $_FN;
//-----------------------------get request vars-------------------------------->
$q = trim(ltrim(FN_GetParam("q", $_REQUEST, "html")));
$method = FN_GetParam("method", $_REQUEST, "html");
$where = FN_GetParam("where", $_REQUEST, "html");
$op = FN_GetParam("op", $_REQUEST, "html");
$maxres = 100;
$contares = 0;
//-----------------------------get request vars--------------------------------<
//----------------------------print search form-------------------------------->
echo FN_HtmlContent("sections/{$_FN['mod']}");
echo "<div class=\"searchform\">";
echo "\n<form method=\"post\" action=\"" . FN_RewriteLink("index.php?mod={$_FN['mod']}&amp;op=$op") . "\" ><div>";
echo FN_i18n("search", false, "Aa") . ": ";
echo "<input type=\"text\" name=\"q\" value = \"$q\" />";
echo "<input type=\"hidden\" name=\"where\" value = \"$where\" />";
echo "<input type=\"hidden\" name=\"op\" value = \"$op\" />";



echo "\n<select name=\"method\">";
$s = ($method == "OR") ? "selected=\"selected\"" : "";
echo "\n\t<option value=\"OR\" $s >" . FN_i18n("any word") . "</option>";
$s = ($method == "AND") ? "selected=\"selected\"" : "";
echo "\n\t<option value=\"AND\" $s >" . FN_i18n("all word") . "</option>";
$s = ($method == "") ? "selected=\"selected\"" : "";
echo "\n\t<option value=\"\" $s >" . FN_i18n("exact phrase") . "</option>";
echo "\n</select>";
echo "<br />\n\t<button type=\"submit\" >";
echo FN_i18n("search");
echo "</button>";
echo "</div></form>\n";
echo "</div>";
//----------------------------print search form--------------------------------<
//---------------------query--------------------------------------------------->
$tosearch_array = array();
if ($q != "")
{
    if ($method == "OR" || $method == "AND")
    {
        $tosearch_tmp = explode(" ", $q);
        foreach ($tosearch_tmp as $searchitem)
        {
            if (strlen($searchitem) > 0)
            {
                if (!in_array($searchitem, $tosearch_array))
                    $tosearch_array = $tosearch_tmp;
            }
        }
    }
    else
    {
        $tosearch_array[] = "$q";
    }
}
//---------------------query---------------------------------------------------<
//---------------------perform search------------------------------------------>
if (count($tosearch_array) > 0)
{

    $sections = FN_GetSections("", true);
    $__results = array();
    foreach ($sections as $sectionvalues)
    {
        if ($where != "" && $sectionvalues['id'] != $where)
        {
            continue;
        }
        $search_module_file = "sections/{$sectionvalues['id']}/";
        $search_function_name = "FNSEARCH_section_{$sectionvalues['id']}";
        if ($sectionvalues['type'] != "")
        {
            $search_module_file = "modules/{$sectionvalues['type']}/";
            $search_function_name = "FNSEARCH_module_{$sectionvalues['type']}";
        }
        //--------------search in section title-------------------------------->
        $found = false;
        $lang_to_search = $_FN['lang'];
        foreach ($tosearch_array as $texttofind)
        {
            if (false !== strpos(strtolower($sectionvalues['title']), strtolower($texttofind)) ||
                false !== strpos(strtolower($sectionvalues['description']), strtolower($texttofind))   
                    )
            {
                $found = true;
            }
            else
                $found = false;
            if ($found == true && $method == "OR")
            {
                break;
            }
        }
        if ($found)
        {
            $contares++;
            $__results[] = array("title" => $sectionvalues['title'], "link" => FN_RewriteLink("index.php?mod={$sectionvalues['id']}&lang=$lang_to_search"), "text" => $sectionvalues['description']);
        }


        //--------------search in section title--------------------------------<
        if (file_exists($search_module_file . "/search.php"))
        {
            //------------------search.php------------------------------------->
            $results = array();
            $method = $method;
            include_once ($search_module_file . "/search.php");
            $results = array();
            if (function_exists("$search_function_name"))
            {
                $results = $search_function_name($tosearch_array, $method, $sectionvalues, $maxres - $contares);
            }
            if (is_array($results))
            {
                foreach ($results as $result)
                {
                    $__results[] = $result;
                    $contares++;
                }
            }
            //------------------search.php-------------------------------------<			
        }
        else
        {
            foreach ($_FN['listlanguages'] as $lang_to_search)
            {
                if ($contares > $maxres)
                    break;
                if (file_exists("$search_module_file/section.$lang_to_search.html"))
                {
                    foreach ($tosearch_array as $texttofind)
                    {
                        $text_section = strip_tags(file_get_contents("$search_module_file/section.$lang_to_search.html"));
                        $text_section = str_replace("&nbsp;", " ", $text_section);
                        $pos = strpos(strtolower($text_section), strtolower($texttofind));
                        $textres = "";
                        if ($pos === false)
                        {
                            $found = false;
                        }
                        else
                        {
                            //start and end text position --------------------->
                            $startpos = $pos - 8;
                            if ($startpos < 0)
                                $startpos = 0;
                            for ($i = 1; $startpos >= 0; $i++)
                            {
                                if ($text_section[$startpos] == " " || $text_section[$startpos] == "." || $i > 10)
                                {
                                    break;
                                }
                                $startpos--;
                            }
                            $endpos = 200;
                            for ($i = 1; isset($text_section[$pos + $endpos]); $i++)
                            {
                                if ($text_section[$pos + $endpos] == " " || $text_section[$pos + $endpos] == "." || $i > 20)
                                {

                                    break;
                                }
                                $endpos++;
                            }
                            $textresTMP = substr($text_section, $startpos, ($pos - $startpos) + $endpos);
                            //start and end text position ---------------------<
                            $textres = $textresTMP;
                            $found = true;
                        }
                        if ($found == true && $method == "OR")
                        {
                            break;
                        }
                    }
                    if ($found)
                    {
                        $contares++;
                        $__results[] = array("title" => $sectionvalues['title'], "link" => FN_RewriteLink("index.php?mod={$sectionvalues['id']}&lang=$lang_to_search"), "text" => $textres);
                    }
                }
            }
        }
    }
    //print results ----------------------------------------------------------->
    if (count($__results) > 0)
    {
        echo "<h3>" . FN_i18n("search results", false, "Aa") . ":</h3>";
        foreach ($__results as $result)
        {
            echo "<div class=\"search_item_result\" ><a class=\"search_item_title\" href=\"{$result['link']}\">{$result['title']}</a>";
            foreach ($tosearch_array as $texttofind)
            {
                $result['text'] = FN_FixEncoding($result['text']);
                $result['text'] = @html_entity_decode(strip_tags($result['text']), ENT_QUOTES, $_FN['charset_page']);
                $result['text'] = preg_replace("/$texttofind/is", "___SPANSTART___{$texttofind}___SPANEND___", $result['text']);
                $result['text'] = preg_replace("/___SPANSTART___/is", "<span style=\"background-color:#ffff00;color:#000000\">", $result['text']);
                $result['text'] = preg_replace("/___SPANEND___/is", "</span>", $result['text']);
            }
            echo "<div class=\"search_item_text\" >{$result['text']}</div>";
            echo "</div>";
        }
    }
    else
    {
        echo FN_i18n("no result");
    }
    //print results -----------------------------------------------------------<	
}
//---------------------perform search------------------------------------------>
?>
