PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/admin/ckfinder/core/connector/php/php5/CommandHandler/FileUpload.php

https://bitbucket.org/datdiep/visatravel.vn
PHP | 198 lines | 128 code | 34 blank | 36 comment | 41 complexity | e4f6e7eeb3bb777a19c8e57843bd9ca3 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * CKFinder
  4. * ========
  5. * http://ckfinder.com
  6. * Copyright (C) 2007-2012, 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'))
  14. exit;
  15. /**
  16. * @package CKFinder
  17. * @subpackage CommandHandlers
  18. * @copyright CKSource - Frederico Knabben
  19. */
  20. /**
  21. * Handle FileUpload command
  22. *
  23. * @package CKFinder
  24. * @subpackage CommandHandlers
  25. * @copyright CKSource - Frederico Knabben
  26. */
  27. class CKFinder_Connector_CommandHandler_FileUpload extends CKFinder_Connector_CommandHandler_CommandHandlerBase {
  28. /**
  29. * Command name
  30. *
  31. * @access protected
  32. * @var string
  33. */
  34. protected $command = "FileUpload";
  35. /**
  36. * send response (save uploaded file, resize if required)
  37. * @access public
  38. *
  39. */
  40. public function sendResponse() {
  41. $iErrorNumber = CKFINDER_CONNECTOR_ERROR_NONE;
  42. $_config = & CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  43. $oRegistry = & CKFinder_Connector_Core_Factory::getInstance("Core_Registry");
  44. $oRegistry->set("FileUpload_fileName", "unknown file");
  45. $uploadedFile = array_shift($_FILES);
  46. if (!isset($uploadedFile['name'])) {
  47. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID);
  48. }
  49. $sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name']));
  50. $sFileName = str_replace(array(":", "*", "?", "|", "/"), "_", $sUnsafeFileName);
  51. if ($_config->getDisallowUnsafeCharacters()) {
  52. $sFileName = str_replace(";", "_", $sFileName);
  53. }
  54. if ($_config->forceAscii()) {
  55. $sFileName = CKFinder_Connector_Utils_FileSystem::convertToAscii($sFileName);
  56. }
  57. if ($sFileName != $sUnsafeFileName) {
  58. $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID_NAME_RENAMED;
  59. }
  60. $oRegistry->set("FileUpload_fileName", $sFileName);
  61. $this->checkConnector();
  62. $this->checkRequest();
  63. if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_UPLOAD)) {
  64. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
  65. }
  66. $_resourceTypeConfig = $this->_currentFolder->getResourceTypeConfig();
  67. if (!CKFinder_Connector_Utils_FileSystem::checkFileName($sFileName) || $_resourceTypeConfig->checkIsHiddenFile($sFileName)) {
  68. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
  69. }
  70. $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
  71. if (!$resourceTypeInfo->checkExtension($sFileName)) {
  72. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
  73. }
  74. $sFileNameOrginal = $sFileName;
  75. $oRegistry->set("FileUpload_fileName", $sFileName);
  76. $oRegistry->set("FileUpload_url", $this->_currentFolder->getUrl());
  77. $maxSize = $resourceTypeInfo->getMaxSize();
  78. if (!$_config->checkSizeAfterScaling() && $maxSize && $uploadedFile['size'] > $maxSize) {
  79. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
  80. }
  81. $htmlExtensions = $_config->getHtmlExtensions();
  82. $sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
  83. if ($htmlExtensions && !CKFinder_Connector_Utils_Misc::inArrayCaseInsensitive($sExtension, $htmlExtensions) && ($detectHtml = CKFinder_Connector_Utils_FileSystem::detectHtml($uploadedFile['tmp_name'])) === true) {
  84. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
  85. }
  86. $sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
  87. $secureImageUploads = $_config->getSecureImageUploads();
  88. if ($secureImageUploads && ($isImageValid = CKFinder_Connector_Utils_FileSystem::isImageValid($uploadedFile['tmp_name'], $sExtension)) === false) {
  89. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
  90. }
  91. switch ($uploadedFile['error']) {
  92. case UPLOAD_ERR_OK:
  93. break;
  94. case UPLOAD_ERR_INI_SIZE:
  95. case UPLOAD_ERR_FORM_SIZE:
  96. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
  97. break;
  98. case UPLOAD_ERR_PARTIAL:
  99. case UPLOAD_ERR_NO_FILE:
  100. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
  101. break;
  102. case UPLOAD_ERR_NO_TMP_DIR:
  103. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_NO_TMP_DIR);
  104. break;
  105. case UPLOAD_ERR_CANT_WRITE:
  106. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
  107. break;
  108. case UPLOAD_ERR_EXTENSION:
  109. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
  110. break;
  111. }
  112. $sServerDir = $this->_currentFolder->getServerPath();
  113. $iCounter = 0;
  114. while (true) {
  115. $sFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($sServerDir, $sFileName);
  116. if (file_exists($sFilePath)) {
  117. $iCounter++;
  118. $sFileName = CKFinder_Connector_Utils_FileSystem::getFileNameWithoutExtension($sFileNameOrginal) .
  119. "(" . $iCounter . ")" . "." .
  120. CKFinder_Connector_Utils_FileSystem::getExtension($sFileNameOrginal);
  121. $oRegistry->set("FileUpload_fileName", $sFileName);
  122. $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_FILE_RENAMED;
  123. } else {
  124. if (false === move_uploaded_file($uploadedFile['tmp_name'], $sFilePath)) {
  125. $iErrorNumber = CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED;
  126. } else {
  127. if (isset($detectHtml) && $detectHtml === -1 && CKFinder_Connector_Utils_FileSystem::detectHtml($sFilePath) === true) {
  128. @unlink($sFilePath);
  129. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_WRONG_HTML_FILE);
  130. } else if (isset($isImageValid) && $isImageValid === -1 && CKFinder_Connector_Utils_FileSystem::isImageValid($sFilePath, $sExtension) === false) {
  131. @unlink($sFilePath);
  132. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_CORRUPT);
  133. }
  134. }
  135. if (is_file($sFilePath) && ($perms = $_config->getChmodFiles())) {
  136. $oldumask = umask(0);
  137. chmod($sFilePath, $perms);
  138. umask($oldumask);
  139. }
  140. break;
  141. }
  142. }
  143. if (!$_config->checkSizeAfterScaling()) {
  144. $this->_errorHandler->throwError($iErrorNumber, true, false);
  145. }
  146. //resize image if required
  147. require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/Thumbnail.php";
  148. $_imagesConfig = $_config->getImagesConfig();
  149. if ($_imagesConfig->getMaxWidth() > 0 && $_imagesConfig->getMaxHeight() > 0 && $_imagesConfig->getQuality() > 0) {
  150. CKFinder_Connector_CommandHandler_Thumbnail::createThumb($sFilePath, $sFilePath, $_imagesConfig->getMaxWidth(), $_imagesConfig->getMaxHeight(), $_imagesConfig->getQuality(), true);
  151. }
  152. if ($_config->checkSizeAfterScaling()) {
  153. //check file size after scaling, attempt to delete if too big
  154. clearstatcache();
  155. if ($maxSize && filesize($sFilePath) > $maxSize) {
  156. @unlink($sFilePath);
  157. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG);
  158. } else {
  159. $this->_errorHandler->throwError($iErrorNumber, true, false);
  160. }
  161. }
  162. CKFinder_Connector_Core_Hooks::run('AfterFileUpload', array(&$this->_currentFolder, &$uploadedFile, &$sFilePath));
  163. }
  164. }