-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtrackmanager.php
executable file
·339 lines (286 loc) · 11.6 KB
/
trackmanager.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* (c) Copyright Ascensio System SIA 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace OnlineEditorsExamplePhp;
use Exception;
use OnlineEditorsExamplePhp\Configuration\ConfigurationManager;
use OnlineEditorsExamplePhp\Helpers\ConfigManager;
use OnlineEditorsExamplePhp\Helpers\JwtManager;
/**
* Read request body
*
* @return int|array
*/
function readBody()
{
$config_manager = new ConfigurationManager();
$result["error"] = 0;
$configManager = new ConfigManager();
$jwtManager = new JwtManager();
// get the body of the post request and check if it is correct
if (($body_stream = file_get_contents('php://input')) === false) {
$result["error"] = "Bad Request";
return $result;
}
$data = json_decode($body_stream, false);
// check if the response is correct
if ($data === null) {
$result["error"] = "Bad Response";
return $result;
}
sendlog(" InputStream data: " . serialize($data), "webedior-ajax.log");
// check if the document token is enabled
if ($jwtManager->isJwtEnabled() && $jwtManager->tokenUseForRequest()) {
sendlog(" jwt enabled, checking tokens", "webedior-ajax.log");
$inHeader = false;
$data = "";
$jwtHeader = $config_manager->jwt_header();
if (!empty($data["token"])) { // if the document token is in the data
$data = $jwtManager->jwtDecode($data["token"]); // decode it
sendlog(" jwt in body", "webedior-ajax.log");
} elseif (!empty(apache_request_headers()[$jwtHeader])) { // if the Authorization header exists
$data = $jwtManager->jwtDecode(
mb_substr(
apache_request_headers()[$jwtHeader],
mb_strlen("Bearer ")
)
); // decode its part after Authorization prefix
$inHeader = true;
sendlog(" jwt in header", "webedior-ajax.log");
} else { // otherwise, an error occurs
sendlog(" jwt token wasn't found in body or headers", "webedior-ajax.log");
$result["error"] = "Expected JWT";
return $result;
}
if ($data === "") { // invalid signature error
sendlog(" token was found but signature is invalid", "webedior-ajax.log");
$result["error"] = "Invalid JWT signature";
return $result;
}
if ($inHeader) {
$data = $data->payload;
}
}
return $data;
}
/**
* File saving process
*
* @param string $data
* @param string $fileName
* @param string $userAddress
*
* @return array
*/
function processSave($data, $fileName, $userAddress)
{
$downloadUri = $data->url;
if ($downloadUri === null) {
$result["error"] = 1;
return $result;
}
$curExt = mb_strtolower('.' . pathinfo($fileName, PATHINFO_EXTENSION)); // get current file extension
$downloadExt = mb_strtolower('.' . $data->filetype); // get the extension of the downloaded file
$newFileName = $fileName;
// convert downloaded file to the file with the current extension if these extensions aren't equal
if ($downloadExt != $curExt) {
$key = generateRevisionId($downloadUri);
try {
sendlog(" Convert " . $downloadUri . " from " . $downloadExt . " to " . $curExt, "webedior-ajax.log");
// convert file and give url to a new file
$convertedData = getConvertedData($downloadUri, $downloadExt, $curExt, $key, false, $convertedUri);
if (!empty($convertedUri)) {
$downloadUri = $convertedUri;
} else {
sendlog(" Convert after save convertedUri is empty", "webedior-ajax.log");
$baseNameWithoutExt = mb_substr($fileName, 0, mb_strlen($fileName) - mb_strlen($curExt));
// get the correct file name if it already exists
$newFileName = GetCorrectName($baseNameWithoutExt . $downloadExt, $userAddress);
}
} catch (Exception $e) {
sendlog(" Convert after save ".$e->getMessage(), "webedior-ajax.log");
$baseNameWithoutExt = mb_substr($fileName, 0, mb_strlen($fileName) - mb_strlen($curExt));
$newFileName = GetCorrectName($baseNameWithoutExt . $downloadExt, $userAddress);
}
}
$saved = 1;
if (!(($new_data = file_get_contents(
$downloadUri,
false,
stream_context_create(["http" => ["timeout" => 5]])
)) === false)
) {
$storagePath = getStoragePath($newFileName, $userAddress); // get the file path
$histDir = getHistoryDir($storagePath); // get the path to the history direction
$verDir = getVersionDir($histDir, getFileVersion($histDir)); // get the path to the file version
mkdir($verDir); // if the path doesn't exist, create it
// get the path to the previous file version and rename the storage path with it
rename(getStoragePath($fileName, $userAddress), $verDir .
DIRECTORY_SEPARATOR . "prev" . $curExt);
file_put_contents($storagePath, $new_data, LOCK_EX); // save file to the storage directory
if ($changesData = file_get_contents(
$data->changesurl,
false,
stream_context_create(["http" => ["timeout" => 5]])
)
) {
// save file changes to the diff.zip archive
file_put_contents($verDir . DIRECTORY_SEPARATOR .
"diff.zip", $changesData, LOCK_EX);
}
$histData = empty($data->changeshistory) ? null : $data->changeshistory;
if (empty($histData)) {
$histData = json_encode($data->history, JSON_PRETTY_PRINT);
}
if (!empty($histData)) {
// write the history changes to the changes.json file
file_put_contents($verDir .
DIRECTORY_SEPARATOR . "changes.json", $histData, LOCK_EX);
}
// write the key value to the key.txt file
file_put_contents($verDir .
DIRECTORY_SEPARATOR . "key.txt", $data->key, LOCK_EX);
// get the path to the forcesaved file version
$forcesavePath = getForcesavePath($newFileName, $userAddress, false);
if ($forcesavePath != "") { // if the forcesaved file version exists
unlink($forcesavePath); // remove it
}
$saved = 0;
}
$result["error"] = $saved;
return $result;
}
/**
* File force saving process
*
* @param string $data
* @param string $fileName
* @param string $userAddress
*
* @return array
*/
function processForceSave($data, $fileName, $userAddress)
{
$downloadUri = $data->url;
if ($downloadUri === null) {
$result["error"] = 1;
return $result;
}
$curExt = mb_strtolower('.' . pathinfo($fileName, PATHINFO_EXTENSION)); // get current file extension
$downloadExt = mb_strtolower('.' . $data->filetype); // get the extension of the downloaded file
$newFileName = false;
// convert downloaded file to the file with the current extension if these extensions aren't equal
if ($downloadExt != $curExt) {
$key = generateRevisionId($downloadUri);
try {
sendlog(" Convert " . $downloadUri . " from " . $downloadExt . " to " . $curExt, "webedior-ajax.log");
// convert file and give url to a new file
$convertedData = getConvertedData($downloadUri, $downloadExt, $curExt, $key, false, $convertedUri);
if (!empty($convertedUri)) {
$downloadUri = $convertedUri;
} else {
sendlog(" Convert after save convertedUri is empty", "webedior-ajax.log");
$baseNameWithoutExt = mb_substr($fileName, 0, mb_strlen($fileName) - mb_strlen($curExt));
$newFileName = true;
}
} catch (Exception $e) {
sendlog(" Convert after save ".$e->getMessage(), "webedior-ajax.log");
$newFileName = true;
}
}
$saved = 1;
if (!(($new_data = file_get_contents(
$downloadUri,
false,
stream_context_create(["http" => ["timeout" => 5]])
)) === false)
) {
$baseNameWithoutExt = mb_substr($fileName, 0, mb_strlen($fileName) - mb_strlen($curExt));
$isSubmitForm = $data->forcesavetype == 3; // SubmitForm
if ($isSubmitForm) {
if ($newFileName) {
$fileName = GetCorrectName($baseNameWithoutExt .
"-form" . $downloadExt, $userAddress); // get the correct file name if it already exists
} else {
$fileName = GetCorrectName($baseNameWithoutExt . "-form" . $curExt, $userAddress);
}
$forcesavePath = getStoragePath($fileName, $userAddress);
} else {
if ($newFileName) {
$fileName = GetCorrectName($baseNameWithoutExt . $downloadExt, $userAddress);
}
// create forcesave path if it doesn't exist
$forcesavePath = getForcesavePath($fileName, $userAddress, false);
if ($forcesavePath == "") {
$forcesavePath = getForcesavePath($fileName, $userAddress, true);
}
}
file_put_contents($forcesavePath, $new_data, LOCK_EX);
if ($isSubmitForm) {
$uid = $data->actions[0]->userid; // get the user id
createMeta($fileName, $uid, "Filling Form", $userAddress); // create meta data for the forcesaved file
}
$saved = 0;
}
$result["error"] = $saved;
return $result;
}
/**
* Create a command request
*
* @param string $method
* @param string $key
* @param string $meta
*
* @return false|string
*/
function commandRequest($method, $key, $meta = null)
{
$config_manager = new ConfigurationManager();
$jwtManager = new JwtManager();
$documentCommandUrl = $config_manager->document_server_command_url()->string();
$arr = [
"c" => $method,
"key" => $key,
];
if ($meta) {
$arr["meta"] = $meta;
}
$headerToken = "";
$jwtHeader = $config_manager->jwt_header();
// check if a secret key to generate token exists or not
if ($jwtManager->isJwtEnabled() && $jwtManager->tokenUseForRequest()) {
$headerToken = $jwtManager->jwtEncode(["payload" => $arr]); // encode a payload object into a header token
$arr["token"] = $jwtManager->jwtEncode($arr); // encode a payload object into a body token
}
$data = json_encode($arr);
$opts = ['http' => [
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
// add a header Authorization with a header token and Authorization prefix in it
(empty($headerToken) ? "" : $jwtHeader.
": Bearer $headerToken\r\n"),
'content' => $data,
]];
if (mb_substr($documentCommandUrl, 0, mb_strlen("https")) === "https") {
if ($config_manager->ssl_verify_peer_mode_enabled()) {
$opts['ssl'] = ['verify_peer' => false, 'verify_peer_name' => false];
}
}
$context = stream_context_create($opts);
$response_data = file_get_contents($documentCommandUrl, false, $context);
return $response_data;
}