-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsaveErrorDump.php
71 lines (57 loc) · 1.75 KB
/
saveErrorDump.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
<?php
/**
* Created by PhpStorm.
* User: Matt Jaquiery
* Date: 19/03/2019
* Time: 10:28
*
*/
/*
The $_POST[] array will contain a JSON string which decomposes into:
{
metadata:
studyId: study identifier (becomes eid below)
studyVersion: study version
idCode: participant id
error: description of the error
data:
JSON string to write to file
}
Data are saved in the ./data/private/error directory
*/
//phpinfo();
error_reporting(0);
//ini_set("display_errors", true);
ini_set("auto_detect_line_endings", true);
$log = "";
// Unpack POST data
$json = json_decode(file_get_contents("php://input"), true);
$meta = $json["metadata"];
$data = $json["data"];
$error = (string) $meta["error"];
$eid = (string) $meta["studyId"];
$version = (string) $meta["studyVersion"];
$version = str_replace(".", "-", $version);
$pid = $meta["idCode"];
$userIssue = (boolean) $meta["userIssue"];
// Check input is valid
function is_alphanumeric($str, $allowHyphen = false) {
if($allowHyphen)
return (bool) preg_match('/^[0-9a-z\-]+$/i', $str);
return (bool) preg_match('/^[0-9a-z]+$/i', $str);
}
if(!is_alphanumeric($version, true) ||
!is_alphanumeric($pid) ||
!is_alphanumeric($eid, true))
die();
const PATH = "./data/private/error/";
$prefix = $eid . "_v" . $version;
$body = date('Y-m-d_H-i-s') . "_" . $prefix . "_" . $pid;
$suffix = $userIssue? "user-issue" : "error-dump";
$logPath = $userIssue? PATH . "issues.log" : PATH . "error.log";
$filename = PATH . $body . "_" . $suffix . ".json";
if(($handle = fopen($filename, "wb")) !== false)
fwrite($handle, $data);
// Create/update error log
if(($handle = fopen(PATH . $logPath, "ab+")) !== false)
fwrite($handle, "File: $filename; Error: $error" . PHP_EOL);