FileListing.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\FileListing class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. /**
  11. * Functions for listing directories
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. class FileListing
  16. {
  17. /**
  18. * Returns array of filtered file names
  19. *
  20. * @param string $dir directory to list
  21. * @param string $expression regular expression to match files
  22. *
  23. * @return array|bool sorted file list on success, false on failure
  24. */
  25. public function getDirContent(string $dir, string $expression = '')
  26. {
  27. if (! @file_exists($dir) || ! ($handle = @opendir($dir))) {
  28. return false;
  29. }
  30. $result = [];
  31. if (substr($dir, -1) != '/') {
  32. $dir .= '/';
  33. }
  34. while ($file = @readdir($handle)) {
  35. if (@is_file($dir . $file)
  36. && ! @is_link($dir . $file)
  37. && ($expression == '' || preg_match($expression, $file))
  38. ) {
  39. $result[] = $file;
  40. }
  41. }
  42. closedir($handle);
  43. asort($result);
  44. return $result;
  45. }
  46. /**
  47. * Returns options of filtered file names
  48. *
  49. * @param string $dir directory to list
  50. * @param string $extensions regular expression to match files
  51. * @param string $active currently active choice
  52. *
  53. * @return string|false Html <option> field, false if not files in dir
  54. */
  55. public function getFileSelectOptions(
  56. string $dir,
  57. string $extensions = '',
  58. string $active = ''
  59. ) {
  60. $list = $this->getDirContent($dir, $extensions);
  61. if ($list === false) {
  62. return false;
  63. }
  64. $result = '';
  65. foreach ($list as $val) {
  66. $result .= '<option value="' . htmlspecialchars($val) . '"';
  67. if ($val == $active) {
  68. $result .= ' selected="selected"';
  69. }
  70. $result .= '>' . htmlspecialchars($val) . '</option>' . "\n";
  71. }
  72. return $result;
  73. }
  74. /**
  75. * Get currently supported decompressions.
  76. *
  77. * @return string separated list of extensions usable in getDirContent
  78. */
  79. public function supportedDecompressions(): string
  80. {
  81. global $cfg;
  82. $compressions = '';
  83. if ($cfg['GZipDump'] && function_exists('gzopen')) {
  84. $compressions = 'gz';
  85. }
  86. if ($cfg['BZipDump'] && function_exists('bzopen')) {
  87. if (! empty($compressions)) {
  88. $compressions .= '|';
  89. }
  90. $compressions .= 'bz2';
  91. }
  92. if ($cfg['ZipDump'] && function_exists('gzinflate')) {
  93. if (! empty($compressions)) {
  94. $compressions .= '|';
  95. }
  96. $compressions .= 'zip';
  97. }
  98. return $compressions;
  99. }
  100. }