123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- declare(strict_types=1);
- namespace OCA\DAV\CardDAV;
- use OCP\AppFramework\Http;
- use Sabre\DAV;
- use Sabre\DAV\Server;
- use Sabre\HTTP\RequestInterface;
- use Sabre\HTTP\ResponseInterface;
- class MultiGetExportPlugin extends DAV\ServerPlugin {
-
- protected $server;
-
- public function initialize(Server $server) {
- $this->server = $server;
- $this->server->on('afterMethod:REPORT', [$this, 'httpReport'], 90);
- }
-
- public function httpReport(RequestInterface $request, ResponseInterface $response) {
- $queryParams = $request->getQueryParameters();
- if (!array_key_exists('export', $queryParams)) {
- return;
- }
-
- $contentType = (string)$response->getHeader('Content-Type');
- if (!str_contains($contentType, 'application/xml') && !str_contains($contentType, 'text/xml')) {
- return;
- }
- $this->server->transactionType = 'vcf-multi-get-intercept-and-export';
-
- $responseBody = $response->getBodyAsString();
- $responseXml = $this->server->xml->parse($responseBody);
-
- $output = array_reduce($responseXml->getResponses(), function ($vcf, $card) {
- $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'] . PHP_EOL;
- return $vcf;
- }, '');
-
- $filename = 'vcfexport-' . date('Y-m-d') . '.vcf';
- $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
- $response->setHeader('Content-Type', 'text/vcard');
- $response->setStatus(Http::STATUS_OK);
- $response->setBody($output);
- return true;
- }
-
- public function getPluginName() {
- return 'vcf-multi-get-intercept-and-export';
- }
-
- public function getPluginInfo() {
- return [
- 'name' => $this->getPluginName(),
- 'description' => 'Intercept a multi-get request and return a single vcf file instead.'
- ];
- }
- }
|