Init   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
c 0
b 0
f 0
dl 0
loc 156
rs 10
wmc 29

1 Method

Rating   Name   Duplication   Size   Complexity  
F execute() 0 154 29
1
<?php
2
3
/*
4
 * CKFinder
5
 * ========
6
 * http://cksource.com/ckfinder
7
 * Copyright (C) 2007-2016, CKSource - Frederico Knabben. All rights reserved.
8
 *
9
 * The software, this file and its contents are subject to the CKFinder
10
 * License. Please read the license.txt file before using, installing, copying,
11
 * modifying or distribute this file or part of its contents. The contents of
12
 * this file is part of the Source Code of CKFinder.
13
 */
14
15
namespace CKSource\CKFinder\Command;
16
17
use CKSource\CKFinder\Acl\Acl;
18
use CKSource\CKFinder\Acl\Permission;
19
use CKSource\CKFinder\CKFinder;
20
use CKSource\CKFinder\Config;
21
use CKSource\CKFinder\Filesystem\Path;
22
use CKSource\CKFinder\ResourceType\ResourceTypeFactory;
23
use CKSource\CKFinder\Utils;
24
use Symfony\Component\HttpFoundation\Request;
25
26
class Init extends CommandAbstract
27
{
28
    public function execute(Request $request, Acl $acl, Config $config, ResourceTypeFactory $resourceTypeFactory)
29
    {
30
        $data = new \stdClass();
31
32
        /**
33
         * The connector is always enabled here.
34
         *
35
         * @see CKFinder::checkAuth()
36
         */
37
        $data->enabled = true;
38
39
        $ln = '';
40
        $lc = str_replace('-', '', ($config->get('licenseKey') ?: $config->get('LicenseKey')) . '                                  ');
41
        $pos = strpos(CKFinder::CHARS, $lc[2]) % 5;
42
43
        if ($pos == 1 || $pos == 2) {
44
            $ln = $config->get('licenseName') ?: $config->get('LicenseName');
45
        }
46
47
        $data->s = $ln;
48
        $data->c = trim($lc[1] . $lc[8] . $lc[17] . $lc[22] . $lc[3] . $lc[13] . $lc[11] . $lc[20] . $lc[5] . $lc[24] . $lc[27]);
49
50
        // Thumbnails
51
        $thumbnailsConfig = $config->get('thumbnails');
52
53
        $thumbnailsEnabled = (bool) $thumbnailsConfig['enabled'];
54
55
        if ($thumbnailsEnabled) {
56
            $sizes = array();
57
            foreach ($thumbnailsConfig['sizes'] as $sizeInfo) {
58
                $sizes[] = sprintf("%dx%d", $sizeInfo['width'], $sizeInfo['height']);
59
            }
60
61
            $data->thumbs = $sizes;
62
        }
63
64
        // Images
65
        $imagesConfig = $config->get('images');
66
67
        $images = array(
68
            'max' => $imagesConfig['maxWidth'] . 'x' . $imagesConfig['maxHeight']
69
        );
70
71
        if (isset($imagesConfig['sizes'])) {
72
            $resize = array();
73
74
            foreach ($imagesConfig['sizes'] as $name => $sizeInfo) {
75
                $resize[$name] = $sizeInfo['width'] . 'x' . $sizeInfo['height'];
76
            }
77
78
            $images['sizes'] = $resize;
79
        }
80
81
        $data->images = $images;
82
83
        $resourceTypesNames = $config->getDefaultResourceTypes() ? : $config->getResourceTypes();
84
85
        $data->resourceTypes = array();
86
87
        if (!empty($resourceTypesNames)) {
88
            $phpMaxSize = 0;
89
90
            $maxUpload = Utils::returnBytes(ini_get('upload_max_filesize'));
91
            if ($maxUpload) {
92
                $phpMaxSize = $maxUpload;
93
            }
94
95
            $maxPost = Utils::returnBytes(ini_get('post_max_size'));
96
            if ($maxPost) {
97
                $phpMaxSize = $phpMaxSize ? min($phpMaxSize, $maxPost) : $maxPost;
98
            }
99
100
            //ini_get('memory_limit') only works if compiled with "--enable-memory-limit"
101
            $memoryLimit = Utils::returnBytes(@ini_get('memory_limit'));
0 ignored issues
show
Bug introduced by
It seems like @ini_get('memory_limit') can also be of type false; however, parameter $val of CKSource\CKFinder\Utils::returnBytes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
            $memoryLimit = Utils::returnBytes(/** @scrutinizer ignore-type */ @ini_get('memory_limit'));
Loading history...
102
            if ($memoryLimit && $memoryLimit != -1) {
103
                $phpMaxSize = $phpMaxSize ? min($phpMaxSize, $memoryLimit) : $memoryLimit;
104
            }
105
106
            $data->uploadMaxSize = $phpMaxSize;
107
            $data->uploadCheckImages = !$config->get('checkSizeAfterScaling');
108
109
            $requestedType = (string) $request->query->get('type');
110
111
            foreach ($resourceTypesNames as $resourceTypeName) {
112
                if ($requestedType && $requestedType !== $resourceTypeName) {
113
                    continue;
114
                }
115
116
                $aclMask = $acl->getComputedMask($resourceTypeName, '/');
117
118
                if (!(Permission::FOLDER_VIEW & $aclMask)) {
119
                    continue;
120
                }
121
122
                $resourceType = $resourceTypeFactory->getResourceType($resourceTypeName);
123
124
                $resourceTypeObject = array(
125
                    'name'              => $resourceTypeName,
126
                    'allowedExtensions' => implode(",", $resourceType->getAllowedExtensions()),
127
                    'deniedExtensions'  => implode(",", $resourceType->getDeniedExtensions()),
128
                    'hash'              => $resourceType->getHash(),
129
                    'acl'               => $aclMask,
130
                    'maxSize'           => $resourceType->getMaxSize() ? min($resourceType->getMaxSize(), $phpMaxSize) : $phpMaxSize
131
                );
132
133
                $resourceTypeBackend = $resourceType->getBackend();
134
135
                if ($resourceType->isLazyLoaded()) {
136
                    $resourceTypeObject['hasChildren'] = false;
137
                    $resourceTypeObject['lazyLoad'] = true;
138
                } else {
139
                    $resourceTypeObject['hasChildren'] = $resourceTypeBackend->containsDirectories($resourceType, $resourceType->getDirectory());
140
                }
141
142
                if ($label = $resourceType->getLabel()) {
143
                    $resourceTypeObject['label'] = $label;
144
                }
145
146
                $useProxyCommand = $resourceTypeBackend->usesProxyCommand();
147
148
                if ($useProxyCommand) {
149
                    $resourceTypeObject['useProxyCommand'] = true;
150
                } else {
151
                    $baseUrl = $resourceTypeBackend->getBaseUrl();
152
153
                    if ($baseUrl) {
154
                        $resourceTypeObject['url'] = rtrim(Path::combine($baseUrl, $resourceType->getDirectory()), '/') . '/';
155
                    }
156
                }
157
158
159
                $trackedOperations = $resourceTypeBackend->getTrackedOperations();
160
161
                if (!empty($trackedOperations)) {
162
                    $resourceTypeObject['trackedOperations'] = $trackedOperations;
163
                }
164
165
                $data->resourceTypes[] = $resourceTypeObject;
166
            }
167
        }
168
169
        $enabledPlugins = $config->get('plugins');
170
171
        if (!empty($enabledPlugins)) {
172
            $data->plugins = $enabledPlugins;
173
        }
174
175
        $proxyCacheLifetime = (int) $config->get('cache.proxyCommand');
176
177
        if ($proxyCacheLifetime) {
178
            $data->proxyCache = $proxyCacheLifetime;
179
        }
180
181
        return $data;
182
    }
183
}
184