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