* @license PHP License * @package WB * @subpackage base */ /** * Stream * * Wrap stream * * @version 0.4.0 * @package WB * @subpackage base */ class WBStream { /** * kind of fopen * * Wrapper to fopen using proxy, if required * * * @throws WBException_File if stream could not be opened * @param string $url * @param string $mode fopen mode * @param bool $useIncludePath whether to use include path * @param array $contextOptions stream context options * @return resource */ static public function open($url, $mode, $useIncludePath = false, $contextOptions = array()) { // create strem context $context = self::getContext($url, $contextOptions); $resource = fopen($url, $mode, false, $context); if (!is_resource($resource)) { WBClass::load('WBException_File'); throw new WBException_File('Could not open stream: "' . $url . '"', 1, __CLASS__); } return $resource; } /** * create stream context for URL * * Load context options on demand. * * @param string $url * @param array $contextOptions stream context options * @return stream context */ static private function getContext($url, $contextOptions = array()) { $options = self::getStreamContextOptions($url, $contextOptions); return stream_context_create($options); } /** * Get context options * * @param string $url * @param array $contextOptions stream context options * @return array stream context options */ static public function getStreamContextOptions($url, $contextOptions = array()) { $type = 'file'; if (strncmp($url, 'http://', 7) == 0 || strncmp($url, 'https://', 8) == 0) { $type = 'http'; } $option = array(); if (isset($contextOptions['ssl'])) { $option['ssl'] = $contextOptions['ssl']; unset($contextOptions['ssl']); } if (isset($contextOptions['socket'])) { $option['socket'] = $contextOptions['socket']; unset($contextOptions['socket']); } switch ($type) { // proxy support for HTTP case 'http': $option['http'] = array(); $conf = WBClass::Create('WBConfig'); /** @var conf WBConfig */ $conf->load('config'); $net = $conf->get('network/http', array()); if (isset($net['proxy']) && $net['proxy']) { $option['http']['proxy'] = $net['proxy']; $option['http']['request_fulluri'] = true; } if (isset($net['useragent']) && $net['useragent']) { $option['http']['user_agent'] = $net['useragent']; } $option['http'] = array_merge($option['http'], $contextOptions); break; case 'file': default: break; } return $option; } }