@@ -33,89 +33,89 @@ |
||
| 33 | 33 | |
| 34 | 34 | class MultiGetExportPlugin extends DAV\ServerPlugin { |
| 35 | 35 | |
| 36 | - /** @var Server */ |
|
| 37 | - protected $server; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Initializes the plugin and registers event handlers |
|
| 41 | - * |
|
| 42 | - * @param Server $server |
|
| 43 | - * @return void |
|
| 44 | - */ |
|
| 45 | - public function initialize(Server $server) { |
|
| 46 | - $this->server = $server; |
|
| 47 | - $this->server->on('afterMethod:REPORT', [$this, 'httpReport'], 90); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Intercepts REPORT requests |
|
| 52 | - * |
|
| 53 | - * @param RequestInterface $request |
|
| 54 | - * @param ResponseInterface $response |
|
| 55 | - * @return bool |
|
| 56 | - */ |
|
| 57 | - public function httpReport(RequestInterface $request, ResponseInterface $response) { |
|
| 58 | - $queryParams = $request->getQueryParameters(); |
|
| 59 | - if (!array_key_exists('export', $queryParams)) { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Only handling xml |
|
| 64 | - $contentType = $response->getHeader('Content-Type'); |
|
| 65 | - if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->server->transactionType = 'vcf-multi-get-intercept-and-export'; |
|
| 70 | - |
|
| 71 | - // Get the xml response |
|
| 72 | - $responseBody = $response->getBodyAsString(); |
|
| 73 | - $responseXml = $this->server->xml->parse($responseBody); |
|
| 74 | - |
|
| 75 | - // Reduce the vcards into one string |
|
| 76 | - $output = array_reduce($responseXml->getResponses(), function ($vcf, $card) { |
|
| 77 | - $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'] . PHP_EOL; |
|
| 78 | - return $vcf; |
|
| 79 | - }, ''); |
|
| 80 | - |
|
| 81 | - // Build and override the response |
|
| 82 | - $filename = 'vcfexport-' . date('Y-m-d') . '.vcf'; |
|
| 83 | - $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
|
| 84 | - $response->setHeader('Content-Type', 'text/vcard'); |
|
| 85 | - |
|
| 86 | - $response->setStatus(200); |
|
| 87 | - $response->setBody($output); |
|
| 88 | - |
|
| 89 | - return true; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Returns a plugin name. |
|
| 94 | - * |
|
| 95 | - * Using this name other plugins will be able to access other plugins |
|
| 96 | - * using \Sabre\DAV\Server::getPlugin |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getPluginName() { |
|
| 101 | - return 'vcf-multi-get-intercept-and-export'; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Returns a bunch of meta-data about the plugin. |
|
| 106 | - * |
|
| 107 | - * Providing this information is optional, and is mainly displayed by the |
|
| 108 | - * Browser plugin. |
|
| 109 | - * |
|
| 110 | - * The description key in the returned array may contain html and will not |
|
| 111 | - * be sanitized. |
|
| 112 | - * |
|
| 113 | - * @return array |
|
| 114 | - */ |
|
| 115 | - public function getPluginInfo() { |
|
| 116 | - return [ |
|
| 117 | - 'name' => $this->getPluginName(), |
|
| 118 | - 'description' => 'Intercept a multi-get request and return a single vcf file instead.' |
|
| 119 | - ]; |
|
| 120 | - } |
|
| 36 | + /** @var Server */ |
|
| 37 | + protected $server; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Initializes the plugin and registers event handlers |
|
| 41 | + * |
|
| 42 | + * @param Server $server |
|
| 43 | + * @return void |
|
| 44 | + */ |
|
| 45 | + public function initialize(Server $server) { |
|
| 46 | + $this->server = $server; |
|
| 47 | + $this->server->on('afterMethod:REPORT', [$this, 'httpReport'], 90); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Intercepts REPORT requests |
|
| 52 | + * |
|
| 53 | + * @param RequestInterface $request |
|
| 54 | + * @param ResponseInterface $response |
|
| 55 | + * @return bool |
|
| 56 | + */ |
|
| 57 | + public function httpReport(RequestInterface $request, ResponseInterface $response) { |
|
| 58 | + $queryParams = $request->getQueryParameters(); |
|
| 59 | + if (!array_key_exists('export', $queryParams)) { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Only handling xml |
|
| 64 | + $contentType = $response->getHeader('Content-Type'); |
|
| 65 | + if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->server->transactionType = 'vcf-multi-get-intercept-and-export'; |
|
| 70 | + |
|
| 71 | + // Get the xml response |
|
| 72 | + $responseBody = $response->getBodyAsString(); |
|
| 73 | + $responseXml = $this->server->xml->parse($responseBody); |
|
| 74 | + |
|
| 75 | + // Reduce the vcards into one string |
|
| 76 | + $output = array_reduce($responseXml->getResponses(), function ($vcf, $card) { |
|
| 77 | + $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'] . PHP_EOL; |
|
| 78 | + return $vcf; |
|
| 79 | + }, ''); |
|
| 80 | + |
|
| 81 | + // Build and override the response |
|
| 82 | + $filename = 'vcfexport-' . date('Y-m-d') . '.vcf'; |
|
| 83 | + $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
|
| 84 | + $response->setHeader('Content-Type', 'text/vcard'); |
|
| 85 | + |
|
| 86 | + $response->setStatus(200); |
|
| 87 | + $response->setBody($output); |
|
| 88 | + |
|
| 89 | + return true; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Returns a plugin name. |
|
| 94 | + * |
|
| 95 | + * Using this name other plugins will be able to access other plugins |
|
| 96 | + * using \Sabre\DAV\Server::getPlugin |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getPluginName() { |
|
| 101 | + return 'vcf-multi-get-intercept-and-export'; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Returns a bunch of meta-data about the plugin. |
|
| 106 | + * |
|
| 107 | + * Providing this information is optional, and is mainly displayed by the |
|
| 108 | + * Browser plugin. |
|
| 109 | + * |
|
| 110 | + * The description key in the returned array may contain html and will not |
|
| 111 | + * be sanitized. |
|
| 112 | + * |
|
| 113 | + * @return array |
|
| 114 | + */ |
|
| 115 | + public function getPluginInfo() { |
|
| 116 | + return [ |
|
| 117 | + 'name' => $this->getPluginName(), |
|
| 118 | + 'description' => 'Intercept a multi-get request and return a single vcf file instead.' |
|
| 119 | + ]; |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -73,14 +73,14 @@ |
||
| 73 | 73 | $responseXml = $this->server->xml->parse($responseBody); |
| 74 | 74 | |
| 75 | 75 | // Reduce the vcards into one string |
| 76 | - $output = array_reduce($responseXml->getResponses(), function ($vcf, $card) { |
|
| 77 | - $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'] . PHP_EOL; |
|
| 76 | + $output = array_reduce($responseXml->getResponses(), function($vcf, $card) { |
|
| 77 | + $vcf .= $card->getResponseProperties()[200]['{urn:ietf:params:xml:ns:carddav}address-data'].PHP_EOL; |
|
| 78 | 78 | return $vcf; |
| 79 | 79 | }, ''); |
| 80 | 80 | |
| 81 | 81 | // Build and override the response |
| 82 | - $filename = 'vcfexport-' . date('Y-m-d') . '.vcf'; |
|
| 83 | - $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
|
| 82 | + $filename = 'vcfexport-'.date('Y-m-d').'.vcf'; |
|
| 83 | + $response->setHeader('Content-Disposition', 'attachment; filename="'.$filename.'"'); |
|
| 84 | 84 | $response->setHeader('Content-Type', 'text/vcard'); |
| 85 | 85 | |
| 86 | 86 | $response->setStatus(200); |