Blame view

sources/apps/contacts/lib/controller/exportcontroller.php 2.62 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
  <?php
  /**
   * @author Thomas Tanghus
6d9380f96   Cédric Dupont   Update sources OC...
4
5
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
   *
d1bafeea1   Kload   [fix] Upgrade to ...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OCA\Contacts\Controller;
  
  use OCA\Contacts\App,
  	OCA\Contacts\JSONResponse,
  	OCA\Contacts\Controller,
  	OCA\Contacts\TextDownloadResponse,
  	Sabre\VObject;
  
  /**
   * Controller importing contacts
   */
  class ExportController extends Controller {
  
  	/**
  	 * Export an entire address book.
  	 *
  	 * @NoAdminRequired
  	 * @NoCSRFRequired
  	 */
  	public function exportAddressBook() {
  		\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
  		$params = $this->request->urlParams;
  
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$lastModified = $addressBook->lastModified();
  
  		$contacts = '';
  		foreach($addressBook->getChildren() as $i => $contact) {
  			$contacts .= $contact->serialize() . "\r
  ";
  		}
  		$name = str_replace(' ', '_', $addressBook->getDisplayName()) . '.vcf';
  		$response = new TextDownloadResponse($contacts, $name, 'text/directory');
  		if(!is_null($lastModified)) {
  			$response->addHeader('Cache-Control', 'private, must-revalidate');
  			$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
  			$response->setETag(md5($lastModified));
  		}
  
  		return $response;
  	}
  
  	/**
  	 * Export a single contact.
  	 *
  	 * @NoAdminRequired
  	 * @NoCSRFRequired
  	 */
  	public function exportContact() {
  
  		$params = $this->request->urlParams;
  
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$contact = $addressBook->getChild($params['contactId']);
  
  		if(!$contact) {
  			$response = new JSONResponse();
  			$response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
  			return $response;
  		}
  
  		$name = str_replace(' ', '_', $contact->getDisplayName()) . '.vcf';
  		return new TextDownloadResponse($contact->serialize(), $name, 'text/vcard');
  	}
  
  	/**
  	 * Export a selected range of contacts potentially from different backends and address books.
  	 *
  	 * @NoAdminRequired
  	 * @NoCSRFRequired
  	 */
  	public function exportSelected() {
  		$targets = $this->request['t'];
  
  		$exports = '';
  		foreach($targets as $backend => $addressBooks) {
  			foreach($addressBooks as $addressBookId => $contacts) {
  				$addressBook = $this->app->getAddressBook($backend, $addressBookId);
  				foreach($contacts as $contactId) {
  					$contact = $addressBook->getChild($contactId);
  					$exports .= $contact->serialize() . "\r
  ";
  				}
  			}
  		}
  
  		$name = 'Selected_contacts' . '.vcf';
  		return new TextDownloadResponse($exports, $name, 'text/vcard');
  	}
  
  }