2023-03-08 15:31:49 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
require_once __DIR__ . "/vendor/autoload.php";
|
|
|
|
|
2023-03-21 09:09:24 +00:00
|
|
|
use Lewisdale\Webmentions\Api\WebmentionApi;
|
2023-03-10 09:47:55 +00:00
|
|
|
use Lewisdale\Webmentions\Endpoint;
|
2023-03-17 10:19:50 +00:00
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidSourceException;
|
2023-03-10 09:47:55 +00:00
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
|
2023-03-17 10:19:50 +00:00
|
|
|
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
|
2023-03-16 10:10:40 +00:00
|
|
|
use Lewisdale\Webmentions\Gateways\SqliteGateway;
|
2023-03-10 09:47:55 +00:00
|
|
|
use Lewisdale\Webmentions\Router\Request;
|
2023-03-09 21:50:59 +00:00
|
|
|
use Lewisdale\Webmentions\Router\Response;
|
2023-03-16 10:10:40 +00:00
|
|
|
use Lewisdale\Webmentions\Router\Router;
|
2023-03-10 09:47:55 +00:00
|
|
|
use Lewisdale\Webmentions\Router\StatusCode;
|
2023-03-08 15:31:49 +00:00
|
|
|
use Lewisdale\Webmentions\Webmention;
|
2023-03-16 10:10:40 +00:00
|
|
|
use Symfony\Component\HttpClient\HttpClient;
|
2023-03-08 15:31:49 +00:00
|
|
|
|
2023-03-21 09:09:24 +00:00
|
|
|
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
|
|
|
$dotenv->load();
|
|
|
|
|
2023-03-09 21:50:59 +00:00
|
|
|
$router = new Router();
|
2023-03-21 09:09:24 +00:00
|
|
|
$gateway = new SqliteGateway($_ENV["DB"]);
|
2023-03-21 08:42:01 +00:00
|
|
|
|
2023-03-21 09:09:24 +00:00
|
|
|
$apiController = new WebmentionApi($gateway);
|
2023-03-09 21:50:59 +00:00
|
|
|
|
2023-03-16 10:10:40 +00:00
|
|
|
$router->post("/send", function (Request $req, Response $response) {
|
2023-03-14 21:08:33 +00:00
|
|
|
$mentioner = new Webmention();
|
|
|
|
$source = $req->query["source"];
|
2023-03-17 10:19:50 +00:00
|
|
|
try {
|
|
|
|
$mentioner->sendForPage($source);
|
|
|
|
return;
|
|
|
|
} catch (InvalidSourceException $e) {
|
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
2023-03-10 09:47:55 +00:00
|
|
|
});
|
|
|
|
|
2023-03-21 08:42:01 +00:00
|
|
|
$router->post("/endpoint", function (Request $req, Response $response) use ($gateway) {
|
2023-03-16 10:10:40 +00:00
|
|
|
$source = $req->post['source'];
|
|
|
|
$target = $req->post['target'];
|
|
|
|
|
2023-03-21 08:42:01 +00:00
|
|
|
$endpoint = new Endpoint(HttpClient::create(), $gateway);
|
2023-03-16 10:10:40 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$endpoint->receiveWebmention($source, $target);
|
2023-03-17 10:19:50 +00:00
|
|
|
} catch (InvalidUrlException) {
|
2023-03-16 10:10:40 +00:00
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source and target must be valid URLs";
|
2023-03-17 10:19:50 +00:00
|
|
|
} catch (InvalidTargetException) {
|
2023-03-16 10:10:40 +00:00
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Target must be on the domain lewisdale.dev";
|
2023-03-17 10:19:50 +00:00
|
|
|
} catch (SourceNotFoundException $e) {
|
2023-03-16 10:10:40 +00:00
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source URL was unreachable";
|
2023-03-17 10:19:50 +00:00
|
|
|
} catch (TargetNotMentionedException) {
|
2023-03-16 10:10:40 +00:00
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source does not mention the target";
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->status_code = StatusCode::Created;
|
2023-03-17 10:19:50 +00:00
|
|
|
return;
|
2023-03-09 21:50:59 +00:00
|
|
|
});
|
|
|
|
|
2023-03-14 21:08:33 +00:00
|
|
|
$router->get('/', fn($req, $res) => "<h1>Webmention server</h1>");
|
2023-03-21 08:42:01 +00:00
|
|
|
|
|
|
|
$router->get('/api/webmentions', [$apiController, "list"]);
|
|
|
|
$router->get('/api/webmentions/(\d+)', [$apiController, "get"]);
|
|
|
|
|
2023-03-14 21:08:33 +00:00
|
|
|
$router->dispatch();
|