4 * Micropub endpoint that stores comments in the database
6 * @author Christian Weiske <cweiske@cweiske.de>
8 header('HTTP/1.0 500 Internal Server Error');
9 require 'www-header.php';
14 * @param string $status HTTP status code line
15 * @param string $code One of the allowed status types:
17 * - insufficient_scope
20 * @param string $description
22 function error($status, $code, $description)
25 header('Content-Type: application/json');
27 ['error' => $code, 'error_description' => $description]
32 function handleCreate($json)
34 if (!isset($json->properties->{'in-reply-to'})) {
36 'HTTP/1.0 400 Bad Request',
38 'Only replies accepted'
41 //FIXME: read bearer token
43 $storage = new Storage();
45 $id = $storage->addComment($json, 0);
47 header('HTTP/1.0 201 Created');
48 header('Location: ' . Urls::full(Urls::comment($id)));
50 } catch (\Exception $e) {
51 //FIXME: return correct status code
52 header('HTTP/1.0 500 Internal Server Error');
57 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
58 if (!isset($_GET['q'])) {
60 'HTTP/1.1 400 Bad Request',
62 'Parameter "q" missing.'
64 } else if ($_GET['q'] === 'config') {
65 header('HTTP/1.0 200 OK');
66 header('Content-Type: application/json');
69 } else if ($_GET['q'] === 'syndicate-to') {
70 header('HTTP/1.0 200 OK');
71 header('Content-Type: application/json');
75 //FIXME: maybe implement $q=source
76 header('HTTP/1.1 501 Not Implemented');
77 header('Content-Type: text/plain');
78 echo 'Unsupported "q" value: ' . $_GET['q'] . "\n";
81 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
82 if (!isset($_SERVER['CONTENT_TYPE'])) {
84 'HTTP/1.1 400 Bad Request',
86 'Content-Type header missing.'
89 $ctype = $_SERVER['CONTENT_TYPE'];
90 if ($ctype == 'application/x-www-form-urlencoded') {
91 if (!isset($_POST['action'])) {
92 $_POST['action'] = 'create';
94 if ($_POST['action'] != 'create') {
95 header('HTTP/1.1 501 Not Implemented');
96 header('Content-Type: text/plain');
97 echo "Creation of posts supported only\n";
103 'type' => ['h-entry'],
105 if (isset($data['h'])) {
106 $base->type = ['h-' . $data['h']];
109 //reserved properties
110 foreach (['access_token', 'q', 'url', 'action'] as $key) {
111 if (isset($data[$key])) {
112 $base->$key = $data[$key];
116 //"mp-" reserved for future use
117 foreach ($data as $key => $value) {
118 if (substr($key, 0, 3) == 'mp-') {
119 $base->$key = $value;
121 } else if (!is_array($value)) {
123 $data[$key] = [$value];
127 $json->properties = (object) $data;
129 } else if ($ctype == 'application/javascript') {
130 $input = file_get_contents('php://stdin');
131 $json = json_decode($input);
132 if ($json === null) {
134 'HTTP/1.1 400 Bad Request',
142 'HTTP/1.1 400 Bad Request',
144 'Unsupported POST content type'
149 'HTTP/1.0 400 Bad Request',
151 'Unsupported HTTP request method'