Last active
January 30, 2017 17:19
-
-
Save noogen/003bcb9d885e53073f37f808d32af877 to your computer and use it in GitHub Desktop.
Limit download per ip
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
<? | |
$path = addslashes($_GET["file"]); | |
$ip = addslashes($_SERVER['REMOTE_ADDR']); | |
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
$dl = false; | |
// get cache dir | |
$ipslug = strtolower(preg_replace('/[^A-Za-z0-9]+/', '_', $ip)); | |
$cacheDir = './dlperip/' . $ipslug; | |
if(!is_dir($cacheDir)) { | |
if (!mkdir($cacheDir, 0755, true)) { | |
die('Failed to create temp cache folders...'); | |
} | |
} | |
$cacheFile = $cacheDir . '/' . hash('md5', $path) . '.txt'; | |
if (!file_exists($cacheFile)) { | |
$fp1 = fopen($cacheFile, "w"); | |
fputs($fp1 , '0'); | |
fclose($fp1); | |
} | |
$content = file($cacheFile); // reading all lines into array | |
$counter = intval($content[0]) + 1; // getting first line | |
file_put_contents($cacheFile, $counter); // writing data | |
# limit one, change this if you want to change the limit | |
if ($counter < 2) { | |
$dl = true; | |
} | |
// reset | |
if ($_GET["reset"] == "some-secret") { | |
unlink($cacheFile); | |
} | |
if ($dl) { | |
$fullPath = "./files/".$path; | |
if ($fd = fopen ($fullPath, "r")) { | |
$fname = basename($fullPath); | |
header('Content-type: application/octet-stream'); | |
header('Content-Disposition: filename="'.$fname.'"'); | |
header('Content-length: '.filesize($fullPath)); | |
header('Cache-control: private'); | |
while(!feof($fd)) { | |
$buffer = fread($fd, 2048); | |
echo $buffer; | |
} | |
fclose ($fd); | |
exit; | |
} | |
} else { | |
header('HTTP/1.0 503 Service Unavailable'); | |
die('Abort, you reached your download limit for this file.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment