plugin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * 'mkdir.pre mkfile.pre rename.pre' => 'Plugin.Normalizer.cmdPreprocess',
  16. * 'upload.presave' => array(
  17. * 'Plugin.Normalizer.onUpLoadPreSave'
  18. * )
  19. * ),
  20. * // global configure (optional)
  21. * 'plugin' => array(
  22. * 'Normalizer' = array(
  23. * 'enable' => true,
  24. * 'nfc' => true,
  25. * 'nfkc' => true
  26. * )
  27. * ),
  28. * // each volume configure (optional)
  29. * 'roots' => array(
  30. * array(
  31. * 'driver' => 'LocalFileSystem',
  32. * 'path' => '/path/to/files/',
  33. * 'URL' => 'http://localhost/to/files/'
  34. * 'plugin' => array(
  35. * 'Normalizer' = array(
  36. * 'enable' => true,
  37. * 'nfc' => true,
  38. * 'nfkc' => true
  39. * )
  40. * )
  41. * )
  42. * )
  43. * );
  44. *
  45. * @package elfinder
  46. * @author Naoki Sawada
  47. * @license New BSD
  48. */
  49. class elFinderPluginNormalizer
  50. {
  51. private $opts = array();
  52. public function __construct($opts) {
  53. $defaults = array(
  54. 'enable' => true, // For control by volume driver
  55. 'nfc' => true, // Canonical Decomposition followed by Canonical Composition
  56. 'nfkc' => true // Compatibility Decomposition followed by Canonical
  57. );
  58. $this->opts = array_merge($defaults, $opts);
  59. }
  60. public function cmdPreprocess($cmd, &$args, $elfinder, $volume) {
  61. $opts = $this->getOpts($volume);
  62. if (! $opts['enable']) {
  63. return false;
  64. }
  65. if (isset($args['name'])) {
  66. $args['name'] = $this->normalize($args['name'], $opts);
  67. }
  68. return true;
  69. }
  70. public function onUpLoadPreSave(&$path, &$name, $src, $elfinder, $volume) {
  71. $opts = $this->getOpts($volume);
  72. if (! $opts['enable']) {
  73. return false;
  74. }
  75. if ($path) {
  76. $path = $this->normalize($path, $opts);
  77. }
  78. $name = $this->normalize($name, $opts);
  79. return false;
  80. }
  81. private function getOpts($volume) {
  82. $opts = $this->opts;
  83. if (is_object($volume)) {
  84. $volOpts = $volume->getOptionsPlugin('Normalizer');
  85. if (is_array($volOpts)) {
  86. $opts = array_merge($this->opts, $volOpts);
  87. }
  88. }
  89. return $opts;
  90. }
  91. private function normalize($str, $opts) {
  92. if (class_exists('Normalizer')) {
  93. if ($opts['nfc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_C))
  94. $str = Normalizer::normalize($str, Normalizer::FORM_C);
  95. if ($opts['nfkc'] && ! Normalizer::isNormalized($str, Normalizer::FORM_KC))
  96. $str = Normalizer::normalize($str, Normalizer::FORM_KC);
  97. } else {
  98. if (! class_exists('I18N_UnicodeNormalizer')) {
  99. @ include_once 'I18N/UnicodeNormalizer.php';
  100. }
  101. if (class_exists('I18N_UnicodeNormalizer')) {
  102. $normalizer = new I18N_UnicodeNormalizer();
  103. if ($opts['nfc'])
  104. $str = $normalizer->normalize($str, 'NFC');
  105. if ($opts['nfkc'])
  106. $str = $normalizer->normalize($str, 'NFKC');
  107. }
  108. }
  109. return $str;
  110. }
  111. }