-
Notifications
You must be signed in to change notification settings - Fork 48
/
recv.php
54 lines (50 loc) · 1.66 KB
/
recv.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
<?php
# Example receiver of Scrup files
#
# Install by putting this file on your web server and give the web server
# user write permissions to the directory in which you put this script.
#
$MAXLENGTH = 4096000; # 4 MB
function rsperr($msg='', $st='400 Bad Request') {
header('HTTP/1.1 '.$st);
exit($msg);
}
function pathfromid($id, $suffix='') {
return substr($id,0,2).'/'.substr($id,2).$suffix;
}
# Build path and url
if (!isset($_GET['name']) || !trim($_GET['name']))
$_GET['name'] = strval(time());
$id = substr(base_convert(md5($_GET['name'].' '.$_SERVER['REMOTE_ADDR']), 16, 36),0,15);
$suffix = strrchr($_GET['name'], '.');
$path = pathfromid($id, $suffix);
$abspath = dirname(realpath(__FILE__)).'/'.$path;
$url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://')
. $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . '/' . $path;
# make dir if needed
$dirpath = dirname($abspath);
if (!file_exists($dirpath) && @mkdir($dirpath, 0775) === false)
rsperr('failed to mkdir '.$dirpath, '500 Internal Server Error');
# Save input to file
$dstf = @fopen($abspath, 'w');
if (!$dstf)
rsperr('unable to write to '.$dirpath, '500 Internal Server Error');
$srcf = fopen('php://input', 'r');
$size = stream_copy_to_stream($srcf, $dstf, $MAXLENGTH);
fclose($dstf);
# No input?
if ($size === 0) {
@unlink($path);
rsperr('empty input');
}
elseif ($size >= $MAXLENGTH) {
@unlink($path); # because it's probably broken
rsperr('Request entity larger than or equal to '.$MAXLENGTH.' B',
'413 Request Entity Too Large');
}
# Respond with the url
header('HTTP/1.1 201 Created');
header('Content-Type: text/plain; charset=utf-8');
header('Content-Length: '.strlen($url));
echo $url;
?>