forked from hushfile/hushfile-php-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhushfile.php
154 lines (138 loc) · 5.02 KB
/
hushfile.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
$config = json_decode(file_get_contents('config.json'));
function get_uniqid() {
$fileid = uniqid();
if (file_exists($datapath.$fileid)) {
//somehow this ID already exists, call recursively
$fileid = get_uniqid();
};
return $fileid;
}
if($_SERVER["REQUEST_URI"] == "/api/upload") {
// THIS IS A FILE UPLOAD, ONLY POST ACCEPTED
if(isset($_REQUEST['cryptofile']) && isset($_REQUEST['metadata']) && isset($_REQUEST['deletepassword'])) {
// first get a new unique ID for this file
$fileid = get_uniqid();
$cryptofile = $config->data_path.$fileid."/cryptofile.dat";
$metadatafile = $config->data_path.$fileid."/metadata.dat";
$serverdatafile = $config->data_path.$fileid."/serverdata.json";
// create folder for this file
mkdir($config->data_path.$fileid);
// write encrypted file
$fh = fopen($cryptofile, 'w') or die(json_encode(array("status" => "unable to write cryptofile", "fileid" => "")));
fwrite($fh, $_REQUEST['cryptofile']);
fclose($fh);
// write metadata file
$fh = fopen($metadatafile, 'w') or die(json_encode(array("status" => "unable to write metadatafile", "fileid" => "")));
fwrite($fh, $_REQUEST['metadata']);
fclose($fh);
// write serverdata file
$fh = fopen($serverdatafile, 'w') or die(json_encode(array("status" => "unable to write serverdatafile", "fileid" => "")));
$json = json_encode(array(
"deletepassword" => $_REQUEST['deletepassword'],
"clientip" => $_SERVER['REMOTE_ADDR']
));
fwrite($fh, $json);
fclose($fh);
// send email
if ($config->admin->send_email === true) {
$to = "{$config->admin->name} <{$config->admin->email}>";
$subject = "new file uploaded to hushfile.it";
$message = "new file uploaded to hushfile.it: http://hushfile.it/" . $fileid;
$from = $config->email_sender;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
}
// encode json reply
echo json_encode(array("status" => "ok", "fileid" => $fileid));
} else {
header("Status: 400 Bad Request");
die(json_encode(array("status" => "invalid upload request, error", "fileid" => "")));
}
} else {
// parse URL
$url = parse_url($_SERVER['REQUEST_URI']);
$vars = explode("&",$url['query']);
foreach($vars as $element) {
if(strpos($element,"=") === false) {
$params[$element] = null;
} else {
$key = substr($element,0,strpos($element,"="));
$params[$key] = substr($element,strpos($element,"=")+1);
};
};
// check if fileid is in the params
if(isset($params['fileid'])) {
//check if fileid exists and is valid
if (!file_exists($config->data_path.$params['fileid'])) {
header("Status: 404 Not Found");
echo json_encode(array("fileid" => $params['fileid'], "exists" => false));
};
} else {
header("Status: 400 Bad Request");
echo json_encode(array("status" => "missing fileid"));
};
switch($url['path']) {
case "/api/exists":
// fileid is valid if we got this far
echo json_encode(array("fileid" => $params['fileid'], "exists" => true));
break;
case "/api/file":
//download cryptofile.dat file
$file = $config->data_path.$params['fileid']."/cryptofile.dat";
header("Content-Length: " . filesize($file));
header("Content-Type: text/plain");
flush();
$fp = fopen($file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // for large downloads
}
fclose($fp);
break;
case "/api/metadata":
//download metadata.dat file
$file = $config->data_path.$params['fileid']."/metadata.dat";
header("Content-Length: " . filesize($file));
header("Content-Type: text/plain");
flush();
readfile($file);
break;
case "/api/delete":
//get deletepassword from serverdata.json
$file = $config->data_path.$params['fileid']."/serverdata.json";
$fh = fopen($file, 'r');
$serverdata = fread($fh, filesize($file));
fclose($fh);
$serverdata = json_decode($serverdata,true);
//check if passwords match
if($params['deletepassword'] == $serverdata['deletepassword']) {
//password valid! delete stuff
unlink($config->data_path.$params['fileid']."/serverdata.json");
unlink($config->data_path.$params['fileid']."/metadata.dat");
unlink($config->data_path.$params['fileid']."/cryptofile.dat");
rmdir($config->data_path.$params['fileid']);
echo json_encode(array("fileid" => $params['fileid'], "deleted" => true));
} else {
//incorrect password
header("Status: 401 Unauthorized");
echo json_encode(array("fileid" => $params['fileid'], "deleted" => false));
};
break;
case "/api/ip":
//return the ip that uploaded this file
$file = $config->data_path.$params['fileid']."/serverdata.json";
$fh = fopen($file, 'r');
$serverdata = fread($fh, filesize($file));
fclose($fh);
$serverdata = json_decode($serverdata,true);
echo json_encode(array("fileid" => $params['fileid'], "uploadip" => $serverdata['clientip']));
break;
default:
// invalid command, show error page
header("Status: 400 Bad Request");
echo json_encode(array("fileid" => $params['fileid'], "status" => "bad request"));
break;
};
};
?>