-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsaveData.php
76 lines (67 loc) · 2.02 KB
/
saveData.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
<?php
/**
* Created by PhpStorm.
* User: Matt Jaquiery
* Date: 06/03/2018
* Time: 10:28
*
*/
/** https://stackoverflow.com/questions/2982059/testing-if-string-is-sha1-in-php
* @param {string} $str - string to test
* @return bool - true if $str is a viable sha1 string
*/
function is_sha1($str) {
return (bool) preg_match('/^[0-9a-f]{40}$/i', $str);
}
$json = $_POST["data"];
$data = json_decode($json);
$raw = json_decode($data->rawData);
$processed = json_decode($data->processedData);
$id = $raw->participantId;
$prefix = $raw->experimentCode;
// Experiment name is the last directory
$experimentName = basename(pathinfo($_SERVER['HTTP_REFERER'], PATHINFO_DIRNAME));
// Whitelist for experiment names:
$experimentNames = array("AdvisorChoice", "DotTask");
$out = array(
"error" => "",
"code" => 200,
"content" => $id
);
//$out['debug'] = basename($_SERVER['HTTP_REFERER']);
if (!is_sha1($id)
|| !in_array($experimentName, $experimentNames, true)
|| !preg_match("/^[a-z0-9\-]+$/i", $prefix)) {
$out['error'] = 'Refused';
$out['code'] = 403;
$out['content'] = "Invalid ID specified '$id'";
die(json_encode($out));
}
$fname = "$experimentName/data/raw/".$prefix."_$id.JSON";
if (file_exists($fname)) {
$out['error'] = 'File exists';
$out['code'] = 500;
$out['content'] = '';
die(json_encode($out));
}
$file = fopen($fname, 'w');
if (gettype($file) !== 'resource') {
$out['error'] = 'Unable to create file';
$out['code'] = 500;
$out['content'] = '';
die(json_encode($out));
}
fwrite($file, json_encode($raw));
fclose($file);
$fname = "$experimentName/data/processed/".$prefix."_$id.JSON";
// A file exists check is unlikely to trigger given the cleanup script which tarballs and compresses everything daily
$file = fopen($fname, 'w');
if (gettype($file) !== 'resource') {
$out['error'] = 'Unable to store processed data';
$out['code'] = 500;
$out['content'] = '';
die(json_encode($out));
}
fwrite($file, json_encode($processed));
fclose($file);
echo json_encode($out);