<?php
        
/////////////////////////////////////////////////////////
        //
        //  AHNAR CSA3004 APT University of Malta
        //  Ian Bugeja 2005
        //
        //////////////////////////////////////////////////////////

        
session_start();
        
header("Cache-control: private"); // IE 6 Fix.
    
        
$keyquery "";
        
//out of array of words compute best search string

        
require_once "AHNAR_Tools.php";
        
$ahtools = new AHNAR_Tools();
        
$wordsarr $ahtools->KeywordSort($_SESSION['keywords']);
        
$maxage $ahtools->maxage;
        
$minage $ahtools->minage;

        
//Compute Query 7 words MAX
        
if (isset($wordsarr))
        {
             
reset($wordsarr);
             
$count count($wordsarr);
             
$j 0;
             
$numflag false;
             
$visited count($_SESSION['visited']); //number of visited sites
             
for ($i=0$i $count$i++)
             {
                if (
is_numeric($wordsarr[$i][0])) //only one numeric value allowed in string...first one ...the most popular 1
                
{
                        if (
$numflag == false)
                        {
                                
$keyquery .= ' '.$wordsarr[$i][0];
                                
$j++;
                                
$numflag true;
                        }
                }
                elseif (
$i 2//first two are choosen for sure
                
{
                        
$keyquery .= ' '.$wordsarr[$i][0];
                        
$j++;
                }
                elseif ((
$wordsarr[$i][3] > $minage + (($maxage $minage) / 2) ) && ($wordsarr[$i][4] < $visited 1.5) && ($wordsarr[$i][2] != 1))
                {
                        
//too old and rarely updated and not updated the last round
                
}
                elseif (
$wordsarr[$i][1] <= 3)
                {
                        
//occurance too low
                
}
                elseif ((
$wordsarr[$i][3] >= $maxage 0.6) && ($j 5) && ($wordsarr[$i][1] > 1) && ($wordsarr[$i][2] == 1))
                {   
//age should be > maxage* 0.6, occurance should be > 1  AND ?? updated last time too
                        
$keyquery .= ' '.$wordsarr[$i][0];
                        
$j++;
                }
             }

             
//Reorganise keyword array ... remove stuff
             
$count count($wordsarr);
             for(
$i=0$i<$count$i++)
             {
                if ((
$wordsarr[$i][1] < $visited) && ($wordsarr[$i][3] > 3))
                {
                        
//total occurance less than 1 per each visited pages (approx)
                        //but age > 3
                        
array_splice($wordsarr$i1); //REMOVE
                
}
                elseif ((
$wordsarr[$i][3] > $minage + (($maxage $minage) / 2) ) && ($wordsarr[$i][4] < $visited 1.5) && ($wordsarr[$i][2] != 1))
                {
                        
//too old and rarely updated and not updated the last round
                        
array_splice($wordsarr$i1);  //REMOVE
                
}

                
$count count($wordsarr);
             }
             
$_SESSION['keywords'] = $wordsarr//REUPDATE keywords in session
        
}

        
$keyquery trim($keyquery);
        
$titles $_SESSION['titles'];
        
$poptitle end($titles);

        if (
$keyquery == "")  //above algo gave no results use last page title
                
$keyquery $poptitle;
        elseif (
strlen($keyquery) < 10//if query is less than 10 characters (ie approx less than 2 words) use page titles
        
{
                
$keyquery .= " ".$poptitle;
        }

        
$keyquery urlencode(trim($keyquery));
        if (
$keyquery != "")
        {
                
//redirect to google.php with correct query           dirname($_SERVER['PHP_SELF']) .
                
header("Location: http://" $_SERVER['HTTP_HOST'] . "/google.php?ahnaralso=" $keyquery);
                exit;
        }
        else
        {
                echo 
"Cannot compute a search string. Please continue browsing till a search string can be found";
        }
?>