Source for file opensearch.php

Documentation is available at opensearch.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Document
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * OpenSearch class, provides an easy interface to display an OpenSearch document
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Document
  17.  * @see         http://www.opensearch.org/
  18.  * @since       11.1
  19.  */
  20. {
  21.     /**
  22.      * ShortName element
  23.      *
  24.      * required
  25.      *
  26.      * @var    string 
  27.      * @since  11.1
  28.      */
  29.     private $_shortName "";
  30.  
  31.     /**
  32.      * Images collection
  33.      *
  34.      * optional
  35.      *
  36.      * @var    object 
  37.      * @since  11.1
  38.      */
  39.     private $_images array();
  40.  
  41.     /**
  42.      * The url collection
  43.      *
  44.      * @var    array 
  45.      * @since  11.1
  46.      */
  47.     private $_urls array();
  48.  
  49.     /**
  50.      * Class constructor
  51.      *
  52.      * @param   array  $options  Associative array of options
  53.      *
  54.      * @since  11.1
  55.      */
  56.     public function __construct($options array())
  57.     {
  58.         parent::__construct($options);
  59.  
  60.         // Set document type
  61.         $this->_type = 'opensearch';
  62.  
  63.         // Set mime type
  64.         $this->_mime = 'application/opensearchdescription+xml';
  65.  
  66.         // Add the URL for self updating
  67.         $update new JOpenSearchUrl;
  68.         $update->type 'application/opensearchdescription+xml';
  69.         $update->rel 'self';
  70.         $update->template JRoute::_(JUri::getInstance());
  71.         $this->addUrl($update);
  72.  
  73.         // Add the favicon as the default image
  74.         // Try to find a favicon by checking the template and root folder
  75.         $app JFactory::getApplication();
  76.         $dirs array(JPATH_THEMES '/' $app->getTemplate()JPATH_BASE);
  77.  
  78.         foreach ($dirs as $dir)
  79.         {
  80.             if (file_exists($dir '/favicon.ico'))
  81.             {
  82.  
  83.                 $path str_replace(JPATH_BASE '/'''$dir);
  84.                 $path str_replace('\\''/'$path);
  85.  
  86.                 $favicon new JOpenSearchImage;
  87.                 $favicon->data JUri::base($path '/favicon.ico';
  88.                 $favicon->height '16';
  89.                 $favicon->width '16';
  90.                 $favicon->type 'image/vnd.microsoft.icon';
  91.  
  92.                 $this->addImage($favicon);
  93.  
  94.                 break;
  95.             }
  96.         }
  97.     }
  98.  
  99.     /**
  100.      * Render the document
  101.      *
  102.      * @param   boolean  $cache   If true, cache the output
  103.      * @param   array    $params  Associative array of attributes
  104.      *
  105.      * @return  The rendered data
  106.      *
  107.      * @since   11.1
  108.      */
  109.     public function render($cache false$params array())
  110.     {
  111.         $xml new DOMDocument('1.0''utf-8');
  112.         if (defined('JDEBUG'&& JDEBUG)
  113.         {
  114.             $xml->formatOutput true;
  115.         }
  116.  
  117.         // The OpenSearch Namespace
  118.         $osns 'http://a9.com/-/spec/opensearch/1.1/';
  119.  
  120.         // Create the root element
  121.         $elOs $xml->createElementNS($osns'OpenSearchDescription');
  122.  
  123.         $elShortName $xml->createElementNS($osns'ShortName');
  124.         $elShortName->appendChild($xml->createTextNode(htmlspecialchars($this->_shortName)));
  125.         $elOs->appendChild($elShortName);
  126.  
  127.         $elDescription $xml->createElementNS($osns'Description');
  128.         $elDescription->appendChild($xml->createTextNode(htmlspecialchars($this->description)));
  129.         $elOs->appendChild($elDescription);
  130.  
  131.         // Always set the accepted input encoding to UTF-8
  132.         $elInputEncoding $xml->createElementNS($osns'InputEncoding');
  133.         $elInputEncoding->appendChild($xml->createTextNode('UTF-8'));
  134.         $elOs->appendChild($elInputEncoding);
  135.  
  136.         foreach ($this->_images as $image)
  137.         {
  138.             $elImage $xml->createElementNS($osns'Image');
  139.             $elImage->setAttribute('type'$image->type);
  140.             $elImage->setAttribute('width'$image->width);
  141.             $elImage->setAttribute('height'$image->height);
  142.             $elImage->appendChild($xml->createTextNode(htmlspecialchars($image->data)));
  143.             $elOs->appendChild($elImage);
  144.         }
  145.  
  146.         foreach ($this->_urls as $url)
  147.         {
  148.             $elUrl $xml->createElementNS($osns'Url');
  149.             $elUrl->setAttribute('type'$url->type);
  150.  
  151.             // Results is the default value so we don't need to add it
  152.             if ($url->rel != 'results')
  153.             {
  154.                 $elUrl->setAttribute('rel'$url->rel);
  155.             }
  156.             $elUrl->setAttribute('template'$url->template);
  157.             $elOs->appendChild($elUrl);
  158.         }
  159.  
  160.         $xml->appendChild($elOs);
  161.         parent::render();
  162.         return $xml->saveXML();
  163.     }
  164.  
  165.     /**
  166.      * Sets the short name
  167.      *
  168.      * @param   string  $name  The name.
  169.      *
  170.      * @return  JDocumentOpensearch instance of $this to allow chaining
  171.      *
  172.      * @since   11.1
  173.      */
  174.     public function setShortName($name)
  175.     {
  176.         $this->_shortName $name;
  177.  
  178.         return $this;
  179.     }
  180.  
  181.     /**
  182.      * Adds an URL to the OpenSearch description.
  183.      *
  184.      * @param   JOpenSearchUrl  $url  The url to add to the description.
  185.      *
  186.      * @return  JDocumentOpensearch instance of $this to allow chaining
  187.      *
  188.      * @since   11.1
  189.      */
  190.     public function addUrl(JOpenSearchUrl $url)
  191.     {
  192.         $this->_urls[$url;
  193.  
  194.         return $this;
  195.     }
  196.  
  197.     /**
  198.      * Adds an image to the OpenSearch description.
  199.      *
  200.      * @param   JOpenSearchImage  $image  The image to add to the description.
  201.      *
  202.      * @return  JDocumentOpensearch instance of $this to allow chaining
  203.      *
  204.      * @since   11.1
  205.      */
  206.     public function addImage(JOpenSearchImage $image)
  207.     {
  208.         $this->_images[$image;
  209.  
  210.         return $this;
  211.     }
  212. }
  213.  
  214. /**
  215.  * JOpenSearchUrl is an internal class that stores the search URLs for the OpenSearch description
  216.  *
  217.  * @package     Joomla.Platform
  218.  * @subpackage  Document
  219.  * @since       11.1
  220.  */
  221. {
  222.     /**
  223.      * Type item element
  224.      *
  225.      * required
  226.      *
  227.      * @var    string 
  228.      * @since  11.1
  229.      */
  230.     public $type = 'text/html';
  231.  
  232.     /**
  233.      * Rel item element
  234.      *
  235.      * required
  236.      *
  237.      * @var    string 
  238.      * @since  11.1
  239.      */
  240.     public $rel = 'results';
  241.  
  242.     /**
  243.      * Template item element. Has to contain the {searchTerms} parameter to work.
  244.      *
  245.      * required
  246.      *
  247.      * @var    string 
  248.      * @since  11.1
  249.      */
  250.     public $template;
  251. }
  252.  
  253. /**
  254.  * JOpenSearchImage is an internal class that stores Images for the OpenSearch Description
  255.  *
  256.  * @package     Joomla.Platform
  257.  * @subpackage  Document
  258.  * @since       11.1
  259.  */
  260. {
  261.     /**
  262.      * The images MIME type
  263.      *
  264.      * required
  265.      *
  266.      * @var    string 
  267.      * @since  11.1
  268.      */
  269.     public $type = "";
  270.  
  271.     /**
  272.      * URL of the image or the image as base64 encoded value
  273.      *
  274.      * required
  275.      *
  276.      * @var    string 
  277.      * @since  11.1
  278.      */
  279.     public $data = "";
  280.  
  281.     /**
  282.      * The image's width
  283.      *
  284.      * required
  285.      *
  286.      * @var    string 
  287.      * @since  11.1
  288.      */
  289.     public $width;
  290.  
  291.     /**
  292.      * The image's height
  293.      *
  294.      * required
  295.      *
  296.      * @var    string 
  297.      * @since  11.1
  298.      */
  299.     public $height;
  300. }

Documentation generated on Tue, 19 Nov 2013 15:09:48 +0100 by phpDocumentor 1.4.3