Passed
Push — master ( 9d67d8...4af955 )
by Paolo
02:01 queued 14s
created

SystemHelper::placeholdersConfig()   A

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
3
declare(strict_types=1);
4
5
namespace App\View\Helper;
6
7
use Cake\Core\Configure;
8
use Cake\Utility\Hash;
9
use Cake\View\Helper;
10
11
/**
12
 * System helper
13
 */
14
class SystemHelper extends Helper
15
{
16
    /**
17
     * Default placeholders configuration for media types
18
     *
19
     * @var array
20
     */
21
    protected $defaultPlaceholders = [
22
        'audio' => ['controls' => 'boolean', 'autoplay' => 'boolean'],
23
        'files' => ['download' => 'boolean'],
24
        'images' => ['width' => 'integer', 'height' => 'integer', 'bearing' => 'integer', 'pitch' => 'integer', 'zoom' => 'integer'],
25
        'videos' => ['controls' => 'boolean', 'autoplay' => 'boolean'],
26
    ];
27
28
    /**
29
     * Accepted mime types for upload
30
     *
31
     * @var array
32
     */
33
    protected $defaultUploadAccepted = [
34
        'audio' => [
35
            'audio/*',
36
        ],
37
        'images' => [
38
            'image/*',
39
        ],
40
        'videos' => [
41
            'application/x-mpegURL',
42
            'video/*',
43
        ],
44
        'media' => [
45
            'application/x-mpegURL',
46
            'audio/*',
47
            'image/*',
48
            'video/*',
49
        ],
50
    ];
51
52
    /**
53
     * Not accepted mime types and extesions for upload
54
     *
55
     * @var array
56
     */
57
    protected $defaultUploadForbidden = [
58
        'mimetypes' => [
59
            'application/javascript',
60
            'application/x-cgi',
61
            'application/x-perl',
62
            'application/x-php',
63
            'application/x-ruby',
64
            'application/x-shellscript',
65
            'text/javascript',
66
            'text/x-perl',
67
            'text/x-php',
68
            'text/x-python',
69
            'text/x-ruby',
70
            'text/x-shellscript',
71
        ],
72
        'extensions' => [
73
            'cgi',
74
            'exe',
75
            'js',
76
            'perl',
77
            'php',
78
            'py',
79
            'rb',
80
            'sh',
81
        ],
82
    ];
83
84
    /**
85
     * Maximum resolution for images
86
     *
87
     * @var string
88
     */
89
    protected $defaultUploadMaxResolution = '4096x2160'; // 4K
90
91
    /**
92
     * Get the minimum value between post_max_size and upload_max_filesize.
93
     *
94
     * @return int
95
     */
96
    public function getMaxFileSize(): int
97
    {
98
        $postMaxSize = intVal(substr(ini_get('post_max_size'), 0, -1));
99
        $uploadMaxFilesize = intVal(substr(ini_get('upload_max_filesize'), 0, -1));
100
101
        return min($postMaxSize, $uploadMaxFilesize) * 1024 * 1024;
102
    }
103
104
    /**
105
     * Return false when API version is less than required, true otherwise.
106
     *
107
     * @return bool
108
     */
109
    public function checkBeditaApiVersion(): bool
110
    {
111
        $project = (array)$this->getView()->get('project');
112
        $apiVersion = Hash::get($project, 'version');
113
        if (empty($apiVersion)) {
114
            return true;
115
        }
116
        $requiredApiVersions = (array)Configure::read('BEditaAPI.versions');
117
        foreach ($requiredApiVersions as $requiredApiVersion) {
118
            $apiMajor = substr($apiVersion, 0, strpos($apiVersion, '.'));
119
            $requiredApiMajor = substr($requiredApiVersion, 0, strpos($requiredApiVersion, '.'));
120
            if ($apiMajor === $requiredApiMajor && version_compare($apiVersion, $requiredApiVersion) >= 0) {
121
                return true;
122
            }
123
        }
124
125
        return false;
126
    }
127
128
    /**
129
     * Placeholders config
130
     *
131
     * @return array
132
     */
133
    public function placeholdersConfig(): array
134
    {
135
        return (array)Configure::read('Placeholders', $this->defaultPlaceholders);
136
    }
137
138
    /**
139
     * Upload config
140
     *
141
     * @return array
142
     */
143
    public function uploadConfig(): array
144
    {
145
        $accepted = (array)Configure::read('uploadAccepted', $this->defaultUploadAccepted);
146
        $forbidden = (array)Configure::read('uploadForbidden', $this->defaultUploadForbidden);
147
        $maxResolution = (string)Configure::read('uploadMaxResolution', $this->defaultUploadMaxResolution);
148
149
        return compact('accepted', 'forbidden', 'maxResolution');
150
    }
151
152
    /**
153
     * Get alert background color
154
     *
155
     * @return string
156
     */
157
    public function alertBgColor(): string
158
    {
159
        if (Configure::read('Recovery')) {
160
            return '#FE2F03';
161
        }
162
        $request = $this->getView()->getRequest();
163
        $prefix = $request->getParam('prefix');
164
        if (Configure::read(sprintf('AlertMessageByArea.%s.color', $prefix))) {
165
            return Configure::read(sprintf('AlertMessageByArea.%s.color', $prefix));
166
        }
167
168
        return Configure::read('AlertMessage.color') ?? '';
169
    }
170
171
    /**
172
     * Get alert message
173
     *
174
     * @return string
175
     */
176
    public function alertMsg(): string
177
    {
178
        if (Configure::read('Recovery')) {
179
            return __('Recovery Mode - Access restricted to admin users');
180
        }
181
        $request = $this->getView()->getRequest();
182
        $prefix = $request->getParam('prefix');
183
        if (Configure::read(sprintf('AlertMessageByArea.%s.text', $prefix))) {
184
            return Configure::read(sprintf('AlertMessageByArea.%s.text', $prefix));
185
        }
186
        $message = Configure::read('AlertMessage.text') ?? '';
187
        $user = $this->getView()->get('user');
188
        if ($user && in_array('admin', $user->get('roles')) && !$this->checkBeditaApiVersion()) {
189
            $message .= ' ' . __('API version required: {0}', Configure::read('BEditaAPI.versions'));
190
        }
191
192
        return $message;
193
    }
194
}
195