diff --git a/.github/workflows/artifact.yml b/.github/workflows/artifact.yml index 2167b2d..fa8bef6 100644 --- a/.github/workflows/artifact.yml +++ b/.github/workflows/artifact.yml @@ -20,10 +20,18 @@ jobs: mkdir -p ./deploy/onlyoffice rsync -av --exclude='deploy' ./ ./deploy/onlyoffice cd ./deploy/onlyoffice + composer install rm -rf ./.github rm -rf ./.git/ rm ./.gitignore rm ./.gitmodules + rm -rf ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-templates + mv ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-formats/onlyoffice-docs-formats.json ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-formats/onlyoffice-docs-formats.txt + rm ./composer.json + rm ./composer.lock + find ./vendor -type f -iname '*.json' -delete + find ./vendor -type f -iname '*.lock' -delete + find ./vendor -type f -iname 'LICENSE' -delete cd ./assets rm .git cd $cwd diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 91c40d2..bb63280 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,21 +2,16 @@ name: Lint on: workflow_dispatch: + push: + branches: [master, develop] pull_request: - branches: [master] + branches: [master, develop] jobs: - lint: + php-cs-fixer: + name: PHP-CS-Fixer runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.1' - tools: cs2pr, phpcs - - - name: Run phpcs - run: phpcs -q --extensions=php,module,inc,install,test,profile,theme,css,info --ignore=node_modules,bower_components,vendor,md,yml,txt ./ \ No newline at end of file + - uses: actions/checkout@v3 + - name: PHP-CS-Fixer + uses: docker://oskarstark/php-cs-fixer-ga \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5f7054b..719aee1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,10 +24,18 @@ jobs: mkdir -p ./onlyoffice rsync -av --exclude='onlyoffice' ./ ./onlyoffice cd ./onlyoffice + composer install rm -rf .github/ rm -rf .git/ rm .gitignore rm .gitmodules + rm -rf ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-templates + mv ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-formats/onlyoffice-docs-formats.json ./vendor/onlyoffice/docs-integration-sdk/resources/assets/document-formats/onlyoffice-docs-formats.txt + rm ./composer.json + rm ./composer.lock + find ./vendor -type f -iname '*.json' -delete + find ./vendor -type f -iname '*.lock' -delete + find ./vendor -type f -iname 'LICENSE' -delete cd ./assets rm .git cd $cwd diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..247927d --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,37 @@ + true, + 'array_syntax' => [ + 'syntax' => 'short', + ], + 'blank_line_after_opening_tag' => false, + 'no_extra_blank_lines' => true, + 'multiline_comment_opening_closing' => true, + 'yoda_style' => false, + 'phpdoc_to_comment' => false, + 'phpdoc_no_package' => false, + 'phpdoc_annotation_without_dot' => false, + 'increment_style' => ['style' => 'post'], + 'no_useless_else' => false, + 'single_quote' => false, + 'no_useless_return' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'phpdoc_order' => true, + 'no_break_comment' => true, +]; + +$finder = PhpCsFixer\Finder::create() + ->exclude('3rdparty') + ->exclude('assets') + ->exclude('layout') + ->exclude('resources') + ->in(__DIR__) +; + +$config = new PhpCsFixer\Config(); +return $config->setRules( + $rules + ) + ->setFinder($finder); \ No newline at end of file diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..4404a4e --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,3 @@ + "Not permitted"]); + echo json_encode(['error' => 'Not permitted']); + return; } $fileExt = strtolower(pathinfo($title, PATHINFO_EXTENSION)); $baseName = strtolower(pathinfo($title, PATHINFO_FILENAME)); -$result = FileUtility::createFile( +$result = OnlyofficeDocumentManager::createFile( $baseName, $fileExt, $folderId, @@ -71,16 +70,17 @@ $url ); -if (isset($result["error"])) { - if ($result["error"] === "fileIsExist") { - $result["error"] = "File is exist"; +if (isset($result['error'])) { + if ('fileIsExist' === $result['error']) { + $result['error'] = 'File is exist'; } - if ($result["error"] === "impossibleCreateFile") { - $result["error"] = "Impossible to create file"; + if ('impossibleCreateFile' === $result['error']) { + $result['error'] = 'Impossible to create file'; } echo json_encode($result); + return; } -echo json_encode(["success" => "File is created"]); \ No newline at end of file +echo json_encode(['success' => 'File is created']); diff --git a/assets b/assets index 6fb121c..32dfc06 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6fb121c134f7e152dd614d1c19386ba2cb578f2f +Subproject commit 32dfc0655d195bd977d58a64b510818951584183 diff --git a/callback.php b/callback.php index 66b3ce8..ce57f4c 100644 --- a/callback.php +++ b/callback.php @@ -1,7 +1,6 @@ readHash($_GET['hash'], api_get_security_key()); + if (null === $hashData) { + $callbackResponseArray['status'] = 'error'; + $callbackResponseArray['error'] = $error; + exit(json_encode($callbackResponseArray)); } $type = $hashData->type; @@ -56,44 +46,44 @@ $sessionId = $hashData->sessionId; $courseInfo = api_get_course_info_by_id($courseId); - $courseCode = $courseInfo["code"]; + $courseCode = $courseInfo['code']; if (!empty($userId)) { $userInfo = api_get_user_info($userId); } else { - $result["error"] = "User not found"; - die (json_encode($result)); + $result['error'] = 'User not found'; + exit(json_encode($result)); } if (api_is_anonymous()) { $loggedUser = [ - "user_id" => $userInfo["id"], - "status" => $userInfo["status"], - "uidReset" => true, + 'user_id' => $userInfo['id'], + 'status' => $userInfo['status'], + 'uidReset' => true, ]; - Session::write("_user", $loggedUser); - Login::init_user($loggedUser["user_id"], true); + Session::write('_user', $loggedUser); + Login::init_user($loggedUser['user_id'], true); } else { $userId = api_get_user_id(); } - switch($type) { - case "track": + switch ($type) { + case 'track': $callbackResponseArray = track(); - die (json_encode($callbackResponseArray)); - case "download": + exit(json_encode($callbackResponseArray)); + case 'download': $callbackResponseArray = download(); - die (json_encode($callbackResponseArray)); + exit(json_encode($callbackResponseArray)); default: - $callbackResponseArray["status"] = "error"; - $callbackResponseArray["error"] = "404 Method not found"; - die(json_encode($callbackResponseArray)); + $callbackResponseArray['status'] = 'error'; + $callbackResponseArray['error'] = '404 Method not found'; + exit(json_encode($callbackResponseArray)); } } /** - * Handle request from the document server with the document status information + * Handle request from the document server with the document status information. */ function track(): array { @@ -106,117 +96,73 @@ function track(): array global $groupId; global $sessionId; global $courseInfo; + global $appSettings; + global $jwtManager; + + if (($body_stream = file_get_contents('php://input')) === false) { + $result['error'] = 'Bad Request'; - if (($body_stream = file_get_contents("php://input")) === false) { - $result["error"] = "Bad Request"; return $result; } $data = json_decode($body_stream, true); - if ($data === null) { - $result["error"] = "Bad Response"; + if (null === $data) { + $result['error'] = 'Bad Response'; + return $result; } - if (!empty($plugin->getDocumentServerSecret())) { - - if (!empty($data["token"])) { + if ($jwtManager->isJwtEnabled()) { + if (!empty($data['token'])) { try { - $payload = JWT::decode($data["token"], new Key($plugin->getDocumentServerSecret(), "HS256")); - } catch (\UnexpectedValueException $e) { - $result["status"] = "error"; - $result["error"] = "403 Access denied"; + $payload = $jwtManager->decode($data['token'], $appSettings->getJwtKey()); + } catch (UnexpectedValueException $e) { + $result['status'] = 'error'; + $result['error'] = '403 Access denied'; + return $result; } } else { - $token = substr(getallheaders()[$plugin->getJwtHeader()], strlen("Bearer ")); + $token = substr(getallheaders()[$appSettings->getJwtHeader()], strlen('Bearer ')); try { - $decodeToken = JWT::decode($token, new Key($plugin->getDocumentServerSecret(), "HS256")); + $decodeToken = $jwtManager->decode($token, $appSettings->getJwtKey()); $payload = $decodeToken->payload; - } catch (\UnexpectedValueException $e) { - $result["status"] = "error"; - $result["error"] = "403 Access denied"; + } catch (UnexpectedValueException $e) { + $result['status'] = 'error'; + $result['error'] = '403 Access denied'; + return $result; } } - $data["url"] = isset($payload->url) ? $payload->url : null; - $data["status"] = $payload->status; + $data['url'] = isset($payload->url) ? $payload->url : null; + $data['status'] = $payload->status; } - $status = $data["status"]; + $docStatus = new CallbackDocStatus($data['status']); + $callback = new OnlyofficeCallback(); + $callback->setStatus($docStatus); + $callback->setKey($docId); + $callback->setUrl($data['url']); + $callbackService = new OnlyofficeCallbackService( + $appSettings, + $jwtManager, + [ + 'courseCode' => $courseCode, + 'userId' => $userId, + 'docId' => $docId, + 'groupId' => $groupId, + 'sessionId' => $sessionId, + 'courseInfo' => $courseInfo, + ]); + $result = $callbackService->processCallback($callback, $docId); - $track_result = 1; - switch ($status) { - case TrackerStatus_MustSave: - case TrackerStatus_Corrupted: - - $downloadUri = $data["url"]; - $downloadUri = $plugin->replaceDocumentServerUrlToInternal($downloadUri); - - if (!empty($docId) && !empty($courseCode)) { - $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, false, $sessionId); - - if ($docInfo === false) { - $result["error"] = "File not found"; - return $result; - } - - $filePath = $docInfo["absolute_path"]; - } else { - $result["error"] = "Bad Request"; - return $result; - } - - list ($isAllowToEdit, $isMyDir, $isGroupAccess, $isReadonly) = getPermissions($docInfo, $userId, $courseCode, $groupId, $sessionId); - - if ($isReadonly) { - break; - } - - if (($new_data = file_get_contents($downloadUri)) === false) { - break; - } - - if ($isAllowToEdit || $isMyDir || $isGroupAccess) { - $groupInfo = GroupManager::get_group_properties($groupId); - - if ($fp = @fopen($filePath, "w")) { - fputs($fp, $new_data); - fclose($fp); - api_item_property_update($courseInfo, - TOOL_DOCUMENT, - $docId, - "DocumentUpdated", - $userId, - $groupInfo, - null, - null, - null, - $sessionId); - update_existing_document($courseInfo, - $docId, - filesize($filePath), - false); - $track_result = 0; - break; - } - } - - case TrackerStatus_Editing: - case TrackerStatus_Closed: - - $track_result = 0; - break; - } - - $result["error"] = $track_result; return $result; } /** - * Downloading file by the document service + * Downloading file by the document service. */ function download() { @@ -227,15 +173,17 @@ function download() global $groupId; global $sessionId; global $courseInfo; + global $appSettings; + global $jwtManager; - if (!empty($plugin->getDocumentServerSecret())) { - $token = substr(getallheaders()[$plugin->getJwtHeader()], strlen("Bearer ")); + if ($jwtManager->isJwtEnabled()) { + $token = substr(getallheaders()[$appSettings->getJwtHeader()], strlen('Bearer ')); try { - $payload = JWT::decode($token, new Key($plugin->getDocumentServerSecret(), "HS256")); + $payload = $jwtManager->decode($token, $appSettings->getJwtKey()); + } catch (UnexpectedValueException $e) { + $result['status'] = 'error'; + $result['error'] = '403 Access denied'; - } catch (\UnexpectedValueException $e) { - $result["status"] = "error"; - $result["error"] = "403 Access denied"; return $result; } } @@ -243,42 +191,22 @@ function download() if (!empty($docId) && !empty($courseCode)) { $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, false, $sessionId); - if ($docInfo === false) { - $result["error"] = "File not found"; + if (false === $docInfo) { + $result['error'] = 'File not found'; + return $result; } - $filePath = $docInfo["absolute_path"]; + $filePath = $docInfo['absolute_path']; } else { - $result["error"] = "File not found"; + $result['error'] = 'File not found'; + return $result; } - @header("Content-Type: application/octet-stream"); - @header("Content-Disposition: attachment; filename=" . $docInfo["title"]); + @header('Content-Type: application/octet-stream'); + @header('Content-Disposition: attachment; filename='.$docInfo['title']); readfile($filePath); - exit(); -} - -/** - * Method checks access rights to document and returns permissions - */ -function getPermissions(array $docInfo, int $userId, string $courseCode, int $groupId = null, int $sessionId = null): array -{ - $isAllowToEdit = api_is_allowed_to_edit(true, true); - $isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo["absolute_parent_path"], $sessionId); - - $isGroupAccess = false; - if (!empty($groupId)) { - $courseInfo = api_get_course_info($courseCode); - Session::write("_real_cid", $courseInfo["real_id"]); - $groupProperties = GroupManager::get_group_properties($groupId); - $docInfoGroup = api_get_item_property_info($courseInfo["real_id"], "document", $docInfo["id"], $sessionId); - $isGroupAccess = GroupManager::allowUploadEditDocument($userId, $courseCode, $groupProperties, $docInfoGroup); - } - - $isReadonly = $docInfo["readonly"]; - - return [$isAllowToEdit, $isMyDir, $isGroupAccess, $isReadonly]; + exit; } diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4232858 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "require": { + "onlyoffice/docs-integration-sdk": "^1.0.0" + }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/ONLYOFFICE/docs-integration-sdk-php.git", + "branch": "master" + } + ] +} diff --git a/create.php b/create.php index eaed39d..73ffdbc 100644 --- a/create.php +++ b/create.php @@ -1,7 +1,6 @@ $plugin->get_lang("document"), - "spreadsheet" => $plugin->get_lang("spreadsheet"), - "presentation" => $plugin->get_lang("presentation"), - "formTemplate" => $plugin->get_lang("formTemplate") + 'text' => $plugin->get_lang('document'), + 'spreadsheet' => $plugin->get_lang('spreadsheet'), + 'presentation' => $plugin->get_lang('presentation'), + 'formTemplate' => $plugin->get_lang('formTemplate'), ]; -$userId = !empty($_GET["userId"]) ? $_GET['userId'] : 0; -$sessionId = !empty($_GET["sessionId"]) ? $_GET["sessionId"] : 0; -$courseId = !empty($_GET["courseId"]) ? $_GET["courseId"] : 0; -$groupId = !empty($_GET["groupId"]) ? $_GET["groupId"] : 0; -$folderId = !empty($_GET["folderId"]) ? $_GET["folderId"] : 0; +$userId = !empty($_GET['userId']) ? $_GET['userId'] : 0; +$sessionId = !empty($_GET['sessionId']) ? $_GET['sessionId'] : 0; +$courseId = !empty($_GET['courseId']) ? $_GET['courseId'] : 0; +$groupId = !empty($_GET['groupId']) ? $_GET['groupId'] : 0; +$folderId = !empty($_GET['folderId']) ? $_GET['folderId'] : 0; $courseInfo = api_get_course_info_by_id($courseId); -$courseCode = $courseInfo["code"]; +$courseCode = $courseInfo['code']; $isMyDir = false; if (!empty($folderId)) { @@ -49,7 +49,7 @@ ); $isMyDir = DocumentManager::is_my_shared_folder( $userId, - $folderInfo["absolute_path"], + $folderInfo['absolute_path'], $sessionId ); } @@ -60,27 +60,28 @@ } $form = new FormValidator( - "doc_create", - "post", - api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/create.php?userId=" . Security::remove_XSS($userId) - . "&groupId=" . Security::remove_XSS($groupId) - . "&courseId=" . Security::remove_XSS($courseId) - . "&sessionId=" . Security::remove_XSS($sessionId) - . "&folderId=" . Security::remove_XSS($folderId) + 'doc_create', + 'post', + api_get_path(WEB_PLUGIN_PATH).'onlyoffice/create.php?userId='.Security::remove_XSS($userId) + .'&groupId='.Security::remove_XSS($groupId) + .'&courseId='.Security::remove_XSS($courseId) + .'&sessionId='.Security::remove_XSS($sessionId) + .'&folderId='.Security::remove_XSS($folderId) ); -$form->addText("fileName", $plugin->get_lang("title"), true); -$form->addSelect("fileFormat", $plugin->get_lang("chooseFileFormat"), $mapFileFormat); -$form->addButtonCreate($plugin->get_lang("create")); +$form->addText('fileName', $plugin->get_lang('title'), true); +$form->addSelect('fileFormat', $plugin->get_lang('chooseFileFormat'), $mapFileFormat); +$form->addButtonCreate($plugin->get_lang('create')); if ($form->validate()) { $values = $form->exportValues(); - $fileType = $values["fileFormat"]; - $fileExt = FileUtility::getDocExt($fileType); + $fileType = $values['fileFormat']; + var_dump($fileType); + $fileExt = $documentManager->getDocExtByType($fileType); - $result = FileUtility::createFile( - $values["fileName"], + $result = OnlyofficeDocumentManager::createFile( + $values['fileName'], $fileExt, $folderId, $userId, @@ -89,23 +90,23 @@ $groupId ); - if (isset($result["error"])) { + if (isset($result['error'])) { Display::addFlash( Display::return_message( - $plugin->get_lang($result["error"]), - "error" + $plugin->get_lang($result['error']), + 'error' ) ); } else { - header("Location: " . FileUtility::getUrlToLocation($courseCode, $sessionId, $groupId, $folderId)); - exit(); + header('Location: '.OnlyofficeDocumentManager::getUrlToLocation($courseCode, $sessionId, $groupId, $folderId)); + exit; } } - $goBackUrl = FileUtility::getUrlToLocation($courseCode, $sessionId, $groupId, $folderId); - $actionsLeft = '' . Display::return_icon("back.png", get_lang("Back") . " " . get_lang("To") . " " . get_lang("DocumentsOverview"), "", ICON_SIZE_MEDIUM) . ""; +$goBackUrl = OnlyofficeDocumentManager::getUrlToLocation($courseCode, $sessionId, $groupId, $folderId); +$actionsLeft = ''.Display::return_icon('back.png', get_lang('Back').' '.get_lang('To').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).''; - Display::display_header($plugin->get_lang("createNewDocument")); - echo Display::toolbarAction("actions-documents", [$actionsLeft]); - echo $form->returnForm(); - Display::display_footer(); +Display::display_header($plugin->get_lang('createNewDocument')); +echo Display::toolbarAction('actions-documents', [$actionsLeft]); +echo $form->returnForm(); +Display::display_footer(); diff --git a/editor.php b/editor.php index 9b54872..115b2b7 100644 --- a/editor.php +++ b/editor.php @@ -1,7 +1,6 @@ get("enable_onlyoffice_plugin") === 'true'; +$isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin'); if (!$isEnable) { - die ("Document server isn't enabled"); + exit("Document server isn't enabled"); + return; } -$documentServerUrl = $plugin->getDocumentServerUrl(); +$appSettings = new OnlyofficeAppsettings($plugin); +$documentServerUrl = $appSettings->getDocumentServerUrl(); if (empty($documentServerUrl)) { - die ("Document server isn't configured"); + exit("Document server isn't configured"); + return; } $config = []; - -$docApiUrl = $documentServerUrl . "/web-apps/apps/api/documents/api.js"; - -$docId = $_GET["docId"]; -$groupId = isset($_GET["groupId"]) && !empty($_GET["groupId"]) ? $_GET["groupId"] : null; - +$docApiUrl = $appSettings->getDocumentServerApiUrl(); +$docId = $_GET['docId']; +$groupId = isset($_GET['groupId']) && !empty($_GET['groupId']) ? $_GET['groupId'] : null; $userId = api_get_user_id(); - $userInfo = api_get_user_info($userId); - $sessionId = api_get_session_id(); $courseId = api_get_course_int_id(); $courseInfo = api_get_course_info(); if (empty($courseInfo)) { api_not_allowed(true); } -$courseCode = $courseInfo["code"]; - +$courseCode = $courseInfo['code']; $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, false, $sessionId); - -$extension = strtolower(pathinfo($docInfo["title"], PATHINFO_EXTENSION)); - $langInfo = LangManager::getLangUser(); - -$docType = FileUtility::getDocType($extension); -$key = FileUtility::getKey($courseCode, $docId); -$fileUrl = FileUtility::getFileUrl($courseId, $userId, $docId, $sessionId, $groupId); - -if (!empty($plugin->getStorageUrl())) { - $fileUrl = str_replace(api_get_path(WEB_PATH), $plugin->getStorageUrl(), $fileUrl); -} - -$config = [ - "type" => "desktop", - "documentType" => $docType, - "document" => [ - "fileType" => $extension, - "key" => $key, - "title" => $docInfo["title"], - "url" => $fileUrl - ], - "editorConfig" => [ - "lang" => $langInfo["isocode"], - "region" => $langInfo["isocode"], - "user" => [ - "id" => strval($userId), - "name" => $userInfo["username"] - ], - "customization" => [ - "goback" => [ - "blank" => false, - "requestClose" => false, - "text" => get_lang("Back"), - "url" => FileUtility::getUrlToLocation($courseCode, $sessionId, $groupId, $docInfo["parent_id"]) - ], - "compactHeader" => true, - "toolbarNoTabs" => true - ] - ] -]; - -$userAgent = $_SERVER["HTTP_USER_AGENT"]; - -$isMobileAgent = preg_match(USER_AGENT_MOBILE, $userAgent); -if ($isMobileAgent) { - $config["type"] = "mobile"; -} - -$isAllowToEdit = api_is_allowed_to_edit(true, true); -$isMyDir = DocumentManager::is_my_shared_folder( - $userId, - $docInfo["absolute_parent_path"], - $sessionId -); - -$isGroupAccess = false; -if (!empty($groupId)) { - $groupProperties = GroupManager::get_group_properties($groupId); - $docInfoGroup = api_get_item_property_info( - api_get_course_int_id(), - "document", - $docId, - $sessionId - ); - $isGroupAccess = GroupManager::allowUploadEditDocument( - $userId, - $courseCode, - $groupProperties, - $docInfoGroup - ); - - $isMemberGroup = GroupManager::is_user_in_group($userId, $groupProperties); - - if (!$isGroupAccess) { - if (!$groupProperties["status"]) { - api_not_allowed(true); - } - if (!$isMemberGroup && $groupProperties["doc_state"] != 1) { - api_not_allowed(true); - } - } +$jwtManager = new OnlyofficeJwtManager($appSettings); +if (isset($_GET['forceEdit']) && (bool)$_GET['forceEdit'] === true) { + $docInfo['forceEdit'] = $_GET['forceEdit']; } - -$accessRights = $isAllowToEdit || $isMyDir || $isGroupAccess; -$canEdit = in_array($extension, FileUtility::$can_edit_types); - -$isVisible = DocumentManager::check_visibility_tree($docId, $courseInfo, $sessionId, $userId, $groupId); -$isReadonly = $docInfo["readonly"]; - -if (!$isVisible) { - api_not_allowed(true); +$documentManager = new OnlyofficeDocumentManager($appSettings, $docInfo); +$extension = $documentManager->getExt($documentManager->getDocInfo('title')); +$docType = $documentManager->getDocType($extension); +$key = $documentManager->getDocumentKey($docId, $courseCode); +$fileUrl = $documentManager->getFileUrl($docId); + +if (!empty($appSettings->getStorageUrl())) { + $fileUrl = str_replace(api_get_path(WEB_PATH), $appSettings->getStorageUrl(), $fileUrl); } -if ($canEdit && $accessRights && !$isReadonly) { - $config["editorConfig"]["mode"] = "edit"; - - $callback = getCallbackUrl( - $docId, - $userId, - $courseId, - $sessionId, - $groupId - ); - - if (!empty($plugin->getStorageUrl())) { - $callback = str_replace(api_get_path(WEB_PATH), $plugin->getStorageUrl(), $callback); - } - $config["editorConfig"]["callbackUrl"] = $callback; -} else { - $canView = in_array($extension, FileUtility::$can_view_types); - if ($canView) { - $config["editorConfig"]["mode"] = "view"; - } else { - api_not_allowed(true); - } -} -$config["document"]["permissions"]["edit"] = $accessRights && !$isReadonly; - -if (!empty($plugin->getDocumentServerSecret())) { - $token = JWT::encode($config, $plugin->getDocumentServerSecret(), "HS256"); - $config["token"] = $token; -} - -/** - * Return callback url - */ -function getCallbackUrl(int $docId, int $userId, int $courseId, int $sessionId, int $groupId = null): string -{ - $url = ""; - - $data = [ - "type" => "track", - "courseId" => $courseId, - "userId" => $userId, - "docId" => $docId, - "sessionId" => $sessionId - ]; - - if (!empty($groupId)) { - $data["groupId"] = $groupId; - } - - $hashUrl = Crypt::GetHash($data); - - return $url . api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/callback.php?hash=" . $hashUrl; -} +$configService = new OnlyofficeConfigService($appSettings, $jwtManager, $documentManager); +$editorsMode = $configService->getEditorsMode(); +$config = $configService->createConfig($docId, $editorsMode, $_SERVER['HTTP_USER_AGENT']); +$config = json_decode(json_encode($config), true); +$isMobileAgent = $configService->isMobileAgent($_SERVER['HTTP_USER_AGENT']); ?> ONLYOFFICE @@ -220,22 +84,22 @@ function getCallbackUrl(int $docId, int $userId, int $courseId, int $sessionId, display: none; } - +