PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Backend/Core/Js/ckfinder/core/connector/php/php5/Core/ResourceTypeConfig.php

http://github.com/forkcms/forkcms
PHP | 367 lines | 169 code | 37 blank | 161 comment | 34 complexity | 85fa41a243291417762d91ea4cdb1441 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://cksource.com/ckfinder
  6. * Copyright (C) 2007-2014, 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 Config
  17. * @copyright CKSource - Frederico Knabben
  18. */
  19. /**
  20. * This class keeps resource types configuration
  21. *
  22. * @package CKFinder
  23. * @subpackage Config
  24. * @copyright CKSource - Frederico Knabben
  25. */
  26. class CKFinder_Connector_Core_ResourceTypeConfig
  27. {
  28. /**
  29. * Resource name
  30. *
  31. * @var string
  32. * @access private
  33. */
  34. private $_name = "";
  35. /**
  36. * Resource url
  37. *
  38. * @var string
  39. * @access private
  40. */
  41. private $_url = "";
  42. /**
  43. * Directory path on a server
  44. *
  45. * @var string
  46. * @access private
  47. */
  48. private $_directory = "";
  49. /**
  50. * Max size
  51. *
  52. * @var unknown_type
  53. * @access private
  54. */
  55. private $_maxSize = 0;
  56. /**
  57. * Array with allowed extensions
  58. *
  59. * @var array[]string
  60. * @access private
  61. */
  62. private $_allowedExtensions = array();
  63. /**
  64. * Array with denied extensions
  65. *
  66. * @var array[]string
  67. * @access private
  68. */
  69. private $_deniedExtensions = array();
  70. /**
  71. * used for CKFinder_Connector_Core_Config object caching
  72. *
  73. * @var CKFinder_Connector_Core_Config
  74. * @access private
  75. */
  76. private $_config;
  77. /**
  78. * Get ResourceType configuration
  79. *
  80. * @param string $resourceTypeNode
  81. * @return array
  82. *
  83. */
  84. function __construct($resourceTypeNode)
  85. {
  86. if (isset($resourceTypeNode["name"])) {
  87. $this->_name = $resourceTypeNode["name"];
  88. }
  89. if (isset($resourceTypeNode["url"])) {
  90. $this->_url = $resourceTypeNode["url"];
  91. }
  92. if (!strlen($this->_url)) {
  93. $this->_url = "/";
  94. }
  95. else if(substr($this->_url,-1,1) != "/") {
  96. $this->_url .= "/";
  97. }
  98. if (isset($resourceTypeNode["maxSize"])) {
  99. $this->_maxSize = CKFinder_Connector_Utils_Misc::returnBytes((string)$resourceTypeNode["maxSize"]);
  100. }
  101. if (isset($resourceTypeNode["directory"])) {
  102. $this->_directory = $resourceTypeNode["directory"];
  103. }
  104. if (!strlen($this->_directory)) {
  105. $this->_directory = resolveUrl($this->_url);
  106. }
  107. if (isset($resourceTypeNode["allowedExtensions"])) {
  108. if (is_array($resourceTypeNode["allowedExtensions"])) {
  109. foreach ($resourceTypeNode["allowedExtensions"] as $e) {
  110. $this->_allowedExtensions[] = strtolower(trim((string)$e));
  111. }
  112. }
  113. else {
  114. $resourceTypeNode["allowedExtensions"] = trim((string)$resourceTypeNode["allowedExtensions"]);
  115. if (strlen($resourceTypeNode["allowedExtensions"])) {
  116. $extensions = explode(",", $resourceTypeNode["allowedExtensions"]);
  117. foreach ($extensions as $e) {
  118. $this->_allowedExtensions[] = strtolower(trim($e));
  119. }
  120. }
  121. }
  122. }
  123. if (isset($resourceTypeNode["deniedExtensions"])) {
  124. if (is_array($resourceTypeNode["deniedExtensions"])) {
  125. foreach ($resourceTypeNode["deniedExtensions"] as $extension) {
  126. $this->_deniedExtensions[] = strtolower(trim((string)$e));
  127. }
  128. }
  129. else {
  130. $resourceTypeNode["deniedExtensions"] = trim((string)$resourceTypeNode["deniedExtensions"]);
  131. if (strlen($resourceTypeNode["deniedExtensions"])) {
  132. $extensions = explode(",", $resourceTypeNode["deniedExtensions"]);
  133. foreach ($extensions as $e) {
  134. $this->_deniedExtensions[] = strtolower(trim($e));
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Get name
  142. *
  143. * @access public
  144. * @return string
  145. */
  146. public function getName()
  147. {
  148. return $this->_name;
  149. }
  150. /**
  151. * Get url
  152. *
  153. * @access public
  154. * @return string
  155. */
  156. public function getUrl()
  157. {
  158. return $this->_url;
  159. }
  160. /**
  161. * Get directory
  162. *
  163. * @access public
  164. * @return string
  165. */
  166. public function getDirectory()
  167. {
  168. return $this->_directory;
  169. }
  170. /**
  171. * Get max size
  172. *
  173. * @access public
  174. * @return int
  175. */
  176. public function getMaxSize()
  177. {
  178. return $this->_maxSize;
  179. }
  180. /**
  181. * Get allowed extensions
  182. *
  183. * @access public
  184. * @return array[]string
  185. */
  186. public function getAllowedExtensions()
  187. {
  188. return $this->_allowedExtensions;
  189. }
  190. /**
  191. * Get denied extensions
  192. *
  193. * @access public
  194. * @return array[]string
  195. */
  196. public function getDeniedExtensions()
  197. {
  198. return $this->_deniedExtensions;
  199. }
  200. /**
  201. * Check extension, return true if file name is valid.
  202. * Return false if extension is on denied list.
  203. * If allowed extensions are defined, return false if extension isn't on allowed list.
  204. *
  205. * @access public
  206. * @param string $extension extension
  207. * @param boolean $renameIfRequired whether try to rename file or not
  208. * @return boolean
  209. */
  210. public function checkExtension(&$fileName, $renameIfRequired = true)
  211. {
  212. if (strpos($fileName, '.') === false) {
  213. return true;
  214. }
  215. if (is_null($this->_config)) {
  216. $this->_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  217. }
  218. if ($this->_config->getCheckDoubleExtension()) {
  219. $pieces = explode('.', $fileName);
  220. // First, check the last extension (ex. in file.php.jpg, the "jpg").
  221. if ( !$this->checkSingleExtension( $pieces[sizeof($pieces)-1] ) ) {
  222. return false;
  223. }
  224. if ($renameIfRequired) {
  225. // Check the other extensions, rebuilding the file name. If an extension is
  226. // not allowed, replace the dot with an underscore.
  227. $fileName = $pieces[0] ;
  228. for ($i=1; $i<sizeof($pieces)-1; $i++) {
  229. $fileName .= $this->checkSingleExtension( $pieces[$i] ) ? '.' : '_' ;
  230. $fileName .= $pieces[$i];
  231. }
  232. // Add the last extension to the final name.
  233. $fileName .= '.' . $pieces[sizeof($pieces)-1] ;
  234. }
  235. }
  236. else {
  237. // Check only the last extension (ex. in file.php.jpg, only "jpg").
  238. return $this->checkSingleExtension( substr($fileName, strrpos($fileName,'.')+1) );
  239. }
  240. return true;
  241. }
  242. /**
  243. * Check given folder name
  244. * Return true if folder name matches hidden folder names list
  245. *
  246. * @param string $folderName
  247. * @access public
  248. * @return boolean
  249. */
  250. public function checkIsHiddenFolder($folderName)
  251. {
  252. if (is_null($this->_config)) {
  253. $this->_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  254. }
  255. $regex = $this->_config->getHideFoldersRegex();
  256. if ($regex) {
  257. return preg_match($regex, $folderName);
  258. }
  259. return false;
  260. }
  261. /**
  262. * Check given file name
  263. * Return true if file name matches hidden file names list
  264. *
  265. * @param string $fileName
  266. * @access public
  267. * @return boolean
  268. */
  269. public function checkIsHiddenFile($fileName)
  270. {
  271. if (is_null($this->_config)) {
  272. $this->_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  273. }
  274. $regex = $this->_config->getHideFilesRegex();
  275. if ($regex) {
  276. return preg_match($regex, $fileName);
  277. }
  278. return false;
  279. }
  280. /**
  281. * Check given path
  282. * Return true if path contains folder name that matches hidden folder names list
  283. *
  284. * @param string $folderName
  285. * @access public
  286. * @return boolean
  287. */
  288. public function checkIsHiddenPath($path)
  289. {
  290. $_clientPathParts = explode("/", trim($path, "/"));
  291. if ($_clientPathParts) {
  292. foreach ($_clientPathParts as $_part) {
  293. if ($this->checkIsHiddenFolder($_part)) {
  294. return true;
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. /**
  301. * Check if extension is allowed
  302. * Return true if the extension is allowed.
  303. *
  304. * @param string $extension
  305. * @access public
  306. * @return boolean
  307. */
  308. public function checkSingleExtension($extension)
  309. {
  310. $extension = strtolower(ltrim($extension,'.'));
  311. if (sizeof($this->_deniedExtensions)) {
  312. if (in_array($extension, $this->_deniedExtensions)) {
  313. return false;
  314. }
  315. }
  316. if (sizeof($this->_allowedExtensions)) {
  317. return in_array($extension, $this->_allowedExtensions);
  318. }
  319. return true;
  320. }
  321. /**
  322. * Generate hash for current resource type
  323. *
  324. * @access public
  325. * @return string 16 digit hash
  326. */
  327. public function getHash(){
  328. return substr(md5($this->getDirectory()), 0, 16);
  329. }
  330. }