-
-
Save elgammalqa/b6c359fd91c8a38197b502de020e6714 to your computer and use it in GitHub Desktop.
Simple PHP image proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$PROXY = 'https://yourdomain.top/proxy.php?url='; | |
# This script forwards the call | |
# Check for url parameter, and prevent file transfer | |
if (isset($_GET['url']) and preg_match('#^https?://#', $_GET['url']) === 1) { | |
$url .= $_GET['url']; | |
} else { | |
header('HTTP/1.1 404 Not Found'); | |
exit; | |
} | |
# Check if the client already has the requested item | |
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) or | |
isset($_SERVER['HTTP_IF_NONE_MATCH'])) { | |
header('HTTP/1.1 304 Not Modified'); | |
exit; | |
} | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Searsia/1.0'); | |
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); | |
curl_setopt($ch, CURLOPT_BUFFERSIZE, 12800); | |
curl_setopt($ch, CURLOPT_NOPROGRESS, false); | |
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($DownloadSize, $Downloaded, $UploadSize, $Uploaded) { return ($Downloaded > 1024 * 512) ? 1 : 0; } ); # max 500kb | |
$out = curl_exec ($ch); | |
curl_close ($ch); | |
$file_array = explode("\r\n\r\n", $out, 2); | |
$header_array = explode("\r\n", $file_array[0]); | |
foreach($header_array as $header_value) { | |
$header_pieces = explode(': ', $header_value); | |
if(count($header_pieces) == 2) { | |
$headers[$header_pieces[0]] = trim($header_pieces[1]); | |
} | |
} | |
if (array_key_exists('Location', $headers)) { | |
$newurl = urlencode($headers['Location']); | |
header("HTTP/1.1 301 Moved Permanently"); | |
header('Location: ' . $PROXY . $newurl); | |
} else { | |
if (array_key_exists('Content-Type', $headers)) { | |
$ct = $headers['Content-Type']; | |
if (preg_match('#image/png|image/.*icon|image/jpe?g|image/gif#', $ct) !== 1) { | |
header('HTTP/1.1 404 Not Found'); | |
exit; | |
} | |
header('Content-Type: ' . $ct); | |
} | |
if (array_key_exists('Content-Length', $headers)) | |
header('Content-Length: ' . $headers['Content-Length']); | |
if (array_key_exists('Expires', $headers)) | |
header('Expires: ' . $headers['Expires']); | |
if (array_key_exists('Cache-Control', $headers)) | |
header('Cache-Control: ' . $headers['Cache-Control']); | |
if (array_key_exists('Last-Modified', $headers)) | |
header('Last-Modified: ' . $headers['Last-Modified']); | |
echo $file_array[1]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment