plugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * elFinder Plugin Normalizer
  4. *
  5. * UTF-8 Normalizer of file-name and file-path etc.
  6. * nfc(NFC): Canonical Decomposition followed by Canonical Composition
  7. * nfkc(NFKC): Compatibility Decomposition followed by Canonical
  8. *
  9. * This plugin require Class "Normalizer" (PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
  10. * or PEAR package "I18N_UnicodeNormalizer"
  11. *
  12. * ex. binding, configure on connector options
  13. * $opts = array(
  14. * 'bind' => array(
  15. * 'upload.pre mkdir.pre mkfile.pre rename.pre archive.pre ls.pre' => array(
  16. * 'Plugin.Normalizer.cmdPreprocess'
  17. * ),
  18. * 'upload.presave' => array(
  19. * 'Plugin.Normalizer.onUpLoadPreSave'
  20. * )
  21. * ),
  22. * // global configure (optional)
  23. * 'plugin' => array(
  24. * 'Normalizer' => array(
  25. * 'enable' => true,
  26. * 'nfc' => true,
  27. * 'nfkc' => true,
  28. * 'umlauts' => false,
  29. * 'lowercase' => false,
  30. * 'convmap' => array()
  31. * )
  32. * ),
  33. * // each volume configure (optional)
  34. * 'roots' => array(
  35. * array(
  36. * 'driver' => 'LocalFileSystem',
  37. * 'path' => '/path/to/files/',
  38. * 'URL' => 'http://localhost/to/files/'
  39. * 'plugin' => array(
  40. * 'Normalizer' => array(
  41. * 'enable' => true,
  42. * 'nfc' => true,
  43. * 'nfkc' => true,
  44. * 'umlauts' => false,
  45. * 'lowercase' => false,
  46. * 'convmap' => array()
  47. * )
  48. * )
  49. * )
  50. * )
  51. * );
  52. *
  53. * @package elfinder
  54. * @author Naoki Sawada
  55. * @license New BSD
  56. */
  57. class elFinderPluginNormalizer extends elFinderPlugin
  58. {
  59. private $replaced = array();
  60. private $keyMap = array(
  61. 'ls' => 'intersect',
  62. 'upload' => 'renames',
  63. 'mkdir' => array('name', 'dirs')
  64. );
  65. public function __construct($opts) {
  66. $defaults = array(
  67. 'enable' => true, // For control by volume driver
  68. 'nfc' => true, // Canonical Decomposition followed by Canonical Composition
  69. 'nfkc' => true, // Compatibility Decomposition followed by Canonical
  70. 'umlauts' => false, // Convert umlauts with their closest 7 bit ascii equivalent
  71. 'lowercase' => false, // Make chars lowercase
  72. 'convmap' => array()// Convert map ('FROM' => 'TO') array
  73. );
  74. $this->opts = array_merge($defaults, $opts);
  75. }
  76. public function cmdPreprocess($cmd, &$args, $elfinder, $volume) {
  77. $opts = $this->getCurrentOpts($volume);
  78. if (! $opts['enable']) {
  79. return false;
  80. }
  81. $this->replaced[$cmd] = array();
  82. $key = (isset($this->keyMap[$cmd]))? $this->keyMap[$cmd] : 'name';
  83. if (is_array($key)) {
  84. $keys = $key;
  85. } else {
  86. $keys = array($key);
  87. }
  88. foreach($keys as $key) {
  89. if (isset($args[$key])) {
  90. if (is_array($args[$key])) {
  91. foreach($args[$key] as $i => $name) {
  92. if ($cmd === 'mkdir' && $key === 'dirs') {
  93. // $name need '/' as prefix see #2607
  94. $name = '/' . ltrim($name, '/');
  95. $_names = explode('/', $name);
  96. $_res = array();
  97. foreach($_names as $_name) {
  98. $_res[] = $this->normalize($_name, $opts);
  99. }
  100. $this->replaced[$cmd][$name] = $args[$key][$i] = join('/', $_res);
  101. } else {
  102. $this->replaced[$cmd][$name] = $args[$key][$i] = $this->normalize($name, $opts);
  103. }
  104. }
  105. } else if ($args[$key] !== '') {
  106. $name = $args[$key];
  107. $this->replaced[$cmd][$name] = $args[$key] = $this->normalize($name, $opts);
  108. }
  109. }
  110. }
  111. if ($cmd === 'ls' || $cmd === 'mkdir') {
  112. if (! empty($this->replaced[$cmd])) {
  113. // un-regist for legacy settings
  114. $elfinder->unbind($cmd, array($this, 'cmdPostprocess'));
  115. $elfinder->bind($cmd, array($this, 'cmdPostprocess'));
  116. }
  117. }
  118. return true;
  119. }
  120. public function cmdPostprocess($cmd, &$result, $args, $elfinder, $volume) {
  121. if ($cmd === 'ls') {
  122. if (! empty($result['list']) && ! empty($this->replaced['ls'])) {
  123. foreach($result['list'] as $hash => $name) {
  124. if ($keys = array_keys($this->replaced['ls'], $name)) {
  125. if (count($keys) === 1) {
  126. $result['list'][$hash] = $keys[0];
  127. } else {
  128. $result['list'][$hash] = $keys;
  129. }
  130. }
  131. }
  132. }
  133. } else if ($cmd === 'mkdir') {
  134. if (! empty($result['hashes']) && ! empty($this->replaced['mkdir'])) {
  135. foreach($result['hashes'] as $name => $hash) {
  136. if ($keys = array_keys($this->replaced['mkdir'], $name)) {
  137. $result['hashes'][$keys[0]] = $hash;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. // NOTE: $thash is directory hash so it unneed to process at here
  144. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) {
  145. $opts = $this->getCurrentOpts($volume);
  146. if (! $opts['enable']) {
  147. return false;
  148. }
  149. $name = $this->normalize($name, $opts);
  150. return true;
  151. }
  152. protected function normalize($str, $opts) {
  153. if ($opts['nfc'] || $opts['nfkc']) {
  154. if (class_exists('Normalizer', false)) {
  155. if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C))
  156. $str = Normalizer::normalize($str, Normalizer::FORM_C);
  157. if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC))
  158. $str = Normalizer::normalize($str, Normalizer::FORM_KC);
  159. } else {
  160. if (! class_exists('I18N_UnicodeNormalizer', false)) {
  161. include_once 'I18N/UnicodeNormalizer.php';
  162. }
  163. if (class_exists('I18N_UnicodeNormalizer', false)) {
  164. $normalizer = new I18N_UnicodeNormalizer();
  165. if ($opts['nfc'])
  166. $str = $normalizer->normalize($str, 'NFC');
  167. if ($opts['nfkc'])
  168. $str = $normalizer->normalize($str, 'NFKC');
  169. }
  170. }
  171. }
  172. if ($opts['umlauts']) {
  173. if (strpos($str = htmlentities($str, ENT_QUOTES, 'UTF-8'), '&') !== false) {
  174. $str = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $str), ENT_QUOTES, 'utf-8');
  175. }
  176. }
  177. if ($opts['convmap'] && is_array($opts['convmap'])) {
  178. $str = strtr($str, $opts['convmap']);
  179. }
  180. if ($opts['lowercase']) {
  181. if (function_exists('mb_strtolower')) {
  182. $str = mb_strtolower($str, 'UTF-8');
  183. } else {
  184. $str = strtolower($str);
  185. }
  186. }
  187. return $str;
  188. }
  189. }