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 $storage = new Storage();
87 $id = $storage->addComment($json, $userId);
89 header('HTTP/1.0 201 Created');
90 header('Location: ' . Urls::full(Urls::comment($id)));
92 } catch (\Exception $e) {
93 if ($e->getCode() == 400) {
95 'HTTP/1.0 400 Bad Request',
102 'HTTP/1.0 500 Internal Server Error',
103 'this_violates_the_spec',
110 function getTokenFromHeader()
112 if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
113 $auth = $_SERVER['HTTP_AUTHORIZATION'];
114 } else if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
115 //php-cgi has it there
116 $auth = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
119 'HTTP/1.0 403 Forbidden', 'forbidden',
120 'Authorization HTTP header missing'
123 list($bearer, $token) = explode(' ', $auth, 2);
124 if ($bearer !== 'Bearer') {
126 'HTTP/1.0 403 Forbidden', 'forbidden',
127 'Authorization header must start with "Bearer"'
134 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
135 if (!isset($_GET['q'])) {
137 'HTTP/1.1 400 Bad Request',
139 'Parameter "q" missing.'
141 } else if ($_GET['q'] === 'config') {
142 header('HTTP/1.0 200 OK');
143 header('Content-Type: application/json');
146 } else if ($_GET['q'] === 'syndicate-to') {
147 header('HTTP/1.0 200 OK');
148 header('Content-Type: application/json');
152 //FIXME: maybe implement $q=source
153 header('HTTP/1.1 501 Not Implemented');
154 header('Content-Type: text/plain');
155 echo 'Unsupported "q" value: ' . $_GET['q'] . "\n";
158 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
159 if (!isset($_SERVER['CONTENT_TYPE'])) {
161 'HTTP/1.1 400 Bad Request',
163 'Content-Type header missing.'
166 $ctype = $_SERVER['CONTENT_TYPE'];
167 if ($ctype == 'application/x-www-form-urlencoded') {
168 if (!isset($_POST['action'])) {
169 $_POST['action'] = 'create';
171 if ($_POST['action'] != 'create') {
172 header('HTTP/1.1 501 Not Implemented');
173 header('Content-Type: text/plain');
174 echo "Creation of posts supported only\n";
180 'type' => ['h-entry'],
182 if (isset($data['h'])) {
183 $base->type = ['h-' . $data['h']];
186 if (isset($data['access_token'])) {
187 $token = $data['access_token'];
188 unset($data['access_token']);
190 $token = getTokenFromHeader();
192 //reserved properties
193 foreach (['q', 'url', 'action'] as $key) {
194 if (isset($data[$key])) {
195 $base->$key = $data[$key];
199 //"mp-" reserved for future use
200 foreach ($data as $key => $value) {
201 if (substr($key, 0, 3) == 'mp-') {
202 $base->$key = $value;
204 } else if (!is_array($value)) {
206 $data[$key] = [$value];
210 $json->properties = (object) $data;
211 handleCreate($json, $token);
212 } else if ($ctype == 'application/javascript') {
213 $input = file_get_contents('php://stdin');
214 $json = json_decode($input);
215 if ($json === null) {
217 'HTTP/1.1 400 Bad Request',
222 $token = getTokenFromHeader();
223 handleCreate($json, $token);
226 'HTTP/1.1 400 Bad Request',
228 'Unsupported POST content type'
233 'HTTP/1.0 400 Bad Request',
235 'Unsupported HTTP request method'