ref: 845ac4011bcd28ce579b0159dd20fb7ca3b4493a
converter/server/upload.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<?php set_time_limit(0); require_once 'vendor/paragonie/sodium_compat/autoload.php'; require_once 'vendor/mustangostang/spyc/Spyc.php'; require_once 'vendor/autoload.php'; use MessagePack\BufferUnpacker; use MessagePack\Exception\UnpackingFailedException; $publicKey = sodium_hex2bin('8593a07f70809c0adc0c72e16c2a958997419bdc428fe1eb46f58e59ac2e53d0'); if ($_SERVER['REQUEST_METHOD'] === 'PUT') { ob_start(); $handle = fopen('php://input', 'rb'); $length = trim(fgets($handle)); $data = fread($handle, $length); $unpacker = new BufferUnpacker(); $unpacker->reset($data); $post = []; try { $post = $unpacker->unpack(); } catch (UnpackingFailedException $e) { http_response_code(400); die; } if (!file_exists('metadata.yml') ) $metadata = []; else $metadata = Spyc::YAMLLoad('metadata.yml'); $signature = $post['signature']; $meta = $post['meta']; $id = $meta['id']; $output = fopen("$id.db.gz", 'wb'); stream_copy_to_stream($handle, $output); fclose($output); fclose($handle); $sha = hash_file('sha256', "$id.db.gz"); $verified = sodium_crypto_sign_verify_detached($signature, $sha, $publicKey); if (!$verified) { http_response_code(403); unlink("$id.db.gz"); die; } $metadata[] = $meta; file_put_contents('metadata.yml', Spyc::YAMLDump($metadata, false, 0, true)); ob_end_flush(); ob_flush(); flush(); } elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') { ob_start(); $req = explode(':', substr(@$_SERVER['PATH_INFO'], 1), 2); $id = $req[0]; $sig = base64_decode($req[1]); if ($id == '') { http_response_code(400); die('no id in DELETE'); } if (preg_match('/[0-9a-f]{64}/', $id, $matches) === 0) { http_response_code(400); die('wrong id in DELETE'); } if ($matches[0] != $id) { http_response_code(400); die('wrong id in DELETE'); } $verified = sodium_crypto_sign_verify_detached($sig, $id, $publicKey); if (!$verified) { http_response_code(403); die('unverified DELETE'); } if (!file_exists('metadata.yml') ) $metadata = []; else $metadata = Spyc::YAMLLoad('metadata.yml'); $newMetadata = []; foreach ($metadata as $it) { if ($it['id'] != $id) $newMetadata[] = $it; } file_put_contents('metadata.yml', Spyc::YAMLDump($newMetadata, false, 0, true)); unlink("$id.db.gz"); ob_end_flush(); ob_flush(); flush(); } ?> |