/default_www/backend/core/js/tiny_mce/plugins/imagemanager/classes/FileSystems/FileFilter.php

https://github.com/zakgrant/forkcms · PHP · 467 lines · 236 code · 69 blank · 162 comment · 81 complexity · 8bc0fec393f7c5fc50f6b782f618ab59 MD5 · raw file

  1. <?php
  2. /**
  3. * $Id: FileFilter.php 539 2008-10-30 13:16:58Z spocke $
  4. *
  5. * @package MCFileManager.filesystems
  6. * @author Moxiecode
  7. * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
  8. */
  9. /**
  10. * This class is the base FileFilter class and is to be extended by all custom FileFilter implementations.
  11. *
  12. * @package mce.core
  13. */
  14. class Moxiecode_FileFilter {
  15. /**
  16. * Returns true or false if the file is accepted or not.
  17. *
  18. * @param MCE_File $file File to grant or deny.
  19. * @return boolean true or false if the file is accepted or not.
  20. */
  21. function accept(&$file) {
  22. // code here...
  23. }
  24. }
  25. /**
  26. * Combines multiple filters into one filter.
  27. *
  28. * @package mce.core
  29. */
  30. class Moxiecode_CombinedFileFilter {
  31. var $_filters;
  32. /**
  33. * Constructs a new combined filer.
  34. */
  35. function Moxiecode_CombinedFileFilter() {
  36. $this->_filters = array();
  37. }
  38. /**
  39. * Adds a new filter to check.
  40. *
  41. * @param Moxiecode_FileFilter $file_filter Filter to add.
  42. */
  43. function addFilter(&$file_filter) {
  44. $this->_filters[] = $file_filter;
  45. }
  46. /**
  47. * Returns true or false if the file is accepted or not.
  48. *
  49. * @param MCE_File $file File to grant or deny.
  50. * @return boolean true or false if the file is accepted or not.
  51. */
  52. function accept(&$file) {
  53. for ($i = 0; $i < count($this->_filters); $i++) {
  54. $state = $this->_filters[$i]->accept($file);
  55. if ($state < 0)
  56. return $state;
  57. }
  58. return 1;
  59. }
  60. }
  61. /**
  62. * DummyFileFiler this filter accepts all files.
  63. *
  64. * @package mce.core
  65. */
  66. class Moxiecode_DummyFileFilter extends Moxiecode_FileFilter {
  67. /**
  68. * Returns true or false if the file is accepted or not.
  69. * Note: This dummb method allways returns true.
  70. *
  71. * @param MCE_File $file File to grant or deny.
  72. * @return boolean true or false if the file is accepted or not.
  73. */
  74. function accept(&$file) {
  75. return true;
  76. }
  77. }
  78. // Define reason constants
  79. define('BASIC_FILEFILTER_ACCEPTED', 1);
  80. define('BASIC_FILEFILTER_INVALID_EXTENSION', -1);
  81. define('BASIC_FILEFILTER_INVALID_NAME', -2);
  82. /**
  83. * Basic file filter, this class handles some common filter problems
  84. * and is possible to extend if needed.
  85. *
  86. * @package mce.core
  87. */
  88. class Moxiecode_BasicFileFilter extends Moxiecode_FileFilter {
  89. /**#@+
  90. * @access private
  91. */
  92. var $_excludeFolders;
  93. var $_includeFolders;
  94. var $_excludeFiles;
  95. var $_includeFiles;
  96. var $_includeFilePattern;
  97. var $_excludeFilePattern;
  98. var $_includeDirectoryPattern;
  99. var $_excludeDirectoryPattern;
  100. var $_filesOnly;
  101. var $_dirsOnly;
  102. var $_includeWildcardPattern;
  103. var $_excludeWildcardPattern;
  104. var $_extensions;
  105. var $_maxLevels;
  106. var $_debug;
  107. /**#@+
  108. * @access public
  109. */
  110. /**
  111. * Main constructor.
  112. */
  113. function BasicFileFilter() {
  114. $this->_debug = false;
  115. $this->_extensions = "";
  116. }
  117. /**
  118. * Sets if debug mode is on or off, default off.
  119. *
  120. * @param boolean $state if true debug mode is enabled.
  121. */
  122. function setDebugMode($state) {
  123. $this->_debug = $state;
  124. }
  125. /**
  126. * Sets if only files are to be accepted in result.
  127. *
  128. * @param boolean $files_only True if only files are to be accepted.
  129. */
  130. function setOnlyFiles($files_only) {
  131. $this->_filesOnly = $files_only;
  132. }
  133. /**
  134. * Sets if only dirs are to be accepted in result.
  135. *
  136. * @param boolean $dirs_only True if only dirs are to be accepted.
  137. */
  138. function setOnlyDirs($dirs_only) {
  139. $this->_dirsOnly = $dirs_only;
  140. }
  141. /**
  142. * Sets maximum number of directory levels to accept.
  143. *
  144. * @param int $max_levels Maximum number of directory levels to accept.
  145. */
  146. function setMaxLevels($max_levels) {
  147. $this->_maxLevels = $max_levels;
  148. }
  149. /**
  150. * Sets a comma separated list of valid file extensions.
  151. *
  152. * @param String $extensions Comma separated list of valid file extensions.
  153. */
  154. function setIncludeExtensions($extensions) {
  155. if ($extensions == "*" || $extensions == "")
  156. return;
  157. $this->_extensions = explode(',', strtolower($extensions));
  158. }
  159. /**
  160. * Sets comma separated string list of filenames to exclude.
  161. *
  162. * @param String $files separated string list of filenames to exclude.
  163. */
  164. function setExcludeFiles($files) {
  165. if ($files != "")
  166. $this->_excludeFiles = split(',', $files);
  167. }
  168. /**
  169. * Sets comma separated string list of filenames to include.
  170. *
  171. * @param String $files separated string list of filenames to include.
  172. */
  173. function setIncludeFiles($files) {
  174. if ($files != "")
  175. $this->_includeFiles = split(',', $files);
  176. }
  177. /**
  178. * Sets comma separated string list of foldernames to exclude.
  179. *
  180. * @param String $folders separated string list of foldernames to exclude.
  181. */
  182. function setExcludeFolders($folders) {
  183. if ($folders != "")
  184. $this->_excludeFolders = split(',', $folders);
  185. }
  186. /**
  187. * Sets comma separated string list of foldernames to include.
  188. *
  189. * @param String $folders separated string list of foldernames to include.
  190. */
  191. function setIncludeFolders($folders) {
  192. if ($folders != "")
  193. $this->_includeFolders = split(',', $folders);
  194. }
  195. /**
  196. * Sets a regexp pattern that is used to accept files path parts.
  197. *
  198. * @param String $pattern regexp pattern that is used to accept files path parts.
  199. */
  200. function setIncludeFilePattern($pattern) {
  201. $this->_includeFilePattern = $pattern;
  202. }
  203. /**
  204. * Sets a regexp pattern that is used to deny files path parts.
  205. *
  206. * @param String $pattern regexp pattern that is used to deny files path parts.
  207. */
  208. function setExcludeFilePattern($pattern) {
  209. $this->_excludeFilePattern = $pattern;
  210. }
  211. /**
  212. * Sets a regexp pattern that is used to accept directory path parts.
  213. *
  214. * @param String $pattern regexp pattern that is used to accept directory path parts.
  215. */
  216. function setIncludeDirectoryPattern($pattern) {
  217. $this->_includeDirectoryPattern = $pattern;
  218. }
  219. /**
  220. * Sets a regexp pattern that is used to deny directory path parts.
  221. *
  222. * @param String $pattern regexp pattern that is used to deny directory path parts.
  223. */
  224. function setExcludeDirectoryPattern($pattern) {
  225. $this->_excludeDirectoryPattern = $pattern;
  226. }
  227. /**
  228. * Sets a wildcard pattern that is used to accept files path parts.
  229. *
  230. * @param String $pattern wildcard pattern that is used to accept files path parts.
  231. */
  232. function setIncludeWildcardPattern($pattern) {
  233. if ($pattern != "")
  234. $this->_includeWildcardPattern = $pattern;
  235. }
  236. /**
  237. * Sets a wildcard pattern that is used to deny files path parts.
  238. *
  239. * @param String $pattern wildcard pattern that is used to deny files path parts.
  240. */
  241. function setExcludeWildcardPattern($pattern) {
  242. if ($pattern != "")
  243. $this->_excludeWildcardPattern = $pattern;
  244. }
  245. /**
  246. * Returns true or false if the file is accepted or not.
  247. *
  248. * @param MCE_File $file File to grant or deny.
  249. * @return boolean true or false if the file is accepted or not.
  250. */
  251. function accept(&$file) {
  252. $name = $file->getName();
  253. $absPath = $file->getAbsolutePath();
  254. $isFile = $file->isFile();
  255. // Handle exclude folders
  256. if (is_array($this->_excludeFolders)) {
  257. foreach ($this->_excludeFolders as $folder) {
  258. if (strpos($absPath, $folder) != "") {
  259. if ($this->_debug)
  260. debug("File denied \"" . $absPath . "\" by \"excludeFolders\".");
  261. return BASIC_FILEFILTER_INVALID_NAME;
  262. }
  263. }
  264. }
  265. // Handle include folders
  266. if (is_array($this->_includeFolders)) {
  267. $state = false;
  268. foreach ($this->_includeFolders as $folder) {
  269. if (strpos($absPath, $folder) != "") {
  270. $state = true;
  271. break;
  272. }
  273. }
  274. if (!$state) {
  275. if ($this->_debug)
  276. debug("File \"" . $absPath . "\" denied by \"includeFolders\".");
  277. return BASIC_FILEFILTER_INVALID_NAME;
  278. }
  279. }
  280. // Handle exclude files
  281. if (is_array($this->_excludeFiles) && $isFile) {
  282. foreach ($this->_excludeFiles as $fileName) {
  283. if ($name == $fileName) {
  284. if ($this->_debug)
  285. debug("File \"" . $absPath . "\" denied by \"excludeFiles\".");
  286. return BASIC_FILEFILTER_INVALID_NAME;
  287. }
  288. }
  289. }
  290. // Handle include files
  291. if (is_array($this->_includeFiles) && $isFile) {
  292. $state = false;
  293. foreach ($this->_includeFiles as $fileName) {
  294. if ($name == $fileName) {
  295. $state = true;
  296. break;
  297. }
  298. }
  299. if (!$state) {
  300. if ($this->_debug)
  301. debug("File \"" . $absPath . "\" denied by \"includeFiles\".");
  302. return BASIC_FILEFILTER_INVALID_NAME;
  303. }
  304. }
  305. // Handle file patterns
  306. if ($isFile) {
  307. if ($this->_dirsOnly) {
  308. if ($this->_debug)
  309. debug("File denied \"" . $absPath . "\" by \"dirsOnly\".");
  310. return BASIC_FILEFILTER_INVALID_NAME;
  311. }
  312. // Handle exclude pattern
  313. if ($this->_excludeFilePattern && preg_match($this->_excludeFilePattern, $name)) {
  314. if ($this->_debug)
  315. debug("File \"" . $absPath . "\" denied by \"excludeFilePattern\".");
  316. return BASIC_FILEFILTER_INVALID_NAME;
  317. }
  318. // Handle include pattern
  319. if ($this->_includeFilePattern && !preg_match($this->_includeFilePattern, $name)) {
  320. if ($this->_debug)
  321. debug("File \"" . $absPath . "\" denied by \"includeFilePattern\".");
  322. return BASIC_FILEFILTER_INVALID_NAME;
  323. }
  324. } else {
  325. if ($this->_filesOnly) {
  326. if ($this->_debug)
  327. debug("Dir denied \"" . $absPath . "\" by \"filesOnly\".");
  328. return BASIC_FILEFILTER_INVALID_NAME;
  329. }
  330. // Handle exclude pattern
  331. if ($this->_excludeDirectoryPattern && preg_match($this->_excludeDirectoryPattern, $name)) {
  332. if ($this->_debug)
  333. debug("File \"" . $absPath . "\" denied by \"excludeDirectoryPattern\".");
  334. return BASIC_FILEFILTER_INVALID_NAME;
  335. }
  336. // Handle include pattern
  337. if ($this->_includeDirectoryPattern && !preg_match($this->_includeDirectoryPattern, $name)) {
  338. if ($this->_debug)
  339. debug("File \"" . $absPath . "\" denied by \"includeDirectoryPattern\".");
  340. return BASIC_FILEFILTER_INVALID_NAME;
  341. }
  342. }
  343. // Handle include wildcard pattern
  344. if ($this->_includeWildcardPattern && !$this->_fnmatch($this->_includeWildcardPattern, $name)) {
  345. if ($this->_debug)
  346. debug("File \"" . $absPath . "\" denied by \"includeWildcardPattern\".");
  347. return BASIC_FILEFILTER_INVALID_NAME;
  348. }
  349. // Handle exclude wildcard pattern
  350. if ($this->_excludeWildcardPattern && $this->_fnmatch($this->_excludeWildcardPattern, $name)) {
  351. if ($this->_debug)
  352. debug("File \"" . $absPath . "\" denied by \"excludeWildcardPattern\".");
  353. return BASIC_FILEFILTER_INVALID_NAME;
  354. }
  355. // Handle file exntetion pattern
  356. if (is_array($this->_extensions) && $isFile) {
  357. $ar = explode('.', $absPath);
  358. $ext = strtolower(array_pop($ar));
  359. $valid = false;
  360. foreach ($this->_extensions as $extension) {
  361. if ($extension == $ext) {
  362. $valid = true;
  363. break;
  364. }
  365. }
  366. if (!$valid)
  367. return BASIC_FILEFILTER_INVALID_EXTENSION;
  368. }
  369. return BASIC_FILEFILTER_ACCEPTED;
  370. }
  371. function _fnmatch($pattern, $file) {
  372. return ereg($this->_fnmatch2regexp(strtolower($pattern)), strtolower($file));
  373. }
  374. function _fnmatch2regexp($str) {
  375. $s = "";
  376. for ($i = 0; $i<strlen($str); $i++) {
  377. $c = $str{$i};
  378. if ($c =='?')
  379. $s .= '.'; // any character
  380. else if ($c == '*')
  381. $s .= '.*'; // 0 or more any characters
  382. else if ($c == '[' || $c == ']')
  383. $s .= $c; // one of characters within []
  384. else
  385. $s .= '\\' . $c;
  386. }
  387. $s = '^' . $s . '$';
  388. //trim redundant ^ or $
  389. //eg ^.*\.txt$ matches exactly the same as \.txt$
  390. if (substr($s,0,3) == "^.*")
  391. $s = substr($s, 3);
  392. if (substr($s,-3,3) == ".*$")
  393. $s = substr($s, 0, -3);
  394. return $s;
  395. }
  396. /**#@-*/
  397. }
  398. ?>