SystemHelper::placeholdersConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2024 Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace App\View\Helper;
16
17
use App\Utility\System;
18
use Cake\Core\Configure;
19
use Cake\Utility\Hash;
20
use Cake\View\Helper;
21
22
/**
23
 * System helper
24
 */
25
class SystemHelper extends Helper
26
{
27
    /**
28
     * Default placeholders configuration for media types
29
     *
30
     * @var array
31
     */
32
    protected $defaultPlaceholders = [
33
        'audio' => ['controls' => 'boolean', 'autoplay' => 'boolean'],
34
        'files' => ['download' => 'boolean'],
35
        'images' => ['width' => 'integer', 'height' => 'integer', 'bearing' => 'integer', 'pitch' => 'integer', 'zoom' => 'integer'],
36
        'videos' => ['controls' => 'boolean', 'autoplay' => 'boolean'],
37
    ];
38
39
    /**
40
     * Accepted mime types for upload
41
     *
42
     * @var array
43
     */
44
    protected $defaultUploadAccepted = [
45
        'audio' => [
46
            'audio/*',
47
        ],
48
        'images' => [
49
            'image/*',
50
        ],
51
        'videos' => [
52
            'application/x-mpegURL',
53
            'video/*',
54
        ],
55
    ];
56
57
    /**
58
     * Not accepted mime types and extesions for upload
59
     *
60
     * @var array
61
     */
62
    protected $defaultUploadForbidden = [
63
        'mimetypes' => [
64
            'application/javascript',
65
            'application/x-cgi',
66
            'application/x-perl',
67
            'application/x-php',
68
            'application/x-ruby',
69
            'application/x-shellscript',
70
            'text/javascript',
71
            'text/x-perl',
72
            'text/x-php',
73
            'text/x-python',
74
            'text/x-ruby',
75
            'text/x-shellscript',
76
        ],
77
        'extensions' => [
78
            'cgi',
79
            'exe',
80
            'js',
81
            'perl',
82
            'php',
83
            'py',
84
            'rb',
85
            'sh',
86
        ],
87
    ];
88
89
    /**
90
     * Maximum size for uploaded files, in bytes
91
     *
92
     * @var int
93
     */
94
    protected int $defaultUploadMaxSize = -1;
95
96
    /**
97
     * Timeout for upload operations, in milliseconds
98
     *
99
     * @var int
100
     */
101
    protected int $defaultUploadTimeout = 30000; // in milliseconds
102
103
    /**
104
     * Maximum resolution for images
105
     *
106
     * @var string
107
     */
108
    protected $defaultUploadMaxResolution = '4096x2160'; // 4K
109
110
    /**
111
     * Get the minimum value between post_max_size and upload_max_filesize.
112
     *
113
     * @return int
114
     */
115
    public function getMaxFileSize(): int
116
    {
117
        $forcedMaxFileSize = Configure::read('Upload.uploadMaxSize');
118
        if ($forcedMaxFileSize) {
119
            return $forcedMaxFileSize;
120
        }
121
        $postMaxSize = intVal(substr(ini_get('post_max_size'), 0, -1));
122
        $uploadMaxFilesize = intVal(substr(ini_get('upload_max_filesize'), 0, -1));
123
124
        return min($postMaxSize, $uploadMaxFilesize) * 1024 * 1024;
125
    }
126
127
    /**
128
     * Return false when API version is less than required, true otherwise.
129
     *
130
     * @return bool
131
     */
132
    public function checkBeditaApiVersion(): bool
133
    {
134
        $project = (array)$this->getView()->get('project');
135
        $apiVersion = Hash::get($project, 'version');
136
        if (empty($apiVersion)) {
137
            return true;
138
        }
139
        $requiredApiVersions = (array)Configure::read('BEditaAPI.versions');
140
        foreach ($requiredApiVersions as $requiredApiVersion) {
141
            if (System::compareBEditaApiVersion($apiVersion, $requiredApiVersion)) {
142
                return true;
143
            }
144
        }
145
146
        return false;
147
    }
148
149
    /**
150
     * Check if BEdita API version is greater or equal to required version.
151
     *
152
     * @param string $requiredApiVersion Required API version
153
     * @return bool
154
     */
155
    public function isBEditaApiVersionGte(string $requiredApiVersion): bool
156
    {
157
        $project = (array)$this->getView()->get('project');
158
        $apiVersion = Hash::get($project, 'version');
159
160
        return empty($apiVersion) ? false : version_compare($apiVersion, $requiredApiVersion) >= 0;
161
    }
162
163
    /**
164
     * Placeholders config
165
     *
166
     * @return array
167
     */
168
    public function placeholdersConfig(): array
169
    {
170
        return (array)Configure::read('Placeholders', $this->defaultPlaceholders);
171
    }
172
173
    /**
174
     * Upload config
175
     *
176
     * @return array
177
     */
178
    public function uploadConfig(): array
179
    {
180
        $accepted = (array)Configure::read('uploadAccepted', $this->defaultUploadAccepted);
181
        $forbidden = (array)Configure::read('uploadForbidden', $this->defaultUploadForbidden);
182
        $maxResolution = (string)Configure::read('uploadMaxResolution', $this->defaultUploadMaxResolution);
183
        $maxSize = (int)Configure::read('uploadMaxSize', $this->defaultUploadMaxSize);
184
        $timeout = (int)Configure::read('uploadTimeout', $this->defaultUploadTimeout);
185
186
        return compact('accepted', 'forbidden', 'maxResolution', 'maxSize', 'timeout');
187
    }
188
189
    /**
190
     * Get alert background color
191
     *
192
     * @return string
193
     */
194
    public function alertBgColor(): string
195
    {
196
        if (Configure::read('Recovery')) {
197
            return '#FE2F03';
198
        }
199
        $request = $this->getView()->getRequest();
200
        $prefix = $request->getParam('prefix');
201
        if (Configure::read(sprintf('AlertMessageByArea.%s.color', $prefix))) {
202
            return Configure::read(sprintf('AlertMessageByArea.%s.color', $prefix));
203
        }
204
205
        return Configure::read('AlertMessage.color') ?? '';
206
    }
207
208
    /**
209
     * Get alert message
210
     *
211
     * @return string
212
     */
213
    public function alertMsg(): string
214
    {
215
        if (Configure::read('Recovery')) {
216
            return __('Recovery Mode - Access restricted to admin users');
217
        }
218
        $request = $this->getView()->getRequest();
219
        $prefix = $request->getParam('prefix');
220
        if (Configure::read(sprintf('AlertMessageByArea.%s.text', $prefix))) {
221
            return Configure::read(sprintf('AlertMessageByArea.%s.text', $prefix));
222
        }
223
        $message = Configure::read('AlertMessage.text') ?? '';
224
        $user = $this->getView()->get('user');
225
        if ($user && in_array('admin', $user->get('roles')) && !$this->checkBeditaApiVersion()) {
226
            $message .= ' ' . __('API version required: {0}', Configure::read('BEditaAPI.versions'));
227
        }
228
229
        return $message;
230
    }
231
}
232