1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
<?php
/**
* ownCloud - Richdocuments App
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Richdocuments\Controller;
use OCA\Richdocuments\TokenManager;
use OCA\Richdocuments\WOPI\Parser;
use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\IL10N;
use \OCP\AppFramework\Http\ContentSecurityPolicy;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCA\Richdocuments\AppConfig;
use \OCA\Richdocuments\Helper;
use \OC\Files\View;
use OCP\ISession;
use OCP\Share\IManager;
class DocumentController extends Controller {
/** @var string */
private $uid;
/** @var IL10N */
private $l10n;
/** @var IConfig */
private $settings;
/** @var AppConfig */
private $appConfig;
/** @var Parser */
private $wopiParser;
/** @var IManager */
private $shareManager;
/** @var TokenManager */
private $tokenManager;
/** @var ISession */
private $session;
/** @var IRootFolder */
private $rootFolder;
const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt';
/**
* @param string $appName
* @param IRequest $request
* @param IConfig $settings
* @param AppConfig $appConfig
* @param IL10N $l10n
* @param Parser $wopiParser
* @param IManager $shareManager
* @param TokenManager $tokenManager
* @param IRootFolder $rootFolder
* @param ISession $session
* @param string $UserId
*/
public function __construct($appName,
IRequest $request,
IConfig $settings,
AppConfig $appConfig,
IL10N $l10n,
Parser $wopiParser,
IManager $shareManager,
TokenManager $tokenManager,
IRootFolder $rootFolder,
ISession $session,
$UserId) {
parent::__construct($appName, $request);
$this->uid = $UserId;
$this->l10n = $l10n;
$this->settings = $settings;
$this->appConfig = $appConfig;
$this->wopiParser = $wopiParser;
$this->shareManager = $shareManager;
$this->tokenManager = $tokenManager;
$this->rootFolder = $rootFolder;
$this->session = $session;
}
/**
* @NoAdminRequired
*
* @param string $fileId
* @return TemplateResponse
*/
public function index($fileId) {
try {
$folder = $this->rootFolder->getUserFolder($this->uid);
$item = $folder->getById($fileId)[0];
if(!($item instanceof Node)) {
throw new \Exception();
}
list($urlSrc, $token) = $this->tokenManager->getToken($item->getId());
$params = [
'permissions' => $item->getPermissions(),
'title' => $item->getName(),
'fileId' => $item->getId() . '_' . $this->settings->getSystemValue('instanceid'),
'token' => $token,
'urlsrc' => $urlSrc,
'path' => '/',
'instanceId' => $this->settings->getSystemValue('instanceid'),
];
$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
$policy = new ContentSecurityPolicy();
$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
$policy->allowInlineScript(true);
$response->setContentSecurityPolicy($policy);
return $response;
} catch (\Exception $e) {
}
return new TemplateResponse('core', '403', [], 'guest');
}
/**
* @PublicPage
*
* @param string $shareToken
* @param string $fileName
* @return TemplateResponse
* @throws \Exception
*/
public function publicPage($shareToken, $fileName) {
try {
$share = $this->shareManager->getShareByToken($shareToken);
// not authenticated ?
if($share->getPassword()){
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
) {
throw new \Exception('Invalid password');
}
}
$node = $share->getNode();
if($node instanceof Folder) {
$item = $node->get($fileName);
} else {
$item = $node;
}
if ($item instanceof Node) {
list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), $shareToken);
$params = [
'permissions' => $share->getPermissions(),
'title' => $item->getName(),
'fileId' => $item->getId() . '_' . $this->settings->getSystemValue('instanceid'),
'token' => $token,
'urlsrc' => $urlSrc,
'path' => '/',
'instanceId' => $this->settings->getSystemValue('instanceid'),
];
$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
$policy = new ContentSecurityPolicy();
$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
$policy->allowInlineScript(true);
$response->setContentSecurityPolicy($policy);
return $response;
}
} catch (\Exception $e) {
}
return new TemplateResponse('core', '403', [], 'guest');
}
/**
* @NoAdminRequired
*
* @param string $mimetype
* @param string $filename
* @param string $dir
* @return JSONResponse
*/
public function create($mimetype,
$filename,
$dir){
$view = new View('/' . $this->uid . '/files');
if (!$dir){
$dir = '/';
}
$basename = $this->l10n->t('New Document.odt');
switch ($mimetype) {
case 'application/vnd.oasis.opendocument.spreadsheet':
$basename = $this->l10n->t('New Spreadsheet.ods');
break;
case 'application/vnd.oasis.opendocument.presentation':
$basename = $this->l10n->t('New Presentation.odp');
break;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
$basename = $this->l10n->t('New Document.docx');
break;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
$basename = $this->l10n->t('New Spreadsheet.xlsx');
break;
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
$basename = $this->l10n->t('New Presentation.pptx');
break;
default:
// to be safe
$mimetype = 'application/vnd.oasis.opendocument.text';
break;
}
if (!$filename){
$path = Helper::getNewFileName($view, $dir . '/' . $basename);
} else {
$path = $dir . '/' . $filename;
}
$content = '';
if (class_exists('\OC\Files\Type\TemplateManager')){
$manager = \OC_Helper::getFileTemplateManager();
$content = $manager->getTemplate($mimetype);
}
if (!$content){
$content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH);
}
if ($content && $view->file_put_contents($path, $content)) {
$info = $view->getFileInfo($path);
$ret = $this->wopiParser->getUrlSrc($mimetype);
$lolang = strtolower(str_replace('_', '-', $this->settings->getUserValue($this->uid, 'core', 'lang', 'en')));
$response = array(
'status' => 'success',
'fileid' => $info['fileid'] . '_' . $this->settings->getSystemValue('instanceid'),
'urlsrc' => $ret['urlsrc'],
'action' => $ret['action'],
'lolang' => $lolang,
'data' => \OCA\Files\Helper::formatFileInfo($info)
);
} else {
$response = array(
'status' => 'error',
'message' => (string) $this->l10n->t('Can\'t create document')
);
}
return $response;
}
}
|