Login   Register  
PHP Classes
elePHPant
Icontem

File: searchmp3.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Aakash Kambuj  >  WebNap - web based PHP napster client  >  searchmp3.php  >  Download  
File: searchmp3.php
Role: ???
Content type: text/plain
Description: the main search file
Class: WebNap - web based PHP napster client
Author: By
Last change:
Date: 2000-05-24 21:16
Size: 15,186 bytes
 

Contents

Class file image Download
<!-- 
#
# This software may be used and distributed according to the terms
# of the GNU Public License, incorporated herein by reference.
#################################
WebNap Client copyright 2000 
Aakash Kambuj - aakash@kambuj.com 
Kalpak Kothari - kalpak@mit.edu
#################################

You are welcome to modify and distribute this code, but please retain
this copyright notice above any notices you put.  Please send us a
copy of your modified code.

Napster is a registered trademark of Napster Inc - http://www.napster.com
-->

<?

$message_type_login = 2;
$message_type_login_response = 3;
$message_type_sysmsg = 621;
$message_type_search = 200;
$message_type_search_result = 201;
$message_type_search_complete = 202;
$message_type_fileinfo_req = 203;
$message_type_fileinfo = 204;
$message_type_get_error = 206;
$server_to_use="";
// $server_socket=0;


function init_conn($napserver){

  global $server_socket;
  global $message_type_login;
  global $message_type_login_response;
  global $message_type_sysmsg;
  global $server_to_use;

  $server="";
  if (!isset($server_socket)){
    $server_socket=0;
    //$server_socket=2;
    
  }

  $already_connected = $server_socket;
  echo "Connection status is $already_connected <br>";
  if ($already_connected<=0){
    /*
      //find best redirector from napster server
      $fp = fsockopen("server.napster.com", 8875, &$errstr, &$errno); // orig_port 8875
      if(!$fp) {
      echo "$errstr ($errno)<br>\n";
      } else {
      
      echo "The master server port is $fp ";
      while(!feof($fp)) {
      $buffer=fgets($fp,128);
      $server .= $buffer;
      }
      fclose($fp);
      }
    
      $tok = strtok($server,":");
      $host = trim($tok);       //the hostname of the napster server
      $tok = strtok(":");
      $port = (int)$tok;    //the port of the server
      echo "Host is a ", gettype($host);
      echo "Port is a ", gettype($port);
    */

    //$host = "208.184.216.206";
    $host = $napserver;
    $server_to_use=$napserver;
    $port = 8888;
    echo " $host $port ";
  
    
    //now we know the hostname and port to connect to
    echo "Connecting to server <br>";
    $server_socket = fsockopen($host, $port, &$errstr, &$errno);    //open a persistent connection to the server
    if (!$server_socket){
      echo "ERROR $errstr ($errno)<br>\n";
      exit;
    }
    
    echo "Connected: server_socket is  $server_socket <br>";
    $sock = sprintf("%d", $server_socket);
    printf("Connected: server_socket is $sock <br>");
    
    //now we can send a login packet
    
    // please use your own username/password pair here
    $login_string = "idearingwn webNapster 6699 \"v2.0 BETA 6\" 10";
    
    $len = strlen($login_string);
    echo "message length is $len \r <br>";
    
    $login_packet = pack("vv", $len, $message_type_login);
    $login_packet = $login_packet . $login_string;

    fwrite($server_socket, $login_packet);
    //echo fgets($server_socket, 3);
    
    $buffer = read_packet();
    //    echo "<br> Pkt len is $buffer[0], pkt type is $buffer[1], message is $buffer[2] <br> ";
    echo "<br> $buffer[2] ";
    $buffer = read_packet();
    //    echo "<br> Pkt len is $buffer[0], pkt type is $buffer[1], message is $buffer[2]  <br> ";
    echo " $buffer[2] ";
    
    do {
      $buffer = read_packet();
      echo "\r";
      // echo " Packet len is $buffer[0], Packet type is $buffer[1], Message is $buffer[2] <br>";
      echo " $buffer[2] ";

    } while ((int)$buffer[1]==$message_type_sysmsg);

    echo "<br><br> Successfully logged into Napster server <br><br>";    
  }
}




function searchmp3($keywords) {

  global $server_socket;


  echo "The status of the socket is $server_socket";
  echo "<br><br>Keyword  is $keywords <br><br>";

  flush();
  $songs = find_song($keywords);

  //now $songs contains the list of songs we have

  for ($i=0;$i<count($songs);$i++){

    $songlist[$i] = new Song;
    $songlist[$i]->init_song($songs[$i]);
    $songlist[$i]->print_song();

    //echo "    ",$songs[$i],"  <br> ";
  }

  fclose($server_socket);


}

function getmp3($songid){

  global  $server_socket;

  //we have already logged in. The find song code can then be called
  //repeatedly

  //First check the connection to the server

  if ($server_socket<=0){
    echo "ERROR. Connection failed!";
  }     

       
  echo "<br><br> Works so far <br><br>";
  echo "<br> the song id is $songid<br>";
     
  $songinfo = urldecode("$songid");              
  echo "After decode songid is $songinfo<br>";

  $songinfo = unserialize(stripslashes($songinfo));
  echo "<br> after unser songinfo is $songinfo<br>";  
       
       
  $curr_song = new Song;
  $curr_song->unpack_data($songinfo);
  $curr_song->print_song();             
  $curr_song->download_song();
  
  flush();
  echo "<br> All Done!";
     
  fclose($server_socket);  //remove this after development is done


}


function find_song($keywords){

  global $message_type_search;
  global $message_type_search_complete;
  global $message_type_search_result;

  $find_str = "FILENAME CONTAINS \"";
  $find_str .= $keywords;
  $find_str .= "\" MAX_RESULTS 100";

  write_packet($message_type_search, $find_str);

  $songs[0]="";
  $search_done = FALSE;
  $i=0;

  do {
 
    $search_result = read_packet();
 
    switch ($search_result[1]) {
   
    case $message_type_search_complete:
      $search_done = TRUE;
      break;

    case $message_type_search_result:
      $songs[$i]=trim($search_result[2]);
      $i++;
      break;

    default:
      break;
   
    } //end of switch
   
  } while (!$search_done);

  return $songs;

}



function write_packet($m_type, $m_mesg){


  global $server_socket;


  $pkt_len = strlen($m_mesg);
 
  $packet = pack("vv",$pkt_len, $m_type);

  $packet = $packet . $m_mesg;

  fwrite($server_socket, $packet);


}



function read_packet() {

  global $server_socket;

  $data_array="";
     
  $buf = fread($server_socket, 4);
  $buf1 = unpack("v2vals",$buf);
  $len1 = $buf1["vals1"];
  $msg_type1 = $buf1["vals2"];

  //  echo "<br> len1 is $len1  messagetype is $msg_type1 ";
    
  if ($len1 <=0 ) {
    //    echo " EMPTY PACKET->";
    echo "<br><br>";

    $data_array[0] = $len1;
    $data_array[1] = $msg_type1;
    $data_array[2] = "";
  }
  else {
    
    $data = fread($server_socket, $len1);

    $data_array[0] = $len1;
    $data_array[1] = $msg_type1;
    $data_array[2] = $data;
  }

  return $data_array;

}


function uinttoip($hostname){

  echo "$hostname <br>";
  $flag=0;
  $storage = $hostname;
  $temp = (double) $hostname;
  $temp = $temp - 2147483648.0;
  echo "$temp <br>";
  $hostname = (int)$temp;
  if ($hostname < 0 ){
    $hostname = (int) $storage;
  }
  else $flag=1;

  $dig4 = ($hostname & 2130706432) >> 24;

  if ($flag == 1)
    $dig4 += 128;

  $dig3 = ($hostname & 16711680) >> 16;
  $dig2 = ($hostname & 65280) >> 8;
  $dig1 = $hostname & 255;  

  echo "<br> hostname is $hostname<br>";
  echo "<br> dig1 is $dig1  dig2 is $dig2  dig3 is $dig3  dig4 is
$dig4<br>";

  $hname = (string) $dig1;
  $hname = $hname . ".";
  $hname = $hname . (string)$dig2;
  $hname = $hname . ".";
  $hname = $hname . (string)$dig3;
  $hname = $hname . ".";
  $hname = $hname . (string)$dig4;

  echo "<br> Hostname is $hname<br>";
  

  return (string)$hname;


}

function addheader($type){
  switch($type) {
  case 0:
    echo "<html>";
    echo "<title>WebNap v0.2</title>";
    echo "<body bgcolor=white>";
    echo "<center><h1>WebNap v0.2</h1>";
    echo "<hr noshade size=1></center>";
    echo "<br><b>Debug version running</b><br>";       
    break;
  case 1:
    break;
  default:
    echo "<html>";
    echo "<title>WebNap v0.2</title>";
    echo "<body bgcolor=white>";
    echo "<center><h1>WebNap v0.2</h1>";
    echo "<hr noshade size=1></center>";
    echo "<br><b>Debug version running</b><br>";
    break;
  }
}




class Song {

  var $data=array();
  var $user="";
  var $title="";
  var $filename="";
  var $checksum="";
  var $size=0;
  var $bitrate=0;
  var $frequency=0;
  var $time=0;
  var $nick="";
  var $nick_ip="";
  var $conn_speed =0;

  function init_song($str){

    //first, get the pathname of the song
  
    for ($i=(strlen($str)-1);$i>=0 && strcmp($str[$i],'"')!=0;$i--);

    $this->title = substr($str,0,$i+1);
    $this->data["title"]=substr($str,0,$i+1);
    $index = $i;
  
    //now get song name

    for ($i=(strlen($this->title)-1);$i>=0 && strcmp($str[$i],'\\')!=0;$i--);
  

    if ($i==-1){
      $this->filename = $this->title;
      $this->data["filename"] = $this->title;
    }
    else {
      $this->filename = substr($this->title, $i+1, strlen($this->title)-2);
      $this->data["filename"] = substr($this->title, $i+1, strlen($this->title)-2);
    }
    
    $info = substr($str, $index+2);
  
    $tok = strtok($info," ");
  
    $this->checksum = $tok;
    $this->data["checksum"] = $tok;
  
    for ($i=0;$i<7;$i++){
      $tok = strtok(" ");
  
      switch($i){

      case 0:
    $this->size = (int)$tok;
    $this->data["size"] = (int)$tok;
    break;
      
      case 1:
    $this->bitrate = (int)$tok;
    $this->data["bitrate"]=(int)$tok;
    break;
     
      case 2:
    $this->frequency = (int)$tok;
    $this->data["frequency"]=(int)$tok;
    break;

      case 3:
    $this->time = (int)$tok;
    $this->data["time"]=(int)$tok;
    break;
     
      case 4:
    $this->nick = $tok;
    $this->data["nick"]=$tok;
    break;
     
      case 5:
    $this->nick_ip = $tok;
    $this->data["nick_ip"]=$tok;
    break;
       
      case 6:
    $this->conn_speed = (int)$tok;
    $this->data["conn_speed"]=(int)$tok;
    break;

      default:
    break;
      }
    }



  }


  function print_song() {

    global $server_socket;
    global $server_to_use;
    $sock = sprintf("%d", $server_socket);
    
    print( "<br><br>");
    print( "Title is $this->title <br>");
    $stream = urlencode(serialize($this->data));
    print ("Filename is <a href=\"searchmp3.php?napserver=$server_to_use&songid=$stream\">$this->filename</a><br>");
    print( "Nick is $this->nick <br>");
    print( "Checksum is $this->checksum <br>");
    print( "Size is $this->size <br>");
    print( "Bitrate is $this->bitrate <br>");
    print( "Frequency is $this->frequency <br>");
    print( "Song length is $this->time <br>");
    print( "Connection speed is $this->conn_speed <br>");
    print( "<br>");
    flush();

  }


  function unpack_data($datum){


    echo "<br> Miaow........DAtum is $datum<br>";


    $this->data = $datum;

    $this->data["title"] = $datum["title"];
    $this->title = $datum["title"];

    $this->data["filename"] = $datum["filename"];
    $this->filename = $datum["filename"];

    $this->data["checksum"] = $datum["checksum"];
    $this->checksum = $datum["checksum"];

    $this->data["size"] = $datum["size"];
    $this->size = $datum["size"];

    $this->data["bitrate"] = $datum["bitrate"];
    $this->bitrate = $datum["bitrate"];

    $this->data["frequency"] = $datum["frequency"];
    $this->frequency = $datum["frequency"];

    $this->data["time"] = $datum["time"];
    $this->time = $datum["time"];

    $this->data["nick"] = $datum["nick"];
    $this->nick = $datum["nick"];
  
    $this->data["nick_ip"] = $datum["nick_ip"];
    $this->nick_ip = $datum["nick_ip"];
  
  
    $this->data["conn_speed"] = $datum["conn_speed"];
    $this->conn_speed = $datum["conn_speed"];
  
  }


  function download_song() {

    global $server_socket;
    global $message_type_fileinfo_req;
    global $message_type_fileinfo;
    global $message_type_get_error;

    //send message to server
    $message = "$this->nick  ";
    $message .= "$this->title";
    print("<br>Requesting file from server message: $message<br>");


    write_packet($message_type_fileinfo_req, $message);

    $answer ="";
    print("<br>Before receiving reply from server: $answer<br>");
    print("<br>message_type_fileinfo: $message_type_fileinfo ; message_type_get_error: $message_type_get_error ;<br>");
    flush();

    $answer = read_packet();
    print("<br>The server replied $answer[1] $answer[2]<br>");

    if((int)$answer[1]==$message_type_get_error) {
      print("<br> Download Error. The remote file you requested to download is unavailable.<br>");
      flush();
      return;
    }
      
    if ((int)$answer[1]!=$message_type_fileinfo) {
      // return if we did not receive download ack
      print("<br> Download Error. Did not receive download ACK.<br>");
      flush();
      return;
    }

    /*
      while(((int)$answer[1]!=$message_type_fileinfo) or ((int)$answer[1]!=$message_type_get_error)){
      $answer = read_packet();
      print("<br>The server replied $answer[1] $answer[2]<br>");
      flush();
      }
    */

    echo "<br> Server sends IP and port of remote user <br>";
    
    $ruser = strtok($answer[2], " ");
    $rhost = strtok(" ");
    $rport = strtok(" ");
    

    //Convert the $rhost to quad dotted ip address format
      
    $rhost = uinttoip($rhost);
    flush();    

    echo "Remote Host is $rhost";
    echo "Remote Port is $rport";

    if ($rport != 0) {
      //    $title_withoutquotes = substr($this->title, 1, -1);
      $title_withoutquotes = rawurlencode($this->title);
      $filename_withoutquotes = rawurlencode($this->filename);

      echo "<br><center><a href=\"download.php?rhost=$rhost&rport=$rport&title=$title_withoutquotes&filename=$filename_withoutquotes\">Download song</a></center><br>";            
      
      fclose($server_socket); // we are still unable to use just one socket. this keeps the number of open sockets low.
    }
    else {
      echo "<br>Remote user is behind a firewall. Download option from such users is yet to implemented.";

      $local_socket = fsockopen("127.0.0.1", 6699, &$errstr, &$errno);
      if (!$local_socket) {
    echo "$errstr ($errno)<br>\n";
      } else {
    while(!feof($local_socket)) {
      echo fgets($local_socket,128);
    }
    fclose($local_socket);
      }



      fclose($server_socket);
    }
  }

}

// make sure that socket is closed when script ends (normal or "user abort")
register_shutdown_function("close_conn");

function close_conn(){
  global $server_socket;
  if ($server_socket>0) {
    fclose($server_socket);
  }
}


?>

<? 
if (isset($songid)){
  addheader(0);
  init_conn($napserver);
  getmp3($songid);
}
else {
  if (isset($keywords)){
    addheader(0);
    init_conn($napserver);
    searchmp3($keywords);
  }
}
?>

</body></html>