1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2017–2019 Ryan Parman <http://ryanparman.com>. |
4
|
|
|
* Copyright (c) 2017–2019 Contributors. |
5
|
|
|
* |
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace SimplePie; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
use Psr\Log\NullLogger; |
15
|
|
|
use SimplePie\Configuration as C; |
16
|
|
|
use SimplePie\Middleware\Xml\Atom; |
17
|
|
|
use SimplePie\Mixin as Tr; |
18
|
|
|
use SimplePie\Parser\Xml as XmlParser; |
19
|
|
|
|
20
|
|
|
\define('SIMPLEPIE_ROOT', __DIR__); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* `SimplePie\SimplePie` is the primary entry point for SimplePie NG. |
24
|
|
|
*/ |
25
|
|
|
class SimplePie implements C\SetLoggerInterface |
26
|
|
|
{ |
27
|
|
|
use Tr\LoggerTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bitwise libxml options to use for parsing XML. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $libxml; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The handler stack which contains registered middleware. |
38
|
|
|
* |
39
|
|
|
* @var HandlerStackInterface |
40
|
|
|
*/ |
41
|
|
|
protected $middleware; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructs a new instance of this class. |
45
|
|
|
*/ |
46
|
561 |
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
// Default logger |
49
|
561 |
|
$this->logger = new NullLogger(); |
50
|
|
|
|
51
|
|
|
// Default middleware stack |
52
|
561 |
|
$this->middleware = new HandlerStack(); |
53
|
561 |
|
$this->middleware->setLogger($this->getLogger()); |
54
|
561 |
|
$this->middleware->append(new Atom(), 'atom'); |
55
|
|
|
|
56
|
|
|
// Default libxml2 settings |
57
|
561 |
|
$this->libxml = \LIBXML_HTML_NOIMPLIED // Required, or things crash. |
58
|
|
|
| \LIBXML_BIGLINES |
59
|
|
|
| \LIBXML_COMPACT |
60
|
|
|
| \LIBXML_HTML_NODEFDTD |
61
|
|
|
| \LIBXML_NOBLANKS |
62
|
|
|
| \LIBXML_NOENT |
63
|
|
|
| \LIBXML_NOXMLDECL |
64
|
|
|
| \LIBXML_NSCLEAN |
65
|
|
|
| \LIBXML_PARSEHUGE; |
66
|
561 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the libxml value to use for parsing XML. |
70
|
|
|
* |
71
|
|
|
* @param int $libxml TODO add a description here. |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
1 |
|
public function setLibxml(int $libxml) |
76
|
|
|
{ |
77
|
1 |
|
$this->libxml = $libxml; |
78
|
|
|
|
79
|
|
|
// What are the libxml2 configurations? |
80
|
1 |
|
$this->logger->debug(\sprintf( |
81
|
1 |
|
'Libxml configuration has a bitwise value of `%s`.%s', |
82
|
1 |
|
$this->libxml, |
83
|
1 |
|
(4792582 === $this->libxml) |
84
|
1 |
|
? ' (This is the default configuration.)' |
85
|
1 |
|
: '' |
86
|
|
|
)); |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets the libxml value to use for parsing XML. |
93
|
|
|
*/ |
94
|
1 |
|
public function getLibxml(): int |
95
|
|
|
{ |
96
|
1 |
|
return $this->libxml; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the handler stack which contains registered middleware. |
101
|
|
|
* |
102
|
|
|
* @param HandlerStackInterface $handlerStack TODO add a description here. |
103
|
|
|
* |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
560 |
|
public function setMiddlewareStack(HandlerStackInterface $handlerStack) |
107
|
|
|
{ |
108
|
560 |
|
$this->middleware = $handlerStack; |
109
|
560 |
|
$this->middleware->setLogger($this->getLogger()); |
110
|
|
|
|
111
|
560 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the handler stack which contains registered middleware. |
116
|
|
|
*/ |
117
|
|
|
public function getMiddlewareStack(): HandlerStackInterface |
118
|
|
|
{ |
119
|
|
|
return $this->middleware; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Parses content which is known to be valid XML and is encoded as UTF-8. |
124
|
|
|
* |
125
|
|
|
* @param StreamInterface $stream A PSR-7 `StreamInterface` which is typically returned by the |
126
|
|
|
* `getBody()` method of a `ResponseInterface` class. |
127
|
|
|
* @param bool $handleHtmlEntitiesInXml Whether or not SimplePie should pre-parse the XML as HTML to |
128
|
|
|
* resolve the entities. A value of `true` means that SimplePie |
129
|
|
|
* should inject the entity definitions. A value of `false` means |
130
|
|
|
* that SimplePie should NOT inject the entity definitions. The |
131
|
|
|
* default value is `false`. |
132
|
|
|
*/ |
133
|
560 |
|
public function parseXml(StreamInterface $stream, bool $handleHtmlEntitiesInXml = false): XmlParser |
134
|
|
|
{ |
135
|
560 |
|
return new XmlParser( |
136
|
560 |
|
$stream, |
137
|
560 |
|
$this->logger, |
138
|
560 |
|
$this->middleware, |
139
|
560 |
|
$this->libxml, |
140
|
|
|
$handleHtmlEntitiesInXml |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|