BeSimpleSoap/src/BeSimple/SoapClient/WsdlDownloader.php

225 lines
7.8 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of the BeSimpleSoapClient.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
2011-10-02 12:09:19 +02:00
*
* @link https://github.com/BeSimple/BeSimpleSoapClient
*/
namespace BeSimple\SoapClient;
/**
* Downloads WSDL files with cURL. Uses all SoapClient options for
* authentication. 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
*/
class WsdlDownloader
{
/**
* Cache enabled.
*
* @var bool
*/
private $cacheEnabled;
/**
* Cache dir.
*
* @var string
*/
private $cacheDir;
/**
* Cache TTL.
*
* @var int
*/
private $cacheTtl;
/**
* Options array
*
* @var array(string=>mixed)
*/
private $options = array();
/**
* Constructor.
2011-10-02 12:09:19 +02:00
*
* @param array $options
*/
2011-10-02 12:09:19 +02:00
public function __construct(array $options = array())
{
// get current WSDL caching config
2011-10-02 12:09:19 +02:00
$this->cacheEnabled = (bool)ini_get('soap.wsdl_cache_enabled');
if ($this->cacheEnabled === true
&& isset($options['cache_wsdl'])
&& $options['cache_wsdl'] === WSDL_CACHE_NONE) {
$this->cacheEnabled = false;
}
2011-10-02 12:09:19 +02:00
$this->cacheDir = ini_get('soap.wsdl_cache_dir');
if (!is_dir($this->cacheDir)) {
$this->cacheDir = sys_get_temp_dir();
}
2011-10-02 12:09:19 +02:00
$this->cacheDir = rtrim($this->cacheDir, '/\\');
$this->cacheTtl = ini_get('soap.wsdl_cache_ttl');
$this->options = $options;
2011-10-02 12:09:19 +02:00
if (!isset($this->options['resolve_xsd_includes'])) {
$this->options['resolve_xsd_includes'] = true;
}
}
/**
* Download given WSDL file and return name of cache file.
*
* @param string $wsdl
* @return string
*/
2011-10-02 12:09:19 +02:00
public function download($wsdl)
{
// download and cache remote WSDL files or local ones where we want to
// resolve remote XSD includes
2011-10-02 12:09:19 +02:00
$isRemoteFile = $this->isRemoteFile($wsdl);
if ($isRemoteFile === true || $this->options['resolve_xsd_includes'] === true) {
$cacheFile = $this->cacheDir . DIRECTORY_SEPARATOR . 'wsdl_' . md5($wsdl) . '.cache';
if ($this->cacheEnabled === false
|| !file_exists($cacheFile)
|| (filemtime($cacheFile) + $this->cacheTtl) < time()) {
if ($isRemoteFile === true) {
// new curl object for request
2011-10-02 12:09:19 +02:00
$curl = new Curl($this->options);
// execute request
2011-10-02 12:09:19 +02:00
$responseSuccessfull = $curl->exec($wsdl);
// get content
2011-10-02 12:09:19 +02:00
if ($responseSuccessfull === true) {
$response = $curl->getResponseBody();
2011-10-02 12:09:19 +02:00
if ($this->options['resolve_xsd_includes'] === true) {
$this->resolveXsdIncludes($response, $cacheFile, $wsdl);
} else {
file_put_contents($cacheFile, $response);
}
2011-10-02 12:09:19 +02:00
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
2011-10-02 12:09:19 +02:00
} elseif (file_exists($wsdl)) {
$response = file_get_contents($wsdl);
$this->resolveXsdIncludes($response, $cacheFile);
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
}
return $cacheFile;
2011-10-02 12:09:19 +02:00
} elseif (file_exists($wsdl)) {
return realpath($wsdl);
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
}
}
/**
* Do we have a remote file?
*
* @param string $file
* @return boolean
*/
2011-10-02 12:09:19 +02:00
private function isRemoteFile($file)
{
$isRemoteFile = false;
// @parse_url to suppress E_WARNING for invalid urls
2011-10-02 12:09:19 +02:00
if (($url = @parse_url($file)) !== false) {
if (isset($url['scheme']) && substr($url['scheme'], 0, 4) == 'http') {
$isRemoteFile = true;
}
}
return $isRemoteFile;
}
/**
* Resolves remote XSD includes within the WSDL files.
*
* @param string $xml
* @param string $cacheFile
* @param unknown_type $parentIsRemote
* @return string
*/
2011-10-02 12:09:19 +02:00
private function resolveXsdIncludes($xml, $cacheFile, $parentFile = null)
{
$doc = new \DOMDocument();
2011-10-02 12:09:19 +02:00
$doc->loadXML($xml);
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace(Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA);
$query = './/' . Helper::PFX_XML_SCHEMA . ':include';
2011-10-02 12:09:19 +02:00
$nodes = $xpath->query($query);
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$schemaLocation = $node->getAttribute('schemaLocation');
if ($this->isRemoteFile($schemaLocation)) {
$schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation);
} elseif (!is_null($parentFile)) {
$schemaLocation = $this->resolveRelativePathInUrl($parentFile, $schemaLocation);
$schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation);
}
}
}
2011-10-02 12:09:19 +02:00
$doc->save($cacheFile);
}
/**
* Resolves the relative path to base into an absolute.
*
* @param string $base
* @param string $relative
* @return string
*/
2011-10-02 12:09:19 +02:00
private function resolveRelativePathInUrl($base, $relative)
{
2011-10-02 12:09:19 +02:00
$urlParts = parse_url($base);
// combine base path with relative path
2011-10-02 12:09:19 +02:00
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
2011-10-02 12:09:19 +02:00
$path = preg_replace('~/\./~', '/', $path);
// remove double slashes
2011-10-02 12:09:19 +02:00
$path = preg_replace('~/+~', '/', $path);
// split path by '/'
2011-10-02 12:09:19 +02:00
$parts = explode('/', $path);
// resolve /../
2011-10-02 12:09:19 +02:00
foreach ($parts as $key => $part) {
if ($part == "..") {
$keyToDelete = $key-1;
while ($keyToDelete > 0) {
if (isset($parts[$keyToDelete])) {
unset($parts[$keyToDelete]);
break;
} else {
$keyToDelete--;
}
}
2011-10-02 12:09:19 +02:00
unset($parts[$key]);
}
}
2011-10-02 12:09:19 +02:00
return $urlParts['scheme'] . '://' . $urlParts['host'] . implode('/', $parts);
}
}