//
// This application is implemented in the PHP programming language.
//
// The application searches the SIETS storage using HTTP API and returns the
// results in HTML.
//
$SIETS_SERVER = "http://127.0.0.1/cgi-bin/siets/api.cgi";
$SIETS_STORAGE = "test";
$SIETS_USER = "guest";
$SIETS_PASSWD = "guest";
//search query
$query = $_GET["q"];
//current position in results
$curr_position = $_GET["p"];
//data encoding
$encoding = "UTF-8";
//send http header with correct encoding
send_headers($encoding);
//max results on page
$result_on_page = 10;
if (empty($curr_position) || $curr_position < 0) {
$curr_position = 0;
}
//max page from one domain to show
$max_page_from_domain = 2;
$xml_text = file_get_contents($SIETS_SERVER . "?storage=$SIETS_STORAGE&command=search&user=" . urlencode($SIETS_USER) . "&password=" . urlencode($SIETS_PASSWD) . "&query=" . urlencode($query) . "&docs=$result_on_page&offset=$curr_position&from_domain=$max_page_from_domain&encoding=UTF-8");
if ($xml_text == "") {
die("Siets_search error!");
}
//initialize xml to array object
$xml2a = new XMLToArray();
//parse xml
$root_node = $xml2a->parse($xml_text);
//pop root node from array
$siets_reply = array_shift($root_node["_ELEMENTS"]);
//array for storing data from search results
//like total time spent, hits, and so on
$spec_data = array();
// examining SIETS reply elements
foreach ($siets_reply["_ELEMENTS"] as $siets_reply_el) {
if ($siets_reply_el["_NAME"] == "seconds") {
$spec_data[$siets_reply_el["_NAME"]] = $siets_reply_el["_DATA"];
}
// examining SIETS content elements folder
foreach ($siets_reply_el["_ELEMENTS"] as $siets_content) {
$spec_data[$siets_content["_NAME"]] = $siets_content["_DATA"];
$last_domain = '';
foreach($siets_content["_ELEMENTS"] as $results) {
$tit = "";
$others = "";
//parse each document tag from the result set
foreach($results["_ELEMENTS"] as $documents) {
switch ($documents["_NAME"]) {
case "title" :
$tit_array = explode(":_:_:", $documents["_DATA"]);
$tit .= ''.$tit_array[0].'';
break;
case "link" :
$others .= '
Link: '.$documents["_DATA"].'';
break;
case "domain" :
if ($last_domain == $documents["_DATA"])
$blockquote = TRUE;
else
$blockquote = FALSE;
$others .= '
Domain: '.$documents["_DATA"].'';
$last_domain = $documents["_DATA"];
break;
case "rate" :
break;
case "info" :
break;
case "text" :
if (!empty($documents["_DATA"]))
$others .= '
Snippet: '.$documents["_DATA"].'';
break;
}
}
//tab domains
if ($blockquote)
$output .= '
'.$tit.$others.'
';
else
$output .= '
'.$tit.$others.'
';
}
}
}
if ($spec_data["hits"] == 0) {
$output = "Your search $query did not match any documents!";
} else {
//page listing
$from = $curr_position + 1;
$to = $curr_position + strval($spec_data["found"]);
$list_begin_pos=0;
$list_end_pos=$curr_position+($result_on_page*10);
$page_list .= "";
$p = 1;
if($curr_position > ($result_on_page * 10)){
$list_begin_pos=$curr_position-($result_on_page*10);
$p=intval($list_begin_pos/$result_on_page)+1;
}
if ($curr_position > 0) {
$page_list .= " <<Previous ";
}
$more_tag = $spec_data["more"];
if ($more_tag[0] == '=') {
$more = substr($more_tag,1);
} else {
$more = substr($more_tag,1) + 1;
}
for ($i = $list_begin_pos; $i - ($curr_position + $more) < $result_on_page && $i < $list_end_pos; $i+= $result_on_page) {
if($i>=1000) break;
if ($i != $curr_position) {
$page_list .= "$p ";
} else {
$page_list .= "$p ";
}
$p++;
}
if (($result_on_page+$curr_position)-($curr_position+$more) < 10 && $curr_position + $result_on_page < 1000) {
$page_list .= " Next>>";
}
$page_list .= "";
//end of page listing
}
//echo output to client
echo '
Searched for: '.$query.' Results: '.$from.' - '.$to.' from '.$spec_data["hits"].' Search lasted '.$spec_data["seconds"].' seconds |
'.$output.'
'.$page_list.'
';
//#########################################################
// XMLToArray helper class
class XMLToArray
{
//----------------------------------------------------------------------
// private variables
var $parser;
var $node_stack = array();
//----------------------------------------------------------------------
// PUBLIC
// If a string is passed in, parse it right away.
function XMLToArray($xmlstring="")
{
if ($xmlstring) return($this->parse($xmlstring));
return(true);
}
//----------------------------------------------------------------------
// PUBLIC
// Parse a text string containing valid XML into a multidimensional array
// located at root node.
function parse($xmlstring="")
{
// set up a new XML parser to do all the work for us
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "characterData");
// Build a Root node and initialize the node_stack
$this->node_stack = array();
$this->startElement(null, "root", array());
// parse the data and free the parser...
xml_parse($this->parser, $xmlstring);
xml_parser_free($this->parser);
// recover the root node from the node stack
$rnode = array_pop($this->node_stack);
// return the root node
return($rnode);
}
//----------------------------------------------------------------------
// PROTECTED
// Start a new Element. This is done by pushing the new element onto the stack
// and reseting its properties.
function startElement($parser, $name, $attrs)
{
// create a new node
$node = array();
$node["_NAME"] = $name;
foreach ($attrs as $key => $value) {
$node[$key] = $value;
}
$node["_DATA"] = "";
$node["_ELEMENTS"] = array();
// add the new node to the end of the node stack
array_push($this->node_stack, $node);
}
//----------------------------------------------------------------------
// PROTECTED
// End an element. This is done by popping the last element from the
// stack and adding it to the previous element on the stack.
function endElement($parser, $name)
{
// pop this element off the node stack
$node = array_pop($this->node_stack);
$node["_DATA"] = trim($node["_DATA"]);
// and add it an element of the last node in the stack...
$lastnode = count($this->node_stack);
array_push($this->node_stack[$lastnode-1]["_ELEMENTS"], $node);
}
//----------------------------------------------------------------------
// PROTECTED
//Collect the data onto the end of the current chars.
function characterData($parser, $data)
{
// add this data to the last node in the stack...
$lastnode = count($this->node_stack);
$this->node_stack[$lastnode-1]["_DATA"] .= $data;
}
//----------------------------------------------------------------------
}
// End of class
//#########################################################
//sends Content-type header to client browser
function send_headers($encoding)
{
Header("Content-type: text/html;charset=$encoding");
}
?>