2017-03-14 03:22:20 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-14 03:22:20 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse;
|
2017-03-14 03:22:20 +00:00
|
|
|
|
|
|
|
class REST {
|
2017-03-19 02:30:36 +00:00
|
|
|
protected $apis = [
|
|
|
|
// NextCloud News version enumerator
|
|
|
|
'ncn' => [
|
|
|
|
'match' => '/index.php/apps/news/api',
|
|
|
|
'strip' => '/index.php/apps/news/api',
|
|
|
|
'class' => REST\NextCloudNews\Versions::class,
|
|
|
|
],
|
|
|
|
// NextCloud News v1-2 https://github.com/nextcloud/news/blob/master/docs/externalapi/Legacy.md
|
|
|
|
'ncn_v1-2' => [
|
|
|
|
'match' => '/index.php/apps/news/api/v1-2/',
|
|
|
|
'strip' => '/index.php/apps/news/api/v1-2',
|
|
|
|
'class' => REST\NextCloudNews\V1_2::class,
|
|
|
|
],
|
|
|
|
// Other candidates:
|
|
|
|
// NextCloud News v2 https://github.com/nextcloud/news/blob/master/docs/externalapi/External-Api.md
|
|
|
|
// Feedbin v1 https://github.com/feedbin/feedbin-api/commit/86da10aac5f1a57531a6e17b08744e5f9e7db8a9
|
|
|
|
// Feedbin v2 https://github.com/feedbin/feedbin-api
|
|
|
|
// Tiny Tiny RSS https://tt-rss.org/gitlab/fox/tt-rss/wikis/ApiReference
|
|
|
|
// Fever https://feedafever.com/api
|
|
|
|
// NewsBlur http://www.newsblur.com/api
|
2017-07-15 20:44:06 +00:00
|
|
|
// Miniflux https://github.com/miniflux/miniflux/blob/master/docs/json-rpc-api.markdown
|
|
|
|
// CommaFeed https://www.commafeed.com/api/
|
2017-03-19 02:30:36 +00:00
|
|
|
];
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __construct() {
|
2017-03-19 02:30:36 +00:00
|
|
|
}
|
2017-03-14 03:22:20 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function dispatch(REST\Request $req = null): REST\Response {
|
|
|
|
if ($req===null) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$req = new REST\Request();
|
|
|
|
}
|
2017-04-02 03:06:52 +00:00
|
|
|
$api = $this->apiMatch($req->url, $this->apis);
|
2017-08-29 14:50:31 +00:00
|
|
|
$req->url = substr($req->url, strlen($this->apis[$api]['strip']));
|
2017-04-02 03:06:52 +00:00
|
|
|
$req->refreshURL();
|
2017-03-19 02:30:36 +00:00
|
|
|
$class = $this->apis[$api]['class'];
|
2017-03-28 22:50:00 +00:00
|
|
|
$drv = new $class();
|
2017-09-29 21:40:07 +00:00
|
|
|
if ($req->head) {
|
2017-09-30 16:05:57 +00:00
|
|
|
$res = $drv->dispatch($req);
|
|
|
|
$res->head = true;
|
|
|
|
return $res;
|
2017-09-29 21:40:07 +00:00
|
|
|
} else {
|
|
|
|
return $drv->dispatch($req);
|
|
|
|
}
|
2017-03-19 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function apiMatch(string $url, array $map): string {
|
2017-03-19 02:30:36 +00:00
|
|
|
// sort the API list so the longest URL prefixes come first
|
2017-08-29 14:50:31 +00:00
|
|
|
uasort($map, function ($a, $b) {
|
|
|
|
return (strlen($a['match']) <=> strlen($b['match'])) * -1;
|
|
|
|
});
|
2017-03-19 02:30:36 +00:00
|
|
|
// find a match
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($map as $id => $api) {
|
|
|
|
if (strpos($url, $api['match'])===0) {
|
2017-07-21 02:40:09 +00:00
|
|
|
return $id;
|
|
|
|
}
|
2017-03-19 02:30:36 +00:00
|
|
|
}
|
|
|
|
// or throw an exception otherwise
|
2017-07-15 20:44:06 +00:00
|
|
|
throw new REST\Exception501();
|
2017-03-19 02:30:36 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|