plugin.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * elFinder Plugin Sanitizer
  4. *
  5. * Sanitizer of file-name and file-path etc.
  6. *
  7. * ex. binding, configure on connector options
  8. * $opts = array(
  9. * 'bind' => array(
  10. * 'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array(
  11. * 'Plugin.Sanitizer.cmdPreprocess'
  12. * ),
  13. * 'upload.presave' => array(
  14. * 'Plugin.Sanitizer.onUpLoadPreSave'
  15. * )
  16. * ),
  17. * // global configure (optional)
  18. * 'plugin' => array(
  19. * 'Sanitizer' => array(
  20. * 'enable' => true,
  21. * 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars
  22. * 'replace' => '_', // replace to this
  23. * 'callBack' => null // Or @callable sanitize function
  24. * )
  25. * ),
  26. * // each volume configure (optional)
  27. * 'roots' => array(
  28. * array(
  29. * 'driver' => 'LocalFileSystem',
  30. * 'path' => '/path/to/files/',
  31. * 'URL' => 'http://localhost/to/files/'
  32. * 'plugin' => array(
  33. * 'Sanitizer' => array(
  34. * 'enable' => true,
  35. * 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars
  36. * 'replace' => '_', // replace to this
  37. * 'callBack' => null // Or @callable sanitize function
  38. * )
  39. * )
  40. * )
  41. * )
  42. * );
  43. *
  44. * @package elfinder
  45. * @author Naoki Sawada
  46. * @license New BSD
  47. */
  48. class elFinderPluginSanitizer extends elFinderPlugin
  49. {
  50. private $replaced = array();
  51. private $keyMap = array(
  52. 'ls' => 'intersect',
  53. 'upload' => 'renames',
  54. 'mkdir' => array('name', 'dirs')
  55. );
  56. public function __construct($opts) {
  57. $defaults = array(
  58. 'enable' => true, // For control by volume driver
  59. 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars
  60. 'replace' => '_', // replace to this
  61. 'callBack' => null // Or callable sanitize function
  62. );
  63. $this->opts = array_merge($defaults, $opts);
  64. }
  65. public function cmdPreprocess($cmd, &$args, $elfinder, $volume) {
  66. $opts = $this->getCurrentOpts($volume);
  67. if (! $opts['enable']) {
  68. return false;
  69. }
  70. $this->replaced[$cmd] = array();
  71. $key = (isset($this->keyMap[$cmd]))? $this->keyMap[$cmd] : 'name';
  72. if (is_array($key)) {
  73. $keys = $key;
  74. } else {
  75. $keys = array($key);
  76. }
  77. foreach($keys as $key) {
  78. if (isset($args[$key])) {
  79. if (is_array($args[$key])) {
  80. foreach($args[$key] as $i => $name) {
  81. if ($cmd === 'mkdir' && $key === 'dirs') {
  82. // $name need '/' as prefix see #2607
  83. $name = '/' . ltrim($name, '/');
  84. $_names = explode('/', $name);
  85. $_res = array();
  86. foreach($_names as $_name) {
  87. $_res[] = $this->sanitizeFileName($_name, $opts);
  88. }
  89. $this->replaced[$cmd][$name] = $args[$key][$i] = join('/', $_res);
  90. } else {
  91. $this->replaced[$cmd][$name] = $args[$key][$i] = $this->sanitizeFileName($name, $opts);
  92. }
  93. }
  94. } else if ($args[$key] !== '') {
  95. $name = $args[$key];
  96. $this->replaced[$cmd][$name] = $args[$key] = $this->sanitizeFileName($name, $opts);
  97. }
  98. }
  99. }
  100. if ($cmd === 'ls' || $cmd === 'mkdir') {
  101. if (! empty($this->replaced[$cmd])) {
  102. // un-regist for legacy settings
  103. $elfinder->unbind($cmd, array($this, 'cmdPostprocess'));
  104. $elfinder->bind($cmd, array($this, 'cmdPostprocess'));
  105. }
  106. }
  107. return true;
  108. }
  109. public function cmdPostprocess($cmd, &$result, $args, $elfinder, $volume) {
  110. if ($cmd === 'ls') {
  111. if (! empty($result['list']) && ! empty($this->replaced['ls'])) {
  112. foreach($result['list'] as $hash => $name) {
  113. if ($keys = array_keys($this->replaced['ls'], $name)) {
  114. if (count($keys) === 1) {
  115. $result['list'][$hash] = $keys[0];
  116. } else {
  117. $result['list'][$hash] = $keys;
  118. }
  119. }
  120. }
  121. }
  122. } else if ($cmd === 'mkdir') {
  123. if (! empty($result['hashes']) && ! empty($this->replaced['mkdir'])) {
  124. foreach($result['hashes'] as $name => $hash) {
  125. if ($keys = array_keys($this->replaced['mkdir'], $name)) {
  126. $result['hashes'][$keys[0]] = $hash;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. // NOTE: $thash is directory hash so it unneed to process at here
  133. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) {
  134. $opts = $this->getCurrentOpts($volume);
  135. if (! $opts['enable']) {
  136. return false;
  137. }
  138. $name = $this->sanitizeFileName($name, $opts);
  139. return true;
  140. }
  141. protected function sanitizeFileName($filename, $opts) {
  142. if(!empty($opts['callBack']) && is_callable($opts['callBack'])) {
  143. return call_user_func_array($opts['callBack'], array($filename, $opts));
  144. }
  145. return str_replace($opts['targets'], $opts['replace'], $filename);
  146. }
  147. }