<?php include "bencode.php"; include "func.php"; include "config.php"; if (isset($_POST["submit"])) { if (isset($_FILES["fileToUpload"])) { $myfile = fopen($_FILES["fileToUpload"]["tmp_name"], "r") or die("Unable to open file!"); $str_file = fread($myfile, filesize($_FILES["fileToUpload"]["tmp_name"])); fclose($myfile); $r = ben_decode($str_file); if (is_valid_torrent($r)) { $r[0]['announce'] = $config_announce; $r[0]['announce-list'] = 0; $str_file = ben_encode($r[0]['info']); $sha1_torrent = sha1('d' . $str_file . 'e'); echo $sha1_torrent . "<br>"; $str_file = ben_encode($r); $torrent_name = mysqli_real_escape_string($mysq_link, get_torrent_name($r)); $sql = "INSERT INTO torrents (name, sha) VALUES ('{$torrent_name}', '{$sha1_torrent}')"; if (mysqli_query($mysq_link, $sql)) { echo "New record created successfully"; $myfile = fopen($config_upload_dir . $sha1_torrent . ".torrent", "w"); fwrite($myfile, $str_file, strlen($str_file)); fclose($myfile); } else { echo "Error: " . $sql . "<br>" . mysqli_error($mysq_link); } } } }
$del_sec = $config_interval * 3; $sql = "DELETE FROM peers WHERE date < (NOW() - INTERVAL {$del_sec} SECOND)"; $result = mysqli_query($mysq_link, $sql); $arr[0]['interval'] = $config_interval; $arr[0]['peers'] = array(); $ip = mysqli_real_escape_string($mysq_link, getip()); $port = (int) $_GET['port']; $peer_id = mysqli_real_escape_string($mysq_link, bin2hex($_GET['peer_id'])); if (isset($_GET['event'])) { if ($_GET['event'] == "stopped") { $sql = "DELETE FROM peers WHERE sha='{$sha}' AND ip='{$ip}' AND port='{$port}' AND peer_id='{$peer_id}'"; mysqli_query($mysq_link, $sql); exit; } } $sql = "INSERT INTO peers (sha, date, ip, port, peer_id) VALUES ('{$sha}', CURRENT_TIMESTAMP, '{$ip}', '{$port}', '{$peer_id}') ON DUPLICATE KEY UPDATE date=VALUES(date), peer_id=VALUES(peer_id)"; mysqli_query($mysq_link, $sql); $sql = "SELECT peer_id, ip, port FROM peers WHERE sha='{$sha}' LIMIT 30"; $result = mysqli_query($mysq_link, $sql); $index = 0; if ($result && mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { $arr[0]['peers'][$index] = array(); $arr[0]['peers'][$index]['ip'] = $row["ip"]; $arr[0]['peers'][$index]['peer id'] = hex2bin($row["peer_id"]); $arr[0]['peers'][$index]['port'] = (int) $row["port"]; $index++; } } echo ben_encode($arr);
function ben_encode(&$arr) { $ret = ""; $start = 0; if (is_array($arr)) { foreach ($arr as $key => $value) { if (ben_is_assoc($arr)) { if (is_integer($key)) { $ret .= "i" . $key . "e"; } else { if (is_string($key)) { $ret .= strlen($key) . ':' . $key; } } } if (is_integer($value)) { $ret .= "i" . $value . "e"; } else { if (is_string($value)) { $ret .= strlen($value) . ':' . $value; } else { if (is_array($value)) { if (ben_is_assoc($value)) { $ret .= "d" . ben_encode($value) . "e"; } else { $ret .= "l" . ben_encode($value) . "e"; } } } } } } return $ret; }