<?php

// ***************************************************************************
// * Copyright © 2003 - Thomas Egtvedt - www.evaria.com - thomas@evaria.com  *
// *                                                                         *
// *      ECMS - Content Management System - Support: forum.evaria.com       *
// *                                                                         *
// * This program is commercial software; you can not redistribute/reproduce *
// * it and/or sell it without the prior written consent of www.evaria.com   *
// *                                                                         *
// * This program is distributed in the hope that it will be useful,         *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    *
// ***************************************************************************

// GETTING TIME WHEN SEARCH IS EXECUTED...
$time_deb = microtime();
$time_deb = explode(" ",$time_deb);
$time_deb = $time_deb[0] + $time_deb[1];

// Running pre-defined search...
if ($mots == "MODSEARCH" && isset($HTTP_POST_VARS['a']) && isset($HTTP_POST_VARS['b']) && isset($HTTP_POST_VARS['c'])) {
    $a = $HTTP_POST_VARS['a'];
	$b = $HTTP_POST_VARS['b'];
	$c = $HTTP_POST_VARS['c'];
	$mots = str_replace("MODSEARCH", $a." ".$b." ".$c, $mots);
	}

// Checking length of input (min 2 characters)...
if ($mots == "Your search :")
	$mots = "";
$trop_court = false;

if (strlen(stripslashes($mots)) < 3)
	$trop_court = true;

if (!get_magic_quotes_gpc())
	$mots = addslashes($mots);

if (!empty($page))
	$mots = rawurldecode($mots);

// ADD OR EDIT BAD WORDS HERE...
$mots_cherches = explode(" ",$mots);
$bad_words[] = "";
$bad_words[] = "one";
$bad_words[] = "une";
$bad_words[] = "les";
$bad_words[] = "php";
$bad_words[] = "Enter";
$bad_words[] = "keywords";

// Including the bad words...
$ln_tab = sizeof($mots_cherches);
for ($i=0;$i<$ln_tab;$i++)
{
	if (!is_in_array(trim($mots_cherches[$i]),$bad_words) && strlen($mots_cherches[$i]) > 2)
		$mots_reels_recherches[] = $mots_cherches[$i];
}
$nb_mots = sizeof($mots_reels_recherches);

if ($nb_mots > 0 && !$trop_court)
{	
	if (empty($page))
		$page = 1;
	$debut = ($page - 1) * $nb_results2show;
	$sql_sites = "SELECT s.*, cat_name, TO_DAYS(CURRENT_DATE) - TO_DAYS(date_validation) AS dif_days FROM $T_sites AS s, $T_categories AS c WHERE s.id_cat = c.id AND s.activation = 1 AND c.activation = 1 AND (";
	$cond_sites = "";
	for ($i=0;$i<$nb_mots;$i++)
	{
		$cond_sites .= " (titre LIKE '%$mots_reels_recherches[$i]%' OR description LIKE '%$mots_reels_recherches[$i]%' OR url LIKE '%$mots_reels_recherches[$i]%') ";
		if ($i != ($nb_mots - 1))
			$cond_sites .= $booleen;
	}
	$sql_sites .= $cond_sites;
	$sql_sites .= ") ORDER BY niveau ASC,titre ASC LIMIT $debut,$nb_results2show ";
		
	if ($page == 1)
	{
		$sql_cats = "SELECT * FROM $T_categories WHERE activation = 1	AND (";
		$sql_nb_sites = "SELECT count(s.id) AS count FROM $T_sites AS s, $T_categories AS c	WHERE s.id_cat = c.id AND s.activation = 1 AND c.activation = 1 AND (";
		for ($i=0;$i<$nb_mots;$i++)
		{
			$sql_cats .= " (cat_name LIKE '%$mots_reels_recherches[$i]%' OR texte LIKE '%$mots_reels_recherches[$i]%') ";
			if ($i != ($nb_mots - 1))
				$sql_cats .= $booleen;
		}
		$sql_nb_sites .= $cond_sites;
		$sql_nb_sites .= ") ";
		$sql_cats .= ") ORDER BY cat_name ASC ";
	}	
	$sql_admin = "SELECT nb_days2benew FROM $T_infos ";

	$lk = connection_BD();
	$res_admin = send_sql($sql_admin,"finding number of days that you have defined as new link");
	$nb_days2benew = mysql_result($res_admin,0,"nb_days2benew");
	$res_sites = send_sql($sql_sites,"finding the link search results");
	if ($page == 1)
	{
		$res_nb_sites = send_sql($sql_nb_sites,"finding the number of links found");
		$res_cats = send_sql($sql_cats,"finding the category search results");	
		$nb_cats_trouvees = mysql_num_rows($res_cats);
		$nb_sites_trouves = mysql_result($res_nb_sites,0,count);
	}
	// Calculating number of result-pages...
	$nb_pages = ceil($nb_sites_trouves / $nb_results2show);
	
	// GETTING TIME WHEN SEARCH IS ENDED...
	$time_fin = microtime();
	$time_fin = explode(" ",$time_fin);
	$time_fin = $time_fin[0] + $time_fin[1];

	$time_search = $time_fin - $time_deb; 		
	if ($page == 1)
	{
    ?>

<TABLE class="link_results" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="left"><b>Search results for:</b> <? printf ("%s\n",stripslashes($mots)); ?>...</TD>
  </TR>
</TABLE>
<TABLE class="link_sresult" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="left">Found: <i><? printf ("%d categories and %d links\n",$nb_cats_trouvees,$nb_sites_trouves); ?></i></TD>
	<TD align="right">Generated in: <? printf ("%.3f s\n",$time_search); ?></TD>
  </TR>
</TABLE>
<br>

		<?
	}

	if ($page == 1 && $nb_cats_trouvees)
	//si on a trouve des categories
	{
		?>

<TABLE class="link_results" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="left">Displaying categories:</TD>
  </TR>
</TABLE>
<br>
<TABLE align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">

		<?
		while ($resultat = mysql_fetch_array($res_cats,MYSQL_ASSOC))
		{
			?>
			
  <TR>
    <TD align="left">

			<?
			$arbo = show_arbo_en_ligne($resultat["id"],"?view=link_home"," » ");
			for ($i=0;$i<$nb_mots;$i++)
				$arbo = keyword_in_bold($mots_reels_recherches[$i],$arbo);
			echo $arbo;
		}
		?>

</TABLE>
<br><br>

		<?
	}
	if ($nb_sites_trouves)
	// Displaying found links...
	{
		?>
<TABLE class="link_results" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD>Displaying links:

		<?
		$fin = $page*$nb_results2show;
		if ($fin > $nb_sites_trouves)
			$fin = $nb_sites_trouves;
		echo ($debut+1)." - ".$fin;
		if ($page != 1)
			echo "</span></TD><TD align=right>Total: ".$nb_sites_trouves."\n";
		if ($page == 1)
			echo "</span></TD><TD align=right>Total: ".$nb_sites_trouves."\n";
		?>

    </TD>
  </TR>
</TABLE>
<br>
<TABLE align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD>

		<?
		while ($resultat = mysql_fetch_array($res_sites,MYSQL_ASSOC))
		{
			for ($i=0;$i<$nb_mots;$i++)
			{
				$resultat["titre"] = keyword_in_bold($mots_reels_recherches[$i],$resultat["titre"]);
				$resultat["description"] = keyword_in_bold($mots_reels_recherches[$i],$resultat["description"]);
			}
			affiche_site($resultat,"search");
		}
		echo "    </TD>\n  </TR>\n</TABLE>\n";
		if ($nb_sites_trouves > $nb_results2show)
		{
			?>

<TABLE class="link_results" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="right"><span class="link_page_number">Go to results page »

			<?
			for ($i=1;$i<$nb_pages;$i++)
			{
				if ($i != $page)
					echo "<a class=\"link_page_nav\" href=\"?view=link_search&mots=".rawurlencode($mots)."&page=".$i."&nb_results2show=".$nb_results2show."&booleen=".$booleen."&nb_sites_trouves=".$nb_sites_trouves."\">".$i."</a>\n";	else
				{ echo "$i"; }
				echo " - ";
			}
			if ($i == $page)
				echo $i;
			else
			{
            echo "<a class=\"link_page_nav\" href=\"?view=link_search&mots=".rawurlencode($mots)."&page=".$i."&nb_results2show=".$nb_results2show."&booleen=".$booleen."&nb_sites_trouves=".$nb_sites_trouves."\">".$i."</a>\n"; }
			?>

    </span></TD>
  </TR>
</TABLE>
<br>

			<?
		}	
	}

	if (!($nb_cats_trouvees + $nb_sites_trouves)) // No links found...
	{
		?>
		<center>
		<br><br><br><br>
		<span class="link_error">
		No match found...
		<br><br><br><br>
		<input type="button" value="  Go to advanced search ...  " onclick="openLink('?view=link_search_form')">
		</span>
		</center>
		<?
	}
	mysql_close($lk);	
}
//if ($nb_mots > 0)
else //pas de mot demande
{
?>
<TABLE class="link_results" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="left"><b>Search results for:</b> ?</TD>
  </TR>
</TABLE>
<TABLE class="link_sresult" align="center" width="<? echo $table_width; ?>" cellpadding="0" cellspacing="0" border="0">
  <TR>
    <TD align="left">Found: 0 categories and 0 links</TD>
	<TD align="right">Generated in: 0.00001 s</TD>
  </TR>
</TABLE>
<center>
<br><br><br><br>
<span class="link_error">
You have either not entered any data at all,<br>
or entered to short keyword (min 3 characters),<br>
or used an illegal word banned in our database.
<br><br><br><br>
<input type="button" value="  Go to advanced search ...  " onclick="openLink('?view=link_search_form')">
</span>
</center>	
<?
}
?>

