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 classes like CKFinder_Connector_CommandHandler_FileUpload 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_FileUpload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 |