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