-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathreceiving_webhook.php
61 lines (50 loc) · 1.81 KB
/
receiving_webhook.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
<?php
define("STORAGE", '/tmp/terrariumpi/');
function save_file($fullPath, $contents, $flags = 0 ) {
$parts = explode( '/', $fullPath );
array_pop( $parts );
$dir = implode( '/', $parts );
if( !is_dir( $dir ) ) {
mkdir( $dir, 0777, true );
}
file_put_contents( $fullPath, $contents, $flags );
}
// Used for testing/debug. Comment out in production
ob_start();
// Read the data from TerrariumPI
$raw_json_data = file_get_contents('php://input');
// Try to load it to a PHP object from expected JSON data
$php_object = null;
try {
$php_object = json_decode($raw_json_data,true);
} catch (Exception $e) {
echo 'Error parsing post data', $e->getMessage(), "\n";
return false;
}
// No data received, so stop here.
if (null == $php_object || count($php_object) == 0) {
return false;
}
// Loop over all the available fields with their data
// Use a nice padding...
$padding = max(array_map('strlen',array_keys($php_object)));
foreach ($php_object as $key => $value) {
// For now, skip the files, will handle them later on.
if ('files' != $key) {
echo "Key: " . str_pad($key, $padding) . " = " . (is_bool($value) ? ($value ? "yes" : "no" ) : "$value") . "\n";
}
}
// Proces the files
if (key_exists('files',$php_object)) {
echo "\nSaving " . count($php_object['files']) ." file(s) to disk at location: " . STORAGE . "\n";
foreach ($php_object['files'] as $file) {
save_file(STORAGE . $file['name'],base64_decode($file['data']));
echo "Saved file '" . $file['name'] . "' to disk on location: " . STORAGE . $file['name'] . " with filesize: " . filesize(STORAGE . $file['name']). " bytes\n";
}
}
// Get debug data. Comment out in production (2 lines)
$data = ob_get_contents();
ob_end_clean();
// Mail debug output Comment out in production
// mail('[email protected]','TerrariumPI WebHook Test',$data);
?>