plugin.php 7.8 KB

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