|
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 OC\OCS\Exception; |
|
28
|
|
|
use OCP\AppFramework\Controller; |
|
29
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
30
|
|
|
use OCP\Files\File; |
|
31
|
|
|
use OCP\Files\IRootFolder; |
|
32
|
|
|
use OCP\Files\NotFoundException; |
|
33
|
|
|
use OCP\IRequest; |
|
34
|
|
|
use OCP\IUserSession; |
|
35
|
|
|
|
|
36
|
|
|
class SubscriptionsProxyController extends Controller { |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var IUserSession |
|
40
|
|
|
*/ |
|
41
|
|
|
private $userSession; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \OCP\Files\Folder |
|
45
|
|
|
*/ |
|
46
|
|
|
private $storage; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $content; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var int |
|
55
|
|
|
*/ |
|
56
|
|
|
private $responseStatusCode; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $appName |
|
60
|
|
|
* @param IRequest $request an instance of the request |
|
61
|
|
|
* @param IUserSession $userSession |
|
62
|
|
|
* @param IRootFolder $storage |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($appName, IRequest $request, |
|
65
|
|
|
IUserSession $userSession, IRootFolder $storage) { |
|
66
|
|
|
parent::__construct($appName, $request); |
|
67
|
|
|
$this->userSession = $userSession; |
|
68
|
|
|
$this->storage = $storage; |
|
|
|
|
|
|
69
|
|
|
$this->content = ''; |
|
70
|
|
|
$this->responseStatusCode = HTTP::STATUS_OK; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @NoAdminRequired |
|
75
|
|
|
* @NoCSRFRequired |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $icsurl |
|
78
|
|
|
* @return DataDisplayResponse |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getIcsFile($icsurl) { |
|
81
|
|
|
try { |
|
82
|
|
|
$file = $this->storage->get('/' . basename($icsurl)); |
|
83
|
|
|
|
|
84
|
|
|
if ($file instanceof File) { |
|
|
|
|
|
|
85
|
|
|
$date = new \DateTime(); |
|
86
|
|
|
if ($date->getTimestamp() - $file->getMtime() > 3600) { |
|
87
|
|
|
$this->fetchFile($file, $icsurl); |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->content = $file->getContent(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
93
|
|
|
$file = $this->createFile($this->userSession->getUser()->getUID(), $icsurl); |
|
94
|
|
|
$this->fetchFile($file, $icsurl); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return new DataDisplayResponse($this->content, $this->responseStatusCode, [ |
|
98
|
|
|
'content-type' => 'text/calendar', |
|
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
|
|
|
$this->content = file_get_contents($icsurl, false, $context); |
|
134
|
|
|
$file->putContent($this->content); |
|
135
|
|
|
} catch (\Exception $e) { |
|
136
|
|
|
$this->responseStatusCode = HTTP::STATUS_REQUEST_ENTITY_TOO_LARGE; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param int $userId |
|
143
|
|
|
* @param string $url |
|
144
|
|
|
* @return File |
|
145
|
|
|
*/ |
|
146
|
|
|
private function createFile($userId, $url) { |
|
147
|
|
|
$folder = $this->getFolderForUser($userId); |
|
148
|
|
|
$file = $folder->newFile(basename($url)); |
|
149
|
|
|
return $file; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $userId |
|
154
|
|
|
* @return IRootFolder |
|
155
|
|
|
*/ |
|
156
|
|
|
private function getFolderForUser($userId) { |
|
157
|
|
|
$path = '/CalendarSubscriptions'; |
|
158
|
|
|
if ($this->storage->nodeExists($path)) { |
|
159
|
|
|
$folder = $this->storage->get($path); |
|
160
|
|
|
} else { |
|
161
|
|
|
$folder = $this->storage->newFolder($path); |
|
162
|
|
|
} |
|
163
|
|
|
return $folder; |
|
164
|
|
|
} |
|
165
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..