-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
47 lines (35 loc) · 1.47 KB
/
update.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
<?php
require_once('config.php');
require_once('utils.php');
function validate($data, $key) {
$value = isset($data[$key]) ? $data[$key] : null;
return is_string($value) && strlen($value) <= 32 && strlen($value) >= 1;
}
function validate_color($data, $key) {
$value = isset($data[$key]) ? $data[$key] : null;
return is_string($value) && (strlen($value) == 3 || strlen($value) == 6) && ctype_xdigit($value);
}
function encode($value) {
return rawurlencode(str_replace("-","--",$value));
}
function create($account, $project, $id, $secret) {
if (! validate_token($secret)) { throwup(401); }
$json = json_decode(file_get_contents("php://input"), true);
if (! is_array($json) || json_last_error() !== JSON_ERROR_NONE) { throwup(400); }
// Value is required, invalid values might be accepted if you pay enough. Alternatively use your own server.
if (! validate($json, 'value')) { throwup(402); }
$value = encode($json['value']);
// Label and color can very well be optional
$label = validate($json, 'label') ? encode($json['label']) : "Coverage";
// No need to encode after ctype_xdigit test passed
$color = validate_color($json, 'color') ? $json['color'] : "D0F055";
$dir = DATADIR."/${account}/${project}";
if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}
$data = "${label}-${value}-${color}";
$datafile = "${dir}/${id}";
if (file_put_contents($datafile, $data) !== strlen($data)) { throwup(409); }
// Seems like it worked, or at least it did not crash
throwup(201);
}