|
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\Folder |
|
44
|
|
|
*/ |
|
45
|
|
|
private $storage; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $appName |
|
49
|
|
|
* @param IRequest $request an instance of the request |
|
50
|
|
|
* @param IUserSession $userSession |
|
51
|
|
|
* @param IRootFolder $storage |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($appName, IRequest $request, |
|
54
|
|
|
IUserSession $userSession, IRootFolder $storage) { |
|
55
|
|
|
parent::__construct($appName, $request); |
|
56
|
|
|
$this->userSession = $userSession; |
|
57
|
|
|
$this->storage = $storage; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @NoAdminRequired |
|
62
|
|
|
* @NoCSRFRequired |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $icsurl |
|
65
|
|
|
* @return DataDisplayResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getIcsFile($icsurl) { |
|
68
|
|
|
try { |
|
69
|
|
|
$file = $this->storage->get('/' . basename($icsurl)); |
|
70
|
|
|
|
|
71
|
|
|
if($file instanceof File) { |
|
|
|
|
|
|
72
|
|
|
$date = new \DateTime(); |
|
73
|
|
|
if ($date->getTimestamp() - $file->getMtime() > 3600) { |
|
74
|
|
|
$content = $this->fetchFile($file, $icsurl); |
|
75
|
|
|
} else { |
|
76
|
|
|
$content = $file->getContent(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
|
|
80
|
|
|
$file = $this->createFile($this->userSession->getUser()->getUID(), $icsurl); |
|
81
|
|
|
$content = $this->fetchFile($file, $icsurl); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new DataDisplayResponse($content, HTTP::STATUS_OK, [ |
|
|
|
|
|
|
85
|
|
|
'content-type' => 'text/calendar', |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param File $file |
|
91
|
|
|
* @param $icsurl |
|
92
|
|
|
* @return null|string |
|
93
|
|
|
*/ |
|
94
|
|
|
private function fetchFile(File $file, $icsurl) { |
|
95
|
|
|
$opts = array('http' => array('method' => 'GET', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60)); |
|
96
|
|
|
$context = stream_context_create($opts); |
|
97
|
|
|
$content = file_get_contents($icsurl, false, $context); |
|
98
|
|
|
$file->putContent($content); |
|
99
|
|
|
return $content; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param int $userId |
|
105
|
|
|
* @param string $url |
|
106
|
|
|
* @return File |
|
107
|
|
|
*/ |
|
108
|
|
|
private function createFile($userId, $url) { |
|
109
|
|
|
$folder = $this->getFolderForUser($userId); |
|
110
|
|
|
$file = $folder->newFile(basename($url)); |
|
111
|
|
|
return $file; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $userId |
|
116
|
|
|
* @return IRootFolder |
|
117
|
|
|
*/ |
|
118
|
|
|
private function getFolderForUser($userId) { |
|
119
|
|
|
$path = '/' . $userId . '/files/CalendarSubscriptions'; |
|
120
|
|
|
if ($this->storage->nodeExists($path)) { |
|
121
|
|
|
$folder = $this->storage->get($path); |
|
122
|
|
|
} else { |
|
123
|
|
|
$folder = $this->storage->newFolder($path); |
|
124
|
|
|
} |
|
125
|
|
|
return $folder; |
|
126
|
|
|
} |
|
127
|
|
|
} |
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..