Passed
Push — master ( 262b3f...86674d )
by Yannick
09:44
created

OnlyofficeTools::getButtonCreateNew()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
nc 3
nop 0
dl 0
loc 27
rs 9.6333
c 1
b 0
f 0
1
<?php
2
/**
3
 * (c) Copyright Ascensio System SIA 2025.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
class OnlyofficeTools
18
{
19
    /**
20
     * Return button-link to onlyoffice editor for file.
21
     */
22
    public static function getButtonEdit(array $document_data): string
23
    {
24
        $plugin = OnlyofficePlugin::create();
25
26
        $appSettings = new OnlyofficeAppsettings($plugin);
27
        $documentManager = new OnlyofficeDocumentManager($appSettings, []);
28
        $isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin');
29
        if (!$isEnable) {
30
            return '';
31
        }
32
33
        $urlToEdit = api_get_path(WEB_PLUGIN_PATH).'onlyoffice/editor.php';
34
35
        $extension = strtolower(pathinfo($document_data['title'], PATHINFO_EXTENSION));
36
37
        $canEdit = null !== $documentManager->getFormatInfo($extension) ? $documentManager->getFormatInfo($extension)->isEditable() : false;
38
        $canView = null !== $documentManager->getFormatInfo($extension) ? $documentManager->getFormatInfo($extension)->isViewable() : false;
39
40
        $groupId = api_get_group_id();
41
        if (!empty($groupId)) {
42
            $urlToEdit = $urlToEdit.'?groupId='.$groupId.'&';
43
        } else {
44
            $urlToEdit = $urlToEdit.'?';
45
        }
46
47
        $documentId = $document_data['id'];
48
        $urlToEdit = $urlToEdit.'docId='.$documentId;
49
50
        if ($canEdit || $canView) {
51
            $tooltip = $plugin->get_lang('openByOnlyoffice');
52
            if ('pdf' === $extension) {
53
                $tooltip = $plugin->get_lang('fillInFormInOnlyoffice');
54
            }
55
56
            return Display::url(
57
                Display::return_icon(
58
                    '../../plugin/onlyoffice/resources/onlyoffice_edit.png',
59
                    $tooltip
60
                ),
61
                $urlToEdit
62
            );
63
        }
64
65
        return '';
66
    }
67
68
    /**
69
     * Return button-link to onlyoffice editor for view file.
70
     */
71
    public static function getButtonView(array $document_data): string
72
    {
73
        $plugin = OnlyofficePlugin::create();
74
        $appSettings = new OnlyofficeAppsettings($plugin);
75
        $documentManager = new OnlyofficeDocumentManager($appSettings, []);
76
77
        $isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin');
78
        if (!$isEnable) {
79
            return '';
80
        }
81
82
        $urlToEdit = api_get_path(WEB_PLUGIN_PATH).'onlyoffice/editor.php';
83
84
        $sessionId = api_get_session_id();
85
        $courseInfo = api_get_course_info();
86
        $documentId = $document_data['id'];
87
        $userId = api_get_user_id();
88
89
        $docInfo = DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], false, $sessionId);
0 ignored issues
show
Deprecated Code introduced by
The function DocumentManager::get_document_data_by_id() has been deprecated: use $repo->find() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
        $docInfo = /** @scrutinizer ignore-deprecated */ DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], false, $sessionId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
91
        $extension = strtolower(pathinfo($document_data['title'], PATHINFO_EXTENSION));
92
        $canView = null !== $documentManager->getFormatInfo($extension) ? $documentManager->getFormatInfo($extension)->isViewable() : false;
93
94
        $isGroupAccess = false;
95
        $groupId = api_get_group_id();
96
        if (!empty($groupId)) {
97
            $groupProperties = GroupManager::get_group_properties($groupId);
98
            $docInfoGroup = api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
0 ignored issues
show
Bug introduced by
The function api_get_item_property_info was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            $docInfoGroup = /** @scrutinizer ignore-call */ api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
Loading history...
99
            $isGroupAccess = GroupManager::allowUploadEditDocument($userId, $courseInfo['code'], $groupProperties, $docInfoGroup);
100
101
            $urlToEdit = $urlToEdit.'?groupId='.$groupId.'&';
102
        } else {
103
            $urlToEdit = $urlToEdit.'?';
104
        }
105
106
        $isAllowToEdit = api_is_allowed_to_edit(true, true);
107
        $isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo['absolute_parent_path'], $sessionId);
0 ignored issues
show
Bug introduced by
The method is_my_shared_folder() does not exist on DocumentManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        /** @scrutinizer ignore-call */ 
108
        $isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo['absolute_parent_path'], $sessionId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
109
        $accessRights = $isAllowToEdit || $isMyDir || $isGroupAccess;
110
111
        $urlToEdit = $urlToEdit.'docId='.$documentId;
112
113
        if ($canView && !$accessRights) {
114
            return Display::url(Display::return_icon('../../plugin/onlyoffice/resources/onlyoffice_view.png', $plugin->get_lang('openByOnlyoffice')), $urlToEdit, ['style' => 'float:right; margin-right:8px']);
115
        }
116
117
        return '';
118
    }
119
120
    /**
121
     * Return button-link to onlyoffice create new.
122
     */
123
    public static function getButtonCreateNew(): string
124
    {
125
        $plugin = OnlyofficePlugin::create();
126
127
        $isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin');
128
        if (!$isEnable) {
129
            return '';
130
        }
131
132
        $courseId = api_get_course_int_id();
133
        $sessionId = api_get_session_id();
134
        $groupId = api_get_group_id();
135
        $userId = api_get_user_id();
136
137
        $urlToCreate = api_get_path(WEB_PLUGIN_PATH).'onlyoffice/create.php'
138
                                                        .'?folderId='.(empty($_GET['id']) ? '0' : (int) $_GET['id'])
139
                                                        .'&courseId='.$courseId
140
                                                        .'&groupId='.$groupId
141
                                                        .'&sessionId='.$sessionId
142
                                                        .'&userId='.$userId;
143
144
        return Display::url(
145
            Display::return_icon(
146
                '../../plugin/onlyoffice/resources/onlyoffice_create.png',
147
                $plugin->get_lang('createNew')
148
            ),
149
            $urlToCreate
150
        );
151
    }
152
153
    /**
154
     * Return path to OnlyOffice viewer for a given file.
155
     */
156
    public static function getPathToView($fileReference, bool $showHeaders = true, ?int $exeId = null, ?int $questionId = null, bool $isReadOnly = false): string
157
    {
158
        $plugin = OnlyofficePlugin::create();
159
        $appSettings = new OnlyofficeAppsettings($plugin);
160
        $documentManager = new OnlyofficeDocumentManager($appSettings, []);
161
162
        $isEnable = 'true' === $plugin->get('enable_onlyoffice_plugin');
163
        if (!$isEnable) {
164
            return '';
165
        }
166
167
        $urlToEdit = api_get_path(WEB_PLUGIN_PATH).'onlyoffice/editor.php';
168
        $queryString = $_SERVER['QUERY_STRING'];
169
        $isExercise = str_contains($queryString, 'exerciseId=');
170
171
        if (is_numeric($fileReference)) {
172
            $documentId = (int) $fileReference;
173
            $courseInfo = api_get_course_info();
174
            $sessionId = api_get_session_id();
175
            $userId = api_get_user_id();
176
177
            $docInfo = DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], false, $sessionId);
0 ignored issues
show
Deprecated Code introduced by
The function DocumentManager::get_document_data_by_id() has been deprecated: use $repo->find() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

177
            $docInfo = /** @scrutinizer ignore-deprecated */ DocumentManager::get_document_data_by_id($documentId, $courseInfo['code'], false, $sessionId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
178
            if (!$docInfo) {
179
                return '';
180
            }
181
182
            $extension = strtolower(pathinfo($docInfo['path'], PATHINFO_EXTENSION));
183
            $canView = null !== $documentManager->getFormatInfo($extension) ? $documentManager->getFormatInfo($extension)->isViewable() : false;
184
185
            $isGroupAccess = false;
186
            $groupId = api_get_group_id();
187
            if (!empty($groupId)) {
188
                $groupProperties = GroupManager::get_group_properties($groupId);
189
                $docInfoGroup = api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
0 ignored issues
show
Bug introduced by
The function api_get_item_property_info was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
                $docInfoGroup = /** @scrutinizer ignore-call */ api_get_item_property_info(api_get_course_int_id(), 'document', $documentId, $sessionId);
Loading history...
190
                $isGroupAccess = GroupManager::allowUploadEditDocument($userId, $courseInfo['code'], $groupProperties, $docInfoGroup);
191
192
                $urlToEdit .= '?'.api_get_cidreq().'&';
193
            } else {
194
                $urlToEdit .= '?'.api_get_cidreq().'&';
195
            }
196
197
            $isMyDir = DocumentManager::is_my_shared_folder($userId, $docInfo['absolute_parent_path'], $sessionId);
198
            $accessRights = $isMyDir || $isGroupAccess;
199
200
            $urlToEdit .= 'docId='.$documentId;
201
            if (false === $showHeaders) {
202
                $urlToEdit .= '&nh=1';
203
            }
204
205
            if ($canView && !$accessRights) {
206
                return $urlToEdit;
207
            }
208
        } else {
209
            $urlToEdit .= '?'.$queryString.'&doc='.urlencode($fileReference);
210
            if ($isExercise) {
211
                $urlToEdit .= '&type=exercise';
212
                if ($exeId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exeId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
213
                    $urlToEdit .= '&exeId='.$exeId;
214
                }
215
216
                if ($questionId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $questionId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
217
                    $urlToEdit .= '&questionId='.$questionId;
218
                }
219
            }
220
            if (false === $showHeaders) {
221
                $urlToEdit .= '&nh=1';
222
            }
223
224
            if (true === $isReadOnly) {
225
                $urlToEdit .= '&readOnly=1';
226
            }
227
228
            return $urlToEdit;
229
        }
230
231
        return '';
232
    }
233
}
234