Plugin: OnlyOffice: Improve return URL definition

pull/3953/head
Yannick Warnier 4 years ago
parent be8a0c5ca8
commit 24974d5a3d
  1. 141
      plugin/onlyoffice/create.php

@ -1,6 +1,7 @@
<?php <?php
/** /**
* (c) Copyright Ascensio System SIA 2021. *
* (c) Copyright Ascensio System SIA 2021
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +14,9 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*
*/ */
require_once __DIR__.'/../../main/inc/global.inc.php'; require_once __DIR__.'/../../main/inc/global.inc.php';
use ChamiloSession as Session; use ChamiloSession as Session;
@ -21,58 +24,42 @@ use ChamiloSession as Session;
$plugin = OnlyofficePlugin::create(); $plugin = OnlyofficePlugin::create();
$mapFileFormat = [ $mapFileFormat = [
"text" => $plugin->get_lang("document"), "text" => $plugin->get_lang("document"),
"spreadsheet" => $plugin->get_lang("spreadsheet"), "spreadsheet" => $plugin->get_lang("spreadsheet"),
"presentation" => $plugin->get_lang("presentation"), "presentation" => $plugin->get_lang("presentation")
]; ];
$userId = !empty($_GET["userId"])? $_GET['userId'] : 0; $userId = $_GET["userId"];
$sessionId = !empty($_GET["sessionId"])? $_GET["sessionId"] :0; $sessionId = $_GET["sessionId"];
$docId = !empty($_GET["folderId"])? $_GET["folderId"] :0; $docId = $_GET["folderId"];
$courseId = !empty($_GET["courseId"])? $_GET["courseId"] :0; $courseId = $_GET["courseId"];
$groupId = !empty($_GET["groupId"])? $_GET["groupId"] :0;
$folderId = !empty($_GET["folderId"])? $_GET["folderId"] :0;
$courseInfo = api_get_course_info_by_id($courseId); $courseInfo = api_get_course_info_by_id($courseId);
$courseCode = $courseInfo["code"]; $courseCode = $courseInfo["code"];
$isMyDir = false; $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, true, $sessionId);
if (!empty($docId)) {
$docInfo = DocumentManager::get_document_data_by_id(
$docId,
$courseCode,
true,
$sessionId
);
$isMyDir = DocumentManager::is_my_shared_folder(
$userId,
$docInfo["absolute_path"],
$sessionId
);
}
$groupRights = Session::read('group_member_with_upload_rights'); $groupRights = Session::read('group_member_with_upload_rights');
$isAllowToEdit = api_is_allowed_to_edit(true, true); $isAllowToEdit = api_is_allowed_to_edit(true, true);
$isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo["absolute_path"], $sessionId);
if (!($isAllowToEdit || $isMyDir || $groupRights)) { if (!($isAllowToEdit || $isMyDir || $groupRights)) {
api_not_allowed(true); api_not_allowed(true);
} }
$form = new FormValidator( $form = new FormValidator("doc_create",
"doc_create", "post",
"post", api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/create.php");
api_get_path(WEB_PLUGIN_PATH)."onlyoffice/create.php"
);
$form->addText("fileName", $plugin->get_lang("title"), true); $form->addText("fileName", $plugin->get_lang("title"), true);
$form->addSelect("fileFormat", $plugin->get_lang("chooseFileFormat"), $mapFileFormat); $form->addSelect("fileFormat", $plugin->get_lang("chooseFileFormat"), $mapFileFormat);
$form->addButtonCreate($plugin->get_lang("create")); $form->addButtonCreate($plugin->get_lang("create"));
$form->addHidden("groupId", $groupId); $form->addHidden("groupId", (int) $_GET["groupId"]);
$form->addHidden("courseId", $courseId); $form->addHidden("courseId", (int) $_GET["courseId"]);
$form->addHidden("sessionId", $sessionId); $form->addHidden("sessionId", (int) $_GET["sessionId"]);
$form->addHidden("userId", $userId); $form->addHidden("userId", (int) $_GET["userId"]);
$form->addHidden("folderId", $folderId); $form->addHidden("folderId", (int) $_GET["folderId"]);
$form->addHidden("goBackUrl", Security::remove_XSS($_SERVER["HTTP_REFERER"])); $form->addHidden("goBackUrl", $_SERVER["HTTP_REFERER"]);
$goBackUrl = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq();
if ($form->validate()) { if ($form->validate()) {
$values = $form->exportValues(); $values = $form->exportValues();
@ -82,41 +69,36 @@ if ($form->validate()) {
$groupId = $values["groupId"]; $groupId = $values["groupId"];
$sessionId = $values["sessionId"]; $sessionId = $values["sessionId"];
$courseId = $values["courseId"]; $courseId = $values["courseId"];
$goBackUrl = Security::remove_XSS($values["goBackUrl"]); $goBackUrl = $values["goBackUrl"];
$fileType = $values["fileFormat"]; $fileType = $values["fileFormat"];
$fileExt = FileUtility::getDocExt($fileType); $fileExt = FileUtility::getDocExt($fileType);
$fileTitle = Security::remove_XSS($values["fileName"]).".".$fileExt; $fileTitle = $values["fileName"] . "." . $fileExt;
$courseInfo = api_get_course_info_by_id($courseId); $courseInfo = api_get_course_info_by_id($courseId);
$courseCode = $courseInfo["code"]; $courseCode = $courseInfo["code"];
$fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId); $fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId);
$fileName = preg_replace('/\.\./', '', $values["fileName"]).$fileNamePrefix.".".$fileExt; $fileName = $values["fileName"] . $fileNamePrefix . "." . $fileExt;
$groupInfo = GroupManager::get_group_properties($groupId); $groupInfo = GroupManager::get_group_properties($groupId);
$emptyTemplatePath = TemplateManager::getEmptyTemplate($fileExt); $emptyTemplatePath = TemplateManager::getEmptyTemplate($fileExt);
$folderPath = '';
$fileRelatedPath = "/"; $fileRelatedPath = "/";
if (!empty($folderId)) { if (!empty($folderId)) {
$document_data = DocumentManager::get_document_data_by_id( $document_data = DocumentManager::get_document_data_by_id($folderId, $courseCode, true, $sessionId);
$folderId,
$courseCode,
true,
$sessionId
);
$folderPath = $document_data["absolute_path"]; $folderPath = $document_data["absolute_path"];
$fileRelatedPath = $fileRelatedPath.substr($document_data["absolute_path_from_document"], 10)."/".$fileName; $fileRelatedPath = $fileRelatedPath . substr($document_data["absolute_path_from_document"], 10) . "/" . $fileName;
} else { } else {
$folderPath = api_get_path(SYS_COURSE_PATH).api_get_course_path($courseCode)."/document"; $folderPath = api_get_path(SYS_COURSE_PATH) . api_get_course_path($courseCode) . "/document";
if (!empty($groupId)) { if (!empty($groupId)) {
$folderPath = $folderPath."/".$groupInfo["directory"]; $folderPath = $folderPath . "/" . $groupInfo["directory"];
$fileRelatedPath = $groupInfo["directory"]."/"; $fileRelatedPath = $groupInfo["directory"] . "/";
} }
$fileRelatedPath = $fileRelatedPath.$fileName; $fileRelatedPath = $fileRelatedPath . $fileName;
} }
$filePath = $folderPath."/".$fileName; $filePath = $folderPath . "/" . $fileName;
if (file_exists($filePath)) { if (file_exists($filePath)) {
Display::addFlash(Display::return_message($plugin->get_lang("fileIsExist"), "error")); Display::addFlash(Display::return_message($plugin->get_lang("fileIsExist"), "error"));
@ -130,47 +112,40 @@ if ($form->validate()) {
chmod($filePath, api_get_permissions_for_new_files()); chmod($filePath, api_get_permissions_for_new_files());
$documentId = add_document( $documentId = add_document($courseInfo,
$courseInfo, $fileRelatedPath,
$fileRelatedPath, "file",
"file", filesize($filePath),
filesize($filePath), $fileTitle,
$fileTitle, null,
null, false);
false
);
if ($documentId) { if ($documentId) {
api_item_property_update( api_item_property_update($courseInfo,
$courseInfo, TOOL_DOCUMENT,
TOOL_DOCUMENT, $documentId,
$documentId, "DocumentAdded",
"DocumentAdded", $userId,
$userId, $groupInfo,
$groupInfo, null,
null, null,
null, null,
null, $sessionId);
$sessionId
); header("Location: " . $goBackUrl);
header("Location: ".$goBackUrl);
exit(); exit();
} }
} else { } else {
Display::addFlash( Display::addFlash(Display::return_message($plugin->get_lang("impossibleCreateFile"), "error"));
Display::return_message(
$plugin->get_lang("impossibleCreateFile"),
"error"
)
);
} }
} }
display: display:
$goBackUrl = $goBackUrl ?: Security::remove_XSS($_SERVER["HTTP_REFERER"]); $goBackUrl = $goBackUrl ?: $_SERVER["HTTP_REFERER"];
$actionsLeft = '<a href="'.$goBackUrl.'">'.Display::return_icon("back.png", get_lang("Back")." ".get_lang("To")." ".get_lang("DocumentsOverview"), "", ICON_SIZE_MEDIUM)."</a>"; $actionsLeft = '<a href="'. $goBackUrl . '">' . Display::return_icon("back.png", get_lang("Back") . " " . get_lang("To") . " " . get_lang("DocumentsOverview"), "", ICON_SIZE_MEDIUM) . "</a>";
Display::display_header($plugin->get_lang("createNewDocument")); Display::display_header($plugin->get_lang("createNewDocument"));
echo Display::toolbarAction("actions-documents", [$actionsLeft]); echo Display::toolbarAction("actions-documents", [$actionsLeft]);
echo $form->returnForm(); echo $form->returnForm();
Display::display_footer(); Display::display_footer();
?>
Loading…
Cancel
Save