[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Document; 11 12 use Joomla\CMS\Document\Opensearch\OpensearchImage; 13 use Joomla\CMS\Document\Opensearch\OpensearchUrl; 14 use Joomla\CMS\Factory; 15 use Joomla\CMS\Router\Route; 16 use Joomla\CMS\Uri\Uri; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('JPATH_PLATFORM') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Opensearch class, provides an easy interface to display an Opensearch document 24 * 25 * @link http://www.opensearch.org/ 26 * @since 1.7.0 27 */ 28 class OpensearchDocument extends Document 29 { 30 /** 31 * ShortName element 32 * 33 * required 34 * 35 * @var string 36 * @since 1.7.0 37 */ 38 private $_shortName = ''; 39 40 /** 41 * Images collection 42 * 43 * optional 44 * 45 * @var object 46 * @since 1.7.0 47 */ 48 private $_images = array(); 49 50 /** 51 * The url collection 52 * 53 * @var array 54 * @since 1.7.0 55 */ 56 private $_urls = array(); 57 58 /** 59 * Class constructor 60 * 61 * @param array $options Associative array of options 62 * 63 * @since 1.7.0 64 */ 65 public function __construct($options = array()) 66 { 67 parent::__construct($options); 68 69 // Set document type 70 $this->_type = 'opensearch'; 71 72 // Set mime type 73 $this->_mime = 'application/opensearchdescription+xml'; 74 75 // Add the URL for self updating 76 $update = new OpensearchUrl(); 77 $update->type = 'application/opensearchdescription+xml'; 78 $update->rel = 'self'; 79 $update->template = Route::_(Uri::getInstance()); 80 $this->addUrl($update); 81 82 // Add the favicon as the default image 83 // Try to find a favicon by checking the template and root folder 84 $app = Factory::getApplication(); 85 $dirs = array(JPATH_THEMES . '/' . $app->getTemplate(), JPATH_BASE); 86 87 foreach ($dirs as $dir) { 88 if (is_file($dir . '/favicon.ico')) { 89 $path = str_replace(JPATH_BASE, '', $dir); 90 $path = str_replace('\\', '/', $path); 91 $favicon = new OpensearchImage(); 92 93 if ($path == '') { 94 $favicon->data = Uri::base() . 'favicon.ico'; 95 } else { 96 if ($path[0] == '/') { 97 $path = substr($path, 1); 98 } 99 100 $favicon->data = Uri::base() . $path . '/favicon.ico'; 101 } 102 103 $favicon->height = '16'; 104 $favicon->width = '16'; 105 $favicon->type = 'image/vnd.microsoft.icon'; 106 107 $this->addImage($favicon); 108 109 break; 110 } 111 } 112 } 113 114 /** 115 * Render the document 116 * 117 * @param boolean $cache If true, cache the output 118 * @param array $params Associative array of attributes 119 * 120 * @return string The rendered data 121 * 122 * @since 1.7.0 123 */ 124 public function render($cache = false, $params = array()) 125 { 126 $xml = new \DOMDocument('1.0', 'utf-8'); 127 128 if (\defined('JDEBUG') && JDEBUG) { 129 $xml->formatOutput = true; 130 } 131 132 // The Opensearch Namespace 133 $osns = 'http://a9.com/-/spec/opensearch/1.1/'; 134 135 // Create the root element 136 $elOs = $xml->createElementNS($osns, 'OpenSearchDescription'); 137 138 $elShortName = $xml->createElementNS($osns, 'ShortName'); 139 $elShortName->appendChild($xml->createTextNode(htmlspecialchars($this->_shortName))); 140 $elOs->appendChild($elShortName); 141 142 $elDescription = $xml->createElementNS($osns, 'Description'); 143 $elDescription->appendChild($xml->createTextNode(htmlspecialchars($this->description))); 144 $elOs->appendChild($elDescription); 145 146 // Always set the accepted input encoding to UTF-8 147 $elInputEncoding = $xml->createElementNS($osns, 'InputEncoding'); 148 $elInputEncoding->appendChild($xml->createTextNode('UTF-8')); 149 $elOs->appendChild($elInputEncoding); 150 151 foreach ($this->_images as $image) { 152 $elImage = $xml->createElementNS($osns, 'Image'); 153 $elImage->setAttribute('type', $image->type); 154 $elImage->setAttribute('width', $image->width); 155 $elImage->setAttribute('height', $image->height); 156 $elImage->appendChild($xml->createTextNode(htmlspecialchars($image->data))); 157 $elOs->appendChild($elImage); 158 } 159 160 foreach ($this->_urls as $url) { 161 $elUrl = $xml->createElementNS($osns, 'Url'); 162 $elUrl->setAttribute('type', $url->type); 163 164 // Results is the default value so we don't need to add it 165 if ($url->rel !== 'results') { 166 $elUrl->setAttribute('rel', $url->rel); 167 } 168 169 $elUrl->setAttribute('template', $url->template); 170 $elOs->appendChild($elUrl); 171 } 172 173 $xml->appendChild($elOs); 174 parent::render($cache, $params); 175 176 return $xml->saveXML(); 177 } 178 179 /** 180 * Sets the short name 181 * 182 * @param string $name The name. 183 * 184 * @return OpensearchDocument instance of $this to allow chaining 185 * 186 * @since 1.7.0 187 */ 188 public function setShortName($name) 189 { 190 $this->_shortName = $name; 191 192 return $this; 193 } 194 195 /** 196 * Adds a URL to the Opensearch description. 197 * 198 * @param OpensearchUrl $url The url to add to the description. 199 * 200 * @return OpensearchDocument instance of $this to allow chaining 201 * 202 * @since 1.7.0 203 */ 204 public function addUrl(OpensearchUrl $url) 205 { 206 $this->_urls[] = $url; 207 208 return $this; 209 } 210 211 /** 212 * Adds an image to the Opensearch description. 213 * 214 * @param OpensearchImage $image The image to add to the description. 215 * 216 * @return OpensearchDocument instance of $this to allow chaining 217 * 218 * @since 1.7.0 219 */ 220 public function addImage(OpensearchImage $image) 221 { 222 $this->_images[] = $image; 223 224 return $this; 225 } 226 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |