forked from BeSimple/BeSimpleSoapClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WsdlDownloader.php
264 lines (231 loc) · 8.53 KB
/
WsdlDownloader.php
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
<?php
/*
* This file is part of the BeSimpleSoapClient.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapClient;
use BeSimple\SoapCommon\Cache;
use BeSimple\SoapCommon\Helper;
/**
* Downloads WSDL files with cURL. Uses the WSDL_CACHE_* constants and the
* 'soap.wsdl_*' ini settings. Does only file caching as SoapClient only
* supports a file name parameter. The class also resolves remote XML schema
* includes.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class WsdlDownloader
{
/**
* Cache enabled.
*
* @var bool
*/
protected $cacheEnabled;
/**
* Cache dir.
*
* @var string
*/
protected $cacheDir;
/**
* Cache TTL.
*
* @var int
*/
protected $cacheTtl;
/**
* cURL instance for downloads.
*
* @var unknown_type
*/
protected $curl;
/**
* Resolve WSDl/XSD includes.
*
* @var boolean
*/
protected $resolveRemoteIncludes = true;
/**
* Constructor.
*
* @param \BeSimple\SoapClient\Curl $curl Curl instance
* @param boolean $resolveRemoteIncludes WSDL/XSD include enabled?
* @param boolean $cacheWsdl Cache constant
*/
public function __construct(Curl $curl, $resolveRemoteIncludes = true, $cacheWsdl = Cache::TYPE_DISK)
{
$this->curl = $curl;
$this->resolveRemoteIncludes = (Boolean) $resolveRemoteIncludes;
// get current WSDL caching config
$this->cacheEnabled = $cacheWsdl === Cache::TYPE_NONE ? Cache::DISABLED : Cache::ENABLED == Cache::isEnabled();
$this->cacheDir = Cache::getDirectory();
$this->cacheTtl = Cache::getLifetime();
}
/**
* Download given WSDL file and return name of cache file.
*
* @param string $wsdl WSDL file URL/path
*
* @return string
*/
public function download($wsdl)
{
// download and cache remote WSDL files or local ones where we want to
// resolve remote XSD includes
$isRemoteFile = $this->isRemoteFile($wsdl);
if ($isRemoteFile || $this->resolveRemoteIncludes) {
$cacheFilePath = $this->cacheDir.DIRECTORY_SEPARATOR.'wsdl_'.md5($wsdl).'.cache';
if (!$this->cacheEnabled || !file_exists($cacheFilePath) || (filemtime($cacheFilePath) + $this->cacheTtl) < time()) {
if ($isRemoteFile) {
// execute request
$responseSuccessfull = $this->curl->exec($wsdl);
// get content
if ($responseSuccessfull) {
$response = $this->curl->getResponseBody();
if ($this->resolveRemoteIncludes) {
$this->resolveRemoteIncludes($response, $cacheFilePath, $wsdl);
} else {
file_put_contents($cacheFilePath, $response);
}
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
} elseif (file_exists($wsdl)) {
$response = file_get_contents($wsdl);
$this->resolveRemoteIncludes($response, $cacheFilePath);
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
}
return $cacheFilePath;
} elseif (file_exists($wsdl)) {
return realpath($wsdl);
}
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
/**
* Do we have a remote file?
*
* @param string $file File URL/path
*
* @return boolean
*/
private function isRemoteFile($file)
{
// @parse_url to suppress E_WARNING for invalid urls
if (false !== $url = @parse_url($file)) {
if (isset($url['scheme']) && 'http' === substr($url['scheme'], 0, 4)) {
return true;
}
}
return false;
}
/**
* Resolves remote WSDL/XSD includes within the WSDL files.
*
* @param string $xml XML file
* @param string $cacheFilePath Cache file name
* @param boolean $parentFilePath Parent file name
*
* @return void
*/
private function resolveRemoteIncludes($xml, $cacheFilePath, $parentFilePath = null)
{
$doc = new \DOMDocument();
$doc->loadXML($xml);
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace(Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA);
$xpath->registerNamespace(Helper::PFX_WSDL, Helper::NS_WSDL);
// WSDL include/import
$query = './/'.Helper::PFX_WSDL.':include | .//'.Helper::PFX_WSDL.':import';
$nodes = $xpath->query($query);
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$location = $node->getAttribute('location');
if ($this->isRemoteFile($location)) {
$location = $this->download($location);
$node->setAttribute('location', $location);
} elseif (null !== $parentFilePath) {
$location = $this->resolveRelativePathInUrl($parentFilePath, $location);
$location = $this->download($location);
$node->setAttribute('location', $location);
}
}
}
// XML schema include/import
$query = './/'.Helper::PFX_XML_SCHEMA.':include | .//'.Helper::PFX_XML_SCHEMA.':import';
$nodes = $xpath->query($query);
if ($nodes->length > 0) {
foreach ($nodes as $node) {
if ($node->hasAttribute('schemaLocation')) {
$schemaLocation = $node->getAttribute('schemaLocation');
if ($this->isRemoteFile($schemaLocation)) {
$schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation);
} elseif (null !== $parentFilePath) {
$schemaLocation = $this->resolveRelativePathInUrl($parentFilePath, $schemaLocation);
$schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation);
}
}
}
}
$doc->save($cacheFilePath);
}
/**
* Resolves the relative path to base into an absolute.
*
* @param string $base Base path
* @param string $relative Relative path
*
* @return string
*/
private function resolveRelativePathInUrl($base, $relative)
{
$urlParts = parse_url($base);
// combine base path with relative path
if (isset($urlParts['path']) && '/' === $relative[0]) {
// $relative is absolute path from domain (starts with /)
$path = $relative;
} elseif (isset($urlParts['path']) && strrpos($urlParts['path'], '/') === (strlen($urlParts['path']) )) {
// base path is directory
$path = $urlParts['path'].$relative;
} elseif (isset($urlParts['path'])) {
// strip filename from base path
$path = substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')).'/'.$relative;
} else {
// no base path
$path = '/'.$relative;
}
// foo/./bar ==> foo/bar
// remove double slashes
$path = preg_replace(array('#/\./#', '#/+#'), '/', $path);
// split path by '/'
$parts = explode('/', $path);
// resolve /../
foreach ($parts as $key => $part) {
if ('..' === $part) {
$keyToDelete = $key - 1;
while ($keyToDelete > 0) {
if (isset($parts[$keyToDelete])) {
unset($parts[$keyToDelete]);
break;
}
$keyToDelete--;
}
unset($parts[$key]);
}
}
$hostname = $urlParts['scheme'].'://'.$urlParts['host'];
if (isset($urlParts['port'])) {
$hostname .= ':'.$urlParts['port'];
}
return $hostname.implode('/', $parts);
}
}