2023-03-08 15:31:49 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
require_once __DIR__ . "/vendor/autoload.php";
|
|
|
|
|
2023-03-10 09:47:55 +00:00
|
|
|
use Lewisdale\Webmentions\Endpoint;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
|
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-09 21:50:59 +00:00
|
|
|
$router = new Router();
|
|
|
|
|
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-16 10:10:40 +00:00
|
|
|
$mentioner->sendForPage($source);
|
2023-03-10 09:47:55 +00:00
|
|
|
});
|
|
|
|
|
2023-03-16 10:10:40 +00:00
|
|
|
$router->post("/endpoint", function (Request $req, Response $response) {
|
|
|
|
$source = $req->post['source'];
|
|
|
|
$target = $req->post['target'];
|
|
|
|
|
|
|
|
$endpoint = new Endpoint(HttpClient::create(), new SqliteGateway("webmentions.db"));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$endpoint->receiveWebmention($source, $target);
|
|
|
|
} catch (InvalidUrlException $e) {
|
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source and target must be valid URLs";
|
|
|
|
} catch (InvalidTargetException $e) {
|
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Target must be on the domain lewisdale.dev";
|
|
|
|
} catch (\Lewisdale\Webmentions\Exceptions\SourceNotFoundException $e) {
|
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source URL was unreachable";
|
|
|
|
} catch (\Lewisdale\Webmentions\Exceptions\TargetNotMentionedException) {
|
|
|
|
$response->status_code = StatusCode::BadRequest;
|
|
|
|
return "Source does not mention the target";
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->status_code = StatusCode::Created;
|
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>");
|
|
|
|
$router->dispatch();
|