Passed
Push — 1.11.x ( 31fff3...86e7ae )
by Yannick
15:37 queued 10s
created

FileUtility::GenerateRevisionId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
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
class FileUtility {
23
24
    /**
25
     * Application name
26
     */
27
    public const app_name = "onlyoffice";
28
29
    /**
30
     * Extensions of files that can edit
31
     *
32
     * @var array
33
     */
34
    public static $can_edit_types = [
35
        "docx",
36
        "xlsx",
37
        "pptx",
38
        "ppsx"
39
    ];
40
41
    /**
42
     * Extensions of files that can view
43
     *
44
     * @var array
45
     */
46
    public static $can_view_types = [
47
        "docx", "xlsx", "pptx", "ppsx",
48
        "txt", "csv", "odt", "ods","odp",
49
        "doc", "xls", "ppt", "pps","epub",
50
        "rtf", "mht", "html", "htm","xps","pdf","djvu"
51
    ];
52
53
    /**
54
     * Extensions of text files
55
     *
56
     * @var array
57
     */
58
    public static $text_doc = [
59
        "docx", "txt", "odt", "doc", "rtf", "html",
60
        "htm", "xps", "pdf", "djvu"
61
    ];
62
63
    /**
64
     * Extensions of presentation files
65
     *
66
     * @var array
67
     */
68
    public static $presentation_doc  = [
69
        "pptx", "ppsx", "odp", "ppt", "pps"
70
    ];
71
72
    /**
73
     * Extensions of spreadsheet files
74
     *
75
     * @var array
76
     */
77
    public static $spreadsheet_doc = [
78
        "xlsx", "csv", "ods", "xls"
79
    ];
80
81
    /**
82
     * Return file type by extension
83
     */
84
    public static function getDocType(string $extension): string
85
    {
86
        if (in_array($extension, self::$text_doc)) {
87
            return "text";
88
        }
89
        if (in_array($extension, self::$presentation_doc)) {
90
            return "presentation";
91
        }
92
        if (in_array($extension, self::$spreadsheet_doc)) {
93
            return "spreadsheet";
94
        }
95
96
        return "";
97
    }
98
99
    /**
100
     * Return file extension by file type
101
     */
102
    public static function getDocExt(string $type): string
103
    {
104
        if ($type === "text") {
105
            return "docx";
106
        }
107
        if ($type === "spreadsheet") {
108
            return "xlsx";
109
        }
110
        if ($type === "presentation") {
111
            return "pptx";
112
        }
113
114
        return "";
115
    }
116
117
    /**
118
     * Return file url for download
119
     */
120
    public static function getFileUrl(int $courseId, int $userId, int $docId, int $sessionId = null, int $groupId = null): string
121
    {
122
123
        $data = [
124
            "type" => "download",
125
            "courseId" => $courseId,
126
            "userId" => $userId,
127
            "docId" => $docId,
128
            "sessionId" => $sessionId
129
        ];
130
131
        if (!empty($groupId)) {
132
            $data["groupId"] = $groupId;
133
        }
134
135
        $hashUrl = Crypt::GetHash($data);
136
137
        return api_get_path(WEB_PLUGIN_PATH) . self::app_name . "/" . "callback.php?hash=" . $hashUrl;
138
    }
139
140
    /**
141
     * Return file key
142
     */
143
    public static function getKey(string $courseCode, int $docId): string
144
    {
145
        $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode);
146
        $mtime = filemtime($docInfo["absolute_path"]);
147
148
        $key = $mtime . $courseCode . $docId;
149
        return self::GenerateRevisionId($key);
150
    }
151
152
    /**
153
     * Translation key to a supported form
154
     */
155
    public static function GenerateRevisionId(string $expectedKey): string
156
    {
157
        if (strlen($expectedKey) > 20) $expectedKey = crc32( $expectedKey);
158
        $key = preg_replace("[^0-9-.a-zA-Z_=]", "_", $expectedKey);
159
        $key = substr($key, 0, min(array(strlen($key), 20)));
160
        return $key;
161
    }
162
}
163