<?php
/* Open search server for PHP
* @author AliReza Ghafouri aka F0ruD (hide@address.com)
* @package PHP Open Search
* @name OpenSearch for PHP
* @link http://www.opensearch.org/Home
*
*/
/* OSDescription document class
* @package PHP Open Search
*/
class OSDescriptionDoc {
//Scope private
/*
* @access private
*/
var $_content_type='application/opensearchdescription+xml';
/*
* @access private
*/
var $_short_name;
/*
* @access private
*/
var $_description;
/*
* @access private
*/
var $_contact=NULL;
/*
* @access private
*/
var $_tags=NULL;
/*
* @access private
*/
var $_long_name=NULL;
/*
* @access private
*/
var $_developer=NULL;
/*
* @access private
*/
var $_attribution=NULL;
/*
* @access private
*/
var $_syndication_right=NULL;
/*
* @access private
*/
var $_adult_content=NULL;
/*
* @access private
*/
var $_referrer=false;
/*
* @access private
*/
var $_time=false;
/*
* @access private
*/
var $_urls=array();
/*
* @access private
*/
var $_images=array();
/*
* @access private
*/
var $_languages=array();
/*
* @access private
*/
var $_input_encoding=array();
/*
* @access private
*/
var $_output_encoding=array();
/*
* @access private
*/
var $_query=array();
/**
* OSDescriptionDoc() - Create new OSDescriptionDoc object.
*
*
* @param string $short_name short name of search engine
* @param string $description search engine description.
*/
function OSDescriptionDoc($short_name,$description){
$this->_short_name=$short_name;
$this->_description=$description;
}
/**
* need_referrer_extension() - need to use referer extension .
* by enabling this you can use {referrer:source?} in search template.
* some client dose not support this!!
* @param bool $need need referer extension or not.
*/
function need_referrer_extension($need=true){
$this->_referrer=$need;
}
/**
* need_time_extension() - need to use time extension .
* by enabling this you can use {time:start?} and {time:stop?} in search template.
* some client dose not support this!!
* @param bool $need need time extension or not.
*/
function need_time_extension($need=true){
$this->_time=$need;
}
/**
* set_short_name() - set short name .
*
* @param string $short short name.
*/
function set_short_name($short){
$this->_short_name=$short;
}
/**
* get_short_name() - get short name .
*
* @return string short name.
*/
function get_short_name(){
return $this->_short_name;
}
/**
* get_content_type() - get content type of this document, default application/opensearchdescription+xml .
*
* @return string content type of this document.
*/
function get_content_type(){
return $this->_content_type;
}
/**
* set_short_name() - set content type of this document, default application/opensearchdescription+xml .
*
* @param string $content content type of this document.
*/
function set_content_type($content){
$this->_content_type=$content;
}
/**
* add_url() - Add a url element to this description document.
*
* @param string $template - The URL template to be processed according to the OpenSearch URL template syntax.
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.
* @param string $rel - The role of the resource being described in relation to the description document.
* Contains a space-delimited list of valid rel value tokens. See the Url rel values specification for allowed rel values.
* Default: "results"
* suggestion and collection and self can be added via @link(add_suggestions) and @link(add_collection) and @link(add_self)
* can be one of this :
* request :Represents the search query that can be performed to retrieve the same set of search results.
* example :Represents a search query that can be performed to demonstrate the search engine.
* related :Represents a search query that can be performed to retrieve similar but different search results.
* correction :Represents a search query that can be performed to improve the result set, such as with a spelling correction.
* subset :Represents a search query that will narrow the current set of search results.
* superset :Represents a search query that will broaden the current set of search results.
* @param int $indexOffset - The index number of the first search result. The value must be an integer.
* Default: "1"
* @param int $pageOffset - The page number of the first set of search results. The value must be an integer.
* Default: "1"
*/
function add_url($template,$type,$rel="results",$indexOffset =1,$pageOffset=1){
$append='';
if ($rel!="results")
$append.=" rel=\"".$rel."\"";
if ($indexOffset!=1)
$append.=" indexOffset=\"".intval($indexOffset)."\"";
if ($pageOffset!=1)
$append.=" pageOffset=\"".intval($pageOffset)."\"";
$this->_urls[]="<Url type=\"$type\" template=\"$template\"$append />";
}
/**
* add_suggestions() - Add a url element contain suggestion link .
*
* @param string $template - The URL template to be processed according to the OpenSearch URL template syntax.
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.default ('application/x-suggestions+json')
* @param int $indexOffset - The index number of the first search result. The value must be an integer.
* Default: "1"
* @param int $pageOffset - The page number of the first set of search results. The value must be an integer.
* Default: "1"
*/
function add_suggestions($template,$type='application/x-suggestions+json',$indexOffset =1,$pageOffset=1){
$this->add_url($template,$type,'suggestions',$indexOffset,$pageOffset) ;
}
/**
* add_collection() - Add a url element contain request for a set of resources. .
*
* @param string $template - The URL template to be processed according to the OpenSearch URL template syntax.
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.
* @param int $indexOffset - The index number of the first search result. The value must be an integer.
* Default: "1"
* @param int $pageOffset - The page number of the first set of search results. The value must be an integer.
* Default: "1"
*/
function add_collection($template,$type,$indexOffset =1,$pageOffset=1){
$this->add_url($template,$type,'collection',$indexOffset,$pageOffset) ;
}
/**
* add_self() - Add a url element contain self link .
*
* @param string $template - The URL template to be processed according to the OpenSearch URL template syntax.
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.default ('application/opensearchdescription+xml')
*/
function add_self($template,$type='application/opensearchdescription+xml'){
$this->add_url($template,$type,'self');
}
/**
* add_image() - Add a image to this OS Description document
*
* @param string $content - link to image for this OSDD (By my tests must be complete not relative.).
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.default .
* @param int $height the height of image.
* @param int $width the width of image
*/
function add_image($content,$type='',$height=0,$width=0){
$append='';
//$type must be valid MIME type!
if ($type!=='')
$append.=" type=\"$type\"";
if (intval($height)>0)
$append.=" height=\"".intval($height)."\"";
if (intval($width)>0)
$append.=" width=\"".intval($width)."\"";
$this->_images[]="<Image$append>$content</Image>" ;
}
/**
* add_image_base64() - Add a image to this OS Description document in base64 format. not described in opensearch.org but used in many :)
*
* @param string $content_file - address of image for this OSDD
* @param string $type - The MIME type of the resource being described. The value must be a valid MIME type.default .
* @param int $height the height of image.
* @param int $width the width of image
*/
function add_image_base64($content_file,$type='',$height=0,$width=0){
$content='data:image/x-icon;base64,'.base64_encode(file_get_contents($content_file));
$this->add_image($content,$type,$height,$width);
}
/**
* add_query() - Defines a search query that can be performed by search clients. Please see the OpenSearch Query element specification for more information.
*
* @param string $role - one of valid roles : 'request','example','related','correction','subset','superset'
* @param array $optionals like searchTerms="cat", custom:color="blue", title="Sample search" and more see specification for more information
*/
function add_query($role,$optionals=array()){
$valid_role=array('request','example','related','correction','subset','superset');
if (!strpos($role,':'))
if (!in_array($role,$valid_role,true)) return;
$append='';
foreach ($optionals as $opt=>$val)
$append.=" $opt=\"$val\"";
$this->_query[]="<Query role=\"$role\"$append></Query>";
}
/**
* set_syndication_right() - Contains a value that indicates the degree to which the search results provided by this search engine can be queried, displayed, and redistributed.
*
* @param string $value - one of valid values : 'open','limited','private','closed'
*/
function set_syndication_right($value=NULL){
$accepted_value=array('open','limited','private','closed');
if (in_array($value,$accepted_value,true))
$this->_syndication_right=$value;
else
$this->_syndication_right=NULL;
}
/**
* set_adult_content() - Contains a boolean value that should be set to true if the search results may contain material intended only for adults.
*
* @param bool $has - has adult content or has not! (My english is so bad!)
*/
function set_adult_content($has){
if ($has)
$this->_adult_content=true;
else
$this->_adult_content=false;
}
/**
* add_language() - Contains a string that indicates that the search engine supports search results in the specified language.
*
* @param string $lang - language code for results like fa - en or ... * means all language
*/
function add_language($lang){
$this->_languages[]=$lang;
$this->_languages=array_unique($this->_languages);
}
/**
* add_input_encoding() - Contains a string that indicates that the search engine supports search requests encoded with the specified character encoding.
*
* @param string $enc - string that indicates that the search engine supports search requests encoded with the specified character encoding. default UTF-8
*/
function add_input_encoding($enc){
$this->_input_encoding[]=$enc;
$this->_input_encoding=array_unique($this->_input_encoding);
}
/**
* add_output_encoding() - Contains a string that indicates that the search engine supports search responses encoded with the specified character encoding.
*
* @param string $enc - string that indicates that the search engine supports search responses encoded with the specified character encoding. default UTF-8
*/
function add_output_encoding($enc){
$this->_output_encoding[]=$enc;
$this->_output_encoding=array_unique($this->_output_encoding);
}
/**
* check_required() - for internal use, check for all valid fields
* @access private
*
*/
function check_required(){
if (count($this->_urls)<1)
trigger_error('The required field "Url" not set.');
}
/**
* serve() - serve the description doc
*
* @param bool $with_header : use header to serve or not?
*/
function serve($with_header=true){
$this->check_required();
if ($with_header)
header("Content-type: ".$this->_content_type);
echo "<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\"";
if ($this->_referrer)
echo "\n\txmlns:referrer=\"http://a9.com/-/opensearch/extensions/referrer/1.0/\"";
if ($this->_time)
echo "\n\txmlns:time=\"http://a9.com/-/opensearch/extensions/time/1.0/\"";
echo ">\n";
echo "\t<ShortName>".$this->_short_name."</ShortName>\n";
echo "\t<Description>".$this->_description."</Description>\n";
foreach ($this->_urls as $url)
echo "\t".$url."\n";
if (!empty($this->_contact))
echo "\t<Contact>".$this->_contact."</Contact>\n";
if (!empty($this->_tags))
echo "\t<Tags>".$this->_tags."</Tags>\n";
if (!empty($this->_long_name))
echo "\t<LongName>".$this->_long_name."</LongName>\n";
foreach ($this->_images as $img)
echo "\t".$img."\n";
foreach ($this->_query as $query)
echo "\t".$query."\n";
if (!empty($this->_developer))
echo "\t<Developer>".$this->_developer."</Developer>\n";
if (!empty($this->_attribution))
echo "\t<Attribution>".$this->_attribution."</Attribution>\n";
if (!empty($this->_syndication_right))
echo "\t<SyndicationRight>".$this->_syndication_right."</SyndicationRight>\n";
if (!empty($this->_adult_content) && $this->_adult_content!==false)
echo "\t<AdultContent>".$this->_adult_content."</AdultContent>\n";
foreach ($this->_languages as $lang)
echo "\t<Language>$lang</Language>\n";
foreach ($this->_input_encoding as $ienc)
echo "\t<InputEncoding>$ienc</InputEncoding>\n";
foreach ($this->_output_encoding as $oenc)
echo "\t<OutputEncoding>$oenc</OutputEncoding>\n";
echo "</OpenSearchDescription>\n";
}
/**
* save_to_file() - serve the description doc to a files
*
* @param string $file_name : file name to save
*/
function save_to_file($file_name){
ob_start();
$this->serve(false);
$content=ob_get_clean();
file_put_contents($file_name,$content);
}
}
/*
* @package PHP Open Search
*/
class OSSuggestions {
/*
* @access private
*/
var $_query_string=NULL;
/*
* @access private
*/
var $_content_type='application/x-suggestions+json';
/*
* @access private
*/
var $_fields=array();
/**
* OSSuggestions() - Create new OSDescriptionDoc object.
*
* @param string $query_string query string for suggest base of.
*/
function OSSuggestions($query_string) {
$this->_query_string=$query_string;
}
/**
* get_content_type() - get content type default 'application/x-suggestions+json'
*
* @return string default content type 'application/x-suggestions+json'.
*/
function get_content_type(){
return $this->_content_type;
}
/**
* set_content_type() - set content type default 'application/x-suggestions+json'
*
* @return string $content return current content type default 'application/x-suggestions+json'.
*/
function set_content_type($content){
$this->_content_type=$content;
}
/**
* add_field() - add new suggestion
*
* @param string $completions suggested completions for the given search term.
* @param string $descriptions human-readable strings that provide additional information or context regarding the suggested completion.
* @param string $query_url URL that should be used by the search client to request the suggested search term at the corresponding position in the completions lists. .
*/
function add_field($completions,$descriptions='',$query_url='' ){
$this->_fields[]=array($completions,$descriptions,$query_url);
}
/**
* serve() - serve the result
*
*/
function serve(){
header("Content-type: ".$this->_content_type);
$complete_a=array();
$desc_a=array();
$query_a=array();
foreach($this->_fields as $f){
$complete_a[]='"'.$f[0].'"';
$desc_a[]='"'.$f[1].'"';
$query_a[]='"'.$f[2].'"';
}
$complete=implode(',',$complete_a);
$desc=implode(',',$desc_a);
$query=implode(',',$query_a);
echo "[\"{$this->_query_string}\",[$complete],[$desc],[$query]]";
}
}
?>