1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
6
|
|
|
* @author John Molakvoæ <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program 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 License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace OCA\Richdocuments\Controller; |
25
|
|
|
|
26
|
|
|
use OCA\Richdocuments\Db\DirectMapper; |
27
|
|
|
use OCA\Richdocuments\TemplateManager; |
28
|
|
|
use OCP\AppFramework\Http\DataResponse; |
29
|
|
|
use OCP\AppFramework\OCS\OCSBadRequestException; |
30
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException; |
31
|
|
|
use OCP\Files\File; |
32
|
|
|
use OCP\Files\Folder; |
33
|
|
|
use OCP\Files\IRootFolder; |
34
|
|
|
use OCP\Files\NotFoundException; |
35
|
|
|
use OCP\IRequest; |
36
|
|
|
use OCP\IURLGenerator; |
37
|
|
|
|
38
|
|
|
class OCSController extends \OCP\AppFramework\OCSController { |
39
|
|
|
|
40
|
|
|
/** @var IRootFolder */ |
41
|
|
|
private $rootFolder; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $userId; |
45
|
|
|
|
46
|
|
|
/** @var DirectMapper */ |
47
|
|
|
private $directMapper; |
48
|
|
|
|
49
|
|
|
/** @var IURLGenerator */ |
50
|
|
|
private $urlGenerator; |
51
|
|
|
|
52
|
|
|
/** @var TemplateManager */ |
53
|
|
|
private $manager; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* OCS controller |
57
|
|
|
* |
58
|
|
|
* @param string $appName |
59
|
|
|
* @param IRequest $request |
60
|
|
|
* @param IRootFolder $rootFolder |
61
|
|
|
* @param string $userId |
62
|
|
|
* @param DirectMapper $directMapper |
63
|
|
|
* @param IURLGenerator $urlGenerator |
64
|
|
|
* @param TemplateManager $manager |
65
|
|
|
*/ |
66
|
|
|
public function __construct(string $appName, |
67
|
|
|
IRequest $request, |
68
|
|
|
IRootFolder $rootFolder, |
69
|
|
|
$userId, |
70
|
|
|
DirectMapper $directMapper, |
71
|
|
|
IURLGenerator $urlGenerator, |
72
|
|
|
TemplateManager $manager) { |
73
|
|
|
parent::__construct($appName, $request); |
74
|
|
|
|
75
|
|
|
$this->rootFolder = $rootFolder; |
76
|
|
|
$this->userId = $userId; |
77
|
|
|
$this->directMapper = $directMapper; |
78
|
|
|
$this->urlGenerator = $urlGenerator; |
79
|
|
|
$this->manager = $manager; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @NoAdminRequired |
84
|
|
|
* |
85
|
|
|
* Init an editing session |
86
|
|
|
* |
87
|
|
|
* @param int $fileId |
88
|
|
|
* @return DataResponse |
89
|
|
|
* @throws OCSNotFoundException|OCSBadRequestException |
90
|
|
|
*/ |
91
|
|
|
public function create($fileId) { |
92
|
|
|
try { |
93
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
94
|
|
|
$nodes = $userFolder->getById($fileId); |
95
|
|
|
|
96
|
|
|
if ($nodes === []) { |
97
|
|
|
throw new OCSNotFoundException(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$node = $nodes[0]; |
101
|
|
|
if ($node instanceof Folder) { |
|
|
|
|
102
|
|
|
throw new OCSBadRequestException('Cannot view folder'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
//TODO check if we can even edit this file with collabora |
106
|
|
|
|
107
|
|
|
$direct = $this->directMapper->newDirect($this->userId, $fileId); |
108
|
|
|
|
109
|
|
|
return new DataResponse([ |
110
|
|
|
'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [ |
111
|
|
|
'token' => $direct->getToken() |
112
|
|
|
]) |
113
|
|
|
]); |
114
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
115
|
|
|
throw new OCSNotFoundException(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @NoAdminRequired |
121
|
|
|
* |
122
|
|
|
* @param string $type The template type |
123
|
|
|
* @return DataResponse |
124
|
|
|
* @throws OCSBadRequestException |
125
|
|
|
*/ |
126
|
|
|
public function getTemplates($type) { |
127
|
|
|
if (array_key_exists($type, TemplateManager::$tplTypes)) { |
128
|
|
|
$templates = $this->manager->getAllFormatted($type); |
129
|
|
|
return new DataResponse($templates); |
130
|
|
|
} |
131
|
|
|
throw new OCSBadRequestException('Wrong type'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @NoAdminRequired |
136
|
|
|
* |
137
|
|
|
* @param string $path Where to create the document |
138
|
|
|
* @param int $template The template id |
139
|
|
|
*/ |
140
|
|
|
public function createFromTemplate($path, $template) { |
141
|
|
|
if ($path === null || $template === null) { |
142
|
|
|
throw new OCSBadRequestException('path and template must be set'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!$this->manager->isTemplate($template)) { |
146
|
|
|
throw new OCSBadRequestException('Invalid template provided'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$info = $this->mb_pathinfo($path); |
150
|
|
|
|
151
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
152
|
|
|
$folder = $userFolder->get($info['dirname']); |
153
|
|
|
$name = $folder->getNonExistingName($info['basename']); |
154
|
|
|
$file = $folder->newFile($name); |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
$direct = $this->directMapper->newDirect($this->userId, $template, $file->getId()); |
158
|
|
|
|
159
|
|
|
return new DataResponse([ |
160
|
|
|
'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [ |
161
|
|
|
'token' => $direct->getToken() |
162
|
|
|
]) |
163
|
|
|
]); |
164
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
165
|
|
|
throw new OCSNotFoundException(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function mb_pathinfo($filepath) { |
170
|
|
|
$result = []; |
171
|
|
|
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im',$filepath,$matches); |
172
|
|
|
if($matches[1]) { |
173
|
|
|
$result['dirname'] = $matches[1]; |
174
|
|
|
} |
175
|
|
|
if($matches[2]) { |
176
|
|
|
$result['basename'] = $matches[2]; |
177
|
|
|
} |
178
|
|
|
if($matches[5]) { |
179
|
|
|
$result['extension'] = $matches[5]; |
180
|
|
|
} |
181
|
|
|
if($matches[3]) { |
182
|
|
|
$result['filename'] = $matches[3]; |
183
|
|
|
} |
184
|
|
|
return $result; |
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.