|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package data-sources |
|
5
|
|
|
*/ |
|
6
|
|
|
/** |
|
7
|
|
|
* The `DynamicXMLDatasource` allows a user to retrieve XML from an URL. |
|
8
|
|
|
* This datasource supports namespaces, partial results using XPath and |
|
9
|
|
|
* caching the result for a number of minutes. |
|
10
|
|
|
* |
|
11
|
|
|
* @since Symphony 2.3 |
|
12
|
|
|
* |
|
13
|
|
|
* @deprecated As of Symphony 2.6.0 this class is deprecated. Please |
|
14
|
|
|
* use the Remote Datasource extensions since it provides more features and |
|
15
|
|
|
* is more robust. It should be completely removed in Symphony 3. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
require_once TOOLKIT . '/class.gateway.php'; |
|
|
|
|
|
|
20
|
|
|
require_once TOOLKIT . '/class.xsltprocess.php'; |
|
21
|
|
|
require_once CORE . '/class.cacheable.php'; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class DynamicXMLDatasource extends Datasource |
|
24
|
|
|
{ |
|
25
|
|
|
public function __construct($env = null, $process_params = true) |
|
26
|
|
|
{ |
|
27
|
|
|
if (Symphony::Log()) { |
|
28
|
|
|
Symphony::Log()->pushDeprecateWarningToLog('new DynamicXMLDatasource()', 'new RemoteDatasource()', array( |
|
29
|
|
|
'alternative-format' => __('Please install the Remote Datasource extension and use `%s` instead.'), |
|
30
|
|
|
)); |
|
31
|
|
|
} |
|
32
|
|
|
parent::__construct($env, $process_params); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function execute(array &$param_pool = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$result = new XMLElement($this->dsParamROOTELEMENT); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->dsParamURL = $this->parseParamURL($this->dsParamURL); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (isset($this->dsParamXPATH)) { |
|
42
|
|
|
$this->dsParamXPATH = $this->__processParametersInString($this->dsParamXPATH, $this->_env); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$stylesheet = new XMLElement('xsl:stylesheet'); |
|
46
|
|
|
$stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform')); |
|
47
|
|
|
|
|
48
|
|
|
$output = new XMLElement('xsl:output'); |
|
49
|
|
|
$output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes')); |
|
50
|
|
|
$stylesheet->appendChild($output); |
|
51
|
|
|
|
|
52
|
|
|
$template = new XMLElement('xsl:template'); |
|
53
|
|
|
$template->setAttribute('match', '/'); |
|
54
|
|
|
|
|
55
|
|
|
$instruction = new XMLElement('xsl:copy-of'); |
|
56
|
|
|
|
|
57
|
|
|
// Namespaces |
|
58
|
|
|
if (isset($this->dsParamFILTERS) && is_array($this->dsParamFILTERS)) { |
|
59
|
|
|
foreach ($this->dsParamFILTERS as $name => $uri) { |
|
60
|
|
|
$instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// XPath |
|
65
|
|
|
$instruction->setAttribute('select', $this->dsParamXPATH); |
|
66
|
|
|
|
|
67
|
|
|
$template->appendChild($instruction); |
|
68
|
|
|
$stylesheet->appendChild($template); |
|
69
|
|
|
|
|
70
|
|
|
$stylesheet->setIncludeHeader(true); |
|
71
|
|
|
|
|
72
|
|
|
$xsl = $stylesheet->generate(true); |
|
73
|
|
|
|
|
74
|
|
|
$cache_id = md5($this->dsParamURL . serialize($this->dsParamFILTERS) . $this->dsParamXPATH); |
|
75
|
|
|
|
|
76
|
|
|
$cache = new Cacheable(Symphony::Database()); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$cachedData = $cache->read($cache_id); |
|
79
|
|
|
$writeToCache = false; |
|
80
|
|
|
$valid = true; |
|
81
|
|
|
$creation = DateTimeObj::get('c'); |
|
82
|
|
|
$timeout = (isset($this->dsParamTIMEOUT)) ? (int)max(1, $this->dsParamTIMEOUT) : 6; |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
// Execute if the cache doesn't exist, or if it is old. |
|
85
|
|
|
if ( |
|
86
|
|
|
(!is_array($cachedData) || empty($cachedData)) // There's no cache. |
|
87
|
|
|
|| (time() - $cachedData['creation']) > ($this->dsParamCACHE * 60) // The cache is old. |
|
|
|
|
|
|
88
|
|
|
) { |
|
89
|
|
|
if (Mutex::acquire($cache_id, $timeout, TMP)) { |
|
|
|
|
|
|
90
|
|
|
$ch = new Gateway; |
|
91
|
|
|
$ch->init($this->dsParamURL); |
|
92
|
|
|
$ch->setopt('TIMEOUT', $timeout); |
|
93
|
|
|
$ch->setopt('HTTPHEADER', array('Accept: text/xml, */*')); |
|
94
|
|
|
|
|
95
|
|
|
$data = $ch->exec(); |
|
96
|
|
|
$info = $ch->getInfoLast(); |
|
97
|
|
|
|
|
98
|
|
|
Mutex::release($cache_id, TMP); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$data = trim($data); |
|
101
|
|
|
$writeToCache = true; |
|
102
|
|
|
|
|
103
|
|
|
// Handle any response that is not a 200, or the content type does not include XML, plain or text |
|
104
|
|
|
if ((int)$info['http_code'] !== 200 || !preg_match('/(xml|plain|text)/i', $info['content_type'])) { |
|
105
|
|
|
$writeToCache = false; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$result->setAttribute('valid', 'false'); |
|
108
|
|
|
|
|
109
|
|
|
// 28 is CURLE_OPERATION_TIMEOUTED |
|
110
|
|
|
if ($info['curl_error'] == 28) { |
|
111
|
|
|
$result->appendChild( |
|
112
|
|
|
new XMLElement('error', |
|
113
|
|
|
sprintf('Request timed out. %d second limit reached.', $timeout) |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
} else { |
|
117
|
|
|
$result->appendChild( |
|
118
|
|
|
new XMLElement('error', |
|
119
|
|
|
sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type']) |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $result; |
|
125
|
|
|
|
|
126
|
|
|
// Handle where there is `$data` |
|
127
|
|
|
} elseif (strlen($data) > 0) { |
|
128
|
|
|
// If the XML doesn't validate.. |
|
129
|
|
|
if (!General::validateXML($data, $errors, false, new XsltProcess)) { |
|
130
|
|
|
$writeToCache = false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// If the `$data` is invalid, return a result explaining why |
|
134
|
|
|
if ($writeToCache === false) { |
|
135
|
|
|
$element = new XMLElement('errors'); |
|
136
|
|
|
|
|
137
|
|
|
$result->setAttribute('valid', 'false'); |
|
138
|
|
|
|
|
139
|
|
|
$result->appendChild(new XMLElement('error', __('Data returned is invalid.'))); |
|
140
|
|
|
|
|
141
|
|
|
foreach ($errors as $e) { |
|
142
|
|
|
if (strlen(trim($e['message'])) == 0) { |
|
143
|
|
|
continue; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$result->appendChild($element); |
|
150
|
|
|
|
|
151
|
|
|
return $result; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// If `$data` is empty, set the `force_empty_result` to true. |
|
155
|
|
|
} elseif (strlen($data) == 0) { |
|
156
|
|
|
$this->_force_empty_result = true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Failed to acquire a lock |
|
160
|
|
|
} else { |
|
161
|
|
|
$result->appendChild( |
|
162
|
|
|
new XMLElement('error', __('The %s class failed to acquire a lock, check that %s exists and is writable.', array( |
|
163
|
|
|
'<code>Mutex</code>', |
|
164
|
|
|
'<code>' . TMP . '</code>' |
|
165
|
|
|
))) |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// The cache is good, use it! |
|
170
|
|
|
} else { |
|
171
|
|
|
$data = trim($cachedData['data']); |
|
172
|
|
|
$creation = DateTimeObj::get('c', $cachedData['creation']); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// If `$writeToCache` is set to false, invalidate the old cache if it existed. |
|
176
|
|
|
if (is_array($cachedData) && !empty($cachedData) && $writeToCache === false) { |
|
177
|
|
|
$data = trim($cachedData['data']); |
|
178
|
|
|
$valid = false; |
|
179
|
|
|
$creation = DateTimeObj::get('c', $cachedData['creation']); |
|
180
|
|
|
|
|
181
|
|
|
if (empty($data)) { |
|
182
|
|
|
$this->_force_empty_result = true; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// If `force_empty_result` is false and `$result` is an instance of |
|
187
|
|
|
// XMLElement, build the `$result`. |
|
188
|
|
|
if (!$this->_force_empty_result && is_object($result)) { |
|
189
|
|
|
$proc = new XsltProcess; |
|
190
|
|
|
$ret = $proc->process($data, $xsl); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
if ($proc->isErrors()) { |
|
193
|
|
|
$result->setAttribute('valid', 'false'); |
|
194
|
|
|
$error = new XMLElement('error', __('Transformed XML is invalid.')); |
|
195
|
|
|
$result->appendChild($error); |
|
196
|
|
|
$element = new XMLElement('errors'); |
|
197
|
|
|
|
|
198
|
|
|
foreach ($proc->getError() as $e) { |
|
199
|
|
|
if (strlen(trim($e['message'])) == 0) { |
|
200
|
|
|
continue; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$result->appendChild($element); |
|
207
|
|
|
} elseif (strlen(trim($ret)) == 0) { |
|
|
|
|
|
|
208
|
|
|
$this->_force_empty_result = true; |
|
209
|
|
|
} else { |
|
210
|
|
|
if ($writeToCache) { |
|
211
|
|
|
$cache->write($cache_id, $data, $this->dsParamCACHE); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$result->setValue(PHP_EOL . str_repeat("\t", 2) . preg_replace('/([\r\n]+)/', "$1\t", $ret)); |
|
215
|
|
|
$result->setAttribute('status', ($valid === true ? 'fresh' : 'stale')); |
|
216
|
|
|
$result->setAttribute('creation', $creation); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $result; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|