<?PHP
/* 
Copyright (c) 2006 David L Norris <dave@webaugur.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ 


/**
 * Encode each char in a string to its HTML ASCII code.
 * Characters encoded by this function can be decoded
 * by chars_decode().
 *
 * This function will work with PHP >= 3.0.9, 4, 5.
 *
 * @author    Tim Burgan <timburgan@gmail.com>
 * @version    1.0.0
 * @link      http://timburgan.com/
 * @link      http://php.net/manual/function.htmlentities.php
 * @param      string  $string    String to encode
 * @param      bool    $encodeAll Optional. Default is false. If true all
chars are encoded, if false, only non-alpha-numeric and non-space chars are
encoded.
 * @return    string  Returns a string of encoded chars
 */

function chars_encode($string$encodeAll false)
{
   
// declare variables
   
$chars = array();
   
$ent null;
  
   
// split string into array
   
$chars preg_split("//"$string, -1PREG_SPLIT_NO_EMPTY);
  
   
// encode each character
   
for ( $i 0$i count($chars); $i++ )
   {
     if ( 
preg_match("/^(\w| )$/",$chars[$i]) && $encodeAll == false )
         
$ent[$i] = $chars[$i];
     else
         
$ent[$i] = "&#" ord($chars[$i]) . ";";
   }
  
   if ( 
sizeof($ent) < 1)
     return 
"";
  
   return 
implode("",$ent);
}






$index "blogger.idx";

header("Content-type: text/xml");
error_reporting(E_ALL);

print(
'<?xml version="1.0"?>'."\n");
print(
"<results>\n");

if(
$_GET['w']){
    
// sanitize the words
    
$words ereg_replace("[^[:space:]a-zA-Z0-9*=\(\)\"_.-]"""urldecode($_GET['w']));
    
// if anything is left...
    
if($words){
        
// well, this is way complex than I'd hoped but XML needs entities encoded.
        
$query '/usr/local/bin/swish-e -f \''.$index.'\' -w \''.$words.'\' -x\'<swishtitle>:::<swishrank>:::<swishdocpath>:::<swishdocsize>\n\'';
        print(
"<![CDATA[".$query."]]>\n");
        
        
$pipe popen("$query","r");
        while (
$s fgets($pipe,10240)) {
            if( !
ereg("^[\#\.]"$s) ) {
                list(
$title$rank$path$size) = explode(":::"$s);
                
$title =  str_replace("&#45; Web Augur"""str_replace("Augur&#39;s Ramblings&#58;"""chars_encode($title)));
                
$path chars_encode($path);
                
//print("<![CDATA[".$s."]]>\n");
                
print('    <result title="'.$title.'" rank="'.$rank.'" href="'.$path.'" size="'.$size.'"/>'."\n");
            }
        }
        
pclose($pipe);
    }
    else {
        print(
"<![CDATA[No words to query.]]>");
    }
}
else{
    print(
"<![CDATA[No query parameters.  usage: swish-e.php?w=some+words+to+search]]>");
}

print(
"</results>\n");

?>