PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/themes/third_party/wygwam/lib/ckfinder/plugins/imageresize/plugin.php

https://bitbucket.org/tdevonshire/guinness-jazz-festival
PHP | 248 lines | 173 code | 34 blank | 41 comment | 53 complexity | 7ceaa39826e19ad1a6dc8ad24d9c15a2 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. * CKFinder extension: resize image according to a given size
  14. */
  15. if (!defined('IN_CKFINDER')) exit;
  16. /**
  17. * Include base XML command handler
  18. */
  19. require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/XmlCommandHandlerBase.php";
  20. class CKFinder_Connector_CommandHandler_ImageResize extends CKFinder_Connector_CommandHandler_XmlCommandHandlerBase
  21. {
  22. /**
  23. * @access private
  24. */
  25. function getConfig()
  26. {
  27. $config = array();
  28. if (isset($GLOBALS['config']['plugin_imageresize'])) {
  29. $config = $GLOBALS['config']['plugin_imageresize'];
  30. }
  31. if (!isset($config['smallThumb'])) {
  32. $config['smallThumb'] = "90x90";
  33. }
  34. if (!isset($config['mediumThumb'])) {
  35. $config['mediumThumb'] = "120x120";
  36. }
  37. if (!isset($config['largeThumb'])) {
  38. $config['largeThumb'] = "180x180";
  39. }
  40. return $config;
  41. }
  42. /**
  43. * handle request and build XML
  44. * @access protected
  45. *
  46. */
  47. function buildXml()
  48. {
  49. if (empty($_POST['CKFinderCommand']) || $_POST['CKFinderCommand'] != 'true') {
  50. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  51. }
  52. $this->checkConnector();
  53. $this->checkRequest();
  54. //resizing to 1x1 is almost equal to deleting a file, that's why FILE_DELETE permissions are required
  55. if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_DELETE) || !$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_UPLOAD)) {
  56. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
  57. }
  58. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  59. $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
  60. if (!isset($_POST["fileName"])) {
  61. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
  62. }
  63. $fileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_POST["fileName"]);
  64. if (!CKFinder_Connector_Utils_FileSystem::checkFileName($fileName) || $resourceTypeInfo->checkIsHiddenFile($fileName)) {
  65. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  66. }
  67. if (!$resourceTypeInfo->checkExtension($fileName, false)) {
  68. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  69. }
  70. $filePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $fileName);
  71. if (!file_exists($filePath) || !is_file($filePath)) {
  72. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND);
  73. }
  74. $newWidth = trim($_POST['width']);
  75. $newHeight = trim($_POST['height']);
  76. $quality = 80;
  77. $resizeOriginal = !empty($_POST['width']) && !empty($_POST['height']);
  78. if ($resizeOriginal) {
  79. if (!preg_match("/^\d+$/", $newWidth) || !preg_match("/^\d+$/", $newHeight) || !preg_match("/^\d+$/", $newWidth)) {
  80. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  81. }
  82. if (!isset($_POST["newFileName"])) {
  83. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
  84. }
  85. $newFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_POST["newFileName"]);
  86. if (!$resourceTypeInfo->checkExtension($newFileName)) {
  87. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_EXTENSION);
  88. }
  89. if (!CKFinder_Connector_Utils_FileSystem::checkFileName($newFileName) || $resourceTypeInfo->checkIsHiddenFile($newFileName)) {
  90. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
  91. }
  92. $newFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $newFileName);
  93. if (!is_writable(dirname($newFilePath))) {
  94. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
  95. }
  96. if ($_POST['overwrite'] != "1" && file_exists($newFilePath)) {
  97. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ALREADY_EXIST);
  98. }
  99. $_imagesConfig = $_config->getImagesConfig();
  100. $maxWidth = $_imagesConfig->getMaxWidth();
  101. $maxHeight = $_imagesConfig->getMaxHeight();
  102. // Shouldn't happen as the JavaScript validation should not allow this.
  103. if ( ( $maxWidth > 0 && $newWidth > $maxWidth ) || ( $maxHeight > 0 && $newHeight > $maxHeight ) ) {
  104. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  105. }
  106. }
  107. require_once CKFINDER_CONNECTOR_LIB_DIR . "/CommandHandler/Thumbnail.php";
  108. if ($resizeOriginal) {
  109. $result = CKFinder_Connector_CommandHandler_Thumbnail::createThumb($filePath, $newFilePath, $newWidth, $newHeight, $quality, false) ;
  110. if (!$result) {
  111. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
  112. }
  113. }
  114. $config = $this->getConfig();
  115. $nameWithoutExt = preg_replace("/^(.+)\_\d+x\d+$/", "$1", CKFinder_Connector_Utils_FileSystem::getFileNameWithoutExtension($fileName));
  116. $extension = CKFinder_Connector_Utils_FileSystem::getExtension($fileName);
  117. foreach (array('small', 'medium', 'large') as $size) {
  118. if (!empty($_POST[$size]) && $_POST[$size] == '1') {
  119. $thumbName = $nameWithoutExt."_".$size.".".$extension;
  120. $newFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $thumbName);
  121. if (!empty($config[$size.'Thumb'])) {
  122. if (preg_match("/^(\d+)x(\d+)$/", $config[$size.'Thumb'], $matches)) {
  123. CKFinder_Connector_CommandHandler_Thumbnail::createThumb($filePath, $newFilePath, $matches[1], $matches[2], $quality, true) ;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * @access public
  131. */
  132. function onInitCommand( &$connectorNode )
  133. {
  134. // "@" protects against E_STRICT (Only variables should be assigned by reference)
  135. @$pluginsInfo = &$connectorNode->getChild("PluginsInfo");
  136. $imageresize = new CKFinder_Connector_Utils_XmlNode("imageresize");
  137. $pluginsInfo->addChild($imageresize);
  138. $config = $this->getConfig();
  139. foreach (array('small', 'medium', 'large') as $size) {
  140. if (!empty($config[$size.'Thumb'])) {
  141. $imageresize->addAttribute($size.'Thumb', $config[$size.'Thumb']);
  142. }
  143. }
  144. return true ;
  145. }
  146. /**
  147. * @access public
  148. */
  149. function onBeforeExecuteCommand( &$command )
  150. {
  151. if ( $command == 'ImageResize' )
  152. {
  153. $this->sendResponse();
  154. return false;
  155. }
  156. return true ;
  157. }
  158. }
  159. class CKFinder_Connector_CommandHandler_ImageResizeInfo extends CKFinder_Connector_CommandHandler_XmlCommandHandlerBase
  160. {
  161. /**
  162. * handle request and build XML
  163. * @access protected
  164. *
  165. */
  166. function buildXml()
  167. {
  168. $this->checkConnector();
  169. $this->checkRequest();
  170. if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_VIEW)) {
  171. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
  172. }
  173. $resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
  174. if (!isset($_GET["fileName"])) {
  175. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_NAME);
  176. }
  177. $fileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_GET["fileName"]);
  178. if (!CKFinder_Connector_Utils_FileSystem::checkFileName($fileName) || $resourceTypeInfo->checkIsHiddenFile($fileName)) {
  179. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  180. }
  181. if (!$resourceTypeInfo->checkExtension($fileName, false)) {
  182. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  183. }
  184. $filePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $fileName);
  185. if (!file_exists($filePath) || !is_file($filePath)) {
  186. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND);
  187. }
  188. list($width, $height) = getimagesize($filePath);
  189. $oNode = new Ckfinder_Connector_Utils_XmlNode("ImageInfo");
  190. $oNode->addAttribute("width", $width);
  191. $oNode->addAttribute("height", $height);
  192. $this->_connectorNode->addChild($oNode);
  193. }
  194. /**
  195. * @access public
  196. */
  197. function onBeforeExecuteCommand( &$command )
  198. {
  199. if ( $command == 'ImageResizeInfo' )
  200. {
  201. $this->sendResponse();
  202. return false;
  203. }
  204. return true ;
  205. }
  206. }
  207. if (function_exists('imagecreate')) {
  208. $CommandHandler_ImageResize = new CKFinder_Connector_CommandHandler_ImageResize();
  209. $CommandHandler_ImageResizeInfo = new CKFinder_Connector_CommandHandler_ImageResizeInfo();
  210. $config['Hooks']['BeforeExecuteCommand'][] = array($CommandHandler_ImageResize, "onBeforeExecuteCommand");
  211. $config['Hooks']['BeforeExecuteCommand'][] = array($CommandHandler_ImageResizeInfo, "onBeforeExecuteCommand");
  212. $config['Hooks']['InitCommand'][] = array($CommandHandler_ImageResize, "onInitCommand");
  213. $config['Plugins'][] = 'imageresize';
  214. }