diff --git a/src/phpbrowscap/Browscap.php b/src/phpbrowscap/Browscap.php index 82748b6..f6fa62a 100644 --- a/src/phpbrowscap/Browscap.php +++ b/src/phpbrowscap/Browscap.php @@ -601,62 +601,68 @@ protected function _getRemoteData($url) throw new Browscap_Exception('Cannot open the local file'); } case self::UPDATE_FOPEN: - $file = file_get_contents($url); - - if ($file !== false) { - return $file; + if (ini_get('allow_url_fopen') && function_exists('file_get_contents')) { + $file = file_get_contents($url); + + if ($file !== false) { + return $file; + } } // else try with the next possibility (break omitted) case self::UPDATE_FSOCKOPEN: - $remote_url = parse_url($url); - $remote_handler = fsockopen($remote_url['host'], 80, $c, $e, $this->timeout); - - if ($remote_handler) { - stream_set_timeout($remote_handler, $this->timeout); - - if (isset($remote_url['query'])) { - $remote_url['path'] .= '?' . $remote_url['query']; - } - - $out = sprintf( - self::REQUEST_HEADERS, - $remote_url['path'], - $remote_url['host'], - $this->_getUserAgent() - ); - - fwrite($remote_handler, $out); - - $response = fgets($remote_handler); - if (strpos($response, '200 OK') !== false) { - $file = ''; - while (!feof($remote_handler)) { - $file .= fgets($remote_handler); + if (function_exists('fsockopen')) { + $remote_url = parse_url($url); + $remote_handler = fsockopen($remote_url['host'], 80, $c, $e, $this->timeout); + + if ($remote_handler) { + stream_set_timeout($remote_handler, $this->timeout); + + if (isset($remote_url['query'])) { + $remote_url['path'] .= '?' . $remote_url['query']; + } + + $out = sprintf( + self::REQUEST_HEADERS, + $remote_url['path'], + $remote_url['host'], + $this->_getUserAgent() + ); + + fwrite($remote_handler, $out); + + $response = fgets($remote_handler); + if (strpos($response, '200 OK') !== false) { + $file = ''; + while (!feof($remote_handler)) { + $file .= fgets($remote_handler); + } + + $file = str_replace("\r\n", "\n", $file); + $file = explode("\n\n", $file); + array_shift($file); + + $file = implode("\n\n", $file); + + fclose($remote_handler); + + return $file; } - - $file = str_replace("\r\n", "\n", $file); - $file = explode("\n\n", $file); - array_shift($file); - - $file = implode("\n\n", $file); - - fclose($remote_handler); - - return $file; } } // else try with the next possibility case self::UPDATE_CURL: - $ch = curl_init($url); - - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); - curl_setopt($ch, CURLOPT_USERAGENT, $this->_getUserAgent()); - - $file = curl_exec($ch); - - curl_close($ch); - - if ($file !== false) { - return $file; + if (extension_loaded('curl')) { // make sure curl is loaded + $ch = curl_init($url); + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); + curl_setopt($ch, CURLOPT_USERAGENT, $this->_getUserAgent()); + + $file = curl_exec($ch); + + curl_close($ch); + + if ($file !== false) { + return $file; + } } // else try with the next possibility case false: throw new Browscap_Exception('Your server can\'t connect to external resources. Please update the file manually.');