if (is_string($bookmark)) { $href = $bookmark; } elseif (is_object($bookmark) && property_exists($bookmark, 'type') && in_array('h-cite', $bookmark->type)) { $href = $bookmark->properties->url[0]; if (property_exists($bookmark->properties, 'name')) { $data['title'] = $bookmark->properties->name[0]; } // TODO: Parse the bookmark URL and find the canonical post title } // If this is a submission of a bookmark, set the post author to the bookmark website. // For now, just set it to the domain of the bookmark. Later we could parse the bookmark for an h-card. if ($href != $sourceURL) { $data['post_author'] = parse_url($href, PHP_URL_SCHEME) . '://' . parse_url($href, PHP_URL_HOST); } } $indieNewsPermalink = Config::$baseURL . '/' . $lang . '/' . slugForURL($href); # If there is no existing post for $source, update the properties $post = ORM::for_table('posts')->where('lang', $lang)->where('href', $href)->find_one(); if ($post != FALSE) { if ($data['date']) { $post->post_date = date('Y-m-d H:i:s', $data['date']); } $post->post_author = $data['post_author']; $post->title = $data['title']; if ($inReplyTo) { $post->in_reply_to = $inReplyTo; } if ($data['body']) { $post->body = $data['body']; } $post->save();
<?php use Cake\I18n\I18n; // Redirect old feed URLs $app->get('/:path(.:format)', function ($path, $format = 'html') use($app) { $app->redirect(Config::$baseURL . '/en' . ($format == 'json' ? '.json' : ''), 301); })->conditions(array('format' => 'json', 'path' => '(home|newest)')); // Redirect post IDs to the post URL version $app->get('/post/:id(.:format)', function ($id, $format = 'html') use($app) { $post = ORM::for_table('posts')->where('id', $id)->find_one(); if (!$post) { $app->pass(); // Will trigger a 404 error } $app->redirect(Config::$baseURL . '/post/' . slugForURL($post->href) . ($format == 'html' ? '' : '.' . $format), 302); })->conditions(array('id' => '\\d+', 'format' => 'json')); // Redirect old "/post/" permalinks $app->get('/post/:slug', function ($slug) use($app) { $app->redirect(Config::$baseURL . '/en/' . $slug, 301); })->conditions(array('slug' => '.+\\..+?')); // Language-specific feeds $app->get('/:lang(.:format)', function ($lang = 'en', $format = 'html') use($app) { I18n::locale($lang); $req = $app->request(); $res = $app->response(); $res['X-Pingback'] = 'https://webmention.io/webmention?forward=' . Config::$baseURL . '/' . $lang . '/webmention'; $res['Link'] = '<' . Config::$baseURL . '/' . $lang . '/webmention>; rel="webmention"'; // Get posts ordered by date submitted $posts = ORM::for_table('posts')->where('lang', $lang)->order_by_desc('date_submitted'); if (array_key_exists('before', $req->params())) { $before = date('Y-m-d H:i:s', b60to10($req->params()['before']));
} $post->post_author = $data['post_author']; $post->title = $data['title']; if ($inReplyTo) { $post->in_reply_to = $inReplyTo; } if ($data['body']) { $post->body = $data['body']; } $post->href = $href; $post->source_url = $sourceURL; $post->save(); $update = false; } $res->status(201); $res['Content-Type'] = 'application/json'; $responseData = array('title' => $data['title'], 'body' => $data['body'] ? true : false, 'author' => $data['post_author'], 'date' => $data['date'] ? date('Y-m-d\\TH:i:sP', $data['date']) : false); if ($inReplyTo) { $responseData['in-reply-to'] = $inReplyTo; } $response = array('result' => 'success', 'notices' => $notices, 'data' => $responseData, 'source' => $req->post('source'), 'href' => Config::$baseURL . '/post/' . slugForURL($post->href)); $res['Location'] = Config::$baseURL . '/post/' . slugForURL($post->href); $res->body(json_encode($response)); }); $app->post('/webmention-error', function () use($app) { $req = $app->request(); $res = $app->response(); $res->status(400); $res['Content-Type'] = 'application/json'; $res->body(json_encode(array('error' => 'no_link_found'))); });