Completed
Pull Request — master (#631)
by Thomas
04:11
created

SubscriptionsProxyController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 135
rs 10
wmc 15
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getIcsFile() 0 21 4
A checkHeaders() 0 15 3
A fetchFile() 0 16 4
A createFile() 0 5 1
A getFolder() 0 9 2
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library 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
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OC\AppFramework\Http;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\DataDisplayResponse;
29
use OCP\Files\File;
30
use OCP\Files\IRootFolder;
31
use OCP\Files\NotFoundException;
32
use OCP\IRequest;
33
use OCP\IUserSession;
34
35
class SubscriptionsProxyController extends Controller {
36
37
	/**
38
	 * @var IUserSession
39
	 */
40
	private $userSession;
41
42
	/**
43
	 * @var \OCP\Files\IRootFolder
44
	 */
45
	private $storage;
46
47
	/**
48
	 * @var string
49
	 */
50
	private $content;
51
52
	/**
53
	 * @var int
54
	 */
55
	private $responseStatusCode;
56
57
	/**
58
	 * @param string $appName
59
	 * @param IRequest $request an instance of the request
60
	 * @param IUserSession $userSession
61
	 * @param IRootFolder $storage
62
	 */
63
	public function __construct($appName, IRequest $request,
64
									IUserSession $userSession, IRootFolder $storage) {
65
		parent::__construct($appName, $request);
66
		$this->userSession = $userSession;
67
		$this->storage = $storage;
68
		$this->content = '';
69
		$this->responseStatusCode = HTTP::STATUS_OK;
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 * @NoCSRFRequired
75
	 *
76
	 * @param string $icsurl
77
	 * @return DataDisplayResponse
78
	 */
79
	public function getIcsFile($icsurl) {
80
		try {
81
			$file = $this->storage->get('/' . basename($icsurl));
82
83
			if ($file instanceof File) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\File 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...
84
				$date = new \DateTime();
85
				if ($date->getTimestamp() - $file->getMtime() > 3600) {
86
					$this->fetchFile($file, $icsurl);
87
				} else {
88
					$this->content = $file->getContent();
89
				}
90
			}
91
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
			$file = $this->createFile($this->userSession->getUser()->getUID(), $icsurl);
93
			$this->fetchFile($file, $icsurl);
94
		}
95
96
		return new DataDisplayResponse($this->content, $this->responseStatusCode, [
97
			'content-type' => 'text/calendar',
98
		]);
99
	}
100
101
	/**
102
	 * @param string $url
103
	 * @return boolean
104
	 * @throws \Exception
105
	 */
106
	private function checkHeaders($url) {
107
		stream_context_set_default(
108
			array(
109
				'http' => array(
110
					'method' => 'HEAD',
111
					'ignore_errors'=> true
112
				)
113
			)
114
		);
115
116
		$headers = array_change_key_case(get_headers($url, 1));
117
118
		if (isset($headers['content-length']) && $headers['content-length'] > 5242880) // 5 MB
119
			throw new \Exception("Calendar subscription file is bigger than the maximum file size. Aborting download.");
120
	}
121
122
	/**
123
	 * @param File $file
124
	 * @param $icsurl
125
	 * @return null|string
126
	 */
127
	private function fetchFile(File $file, $icsurl) {
128
		try {
129
			$this->checkHeaders($icsurl);
130
			$opts = array('http' => array('method' => 'GET', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60));
131
			$context = stream_context_create($opts);
132
			$content = file_get_contents($icsurl, false, $context, 0, 5242880);
133
134
			// check if contents starts as a calendar file : it's something ¯\_(ツ)_/¯
135
			if (substr($content, 0, 15) === 'BEGIN:VCALENDAR' || substr($content, 0, 12) === 'BEGIN:VEVENT') {
136
				$this->content = $content;
137
				$file->putContent($this->content);
138
			}
139
		} catch (\Exception $e) {
140
			$this->responseStatusCode = HTTP::STATUS_REQUEST_ENTITY_TOO_LARGE;
141
		}
142
	}
143
144
145
	/**
146
	 * @param int $userId
147
	 * @param string $url
148
	 * @return File
149
	 */
150
	private function createFile($userId, $url) {
151
		$folder = $this->getFolder($userId);
152
		$file = $folder->newFile(basename($url));
153
		return $file;
154
	}
155
156
	/**
157
	 * @param $userId
158
	 * @return IRootFolder
159
	 */
160
	private function getFolder() {
161
		$path = '/CalendarSubscriptions';
162
		if ($this->storage->nodeExists($path)) {
163
			$folder = $this->storage->get($path);
164
		} else {
165
			$folder = $this->storage->newFolder($path);
166
		}
167
		return $folder;
168
	}
169
}