FileListing.php 2.8 KB

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