2024-05-03 18:18:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2024 Jonathan Treffler <mail@jonathan-treffler.de>
|
|
|
|
*
|
|
|
|
* @author Jonathan Treffler <mail@jonathan-treffler.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DavPush\Dav;
|
|
|
|
|
2024-06-12 01:58:00 +02:00
|
|
|
use OCA\DavPush\Transport\TransportManager;
|
|
|
|
use OCA\DavPush\Db\Subscription;
|
2024-06-12 14:19:52 +02:00
|
|
|
use OCA\DavPush\Service\SubscriptionService;
|
2024-05-03 18:18:33 +02:00
|
|
|
|
2024-06-12 01:58:00 +02:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\IURLGenerator;
|
2024-05-03 18:18:33 +02:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
|
|
|
|
class SubscriptionManagementPlugin extends ServerPlugin {
|
|
|
|
|
|
|
|
public const PUSH_PREFIX = '{DAV:Push}';
|
|
|
|
public const PUSH_REGISTER = self::PUSH_PREFIX . "push-register";
|
|
|
|
public const PUSH_SUBSCRIPTION = self::PUSH_PREFIX . "subscription";
|
|
|
|
public const PUSH_EXPIRES = self::PUSH_PREFIX . "expires";
|
|
|
|
|
2024-06-12 01:58:00 +02:00
|
|
|
public const IMF_FIXDATE_FORMAT = "D, d M Y H:i:s O+";
|
|
|
|
|
2024-05-03 18:18:33 +02:00
|
|
|
/**
|
|
|
|
* Reference to SabreDAV server object.
|
|
|
|
*
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
private IUserSession $userSession,
|
2024-06-12 01:58:00 +02:00
|
|
|
private TransportManager $transportManager,
|
|
|
|
private IURLGenerator $URLGenerator,
|
2024-07-01 22:22:41 +02:00
|
|
|
private SubscriptionService $subscriptionService
|
2024-05-03 18:18:33 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function initialize(Server $server): void {
|
|
|
|
$this->server = $server;
|
|
|
|
|
|
|
|
$this->server->on('method:POST', [$this, 'httpPost']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function httpPost(RequestInterface $request, ResponseInterface $response) {
|
|
|
|
// Only handle xml
|
|
|
|
$contentType = (string) $request->getHeader('Content-Type');
|
|
|
|
if (!(str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-12 01:58:00 +02:00
|
|
|
$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
|
|
|
|
|
2024-05-03 18:18:33 +02:00
|
|
|
$requestBody = $request->getBodyAsString();
|
|
|
|
|
|
|
|
// If this request handler could not deal with this POST request, it
|
|
|
|
// will return 'null' and other plugins get a chance to handle the
|
|
|
|
// request.
|
|
|
|
//
|
|
|
|
// However, we already requested the full body. This is a problem,
|
|
|
|
// because a body can only be read once. This is why we preemptively
|
|
|
|
// re-populated the request body with the existing data.
|
|
|
|
$request->setBody($requestBody);
|
|
|
|
|
|
|
|
$parameters = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
|
|
|
|
|
|
|
|
if($documentType == self::PUSH_REGISTER) {
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
$subscriptionParameterIncluded = False;
|
|
|
|
|
|
|
|
$subscriptionType = "";
|
|
|
|
$subscriptionOptions = [];
|
2024-06-12 14:19:52 +02:00
|
|
|
$subscriptionExpires = 0;
|
2024-05-03 18:18:33 +02:00
|
|
|
|
|
|
|
foreach($parameters as $parameter) {
|
|
|
|
if($parameter["name"] == self::PUSH_SUBSCRIPTION && !$subscriptionParameterIncluded) {
|
|
|
|
$subscriptionParameterIncluded = True;
|
|
|
|
|
|
|
|
if(sizeof($parameter["value"]) == 1) {
|
2024-06-12 21:50:28 +02:00
|
|
|
$subscriptionType = $parameter["value"][0]["name"];
|
|
|
|
$subscriptionType = preg_replace('/^\{DAV:Push\}/', '', $subscriptionType);
|
|
|
|
$subscriptionType = preg_replace('/-subscription$/', '', $subscriptionType);
|
|
|
|
|
2024-05-03 18:18:33 +02:00
|
|
|
$subscriptionOptions = $parameter["value"][0]["value"];
|
|
|
|
} else {
|
|
|
|
$errors[] = "only one subscription allowed";
|
|
|
|
}
|
2024-06-12 14:19:52 +02:00
|
|
|
} elseif($parameter["name"] == self::PUSH_EXPIRES && $subscriptionExpires === 0) {
|
|
|
|
$subscriptionExpires = \DateTime::createFromFormat(self::IMF_FIXDATE_FORMAT, $parameter["value"])->getTimestamp();
|
2024-05-03 18:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$subscriptionParameterIncluded) {
|
|
|
|
$errors[] = "no subscription included";
|
|
|
|
}
|
|
|
|
|
2024-06-12 03:03:35 +02:00
|
|
|
$transport = $this->transportManager->getTransport($subscriptionType);
|
2024-06-12 01:58:00 +02:00
|
|
|
|
2024-06-12 21:32:52 +02:00
|
|
|
if(!is_null($transport)) {
|
|
|
|
[
|
2024-07-24 13:52:09 +02:00
|
|
|
'success' => $validateSuccess,
|
|
|
|
'errors' => $validateErrors,
|
|
|
|
] = $transport->validateOptions($subscriptionOptions);
|
|
|
|
|
|
|
|
if(!$validateSuccess) {
|
|
|
|
if(isset($validateErrors) && !empty($validateErrors)) {
|
|
|
|
$errors = array_merge($errors, $validateErrors);
|
|
|
|
} else {
|
|
|
|
$errors[] = "options validation error";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$existingSubscriptionId = $transport->getSubscriptionIdFromOptions($subscriptionOptions);
|
|
|
|
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
|
|
|
|
if(!is_int($existingSubscriptionId)) {
|
|
|
|
// create new subscription entry in db
|
|
|
|
$subscription = $this->subscriptionService->create($user->getUID(), $node->getName(), $subscriptionType, $subscriptionExpires);
|
|
|
|
|
|
|
|
[
|
|
|
|
'success' => $registerSuccess,
|
|
|
|
'errors' => $registerErrors,
|
|
|
|
'responseStatus' => $responseStatus,
|
|
|
|
'response' => $responseContent,
|
|
|
|
'unsubscribeLink' => $unsubscribeLink,
|
|
|
|
] = $transport->registerSubscription($subscription->getId(), $subscriptionOptions);
|
|
|
|
|
|
|
|
$responseStatus = $responseStatus ?? Http::STATUS_CREATED;
|
|
|
|
|
|
|
|
if(!$registerSuccess) {
|
|
|
|
if(isset($registerErrors) && !empty($registerErrors)) {
|
|
|
|
$errors = array_merge($errors, $registerErrors);
|
|
|
|
} else {
|
|
|
|
$errors[] = "registration error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$subscription = $this->subscriptionService->find($user->getUID(), $existingSubscriptionId);
|
|
|
|
|
|
|
|
[
|
|
|
|
'success' => $updateSuccess,
|
|
|
|
'errors' => $updateErrors,
|
2024-07-24 14:23:16 +02:00
|
|
|
'response' => $responseContent,
|
2024-07-24 13:52:09 +02:00
|
|
|
] = $transport->updateSubscription($subscription->getId(), $subscriptionOptions);
|
|
|
|
|
|
|
|
if(!$updateSuccess) {
|
|
|
|
if(isset($updateErrors) && !empty($updateErrors)) {
|
|
|
|
$errors = array_merge($errors, $updateErrors);
|
|
|
|
} else {
|
|
|
|
$errors[] = "subscription update error";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$subscription = $this->subscriptionService->update($user->getUID(), $subscription->getId(), $subscriptionExpires);
|
|
|
|
|
|
|
|
$responseStatus = Http::STATUS_CREATED;
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 21:32:52 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-12 01:58:00 +02:00
|
|
|
$errors[] = $subscriptionType . " transport does not exist";
|
|
|
|
}
|
2024-05-03 18:18:33 +02:00
|
|
|
|
2024-06-12 01:58:00 +02:00
|
|
|
if(sizeof($errors) == 0) {
|
|
|
|
$response->setStatus($responseStatus);
|
|
|
|
|
|
|
|
// generate default unsubscribe link, unless transport requested a custom url
|
|
|
|
$unsubscribeLink = $unsubscribeLink ?? $this->URLGenerator->getAbsoluteURL("/apps/dav_push/subscriptions/" . $subscription->getId());
|
|
|
|
$response->setHeader("Location", $unsubscribeLink);
|
|
|
|
|
|
|
|
$xml = $this->server->xml->write(self::PUSH_REGISTER, $responseContent);
|
|
|
|
$response->setBody($xml);
|
2024-05-03 18:18:33 +02:00
|
|
|
} else {
|
|
|
|
$response->setStatus(Http::STATUS_BAD_REQUEST);
|
|
|
|
|
|
|
|
$errorsXML = [];
|
|
|
|
|
|
|
|
foreach($errors as $error) {
|
|
|
|
$errorsXML[] = [
|
|
|
|
"name" => "error",
|
|
|
|
"value" => $error
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml = $this->server->xml->write(
|
|
|
|
'{DAV:}error',
|
|
|
|
$errorsXML
|
|
|
|
);
|
|
|
|
|
|
|
|
$response->setBody($xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 21:32:52 +02:00
|
|
|
}
|