Skip to content

Commit b7953df

Browse files
committed
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
1 parent e70c827 commit b7953df

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

config-templates/config.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,13 @@
544544
'metadata.sign.privatekey_pass' => NULL,
545545
'metadata.sign.certificate' => NULL,
546546

547-
);
548547

548+
/*
549+
* Proxy to use for retrieving URLs.
550+
*
551+
* Example:
552+
* 'proxy' => 'tcp://proxy.example.com:5100'
553+
*/
554+
'proxy' => NULL,
549555

550-
?>
556+
);

lib/SimpleSAML/Utilities.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,39 @@ public static function debugMessage($message, $type) {
19501950
}
19511951
}
19521952

1953-
}
19541953

1955-
?>
1954+
/**
1955+
* Helper function to retrieve a file or URL with proxy support.
1956+
*
1957+
* An exception will be thrown if we are unable to retrieve the data.
1958+
*
1959+
* @param string $path The path or URL we should fetch.
1960+
* @param array $context Extra context options. This parameter is optional.
1961+
* @return string The data we fetched.
1962+
*/
1963+
public static function fetch($path, $context = array()) {
1964+
assert('is_string($path)');
1965+
1966+
$config = SimpleSAML_Configuration::getInstance();
1967+
1968+
$proxy = $config->getString('proxy', NULL);
1969+
if ($proxy !== NULL) {
1970+
if (!isset($context['http']['proxy'])) {
1971+
$context['http']['proxy'] = $proxy;
1972+
}
1973+
if (!isset($context['http']['request_fulluri'])) {
1974+
$context['http']['request_fulluri'] = TRUE;
1975+
}
1976+
}
1977+
1978+
$context = stream_context_create($context);
1979+
1980+
$data = file_get_contents($path, FALSE, $context);
1981+
if ($data === FALSE) {
1982+
throw new SimpleSAML_Error_Exception('Error fetching ' . var_export($path, TRUE) . ':' . self::getLastError());
1983+
}
1984+
1985+
return $data;
1986+
}
1987+
1988+
}

0 commit comments

Comments
 (0)