From b6b983de98faa2cae0661b9abfbadf2a9b63abb1 Mon Sep 17 00:00:00 2001 From: pecesama Date: Sat, 28 Apr 2007 04:49:08 +0000 Subject: [PATCH] file_get_contents wrapper --- classes/functions.php | 100 ++++++++++++++++++++++++++++++++++++++- classes/gelato.class.php | 2 +- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/classes/functions.php b/classes/functions.php index 92b5d43..ec940cd 100644 --- a/classes/functions.php +++ b/classes/functions.php @@ -63,7 +63,7 @@ function getFile($remoteFileName) { $fileName = "../uploads/".getFileName($remoteFileName); - $str = file_get_contents($remoteFileName); + $str = _file_get_contents($remoteFileName); if (!$handle = fopen($fileName, 'w')) { return false; } @@ -151,5 +151,101 @@ } closedir($handle); return $dirs; - } + } + + function _file_get_contents($path) { + // Modified function from: + // http://work.dokoku.net/Anieto2k/_file_get_contents.phps + // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/ + if (!preg_match('/^http/i', $path)) { + if ($fp = fopen($path, 'r')) { + return fread($fp, 1024); + } else { + return false; + } + } else { + if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) { + $ch = curl_init(); + $timeout = 5; + curl_setopt ($ch, CURLOPT_URL, $path); + curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + $file_contents = curl_exec($ch); + curl_close($ch); + if (is_string($file_contents)) { + return $file_contents; + } else { + return false; + } + } else { + $data = parse_url($path); + if (!$data['host'] || $data['scheme'] != "http") { + return false; + } + + $f = fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3); + if (!$f) { + return false; + } + + $q = "GET " . $data['path'] . "?" . $data['query'] . " HTTP/1.1\r\n"; + $q .= "Host: " . $data['host'] . "\r\n"; + $q .= "Connection: close\r\n"; + $q .= "Referer: http://www.gelatocms.com/\r\n\r\n"; + + $recv = ""; + fwrite($f, $q); + while (!feof($f)) { + $recv .= fread($f, 1024); + } + + $request = $q; + $response = substr($recv, 0, strpos($recv, "\r\n\r\n")); + $body = substr($recv, strpos($recv, "\r\n\r\n") + 4); + + if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) { + if ($res[1][0] != "2") { + return false; + } + } else { + return false; + } + + if (preg_match('/transfer-encoding:\s*chunked/i', $response)) { + $tmp_body = $body; + $new = ""; + $exit = false; + while (!$exit) { + if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) { + $len = hexdec($res[1]); + if ($len == "0") { + $exit = true; + break; + } + $new .= substr($tmp_body, strlen($res[0]), $len); + $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n")); + } else { + $exit = true; + } + } + $body = $new; + } + return $body; + } + } + } + + function get_curl_version() { + $curl = 0; + if (is_array(curl_version())) { + $curl = curl_version(); + $curl = $curl['version']; + } else { + $curl = curl_version(); + $curl = explode(' ', $curl); + $curl = explode('/', $curl[0]); + $curl = $curl[1]; + } + return $curl; + } ?> \ No newline at end of file diff --git a/classes/gelato.class.php b/classes/gelato.class.php index bf17017..abaac0b 100644 --- a/classes/gelato.class.php +++ b/classes/gelato.class.php @@ -111,7 +111,7 @@ class gelato extends Conexion_Mysql { $id_video = getVimeoVideoUrl($url); return "\t\t\t\n"; } elseif (isGoogleVideo($url)) { - $html = trim(file_get_contents($url)); + $html = trim(_file_get_contents($url)); $start_code = strpos($html, "var flashObj =\n \""); $end_code = strpos($html, "\";\n flashObj = flashObj.replace"); $start_code = $start_code + strlen("var flashObj =\n \"");