PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/studio-42/elfinder/php/plugins/Normalizer/plugin.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 172 lines | 107 code | 9 blank | 56 comment | 31 complexity | 2cee2250466d26122b9eb4f853a3bb42 MD5 | raw file
  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. * 'lowercase' => false,
  32. * 'convmap' => array()
  33. * )
  34. * ),
  35. * // each volume configure (optional)
  36. * 'roots' => array(
  37. * array(
  38. * 'driver' => 'LocalFileSystem',
  39. * 'path' => '/path/to/files/',
  40. * 'URL' => 'http://localhost/to/files/'
  41. * 'plugin' => array(
  42. * 'Normalizer' => array(
  43. * 'enable' => true,
  44. * 'nfc' => true,
  45. * 'nfkc' => true,
  46. * 'lowercase' => false,
  47. * 'convmap' => array()
  48. * )
  49. * )
  50. * )
  51. * )
  52. * );
  53. *
  54. * @package elfinder
  55. * @author Naoki Sawada
  56. * @license New BSD
  57. */
  58. class elFinderPluginNormalizer
  59. {
  60. private $opts = array();
  61. private $replaced = array();
  62. private $keyMap = array(
  63. 'ls' => 'intersect',
  64. 'upload' => 'renames'
  65. );
  66. public function __construct($opts) {
  67. $defaults = array(
  68. 'enable' => true, // For control by volume driver
  69. 'nfc' => true, // Canonical Decomposition followed by Canonical Composition
  70. 'nfkc' => true, // Compatibility Decomposition followed by Canonical
  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->getOpts($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 (isset($args[$key])) {
  84. if (is_array($args[$key])) {
  85. foreach($args[$key] as $i => $name) {
  86. $this->replaced[$cmd][$name] = $args[$key][$i] = $this->normalize($name, $opts);
  87. }
  88. } else {
  89. $name = $args[$key];
  90. $this->replaced[$cmd][$name] = $args[$key] = $this->normalize($name, $opts);
  91. }
  92. }
  93. return true;
  94. }
  95. public function cmdPostprocess($cmd, &$result, $args, $elfinder) {
  96. if ($cmd === 'ls') {
  97. if (! empty($result['list']) && ! empty($this->replaced['ls'])) {
  98. foreach($result['list'] as $hash => $name) {
  99. if ($keys = array_keys($this->replaced['ls'], $name)) {
  100. if (count($keys) === 1) {
  101. $result['list'][$hash] = $keys[0];
  102. } else {
  103. $result['list'][$hash] = $keys;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) {
  111. $opts = $this->getOpts($volume);
  112. if (! $opts['enable']) {
  113. return false;
  114. }
  115. if ($path) {
  116. $path = $this->normalize($path, $opts);
  117. }
  118. $name = $this->normalize($name, $opts);
  119. return true;
  120. }
  121. private function getOpts($volume) {
  122. $opts = $this->opts;
  123. if (is_object($volume)) {
  124. $volOpts = $volume->getOptionsPlugin('Normalizer');
  125. if (is_array($volOpts)) {
  126. $opts = array_merge($this->opts, $volOpts);
  127. }
  128. }
  129. return $opts;
  130. }
  131. private function normalize($str, $opts) {
  132. if ($opts['nfc'] || $opts['nfkc']) {
  133. if (class_exists('Normalizer', false)) {
  134. if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C))
  135. $str = Normalizer::normalize($str, Normalizer::FORM_C);
  136. if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC))
  137. $str = Normalizer::normalize($str, Normalizer::FORM_KC);
  138. } else {
  139. if (! class_exists('I18N_UnicodeNormalizer', false)) {
  140. include_once 'I18N/UnicodeNormalizer.php';
  141. }
  142. if (class_exists('I18N_UnicodeNormalizer', false)) {
  143. $normalizer = new I18N_UnicodeNormalizer();
  144. if ($opts['nfc'])
  145. $str = $normalizer->normalize($str, 'NFC');
  146. if ($opts['nfkc'])
  147. $str = $normalizer->normalize($str, 'NFKC');
  148. }
  149. }
  150. }
  151. if ($opts['convmap'] && is_array($opts['convmap'])) {
  152. $str = strtr($str, $opts['convmap']);
  153. }
  154. if ($opts['lowercase']) {
  155. if (function_exists('mb_strtolower')) {
  156. $str = mb_strtolower($str, 'UTF-8');
  157. } else {
  158. $str = strtolower($str);
  159. }
  160. }
  161. return $str;
  162. }
  163. }