home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / on-line / webnap-0.4.1 / download.php < prev    next >
Encoding:
PHP Script  |  2000-06-09  |  2.5 KB  |  116 lines

  1. <?
  2. # This software may be used and distributed according to the terms
  3. # of the GNU Public License, incorporated herein by reference.
  4. #
  5. # See file COPYING for details of the GNU GPL.
  6. #
  7. #################################
  8. #WebNap Client copyright 2000
  9. #Aakash Kambuj - aakash@idearing.com
  10. #Kalpak Kothari - kalpak@idearing.com
  11. #################################
  12. #
  13. #You are welcome to modify and distribute this code, but you MUST retain
  14. #this copyright notice above any notices you put.  If you use this program
  15. #beyond personal use, we request that you send us a copy of your modified code.
  16. #
  17. #Napster is a registered trademark of Napster Inc - http://www.napster.com
  18. #
  19.  
  20. function download($username,$rhost,$rport,$title,$filename) {
  21.   global $QUERY_STRING;
  22.  
  23.   $MTU = 2048;
  24.   $title = rawurldecode($title);
  25.   $filename = rawurldecode($filename);
  26.  
  27.   // if using HTTP_GET, we also need to un-quote the strings
  28.   if ($QUERY_STRING != "") {
  29.     $title = stripslashes($title);
  30.     $filename = stripslashes($filename);
  31.   }
  32.  
  33.   $remote_user = fsockopen($rhost, $rport, &$errstr, &$errno);     //open a connection to the remote user
  34.   if (!$remote_user){
  35.     echo  "ERROR $errstr ($errno)<br>\n";
  36.     return;
  37.   }
  38.  
  39.    //read magic character '1'
  40.   $dt = (string)fread($remote_user, 1);
  41.   if ($dt !=  "1") {
  42.     print( "<br>ERROR Expected magic character '1' from remote user: $dt");
  43.     return;
  44.   }
  45.  
  46.   fwrite($remote_user,  "GET");         // write string "GET"
  47.   flush();
  48.    //send file name to get
  49.   $mess =  "$username " . $title .  " 0";
  50.    //  echo "String sent to remote client after \"GET\": $mess <br>";
  51.   fwrite($remote_user, $mess);
  52.  
  53.  
  54.  
  55.  
  56.    //read the size of the mp3 file
  57.   $file_length = "";
  58.   $temp=0;
  59.   for ($i=0;$i<$MTU;$i++){
  60.  
  61.     $temp = fread($remote_user,1);
  62.     if ($temp< '0' || $temp> '9'){
  63.       break;
  64.     }
  65.     $file_length .= $temp;
  66.   }
  67.  
  68.   $mpsize = (int)$file_length;
  69.   settype($mpsize,"integer");
  70.     flush();
  71.  
  72.  
  73.   if ($mpsize <= 0) {
  74.     echo  "<br>ERROR. Remote user sent an invalid filesize: $mpsize";
  75.     fclose($remote_user);
  76.     exit;
  77.   }
  78.  
  79.   $num = pack( "C",$temp);
  80.  
  81.          // start sending file. indicate browser to download.
  82.   header(  "Content-type: application/octet-stream" );
  83.   header(  "Content-Disposition: attachment; filename=$filename" );
  84.   header(  "Content-Length: $mpsize" );
  85.  
  86.   echo $num;    //first byte of mp3 file
  87.  
  88.   $count=1;
  89.   do {
  90.     $received = fread($remote_user, $MTU);
  91.     $count +=$MTU;
  92.     echo $received;
  93.   } while ($count<$mpsize);
  94.   fclose($remote_user);
  95.   exit;
  96. }
  97.  
  98. download($username,$rhost,$rport,$title,$filename);
  99.  
  100. ?>
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.