StreamContextFactory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. /**
  13. * Allows the creation of a basic context supporting http proxy
  14. *
  15. * @author Jordan Alliot <jordan.alliot@gmail.com>
  16. */
  17. final class StreamContextFactory
  18. {
  19. /**
  20. * Creates a context supporting HTTP proxies
  21. *
  22. * @param array $options Options to merge with the default
  23. * @param array $params Parameters to specify on the context
  24. * @return resource Default context
  25. * @throws \RuntimeException if https proxy required and OpenSSL uninstalled
  26. */
  27. static public function getContext(array $options = array(), array $params = array())
  28. {
  29. $options = array_merge(array('http' => array()), $options);
  30. // Handle system proxy
  31. if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) {
  32. // Some systems seem to rely on a lowercased version instead...
  33. $proxy = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : $_SERVER['http_proxy'];
  34. // http(s):// is not supported in proxy
  35. $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy);
  36. if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
  37. throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
  38. }
  39. $options['http'] = array(
  40. 'proxy' => $proxy,
  41. 'request_fulluri' => true,
  42. );
  43. }
  44. return stream_context_create($options, $params);
  45. }
  46. }