forked from joshpangell/single-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
102 lines (90 loc) · 2.61 KB
/
generate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* This file creates the single use download link
* The query string should be the password (which is set in variables.php)
* If the password fails, then 404 is rendered
*
* Expects: generate.php?1234
*/
include("variables.php");
// Grab the query string as a password
$password = trim($_SERVER['QUERY_STRING']);
// Get a human readable file size from bytes
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
/*
* Verify the admin password (in variables.php)
*/
if($password == ADMIN_PASSWORD) {
// Create a list of files to download from
$download_list = array();
if(is_array($PROTECTED_DOWNLOADS)) {
foreach ($PROTECTED_DOWNLOADS as $key => $download) {
// Create a new key
$new = uniqid('key',TRUE);
// get download link and file size
$download_link = "http://" . $_SERVER['HTTP_HOST'] . DOWNLOAD_PATH . "?key=" . $new . "&i=" . $key;
$filesize = human_filesize(filesize($download['protected_path']), 2);
// Add to the download list
$download_list[] = array(
'download_link' => $download_link,
'filesize' => $filesize
);
/*
* Create a protected directory to store keys in
*/
if(!is_dir('keys')) {
mkdir('keys');
$file = fopen('keys/.htaccess','w');
fwrite($file,"Order allow,deny\nDeny from all");
fclose($file);
}
/*
* Write the key key to the keys list
*/
$file = fopen('keys/keys','a');
fwrite($file,"{$new}\n");
fclose($file);
}
}
?>
<html>
<head>
<title>Download created</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<link href="bootstrap/css/docs.css" rel="stylesheet">
<link href="bootstrap/google-code-prettify/prettify.css" rel="stylesheet">
<style>
body {
padding-top: 25px;
}
</style>
</head>
<body>
<div class="container">
<h1>Download key created</h1>
<h6>Your new single-use download links:<h6><br>
<? foreach ($download_list as $download) { ?>
<h4>
<a href="<?= $download['download_link'] ?>"><?= $download['download_link'] ?></a><br>
Size: <?= $download['filesize'] ?>
</h4>
<? } ?>
<br><br>
<a href="/singleuse">Back to the demo</a>
</div>
</body>
</html>
<?php
} else {
/*
* Someone stumbled upon this link with the wrong password
* Fake a 404 so it does not look like this is a correct path
*/
header("HTTP/1.0 404 Not Found");
}
?>