PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/usr/local/www/filebrowser/browser.php

https://github.com/vongrippen/pfsense
PHP | 137 lines | 113 code | 16 blank | 8 comment | 31 complexity | 8780f2b62cd0a206a0267b63924f01d6 MD5 | raw file
  1. <?php
  2. /*
  3. pfSense_MODULE: shell
  4. */
  5. // Fetch a list of directories and files inside a given directory
  6. function get_content($dir) {
  7. $dirs = array();
  8. $files = array();
  9. clearstatcache();
  10. $fd = @opendir($dir);
  11. while($entry = @readdir($fd)) {
  12. if($entry == ".") continue;
  13. if($entry == ".." && $dir == "/") continue;
  14. if(is_dir("{$dir}/{$entry}"))
  15. array_push($dirs, $entry);
  16. else
  17. array_push($files, $entry);
  18. }
  19. @closedir($fd);
  20. natsort($dirs);
  21. natsort($files);
  22. return array($dirs, $files);
  23. }
  24. $path = realpath(strlen($_GET['path']) > 0 ? $_GET['path'] : "/");
  25. if(is_file($path))
  26. $path = dirname($path);
  27. // ----- header -----
  28. ?>
  29. <table width="100%">
  30. <tr>
  31. <td class="fbHome" width="25px" align="left">
  32. <img onClick="$('fbTarget').value='<?=$realDir?>'; fbBrowse('/');" src="/filebrowser/images/icon_home.gif" alt="Home" title="Home" />
  33. </td>
  34. <td><b><?=$path;?></b></td>
  35. <td class="fbClose" align="right">
  36. <img onClick="new Effect.Fade($('fbBrowser'));" border="0" src="/filebrowser/images/icon_cancel.gif" alt="Close" title="Close" />
  37. </td>
  38. </tr>
  39. <tr>
  40. <td id="fbCurrentDir" colspan="3" class="vexpl" align="left">
  41. <?php
  42. // ----- read contents -----
  43. if(is_dir($path)) {
  44. list($dirs, $files) = get_content($path);
  45. ?>
  46. </td>
  47. </tr>
  48. <?php
  49. }
  50. else {
  51. ?>
  52. Directory does not exist.
  53. </td>
  54. </tr>
  55. </table>
  56. <?php
  57. exit;
  58. }
  59. // ----- directories -----
  60. foreach($dirs as $dir):
  61. $realDir = realpath("{$path}/{$dir}");
  62. ?>
  63. <tr>
  64. <td></td>
  65. <td class="fbDir vexpl" id="<?=$realDir;?>" align="left">
  66. <div onClick="$('fbTarget').value='<?=$realDir?>'; fbBrowse('<?=$realDir?>');">
  67. <img src="/filebrowser/images/folder_generic.gif" />
  68. &nbsp;<?=$dir;?>
  69. </div>
  70. </td>
  71. <td></td>
  72. </tr>
  73. <?php
  74. endforeach;
  75. // ----- files -----
  76. foreach($files as $file):
  77. $ext = strrchr($file, ".");
  78. if($ext == ".css" ) $type = "code";
  79. elseif($ext == ".html") $type = "code";
  80. elseif($ext == ".xml" ) $type = "code";
  81. elseif($ext == ".rrd" ) $type = "database";
  82. elseif($ext == ".gif" ) $type = "image";
  83. elseif($ext == ".jpg" ) $type = "image";
  84. elseif($ext == ".png" ) $type = "image";
  85. elseif($ext == ".js" ) $type = "js";
  86. elseif($ext == ".pdf" ) $type = "pdf";
  87. elseif($ext == ".inc" ) $type = "php";
  88. elseif($ext == ".php" ) $type = "php";
  89. elseif($ext == ".conf") $type = "system";
  90. elseif($ext == ".pid" ) $type = "system";
  91. elseif($ext == ".sh" ) $type = "system";
  92. elseif($ext == ".bz2" ) $type = "zip";
  93. elseif($ext == ".gz" ) $type = "zip";
  94. elseif($ext == ".tgz" ) $type = "zip";
  95. elseif($ext == ".zip" ) $type = "zip";
  96. else $type = "generic";
  97. $fqpn = "{$path}/{$file}";
  98. if(is_file($fqpn)) {
  99. $fqpn = realpath($fqpn);
  100. $size = sprintf("%.2f KiB", filesize($fqpn) / 1024);
  101. }
  102. else
  103. $size = "";
  104. ?>
  105. <tr>
  106. <td></td>
  107. <td class="fbFile vexpl" id="<?=$fqpn;?>" align="left">
  108. <?php $filename = str_replace("//","/", "{$path}/{$file}"); ?>
  109. <div onClick="$('fbTarget').value='<?=$filename?>'; loadFile(); new Effect.Fade($('fbBrowser'));">
  110. <img src="/filebrowser/images/file_<?=$type;?>.gif" alt="" title="">
  111. &nbsp;<?=$file;?>
  112. </div>
  113. </td>
  114. <td align="right" class="vexpl">
  115. <?=$size;?>
  116. </td>
  117. </tr>
  118. <?php
  119. endforeach;
  120. ?>
  121. </table>