<?php

include "funcs.php";
header("Cache-Control: public");

if (!ValidateReferer()) {
	header("Location: index$Lingua.php");
	die();
}


$filtro = $HTTP_POST_VARS["search"];
$filtroJava = MakeClean($filtro);
$metodo = strtolower($HTTP_POST_VARS["method"]);
$campo = strtolower($HTTP_POST_VARS["field"]);
if (!isset($campo)) {
	$campo = "";
}
$matchword = strtolower($HTTP_POST_VARS["match"]);
if (!isset($matchword)) {
	$matchword = "";
}
$npage = $HTTP_POST_VARS["page"];
$sqlquery = $HTTP_POST_VARS["sqlquery"];
$sqlqueryJava = MakeClean($sqlquery);
$sqlquery = str_replace("articoli.","articoli$Lingua.",$sqlquery);

$numart = <<1-search_maxart>>;

$termine = explode(' ', $filtro);
$termini = sizeof($termine) - 1;

$sqlcond = "";

if ($metodo == "all") {
	$operatore = " AND ";
} else {
	$operatore = " OR ";
}

if ($campo == "titolo" || $campo == "sottotitolo") {
	$campo = "plain_" . $campo;
}

if (!$sqlquery) {
	foreach ($termine as $testo) {
		if ($matchword == "w") {
			$testo = quotemeta($testo);
			$testo = str_replace("%","\\%",$testo);
			$testo = addslashes($testo);
			$testo = str_replace("\\\\","\\",$testo);
			$confr = 'REGEXP \'[[:<:]]' .$testo . '[[:>:]]\'';
		} else {
			$testo = addslashes($testo);
			$testo = str_replace("%","\\%",$testo);
			$testo = str_replace("_","\\_",$testo);
			$confr = "LIKE BINARY '%$testo%'";
		}
		if ($sqlcond != "") {
			$sqlcond .= $operatore;
		}
		if ($campo == "") {
			$sqlcond .= "LCASE(CONCAT_WS(' ', dettaglio.codice, articoli$Lingua.plain_titolo, articoli$Lingua.plain_sottotitolo, articoli$Lingua.descrizione, articoli$Lingua.note)) $confr";
		} else {
			$sqlcond .= "LCASE(articoli$Lingua.$campo) $confr";
		}
	}
} else {
	$sqlcond = $sqlquery;
}

$ResultsFileName="results.htm";

$file = fopen($ResultsFileName, "rb");

$TestoResults = fread($file, filesize($ResultsFileName));
fclose($file);

$edStart=strpos(strtoupper($TestoResults), "<ED_START>");
if (is_integer($edStart)) {
	$edStart = $edStart +1;
}
$edStartIn=$edStart+strlen("<ED_START>");
$edEndIn=strpos(strtoupper($TestoResults), "<ED_END>");
if (is_integer($edEndIn)) {
	$edEndIn = $edEndIn +1;
}
$edEnd=$edEndIn+strlen("<ED_END>");
$TestoOut=substr($TestoResults,0,$edStart -1);
print $TestoOut;

$TestoOut=substr($TestoResults, $edStartIn - 1, $edEndIn-$edStartIn);

$artTo=($npage+1)*$numart;
$artFrom=$artTo-$numart+1;

$field = str_replace("0","", "inaccessibile" . $listino);

$sql = "SELECT articoli$Lingua.*, dettaglio.*, ordinamento.contatore, articoli$Lingua.pagina as 'linkPage' FROM articoli$Lingua INNER JOIN dettaglio USING (ID) INNER JOIN ordinamento USING (ID) INNER JOIN categorie ON dettaglio.CatID = categorie.ID ";
$sql .= "WHERE categorie.nascosto = 0 AND categorie.$field = 0 AND ($sqlcond)";

$resultArt = mysql_query($sql);

$artFound = 0;

while ($rowArt = mysql_fetch_array($resultArt)) {
	$artFound = $artFound + 1;
	$fpagina[$artFound]=$rowArt["linkPage"].".php?n=".$rowArt["sottopagina"]."#".$rowArt["contatore"];
	$fcodice[$artFound]=MakeHTML($rowArt["codice"]);
	$ftitolo[$artFound]=MakeHTML($rowArt["plain_titolo"]);
	$fsottotitolo[$artFound]=MakeHTML($rowArt["plain_sottotitolo"]);
	$fimmagine[$artFound]=$rowArt["immagine3"];
}

if ($artTo > $artFound) {$artTo=$artFound;}

for ($i=$artFrom; $i<$artTo +1; $i++) {
	$TestoOut1=$TestoOut;
	$TestoOut1=str_replace('$arg1',$fpagina[$i], $TestoOut1);
	$TestoOut1=str_replace('$arg2',$fcodice[$i], $TestoOut1);
	$TestoOut1=str_replace('$arg3',$ftitolo[$i], $TestoOut1);
	$TestoOut1=str_replace('$arg4',$fsottotitolo[$i], $TestoOut1);
	$TestoOut1=str_replace('$arg5',$fimmagine[$i], $TestoOut1);
	print $TestoOut1;
}

print "<FONT FACE=Arial SIZE=2><<4-articoli_trovati>>: " . $artFound . "</FONT>";

if ($artFound > $numart) {

?>

<SCRIPT LANGUAGE = "JavaScript">

<!--
    function GoToPage(np) {
	document.SearchOn.page.value = np;
	document.SearchOn.submit();
    }
//-->
</SCRIPT>
<FORM NAME="SearchOn" METHOD="POST" ACTION="edsearch.php">
<INPUT TYPE="HIDDEN" NAME="search" VALUE="<?php echo $filtroJava; ?>">
<INPUT TYPE="HIDDEN" NAME="method" VALUE="<?php echo $metodo; ?>">
<INPUT TYPE="HIDDEN" NAME="match" VALUE="<?php echo $matchword; ?>">
<INPUT TYPE="HIDDEN" NAME="field" VALUE="<?php echo $campo; ?>">
<INPUT TYPE="HIDDEN" NAME="sqlquery" VALUE="<?php echo $sqlqueryJava; ?>">
<INPUT TYPE="HIDDEN" NAME="page" VALUE="0">
<FONT FACE="Arial" SIZE="2"><<4-pagine>>:&nbsp;
<?php
$ni = intval(($artFound -1) / $numart);

for ($i=0; $i < $ni +1; $i++) {
	if ($i != $npage) {
	    print " <A HREF=\"javascript:GoToPage(" . $i . ");\">";
	} else {
	    print " <B>";
	}
	$pagnum = $i + 1;
	print $pagnum;
	if ($i != $npage) {
	    print "</A> ";
	} else {
	    print "</B> ";
	}
	if ($i < $ni) {
	    print " - ";
	}
}
print "</FONT></FORM>";

} else {
	print "<p></p>";
}

$TestoOut=substr($TestoResults, $edEnd - 1);
print $TestoOut;
?>
