2011-12-17 20:36:56 +01:00
|
|
|
<?php
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2011-12-11 21:20:35 +01:00
|
|
|
use BeSimple\SoapCommon\Helper;
|
2011-11-02 12:48:51 +01:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
/**
|
2011-11-02 12:48:51 +01:00
|
|
|
* 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.
|
2011-10-16 19:49:24 +02:00
|
|
|
*
|
2011-10-22 11:28:15 +02:00
|
|
|
* @author Andreas Schamberger <mail@andreass.net>
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
|
|
|
class WsdlDownloader
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cache enabled.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
protected $cacheEnabled;
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache dir.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
protected $cacheDir;
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache TTL.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
protected $cacheTtl;
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
2011-11-02 12:48:51 +01:00
|
|
|
* cURL instance for downloads.
|
|
|
|
*
|
|
|
|
* @var unknown_type
|
|
|
|
*/
|
2011-12-11 21:20:35 +01:00
|
|
|
protected $curl;
|
2011-11-02 12:48:51 +01:00
|
|
|
|
|
|
|
/**
|
2011-11-20 18:13:42 +01:00
|
|
|
* Resolve WSDl/XSD includes.
|
2011-10-16 19:49:24 +02:00
|
|
|
*
|
2011-11-02 12:48:51 +01:00
|
|
|
* @var boolean
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
2011-11-20 18:13:42 +01:00
|
|
|
protected $resolveRemoteIncludes = true;
|
2011-10-16 19:49:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-11-20 18:13:42 +01:00
|
|
|
* @param \BeSimple\SoapClient\Curl $curl Curl instance
|
|
|
|
* @param boolean $resolveRemoteIncludes WSDL/XSD include enabled?
|
|
|
|
* @param boolean $cacheWsdl Cache constant
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
2011-11-20 18:13:42 +01:00
|
|
|
public function __construct(Curl $curl, $resolveRemoteIncludes = true, $cacheWsdl = WSDL_CACHE_DISK)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
2011-11-02 12:48:51 +01:00
|
|
|
$this->curl = $curl;
|
2011-11-20 18:13:42 +01:00
|
|
|
$this->resolveRemoteIncludes = $resolveRemoteIncludes;
|
2011-10-16 19:49:24 +02:00
|
|
|
// get current WSDL caching config
|
2011-12-17 16:05:25 +01:00
|
|
|
$this->cacheEnabled = (bool) ini_get('soap.wsdl_cache_enabled');
|
2011-10-16 19:49:24 +02:00
|
|
|
if ($this->cacheEnabled === true
|
2011-11-02 12:48:51 +01:00
|
|
|
&& $cacheWsdl === WSDL_CACHE_NONE) {
|
2011-10-16 19:49:24 +02:00
|
|
|
$this->cacheEnabled = false;
|
|
|
|
}
|
|
|
|
$this->cacheDir = ini_get('soap.wsdl_cache_dir');
|
|
|
|
if (!is_dir($this->cacheDir)) {
|
|
|
|
$this->cacheDir = sys_get_temp_dir();
|
|
|
|
}
|
|
|
|
$this->cacheDir = rtrim($this->cacheDir, '/\\');
|
|
|
|
$this->cacheTtl = ini_get('soap.wsdl_cache_ttl');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download given WSDL file and return name of cache file.
|
|
|
|
*
|
2011-12-17 16:05:25 +01:00
|
|
|
* @param string $wsdl WSDL file URL/path
|
|
|
|
*
|
2011-10-16 19:49:24 +02:00
|
|
|
* @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);
|
2011-11-20 18:13:42 +01:00
|
|
|
if ($isRemoteFile === true || $this->resolveRemoteIncludes === true) {
|
2011-10-16 19:49:24 +02:00
|
|
|
$cacheFile = $this->cacheDir . DIRECTORY_SEPARATOR . 'wsdl_' . md5($wsdl) . '.cache';
|
|
|
|
if ($this->cacheEnabled === false
|
|
|
|
|| !file_exists($cacheFile)
|
|
|
|
|| (filemtime($cacheFile) + $this->cacheTtl) < time()) {
|
|
|
|
if ($isRemoteFile === true) {
|
|
|
|
// execute request
|
2011-11-02 12:48:51 +01:00
|
|
|
$responseSuccessfull = $this->curl->exec($wsdl);
|
2011-10-16 19:49:24 +02:00
|
|
|
// get content
|
|
|
|
if ($responseSuccessfull === true) {
|
2011-11-02 12:48:51 +01:00
|
|
|
$response = $this->curl->getResponseBody();
|
2011-11-20 18:13:42 +01:00
|
|
|
if ($this->resolveRemoteIncludes === true) {
|
|
|
|
$this->resolveRemoteIncludes($response, $cacheFile, $wsdl);
|
2011-10-16 19:49:24 +02:00
|
|
|
} else {
|
|
|
|
file_put_contents($cacheFile, $response);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
|
|
|
|
}
|
|
|
|
} elseif (file_exists($wsdl)) {
|
|
|
|
$response = file_get_contents($wsdl);
|
2011-11-20 18:13:42 +01:00
|
|
|
$this->resolveRemoteIncludes($response, $cacheFile);
|
2011-10-16 19:49:24 +02:00
|
|
|
} else {
|
|
|
|
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
|
|
|
|
}
|
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
return $cacheFile;
|
|
|
|
} elseif (file_exists($wsdl)) {
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
return realpath($wsdl);
|
|
|
|
} else {
|
|
|
|
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we have a remote file?
|
|
|
|
*
|
2011-12-17 16:05:25 +01:00
|
|
|
* @param string $file File URL/path
|
|
|
|
*
|
2011-10-16 19:49:24 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function isRemoteFile($file)
|
|
|
|
{
|
|
|
|
$isRemoteFile = false;
|
|
|
|
// @parse_url to suppress E_WARNING for invalid urls
|
|
|
|
if (($url = @parse_url($file)) !== false) {
|
|
|
|
if (isset($url['scheme']) && substr($url['scheme'], 0, 4) == 'http') {
|
|
|
|
$isRemoteFile = true;
|
|
|
|
}
|
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
return $isRemoteFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-20 18:13:42 +01:00
|
|
|
* Resolves remote WSDL/XSD includes within the WSDL files.
|
2011-10-16 19:49:24 +02:00
|
|
|
*
|
2011-12-17 16:05:25 +01:00
|
|
|
* @param string $xml XML file
|
|
|
|
* @param string $cacheFile Cache file name
|
|
|
|
* @param boolean $parentFile Parent file name
|
|
|
|
*
|
2011-12-11 21:20:35 +01:00
|
|
|
* @return void
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
2011-11-20 18:13:42 +01:00
|
|
|
private function resolveRemoteIncludes($xml, $cacheFile, $parentFile = null)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
|
|
|
$doc = new \DOMDocument();
|
|
|
|
$doc->loadXML($xml);
|
|
|
|
$xpath = new \DOMXPath($doc);
|
|
|
|
$xpath->registerNamespace(Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA);
|
2011-12-17 11:10:08 +01:00
|
|
|
$xpath->registerNamespace(Helper::PFX_WSDL, Helper::NS_WSDL);
|
2011-11-20 18:13:42 +01:00
|
|
|
// WSDL include/import
|
2011-12-17 11:10:08 +01:00
|
|
|
$query = './/' . Helper::PFX_WSDL . ':include | .//' . Helper::PFX_WSDL . ':import';
|
2011-11-20 18:13:42 +01:00
|
|
|
$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 (!is_null($parentFile)) {
|
|
|
|
$location = $this->resolveRelativePathInUrl($parentFile, $location);
|
|
|
|
$location = $this->download($location);
|
|
|
|
$node->setAttribute('location', $location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// XML schema include/import
|
|
|
|
$query = './/' . Helper::PFX_XML_SCHEMA . ':include | .//' . Helper::PFX_XML_SCHEMA . ':import';
|
2011-10-16 19:49:24 +02:00
|
|
|
$nodes = $xpath->query($query);
|
|
|
|
if ($nodes->length > 0) {
|
|
|
|
foreach ($nodes as $node) {
|
2012-08-02 10:13:49 +02:00
|
|
|
if ($node->hasAttribute('schemaLocation')) {
|
2012-07-31 16:57:35 +02:00
|
|
|
$schemaLocation = $node->getAttribute('schemaLocation');
|
|
|
|
if ($this->isRemoteFile($schemaLocation)) {
|
|
|
|
$schemaLocation = $this->download($schemaLocation);
|
|
|
|
$node->setAttribute('schemaLocation', $schemaLocation);
|
2012-08-02 10:13:49 +02:00
|
|
|
} elseif (null !== $parentFile) {
|
2012-07-31 16:57:35 +02:00
|
|
|
$schemaLocation = $this->resolveRelativePathInUrl($parentFile, $schemaLocation);
|
|
|
|
$schemaLocation = $this->download($schemaLocation);
|
|
|
|
$node->setAttribute('schemaLocation', $schemaLocation);
|
|
|
|
}
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$doc->save($cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the relative path to base into an absolute.
|
|
|
|
*
|
2011-12-17 16:05:25 +01:00
|
|
|
* @param string $base Base path
|
|
|
|
* @param string $relative Relative path
|
|
|
|
*
|
2011-10-16 19:49:24 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function resolveRelativePathInUrl($base, $relative)
|
|
|
|
{
|
|
|
|
$urlParts = parse_url($base);
|
|
|
|
// combine base path with relative path
|
|
|
|
if (isset($urlParts['path']) && strpos($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
|
|
|
|
$path = preg_replace('~/\./~', '/', $path);
|
|
|
|
// remove double slashes
|
|
|
|
$path = preg_replace('~/+~', '/', $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;
|
|
|
|
} else {
|
|
|
|
$keyToDelete--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($parts[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$hostname = $urlParts['scheme'] . '://' . $urlParts['host'];
|
|
|
|
if (isset($urlParts['port'])) {
|
|
|
|
$hostname .= ':' . $urlParts['port'];
|
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
return $hostname . implode('/', $parts);
|
|
|
|
}
|
2011-09-26 22:42:21 +02:00
|
|
|
}
|