Completed
Push — master ( 2d707f...1a83f1 )
by Morris
12:00
created

ImageExportPlugin::getPhoto()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 21
nop 1
dl 0
loc 44
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\DAV\CardDAV;
24
25
use OCP\Files\NotFoundException;
26
use OCP\ILogger;
27
use Sabre\CardDAV\Card;
28
use Sabre\DAV\Server;
29
use Sabre\DAV\ServerPlugin;
30
use Sabre\HTTP\RequestInterface;
31
use Sabre\HTTP\ResponseInterface;
32
33
class ImageExportPlugin extends ServerPlugin {
34
35
	/** @var Server */
36
	protected $server;
37
	/** @var PhotoCache */
38
	private $cache;
39
40
	/**
41
	 * ImageExportPlugin constructor.
42
	 *
43
	 * @param PhotoCache $cache
44
	 */
45
	public function __construct(PhotoCache $cache) {
46
		$this->cache = $cache;
47
	}
48
49
	/**
50
	 * Initializes the plugin and registers event handlers
51
	 *
52
	 * @param Server $server
53
	 * @return void
54
	 */
55
	public function initialize(Server $server) {
56
		$this->server = $server;
57
		$this->server->on('method:GET', [$this, 'httpGet'], 90);
58
	}
59
60
	/**
61
	 * Intercepts GET requests on addressbook urls ending with ?photo.
62
	 *
63
	 * @param RequestInterface $request
64
	 * @param ResponseInterface $response
65
	 * @return bool
66
	 */
67
	public function httpGet(RequestInterface $request, ResponseInterface $response) {
68
69
		$queryParams = $request->getQueryParameters();
70
		// TODO: in addition to photo we should also add logo some point in time
71
		if (!array_key_exists('photo', $queryParams)) {
72
			return true;
73
		}
74
75
		$size = isset($queryParams['size']) ? (int)$queryParams['size'] : -1;
76
77
		$path = $request->getPath();
78
		$node = $this->server->tree->getNodeForPath($path);
79
80
		if (!($node instanceof Card)) {
0 ignored issues
show
Bug introduced by
The class Sabre\CardDAV\Card does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
81
			return true;
82
		}
83
84
		$this->server->transactionType = 'carddav-image-export';
85
86
		// Checking ACL, if available.
87
		if ($aclPlugin = $this->server->getPlugin('acl')) {
88
			/** @var \Sabre\DAVACL\Plugin $aclPlugin */
89
			$aclPlugin->checkPrivileges($path, '{DAV:}read');
90
		}
91
92
		// Fetch addressbook
93
		$addressbookpath = explode('/', $path);
94
		array_pop($addressbookpath);
95
		$addressbookpath = implode('/', $addressbookpath);
96
		/** @var AddressBook $addressbook */
97
		$addressbook = $this->server->tree->getNodeForPath($addressbookpath);
98
99
		$response->setHeader('Cache-Control', 'private, max-age=3600, must-revalidate');
100
		$response->setHeader('Etag', $node->getETag() );
101
		$response->setHeader('Pragma', 'public');
102
103
		try {
104
			$file = $this->cache->get($addressbook->getResourceId(), $node->getName(), $size, $node);
105
			$response->setHeader('Content-Type', $file->getMimeType());
106
			$response->setHeader('Content-Disposition', 'attachment');
107
			$response->setStatus(200);
108
109
			$response->setBody($file->getContent());
110
		} catch (NotFoundException $e) {
111
			$response->setStatus(404);
112
		}
113
114
		return false;
115
	}
116
}
117