Passed
Push — 1.11.x ( 24974d...655124 )
by Angel Fernando Quiroz
20:31
created

plugin/onlyoffice/create.php (1 issue)

1
<?php
2
/**
3
 *
4
 * (c) Copyright Ascensio System SIA 2021
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 */
19
20
require_once __DIR__.'/../../main/inc/global.inc.php';
21
22
use ChamiloSession as Session;
23
24
$plugin = OnlyofficePlugin::create();
25
26
$mapFileFormat = [
27
    "text" => $plugin->get_lang("document"), 
28
    "spreadsheet" => $plugin->get_lang("spreadsheet"),
29
    "presentation" => $plugin->get_lang("presentation")
30
];
31
32
$userId = $_GET["userId"];
33
$sessionId = $_GET["sessionId"];
34
$docId = $_GET["folderId"];
35
$courseId = $_GET["courseId"];
36
37
$courseInfo = api_get_course_info_by_id($courseId);
38
$courseCode = $courseInfo["code"];
39
40
$docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, true, $sessionId);
41
42
$groupRights = Session::read('group_member_with_upload_rights');
43
$isAllowToEdit = api_is_allowed_to_edit(true, true);
44
$isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo["absolute_path"], $sessionId);
45
if (!($isAllowToEdit || $isMyDir || $groupRights)) {
46
    api_not_allowed(true);
47
}
48
49
$form = new FormValidator("doc_create",
50
                          "post",
51
                          api_get_path(WEB_PLUGIN_PATH) . "onlyoffice/create.php");
52
53
$form->addText("fileName", $plugin->get_lang("title"), true);
54
$form->addSelect("fileFormat", $plugin->get_lang("chooseFileFormat"), $mapFileFormat);
55
$form->addButtonCreate($plugin->get_lang("create"));
56
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"]);
63
64
if ($form->validate()) {
65
    $values = $form->exportValues();
66
67
    $folderId = $values["folderId"];
68
    $userId = $values["userId"];
69
    $groupId = $values["groupId"];
70
    $sessionId = $values["sessionId"];
71
    $courseId = $values["courseId"];
72
    $goBackUrl = $values["goBackUrl"];
73
74
    $fileType = $values["fileFormat"];
75
    $fileExt = FileUtility::getDocExt($fileType);
76
    $fileTitle = $values["fileName"] . "." . $fileExt;
77
78
    $courseInfo = api_get_course_info_by_id($courseId);
79
    $courseCode = $courseInfo["code"];
80
    
81
    $fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId);
82
    $fileName = $values["fileName"] . $fileNamePrefix . "." . $fileExt;
83
84
    $groupInfo = GroupManager::get_group_properties($groupId);
85
86
    $emptyTemplatePath = TemplateManager::getEmptyTemplate($fileExt);
87
88
    $fileRelatedPath = "/";
89
    if (!empty($folderId)) {
90
        $document_data = DocumentManager::get_document_data_by_id($folderId, $courseCode, true, $sessionId);
91
        $folderPath = $document_data["absolute_path"];
92
        $fileRelatedPath = $fileRelatedPath . substr($document_data["absolute_path_from_document"], 10) . "/" . $fileName;
93
    } else {
94
        $folderPath = api_get_path(SYS_COURSE_PATH) . api_get_course_path($courseCode) . "/document";
95
        if (!empty($groupId)) {
96
            $folderPath = $folderPath . "/" . $groupInfo["directory"];
97
            $fileRelatedPath = $groupInfo["directory"] . "/";
98
        }
99
        $fileRelatedPath = $fileRelatedPath . $fileName;
100
    }
101
    $filePath = $folderPath . "/" . $fileName;
102
103
    if (file_exists($filePath)) {
104
        Display::addFlash(Display::return_message($plugin->get_lang("fileIsExist"), "error"));
105
        goto display;
106
    }
107
108
    if ($fp = @fopen($filePath, "w")) {
109
        $content = file_get_contents($emptyTemplatePath);
110
        fputs($fp, $content);
111
        fclose($fp);
112
113
        chmod($filePath, api_get_permissions_for_new_files());
114
115
        $documentId = add_document($courseInfo,
116
                                    $fileRelatedPath,
117
                                    "file",
118
                                    filesize($filePath),
119
                                    $fileTitle,
120
                                    null,
121
                                    false);
122
        if ($documentId) {
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);
135
            exit();
136
        }
137
138
    } else {
139
        Display::addFlash(Display::return_message($plugin->get_lang("impossibleCreateFile"), "error"));
140
    }
141
}
142
143
display:
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>";
146
147
    Display::display_header($plugin->get_lang("createNewDocument"));
148
    echo Display::toolbarAction("actions-documents", [$actionsLeft]);
149
    echo $form->returnForm();
150
    Display::display_footer();
151
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...