Location: PHPKode > projects > Pindorama > pindorama/etc/includes/search-component.php
<?php
global $sc;
$sc["swish_dir"] = (is_file($sc["swish_dir"]."swish-e"))?($sc["swish_dir"]):("");
if ($sc['method'] == "") { $sc['method'] = "and"; if ($_GET["method"] == "or") { $sc['method'] = "or"; } }
if (!is_array($sc['restrict'])) { $sc['restrict'] = $_GET["restrict"]; }
if (!is_array($sc['exclude'])) { $sc['exclude'] = $_GET["exclude"]; }
if ($sc['sort'] == "") { if ($_GET["sort"] != "" && $_GET["sort"] != "score") { $sc['sort'] = clean_arg($_GET["sort"], FALSE); } }
if ($sc["meta"] == "") { $sc["meta"] = $_GET["meta"]; }
$sc["hits-per-page"] = 10;
if ($sc["maximun"] == "" || $sc["maximun"] <= 0) { $sc["maximun"] = $sc["hits-per-page"]; }
if ($sc["words"] == "") { $sc["words"] = $_GET["words"]; } if (is_array($sc["words"])) { $sc["words"] = implode(" ", $sc["words"]); } $sc["words"] = stripslashes($sc["words"]);
$sc["dates"]  = $_GET["date"];
$sc["begin"] = (int)$_GET["begin"]; if (!is_int($sc["begin"]) || $sc["begin"] <= 0) { $sc["begin"] = 1; }
if ($sc["index"] == "") { $sc["index"] = "index.swish-e"; } $sc["index"] = $sc["index_dir"].$sc["index"];
$sc["keys"] = "swishreccount swishrank swishdocpath swishtitle swishdocsize swishlastmodified swishdescription title date subject description extent publisher identifier format language source creator";
$sc["sys"] = $_ENV["OS"]; if (preg_match("/win/i", $sc["sys"])) { $sc["sep"] = "\""; $sc["win"] = FALSE; } else { $sc["sep"] = "\""; $sc["win"] = TRUE; }
$sc["aliases"] = array( "swishtitle"=>"title", "swishlastmodified"=>"date", "swishdescription"=>"subject", "swishdocsize"=>"extent");
$sc["host"] = ($sc["host"] == "")?($_SERVER["HTTP_HOST"]):($sc["host"]);
$sc["host"] = (substr($sc["host"], 0, 7) == "members" || substr($sc["host"], 0, 7) == "forms")?($sc["host"]):("www".substr($sc["host"], strpos($sc["host"], ".")));
$sc["scheme"] = ($_SERVER["SERVER_PORT"] == "443")?("https"):("http");
if ($sc["template"] == "") { $sc["template"] = <<<EOF
<div class="list">
<p class="title"><a href="{$sc["scheme"]}://{$sc["host"]}[[swishdocpath]]">[[title]]</a></p>
<p class="subject">...[[subject]]</p>
<p class="cgray">{$sc["scheme"]}://{$sc["host"]}[[swishdocpath]]</p>
<p class="cgray">[[extent]] &#150; [[date]]</p>
<br />
</div>
EOF;
}
if (!function_exists("search_results")) {

function format_searchresults($arr)
{
    global $sc;
    if (is_array($sc["aliases"])) { foreach ($sc["aliases"] as $k=>$v) { if ($arr[$v] == "" || $arr[$v] == "(NULL)") { $arr[$v] = $arr[$k]; } } }
    $arr["date"] = substr($arr["date"], 0, 10);
    $arr["date"] = date("d M Y", strtotime($arr["date"]));
    $arr["subject"] = ($arr["subject"] != "(null)")?(substr($arr["subject"], 0, strlen($arr["subject"]) -100).preg_replace("/ .*/", "...", substr($arr["subject"], -100))):(substr($arr["swishdescription"], 0, strlen($arr["swishdescription"]) -100).preg_replace("/ .*/", "...", substr($arr["swishdescription"], -100)));
    if ($arr["extent"] < 1024) { $arr["extent"] .= " bytes"; }
    else if ($arr["extent"] < (1024 * 1024)) { $arr["extent"] = round($arr["extent"] / 1024)." Kb"; }
    else { $arr["extent"] = format_number($arr["extent"] / (1024 * 1024), 2)." Mb"; }
    $str = preg_replace("/\[\[([a-z0-9\-]+)\]\]/ie", "\$arr['\\1']", $sc["template"]);
    return $str;
}
function format_resultspages($start, $hits, $hpp)
{
    global $sc;
    if ($hits <= $hpp) {
        return;
    }
    $numpages = 10;
    $url = $_SERVER["PHP_SELF"]."?".preg_replace("/(\&(amp\;)?)?begin\=[0-9]+/", "", $_SERVER["QUERY_STRING"]);
    $pno = ceil($start / $hpp);
    $last = ceil ($hits / $hpp);
    $first = $pno - ($numpages / 2);
    if ($first < 1 || $last <= $numpages) { $first = 1; }
    for($i = $first; ($i <= $last) && ($i < ($first + $numpages)); $i++) {
        $ps = $i * $hpp - $hpp +1;
        if ($sc["begin"] == $ps) {
            $str .= "$i | ";
        } else {
            $str .= "<a href='$url&amp;begin=$ps'>$i</a> | ";
        }
    }
    $str = substr($str, 0, strlen($str) -3);
    $pp = $pno * $hpp - (2* $hpp) +1;
    $np = $pno * $hpp +1;
    $str = ($pno > 1)?("<a href=\"{$url}&amp;begin={$pp}\"><img src=\"/_images/seta_left_on.gif\" width=\"10px\" height=\"10px\" border=\"0\" alt=\"Previous page\" /></a>&#160;&#160;".$str):("<img src=\"/_images/seta_left_off.gif\" width=\"10px\" height=\"10px\" border=\"0\" alt=\"No previous page\" />&#160;&#160;".$str);
    $str .= ($pno < $last)?("&#160;&#160;<a href=\"{$url}&amp;begin={$np}\"><img src=\"/_images/seta_right_on.gif\" width=\"10px\" height=\"10px\" border=\"0\" alt=\"Next page\" /></a>"):("&#160;&#160;<img src=\"/_images/seta_right_off.gif\" width=\"10px\" height=\"10px\" border=\"0\" alt=\"No next page\" />");
    $str = "<p class=\"searchresults\">$str</p>";
    return $str;
}

function clean_arg ($str, $bool = TRUE) {
    global $sc;
    $str = strtr(strtolower($str), "àáäâãéèëêíìïîóòöôõúùüûñÿç", "aaaaaeeeeiiiiooooouuuunyc");
	$str = str_replace("\\", "/", $str);
	$str = str_replace("'", "\"", $str);
	$str = preg_replace("/[\r\n\t  ]/", " ", $str);
	if ($bool && $sc["method"] == "or") { $str = preg_replace("/ +/", " or ", $str); }
	$str = preg_replace("/[^a-z0-9\-\.\"\* ]/", " ", $str);
	$str = preg_replace("/\"([^\"]+)\"/", "&quote;\\1&quote;", $str);
	$str = str_replace('"', "", $str);
	$str = preg_replace("/\&quote\;/", "\\\"", $str);
    return $str;
}

function search_results ($words, $date = array(), $keys, $begin = 1, $maximun = 20, $xmlel = "", $meta = "")
{
    global $sc;
    $sc["msg"] = "";
    if (is_array($date)) {
        $date = array("from"=>$date[0], "to"=>$date[1]);
    }
    if ($date["from"] != "" || $date["to"] != "") {
    	$pdate = '/^([0-9]{4})(\-[0-9]{2}){0,2} *$/';
    	if (preg_match($pdate, $date["from"])){
    		$dates .= $date["from"]." ";
    	} else {
    		$dates .= "0000-00-00 ";
    	}
    	if (preg_match($pdate, $date["to"])){
    		$dates .= $date["to"]." ";
    	} else {
    		$dates .= "9999-99-99 ";
    	}
    	$dates = " -L date ".$dates;
    }
    $words_cmd = "";
    if ($words != "") {
        $words_cmd .= "(".clean_arg($words).") ";
    }
    if ($xmlel != "") {
        $words_cmd = clean_arg($xmlel)."=".$words_cmd;
    }
    if (is_array($meta)) {
        foreach ($meta as $k=>$v) {
            if ($v != "") {
                $v = stripslashes($v);
                if ($words_cmd != "") {
                    $words_cmd .= " {$sc['method']} ";
                }
                $words_cmd .= preg_replace("/[^a-z\:0-9]/i", "", $k)."=(".clean_arg($v).")";
                $meta[$k] = htmlspecialchars($v);
            }
        }
    } else if ($meta != "" && $meta != "html") {
    	if ($meta == "body") {
	    	$words_cmd .= " -t B ";
	    } else {
			$words_cmd = clean_arg($meta)."=".$words_cmd;
		}
    }
    if (!is_int($maximun)) {
        $maximun = 20;
    }
    if (!is_int($begin)) {
        $begin = 1;
    }
    if ($sc['sort'] != "") {
        if (preg_match("/^date|title|identifier|creator$/", $sc['sort'])) {
            $sortop = "-s {$sc['sort']} ";
            if ($sc['sort'] != "date") {
                $sortop .= "asc";
            } else {
                $sortop .= "desc";
            }
        }
    }
    $output = "";
    $keys = split(" ", preg_replace("/[ \t]+/", " ", $keys));
    foreach ($keys as $k) {
    	$output .= "<$k>".'\t';
    }
    $output = preg_replace("/\\t$/", "", $output).'\n';

    if ($words_cmd == "" && $dates != "") {
	    $words_cmd = "not blablablablablablablablablaz";
    }
    if (is_array($sc['restrict']) && count($sc['restrict']) > 0) {
        $i = 0;
        $temp = "";
        foreach ($sc['restrict'] as $r) {
            if ($i > 0) {
                $temp .= " or ";
            }
            $temp .= "(".clean_arg($r).")";
            $i++;
        }
        $words_cmd .=  " identifier=(".$temp.") ";
    } else if ($sc['restrict'] != "") {
        $words_cmd .= " identifier=(".clean_arg($sc['restrict']).") ";
    }
    if (is_array($sc['exclude']) && count($sc['exclude']) > 0) {
        $i = 0;
        $temp = "";
        foreach ($sc['exclude'] as $r) {
            if ($i > 0) {
                $temp .= " or ";
            }
            $temp .= "(".clean_arg($r).")";
            $i++;
        }
        $words_cmd .=  " not identifier=(".$temp.") ";
    } else if ($sc['exclude'] != "") {
        $words_cmd .= " not identifier=(".clean_arg($sc['exclude']).") ";
    }
    if ($words_cmd == "") {
        $sc["msg"] = "escreva as palavras";
    } else {
        $cmd = $sc["swish_dir"]."swish-e -f {$sc['sep']}{$sc['index']}{$sc['sep']} -w {$sc['sep']}$words_cmd{$sc['sep']} -x {$sc['sep']}$output{$sc['sep']} -b $begin -m $maximun $dates $sortop";
        exec($cmd, $result);
        $n = 0;
        foreach ($result as $ln) {
        	if (substr($ln, 0, 1) != "#" && $ln != "." && substr($ln, 0, 4) != "err:") {
        		$i = 0;
        		foreach(split("\t", $ln) as $el) {
        		   $res[$n][$keys[$i]] = $el;
        		   $i++;
        		}
        		$n++;
        	} else {
        	    $sc["msg"] .= $ln."\n";
        	}
        }
    }
    if (preg_match("/\# Number of hits\: ([0-9]*)/", $sc["msg"], $matches)) {
        $sc["hits"] = $matches[1];
    }
    if (is_array($res) && count($res) > 0) {
        if (($sc["begin"] + $sc["hits-per-page"]) < $sc["hits"]) {
            $sc["end"] = $sc["begin"] + $sc["hits-per-page"] -1;
        } else {
            $sc["end"] = $sc["hits"];
        }
        $sc["result"] = preg_replace("/\[\[([a-z0-9\-]+)\]\]/ie", "\$sc['\\1']", $sc["results"]);
        $sc["result"] .= format_resultspages($sc["begin"], $sc["hits"], $sc["hits-per-page"]);
        foreach ($res as $tmp) {
            $sc["result"] .= format_searchresults($tmp);
        }
        $sc["result"] .= format_resultspages($sc["begin"], $sc["hits"], $sc["hits-per-page"]);
    } else {
        $sc["result"] = preg_replace("/\[\[([a-z0-9\-]+)\]\]/ie", "\$sc['\\1']", $sc["no-results"]);
    }
    return $res;
}

} // function_exists
$resulttmp = search_results ($sc["words"], $sc["dates"], $sc["keys"], $sc["begin"], $sc["maximun"], "", $sc['meta']);
?>
Return current item: Pindorama