plugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * 'ls' => array(
  19. * 'Plugin.Normalizer.cmdPostprocess'
  20. * ),
  21. * 'upload.presave' => array(
  22. * 'Plugin.Normalizer.onUpLoadPreSave'
  23. * )
  24. * ),
  25. * // global configure (optional)
  26. * 'plugin' => array(
  27. * 'Normalizer' => array(
  28. * 'enable' => true,
  29. * 'nfc' => true,
  30. * 'nfkc' => true,
  31. * 'umlauts' => false,
  32. * 'lowercase' => false,
  33. * 'convmap' => array()
  34. * )
  35. * ),
  36. * // each volume configure (optional)
  37. * 'roots' => array(
  38. * array(
  39. * 'driver' => 'LocalFileSystem',
  40. * 'path' => '/path/to/files/',
  41. * 'URL' => 'http://localhost/to/files/'
  42. * 'plugin' => array(
  43. * 'Normalizer' => array(
  44. * 'enable' => true,
  45. * 'nfc' => true,
  46. * 'nfkc' => true,
  47. * 'umlauts' => false,
  48. * 'lowercase' => false,
  49. * 'convmap' => array()
  50. * )
  51. * )
  52. * )
  53. * )
  54. * );
  55. *
  56. * @package elfinder
  57. * @author Naoki Sawada
  58. * @license New BSD
  59. */
  60. class elFinderPluginNormalizer extends elFinderPlugin
  61. {
  62. private $replaced = array();
  63. private $keyMap = array(
  64. 'ls' => 'intersect',
  65. 'upload' => 'renames'
  66. );
  67. public function __construct($opts) {
  68. $defaults = array(
  69. 'enable' => true, // For control by volume driver
  70. 'nfc' => true, // Canonical Decomposition followed by Canonical Composition
  71. 'nfkc' => true, // Compatibility Decomposition followed by Canonical
  72. 'umlauts' => false, // Convert umlauts with their closest 7 bit ascii equivalent
  73. 'lowercase' => false, // Make chars lowercase
  74. 'convmap' => array()// Convert map ('FROM' => 'TO') array
  75. );
  76. $this->opts = array_merge($defaults, $opts);
  77. }
  78. public function cmdPreprocess($cmd, &$args, $elfinder, $volume) {
  79. $opts = $this->getCurrentOpts($volume);
  80. if (! $opts['enable']) {
  81. return false;
  82. }
  83. $this->replaced[$cmd] = array();
  84. $key = (isset($this->keyMap[$cmd]))? $this->keyMap[$cmd] : 'name';
  85. if (isset($args[$key])) {
  86. if (is_array($args[$key])) {
  87. foreach($args[$key] as $i => $name) {
  88. $this->replaced[$cmd][$name] = $args[$key][$i] = $this->normalize($name, $opts);
  89. }
  90. } else {
  91. $name = $args[$key];
  92. $this->replaced[$cmd][$name] = $args[$key] = $this->normalize($name, $opts);
  93. }
  94. }
  95. return true;
  96. }
  97. public function cmdPostprocess($cmd, &$result, $args, $elfinder) {
  98. if ($cmd === 'ls') {
  99. if (! empty($result['list']) && ! empty($this->replaced['ls'])) {
  100. foreach($result['list'] as $hash => $name) {
  101. if ($keys = array_keys($this->replaced['ls'], $name)) {
  102. if (count($keys) === 1) {
  103. $result['list'][$hash] = $keys[0];
  104. } else {
  105. $result['list'][$hash] = $keys;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. // NOTE: $thash is directory hash so it unneed to process at here
  113. public function onUpLoadPreSave(&$thash, &$name, $src, $elfinder, $volume) {
  114. $opts = $this->getCurrentOpts($volume);
  115. if (! $opts['enable']) {
  116. return false;
  117. }
  118. $name = $this->normalize($name, $opts);
  119. return true;
  120. }
  121. private function normalize($str, $opts) {
  122. if ($opts['nfc'] || $opts['nfkc']) {
  123. if (class_exists('Normalizer', false)) {
  124. if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C))
  125. $str = Normalizer::normalize($str, Normalizer::FORM_C);
  126. if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC))
  127. $str = Normalizer::normalize($str, Normalizer::FORM_KC);
  128. } else {
  129. if (! class_exists('I18N_UnicodeNormalizer', false)) {
  130. include_once 'I18N/UnicodeNormalizer.php';
  131. }
  132. if (class_exists('I18N_UnicodeNormalizer', false)) {
  133. $normalizer = new I18N_UnicodeNormalizer();
  134. if ($opts['nfc'])
  135. $str = $normalizer->normalize($str, 'NFC');
  136. if ($opts['nfkc'])
  137. $str = $normalizer->normalize($str, 'NFKC');
  138. }
  139. }
  140. }
  141. if ($opts['umlauts']) {
  142. if (strpos($str = htmlentities($str, ENT_QUOTES, 'UTF-8'), '&') !== false) {
  143. $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');
  144. }
  145. }
  146. if ($opts['convmap'] && is_array($opts['convmap'])) {
  147. $str = strtr($str, $opts['convmap']);
  148. }
  149. if ($opts['lowercase']) {
  150. if (function_exists('mb_strtolower')) {
  151. $str = mb_strtolower($str, 'UTF-8');
  152. } else {
  153. $str = strtolower($str);
  154. }
  155. }
  156. return $str;
  157. }
  158. }