file_get_contents wrapper

master
pecesama 2007-04-28 04:49:08 +00:00
parent 5498206a5a
commit b6b983de98
2 changed files with 99 additions and 3 deletions

View File

@ -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;
}
?>

View File

@ -111,7 +111,7 @@ class gelato extends Conexion_Mysql {
$id_video = getVimeoVideoUrl($url);
return "\t\t\t<object type=\"application/x-shockwave-flash\" style=\"width:500px;height:393px\" data=\"http://www.vimeo.com/moogaloop.swf?clip_id=".$id_video."\"><param name=\"movie\" value=\"http://www.vimeo.com/moogaloop.swf?clip_id=".$id_video."\" /></object>\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 \"");