1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Richdocuments\Controller; |
23
|
|
|
|
24
|
|
|
use OC\Files\View; |
25
|
|
|
use OCA\Richdocuments\Db\Wopi; |
26
|
|
|
use OCA\Richdocuments\Helper; |
27
|
|
|
use OCA\Richdocuments\WOPI\Parser; |
28
|
|
|
use OCP\AppFramework\Controller; |
29
|
|
|
use OCP\AppFramework\Http; |
30
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
31
|
|
|
use OCP\Files\File; |
32
|
|
|
use OCP\Files\IRootFolder; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\IUserManager; |
35
|
|
|
use OCP\AppFramework\Http\StreamResponse; |
36
|
|
|
|
37
|
|
|
class WopiController extends Controller { |
38
|
|
|
/** @var IRootFolder */ |
39
|
|
|
private $rootFolder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $appName |
43
|
|
|
* @param IRequest $request |
44
|
|
|
* @param IRootFolder $rootFolder |
45
|
|
|
* @param string $UserId |
46
|
|
|
*/ |
47
|
|
|
public function __construct($appName, |
48
|
|
|
$UserId, |
49
|
|
|
IRequest $request, |
50
|
|
|
IRootFolder $rootFolder) { |
51
|
|
|
parent::__construct($appName, $request); |
52
|
|
|
$this->rootFolder = $rootFolder; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns general info about a file. |
57
|
|
|
* |
58
|
|
|
* @NoAdminRequired |
59
|
|
|
* @NoCSRFRequired |
60
|
|
|
* @PublicPage |
61
|
|
|
* |
62
|
|
|
* @param string $fileId |
63
|
|
|
* @return JSONResponse |
64
|
|
|
*/ |
65
|
|
|
public function checkFileInfo($fileId) { |
66
|
|
|
$token = $this->request->getParam('access_token'); |
67
|
|
|
|
68
|
|
|
list($fileId, , $version) = Helper::parseFileId($fileId); |
69
|
|
|
$db = new Wopi(); |
70
|
|
|
$res = $db->getPathForToken($fileId, $token); |
71
|
|
|
if ($res === false) { |
72
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Login the user to see his mount locations |
76
|
|
|
try { |
77
|
|
|
/** @var File $file */ |
78
|
|
|
$userFolder = $this->rootFolder->getUserFolder($res['owner']); |
79
|
|
|
$file = $userFolder->getById($fileId)[0]; |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if(!($file instanceof File)) { |
|
|
|
|
85
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return new JSONResponse( |
89
|
|
|
[ |
90
|
|
|
'BaseFileName' => $file->getName(), |
91
|
|
|
'Size' => $file->getSize(), |
92
|
|
|
'Version' => $version, |
93
|
|
|
'UserId' => $res['editor'] !== '' ? $res['editor'] : 'Guest user', |
94
|
|
|
'OwnerId' => $res['owner'], |
95
|
|
|
'UserFriendlyName' => $res['editor'] !== '' ? $res['editor'] : 'Guest user', |
96
|
|
|
'UserCanWrite' => $res['canwrite'] ? true : false, |
97
|
|
|
'PostMessageOrigin' => $res['server_host'], |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Given an access token and a fileId, returns the contents of the file. |
104
|
|
|
* Expects a valid token in access_token parameter. |
105
|
|
|
* |
106
|
|
|
* @PublicPage |
107
|
|
|
* @NoCSRFRequired |
108
|
|
|
* |
109
|
|
|
* @param string $fileId |
110
|
|
|
* @param string $access_token |
111
|
|
|
* @return Http\Response |
112
|
|
|
*/ |
113
|
|
|
public function getFile($fileId, |
114
|
|
|
$access_token) { |
115
|
|
|
list($fileId, , $version) = Helper::parseFileId($fileId); |
116
|
|
|
$row = new Wopi(); |
117
|
|
|
$row->loadBy('token', $access_token); |
118
|
|
|
$res = $row->getPathForToken($fileId, $access_token); |
119
|
|
|
try { |
120
|
|
|
/** @var File $file */ |
121
|
|
|
$userFolder = $this->rootFolder->getUserFolder($res['owner']); |
122
|
|
|
$file = $userFolder->getById($fileId)[0]; |
123
|
|
|
|
124
|
|
|
if ($version !== '0') |
125
|
|
|
{ |
126
|
|
|
$view = new View('/' . $res['owner'] . '/files'); |
127
|
|
|
$relPath = $view->getRelativePath($file->getPath()); |
128
|
|
|
$versionPath = '/files_versions/' . $relPath . '.v' . $version; |
129
|
|
|
$view = new View('/' . $res['owner']); |
130
|
|
|
if ($view->file_exists($versionPath)){ |
131
|
|
|
$response = new StreamResponse($view->fopen($versionPath, 'rb')); |
132
|
|
|
} |
133
|
|
|
else { |
134
|
|
|
$response->setStatus(Http::STATUS_NOT_FOUND); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
else |
138
|
|
|
{ |
139
|
|
|
$response = new StreamResponse($file->fopen('rb')); |
140
|
|
|
} |
141
|
|
|
$response->addHeader('Content-Disposition', 'attachment'); |
|
|
|
|
142
|
|
|
$response->addHeader('Content-Type', 'application/octet-stream'); |
143
|
|
|
return $response; |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Given an access token and a fileId, replaces the files with the request body. |
151
|
|
|
* Expects a valid token in access_token parameter. |
152
|
|
|
* |
153
|
|
|
* @PublicPage |
154
|
|
|
* @NoCSRFRequired |
155
|
|
|
* |
156
|
|
|
* @param string $fileId |
157
|
|
|
* @param string $access_token |
158
|
|
|
* @return JSONResponse |
159
|
|
|
*/ |
160
|
|
|
public function putFile($fileId, |
161
|
|
|
$access_token) { |
162
|
|
|
list($fileId, , $version) = Helper::parseFileId($fileId); |
|
|
|
|
163
|
|
|
|
164
|
|
|
$row = new Wopi(); |
165
|
|
|
$row->loadBy('token', $access_token); |
166
|
|
|
|
167
|
|
|
$res = $row->getPathForToken($fileId, $access_token); |
168
|
|
|
if (!$res['canwrite']) { |
169
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
try { |
173
|
|
|
/** @var File $file */ |
174
|
|
|
$userFolder = $this->rootFolder->getUserFolder($res['owner']); |
175
|
|
|
$file = $userFolder->getById($fileId)[0]; |
176
|
|
|
$content = fopen('php://input', 'rb'); |
177
|
|
|
// Setup the FS which is needed to emit hooks (versioning). |
178
|
|
|
\OC_Util::tearDownFS(); |
179
|
|
|
\OC_Util::setupFS($res['owner']); |
180
|
|
|
$file->putContent($content); |
181
|
|
|
return new JSONResponse(); |
182
|
|
|
} catch (\Exception $e) { |
183
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.