Skip to content

Commit 24974d5

Browse files
committed
Plugin: OnlyOffice: Improve return URL definition
1 parent be8a0c5 commit 24974d5

File tree

1 file changed

+58
-83
lines changed

1 file changed

+58
-83
lines changed

plugin/onlyoffice/create.php

Lines changed: 58 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
3-
* (c) Copyright Ascensio System SIA 2021.
3+
*
4+
* (c) Copyright Ascensio System SIA 2021
45
*
56
* Licensed under the Apache License, Version 2.0 (the "License");
67
* you may not use this file except in compliance with the License.
@@ -13,66 +14,52 @@
1314
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1415
* See the License for the specific language governing permissions and
1516
* limitations under the License.
17+
*
1618
*/
19+
1720
require_once __DIR__.'/../../main/inc/global.inc.php';
1821

1922
use ChamiloSession as Session;
2023

2124
$plugin = OnlyofficePlugin::create();
2225

2326
$mapFileFormat = [
24-
"text" => $plugin->get_lang("document"),
27+
"text" => $plugin->get_lang("document"),
2528
"spreadsheet" => $plugin->get_lang("spreadsheet"),
26-
"presentation" => $plugin->get_lang("presentation"),
29+
"presentation" => $plugin->get_lang("presentation")
2730
];
2831

29-
$userId = !empty($_GET["userId"])? $_GET['userId'] : 0;
30-
$sessionId = !empty($_GET["sessionId"])? $_GET["sessionId"] :0;
31-
$docId = !empty($_GET["folderId"])? $_GET["folderId"] :0;
32-
$courseId = !empty($_GET["courseId"])? $_GET["courseId"] :0;
33-
$groupId = !empty($_GET["groupId"])? $_GET["groupId"] :0;
34-
$folderId = !empty($_GET["folderId"])? $_GET["folderId"] :0;
32+
$userId = $_GET["userId"];
33+
$sessionId = $_GET["sessionId"];
34+
$docId = $_GET["folderId"];
35+
$courseId = $_GET["courseId"];
3536

3637
$courseInfo = api_get_course_info_by_id($courseId);
3738
$courseCode = $courseInfo["code"];
3839

39-
$isMyDir = false;
40-
if (!empty($docId)) {
41-
$docInfo = DocumentManager::get_document_data_by_id(
42-
$docId,
43-
$courseCode,
44-
true,
45-
$sessionId
46-
);
47-
$isMyDir = DocumentManager::is_my_shared_folder(
48-
$userId,
49-
$docInfo["absolute_path"],
50-
$sessionId
51-
);
52-
}
40+
$docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, true, $sessionId);
41+
5342
$groupRights = Session::read('group_member_with_upload_rights');
5443
$isAllowToEdit = api_is_allowed_to_edit(true, true);
44+
$isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo["absolute_path"], $sessionId);
5545
if (!($isAllowToEdit || $isMyDir || $groupRights)) {
5646
api_not_allowed(true);
5747
}
5848

59-
$form = new FormValidator(
60-
"doc_create",
61-
"post",
62-
api_get_path(WEB_PLUGIN_PATH)."onlyoffice/create.php"
63-
);
49+
$form = new FormValidator("doc_create",
50+
"post",
51+
api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/create.php");
6452

6553
$form->addText("fileName", $plugin->get_lang("title"), true);
6654
$form->addSelect("fileFormat", $plugin->get_lang("chooseFileFormat"), $mapFileFormat);
6755
$form->addButtonCreate($plugin->get_lang("create"));
6856

69-
$form->addHidden("groupId", $groupId);
70-
$form->addHidden("courseId", $courseId);
71-
$form->addHidden("sessionId", $sessionId);
72-
$form->addHidden("userId", $userId);
73-
$form->addHidden("folderId", $folderId);
74-
$form->addHidden("goBackUrl", Security::remove_XSS($_SERVER["HTTP_REFERER"]));
75-
$goBackUrl = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq();
57+
$form->addHidden("groupId", (int) $_GET["groupId"]);
58+
$form->addHidden("courseId", (int) $_GET["courseId"]);
59+
$form->addHidden("sessionId", (int) $_GET["sessionId"]);
60+
$form->addHidden("userId", (int) $_GET["userId"]);
61+
$form->addHidden("folderId", (int) $_GET["folderId"]);
62+
$form->addHidden("goBackUrl", $_SERVER["HTTP_REFERER"]);
7663

7764
if ($form->validate()) {
7865
$values = $form->exportValues();
@@ -82,41 +69,36 @@
8269
$groupId = $values["groupId"];
8370
$sessionId = $values["sessionId"];
8471
$courseId = $values["courseId"];
85-
$goBackUrl = Security::remove_XSS($values["goBackUrl"]);
72+
$goBackUrl = $values["goBackUrl"];
8673

8774
$fileType = $values["fileFormat"];
8875
$fileExt = FileUtility::getDocExt($fileType);
89-
$fileTitle = Security::remove_XSS($values["fileName"]).".".$fileExt;
76+
$fileTitle = $values["fileName"] . "." . $fileExt;
9077

9178
$courseInfo = api_get_course_info_by_id($courseId);
9279
$courseCode = $courseInfo["code"];
93-
80+
9481
$fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId);
95-
$fileName = preg_replace('/\.\./', '', $values["fileName"]).$fileNamePrefix.".".$fileExt;
82+
$fileName = $values["fileName"] . $fileNamePrefix . "." . $fileExt;
83+
9684
$groupInfo = GroupManager::get_group_properties($groupId);
9785

9886
$emptyTemplatePath = TemplateManager::getEmptyTemplate($fileExt);
9987

100-
$folderPath = '';
10188
$fileRelatedPath = "/";
10289
if (!empty($folderId)) {
103-
$document_data = DocumentManager::get_document_data_by_id(
104-
$folderId,
105-
$courseCode,
106-
true,
107-
$sessionId
108-
);
90+
$document_data = DocumentManager::get_document_data_by_id($folderId, $courseCode, true, $sessionId);
10991
$folderPath = $document_data["absolute_path"];
110-
$fileRelatedPath = $fileRelatedPath.substr($document_data["absolute_path_from_document"], 10)."/".$fileName;
92+
$fileRelatedPath = $fileRelatedPath . substr($document_data["absolute_path_from_document"], 10) . "/" . $fileName;
11193
} else {
112-
$folderPath = api_get_path(SYS_COURSE_PATH).api_get_course_path($courseCode)."/document";
94+
$folderPath = api_get_path(SYS_COURSE_PATH) . api_get_course_path($courseCode) . "/document";
11395
if (!empty($groupId)) {
114-
$folderPath = $folderPath."/".$groupInfo["directory"];
115-
$fileRelatedPath = $groupInfo["directory"]."/";
96+
$folderPath = $folderPath . "/" . $groupInfo["directory"];
97+
$fileRelatedPath = $groupInfo["directory"] . "/";
11698
}
117-
$fileRelatedPath = $fileRelatedPath.$fileName;
99+
$fileRelatedPath = $fileRelatedPath . $fileName;
118100
}
119-
$filePath = $folderPath."/".$fileName;
101+
$filePath = $folderPath . "/" . $fileName;
120102

121103
if (file_exists($filePath)) {
122104
Display::addFlash(Display::return_message($plugin->get_lang("fileIsExist"), "error"));
@@ -130,47 +112,40 @@
130112

131113
chmod($filePath, api_get_permissions_for_new_files());
132114

133-
$documentId = add_document(
134-
$courseInfo,
135-
$fileRelatedPath,
136-
"file",
137-
filesize($filePath),
138-
$fileTitle,
139-
null,
140-
false
141-
);
115+
$documentId = add_document($courseInfo,
116+
$fileRelatedPath,
117+
"file",
118+
filesize($filePath),
119+
$fileTitle,
120+
null,
121+
false);
142122
if ($documentId) {
143-
api_item_property_update(
144-
$courseInfo,
145-
TOOL_DOCUMENT,
146-
$documentId,
147-
"DocumentAdded",
148-
$userId,
149-
$groupInfo,
150-
null,
151-
null,
152-
null,
153-
$sessionId
154-
);
155-
156-
header("Location: ".$goBackUrl);
123+
api_item_property_update($courseInfo,
124+
TOOL_DOCUMENT,
125+
$documentId,
126+
"DocumentAdded",
127+
$userId,
128+
$groupInfo,
129+
null,
130+
null,
131+
null,
132+
$sessionId);
133+
134+
header("Location: " . $goBackUrl);
157135
exit();
158136
}
137+
159138
} else {
160-
Display::addFlash(
161-
Display::return_message(
162-
$plugin->get_lang("impossibleCreateFile"),
163-
"error"
164-
)
165-
);
139+
Display::addFlash(Display::return_message($plugin->get_lang("impossibleCreateFile"), "error"));
166140
}
167141
}
168142

169143
display:
170-
$goBackUrl = $goBackUrl ?: Security::remove_XSS($_SERVER["HTTP_REFERER"]);
171-
$actionsLeft = '<a href="'.$goBackUrl.'">'.Display::return_icon("back.png", get_lang("Back")." ".get_lang("To")." ".get_lang("DocumentsOverview"), "", ICON_SIZE_MEDIUM)."</a>";
144+
$goBackUrl = $goBackUrl ?: $_SERVER["HTTP_REFERER"];
145+
$actionsLeft = '<a href="'. $goBackUrl . '">' . Display::return_icon("back.png", get_lang("Back") . " " . get_lang("To") . " " . get_lang("DocumentsOverview"), "", ICON_SIZE_MEDIUM) . "</a>";
172146

173147
Display::display_header($plugin->get_lang("createNewDocument"));
174148
echo Display::toolbarAction("actions-documents", [$actionsLeft]);
175149
echo $form->returnForm();
176150
Display::display_footer();
151+
?>

0 commit comments

Comments
 (0)