function get_peers($tid)
{
global $db, $bb_cfg;
if(!$tid) return 'Топик неполучен';
set_time_limit(300);
class bencoding
{
public function encode($value)
{
$type = gettype($value);
$out = '';
switch($type)
{
case 'integer':
$out.= 'i' . $value . 'e';
break;
case 'string':
$out.= strlen($value) . ':' . utf8_encode($value);
break;
case 'array':
if(!$this->is_associative($value))
{
$out.= 'l';
foreach($value as $entry)
{
$out.= $this->encode($entry);
}
$out.= 'e';
}
else
{
$out.= 'd';
foreach($value as $key => $entry)
{
$out.= $this->encode($key) . $this->encode($entry);
}
$out.= 'e';
}
break;
default:
throw new bencoding_exception('type must be integer / string or array');
break;
}
return $out;
}
public function decode($value)
{
list($v, $r) = $this->rec_decode($value);
return $v;
}
private function rec_decode($value)
{
switch($value[0])
{
# list
case 'l':
$value = substr($value, 1, -1);
$out = array();
while(!empty($value))
{
list($v, $r) = $this->rec_decode($value);
$value = $r;
if(!empty($v))
{
$out[] = $v;
}
}
return array($out, false);
break;
# dictonary
case 'd':
$value = substr($value, 1, -1);
$out = array();
while(!empty($value))
{
list($k, $r) = $this->rec_decode($value);
$value = $r;
list($v, $r) = $this->rec_decode($value);
$value = $r;
if(!empty($k) && !empty($v))
{
$out[$k] = $v;
}
}
return array($out, false);
break;
# integer
case 'i':
return $this->decode_int($value);
break;
# string
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return $this->decode_str($value);
break;
default:
return false;
}
}
private function decode_int($value)
{
if(isset($value[0]) && $value[0] == 'i')
{
$i = 1;
$length = '';
while($value[$i] != 'e')
{
$length.= $value[$i];
$i++;
}
$result = intval($length);
$value = substr($value, strlen($length) + 2);
return array($result, $value);
}
return array(false, false);
}
private function decode_str($value)
{
if(is_numeric($value[0]))
{
$i = 0;
$length = '';
while($value[$i] != ':')
{
$length.= $value[$i];
$i++;
}
$length = intval($length);
$result = substr($value, $i + 1, $length);
$value = substr($value, strlen($length) + 1 + $length);
return array($result, $value);
}
return array(false, false);
}
private function is_associative($array)
{
for($i = 0; $i < count($array); $i++)
{
if(!isset($array[$i]))
{
return true;
}
}
return false;
}
}
if(!$a_data = $db->fetch_row('SELECT attach_id FROM bb_bt_torrents WHERE topic_id = '.$tid)) return 'Данные неполучены';
$attach_id = $a_data['attach_id'];
$t_data = $db->fetch_row('SELECT topic_id, tor_type, rpad(info_hash,20," ") AS info_hash, size FROM '. BT_TORRENTS_TABLE .' WHERE attach_id = '.$attach_id.' LIMIT 1');
$info_hash = $t_data['info_hash'];
$size = ($t_data['size']) ? $t_data['size'] : 2097152000;
$topic_id = $t_data['topic_id'];
$tor_type = $t_data['tor_type'];
$get_req_started = 'info_hash='.urlencode($info_hash).'&peer_id=-UT1800-8H%17%94%08%ef%8c%0f%bd%0c%8d%3a&port=32080&uploaded=0&downloaded=0&left='.$size.'&corrupt=0&key=1F64382C&event=started&numwant=200&compact=1&no_peer_id=1';
-------------
ВЫРЕЗАН КУСОК КОДА
-------------
$node_count = count($announces);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$url = $announces[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'uTorrent/1800(18400)');
curl_multi_add_handle($master, $curl_arr[$i]);
}
do
{
@curl_multi_exec($master, $running);
}
while($running > 0);
$bencode = new bencoding();
$peers = $sql_peers = '';
$complete = $incomplete = 0;
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent ( $curl_arr[$i] );
$xall = $bencode->decode($results);
if (!empty($xall['peers']))
{
$peers = get_sql_peer($xall['peers'], $topic_id, $tor_type, $info_hash);
if(!empty($peers)) $sql_peers[] = $peers;
}
curl_multi_remove_handle($master, $curl_arr[$i]);
}
curl_multi_close($master);
if(!empty($sql_peers))
{
foreach($sql_peers AS $row)
{
foreach($row AS $val=>$key)
{
$new_arr[] = $key;
}
}
$master = '';
$curl_arr = array();
$master = curl_multi_init();
$cnt = count($new_arr);
for($i = 0; $i < $cnt; $i++)
{
$url = $new_arr[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'uTorrent/1800(18400)');
curl_multi_add_handle($master, $curl_arr[$i]);
}
do
{
curl_multi_exec($master, $running);
}
while($running > 0);
curl_multi_close($master);
return '';
}
}