PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/ckeditor/ckfinder/core/connector/php/php5/CommandHandler/MoveFiles.php

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