From 2bb01fbfffad9b16c55a251bd715544e3e4d023f Mon Sep 17 00:00:00 2001 From: rivexe Date: Fri, 17 Feb 2023 14:58:13 +0300 Subject: [PATCH] php: building an docEditorView from a templates + formatting by linter --- web/documentserver-example/php/doceditor.php | 465 +----------------- .../php/trackmanager.php | 3 +- .../php/views/DocEditorView.php | 243 +++++++++ .../php/views/IndexStoredListView.php | 2 +- .../php/views/IndexView.php | 1 - 5 files changed, 249 insertions(+), 465 deletions(-) diff --git a/web/documentserver-example/php/doceditor.php b/web/documentserver-example/php/doceditor.php index b1ca2ee4..5d2204d8 100755 --- a/web/documentserver-example/php/doceditor.php +++ b/web/documentserver-example/php/doceditor.php @@ -20,470 +20,11 @@ namespace OnlineEditorsExamplePhp; use OnlineEditorsExamplePhp\Helpers\ConfigManager; use OnlineEditorsExamplePhp\Helpers\ExampleUsers; use OnlineEditorsExamplePhp\Helpers\JwtManager; +use OnlineEditorsExamplePhp\Views\DocEditorView; require_once dirname(__FILE__) . '/functions.php'; require_once dirname(__FILE__) . '/vendor/autoload.php'; -$userList = new ExampleUsers(); -$confgManager = new ConfigManager(); -$jwtManager = new JwtManager(); -$user = $userList->getUser($_GET["user"]); -$isEnableDirectUrl = isset($_GET["directUrl"]) ? filter_var($_GET["directUrl"], FILTER_VALIDATE_BOOLEAN) : false; -// get the file url and upload it -$externalUrl = $_GET["fileUrl"] ?? ""; -if (!empty($externalUrl)) { - $filename = doUpload($externalUrl); -} else { // if the file url doesn't exist, get file name and file extension - $filename = basename($_GET["fileID"]); -} -$createExt = $_GET["fileExt"] ?? ""; - -if (!empty($createExt)) { - // and get demo file name by the extension - $filename = tryGetDefaultByType($createExt, $user); - - // create the demo file url - $new_url = "doceditor.php?fileID=" . $filename . "&user=" . $_GET["user"]; - header('Location: ' . $new_url, true); - exit; -} - -$fileuri = fileUri($filename, true); -$fileuriUser = realpath($confgManager->getConfig("storagePath")) === - $confgManager->getConfig("storagePath") ? - getDownloadUrl($filename) . "&dmode=emb" : fileUri($filename); -$directUrl = getDownloadUrl($filename, false); -$docKey = getDocEditorKey($filename); -$filetype = mb_strtolower(pathinfo($filename, PATHINFO_EXTENSION)); - -$ext = mb_strtolower('.' . pathinfo($filename, PATHINFO_EXTENSION)); -$editorsMode = empty($_GET["action"]) ? "edit" : $_GET["action"]; // get the editors mode -$canEdit = in_array($ext, $confgManager->getConfig("docServEdited")); // check if the file can be edited -if ((!$canEdit && $editorsMode == "edit" - || $editorsMode == "fillForms") - && in_array($ext, $confgManager->getConfig("docServFillforms")) -) { - $editorsMode = "fillForms"; - $canEdit = true; -} - -// check if the Submit form button is displayed or not -$submitForm = $editorsMode == "fillForms" && $user->id == "uid-1" && !1; -$mode = $canEdit && $editorsMode != "view" ? "edit" : "view"; // define if the editing mode is edit or view -$type = empty($_GET["type"]) ? "desktop" : $_GET["type"]; - -$templatesImageUrl = getTemplateImageUrl($filename); // templates image url in the "From Template" section -$createUrl = getCreateUrl($filename, $user->id, $type); -$templates = [ - [ - "image" => "", - "title" => "Blank", - "url" => $createUrl, - ], - [ - "image" => $templatesImageUrl, - "title" => "With sample content", - "url" => $createUrl . "&sample=true", - ], -]; - -// specify the document config -$config = [ - "type" => $type, - "documentType" => getDocumentType($filename), - "document" => [ - "title" => $filename, - "url" => getDownloadUrl($filename), - "directUrl" => $isEnableDirectUrl ? $directUrl : "", - "fileType" => $filetype, - "key" => $docKey, - "info" => [ - "owner" => "Me", - "uploaded" => date('d.m.y'), - "favorite" => $user->favorite, - ], - "permissions" => [ // the permission for the document to be edited and downloaded or not - "comment" => $editorsMode != "view" && $editorsMode - != "fillForms" && $editorsMode != "embedded" && $editorsMode != "blockcontent", - "copy" => !in_array("copy", $user->deniedPermissions), - "download" => !in_array("download", $user->deniedPermissions), - "edit" => $canEdit && ($editorsMode == "edit" || - $editorsMode == "view" || $editorsMode == "filter" || $editorsMode == "blockcontent"), - "print" => !in_array("print", $user->deniedPermissions), - "fillForms" => $editorsMode != "view" && $editorsMode != "comment" - && $editorsMode != "embedded" && $editorsMode != "blockcontent", - "modifyFilter" => $editorsMode != "filter", - "modifyContentControl" => $editorsMode != "blockcontent", - "review" => $canEdit && ($editorsMode == "edit" || $editorsMode == "review"), - "chat" => $user->id != "uid-0", - "reviewGroups" => $user->reviewGroups, - "commentGroups" => $user->commentGroups, - "userInfoGroups" => $user->userInfoGroups, - ], - ], - "editorConfig" => [ - "actionLink" => empty($_GET["actionLink"]) ? null : json_decode($_GET["actionLink"]), - "mode" => $mode, - "lang" => empty($_COOKIE["ulang"]) ? "en" : $_COOKIE["ulang"], - "callbackUrl" => getCallbackUrl($filename), // absolute URL to the document storage service - "coEditing" => $editorsMode == "view" && $user->id == "uid-0" ? [ - "mode" => "strict", - "change" => false, - ] : null, - "createUrl" => $user->id != "uid-0" ? $createUrl : null, - "templates" => $user->templates ? $templates : null, - "user" => [ // the user currently viewing or editing the document - "id" => $user->id != "uid-0" ? $user->id : null, - "name" => $user->name, - "group" => $user->group, - ], - "embedded" => [ // the parameters for the embedded document type - // the absolute URL that will allow the document to be saved onto the user personal computer - "saveUrl" => $directUrl, - // the absolute URL to the document serving as a source file for the document embedded into the web page - "embedUrl" => $directUrl, - // the absolute URL that will allow other users to share this document - "shareUrl" => $directUrl, - "toolbarDocked" => "top", // the place for the embedded viewer toolbar (top or bottom) - ], - "customization" => [ // the parameters for the editor interface - "about" => true, // the About section display - "comments" => true, - "feedback" => true, // the Feedback & Support menu button display - // adds the request for the forced file saving to the callback handler when saving the document - "forcesave" => false, - "submitForm" => $submitForm, // if the Submit form button is displayed or not - "goback" => [ // settings for the Open file location menu button and upper right corner button - // the absolute URL to the website address which will be opened - // when clicking the Open file location menu button - "url" => serverPath(), - ], - ], - ], -]; - -// an image for inserting -$dataInsertImage = $isEnableDirectUrl ? [ - "fileType" => "png", - "url" => serverPath(true) . "/css/images/logo.png", - "directUrl" => serverPath(false) . "/css/images/logo.png", -] : [ - "fileType" => "png", - "url" => serverPath(true) . "/css/images/logo.png", -]; - -// a document for comparing -$dataCompareFile = $isEnableDirectUrl ? [ - "fileType" => "docx", - "url" => serverPath(true) . "/webeditor-ajax.php?type=assets&name=sample.docx", - "directUrl" => serverPath(false) . "/webeditor-ajax.php?type=assets&name=sample.docx", -] : [ - "fileType" => "docx", - "url" => serverPath(true) . "/webeditor-ajax.php?type=assets&name=sample.docx", -]; - -// recipients data for mail merging -$dataMailMergeRecipients = $isEnableDirectUrl ? [ - "fileType" => "csv", - "url" => serverPath(true) . "/webeditor-ajax.php?type=csv", - "directUrl" => serverPath(false) . "/webeditor-ajax.php?type=csv", -] : [ - "fileType" => "csv", - "url" => serverPath(true) . "/webeditor-ajax.php?type=csv", -]; - -// users data for mentions -$usersForMentions = $user->id != "uid-0" ? $userList->getUsersForMentions($user->id) : null; - -// check if the secret key to generate token exists -if ($jwtManager->isJwtEnabled()) { - $config["token"] = $jwtManager->jwtEncode($config); // encode config into the token - // encode the dataInsertImage object into the token - $dataInsertImage["token"] = $jwtManager->jwtEncode($dataInsertImage); - // encode the dataCompareFile object into the token - $dataCompareFile["token"] = $jwtManager->jwtEncode($dataCompareFile); - // encode the dataMailMergeRecipients object into the token - $dataMailMergeRecipients["token"] = $jwtManager->jwtEncode($dataMailMergeRecipients); -} - -?> - - - - - - - - - - ONLYOFFICE - - - - - - - - -
-
-
-
- - +$docEditorView = new DocEditorView($_REQUEST); +$docEditorView->render(); diff --git a/web/documentserver-example/php/trackmanager.php b/web/documentserver-example/php/trackmanager.php index 3715b835..935bf2be 100755 --- a/web/documentserver-example/php/trackmanager.php +++ b/web/documentserver-example/php/trackmanager.php @@ -308,7 +308,8 @@ function commandRequest($method, $key, $meta = null) $jwtHeader = $configManager->getConfig("docServJwtHeader") == "" ? "Authorization" : $configManager->getConfig("docServJwtHeader"); - if ($jwtManager->isJwtEnabled() && $jwtManager->tokenUseForRequest()) { // check if a secret key to generate token exists or not + // 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 } diff --git a/web/documentserver-example/php/views/DocEditorView.php b/web/documentserver-example/php/views/DocEditorView.php index e7e99955..584d2f98 100644 --- a/web/documentserver-example/php/views/DocEditorView.php +++ b/web/documentserver-example/php/views/DocEditorView.php @@ -17,12 +17,255 @@ namespace OnlineEditorsExamplePhp\Views; +use OnlineEditorsExamplePhp\Helpers\ConfigManager; +use OnlineEditorsExamplePhp\Helpers\ExampleUsers; +use OnlineEditorsExamplePhp\Helpers\JwtManager; +use function OnlineEditorsExamplePhp\doUpload; +use function OnlineEditorsExamplePhp\fileUri; +use function OnlineEditorsExamplePhp\getCallbackUrl; +use function OnlineEditorsExamplePhp\getCreateUrl; +use function OnlineEditorsExamplePhp\getDocEditorKey; +use function OnlineEditorsExamplePhp\getDocumentType; +use function OnlineEditorsExamplePhp\getDownloadUrl; +use function OnlineEditorsExamplePhp\getHistory; +use function OnlineEditorsExamplePhp\getStoragePath; +use function OnlineEditorsExamplePhp\getTemplateImageUrl; +use function OnlineEditorsExamplePhp\serverPath; +use function OnlineEditorsExamplePhp\tryGetDefaultByType; + final class DocEditorView extends View { public function __construct($request, $tempName = "docEditor") { parent::__construct($tempName); + $externalUrl = $request["fileUrl"] ?? ""; + $confgManager = new ConfigManager(); + $jwtManager = new JwtManager(); + $userList = new ExampleUsers(); + $user = $userList->getUser($request["user"]); + $isEnableDirectUrl = isset($request["directUrl"]) ? filter_var($request["directUrl"], FILTER_VALIDATE_BOOLEAN) + : false; + if (!empty($externalUrl)) { + $filename = doUpload($externalUrl); + } else { // if the file url doesn't exist, get file name and file extension + $filename = basename($request["fileID"]); + } + $createExt = $request["fileExt"] ?? ""; + + if (!empty($createExt)) { + // and get demo file name by the extension + $filename = tryGetDefaultByType($createExt, $user); + + // create the demo file url + $new_url = "doceditor.php?fileID=" . $filename . "&user=" . $request["user"]; + header('Location: ' . $new_url, true); + exit; + } + + $fileuri = fileUri($filename, true); + $fileuriUser = realpath($confgManager->getConfig("storagePath")) === + $confgManager->getConfig("storagePath") ? + getDownloadUrl($filename) . "&dmode=emb" : fileUri($filename); + $directUrl = getDownloadUrl($filename, false); + $docKey = getDocEditorKey($filename); + $filetype = mb_strtolower(pathinfo($filename, PATHINFO_EXTENSION)); + + $ext = mb_strtolower('.' . pathinfo($filename, PATHINFO_EXTENSION)); + $editorsMode = empty($request["action"]) ? "edit" : $request["action"]; // get the editors mode + $canEdit = in_array($ext, $confgManager->getConfig("docServEdited")); // check if the file can be edited + if ((!$canEdit && $editorsMode == "edit" + || $editorsMode == "fillForms") + && in_array($ext, $confgManager->getConfig("docServFillforms")) + ) { + $editorsMode = "fillForms"; + $canEdit = true; + } + + // check if the Submit form button is displayed or not + $submitForm = $editorsMode == "fillForms" && $user->id == "uid-1" && !1; + $mode = $canEdit && $editorsMode != "view" ? "edit" : "view"; // define if the editing mode is edit or view + $type = empty($request["type"]) ? "desktop" : $request["type"]; + + $templatesImageUrl = getTemplateImageUrl($filename); // templates image url in the "From Template" section + $createUrl = getCreateUrl($filename, $user->id, $type); + $templates = [ + [ + "image" => "", + "title" => "Blank", + "url" => $createUrl, + ], + [ + "image" => $templatesImageUrl, + "title" => "With sample content", + "url" => $createUrl . "&sample=true", + ], + ]; + + // specify the document config + $config = [ + "type" => $type, + "documentType" => getDocumentType($filename), + "document" => [ + "title" => $filename, + "url" => getDownloadUrl($filename), + "directUrl" => $isEnableDirectUrl ? $directUrl : "", + "fileType" => $filetype, + "key" => $docKey, + "info" => [ + "owner" => "Me", + "uploaded" => date('d.m.y'), + "favorite" => $user->favorite, + ], + "permissions" => [ // the permission for the document to be edited and downloaded or not + "comment" => $editorsMode != "view" && $editorsMode + != "fillForms" && $editorsMode != "embedded" && $editorsMode != "blockcontent", + "copy" => !in_array("copy", $user->deniedPermissions), + "download" => !in_array("download", $user->deniedPermissions), + "edit" => $canEdit && ($editorsMode == "edit" || + $editorsMode == "view" || $editorsMode == "filter" || $editorsMode == "blockcontent"), + "print" => !in_array("print", $user->deniedPermissions), + "fillForms" => $editorsMode != "view" && $editorsMode != "comment" + && $editorsMode != "embedded" && $editorsMode != "blockcontent", + "modifyFilter" => $editorsMode != "filter", + "modifyContentControl" => $editorsMode != "blockcontent", + "review" => $canEdit && ($editorsMode == "edit" || $editorsMode == "review"), + "chat" => $user->id != "uid-0", + "reviewGroups" => $user->reviewGroups, + "commentGroups" => $user->commentGroups, + "userInfoGroups" => $user->userInfoGroups, + ], + ], + "editorConfig" => [ + "actionLink" => empty($request["actionLink"]) ? null : json_decode($request["actionLink"]), + "mode" => $mode, + "lang" => empty($_COOKIE["ulang"]) ? "en" : $_COOKIE["ulang"], + "callbackUrl" => getCallbackUrl($filename), // absolute URL to the document storage service + "coEditing" => $editorsMode == "view" && $user->id == "uid-0" ? [ + "mode" => "strict", + "change" => false, + ] : null, + "createUrl" => $user->id != "uid-0" ? $createUrl : null, + "templates" => $user->templates ? $templates : null, + "user" => [ // the user currently viewing or editing the document + "id" => $user->id != "uid-0" ? $user->id : null, + "name" => $user->name, + "group" => $user->group, + ], + "embedded" => [ // the parameters for the embedded document type + // the absolute URL that will allow the document to be saved onto the user personal computer + "saveUrl" => $directUrl, + // the absolute URL to the document serving as a source file for the document embedded into + // the web page + "embedUrl" => $directUrl, + // the absolute URL that will allow other users to share this document + "shareUrl" => $directUrl, + "toolbarDocked" => "top", // the place for the embedded viewer toolbar (top or bottom) + ], + "customization" => [ // the parameters for the editor interface + "about" => true, // the About section display + "comments" => true, + "feedback" => true, // the Feedback & Support menu button display + // adds the request for the forced file saving to the callback handler when saving the document + "forcesave" => false, + "submitForm" => $submitForm, // if the Submit form button is displayed or not + "goback" => [ // settings for the Open file location menu button and upper right corner button + // the absolute URL to the website address which will be opened + // when clicking the Open file location menu button + "url" => serverPath(), + ], + ], + ], + ]; + + // an image for inserting + $dataInsertImage = $isEnableDirectUrl ? [ + "fileType" => "png", + "url" => serverPath(true) . "/css/images/logo.png", + "directUrl" => serverPath(false) . "/css/images/logo.png", + ] : [ + "fileType" => "png", + "url" => serverPath(true) . "/css/images/logo.png", + ]; + + // a document for comparing + $dataCompareFile = $isEnableDirectUrl ? [ + "fileType" => "docx", + "url" => serverPath(true) . "/webeditor-ajax.php?type=assets&name=sample.docx", + "directUrl" => serverPath(false) . "/webeditor-ajax.php?type=assets&name=sample.docx", + ] : [ + "fileType" => "docx", + "url" => serverPath(true) . "/webeditor-ajax.php?type=assets&name=sample.docx", + ]; + + // recipients data for mail merging + $dataMailMergeRecipients = $isEnableDirectUrl ? [ + "fileType" => "csv", + "url" => serverPath(true) . "/webeditor-ajax.php?type=csv", + "directUrl" => serverPath(false) . "/webeditor-ajax.php?type=csv", + ] : [ + "fileType" => "csv", + "url" => serverPath(true) . "/webeditor-ajax.php?type=csv", + ]; + + // users data for mentions + $usersForMentions = $user->id != "uid-0" ? $userList->getUsersForMentions($user->id) : null; + + // check if the secret key to generate token exists + if ($jwtManager->isJwtEnabled()) { + $config["token"] = $jwtManager->jwtEncode($config); // encode config into the token + // encode the dataInsertImage object into the token + $dataInsertImage["token"] = $jwtManager->jwtEncode($dataInsertImage); + // encode the dataCompareFile object into the token + $dataCompareFile["token"] = $jwtManager->jwtEncode($dataCompareFile); + // encode the dataMailMergeRecipients object into the token + $dataMailMergeRecipients["token"] = $jwtManager->jwtEncode($dataMailMergeRecipients); + } + $out = getHistory($filename, $filetype, $docKey, $fileuri, $isEnableDirectUrl); + $history = $out[0]; + $historyData = $out[1]; + $historyLayout = ""; + if ($user->id != "uid-0") { + if ($history != null && $historyData != null) { + $historyLayout .= " config.events['onRequestHistory'] = function () { + // show the document version history + docEditor.refreshHistory(".json_encode($history).");};"; + $historyLayout .= " config.events['onRequestHistoryData'] = function (event) { + var ver = event.data; + var histData = ".json_encode($historyData).";". + "docEditor.setHistoryData(histData[ver - 1]);}; + config.events['onRequestHistoryClose'] = function () { + document.location.reload(); + };"; + } + $historyLayout .= "// add mentions for not anonymous users + config.events['onRequestUsers'] = function () { + docEditor.setUsers({ // set a list of users to mention in the comments + \"users\": {usersForMentions} + }); + }; + // the user is mentioned in a comment + config.events['onRequestSendNotify'] = function (event) { + event.data.actionLink = replaceActionLink(location.href, JSON.stringify(event.data.actionLink)); + var data = JSON.stringify(event.data); + innerAlert(\"onRequestSendNotify: \" + data); + }; + // prevent file renaming for anonymous users + config.events['onRequestRename'] = onRequestRename;"; + } $this->tagsValues = [ + "docType" => getDocumentType($filename), + "apiUrl" => $confgManager->getConfig("docServSiteUrl").$confgManager->getConfig("docServApiUrl"), + "dataInsertImage" => mb_strimwidth( + json_encode($dataInsertImage), + 1, + mb_strlen(json_encode($dataInsertImage)) - 2 + ), + "dataCompareFile" => json_encode($dataCompareFile), + "dataMailMergeRecipients" => json_encode($dataMailMergeRecipients), + "fileNotFoundAlert" => !file_exists(getStoragePath($filename)) ? "alert('File not found'); return;" : "", + "config" => json_encode($config), + "history" => $historyLayout, + "usersForMentions" => json_encode($usersForMentions), ]; } } diff --git a/web/documentserver-example/php/views/IndexStoredListView.php b/web/documentserver-example/php/views/IndexStoredListView.php index abed9f91..cba1ff0e 100644 --- a/web/documentserver-example/php/views/IndexStoredListView.php +++ b/web/documentserver-example/php/views/IndexStoredListView.php @@ -149,4 +149,4 @@ class IndexStoredListView extends View } return $layout; } -} \ No newline at end of file +} diff --git a/web/documentserver-example/php/views/IndexView.php b/web/documentserver-example/php/views/IndexView.php index 02b199f8..b1a987b9 100644 --- a/web/documentserver-example/php/views/IndexView.php +++ b/web/documentserver-example/php/views/IndexView.php @@ -112,5 +112,4 @@ final class IndexView extends View return $configManager->getConfig("mode") != "view" ? '
Edit
' : ""; } - }