ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
VCFExportPlugin.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\DAV;
9
23
29 protected $server;
30
37 function initialize(DAV\Server $server) {
38
39 $this->server = $server;
40 $this->server->on('method:GET', [$this, 'httpGet'], 90);
41 $server->on('browserButtonActions', function($path, $node, &$actions) {
42 if ($node instanceof IAddressBook) {
43 $actions .= '<a href="' . htmlspecialchars($path, ENT_QUOTES, 'UTF-8') . '?export"><span class="oi" data-glyph="book"></span></a>';
44 }
45 });
46 }
47
56
57 $queryParams = $request->getQueryParameters();
58 if (!array_key_exists('export', $queryParams)) return;
59
60 $path = $request->getPath();
61
62 $node = $this->server->tree->getNodeForPath($path);
63
64 if (!($node instanceof IAddressBook)) return;
65
66 $this->server->transactionType = 'get-addressbook-export';
67
68 // Checking ACL, if available.
69 if ($aclPlugin = $this->server->getPlugin('acl')) {
70 $aclPlugin->checkPrivileges($path, '{DAV:}read');
71 }
72
73 $nodes = $this->server->getPropertiesForPath($path, [
74 '{' . Plugin::NS_CARDDAV . '}address-data',
75 ], 1);
76
77 $format = 'text/directory';
78
79 $output = null;
80 $filenameExtension = null;
81
82 switch ($format) {
83 case 'text/directory':
84 $output = $this->generateVCF($nodes);
85 $filenameExtension = '.vcf';
86 break;
87 }
88
89 $filename = preg_replace(
90 '/[^a-zA-Z0-9-_ ]/um',
91 '',
92 $node->getName()
93 );
94 $filename .= '-' . date('Y-m-d') . $filenameExtension;
95
96 $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
97 $response->setHeader('Content-Type', $format);
98
99 $response->setStatus(200);
100 $response->setBody($output);
101
102 // Returning false to break the event chain
103 return false;
104
105 }
106
113 function generateVCF(array $nodes) {
114
115 $output = "";
116
117 foreach ($nodes as $node) {
118
119 if (!isset($node[200]['{' . Plugin::NS_CARDDAV . '}address-data'])) {
120 continue;
121 }
122 $nodeData = $node[200]['{' . Plugin::NS_CARDDAV . '}address-data'];
123
124 // Parsing this node so VObject can clean up the output.
125 $vcard = VObject\Reader::read($nodeData);
126 $output .= $vcard->serialize();
127
128 // Destroy circular references to PHP will GC the object.
129 $vcard->destroy();
130
131 }
132
133 return $output;
134
135 }
136
145 function getPluginName() {
146
147 return 'vcf-export';
148
149 }
150
162 function getPluginInfo() {
163
164 return [
165 'name' => $this->getPluginName(),
166 'description' => 'Adds the ability to export CardDAV addressbooks as a single vCard file.',
167 'link' => 'http://sabre.io/dav/vcf-export-plugin/',
168 ];
169
170 }
171
172}
$path
Definition: aliased.php:25
foreach($paths as $path) $request
Definition: asyncclient.php:32
$filename
Definition: buildRTE.php:89
$aclPlugin
An exception for terminatinating execution or to throw for unit testing.
const NS_CARDDAV
xml namespace for CardDAV elements
Definition: Plugin.php:33
initialize(DAV\Server $server)
Initializes the plugin and registers event handlers.
generateVCF(array $nodes)
Merges all vcard objects, and builds one big vcf export.
getPluginName()
Returns a plugin name.
httpGet(RequestInterface $request, ResponseInterface $response)
Intercepts GET requests on addressbook urls ending with ?export.
getPluginInfo()
Returns a bunch of meta-data about the plugin.
The baseclass for all server plugins.
Main DAV server class.
Definition: Server.php:23
static read($data, $options=0, $charset='UTF-8')
Parses a vCard or iCalendar object, and returns the top component.
Definition: Reader.php:42
AddressBook interface.
The RequestInterface represents a HTTP request.
This interface represents a HTTP response.
$format
Definition: metadata.php:141
$response