Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php
  2.     function rglob($pattern='*', $flags = 0, $path=false)
  3.         {
  4.                 if (!$path) { $path=dirname($pattern).DIRECTORY_SEPARATOR; }
  5.                 $pattern=basename($pattern);
  6.                 $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
  7.                 $files=glob($path.$pattern, $flags);
  8.                 foreach ($paths as $path) {
  9.                         $files=array_merge($files,rglob($pattern, $flags, $path));
  10.                 }
  11.                 return $files;
  12.         }
  13.     function myurlencode($string) {
  14.     $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  15.     $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
  16.     return str_replace($entities, $replacements, urlencode($string));
  17. }
  18.         function disp_files($pattern, $path='/data/tf/gecko')
  19.         {
  20.                 $files = rglob($pattern, 0, $path);
  21.                 echo "<table border=1><tr><td>FileName</td><td>List</td></tr>";
  22.                 foreach ( $files as $file)
  23.                 {
  24.                         if (preg_match('/finished$/',dirname($file)))
  25.                         {
  26.                                 continue;
  27.                         }
  28.  
  29.             $file = str_replace("%2F", "/", $file);
  30.                         echo "<tr><td>".basename($file)."</td><td><a href=\"rar".$file."\">List</a></td></tr>";
  31.                 }
  32.                 echo "</table>";
  33.         }
  34.    
  35.         function detectUserAgent() {
  36.     if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
  37.         return "Other";
  38.  
  39.     $uas = $_SERVER['HTTP_USER_AGENT'];
  40.     if (preg_match("@Opera/@", $uas))
  41.         return "Opera";
  42.     if (preg_match("@Firefox/@", $uas))
  43.         return "Firefox";
  44.     if (preg_match("@Chrome/@", $uas))
  45.         return "Chrome";
  46.     if (preg_match("@MSIE ([0-9.]+);@", $uas, $matches)) {
  47.         if (((float)$matches[1]) >= 7.0)
  48.             return "IE";
  49.     }
  50.  
  51.     return "Other";
  52. }
  53. function format_bytes($size) {
  54.     $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  55.     for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  56.     return round($size, 2).$units[$i];
  57. }
  58.  
  59. function crunch_uri($params)
  60. {
  61.     global $keywords;
  62.     $cur_item = array_shift($params);
  63.     switch ($cur_item)
  64.     {
  65.         case 'rar':
  66.         $response['rar']['set'] = true;
  67.         $response['rar']['param'] = urldecode(array_shift($params));
  68.         break;
  69.        
  70.         case 'ex':
  71.         $response['ex']['set'] = true;
  72.         $response['ex']['param'] = urldecode(array_shift($params));
  73.         break;
  74.        
  75.         case 'list':
  76.         $response['list']['set'] = true;
  77.         break;
  78.  
  79.  
  80.  
  81.     }
  82.     if(!empty($params) && $params[0] != "")
  83.     {
  84.     $response += crunch_uri($params, $method);
  85.     }
  86.     return $response;
  87. }
  88.     $keywords[] = 'rar';
  89.     $keywords[] = 'ex';
  90.     $keywords[] = 'list';
  91.     $params = explode( '/', $_SERVER['REQUEST_URI']);
  92.     while (strtolower(array_shift($params)) != 'www'): endwhile;
  93.     if(!empty($params) && $params[0] != "")
  94.     {
  95.         $crunched_uri = crunch_uri($params,$method);
  96.     }
  97.     else
  98.     {
  99.         $crunched_uri['list']['set'] = true;
  100.     }
  101.    
  102.     if (! isset($crunched_uri['rar']['param']))
  103.     {
  104.         disp_files('*.rar');       
  105.     }
  106.     else
  107.     {
  108.     $filename=$crunched_uri['rar']['param'];
  109.    
  110.     $rar_file = rar_open($filename);
  111.     if (isset($crunched_uri['ex']['param']))
  112.     {
  113.         $returnfile=$crunched_uri['ex']['param'];
  114.        
  115.         $formatRFC2231 = 'Content-Disposition: attachment; filename*=UTF-8\'\'%s';
  116.         $formatDef = 'Content-Disposition: attachment; filename="%s"';
  117.         $orfilename = urldecode($returnfile);
  118.         $entry = $rar_file->getEntry($orfilename);
  119.         $filesize = $entry->getUnpackedSize();
  120.         $fp = $entry->getStream();
  121.         $rar_file->close();
  122.         switch (detectUserAgent()) {
  123.     case "Opera":
  124.     case "Firefox":
  125.         $orfilename = rawurlencode($orfilename);
  126.         $format = $formatRFC2231;
  127.         break;
  128.  
  129.     case "IE":
  130.     case "Chrome":
  131.         $orfilename = rawurlencode($orfilename);
  132.         $format = $formatDef;
  133.         break;
  134.     default:
  135.         if (function_exists('iconv'))
  136.             $orfilename =
  137.                 @iconv("UTF-8", "ISO-8859-1//TRANSLIT", $orfilename);
  138.         $format = $formatDef;
  139. }
  140.  
  141. header(sprintf($format, $orfilename));
  142. //cannot send error messages from now on (headers already sent)
  143.  
  144. //replace by real content type, perhaps infering from the file extension
  145. $contentType = "application/octet-stream";
  146. header("Content-Type: $contentType");
  147.  
  148. header("Content-Transfer-Encoding: binary");
  149.  
  150. header("Content-Length: $filesize");
  151.  
  152. if ($_SERVER['REQUEST_METHOD'] == "HEAD")
  153.     die();
  154.    
  155. while (!feof($fp)) {
  156.     $s = @fread($fp, 8192);
  157.     if ($s === false)
  158.         break; //useless to send error messages
  159.  
  160.     echo $s;
  161. }
  162.        
  163.     }
  164.     else {
  165.     $file_list = rar_list($rar_file);
  166. #   print_r($file_list);
  167.     echo "<table border=1><tr><td>Filename</td><td>Size</td><td>F</td><td>D</td></tr>";
  168.     foreach($file_list as $file)
  169.     {
  170.        
  171.         $name= $file->getName();
  172.         $size = format_bytes($file->getUnpackedSize());
  173.         $isfolder = $file->isDirectory();
  174.         echo "<tr><td>$name</td><td>$size</td><td>$isfolder</td><td><a href=\"rar/".myurlencode($filename)."/ex/".myurlencode($name)."\">D</a></td></tr>";
  175.     }
  176.     echo "</table>";
  177.  
  178.     }  
  179. }
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement