<?php
// 기본적으로 UTF-8 형식임.

// Protect our namespace using a class
class BDecode {
 function numberdecode($wholefile, $offset) {
  // Funky handling of negative numbers and zero
  $negative = false;
  if ($wholefile[$offset] == '-') {
   $negative = true;
   $offset++;
  }
  if ($wholefile[$offset] == '0') {
   $offset++;
   if ($negative)
    return array(false);
   if ($wholefile[$offset] == ':' || $wholefile[$offset] == 'e')
    return array(0, ++$offset);
   return array(false);
  }
  $ret[0] = 0;
  for(;;) {
   if ($wholefile[$offset] >= '0' && $wholefile[$offset] <= '9') {
    $ret[0] *= 10;
    //Added 2005.02.21 - VisiGod
        //Changing the type of variable from integer to double to prevent a numeric overflow  
    settype($ret[0],'double');
    //Added 2005.02.21 - VisiGod
    $ret[0] += ord($wholefile[$offset]) - ord('0');
    $offset++;
   } else if ($wholefile[$offset] == 'e' || $wholefile[$offset] == ':') {
    // Tolerate : or e because this is a multiuse function
    $ret[1] = $offset+1;
    if ($negative) {
     if ($ret[0] == 0)
      return array(false);
     $ret[0] = - $ret[0];
    }
    return $ret;
   } else return array(false);
  }
 }

 function decodeEntry($wholefile, $offset=0) {
  if ($wholefile[$offset] == 'd')
   return $this->decodeDict($wholefile, $offset);
  if ($wholefile[$offset] == 'l')
   return $this->decodelist($wholefile, $offset);
  if ($wholefile[$offset] == 'i')
   return $this->numberdecode($wholefile, ++$offset);
  // String value: decode number, then grab substring

  $info = $this->numberdecode($wholefile, $offset);
  if ($info[0] === false)
   return array(false);
  $ret[0] = substr($wholefile, $info[1], $info[0]);
  $ret[1] = $info[1]+strlen($ret[0]);
  return $ret;
 }

 function decodeList($wholefile, $offset) {
  if ($wholefile[$offset] != 'l')
   return array(false);
  $offset++;
  $ret = array();
  for ($i=0;;$i++) {
   if ($wholefile[$offset] == 'e')
    break;
   $value = $this->decodeEntry($wholefile, $offset);
   if ($value[0] === false)
    return array(false);
   $ret[$i] = $value[0];
   $offset = $value[1];
  }
  // The empty list is an empty array. Seems fine.
  return array(0=>$ret, 1=>++$offset);
 }

 // Tries to construct an array
 function decodeDict($wholefile, $offset=0) {
  if ($wholefile[$offset] == 'l')
   return $this->decodeList($wholefile, $offset);
  if ($wholefile[$offset] != 'd')
   return false;
  $ret=array();
  $offset++;
  for (;;) { 
   if ($wholefile[$offset] == 'e') {
    $offset++;
    break;
   }
   $left = $this->decodeEntry($wholefile, $offset);
   if (!$left[0])
    return false;
   $offset = $left[1];
   if ($wholefile[$offset] == 'd') {
    // Recurse
    $value = $this->decodedict($wholefile, $offset);
    if (!$value[0])
     return false;
    $ret[addslashes($left[0])] = $value[0];
    $offset= $value[1];
    continue;
   }
   if ($wholefile[$offset] == 'l') {
    $value = $this->decodeList($wholefile, $offset);
    if (!$value[0] && is_bool($value[0]))
     return false;
    $ret[addslashes($left[0])] = $value[0];
    $offset = $value[1];
    continue;
   }
   $value = $this->decodeEntry($wholefile, $offset);
   if ($value[0] === false)
    return false;
   $ret[addslashes($left[0])] = $value[0];
   $offset = $value[1];
  }
  return array(0=>(empty($ret)?true:$ret), 1=>$offset);
 }
} // End of class declaration.

// Use this function. eg:  BDecode("d8:announce44:http://www. ... e");
function BDecode($wholefile) {
 $decoder = new BDecode;
 $return = $decoder->decodeEntry($wholefile);
 return $return[0];
}

// We'll protect the namespace of our code
// using a class
class BEncode {
 // Dictionary keys must be sorted. foreach tends to iterate over the order
 // the array was made, so we make a new one in sorted order. :)
 function makeSorted($array) {
  // Shouldn't happen!
  if (empty($array))
   return $array;
  $i = 0;
  foreach($array as $key => $dummy)
   $keys[$i++] = stripslashes($key);
  sort($keys);
  for ($i=0; isset($keys[$i]); $i++)
   $return[addslashes($keys[$i])] = $array[addslashes($keys[$i])];
  return $return;
 }

 // Encodes strings, integers and empty dictionaries.
 // $unstrip is set to true when decoding dictionary keys
 function encodeEntry($entry, &$fd, $unstrip = false) {
  if (is_bool($entry)) {
   $fd .= 'de';
   return;
  }
  if (is_int($entry) || is_float($entry)) {
   $fd .= 'i'.$entry.'e';
   return;
  }
  if ($unstrip)
   $myentry = stripslashes($entry);
  else
   $myentry = $entry;
  $length = strlen($myentry);
  $fd .= $length.':'.$myentry;
 }

 // Encodes lists
 function encodeList($array, &$fd) {
  $fd .= 'l';
  // The empty list is defined as array();
  if (empty($array)) {
   $fd .= 'e';
   return;
  }
  for ($i = 0; isset($array[$i]); $i++)
   $this->decideEncode($array[$i], $fd);
  $fd .= 'e';
 }

 // Passes lists and dictionaries accordingly, and has encodeEntry handle
 // the strings and integers.
 function decideEncode($unknown, &$fd) {
  if (is_array($unknown)) {
   if (isset($unknown[0]) || empty($unknown))
    return $this->encodeList($unknown, $fd);
   else
    return $this->encodeDict($unknown, $fd);
  }
  $this->encodeEntry($unknown, $fd);
 }

 // Encodes dictionaries
 function encodeDict($array, &$fd) {
  $fd .= 'd';
  if (is_bool($array)) {
   $fd .= 'e';
   return;
  }
  // NEED TO SORT!
  $newarray = $this->makeSorted($array);
  foreach($newarray as $left => $right) {
   $this->encodeEntry($left, $fd, true);
   $this->decideEncode($right, $fd);
  }
  $fd .= 'e';
 }
} // End of class declaration.

// Use this function in your own code.
function BEncode($array) {
 $string = '';
 $encoder = new BEncode;
 $encoder->decideEncode($array, $string);
 return $string;
}

$url = trim($_GET[url]);
if ($url) {
  $alltorrent = trim(Yreadfile($url, ''));
} else exit;
if ($alltorrent == "") exit;
$array = BDecode($alltorrent);

$hash=sha1(BEncode($array["info"]));
echo "<b>MAGNET :</b> magnet:?xt=urn:btih:".$hash."<br />";
echo "<b>ANNOUNCE :</b> ".$array[announce]."<br />";
echo "<b>ANNOUNCE-LIST :</b> ";
$announce_list = "announce-list";
$announce_list_cnt = count($array[$announce_list]);
for ($i=0;$i < $announce_list_cnt;$i++) {
 echo $array[$announce_list][$i][0];
 if ($i < $announce_list_cnt-1)
  echo ", ";
 else
  echo "<br />";
}

echo "<b>COMMENT :</b> ".$array[comment]."<br />";
$creation_date = "creation date";
echo "<b>CREATION DATE :</b> ".date("Y-m-d",$array[$creation_date])."<br />";
echo "<b>NAME :</b> ".$array[info][name]."<br />";
$file_cnt = 0;
$info_files_cnt = count($array[info][files]);
for ($i=0;$i < $info_files_cnt;$i++) {
 $info_file_path_cnt = count($array[info][files][$i][path]);
 $file_cnt++;
 echo $file_cnt.": ";
 for ($j=0;$j < $info_file_path_cnt;$j++) {
  echo $array[info][files][$i][path][$j];
  if ($j < $info_file_path_cnt-1) echo "/";
  echo " (".$array[info][files][$i][length]."byte)";
 }
 echo "<br />";
}


/*
 아래 소켓방식으로 파일 가저오기는 유창화 님의 소스 입니다.
 */
//변수가 정의 되었으면 해당 값을 반환하고 정의 되어있지 않으면 지정된 기본값을 반환합니다.
function Yvar_check(&$var, $return=''){
  return (isset($var)) ? $var : $return;
}

//변수가 정의되지 않았거나 값이 NULL값 이거나, 값이 0, false 일경우 지정된 기본값을 반환합니다.
function Yvar_empty_check(&$var, $return=''){
  return (!empty($var)) ? $var : $return;
}

//http, GET 방식의 소켓연결
function Ysockopen($url, $referer='', $accept='*/*', $contenttype='application/x-www-form-urlencoded', $cachecontrol='private', $pragma='no-cache', $connection='close'){
  if (!empty($url) && preg_match("`^http://.+`i", $url)) {

    $array = parse_url($url);
    $url_scheme = Yvar_check($array['scheme']);
    $url_server = Yvar_check($array['host']);
    $url_path = Yvar_check($array['path']);
    $url_query= Yvar_check($array['query']);
    $url_port = Yvar_empty_check($array['port'], 80);
 
    $fp = @($url_server, $url_port);

      if (empty($fp)) {
      return '';
      }

    fputs($fp, "GET " . $url_path . "?" . $url_query . " HTTP/1.0\n");
    if (!empty($accept)) fputs($fp, "Accept: " . $accept . "\n");
    if (!empty($contenttype)) fputs($fp, "Content-Type: " . $contenttype . "\n");
    fputs($fp, "Host: $url_server:$url_port\n");
    if (!empty($referer)) fputs($fp, "Referer: " . $referer . "\n");
    if (!empty($cachecontrol)) fputs($fp, "Cache-control: " . $cachecontrol . "\n");
    if (!empty($pragma)) fputs($fp, "Pragma: " . $pragma . "\n");
    if (!empty($connection)) fputs($fp, "Connection: " . $connection . "\n\n");

    return $fp;
  } else {
    return '';
  }
}

function Ysockclose(&$fp){
  if (!empty($fp)) fclose($fp);
  return true;
}

//소켓으로 외부파일 통째로 텍스트로 읽어옴
function Yreadfile($url, $referer='', $accept='*/*', $contenttype='application/x-www-form-urlencoded', $cachecontrol='private', $pragma='no-cache', $connection='close') {
  $fp = Ysockopen($url, $referer, $accept, $contenttype, $cachecontrol, $pragma, $connection);
  if (!empty($fp)) {
    $text = '';
    $check_header = false;
    while (!feof($fp)){
      $temp = fgets($fp, 1024);
      if ($check_header == false){
        //실제 파일이 아닌 헤더 정보인지 체크 하여 추가하지 않는다.
        if (preg_match("'^(\r)?\n$'", $temp)) {
          $check_header = true;
          continue;
        }
      } else{
        //헤더 정보가 아닌 경우에만 추가한다.
        $text .= $temp;
      }
    }
    Ysockclose($fp);
    return $text;
  } else {
   return '';
  }
}
?>





+ Recent posts