|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* (c) Copyright Ascensio System SIA 2021 |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
require_once __DIR__ . "/../../../main/inc/global.inc.php"; |
|
21
|
|
|
|
|
22
|
|
|
class FileUtility { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Application name |
|
26
|
|
|
*/ |
|
27
|
|
|
public const app_name = "onlyoffice"; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Extensions of files that can edit |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
public static $can_edit_types = [ |
|
35
|
|
|
"docx", |
|
36
|
|
|
"docxf", |
|
37
|
|
|
"oform", |
|
38
|
|
|
"xlsx", |
|
39
|
|
|
"pptx", |
|
40
|
|
|
"ppsx" |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Extensions of files that can view |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static $can_view_types = [ |
|
49
|
|
|
"docx", "docxf", "oform", "xlsx", "pptx", "ppsx", |
|
50
|
|
|
"txt", "csv", "odt", "ods","odp", |
|
51
|
|
|
"doc", "xls", "ppt", "pps","epub", |
|
52
|
|
|
"rtf", "mht", "html", "htm","xps","pdf","djvu" |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Extensions of text files |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
public static $text_doc = [ |
|
61
|
|
|
"docx", "docxf", "oform", "txt", "odt", "doc", "rtf", "html", |
|
62
|
|
|
"htm", "xps", "pdf", "djvu" |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Extensions of presentation files |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
public static $presentation_doc = [ |
|
71
|
|
|
"pptx", "ppsx", "odp", "ppt", "pps" |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Extensions of spreadsheet files |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
public static $spreadsheet_doc = [ |
|
80
|
|
|
"xlsx", "csv", "ods", "xls" |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return file type by extension |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function getDocType(string $extension): string |
|
87
|
|
|
{ |
|
88
|
|
|
if (in_array($extension, self::$text_doc)) { |
|
89
|
|
|
return "text"; |
|
90
|
|
|
} |
|
91
|
|
|
if (in_array($extension, self::$presentation_doc)) { |
|
92
|
|
|
return "presentation"; |
|
93
|
|
|
} |
|
94
|
|
|
if (in_array($extension, self::$spreadsheet_doc)) { |
|
95
|
|
|
return "spreadsheet"; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return ""; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return file extension by file type |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function getDocExt(string $type): string |
|
105
|
|
|
{ |
|
106
|
|
|
if ($type === "text") { |
|
107
|
|
|
return "docx"; |
|
108
|
|
|
} |
|
109
|
|
|
if ($type === "spreadsheet") { |
|
110
|
|
|
return "xlsx"; |
|
111
|
|
|
} |
|
112
|
|
|
if ($type === "presentation") { |
|
113
|
|
|
return "pptx"; |
|
114
|
|
|
} |
|
115
|
|
|
if ($type === "formTemplate") { |
|
116
|
|
|
return "docxf"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return ""; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Return file url for download |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function getFileUrl(int $courseId, int $userId, int $docId, int $sessionId = null, int $groupId = null): string |
|
126
|
|
|
{ |
|
127
|
|
|
|
|
128
|
|
|
$data = [ |
|
129
|
|
|
"type" => "download", |
|
130
|
|
|
"courseId" => $courseId, |
|
131
|
|
|
"userId" => $userId, |
|
132
|
|
|
"docId" => $docId, |
|
133
|
|
|
"sessionId" => $sessionId |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
if (!empty($groupId)) { |
|
137
|
|
|
$data["groupId"] = $groupId; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$hashUrl = Crypt::GetHash($data); |
|
141
|
|
|
|
|
142
|
|
|
return api_get_path(WEB_PLUGIN_PATH) . self::app_name . "/" . "callback.php?hash=" . $hashUrl; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Return location file in chamilo documents |
|
147
|
|
|
*/ |
|
148
|
|
|
function getUrlToLocation($courseCode, $sessionId, $groupId, $folderId) { |
|
|
|
|
|
|
149
|
|
|
return api_get_path(WEB_CODE_PATH)."document/document.php" |
|
150
|
|
|
. "?cidReq=" . Security::remove_XSS($courseCode) |
|
151
|
|
|
. "&id_session=" . Security::remove_XSS($sessionId) |
|
152
|
|
|
. "&gidReq=" . Security::remove_XSS($groupId) |
|
153
|
|
|
. "&id=" . Security::remove_XSS($folderId); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Return file key |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function getKey(string $courseCode, int $docId): string |
|
160
|
|
|
{ |
|
161
|
|
|
$docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode); |
|
162
|
|
|
$mtime = filemtime($docInfo["absolute_path"]); |
|
163
|
|
|
|
|
164
|
|
|
$key = $mtime . $courseCode . $docId; |
|
165
|
|
|
return self::GenerateRevisionId($key); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Translation key to a supported form |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function GenerateRevisionId(string $expectedKey): string |
|
172
|
|
|
{ |
|
173
|
|
|
if (strlen($expectedKey) > 20) $expectedKey = crc32( $expectedKey); |
|
174
|
|
|
$key = preg_replace("[^0-9-.a-zA-Z_=]", "_", $expectedKey); |
|
175
|
|
|
$key = substr($key, 0, min(array(strlen($key), 20))); |
|
176
|
|
|
return $key; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Create new file |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function createFile( |
|
183
|
|
|
string $basename, |
|
184
|
|
|
string $fileExt, |
|
185
|
|
|
int $folderId, |
|
186
|
|
|
int $userId, |
|
187
|
|
|
int $sessionId, |
|
188
|
|
|
int $courseId, |
|
189
|
|
|
int $groupId, |
|
190
|
|
|
string $templatePath = ""): array |
|
191
|
|
|
{ |
|
192
|
|
|
$courseInfo = api_get_course_info_by_id($courseId); |
|
193
|
|
|
$courseCode = $courseInfo["code"]; |
|
194
|
|
|
$groupInfo = GroupManager::get_group_properties($groupId); |
|
195
|
|
|
|
|
196
|
|
|
$fileTitle = Security::remove_XSS($basename). "." .$fileExt; |
|
197
|
|
|
|
|
198
|
|
|
$fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId); |
|
199
|
|
|
$fileName = preg_replace('/\.\./', '', $basename) . $fileNamePrefix . "." . $fileExt; |
|
200
|
|
|
|
|
201
|
|
|
if (empty($templatePath)) { |
|
202
|
|
|
$templatePath = TemplateManager::getEmptyTemplate($fileExt); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$folderPath = ''; |
|
206
|
|
|
$fileRelatedPath = "/"; |
|
207
|
|
|
if (!empty($folderId)) { |
|
208
|
|
|
$document_data = DocumentManager::get_document_data_by_id( |
|
209
|
|
|
$folderId, |
|
210
|
|
|
$courseCode, |
|
211
|
|
|
true, |
|
212
|
|
|
$sessionId |
|
213
|
|
|
); |
|
214
|
|
|
$folderPath = $document_data["absolute_path"]; |
|
215
|
|
|
$fileRelatedPath = $fileRelatedPath . substr($document_data["absolute_path_from_document"], 10) . "/" . $fileName; |
|
216
|
|
|
} else { |
|
217
|
|
|
$folderPath = api_get_path(SYS_COURSE_PATH) . api_get_course_path($courseCode) . "/document"; |
|
218
|
|
|
if (!empty($groupId)) { |
|
219
|
|
|
$folderPath = $folderPath . "/" . $groupInfo["directory"]; |
|
220
|
|
|
$fileRelatedPath = $groupInfo["directory"] . "/"; |
|
221
|
|
|
} |
|
222
|
|
|
$fileRelatedPath = $fileRelatedPath . $fileName; |
|
223
|
|
|
} |
|
224
|
|
|
$filePath = $folderPath . "/" . $fileName; |
|
225
|
|
|
|
|
226
|
|
|
if (file_exists($filePath)) { |
|
227
|
|
|
return ["error" => "fileIsExist"]; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if ($fp = @fopen($filePath, "w")) { |
|
231
|
|
|
$content = file_get_contents($templatePath); |
|
232
|
|
|
fputs($fp, $content); |
|
233
|
|
|
fclose($fp); |
|
234
|
|
|
|
|
235
|
|
|
chmod($filePath, api_get_permissions_for_new_files()); |
|
236
|
|
|
|
|
237
|
|
|
$documentId = add_document( |
|
238
|
|
|
$courseInfo, |
|
239
|
|
|
$fileRelatedPath, |
|
240
|
|
|
"file", |
|
241
|
|
|
filesize($filePath), |
|
242
|
|
|
$fileTitle, |
|
243
|
|
|
null, |
|
244
|
|
|
false |
|
245
|
|
|
); |
|
246
|
|
|
if ($documentId) { |
|
247
|
|
|
api_item_property_update( |
|
248
|
|
|
$courseInfo, |
|
249
|
|
|
TOOL_DOCUMENT, |
|
250
|
|
|
$documentId, |
|
251
|
|
|
"DocumentAdded", |
|
252
|
|
|
$userId, |
|
253
|
|
|
$groupInfo, |
|
254
|
|
|
null, |
|
255
|
|
|
null, |
|
256
|
|
|
null, |
|
257
|
|
|
$sessionId |
|
258
|
|
|
); |
|
259
|
|
|
} else { |
|
260
|
|
|
return ["error" => "impossibleCreateFile"]; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return ["documentId" => $documentId]; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.