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();
88 $id = $storage->addComment($json, $userId);
91 header('HTTP/1.0 201 Created');
92 header('Location: ' . Urls::full(Urls::comment($id)));
94 } catch (\Exception $e) {
95 if ($e->getCode() == 400) {
97 'HTTP/1.0 400 Bad Request',
104 'HTTP/1.0 500 Internal Server Error',
105 'this_violates_the_spec',
112 function getTokenFromHeader()
114 if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
115 $auth = $_SERVER['HTTP_AUTHORIZATION'];
116 } else if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
117 //php-cgi has it there
118 $auth = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
121 'HTTP/1.0 403 Forbidden', 'forbidden',
122 'Authorization HTTP header missing'
125 list($bearer, $token) = explode(' ', $auth, 2);
126 if ($bearer !== 'Bearer') {
128 'HTTP/1.0 403 Forbidden', 'forbidden',
129 'Authorization header must start with "Bearer"'
136 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
137 if (!isset($_GET['q'])) {
139 'HTTP/1.1 400 Bad Request',
141 'Parameter "q" missing.'
143 } else if ($_GET['q'] === 'config') {
144 header('HTTP/1.0 200 OK');
145 header('Content-Type: application/json');
148 } else if ($_GET['q'] === 'syndicate-to') {
149 header('HTTP/1.0 200 OK');
150 header('Content-Type: application/json');
154 //FIXME: maybe implement $q=source
155 header('HTTP/1.1 501 Not Implemented');
156 header('Content-Type: text/plain');
157 echo 'Unsupported "q" value: ' . $_GET['q'] . "\n";
160 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
161 if (!isset($_SERVER['CONTENT_TYPE'])) {
163 'HTTP/1.1 400 Bad Request',
165 'Content-Type header missing.'
168 list($ctype) = explode(';', $_SERVER['CONTENT_TYPE'], 2);
169 $ctype = trim($ctype);
170 if ($ctype == 'application/x-www-form-urlencoded') {
171 if (!isset($_POST['action'])) {
172 $_POST['action'] = 'create';
174 if ($_POST['action'] != 'create') {
175 header('HTTP/1.1 501 Not Implemented');
176 header('Content-Type: text/plain');
177 echo "Creation of posts supported only\n";
183 'type' => ['h-entry'],
185 if (isset($data['h'])) {
186 $base->type = ['h-' . $data['h']];
189 if (isset($data['access_token'])) {
190 $token = $data['access_token'];
191 unset($data['access_token']);
193 $token = getTokenFromHeader();
195 //reserved properties
196 foreach (['q', 'url', 'action'] as $key) {
197 if (isset($data[$key])) {
198 $base->$key = $data[$key];
202 //"mp-" reserved for future use
203 foreach ($data as $key => $value) {
204 if (substr($key, 0, 3) == 'mp-') {
205 $base->$key = $value;
207 } else if (!is_array($value)) {
209 $data[$key] = [$value];
213 $json->properties = (object) $data;
214 handleCreate($json, $token);
215 } else if ($ctype == 'application/json') {
216 $input = file_get_contents('php://input');
217 $json = json_decode($input);
218 if ($json === null) {
220 'HTTP/1.1 400 Bad Request',
225 $token = getTokenFromHeader();
226 handleCreate($json, $token);
229 'HTTP/1.1 400 Bad Request',
231 'Unsupported POST content type'
236 'HTTP/1.0 400 Bad Request',
238 'Unsupported HTTP request method'