diff --git a/announce.php b/announce.php index fc74480..88ac91d 100644 --- a/announce.php +++ b/announce.php @@ -2,6 +2,7 @@ require("../php/config.php"); require("network.php"); require("bencoder.php"); +require("peer.php"); $DB_NAME = "torrent"; $ANN_MIN_INTERVAL = 15; $ANN_INTERVAL = 60; @@ -74,10 +75,8 @@ $conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASS); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("REPLACE INTO `peers` (info_hash, ip, port, update_time) VALUES (:info_hash, :ip, :port, :update_time)"); -$stmt->bindValue(":info_hash", $hash, PDO::PARAM_STR); -$stmt->bindValue(":ip", is_loopback($ip) ? $externalIp : $ip, PDO::PARAM_STR); -$stmt->bindValue(":port", $port, PDO::PARAM_INT); -$stmt->bindValue(":update_time", $now, PDO::PARAM_STR); +$peer = new Peer($hash, is_loopback($ip) ? $externalIp : $ip, $port); +$peer->bind($stmt); $stmt->execute(); $peers = ''; diff --git a/peer.php b/peer.php new file mode 100644 index 0000000..4d1f418 --- /dev/null +++ b/peer.php @@ -0,0 +1,26 @@ +info_hash = $hash; + $this->ip = $ip; + $this->port = $port; + + $date = new DateTime("now"); + $this->update_time = $date->format('Y-m-d H:i:s'); + } + + public function bind($stmt) { + $stmt->bindValue(":info_hash", $this->info_hash, PDO::PARAM_STR); + $stmt->bindValue(":ip", $this->ip, PDO::PARAM_STR); + $stmt->bindValue(":port", $this->port, PDO::PARAM_INT); + $stmt->bindValue(":update_time", $this->update_time, PDO::PARAM_STR); + } +} + +?> \ No newline at end of file