<?
include("config.php");
include("funzioni_url.php");
include("funzioni_varie.php");

mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
mysql_select_db($db['database']) or die(mysql_error());
$start = gettimeofday();

$urlvisualizza="visualizza.php";
$recordsperpage=25;

?>
<html>
<head>
<title>Gufi.org test!</title>
<link href="gufiml.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000">

<table cellspacing=1 align=center class=tabellona width=70%>
	<tr>
		<td colspan=2 class=evidenziato>
			<span class=titolo>Ricerca nelle Mailing List</span>
		</td>
	</tr>
	<tr><td>&nbsp;</td></tr>
	<tr>
		<td align=right class=normale>
			<form method="GET" action="<?echo $_SERVER['PHP_SELF']?>">
			Parole chiave:
		</td><td>
			<input type="text" name="cerca">
		</td>
	</tr>
	<tr>
		<td align=right class=normale>
			Lista:
		</td>
		<td>
			<select name="ml">
			<option value="gufi_aiuto"<?if($_GET['ml']=="gufi_aiuto") echo " selected"?>>aiuto
			<option value="gufi_annunci"<?if($_GET['ml']=="gufi_annunci") echo " selected"?>>annunci
			<option value="gufi_esperti"<?if($_GET['ml']=="gufi_esperti") echo " selected"?>>esperti
			<option value="gufi_freesbie"<?if($_GET['ml']=="gufi_freesbie") echo " selected"?>>freesbie
			<option value="gufi_irc"<?if($_GET['ml']=="gufi_irc") echo " selected"?>>irc
			<option value="gufi_test"<?if($_GET['ml']=="gufi_test") echo " selected"?>>test
			<option value="gufi_traduzioni"<?if($_GET['ml']=="gufi_traduzioni") echo " selected"?>>traduzioni
			<option value="gufi_varie"<?if($_GET['ml']=="gufi_varie") echo " selected"?>>varie
			</select>
		</td>
	</tr>
	<tr>
		<td colspan=2 class=normale align=center>
		<input type="submit" name="ricerca" value="Cerca">
		</form>
	</td></tr>
	<tr><td>&nbsp;</td></tr>
        <tr>
		<td colspan=2 class=evidenziato align=center>
			<a class=evidenziato href=http://liste.gufi.org/mailman/listinfo>Pagina di informazioni delle Mailing List</a>
		</td>
	</tr>	
</table>
<?
if (isset($_GET['ml']) && strlen(trim($_GET['cerca']))) { ?>
<br>
<table align="center" border=0 cellspacing=1>
<?
	// Sanity check
	switch($_GET['ml']) {
		case 'gufi_aiuto':
		case 'gufi_annunci':
		case 'gufi_esperti':
		case 'gufi_freesbie':
		case 'gufi_irc':
		case 'gufi_test':
		case 'gufi_traduzioni':
		case 'gufi_varie':
		  break;
		default:
		  exit(1);
		  break;
	}

	$keywords = explode(" ",$_GET['cerca']);
	$totkey = count($keywords);
	/*
	$query = "SELECT `id`,`from`,`subject`,UNIX_TIMESTAMP(`date`) as `date`
				FROM `".mysql_escape_string($_GET['ml'])."` WHERE
				MATCH(`from`,`subject`,`body`) AGAINST ('".mysql_escape_string($_GET['cerca'])."')
				LIMIT ".(isset($_GET['page'])?$_GET['page']*$recordsperpage:0).",".($recordsperpage+1)."";
	*/
	// Una prima query per sapere quanto e' il valore massimo del
	// rating.. Non se ne puo' fare a meno? E' lenta =((

	$query = "SELECT MATCH(`from`,`subject`,`body`) AGAINST ('".mysql_escape_string($_GET['cerca'])."') AS `rating`
				FROM `".mysql_escape_string($_GET['ml'])."` WHERE
				MATCH(`from`,`subject`,`body`) AGAINST ('".mysql_escape_string($_GET['cerca'])."')";
				//LIMIT ".(isset($_GET['page'])?$_GET['page']*$recordsperpage:0).",".($recordsperpage+1)."";
		// Group by idthread? Possibile.. magari nelle opzioni
		// GROUP BY(`idthread`) ORDER BY `rating` desc LIMIT 1";
	$res = mysql_query($query) or die(err($query));
	$temp = mysql_fetch_array($res);
	$rating_max = $temp['rating'];

	// Una seconda per prendere i dati veri e propri
	$query = "SELECT `id`,`from`,`subject`,UNIX_TIMESTAMP(`date`) as `date`,
				MATCH(`from`,`subject`,`body`) AGAINST ('".mysql_escape_string($_GET['cerca'])."') AS `rating`
				FROM `".mysql_escape_string($_GET['ml'])."` WHERE
				MATCH(`from`,`subject`,`body`) AGAINST ('".mysql_escape_string($_GET['cerca'])."')
				LIMIT ".(isset($_GET['page'])?$_GET['page']*$recordsperpage:0)."
				,".($recordsperpage+1)."";
	$res = mysql_query($query) or die(err($query));
	$tot = mysql_num_rows($res);

	// Questa non serve
	// print "<tr><td colspan=\"2\">$query</td></tr>";

	$res = mysql_query($query);
	$end = gettimeofday();
	$elapsed = $end["sec"] - $start["sec"] + ($end["usec"] - $start["usec"]) / 1000000;
	$elapsed = round($elapsed * 1000 + .5) / 1000;

	// Questo magari ce lo mettiamo in basso
	// print "<tr><td colspan=\"2\">Tempo query: $elapsed secondi</td></tr>";

	$tot = mysql_num_rows($res);

	for ($i=0; ($d = mysql_fetch_assoc($res)) && ($i < $recordsperpage); $i++) {

		$d['from'] = pulisci_from($d['from']);
		$d['rating'] = rating($d['rating'], $rating_max);

		echo "<tr>
				<td class=risultato".($i%2).">".$d['rating']."</td>
				<td class=risultato".($i%2).">".$d['from']."</td>
				<td class=risultato".($i%2).">
					<a href=\"".$urlvisualizza."?".query_cambia("id", $d['id'])."\">".htmlentities($d['subject'])."</a>
				</td>
				<td class=risultato".($i%2).">".date('d/m/Y, h:i:s',$d['date'])."</td>
			</tr>\n";

	} // for i

		if ($tot>$recordsperpage) {
			echo "
				<tr><td colspan=4 class=evidenziato align=right>
				<a class=evidenziato href=\"".$_SERVER['PHP_SELF']."?cerca=".$_GET['cerca']."&ml=".$_GET['ml'].
				"&ricerca=Sottometti&page=".($_GET['page']+1)."\">Continua</a>
			</td></tr>";
		}

		echo "</table>";

	} // if isset _GET[ml] &&
?>
</body>
</html>
