-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathput.php
103 lines (82 loc) · 2.79 KB
/
put.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
103
<?php
require_once("config.php");
if ($_SERVER['REQUEST_METHOD'] !== 'PUT') {
header("Location: " . SITE_URL);
exit();
}
header('Content-Type: text/plain');
define("CI_SNAPSHOTS", true);
require_once('lib/functions.common.php');
require_once('lib/functions.http.php');
if ((!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) && PUT_REQUIRES_AUTH) {
ExitUnauthorized();
}
// bootstrap web-app.
require_once('lib/init.php');
// test given auth credentials.
if (PUT_REQUIRES_AUTH) {
if (!CheckUserCredentials($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
ExitForbidden();
}
}
if (getSnapshotDirectorySize() >= MAX_CAPACITY_BYTES && MAX_CAPACITY_DELETE_OLDEST == false) {
ExitFullStorage();
}
$basename = basename($_SERVER['REQUEST_URI']);
$uri_parts = pathinfo($basename);
if (! isSafeExtension($uri_parts['extension'])) {
ExitFailedRequest("Failed to save {$basename} - Bad Extension\n");
}
$tmpname = getTempFileName();
$tmpfp = fopen($tmpname, "w");
// PUT data comes in on stdin
$putdata = fopen("php://input", "r");
while ($data = fread($putdata, 8192)) {
fwrite($tmpfp, $data);
}
fclose($tmpfp);
fclose($putdata);
if (getSnapshotDirectorySize() >= MAX_CAPACITY_BYTES && MAX_CAPACITY_DELETE_OLDEST == true) {
$targetSize = filesize($tmpname);
$clearedSize = 0;
$stmt = $dbh->prepare("SELECT `file_name`, `file_key`, `time_created`
FROM `Snapshots`
ORDER BY `time_created` ASC
LIMIT 10 ");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$delFilePath = getSnapshotFilePath($row['file_name'], $row['file_key']);
if (is_file($delFilePath)) {
$clearedSize = $clearedSize + filesize($delFilePath);
unlink($delFilePath);
}
if ($clearedSize >= $targetSize) {
break;
}
}
}
$snapshot_ids = makeSnapshotFileIDs($basename);
$snapshot_path = $snapshot_ids[0];
$snapshot_url = $snapshot_ids[1];
$snapshot_uid = $snapshot_ids[2];
if (rename($tmpname, $snapshot_path)) {
$maxdays = DEFAULT_FILE_LIFETIME_DAYS;
if (isset($_SERVER['HTTP_MAX_DAYS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DAYS']) === 1) {
$maxdays = intval($_SERVER['HTTP_MAX_DAYS']);
}
}
$maxdl = 0;
if (isset($_SERVER['HTTP_MAX_DOWNLOADS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DOWNLOADS']) === 1) {
$maxdl = intval($_SERVER['HTTP_MAX_DOWNLOADS']);
}
}
echo($snapshot_url);
echo("\n");
AddNewSnapshot($basename, $snapshot_uid, $maxdays, $maxdl);
AddUploadLogRecord($snapshot_path);
} else {
ExitFailedRequest("Failed to save {$basename}\n");
unlink($tmpname);
}