/libraries/joomla/document/opensearch/opensearch.php

https://gitlab.com/vitaliylukin91/text · PHP · 312 lines · 117 code · 41 blank · 154 comment · 9 complexity · 0215b92943971ab1a2f8bc904db06a50 MD5 · raw file

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