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