moazzamabbas / phpbrowscap

Automatically exported from code.google.com/p/phpbrowscap
GNU Lesser General Public License v2.1
0 stars 0 forks source link

private function _getRemoteData($url) #9

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
    private function _getRemoteData($url)
    {
        switch ($this->updateMethod) {
            case null:
            case self::UPDATE_FOPEN:
                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:
                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;
                        }
                    } // else try with the next possibility
                }
            case self::UPDATE_CURL:
                if (extension_loaded('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;
                    } // else try with the next possibility
                }
            case self::UPDATE_LOCAL:
                if($this->localFile && is_readable($this->localFile)){
                    $file = file_get_contents($this->localFile);

                    if ($file !== false) {
                        return $file;
                    } else {
                        throw new Browscap_Exception('Cannot open the local
file');
                    }
                }
            default:
                throw new Browscap_Exception('Your server can\'t connect to external
resources. Please update the file manually.');
        }
    }

Original issue reported on code.google.com by zdanowic...@gmail.com on 14 May 2009 at 12:03