Guest
Public paste!

Untitled

By: a guest | Nov 25th, 2009 | Syntax: PHP | Size: 3.32 KB | Hits: 489 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?
  2. // (c) marco "harddisk" schuster
  3. // marco @ vmsoft-gbr.de
  4. // rapidshare account data here
  5. $rsc_acc="123456789";
  6. $rsc_pass="blublub";
  7.  
  8. //sanity check
  9. if(isset($_GET["f"]))
  10.         $file=$_GET["f"];
  11. else
  12.         die("no file given");
  13.  
  14. //is it a valid rs link?
  15. $r="@(http.?\:\/\/)?(.*)?rapidshare\.com\/files\/(.*)\/(.*)$@isU";
  16. if(preg_match($r,$file,$hit)) {
  17.         $fid=$hit[3]; //file identifier
  18.         $fn=$hit[4];  //file name
  19. } else
  20.         die("incorrect url");
  21.  
  22. //check if the premium account has enough traffic for the file
  23. $pa_stats=file("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=getaccountdetails_v1&type=prem&login=$rsc_acc&password=$rsc_pass&withcookie=1");
  24. foreach($pa_stats as $line) {
  25.         $pair=split("=",$line);
  26.         $pa[trim($pair[0])]=trim($pair[1]);
  27. }
  28. if( !($pa["premkbleft"]>0) || !(strlen($pa["cookie"])>0) )
  29.         die("can not access account data. report to author!");
  30. else {
  31.         $kbleft=$pa["premkbleft"];
  32.         $cookie=$pa["cookie"];
  33. }
  34.  
  35. //let's get some file data and check the file itself
  36. $fstats=file("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files=$fid&filenames=$fn");
  37. $fstats=split(",",trim($fstats[0]));
  38. $fs=$fstats[2]/1024;    //convert to KB
  39. if($fs>$kbleft)
  40.         die("premium account is empty or has not enough KB left (available: $kbleft KB, filesize $fs KB). wait 24 hrs or contact account owner.");
  41.  
  42. //check if the file is accessible
  43. switch((int)trim($fstats[4])){
  44.         case 0:
  45.         case 4:
  46.         case 5:
  47.                 header("HTTP/1.1 404 Not Found");
  48.                 die("file not available (deleted or locked)");
  49.                 break;
  50.         case 1:
  51.         case 2:
  52.         case 6:
  53.                 $url="http://rs".$fstats[3].$fstats[5].".rapidshare.com/files/".$fstats[0]."/".$fstats[1]."";
  54.                 break;
  55.         case 3:
  56.                 header("HTTP/1.1 503 Service Unavailable");
  57.                 die("rapidshare server down, retry later");
  58.                 break;
  59. }
  60.  
  61. $multipart=false;
  62.  
  63. //was a part of the file requested? (partial download)
  64. $range=$_SERVER["HTTP_RANGE"];
  65. if($range) {
  66.         //pass client Range header to rapidshare
  67.         $cookie.="\r\nRange: $range";
  68.         $multipart=true;
  69.         header("X-UR-RANGE-Range: $range");
  70. }
  71.  
  72. //octet-stream + attachment => client always stores file
  73. header('Content-type: application/octet-stream');
  74. header('Content-Disposition: attachment; filename="'.$fn.'"');
  75. //always included so clients know this script supports resuming
  76. header("Accept-Ranges: bytes");
  77.  
  78. //awful hack to pass rapidshare the premium cookie
  79. $user_agent = ini_get("user_agent");
  80. ini_set("user_agent",$user_agent . "\r\nCookie: enc=$cookie");
  81. $httphandle = fopen($url,"r");
  82. $headers = stream_get_meta_data($httphandle);
  83.  
  84. //let's check the return header of rapidshare for range / length indicators
  85. //we'll just pass these to the client
  86. foreach($headers["wrapper_data"] as $header) {
  87.         $header=trim($header);
  88.         if(substr(strtolower($header),0,strlen("content-range"))=="content-range") {
  89.                 header($header);
  90.                 header("X-RS-RANGE-".$header);
  91.                 $multipart=true; //content-range indicates partial download
  92.         } elseif(substr(strtolower($header),0,strlen("Content-Length"))=="content-length") {
  93.                 header($header);
  94.                 header("X-RS-CL-".$header);
  95.         }
  96. }
  97.  
  98. //now show the client he has a partial download
  99. if($multipart)
  100.         header('HTTP/1.1 206 Partial Content');
  101.  
  102. fpassthru($httphandle); //simply proxy the file content
  103. fclose($httphandle); //free up resources
  104. ini_set("user_agent",$user_agent); //restore old user agent to revert the hack
  105.  
  106.  
  107. ?>