plugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * elFinder Plugin Sanitizer
  4. * Sanitizer of file-name and file-path etc.
  5. * ex. binding, configure on connector options
  6. * $opts = array(
  7. * 'bind' => array(
  8. * 'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array(
  9. * 'Plugin.Sanitizer.cmdPreprocess'
  10. * ),
  11. * 'upload.presave paste.copyfrom' => array(
  12. * 'Plugin.Sanitizer.onUpLoadPreSave'
  13. * )
  14. * ),
  15. * // global configure (optional)
  16. * 'plugin' => array(
  17. * 'Sanitizer' => array(
  18. * 'enable' => true,
  19. * 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars
  20. * 'replace' => '_', // replace to this
  21. * 'callBack' => null // Or @callable sanitize function
  22. * )
  23. * ),
  24. * // each volume configure (optional)
  25. * 'roots' => array(
  26. * array(
  27. * 'driver' => 'LocalFileSystem',
  28. * 'path' => '/path/to/files/',
  29. * 'URL' => 'http://localhost/to/files/'
  30. * 'plugin' => array(
  31. * 'Sanitizer' => array(
  32. * 'enable' => true,
  33. * 'targets' => array('\\','/',':','*','?','"','<','>','|'), // target chars
  34. * 'replace' => '_', // replace to this
  35. * 'callBack' => null // Or @callable sanitize function
  36. * )
  37. * )
  38. * )
  39. * )
  40. * );
  41. *
  42. * @package elfinder
  43. * @author Naoki Sawada
  44. * @license New BSD
  45. */
  46. class elFinderPluginSanitizer extends elFinderPlugin
  47. {
  48. private $replaced = array();
  49. private $keyMap = array(
  50. 'ls' => 'intersect',
  51. 'upload' => 'renames',
  52. 'mkdir' => array('name', 'dirs')
  53. );
  54. public function __construct($opts)
  55. {
  56. $defaults = array(
  57. 'enable' => true, // For control by volume driver
  58. 'targets' => array('\\', '/', ':', '*', '?', '"', '<', '>', '|'), // target chars
  59. 'replace' => '_', // replace to this
  60. 'callBack' => null // Or callable sanitize function
  61. );
  62. $this->opts = array_merge($defaults, $opts);
  63. }
  64. public function cmdPreprocess($cmd, &$args, $elfinder, $volume)
  65. {
  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. {
  111. if ($cmd === 'ls') {
  112. if (!empty($result['list']) && !empty($this->replaced['ls'])) {
  113. foreach ($result['list'] as $hash => $name) {
  114. if ($keys = array_keys($this->replaced['ls'], $name)) {
  115. if (count($keys) === 1) {
  116. $result['list'][$hash] = $keys[0];
  117. } else {
  118. $result['list'][$hash] = $keys;
  119. }
  120. }
  121. }
  122. }
  123. } else if ($cmd === 'mkdir') {
  124. if (!empty($result['hashes']) && !empty($this->replaced['mkdir'])) {
  125. foreach ($result['hashes'] as $name => $hash) {
  126. if ($keys = array_keys($this->replaced['mkdir'], $name)) {
  127. $result['hashes'][$keys[0]] = $hash;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. // NOTE: $thash is directory hash so it unneed to process at here
  134. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume)
  135. {
  136. $opts = $this->getCurrentOpts($volume);
  137. if (!$opts['enable']) {
  138. return false;
  139. }
  140. $name = $this->sanitizeFileName($name, $opts);
  141. return true;
  142. }
  143. protected function sanitizeFileName($filename, $opts)
  144. {
  145. if (!empty($opts['callBack']) && is_callable($opts['callBack'])) {
  146. return call_user_func_array($opts['callBack'], array($filename, $opts));
  147. }
  148. return str_replace($opts['targets'], $opts['replace'], $filename);
  149. }
  150. }