You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiGetExportPlugin.php 3.3KB

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