MultiGetExportPlugin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\CardDAV;
  26. use Sabre\DAV;
  27. use Sabre\DAV\Server;
  28. use Sabre\HTTP\RequestInterface;
  29. use Sabre\HTTP\ResponseInterface;
  30. class MultiGetExportPlugin extends DAV\ServerPlugin {
  31. /** @var Server */
  32. protected $server;
  33. /**
  34. * Initializes the plugin and registers event handlers
  35. *
  36. * @param Server $server
  37. * @return void
  38. */
  39. public function initialize(Server $server) {
  40. $this->server = $server;
  41. $this->server->on('afterMethod:REPORT', [$this, 'httpReport'], 90);
  42. }
  43. /**
  44. * Intercepts REPORT requests
  45. *
  46. * @param RequestInterface $request
  47. * @param ResponseInterface $response
  48. * @return bool
  49. */
  50. public function httpReport(RequestInterface $request, ResponseInterface $response) {
  51. $queryParams = $request->getQueryParameters();
  52. if (!array_key_exists('export', $queryParams)) {
  53. return;
  54. }
  55. // Only handling xml
  56. $contentType = $response->getHeader('Content-Type');
  57. if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
  58. return;
  59. }
  60. $this->server->transactionType = 'vcf-multi-get-intercept-and-export';
  61. // Get the xml response
  62. $responseBody = $response->getBodyAsString();
  63. $responseXml = $this->server->xml->parse($responseBody);
  64. // Reduce the vcards into one string
  65. $output = array_reduce($responseXml->getResponses(), function ($vcf, $card) {
  66. $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'] . PHP_EOL;
  67. return $vcf;
  68. }, '');
  69. // Build and override the response
  70. $filename = 'vcfexport-' . date('Y-m-d') . '.vcf';
  71. $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  72. $response->setHeader('Content-Type', 'text/vcard');
  73. $response->setStatus(200);
  74. $response->setBody($output);
  75. return true;
  76. }
  77. /**
  78. * Returns a plugin name.
  79. *
  80. * Using this name other plugins will be able to access other plugins
  81. * using \Sabre\DAV\Server::getPlugin
  82. *
  83. * @return string
  84. */
  85. public function getPluginName() {
  86. return 'vcf-multi-get-intercept-and-export';
  87. }
  88. /**
  89. * Returns a bunch of meta-data about the plugin.
  90. *
  91. * Providing this information is optional, and is mainly displayed by the
  92. * Browser plugin.
  93. *
  94. * The description key in the returned array may contain html and will not
  95. * be sanitized.
  96. *
  97. * @return array
  98. */
  99. public function getPluginInfo() {
  100. return [
  101. 'name' => $this->getPluginName(),
  102. 'description' => 'Intercept a multi-get request and return a single vcf file instead.'
  103. ];
  104. }
  105. }