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';
12 * Send out a micropub error
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 mpError($status, $code, $description)
25 header('Content-Type: application/json');
27 ['error' => $code, 'error_description' => $description]
32 function validateToken($token)
34 $ctx = stream_context_create(
38 'Authorization: Bearer ' . $token
40 'ignore_errors' => true,
44 //FIXME: make hard-coded token server URL configurable
45 $res = @file_get_contents(Urls::full('/token.php'), false, $ctx);
46 list($dummy, $code, $msg) = explode(' ', $http_response_header[0]);
49 'HTTP/1.0 403 Forbidden',
51 'Error verifying bearer token: ' . trim($res)
55 parse_str($res, $data);
56 //FIXME: they spit out non-micropub json error responess
57 verifyUrlParameter($data, 'me');
58 verifyUrlParameter($data, 'client_id');
59 verifyParameter($data, 'scope');
61 return [$data['me'], $data['client_id'], $data['scope']];
64 function handleCreate($json, $token)
66 list($me, $client_id, $scope) = validateToken($token);
67 $userId = Urls::userId($me);
68 if ($userId === null) {
70 'HTTP/1.0 403 Forbidden',
75 $storage = new Storage();
76 $rowUser = $storage->getUser($userId);
77 if ($rowUser === null) {
79 'HTTP/1.0 403 Forbidden',
81 'User not found: ' . $userId
85 if (!isset($json->properties->{'in-reply-to'})) {
87 'HTTP/1.0 400 Bad Request',
89 'Only replies accepted'
93 $storage = new Storage();
95 $id = $storage->addComment($json, $userId);
97 header('HTTP/1.0 201 Created');
98 header('Location: ' . Urls::full(Urls::comment($id)));
100 } catch (\Exception $e) {
101 //FIXME: return correct status code
102 header('HTTP/1.0 500 Internal Server Error');
107 function getTokenFromHeader()
109 if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
111 'HTTP/1.0 403 Forbidden', 'forbidden',
112 'Authorization HTTP header missing'
115 list($bearer, $token) = explode(' ', $_SERVER['HTTP_AUTHORIZATION'], 2);
116 if ($bearer !== 'Bearer') {
118 'HTTP/1.0 403 Forbidden', 'forbidden',
119 'Authorization header must start with "Bearer"'
126 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
127 if (!isset($_GET['q'])) {
129 'HTTP/1.1 400 Bad Request',
131 'Parameter "q" missing.'
133 } else if ($_GET['q'] === 'config') {
134 header('HTTP/1.0 200 OK');
135 header('Content-Type: application/json');
138 } else if ($_GET['q'] === 'syndicate-to') {
139 header('HTTP/1.0 200 OK');
140 header('Content-Type: application/json');
144 //FIXME: maybe implement $q=source
145 header('HTTP/1.1 501 Not Implemented');
146 header('Content-Type: text/plain');
147 echo 'Unsupported "q" value: ' . $_GET['q'] . "\n";
150 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
151 if (!isset($_SERVER['CONTENT_TYPE'])) {
153 'HTTP/1.1 400 Bad Request',
155 'Content-Type header missing.'
158 $ctype = $_SERVER['CONTENT_TYPE'];
159 if ($ctype == 'application/x-www-form-urlencoded') {
160 if (!isset($_POST['action'])) {
161 $_POST['action'] = 'create';
163 if ($_POST['action'] != 'create') {
164 header('HTTP/1.1 501 Not Implemented');
165 header('Content-Type: text/plain');
166 echo "Creation of posts supported only\n";
172 'type' => ['h-entry'],
174 if (isset($data['h'])) {
175 $base->type = ['h-' . $data['h']];
178 if (isset($data['access_token'])) {
179 $token = $data['access_token'];
180 unset($data['access_token']);
182 $token = getTokenFromHeader();
184 //reserved properties
185 foreach (['q', 'url', 'action'] as $key) {
186 if (isset($data[$key])) {
187 $base->$key = $data[$key];
191 //"mp-" reserved for future use
192 foreach ($data as $key => $value) {
193 if (substr($key, 0, 3) == 'mp-') {
194 $base->$key = $value;
196 } else if (!is_array($value)) {
198 $data[$key] = [$value];
202 $json->properties = (object) $data;
203 handleCreate($json, $token);
204 } else if ($ctype == 'application/javascript') {
205 $input = file_get_contents('php://stdin');
206 $json = json_decode($input);
207 if ($json === null) {
209 'HTTP/1.1 400 Bad Request',
214 $token = getTokenFromHeader();
215 handleCreate($json, $token);
218 'HTTP/1.1 400 Bad Request',
220 'Unsupported POST content type'
225 'HTTP/1.0 400 Bad Request',
227 'Unsupported HTTP request method'