-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwrite_data.php
49 lines (39 loc) · 1.31 KB
/
write_data.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
<?php
function sanitize($content){
$no_newlines = str_replace(array("\n", "\r"), '', $content);
return preg_replace('/[^\dA-Za-z_]/i', '', $no_newlines);
}
$rootpath = '/var/www/data/';
$post_data = json_decode(file_get_contents('php://input'), true);
$sanitized_filename = sanitize($post_data['filename']);
$user_dir = $rootpath;
if ($post_data["username"] !== ""){
$sanitized_username = sanitize($post_data["username"]);
$user_dir = $rootpath . "encrypted/" . $sanitized_username . "/";
if(!is_dir($user_dir)){
if(!mkdir($user_dir, 0755, true)){
http_response_code(500);
die();
};
}
}
$filetype = $post_data['filetype'];
$sanitized_filetype = '';
if ($filetype === 'json' || $filetype === 'json.enc' || $filetype === 'csv' || $filetype === 'csv.enc' || $filetype === 'webm.enc') {
$data = $post_data['filedata'];
$sanitized_filetype = $filetype;
} else if ($filetype === 'webm') {
$data = base64_decode(explode(',', $post_data['filedata'])[1], true);
$sanitized_filetype = 'webm';
} else {
http_response_code(400);
die();
}
$name = $user_dir . $sanitized_filename . '.' . $sanitized_filetype;
while (file_exists($name)) {
$name = $name . '-' . time() . '-' . uniqid();
}
if (!file_put_contents($name, $data)) {
http_response_code(500);
}
?>