Advertisement
amfm

php 2

May 22nd, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. <?php
  2.  
  3. $seperator = "\t";
  4. $indentation = "\t";
  5. $max_symlink_level = 2;
  6.  
  7. // Print a line for a file
  8. function outputLine( $path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level, $isDir = False ) {
  9. global $seperator;
  10. global $indentation;
  11.  
  12. //$spaces = str_repeat($indentation, $level);
  13. // no space at beginning
  14. $spaces = "";
  15.  
  16. // Concatenate together output line
  17. $output_str = $spaces . $path."/".$file . $seperator;
  18. // output a 'd' instead of filesize for directories
  19. if ($isDir)
  20. $output_str .= "d";
  21. else
  22. $output_str .= $fs;
  23.  
  24. $output_str .= $seperator . $lm;
  25.  
  26. if ( is_link("$path/$file") )
  27. $output_str .= $seperator . "l";
  28. else
  29. $output_str .= $seperator . "f";
  30.  
  31. $output_str .= $seperator . $user;
  32.  
  33. $output_str .= $seperator . $group;
  34.  
  35. $output_str .= $seperator . sprintf('%o', $permissions);
  36.  
  37. $output_str .= $seperator . $symlink_level;
  38.  
  39. $output_str .= "\n";
  40.  
  41. echo $output_str;
  42. }
  43.  
  44. function getDirectory( $path = '.', $level = 0, $symlink_level = 0 ){
  45.  
  46. global $max_symlink_level;
  47.  
  48. // Sanity check for directory depth
  49. if ($level > 100)
  50. return;
  51.  
  52. // echo "symlink level: ".$symlink_level."\n";
  53. $ignore = array( 'cgi-bin', '.', '..', '/' );
  54. // Directories to ignore when listing output. Many hosts
  55. // will deny PHP access to the cgi-bin.
  56.  
  57. $dh = @opendir( $path );
  58. // Open the directory to the handle $dh
  59.  
  60. while( false !== ( $file = readdir( $dh ) ) ){
  61. // Loop through the directory
  62.  
  63. if( !in_array( $file, $ignore ) && strlen($file) != 0 ){
  64. // Check that this file is not to be ignored
  65.  
  66. if( is_dir( "$path/$file" ) ){
  67. // Its a directory, so we need to keep reading down...
  68.  
  69. $fs = filesize("$path/$file");
  70. $lm = filemtime("$path/$file");
  71. $user = fileowner("$path/$file");
  72. $group = filegroup("$path/$file");
  73. $permissions = fileperms("$path/$file");
  74. outputLine($path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level, True);
  75. if ( is_link("$path/$file") && $symlink_level < $max_symlink_level ) {
  76. getDirectory( "$path/$file", ($level+1), ($symlink_level + 1) );
  77. } else if (!is_link("$path/$file")) {
  78. getDirectory( "$path/$file", ($level+1), $symlink_level );
  79. }
  80. // Re-call this same function but on a new directory.
  81. // this is what makes function recursive.
  82.  
  83. } else {
  84.  
  85. $fs = filesize("$path/$file");
  86. $lm = filemtime("$path/$file");
  87. $user = fileowner("$path/$file");
  88. $group = filegroup("$path/$file");
  89. $permissions = fileperms("$path/$file");
  90.  
  91. outputLine($path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level);
  92. // Just print out the filename
  93.  
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. closedir( $dh );
  101. // Close the directory handle
  102.  
  103. }
  104.  
  105. // Output as plaintext with utf8 encoding
  106. // change charset to iso-8859-1 to change to ASCII
  107. header('Content-Type: text/plain; charset=utf8');
  108.  
  109. $dir_arg = $_GET['dir'];
  110.  
  111. $print_path = $_GET['print_path'];
  112.  
  113. if ($print_path) {
  114. echo "-listing-begin-\n";
  115. echo getcwd() . "\n";
  116. echo "-listing-end-\n";
  117. return;
  118. }
  119.  
  120.  
  121. // Check if dir parameter is valid
  122. if( is_dir($dir_arg) ) {
  123. //echo $dir_arg."\n\n";
  124. echo "-listing-begin-\n";
  125. getDirectory( $dir_arg );
  126. echo "-listing-end-\n";
  127. }
  128. else
  129. echo "bad dir";
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement