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;
|
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
use BeSimple\SoapClient\Curl\Curl;
|
2013-07-24 23:18:41 +02:00
|
|
|
use BeSimple\SoapCommon\Cache;
|
2011-12-11 21:20:35 +01:00
|
|
|
use BeSimple\SoapCommon\Helper;
|
2017-02-03 15:22:37 +01:00
|
|
|
use DOMDocument;
|
|
|
|
use DOMElement;
|
|
|
|
use DOMXPath;
|
|
|
|
use Exception;
|
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
|
|
|
|
{
|
|
|
|
/**
|
2016-11-08 15:42:52 +01:00
|
|
|
* @param Curl $curl
|
2017-02-03 15:22:37 +01:00
|
|
|
* @param string $wsdlPath WSDL file URL/path
|
|
|
|
* @param int $wsdCacheType = Cache::TYPE_NONE|Cache::WSDL_CACHE_DISK|Cache::WSDL_CACHE_BOTH|Cache::WSDL_CACHE_MEMORY
|
2016-11-08 15:42:52 +01:00
|
|
|
* @param boolean $resolveRemoteIncludes
|
2017-02-03 15:22:37 +01:00
|
|
|
* @return string
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
2017-02-03 15:22:37 +01:00
|
|
|
public function getWsdlPath(Curl $curl, $wsdlPath, $wsdCacheType, $resolveRemoteIncludes = true)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
2017-02-03 15:22:37 +01:00
|
|
|
$isRemoteFile = $this->isRemoteFile($wsdlPath);
|
|
|
|
$isCacheEnabled = $wsdCacheType === Cache::TYPE_NONE ? false : Cache::isEnabled();
|
|
|
|
if ($isCacheEnabled === true) {
|
|
|
|
$cacheFilePath = Cache::getDirectory().DIRECTORY_SEPARATOR.'wsdl_'.md5($wsdlPath).'.cache';
|
|
|
|
$isCacheExisting = file_exists($cacheFilePath);
|
|
|
|
if ($isCacheExisting) {
|
|
|
|
$fileModificationTime = filemtime($cacheFilePath);
|
|
|
|
if ($fileModificationTime === false) {
|
|
|
|
throw new Exception('File modification time could not be get for wsdl path: ' . $cacheFilePath);
|
|
|
|
}
|
|
|
|
$isCacheValid = ($fileModificationTime + Cache::getLifetime()) >= time();
|
|
|
|
} else {
|
|
|
|
$isCacheExisting = $isCacheValid = false;
|
|
|
|
}
|
|
|
|
if ($isCacheExisting === false || $isCacheValid === false) {
|
|
|
|
$this->writeCacheFile($curl, $wsdCacheType, $wsdlPath, $cacheFilePath, $resolveRemoteIncludes, $isRemoteFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getLocalWsdlPath($cacheFilePath);
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if ($isRemoteFile === true) {
|
|
|
|
return $wsdlPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getLocalWsdlPath($wsdlPath);
|
|
|
|
}
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
private function writeCacheFile(Curl $curl, $cacheType, $wsdlPath, $cacheFilePath, $resolveRemoteIncludes, $isRemoteFile)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
2017-02-03 15:22:37 +01:00
|
|
|
if ($isRemoteFile === true) {
|
|
|
|
$curlResponse = $curl->executeCurlWithCachedSession($wsdlPath);
|
|
|
|
if ($curlResponse->curlStatusSuccess()) {
|
|
|
|
if (mb_strlen($curlResponse->getResponseBody()) === 0) {
|
2017-02-18 00:13:02 +01:00
|
|
|
throw new Exception('Could not write WSDL cache file: empty curl response from: '.$wsdlPath);
|
2017-02-03 15:22:37 +01:00
|
|
|
}
|
|
|
|
if ($resolveRemoteIncludes === true) {
|
|
|
|
$document = $this->getXmlFileDOMDocument($curl, $cacheType, $curlResponse->getResponseBody(), $wsdlPath);
|
|
|
|
$this->saveXmlDOMDocument($document, $cacheFilePath);
|
2011-10-16 19:49:24 +02:00
|
|
|
} else {
|
2017-02-03 15:22:37 +01:00
|
|
|
file_put_contents($cacheFilePath, $curlResponse->getResponseBody());
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2017-02-03 15:22:37 +01:00
|
|
|
} else {
|
|
|
|
throw new Exception('Could not write WSDL cache file: Download failed with message: '.$curlResponse->getCurlErrorMessage());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (file_exists($wsdlPath)) {
|
|
|
|
$document = $this->getXmlFileDOMDocument($curl, $cacheType, file_get_contents($wsdlPath));
|
|
|
|
$this->saveXmlDOMDocument($document, $cacheFilePath);
|
|
|
|
} else {
|
|
|
|
throw new Exception('Could write WSDL cache file: local file does not exist: '.$wsdlPath);
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-03 15:22:37 +01:00
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
private function getLocalWsdlPath($wsdlPath)
|
|
|
|
{
|
|
|
|
if (file_exists($wsdlPath)) {
|
|
|
|
|
|
|
|
return realpath($wsdlPath);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new Exception('Could not download WSDL: local file does not exist: '.$wsdlPath);
|
|
|
|
}
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-03 15:22:37 +01:00
|
|
|
* @param string $wsdlPath File URL/path
|
2011-10-16 19:49:24 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-02-03 15:22:37 +01:00
|
|
|
private function isRemoteFile($wsdlPath)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
2017-02-03 15:22:37 +01:00
|
|
|
$parsedUrlOrFalse = @parse_url($wsdlPath);
|
|
|
|
if ($parsedUrlOrFalse !== false) {
|
|
|
|
if (isset($parsedUrlOrFalse['scheme']) && substr($parsedUrlOrFalse['scheme'], 0, 4) === 'http') {
|
|
|
|
|
2013-07-24 23:18:41 +02:00
|
|
|
return true;
|
2017-02-03 15:22:37 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
throw new Exception('Could not determine wsdlPath is remote: '.$wsdlPath);
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2017-02-03 15:22:37 +01:00
|
|
|
* @param Curl $curl
|
|
|
|
* @param int $cacheType
|
|
|
|
* @param string $xmlFileSource XML file contents
|
2013-07-24 23:18:41 +02:00
|
|
|
* @param boolean $parentFilePath Parent file name
|
2017-02-03 15:22:37 +01:00
|
|
|
* @return DOMDocument
|
2011-10-16 19:49:24 +02:00
|
|
|
*/
|
2017-02-03 15:22:37 +01:00
|
|
|
private function getXmlFileDOMDocument(Curl $curl, $cacheType, $xmlFileSource, $parentFilePath = null)
|
2011-10-16 19:49:24 +02:00
|
|
|
{
|
2017-02-03 15:22:37 +01:00
|
|
|
$document = new DOMDocument('1.0', 'utf-8');
|
|
|
|
if ($document->loadXML($xmlFileSource) === false) {
|
|
|
|
throw new Exception('Could not save downloaded WSDL cache: '.$xmlFileSource);
|
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
$xpath = new DOMXPath($document);
|
|
|
|
$this->updateXmlDocument($curl, $cacheType, $xpath, Helper::PFX_WSDL, Helper::NS_WSDL, 'location', $parentFilePath);
|
|
|
|
$this->updateXmlDocument($curl, $cacheType, $xpath, Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA, 'schemaLocation', $parentFilePath);
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
return $document;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function saveXmlDOMDocument(DOMDocument $document, $cacheFilePath)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$xmlContents = $document->saveXML();
|
|
|
|
if ($xmlContents === '') {
|
|
|
|
throw new Exception('Could not write WSDL cache file: DOMDocument returned empty XML file');
|
2011-11-20 18:13:42 +01:00
|
|
|
}
|
2017-02-03 15:22:37 +01:00
|
|
|
file_put_contents($cacheFilePath, $xmlContents);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
unlink($cacheFilePath);
|
|
|
|
throw new Exception('Could not write WSDL cache file: save method returned error: ' . $e->getMessage());
|
2011-11-20 18:13:42 +01:00
|
|
|
}
|
2017-02-03 15:22:37 +01:00
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2017-02-03 15:22:37 +01:00
|
|
|
private function updateXmlDocument(
|
|
|
|
Curl $curl,
|
|
|
|
$cacheType,
|
|
|
|
DOMXPath $xpath,
|
|
|
|
$schemaPrefix,
|
|
|
|
$schemaUrl,
|
|
|
|
$locationAttributeName,
|
|
|
|
$parentFilePath = null
|
|
|
|
) {
|
|
|
|
$xpath->registerNamespace($schemaPrefix, $schemaUrl);
|
|
|
|
$nodes = $xpath->query('.//'.$schemaPrefix.':include | .//'.$schemaPrefix.':import');
|
2011-10-16 19:49:24 +02:00
|
|
|
if ($nodes->length > 0) {
|
|
|
|
foreach ($nodes as $node) {
|
2017-02-03 15:22:37 +01:00
|
|
|
/** @var DOMElement $node */
|
|
|
|
$locationPath = $node->getAttribute($locationAttributeName);
|
2017-02-17 00:14:05 +01:00
|
|
|
if ($locationPath !== '') {
|
|
|
|
if ($this->isRemoteFile($locationPath)) {
|
|
|
|
$node->setAttribute(
|
|
|
|
$locationAttributeName,
|
|
|
|
$this->getWsdlPath(
|
|
|
|
$curl,
|
|
|
|
$locationPath,
|
|
|
|
$cacheType,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if ($parentFilePath !== null) {
|
|
|
|
$node->setAttribute(
|
|
|
|
$locationAttributeName,
|
|
|
|
$this->getWsdlPath(
|
|
|
|
$curl,
|
|
|
|
$this->resolveRelativePathInUrl($parentFilePath, $locationPath),
|
|
|
|
$cacheType,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
// combine base path with relative path
|
2017-02-17 00:14:05 +01:00
|
|
|
if (isset($urlParts['path']) && mb_strlen($relative) > 0 && '/' === $relative{0}) {
|
2011-10-16 19:49:24 +02:00
|
|
|
// $relative is absolute path from domain (starts with /)
|
|
|
|
$path = $relative;
|
|
|
|
} elseif (isset($urlParts['path']) && strrpos($urlParts['path'], '/') === (strlen($urlParts['path']) )) {
|
|
|
|
// base path is directory
|
2013-07-24 23:18:41 +02:00
|
|
|
$path = $urlParts['path'].$relative;
|
2011-10-16 19:49:24 +02:00
|
|
|
} elseif (isset($urlParts['path'])) {
|
|
|
|
// strip filename from base path
|
2013-07-24 23:18:41 +02:00
|
|
|
$path = substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')).'/'.$relative;
|
2011-10-16 19:49:24 +02:00
|
|
|
} else {
|
|
|
|
// no base path
|
2013-07-24 23:18:41 +02:00
|
|
|
$path = '/'.$relative;
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
// foo/./bar ==> foo/bar
|
|
|
|
// remove double slashes
|
2013-07-24 23:18:41 +02:00
|
|
|
$path = preg_replace(array('#/\./#', '#/+#'), '/', $path);
|
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
// split path by '/'
|
|
|
|
$parts = explode('/', $path);
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
// resolve /../
|
|
|
|
foreach ($parts as $key => $part) {
|
2017-02-03 15:22:37 +01:00
|
|
|
if ($part === '..') {
|
2013-07-24 23:18:41 +02:00
|
|
|
$keyToDelete = $key - 1;
|
2011-10-16 19:49:24 +02:00
|
|
|
while ($keyToDelete > 0) {
|
|
|
|
if (isset($parts[$keyToDelete])) {
|
|
|
|
unset($parts[$keyToDelete]);
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
|
|
|
$keyToDelete--;
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
2011-10-16 19:49:24 +02:00
|
|
|
unset($parts[$key]);
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
|
|
|
|
$hostname = $urlParts['scheme'].'://'.$urlParts['host'];
|
2011-10-16 19:49:24 +02:00
|
|
|
if (isset($urlParts['port'])) {
|
2013-07-24 23:18:41 +02:00
|
|
|
$hostname .= ':'.$urlParts['port'];
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2011-12-11 21:20:35 +01:00
|
|
|
|
2013-07-24 23:18:41 +02:00
|
|
|
return $hostname.implode('/', $parts);
|
2011-10-16 19:49:24 +02:00
|
|
|
}
|
2013-07-24 23:18:41 +02:00
|
|
|
}
|