Completed
Push — master ( 636ea3...70c8fc )
by Alberto
19s queued 12s
created

SystemHelper::uploadConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace App\View\Helper;
5
6
use Cake\Core\Configure;
7
use Cake\Utility\Hash;
8
use Cake\View\Helper;
9
10
/**
11
 * System helper
12
 */
13
class SystemHelper extends Helper
14
{
15
    /**
16
     * Accepted mime types for upload
17
     *
18
     * @var array
19
     */
20
    protected $defaultUploadAccepted = [
21
        'audio' => [
22
            'audio/*',
23
        ],
24
        'images' => [
25
            'image/*',
26
        ],
27
        'videos' => [
28
            'application/x-mpegURL',
29
            'video/*',
30
        ],
31
    ];
32
33
    /**
34
     * Not accepted mime types and extesions for upload
35
     *
36
     * @var array
37
     */
38
    protected $defaultUploadForbidden = [
39
        'mimetypes' => [
40
            'application/javascript',
41
            'application/x-cgi',
42
            'application/x-perl',
43
            'application/x-php',
44
            'application/x-ruby',
45
            'application/x-shellscript',
46
            'text/javascript',
47
            'text/x-perl',
48
            'text/x-php',
49
            'text/x-python',
50
            'text/x-ruby',
51
            'text/x-shellscript',
52
        ],
53
        'extensions' => [
54
            'cgi',
55
            'exe',
56
            'js',
57
            'perl',
58
            'php',
59
            'py',
60
            'rb',
61
            'sh',
62
        ],
63
    ];
64
65
    /**
66
     * Get the minimum value between post_max_size and upload_max_filesize.
67
     *
68
     * @return int
69
     */
70
    public function getMaxFileSize(): int
71
    {
72
        $postMaxSize = intVal(substr(ini_get('post_max_size'), 0, -1));
73
        $uploadMaxFilesize = intVal(substr(ini_get('upload_max_filesize'), 0, -1));
74
75
        return min($postMaxSize, $uploadMaxFilesize) * 1024 * 1024;
76
    }
77
78
    /**
79
     * Return false when API version is less than required, true otherwise.
80
     *
81
     * @return bool
82
     */
83
    public function checkBeditaApiVersion(): bool
84
    {
85
        $project = (array)$this->getView()->get('project');
86
        $apiVersion = Hash::get($project, 'version');
87
        if (empty($apiVersion)) {
88
            return true;
89
        }
90
        $requiredApiVersions = (array)Configure::read('BEditaAPI.versions');
91
        foreach ($requiredApiVersions as $requiredApiVersion) {
92
            $apiMajor = substr($apiVersion, 0, strpos($apiVersion, '.'));
93
            $requiredApiMajor = substr($requiredApiVersion, 0, strpos($requiredApiVersion, '.'));
94
            if ($apiMajor === $requiredApiMajor && version_compare($apiVersion, $requiredApiVersion) >= 0) {
95
                return true;
96
            }
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * Upload config
104
     *
105
     * @return array
106
     */
107
    public function uploadConfig(): array
108
    {
109
        $accepted = (array)Configure::read('uploadAccepted', $this->defaultUploadAccepted);
110
        $forbidden = (array)Configure::read('uploadForbidden', $this->defaultUploadForbidden);
111
112
        return compact('accepted', 'forbidden');
113
    }
114
}
115