Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Header("content-type: text/plain");
- $parse_start_time = microtime(true);
- $tag='_UG-DHT_';
- if ($dh = opendir("./"))
- {
- while (($f = readdir($dh)) !== false)
- if(!(($f==".")||($f=="..")) && filetype("./$f")!="dir" && strtolower(pathinfo($f, PATHINFO_EXTENSION))=="torrent" && substr($f, 0, strlen($tag))!=$tag )
- {
- $r=bdec(file_get_contents($f));
- if (isset($r["info"]["source"]) && $r["info"]["source"]=="Underground Gamer")
- {
- echo ($f.": ");
- $r["announce"]="http://localhost";
- unset($r["info"]["private"]);
- file_put_contents($tag.$f,benc($r));
- echo ("OK!\n");
- }
- }
- closedir($dh);
- }
- function info_hash($info)
- {
- return strtoupper(bin2hex(pack("H*", sha1(benc($info)))));
- }
- echo (memory_get_peak_usage() / 1024 / 1024) . " MB of RAM, " . (microtime(true) - $parse_start_time) . " seconds.\n\n";
- /*
- Bencoding OOP wrapper for PHP | by Proger_XP | In public domain
- http://proger.i-forge.net/BEncoded/7Tn
- Based on lightenc.php functions from
- http://wiki.theory.org/Decoding_encoding_bencoded_data_with_PHP
- */
- /* lightenc.php */
- function bdec($s, &$pos=0) {
- if($pos>=strlen($s)) {
- return null;
- }
- switch($s[$pos]){
- case 'd':
- $pos++;
- $retval=array();
- while ($s[$pos]!='e'){
- $key=bdec($s, $pos);
- $val=bdec($s, $pos);
- if ($key===null || $val===null)
- break;
- $retval[$key]=$val;
- }
- $retval["isDct"]=true;
- $pos++;
- return $retval;
- case 'l':
- $pos++;
- $retval=array();
- while ($s[$pos]!='e'){
- $val=bdec($s, $pos);
- if ($val===null)
- break;
- $retval[]=$val;
- }
- $pos++;
- return $retval;
- case 'i':
- $pos++;
- $digits=strpos($s, 'e', $pos)-$pos;
- // Proger_XP: changed (int) -> (float) to avoid trimming of values exceeding
- // signed int's max value (2147483647).
- $val=(float)substr($s, $pos, $digits);
- $pos+=$digits+1;
- return $val;
- // case "0": case "1": case "2": case "3": case "4":
- // case "5": case "6": case "7": case "8": case "9":
- default:
- $digits=strpos($s, ':', $pos)-$pos;
- if ($digits<0 || $digits >20)
- return null;
- $len=(float)substr($s, $pos, $digits);
- $pos+=$digits+1;
- $str=substr($s, $pos, $len);
- $pos+=$len;
- //echo "pos: $pos str: [$str] len: $len digits: $digits\n";
- return (string)$str;
- }
- return null;
- }
- // Proger_XP: added added skipping for null values and $tempChar prefix for list/dicts.
- function benc(&$d, $tempChar = null){
- if(is_array($d)){
- $ret="l";
- $isDict=!empty($d["isDct"]);
- if($isDict){
- $ret="d";
- // this is required by the specs, and BitTornado actualy chokes on unsorted dictionaries
- ksort($d, SORT_STRING);
- }
- foreach($d as $key=>$value) {
- if($isDict){
- // skip the isDct element, only if it's set by us
- if (!bskip($key, $value, $tempChar)) {
- $ret .= strlen($key).":$key";
- }
- } elseif (!is_int($key) and !is_float($key) and trim($key, '0..9') !== '') {
- // Proger_XP: added exception raising for non-numeric list keys.
- throw new Exception('Cannot bencode() a list - it contains a non-numeric key "'.$key.'".');
- }
- if (is_string($value)) {
- $ret.=strlen($value).":".$value;
- } elseif (is_int($value) or is_float($value)){
- $ret.="i${value}e";
- } else {
- $ret.=benc ($value);
- }
- }
- return $ret."e";
- } elseif (is_string($d)) { // fallback if we're given a single bencoded string or int
- return strlen($d).":".$d;
- } elseif (is_int($d) or is_float($d)) {
- return "i${d}e";
- } else {
- return null;
- }
- }
- // bskip by Proger_XP.
- function bskip($key, &$value, $tempChar = null) {
- return ($key === 'isDct' and $value) or $value === null or strpos($key, $tempChar) === 0;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment