-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
63 lines (58 loc) · 1.72 KB
/
Copy pathindex.php
File metadata and controls
63 lines (58 loc) · 1.72 KB
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
<?php
#define("TOKEN", "put token here or take from \$_ENV");
define("TOKEN", "put toekn here or take from \$_ENV");
function response(){
return 'BAD REQUEST';
}
function isValidJSON($str){
json_decode($str);
return json_last_error() == JSON_ERROR_NONE;
}
function read_hash($base){
$file_path = "$base/api/v1/state.json";
if(is_file($file_path)){
$file = fopen($file_path,'r');
$data = fread($file,filesize($file_path));
fclose($file);
if(isValidJSON($data)){
$data_json = json_decode($data,true);
return $data_json['data'];
}
return $data;
}
return '';
}
$base = "$_SERVER[DOCUMENT_ROOT]/covid";
if($_SERVER['REQUEST_METHOD']==='POST'){
if(isset($_SERVER['HTTP_TOKEN'])){
if(isset($_SERVER['HTTP_JSON_NAME']) && isset($_SERVER['HTTP_JSON_PATH'])){
if($_SERVER['HTTP_TOKEN']===TOKEN){
$data = file_get_contents('php://input');
$path = "$base$_SERVER[HTTP_JSON_PATH]";
if(is_dir($path)===false){
mkdir($path,0,true);
}
$file = fopen("$path/$_SERVER[HTTP_JSON_NAME]",'w');
fwrite($file, $data);
fclose($file);
}else{
#echo response();
echo "BAD token";
}
}elseif(isset($_SERVER['HTTP_DATA_HASH'])){
$data_hash = read_hash($base);
if($data_hash===$_SERVER['HTTP_DATA_HASH']){
echo '{"update":false}';
}
else{
echo '{"update":true}';
}
}
}else{
#echo response();
echo "BAD headers";
}
}else{
echo response();
}
?>