1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Thomas Tanghus |
4
|
|
|
* @copyright 2013-2014 Thomas Tanghus ([email protected]) |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. |
8
|
|
|
* See the COPYING-README file. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace OCA\Contacts\Controller; |
12
|
|
|
|
13
|
|
|
use OCA\Contacts\App, |
14
|
|
|
OCA\Contacts\JSONResponse, |
15
|
|
|
OCA\Contacts\Controller, |
16
|
|
|
OCA\Contacts\TextDownloadResponse, |
17
|
|
|
Sabre\VObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Controller importing contacts |
21
|
|
|
*/ |
22
|
|
|
class ExportController extends Controller { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Export an entire address book. |
26
|
|
|
* |
27
|
|
|
* @NoAdminRequired |
28
|
|
|
* @NoCSRFRequired |
29
|
|
|
*/ |
30
|
|
|
public function exportAddressBook() { |
31
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG); |
32
|
|
|
$params = $this->request->urlParams; |
33
|
|
|
|
34
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
35
|
|
|
$lastModified = $addressBook->lastModified(); |
36
|
|
|
|
37
|
|
|
$contacts = ''; |
38
|
|
|
foreach($addressBook->getChildren() as $i => $contact) { |
39
|
|
|
$contacts .= $contact->serialize() . "\r\n"; |
40
|
|
|
} |
41
|
|
|
$name = str_replace(' ', '_', $addressBook->getDisplayName()) . '.vcf'; |
42
|
|
|
$response = new TextDownloadResponse($contacts, $name, 'text/directory'); |
43
|
|
View Code Duplication |
if(!is_null($lastModified)) { |
|
|
|
|
44
|
|
|
$response->addHeader('Cache-Control', 'private, must-revalidate'); |
45
|
|
|
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null); |
46
|
|
|
$response->setETag(md5($lastModified)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Export a single contact. |
54
|
|
|
* |
55
|
|
|
* @NoAdminRequired |
56
|
|
|
* @NoCSRFRequired |
57
|
|
|
*/ |
58
|
|
|
public function exportContact() { |
59
|
|
|
|
60
|
|
|
$params = $this->request->urlParams; |
61
|
|
|
|
62
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
63
|
|
|
$contact = $addressBook->getChild($params['contactId']); |
64
|
|
|
|
65
|
|
|
if(!$contact) { |
66
|
|
|
$response = new JSONResponse(); |
67
|
|
|
$response->bailOut(App::$l10n->t('Couldn\'t find contact.')); |
68
|
|
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$name = str_replace(' ', '_', $contact->getDisplayName()) . '.vcf'; |
72
|
|
|
return new TextDownloadResponse($contact->serialize(), $name, 'text/vcard'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Export a selected range of contacts potentially from different backends and address books. |
77
|
|
|
* |
78
|
|
|
* @NoAdminRequired |
79
|
|
|
* @NoCSRFRequired |
80
|
|
|
*/ |
81
|
|
|
public function exportSelected() { |
82
|
|
|
$targets = json_decode($this->request['t']); |
83
|
|
|
|
84
|
|
|
$exports = ''; |
85
|
|
|
foreach($targets as $backend => $addressBooks) { |
86
|
|
|
foreach($addressBooks as $addressBookId => $contacts) { |
87
|
|
|
$addressBook = $this->app->getAddressBook($backend, $addressBookId); |
88
|
|
|
foreach($contacts as $contactId) { |
89
|
|
|
$contact = $addressBook->getChild($contactId); |
90
|
|
|
$exports .= $contact->serialize() . "\r\n"; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$name = 'Selected_contacts' . '.vcf'; |
96
|
|
|
return new TextDownloadResponse($exports, $name, 'text/vcard'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.