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 execute(array &$param_pool = null) |
26
|
|
|
{ |
27
|
|
|
$result = new XMLElement($this->dsParamROOTELEMENT); |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->dsParamURL = $this->parseParamURL($this->dsParamURL); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if (isset($this->dsParamXPATH)) { |
32
|
|
|
$this->dsParamXPATH = $this->__processParametersInString($this->dsParamXPATH, $this->_env); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$stylesheet = new XMLElement('xsl:stylesheet'); |
36
|
|
|
$stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform')); |
37
|
|
|
|
38
|
|
|
$output = new XMLElement('xsl:output'); |
39
|
|
|
$output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes')); |
40
|
|
|
$stylesheet->appendChild($output); |
41
|
|
|
|
42
|
|
|
$template = new XMLElement('xsl:template'); |
43
|
|
|
$template->setAttribute('match', '/'); |
44
|
|
|
|
45
|
|
|
$instruction = new XMLElement('xsl:copy-of'); |
46
|
|
|
|
47
|
|
|
// Namespaces |
48
|
|
|
if (isset($this->dsParamFILTERS) && is_array($this->dsParamFILTERS)) { |
49
|
|
|
foreach ($this->dsParamFILTERS as $name => $uri) { |
|
|
|
|
50
|
|
|
$instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// XPath |
55
|
|
|
$instruction->setAttribute('select', $this->dsParamXPATH); |
56
|
|
|
|
57
|
|
|
$template->appendChild($instruction); |
58
|
|
|
$stylesheet->appendChild($template); |
59
|
|
|
|
60
|
|
|
$stylesheet->setIncludeHeader(true); |
61
|
|
|
|
62
|
|
|
$xsl = $stylesheet->generate(true); |
63
|
|
|
|
64
|
|
|
$cache_id = md5($this->dsParamURL . serialize($this->dsParamFILTERS) . $this->dsParamXPATH); |
65
|
|
|
|
66
|
|
|
$cache = new Cacheable(Symphony::Database()); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$cachedData = $cache->read($cache_id); |
69
|
|
|
$writeToCache = false; |
70
|
|
|
$valid = true; |
71
|
|
|
$creation = DateTimeObj::get('c'); |
72
|
|
|
$timeout = (isset($this->dsParamTIMEOUT)) ? (int)max(1, $this->dsParamTIMEOUT) : 6; |
|
|
|
|
73
|
|
|
|
74
|
|
|
// Execute if the cache doesn't exist, or if it is old. |
75
|
|
|
if ( |
76
|
|
|
(!is_array($cachedData) || empty($cachedData)) // There's no cache. |
77
|
|
|
|| (time() - $cachedData['creation']) > ($this->dsParamCACHE * 60) // The cache is old. |
|
|
|
|
78
|
|
|
) { |
79
|
|
|
if (Mutex::acquire($cache_id, $timeout, TMP)) { |
80
|
|
|
$ch = new Gateway; |
81
|
|
|
$ch->init($this->dsParamURL); |
82
|
|
|
$ch->setopt('TIMEOUT', $timeout); |
83
|
|
|
$ch->setopt('HTTPHEADER', array('Accept: text/xml, */*')); |
84
|
|
|
|
85
|
|
|
$data = $ch->exec(); |
86
|
|
|
$info = $ch->getInfoLast(); |
87
|
|
|
|
88
|
|
|
Mutex::release($cache_id, TMP); |
89
|
|
|
|
90
|
|
|
$data = trim($data); |
91
|
|
|
$writeToCache = true; |
92
|
|
|
|
93
|
|
|
// Handle any response that is not a 200, or the content type does not include XML, plain or text |
94
|
|
|
if ((int)$info['http_code'] !== 200 || !preg_match('/(xml|plain|text)/i', $info['content_type'])) { |
95
|
|
|
$writeToCache = false; |
|
|
|
|
96
|
|
|
|
97
|
|
|
$result->setAttribute('valid', 'false'); |
98
|
|
|
|
99
|
|
|
// 28 is CURLE_OPERATION_TIMEOUTED |
100
|
|
|
if ($info['curl_error'] === 28) { |
101
|
|
|
$result->appendChild( |
102
|
|
|
new XMLElement('error', |
103
|
|
|
sprintf('Request timed out. %d second limit reached.', $timeout) |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} else { |
107
|
|
|
$result->appendChild( |
108
|
|
|
new XMLElement('error', |
109
|
|
|
sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type']) |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $result; |
115
|
|
|
|
116
|
|
|
// Handle where there is `$data` |
117
|
|
|
} elseif (strlen($data) > 0) { |
118
|
|
|
// If the XML doesn't validate.. |
119
|
|
|
if (!General::validateXML($data, $errors, false, new XsltProcess)) { |
120
|
|
|
$writeToCache = false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// If the `$data` is invalid, return a result explaining why |
124
|
|
|
if ($writeToCache === false) { |
125
|
|
|
$element = new XMLElement('errors'); |
126
|
|
|
|
127
|
|
|
$result->setAttribute('valid', 'false'); |
128
|
|
|
|
129
|
|
|
$result->appendChild(new XMLElement('error', __('Data returned is invalid.'))); |
130
|
|
|
|
131
|
|
View Code Duplication |
foreach ($errors as $e) { |
132
|
|
|
if (strlen(trim($e['message'])) === 0) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$result->appendChild($element); |
140
|
|
|
|
141
|
|
|
return $result; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// If `$data` is empty, set the `force_empty_result` to true. |
145
|
|
|
} elseif (strlen($data) === 0) { |
146
|
|
|
$this->_force_empty_result = true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Failed to acquire a lock |
150
|
|
|
} else { |
151
|
|
|
$result->appendChild( |
152
|
|
|
new XMLElement('error', __('The %s class failed to acquire a lock, check that %s exists and is writable.', array( |
153
|
|
|
'<code>Mutex</code>', |
154
|
|
|
'<code>' . TMP . '</code>' |
155
|
|
|
))) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// The cache is good, use it! |
160
|
|
|
} else { |
161
|
|
|
$data = trim($cachedData['data']); |
162
|
|
|
$creation = DateTimeObj::get('c', $cachedData['creation']); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// If `$writeToCache` is set to false, invalidate the old cache if it existed. |
166
|
|
|
if (is_array($cachedData) && !empty($cachedData) && $writeToCache === false) { |
167
|
|
|
$data = trim($cachedData['data']); |
168
|
|
|
$valid = false; |
169
|
|
|
$creation = DateTimeObj::get('c', $cachedData['creation']); |
170
|
|
|
|
171
|
|
|
if (empty($data)) { |
172
|
|
|
$this->_force_empty_result = true; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// If `force_empty_result` is false and `$result` is an instance of |
177
|
|
|
// XMLElement, build the `$result`. |
178
|
|
|
if (!$this->_force_empty_result && is_object($result)) { |
179
|
|
|
$proc = new XsltProcess; |
180
|
|
|
$ret = $proc->process($data, $xsl); |
|
|
|
|
181
|
|
|
|
182
|
|
|
if ($proc->isErrors()) { |
183
|
|
|
$result->setAttribute('valid', 'false'); |
184
|
|
|
$error = new XMLElement('error', __('Transformed XML is invalid.')); |
185
|
|
|
$result->appendChild($error); |
186
|
|
|
$element = new XMLElement('errors'); |
187
|
|
|
|
188
|
|
View Code Duplication |
foreach ($proc->getError() as $e) { |
189
|
|
|
if (strlen(trim($e['message'])) === 0) { |
190
|
|
|
continue; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$element->appendChild(new XMLElement('item', General::sanitize($e['message']))); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$result->appendChild($element); |
197
|
|
|
} elseif (strlen(trim($ret)) === 0) { |
198
|
|
|
$this->_force_empty_result = true; |
199
|
|
|
} else { |
200
|
|
|
if ($writeToCache) { |
201
|
|
|
$cache->write($cache_id, $data, $this->dsParamCACHE); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$result->setValue(PHP_EOL . str_repeat("\t", 2) . preg_replace('/([\r\n]+)/', "$1\t", $ret)); |
205
|
|
|
$result->setAttribute('status', ($valid === true ? 'fresh' : 'stale')); |
206
|
|
|
$result->setAttribute('creation', $creation); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $result; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: