3 * Minimal micropub media endpoint
\r
5 * @author Christian Weiske <cweiske@cweiske.de>
\r
7 function error($code, $error, $description)
\r
9 header('HTTP/1.0 ' . $code);
\r
10 header('Content-type: application/json');
\r
12 ['error' => $error, 'error_description' => $description]
\r
17 if (!isset($_FILES['file'])) {
\r
18 error(400, 'invalid_request', 'file property missing');
\r
20 $file = $_FILES['file'];
\r
21 if (!is_int($file['error'])) {
\r
22 error(400, 'invalid_request', 'file not uploaded correctly');
\r
24 if ($file['error'] != 0) {
\r
26 400, 'invalid_request',
\r
27 'file upload failed; php upload error' . $file['error']
\r
31 $reldir = '/micropub-media-endpoint/' . microtime(true) . '/';
\r
32 if (!is_dir(__DIR__ . $reldir)) {
\r
33 $ok = mkdir(__DIR__ . $reldir, 0700, true);
\r
35 error(403, 'forbidden', 'Failed to create upload directory');
\r
38 if ($file['name'] == '') {
\r
39 $file['name'] = 'file.dat';
\r
41 $relfile = $reldir . $file['name'];
\r
42 $ok = move_uploaded_file($file['tmp_name'], __DIR__ . $relfile);
\r
44 error(500, 'internal_error', 'Failed to move uploaded file');
\r
47 $dir = dirname($_SERVER['PHP_SELF']);
\r
48 header('HTTP/1.1 201 Created');
\r
49 //RFC 7231 allows relative URIs in location header
\r
50 header('Location: ' . str_replace('//', '/', $dir . $relfile));
\r