forked from jaeksoft/opensearchserver-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_misc.lib.php
More file actions
336 lines (273 loc) · 8.63 KB
/
oss_misc.lib.php
File metadata and controls
336 lines (273 loc) · 8.63 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/*
* This file is part of OpenSearchServer PHP Client.
*
* Copyright (C) 2008-2013 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* OpenSearchServer PHP Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer PHP Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenSearchServer PHP Client. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Class to access miscellaneous functions
* @package OpenSearchServer
*/
/**
* Store and retrieve a value from the browser (In order REQUEST, COOKIE, DEFAULT)
* @return unknown_type
*/
function config_request_value($key, $default, $request_field = NULL) {
$value = NULL;
if (!empty($_REQUEST[$request_field])) {
$value = $_REQUEST[$request_field];
setcookie($key, $value, time() + 3600 * 365, '/');
}
if (!$value && isset($_COOKIE[$key])) {
$value = $_COOKIE[$key];
}
if (!$value) {
$value = $default;
}
return $value;
}
/**
* Retrieve an XML feed
* @param string $url The feed URL
* @param array $curl_info By Reference. If given, the informations provided by curl will be returned using the provided array
* @return SimpleXMLElement Will return FALSE if something gone wrong
*/
function retrieve_xml($url, &$curl_info = NULL) {
$rcurl = curl_init($url);
curl_setopt($rcurl, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($rcurl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($rcurl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($rcurl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($rcurl, CURLOPT_TIMEOUT, 5);
$content = curl_exec($rcurl);
if ($curl_info !== NULL) {
$curl_info = curl_getinfo($rcurl);
}
if ($content === FALSE) {
trigger_error('CURL failed to execute on URL "' . $url . '"');
return FALSE;
}
$previous_error_level = error_reporting(0);
$xml = simplexml_load_string($content);
error_reporting($previous_error_level);
return (!$xml instanceof SimpleXMLElement) ? FALSE : $xml;
}
/**
* Wrapper for reset to use arrays returned from functions and methods
* @param $array
* @return mixed
*/
function array_first($array) {
return reset($array);
}
/**
* Wrapper for end to use arrays returned from functions and methods
* @param $array
* @return mixed
*/
function array_last($array) {
return end($array);
}
/**
* Misc classes to parse and simplify usage of different feed format during the indexation
*/
abstract class NewsFeedParser extends ArrayObject {
protected $feedFormat;
protected $channelTitle;
protected $channelSubtitle;
protected $channelHome;
/**
* @param SimpleXMLElement $xml
* @return NewsFeedParser
*/
public static function factory(SimpleXMLElement $xml) {
// Determine the format of the xml
// RSS
if (isset($xml->channel->item[0])) {
return new NewsFeedParser_RSS($xml);
}
// Atom
elseif (isset($xml->entry[0])) {
return new NewsFeedParser_Atom($xml);
}
}
public function getFeedFormat() {
return $this->feedFormat;
}
public function getChannelTitle() {
return $this->channelTitle;
}
public function getChannelSubtitle() {
return $this->channelSubtitle;
}
public function getChannelHome() {
return $this->channelHome;
}
}
abstract class NewsFeedParser_Feed_Entry {
protected $author;
protected $content;
protected $id;
protected $link;
protected $published;
protected $summary;
protected $title;
protected $language;
public function getLanguage() {
return $this->language;
}
public function getAuthor() {
return $this->author;
}
public function getContent() {
return $this->content;
}
public function getId() {
return $this->id;
}
public function getLink() {
return $this->link;
}
public function getPublished() {
return $this->published;
}
public function getSummary() {
return $this->summary;
}
public function getTitle() {
return $this->title;
}
}
class NewsFeedParser_RSS extends NewsFeedParser {
/**
* @param SimpleXMLElement $xml
* @return NewsFeedParser
*/
public function __construct(SimpleXMLElement $xml) {
$this->feedFormat = 'RSS';
// Misc informations
$this->channelTitle = (string) $xml->channel->title;
$this->channelSubtitle = (string )$xml->channel->description;
$this->channelHome = (string) $xml->channel->link;
// Entries
$items = (array)$xml->xpath('channel/item');
foreach ($items as $item) {
$this->append(new NewsFeedParser_RSS_Entry($item));
}
}
}
class NewsFeedParser_RSS_Entry extends NewsFeedParser_Feed_Entry {
public function __construct(SimpleXMLElement $xml) {
$this->id = md5((string)$xml->guid);
$this->link = $xml->link;
$this->published = date('Y-m-d\TH:i:sO', strtotime((string)$xml->pubDate));
$this->summary = (string)$xml->description;
$this->title = $xml->title;
// Only RSSS2.0
$this->author = (string)$xml->author;
if (empty($this->author)) {
$this->author = $xml->children("http://purl.org/dc/elements/1.1/")->creator;
}
$this->content = (string)$xml->children('http://purl.org/rss/1.0/modules/content/');
}
}
class NewsFeedParser_Atom extends NewsFeedParser {
/**
* @param SimpleXMLElement $xml
* @return NewsFeedParser
*/
public function __construct(SimpleXMLElement $xml) {
$this->feedFormat = 'ATOM';
// Misc informations
$this->channelTitle = (string)$xml->title;
$this->channelSubtitle = (string)$xml->subtitle;
$this->channelHome = preg_replace('/(\.\w+)\/.*$/', '$1', (string)$xml->id);
// Entries
foreach ($xml->entry as $item) {
$this->append(new NewsFeedParser_Atom_Entry($item));
}
}
}
class NewsFeedParser_Atom_Entry extends NewsFeedParser_Feed_Entry {
public function __construct(SimpleXMLElement $xml) {
$this->id = md5((string)$xml->id);
$this->link = $xml->link['href'];
$this->published = date('Y-m-d\TH:i:sO', strtotime((string)$xml->published));
$this->summary = (string)$xml->content;
$this->title = $xml->title;
// Only RSSS2.0
$this->author = (string)$xml->author->name;
$this->content = (string)$xml->content;
}
}
function indentXML($string) {
function indentXML_pregCallback($matches) {
static $indent = 0;
static $indentExclusion = array('?');
if (substr($matches[0], 0, 9) == "<[CDATA[!") {
$pad = str_repeat(' ', max(0, $indent));
}
elseif ($matches[0][1] == '?') {
$pad = str_repeat(' ', max(0, $indent));
}
elseif ($matches[0][1] == '/') {
$indent--;
$pad = str_repeat(' ', max(0, $indent));
}
elseif (substr($matches[0], -2, 1) != '/') {
$indent++;
$pad = str_repeat(' ', max(0, $indent-1));
}
return $pad . $matches[0] . ($indent ? "\n" : "");
}
return preg_replace_callback('/<[^>]+>/', "indentXML_pregCallback", $string);
}
function beautifulXML($string) {
function beautifulXML_tagging($string) {
$string = preg_replace('/^(\w+)/i', '<span class="nodeName">$1</span>', $string);
return $string;
}
function beautifulXML_pregCallback($matches) {
$before = '';
$after = '';
if (substr($matches[0], 0, 9) == "<![CDATA[") {
$before = '<div class="node"><span class="delimiter"><[!CDATA[</span><span class="cdata">';
$content = substr($matches[0], 9, -3);
$after = '</span><span class="delimiter">]]></span></div>';
}
elseif ($matches[0][1] == '?') {
$before = '<div class="node"><span class="delimiter"><?</span>';
$content = beautifulXML_tagging(substr($matches[0], 2, -2));
$after = '<span class="delimiter">?></span></div>';
}
elseif ($matches[0][1] == '/') {
$before = '<span class="delimiter"></</span>';
$content = beautifulXML_tagging(substr($matches[0], 2, -1));
$after = '<span class="delimiter">></span></div>';
}
elseif (substr($matches[0], -2, 1) != '/') {
$before = '<div class="node"><span class="delimiter"><</span>';
$content = beautifulXML_tagging(substr($matches[0], 1, -1));
$after = '<span class="delimiter">></span>';
}
return $before . $content . $after;
}
return preg_replace_callback('/<[^>]+>/', "beautifulXML_pregCallback", $string);
}
?>