Passed
Push — master ( ef3387...d0a4bc )
by Anthony
07:14
created

CKFinder_Connector_CommandHandler_FileUpload::sendResponse()   F

Complexity

Conditions 42
Paths > 20000

Size

Total Lines 160
Code Lines 103

Duplication

Lines 160
Ratio 100 %

Importance

Changes 0
Metric Value
cc 42
eloc 103
nc 552960
nop 0
dl 160
loc 160
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CKFinder
4
 * ========
5
 * http://ckfinder.com
6
 * Copyright (C) 2007-2010, CKSource - Frederico Knabben. All rights reserved.
7
 *
8
 * The software, this file and its contents are subject to the CKFinder
9
 * License. Please read the license.txt file before using, installing, copying,
10
 * modifying or distribute this file or part of its contents. The contents of
11
 * this file is part of the Source Code of CKFinder.
12
 */
13
if (!defined('IN_CKFINDER')) exit;
14
15
/**
16
 * @package CKFinder
17
 * @subpackage CommandHandlers
18
 * @copyright CKSource - Frederico Knabben
19
 */
20
21
/**
22
 * Handle FileUpload command
23
 *
24
 * @package CKFinder
25
 * @subpackage CommandHandlers
26
 * @copyright CKSource - Frederico Knabben
27
 */
28
class CKFinder_Connector_CommandHandler_FileUpload extends CKFinder_Connector_CommandHandler_CommandHandlerBase
29
{
30
    /**
31
     * Command name
32
     *
33
     * @access protected
34
     * @var string
35
     */
36
    var $command = "FileUpload";
37
38
    /**
39
     * send response (save uploaded file, resize if required)
40
     * @access public
41
     *
42
     */
43
    function sendResponse()
44
    {
45
        $iErrorNumber = CKFINDER_CONNECTOR_ERROR_NONE;
46
47
        $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
48
        $oRegistry =& CKFinder_Connector_Core_Factory::getInstance("Core_Registry");
49
        $oRegistry->set("FileUpload_fileName", "unknown file");
50
51
        $uploadedFile = array_shift($_FILES);
52
53
        if (!isset($uploadedFile['name'])) {
54
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID);
55
        }
56
57
        $sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name']));
58
        $sFileName = str_replace(array(":", "*", "?", "|", "/"), "_", $sUnsafeFileName);
59
        if ($_config->forceAscii()) {
60
            $sFileName = CKFinder_Connector_Utils_FileSystem::convertToAscii($sFileName);
61
        }
62
        if ($sFileName != $sUnsafeFileName) {
63
          $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID_NAME_RENAMED;
64
        }
65
        $oRegistry->set("FileUpload_fileName", $sFileName);
66
        $oRegistry->set("FileUpload_url", $this->_currentFolder->getUrl());
67
68
        $this->checkConnector();
69
        $this->checkRequest();
70
71
        if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_UPLOAD)) {
72
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
73
        }
74
75
        $_resourceTypeConfig = $this->_currentFolder->getResourceTypeConfig();
76
        if (!CKFinder_Connector_Utils_FileSystem::checkFileName($sFileName) || $_resourceTypeConfig->checkIsHiddenFile($sFileName)) {
77
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
78
        }
79
80
        $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
81
        if (!$resourceTypeInfo->checkExtension($sFileName)) {
82
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
83
        }
84
85
        $sFileNameOrginal = $sFileName;
86
        $oRegistry->set("FileUpload_fileName", $sFileName);
87
88
        $maxSize = $resourceTypeInfo->getMaxSize();
89
        if (!$_config->checkSizeAfterScaling() && $maxSize && $uploadedFile['size']>$maxSize) {
90
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
91
        }
92
93
        $htmlExtensions = $_config->getHtmlExtensions();
94
        $sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
95
96
        if ($htmlExtensions
97
        && !CKFinder_Connector_Utils_Misc::inArrayCaseInsensitive($sExtension, $htmlExtensions)
98
        && ($detectHtml = CKFinder_Connector_Utils_FileSystem::detectHtml($uploadedFile['tmp_name'])) === true ) {
99
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
100
        }
101
102
        $sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
103
        $secureImageUploads = $_config->getSecureImageUploads();
104
        if ($secureImageUploads
105
        && ($isImageValid = CKFinder_Connector_Utils_FileSystem::isImageValid($uploadedFile['tmp_name'], $sExtension)) === false ) {
106
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
107
        }
108
109
        switch ($uploadedFile['error']) {
110
            case UPLOAD_ERR_OK:
111
                break;
112
113
            case UPLOAD_ERR_INI_SIZE:
114
            case UPLOAD_ERR_FORM_SIZE:
115
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
116
                break;
117
118
            case UPLOAD_ERR_PARTIAL:
119
            case UPLOAD_ERR_NO_FILE:
120
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
121
                break;
122
123
            case UPLOAD_ERR_NO_TMP_DIR:
124
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_NO_TMP_DIR);
125
                break;
126
127
            case UPLOAD_ERR_CANT_WRITE:
128
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
129
                break;
130
131
            case UPLOAD_ERR_EXTENSION:
132
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
133
                break;
134
        }
135
136
        $sServerDir = $this->_currentFolder->getServerPath();
137
        $iCounter = 0;
138
139
        while (true)
140
        {
141
            $sFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($sServerDir, $sFileName);
142
143
144
            if (file_exists($sFilePath)) {
145
146
                $iCounter++;
147
                $sFileName =
148
                CKFinder_Connector_Utils_FileSystem::getFileNameWithoutExtension($sFileNameOrginal) .
149
                "(" . $iCounter . ")" . "." .
150
                CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
151
                $oRegistry->set("FileUpload_fileName", $sFileName);
152
153
                $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_FILE_RENAMED;
154
            } else {
155
                if (false === move_uploaded_file($uploadedFile['tmp_name'], $sFilePath)) {
156
                    $iErrorNumber = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
157
                }
158
                else {
159
                    if (isset($detectHtml) && $detectHtml === -1 && CKFinder_Connector_Utils_FileSystem::detectHtml($sFilePath) === true) {
160
                        @unlink($sFilePath);
161
                        $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
162
                    }
163
                    else if (isset($isImageValid) && $isImageValid === -1 && CKFinder_Connector_Utils_FileSystem::isImageValid($sFilePath, $sExtension) === false) {
164
                        @unlink($sFilePath);
165
                        $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
166
                    }
167
                }
168
                if (is_file($sFilePath) && ($perms = $_config->getChmodFiles())) {
169
                    $oldumask = umask(0);
170
                    chmod($sFilePath, $perms);
171
                    umask($oldumask);
172
                }
173
                break;
174
            }
175
        }
176
177
        if (!$_config->checkSizeAfterScaling()) {
178
            $this->_errorHandler->throwError($iErrorNumber, true, false);
179
        }
180
181
        //resize image if required
182
        require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/Thumbnail.php";
183
        $_imagesConfig = $_config->getImagesConfig();
184
185
        if ($_imagesConfig->getMaxWidth()>0 && $_imagesConfig->getMaxHeight()>0 && $_imagesConfig->getQuality()>0) {
186
            CKFinder_Connector_CommandHandler_Thumbnail::createThumb($sFilePath, $sFilePath, $_imagesConfig->getMaxWidth(), $_imagesConfig->getMaxHeight(), $_imagesConfig->getQuality(), true) ;
187
        }
188
189
        if ($_config->checkSizeAfterScaling()) {
190
            //check file size after scaling, attempt to delete if too big
191
            clearstatcache();
192
            if ($maxSize && filesize($sFilePath)>$maxSize) {
193
                @unlink($sFilePath);
194
                $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
195
            }
196
            else {
197
                $this->_errorHandler->throwError($iErrorNumber, true, false);
198
            }
199
        }
200
201
        CKFinder_Connector_Core_Hooks::run('AfterFileUpload', array(&$this->_currentFolder, &$uploadedFile, &$sFilePath));
202
    }
203
}
204