From b7953df9a2a9ee5228dc5d15f67d8f482c2b7f48 Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Tue, 30 Nov 2010 10:06:03 +0000 Subject: [PATCH] Utilities: Add fetch()-function, for retrieving URLs via a proxy. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2659 44740490-163a-0410-bde0-09ae8108e29a --- config-templates/config.php | 10 ++++++++-- lib/SimpleSAML/Utilities.php | 37 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/config-templates/config.php b/config-templates/config.php index a864e976f..85942703e 100644 --- a/config-templates/config.php +++ b/config-templates/config.php @@ -544,7 +544,13 @@ $config = array ( 'metadata.sign.privatekey_pass' => NULL, 'metadata.sign.certificate' => NULL, -); + /* + * Proxy to use for retrieving URLs. + * + * Example: + * 'proxy' => 'tcp://proxy.example.com:5100' + */ + 'proxy' => NULL, -?> \ No newline at end of file +); diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index de336154b..46a0ea887 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1950,6 +1950,39 @@ class SimpleSAML_Utilities { } } -} -?> \ No newline at end of file + /** + * Helper function to retrieve a file or URL with proxy support. + * + * An exception will be thrown if we are unable to retrieve the data. + * + * @param string $path The path or URL we should fetch. + * @param array $context Extra context options. This parameter is optional. + * @return string The data we fetched. + */ + public static function fetch($path, $context = array()) { + assert('is_string($path)'); + + $config = SimpleSAML_Configuration::getInstance(); + + $proxy = $config->getString('proxy', NULL); + if ($proxy !== NULL) { + if (!isset($context['http']['proxy'])) { + $context['http']['proxy'] = $proxy; + } + if (!isset($context['http']['request_fulluri'])) { + $context['http']['request_fulluri'] = TRUE; + } + } + + $context = stream_context_create($context); + + $data = file_get_contents($path, FALSE, $context); + if ($data === FALSE) { + throw new SimpleSAML_Error_Exception('Error fetching ' . var_export($path, TRUE) . ':' . self::getLastError()); + } + + return $data; + } + +} -- GitLab