forked from ONLYOFFICE/onlyoffice-humhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
240 lines (199 loc) · 6.67 KB
/
Module.php
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\onlydocuments;
use Yii;
use yii\helpers\Url;
use yii\helpers\Json;
use humhub\modules\file\libs\FileHelper;
use humhub\libs\CURLHelper;
use \Firebase\JWT\JWT;
/**
* File Module
*
* @since 0.5
*/
class Module extends \humhub\components\Module
{
public $resourcesPath = 'resources';
/**
* Open modes
*/
const OPEN_MODE_VIEW = 'view';
const OPEN_MODE_EDIT = 'edit';
/**
* Only document types
*/
const DOCUMENT_TYPE_TEXT = 'text';
const DOCUMENT_TYPE_PRESENTATION = 'presentation';
const DOCUMENT_TYPE_SPREADSHEET = 'spreadsheet';
/**
* @var string[] allowed spreadsheet extensions
*/
public $spreadsheetExtensions = ['xls', 'xlsx', 'ods', 'csv'];
/**
* @var string[] allowed presentation extensions
*/
public $presentationExtensions = ['ppsx', 'pps', 'ppt', 'pptx', 'odp'];
/**
* @var string[] allowed text extensions
*/
public $textExtensions = ['docx', 'doc', 'odt', 'rtf', 'txt', 'html', 'htm', 'mht', 'pdf', 'djvu', 'fb2', 'epub', 'xps'];
/**
* @var string[] allowed for editing extensions
*/
public $editableExtensions = ['xlsx', 'ppsx', 'pptx', 'docx' ];
public $convertableExtensions = ['doc','odt','xls','ods','ppt','odp','txt','csv'];
public $convertsTo = [
'doc' => 'docx',
'odt' => 'docx',
'txt' => 'docx',
'xls' => 'xlsx',
'ods' => 'xlsx',
'csv' => 'xlsx',
'ppt' => 'pptx',
'odp' => 'pptx',
];
public function isJwtEnabled() {
return !empty($this->getJwtSecret());
}
public function getJwtSecret() {
return $this->settings->get('jwtSecret');
}
public function getServerUrl()
{
return $this->settings->get('serverUrl');
}
/**
*
* @return type
*/
public function getServerApiUrl()
{
return $this->getServerUrl() . '/web-apps/apps/api/documents/api.js';
}
public function getDocumentType($file)
{
$fileExtension = FileHelper::getExtension($file);
if (in_array($fileExtension, $this->spreadsheetExtensions)) {
return self::DOCUMENT_TYPE_SPREADSHEET;
} elseif (in_array($fileExtension, $this->presentationExtensions)) {
return self::DOCUMENT_TYPE_PRESENTATION;
} elseif (in_array($fileExtension, $this->textExtensions)) {
return self::DOCUMENT_TYPE_TEXT;
}
return null;
}
public function canEdit($file)
{
$fileExtension = FileHelper::getExtension($file);
return in_array($fileExtension, $this->editableExtensions);
}
public function canConvert($file)
{
$fileExtension = FileHelper::getExtension($file);
return in_array($fileExtension, $this->convertableExtensions);
}
public function canView($file)
{
return !empty($this->getDocumentType($file));
}
/**
* @inheritdoc
*/
public function getConfigUrl()
{
return Url::to([
'/onlydocuments/admin'
]);
}
/**
* Generate unique document key
*
* @return string
*/
public function generateDocumentKey($file)
{
if (!empty($file->onlydocuments_key)) {
return $file->onlydocuments_key;
}
$key = substr(strtolower(md5(Yii::$app->security->generateRandomString(20))), 0, 20);
$file->updateAttributes(['onlydocuments_key' => $key]);
return $key;
}
public function commandService($data)
{
$url = $this->getServerUrl() . '/coauthoring/CommandService.ashx';
try {
$http = new \Zend\Http\Client($url, [
'adapter' => '\Zend\Http\Client\Adapter\Curl',
'curloptions' => CURLHelper::getOptions(),
'timeout' => 10
]);
$http->setMethod('POST');
$headers = $http->getRequest()->getHeaders();
if ($this->isJwtEnabled()) {
$data['token'] = JWT::encode($data, $this->getJwtSecret());
$headers->addHeaderLine('Authorization', 'Bearer ' . JWT::encode(['payload' => $data], $this->getJwtSecret()));
}
$http->setRawBody(Json::encode($data));
$headers->addHeaderLine('Accept', 'application/json');
$response = $http->send();
$json = $response->getBody();
} catch (\Exception $ex) {
Yii::error('Could not get document server response! ' . $ex->getMessage());
return [];
}
try {
return Json::decode($json);
} catch (\yii\base\InvalidParamException $ex) {
Yii::error('Could not get document server response! ' . $ex->getMessage());
return [];
}
}
public function convertService($file, $ts)
{
$url = $this->getServerUrl() . '/ConvertService.ashx';
$key = $this->generateDocumentKey($file);
$ext = FileHelper::getExtension($file);
$data = [
'async' => true,
'embeddedfonts' => true,
'filetype' => $ext,
'outputtype' => $this->convertsTo[$ext],
'key' => $key . $ts,
'url' => Url::to(['/onlydocuments/backend/download', 'key' => $key], true),
];
try {
$http = new \Zend\Http\Client($url, [
'adapter' => '\Zend\Http\Client\Adapter\Curl',
'curloptions' => CURLHelper::getOptions(),
'timeout' => 10
]);
$http->setMethod('POST');
$headers = $http->getRequest()->getHeaders();
if ($this->isJwtEnabled()) {
$data['token'] = JWT::encode($data, $this->getJwtSecret());
$headers->addHeaderLine('Authorization', 'Bearer ' . JWT::encode(['payload' => $data], $this->getJwtSecret()));
}
$http->setRawBody(Json::encode($data));
$headers->addHeaderLine('Accept', 'application/json');
$response = $http->send();
$json = $response->getBody();
} catch (\Exception $ex) {
$error = 'Could not get document server response! ' . $ex->getMessage();
Yii::error($error);
return [ 'error' => $error ];
}
try {
return Json::decode($json);
} catch (\yii\base\InvalidParamException $ex) {
$error = 'Could not get document server response! ' . $ex->getMessage();
Yii::error($error);
return [ 'error' => $error ];
}
}
}