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

CKFinder_Connector_CommandHandler_RenameFile::buildXml()   F

Complexity

Conditions 18
Paths 16384

Size

Total Lines 76
Code Lines 44

Duplication

Lines 76
Ratio 100 %

Importance

Changes 0
Metric Value
cc 18
eloc 44
nc 16384
nop 0
dl 76
loc 76
rs 2.2902
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
 * Include base XML command handler
23
 */
24
require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/XmlCommandHandlerBase.php";
25
26
/**
27
 * Handle RenameFile command
28
 *
29
 * @package CKFinder
30
 * @subpackage CommandHandlers
31
 * @copyright CKSource - Frederico Knabben
32
 */
33
class CKFinder_Connector_CommandHandler_RenameFile extends CKFinder_Connector_CommandHandler_XmlCommandHandlerBase
34
{
35
    /**
36
     * Command name
37
     *
38
     * @access private
39
     * @var string
40
     */
41
    var $command = "RenameFile";
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
        if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_RENAME)) {
56
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
57
        }
58
59
        if (!isset($_GET["fileName"])) {
60
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
61
        }
62
        if (!isset($_GET["newFileName"])) {
63
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
64
        }
65
66
        $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
67
        $fileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_GET["fileName"]);
68
        $newFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_GET["newFileName"]);
69
70
        $oRenamedFileNode = new Ckfinder_Connector_Utils_XmlNode("RenamedFile");
71
        $this->_connectorNode->addChild($oRenamedFileNode);
72
        $oRenamedFileNode->addAttribute("name", CKFinder_Connector_Utils_FileSystem::convertToConnectorEncoding($fileName));
73
74
        $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
75
        if (!$resourceTypeInfo->checkExtension($newFileName)) {
76
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
77
        }
78
79
        if (!CKFinder_Connector_Utils_FileSystem::checkFileName($fileName) || $resourceTypeInfo->checkIsHiddenFile($fileName)) {
80
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
81
        }
82
83
        if (!CKFinder_Connector_Utils_FileSystem::checkFileName($newFileName) || $resourceTypeInfo->checkIsHiddenFile($newFileName)) {
84
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
85
        }
86
87
        if (!$resourceTypeInfo->checkExtension($fileName, false)) {
88
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
89
        }
90
91
        if ($_config->forceAscii()) {
92
            $newFileName = CKFinder_Connector_Utils_FileSystem::convertToAscii($newFileName);
93
        }
94
        $filePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $fileName);
95
        $newFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $newFileName);
96
97
        $bMoved = false;
98
99
        if (!file_exists($filePath)) {
100
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND);
101
        }
102
103
        if (!is_writable(dirname($newFilePath))) {
104
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
105
        }
106
107
        if (!is_writable($filePath)) {
108
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
109
        }
110
        if (file_exists($newFilePath)) {
111
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ALREADY_EXIST);
112
        }
113
114
        $bMoved = @rename($filePath, $newFilePath);
115
116
        if (!$bMoved) {
117
            $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNKNOWN, "File " . CKFinder_Connector_Utils_FileSystem::convertToConnectorEncoding($fileName) . "has not been renamed");
118
        } else {
119
            $oRenamedFileNode->addAttribute("newName", CKFinder_Connector_Utils_FileSystem::convertToConnectorEncoding($newFileName));
120
121
            $thumbPath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getThumbsServerPath(), $fileName);
122
            CKFinder_Connector_Utils_FileSystem::unlink($thumbPath);
123
        }
124
    }
125
}
126