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

CKFinder_Connector_CommandHandler_MoveFiles   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 239
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 239
loc 239
rs 8.2769
c 0
b 0
f 0
wmc 41
lcom 1
cbo 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CKFinder_Connector_CommandHandler_MoveFiles often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CKFinder_Connector_CommandHandler_MoveFiles, and based on these observations, apply Extract Interface, too.

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
 * Include base XML command handler
23
 */
24
require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/XmlCommandHandlerBase.php";
25
26
/**
27
 * Handle MoveFiles command
28
 *
29
 * @package CKFinder
30
 * @subpackage CommandHandlers
31
 * @copyright CKSource - Frederico Knabben
32
 */
33
class CKFinder_Connector_CommandHandler_MoveFiles extends CKFinder_Connector_CommandHandler_XmlCommandHandlerBase
34
{
35
    /**
36
     * Command name
37
     *
38
     * @access private
39
     * @var string
40
     */
41
    var $command = "MoveFiles";
42
43
44
    /**
45
     * handle request and build XML
46
     * @access protected
47
     *
48
     */
49
    function buildXml()
50
    {
51
        if (empty($_POST['CKFinderCommand']) || $_POST['CKFinderCommand'] != 'true') {
52
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
53
        }
54
55
        $clientPath = $this->_currentFolder->getClientPath();
56
        $sServerDir = $this->_currentFolder->getServerPath();
57
        $currentResourceTypeConfig = $this->_currentFolder->getResourceTypeConfig();
58
        $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
59
        $_aclConfig = $_config->getAccessControlConfig();
60
        $aclMasks = array();
61
        $_resourceTypeConfig = array();
62
63
        if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_RENAME | CKFINDER_CONNECTOR_ACL_FILE_UPLOAD | CKFINDER_CONNECTOR_ACL_FILE_DELETE)) {
64
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
65
        }
66
67
        // Create the "Errors" node.
68
        $oErrorsNode = new CKFinder_Connector_Utils_XmlNode("Errors");
69
        $errorCode = CKFINDER_CONNECTOR_ERROR_NONE;
70
        $moved = 0;
71
        $movedAll = 0;
72
        if (!empty($_POST['moved'])) {
73
            $movedAll = intval($_POST['moved']);
74
        }
75
        $checkedPaths = array();
76
77
        $oMoveFilesNode = new Ckfinder_Connector_Utils_XmlNode("MoveFiles");
78
79
        if (!empty($_POST['files']) && is_array($_POST['files'])) {
80
            foreach ($_POST['files'] as $index => $arr) {
81
                if (empty($arr['name'])) {
82
                    continue;
83
                }
84
                if (!isset($arr['name'], $arr['type'], $arr['folder'])) {
85
                    $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
86
                }
87
88
                // file name
89
                $name = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($arr['name']);
90
                // resource type
91
                $type = $arr['type'];
92
                // client path
93
                $path = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($arr['folder']);
94
                // options
95
                $options = (!empty($arr['options'])) ? $arr['options'] : '';
96
97
                $destinationFilePath = $sServerDir.$name;
98
99
                // check #1 (path)
100
                if (!CKFinder_Connector_Utils_FileSystem::checkFileName($name) || preg_match(CKFINDER_REGEX_INVALID_PATH, $path)) {
101
                    $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
102
                }
103
104
                // get resource type config for current file
105
                if (!isset($_resourceTypeConfig[$type])) {
106
                    $_resourceTypeConfig[$type] = $_config->getResourceTypeConfig($type);
107
                }
108
109
                // check #2 (resource type)
110
                if (is_null($_resourceTypeConfig[$type])) {
111
                    $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
112
                }
113
114
                // check #3 (extension)
115
                if (!$_resourceTypeConfig[$type]->checkExtension($name, false)) {
116
                    $errorCode = CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION;
117
                    $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
118
                    continue;
119
                }
120
121
                // check #4 (extension) - when moving to another resource type, double check extension
122
                if ($currentResourceTypeConfig->getName() != $type) {
123
                    if (!$currentResourceTypeConfig->checkExtension($name, false)) {
124
                        $errorCode = CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION;
125
                        $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
126
                        continue;
127
                    }
128
                }
129
130
                // check #5 (hidden folders)
131
                // cache results
132
                if (empty($checkedPaths[$path])) {
133
                    $checkedPaths[$path] = true;
134
135
                    if ($_resourceTypeConfig[$type]->checkIsHiddenPath($path)) {
136
                        $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
137
                    }
138
                }
139
140
                $sourceFilePath = $_resourceTypeConfig[$type]->getDirectory().$path.$name;
141
142
                // check #6 (hidden file name)
143
                if ($currentResourceTypeConfig->checkIsHiddenFile($name)) {
144
                    $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
145
                }
146
147
                // check #7 (Access Control, need file view permission to source files)
148
                if (!isset($aclMasks[$type."@".$path])) {
149
                    $aclMasks[$type."@".$path] = $_aclConfig->getComputedMask($type, $path);
150
                }
151
152
                $isAuthorized = (($aclMasks[$type."@".$path] & CKFINDER_CONNECTOR_ACL_FILE_VIEW) == CKFINDER_CONNECTOR_ACL_FILE_VIEW);
153
                if (!$isAuthorized) {
154
                    $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
155
                }
156
157
                // check #8 (invalid file name)
158
                if (!file_exists($sourceFilePath) || !is_file($sourceFilePath)) {
159
                    $errorCode = CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND;
160
                    $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
161
                    continue;
162
                }
163
164
                // check #9 (max size)
165
                if ($currentResourceTypeConfig->getName() != $type) {
166
                    $maxSize = $currentResourceTypeConfig->getMaxSize();
167
                    $fileSize = filesize($sourceFilePath);
168
                    if ($maxSize && $fileSize>$maxSize) {
169
                        $errorCode = CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG;
170
                        $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
171
                        continue;
172
                    }
173
                }
174
175
                //$overwrite
176
                // finally, no errors so far, we may attempt to copy a file
177
                // protection against copying files to itself
178
                if ($sourceFilePath == $destinationFilePath) {
179
                    $errorCode = CKFINDER_CONNECTOR_ERROR_SOURCE_AND_TARGET_PATH_EQUAL;
180
                    $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
181
                    continue;
182
                }
183
                // check if file exists if we don't force overwriting
184
                else if (file_exists($destinationFilePath)) {
185
                    if (strpos($options, "overwrite") !== false) {
186
                        if (!@unlink($destinationFilePath)) {
187
                            $errorCode = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
188
                            $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
189
                            continue;
190
                        }
191
                        else {
192
                            if (!@rename($sourceFilePath, $destinationFilePath)) {
193
                                $errorCode = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
194
                                $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
195
                                continue;
196
                            }
197
                            else {
198
                                $moved++;
199
                            }
200
                        }
201
                    }
202
                    else if (strpos($options, "autorename") !== false) {
203
                        $iCounter = 1;
204
                        while (true)
205
                        {
206
                            $fileName = CKFinder_Connector_Utils_FileSystem::getFileNameWithoutExtension($name) .
207
                                "(" . $iCounter . ")" . "." .
208
                                CKFinder_Connector_Utils_FileSystem::getExtension($name);
209
210
                            $destinationFilePath = $sServerDir.$fileName;
211
                            if (!file_exists($destinationFilePath)) {
212
                                break;
213
                            }
214
                            else {
215
                                $iCounter++;
216
                            }
217
                        }
218
                        if (!@rename($sourceFilePath, $destinationFilePath)) {
219
                            $errorCode = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
220
                            $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
221
                            continue;
222
                        }
223
                        else {
224
                            $moved++;
225
                        }
226
                    }
227
                    else {
228
                        $errorCode = CKFINDER_CONNECTOR_ERROR_ALREADY_EXIST;
229
                        $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
230
                        continue;
231
                    }
232
                }
233
                else {
234
                    if (!@rename($sourceFilePath, $destinationFilePath)) {
235
                        $errorCode = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
236
                        $this->appendErrorNode($oErrorsNode, $errorCode, $name, $type, $path);
237
                        continue;
238
                    }
239
                    else {
240
                        $moved++;
241
                    }
242
                }
243
            }
244
        }
245
246
        $this->_connectorNode->addChild($oMoveFilesNode);
247
        if ($errorCode != CKFINDER_CONNECTOR_ERROR_NONE) {
248
            $this->_connectorNode->addChild($oErrorsNode);
249
        }
250
        $oMoveFilesNode->addAttribute("moved", $moved);
251
        $oMoveFilesNode->addAttribute("movedTotal", $movedAll + $moved);
252
253
        /**
254
         * Note: actually we could have more than one error.
255
         * This is just a flag for CKFinder interface telling it to check all errors.
256
         */
257
        if ($errorCode != CKFINDER_CONNECTOR_ERROR_NONE) {
258
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_MOVE_FAILED);
259
        }
260
    }
261
262
    function appendErrorNode(&$oErrorsNode, $errorCode, $name, $type, $path)
263
    {
264
        $oErrorNode = new CKFinder_Connector_Utils_XmlNode("Error");
265
        $oErrorNode->addAttribute("code", $errorCode);
266
        $oErrorNode->addAttribute("name", CKFinder_Connector_Utils_FileSystem::convertToConnectorEncoding($name));
267
        $oErrorNode->addAttribute("type", $type);
268
        $oErrorNode->addAttribute("folder", $path);
269
        $oErrorsNode->addChild($oErrorNode);
270
    }
271
}
272