<?



$q 
= isset($_GET['q'])?$_GET['q']:'';

$q preg_replace('/ OR /',' | ',$q);

$q preg_replace('/[^\w~\|\(\)\^\$\?-]+/',' ',trim(strtolower($q)));




if (empty(
$q)) {
    die(
'no query');
}


//not needed as we strip all funny chars anyway!
#$q = mysql_real_escape_string();

    

    
$qo $q;
    if (
strlen($qo) > 64) {
        
$qo '--complex query--';
    } 
    
    if (
1) {
        
//text query
        
if (!empty($_GET['h'])) {
            print 
"<div style=\"text-align:right\">Results for ".htmlentities($qo)."</div>";
        }    
        
// --------------
        
require ( "sphinxapi.php" );
        
        
$mode SPH_MATCH_ALL;
        if (
strpos($q,'~') === 0) {
            
$q preg_replace('/^\~/','',$q);
            if (
substr_count($q,' ') > 1//over 2 words
                
$mode SPH_MATCH_ANY;
        } elseif (
strpos($q,'-') !== FALSE || strpos($q,'|') !== FALSE) {
            
$mode SPH_MATCH_EXTENDED;
        } 
        
$host "localhost";
        
$port 1238;
        
$index "geostemmed";
        
$cl = new SphinxClient ();
        
$cl->SetServer $host$port );
        
$cl->SetWeights ( array ( 100) );
        
$cl->SetSortMode SPH_SORT_EXTENDED"@relevance DESC, @id DESC" );
        
$cl->SetMatchMode $mode );
        
$cl->SetLimits(0,25);
        
$res $cl->Query $q$index );
        
        
// --------------
        
        
if ( $res===false )
        {
            print 
"\tQuery failed: -- please try again later.\n";
            exit;
        } else
        {
            if ( 
$cl->GetLastWarning() )
                print 
"\nWARNING: " $cl->GetLastWarning() . "\n\n";
        
            
$query_info "Query '$qo' retrieved ".count($res['matches'])." of $res[total_found] matches in $res[time] sec.\n";
            
        
        }
        
        if (
is_array($res["matches"]) ) {
        
            
$ids array_keys($res["matches"]);
            
            
$where "gridimage_id IN(".join(",",$ids).")";
        
            
$sql "SELECT gridimage_id,grid_reference,title,realname,user_id
            FROM gridimage_search
            WHERE 
$where
            LIMIT 60"
;
        } else {
            
$r "\t--none--";
        }
            
    }
    
    if (
$sql) {
        require_once(
"mysql-config.inc.php");
        
        
$result mysql_query($sql) or die ("Couldn't select query : $sql " mysql_error() . "\n");
        
$r '';
        if (
mysql_num_rows($result) > 0) {
            
?><link rel="stylesheet" type="text/css" href="search-service.css"><base target="geowindow"><?
            $rows 
= array();
            while (
$row mysql_fetch_array($result,MYSQL_ASSOC)) {
                
$rows[$row['gridimage_id']] = $row;
            }
            foreach (
$ids as $c => $id) {
                
$row $rows[$id];
                
                
?>
                <div class="a">
                <div class="b">[[[<?= $row['gridimage_id'?>]]]</div>
                <div align="center">
                <a title="<?= $row['grid_reference'?> : <? echo htmlspecialchars2($row['title']); ?> by <? echo htmlspecialchars2($row['realname']); ?> - click to view full size image" href="http://www.geograph.org.uk/photo/<?= $row['gridimage_id'?>"><img <? if ($c 14) { print 'alt="show image" onmouseover="this.src = this.lang;" lang="'; } else { echo 'src="';} echo $url?>" border="0"/></a></div>
                </div>
                <?
            
}
            print 
"<br style='clear:both'><pre>$query_info</pre>";
        } else {
            print 
"--none--";
        }
    }