<?php
/**
* Info about a torrent metadata file
*
* @author Charalampos Pournaris
*/
class TorrentInfo {
/**
* Torrent file bdecoder
*
* @var BDecode
*/
private $torrent = null;
/**
* All the pieces as a single string
*
* @var string
*/
private $pieces = '';
/**
* All pieces length
*
* @var integer
*/
private $pieces_length = 0;
/**
* Piece length used to create torrent
*
* @var integer
*/
private $piece_length = 0;
/**
* Total length of the torrent data
*
* @var integer
*/
private $total_length = 0;
/**
* The info hash string (sha1 hashed)
*
* @var string
*/
private $info_hash = '';
/**
* Create a new torrentinfo object
*
* @param array $torrent
*/
public function __construct($torrent = array()) {
$encoder = new BEncode ( );
if (! empty ( $torrent )) {
$this->torrent = $torrent;
$this->pieces = bin2hex ( $torrent->result ['info'] ['pieces'] );
$this->pieces_length = strlen ( $this->pieces );
$this->piece_length = $torrent->result ['info'] ['piece length'];
$this->info_hash = sha1 ( $encoder->encode ( $torrent->result ['info'] ) );
}
}
/**
* Getter method for pieces field
*
* @return string
*/
public function getPieces() {
return $this->pieces;
}
/**
* Getter method for info_hash field
*
* @return string
*/
public function getInfoHash() {
return $this->info_hash;
}
/**
* Getter method for piece_length field
*
* @return integer
*/
public function getPieceLength() {
return $this->piece_length;
}
/**
* Returns the hash (sha1'd) for a specific piece
*
* @param integer $piece_index
* @return string
*/
public function getPieceHash($piece_index) {
return substr ( $this->pieces, ($piece_index - 1) * 40, 40 );
}
/**
* Checks pieces for equality
*
* @param integer $piece_index
* @param string $hash
* @return boolean
*/
public function equalPieceHash($piece_index, $hash) {
return substr_compare ( $this->pieces, $hash, ($piece_index - 1) * 40, 40 ) == 0;
}
/**
* Returns the file list
*
* @return array
*/
public function getFiles() {
return $this->torrent->result ['info'] ['files'];
}
/**
* Returns the torrent target directory/filename
*
* @return string
*/
public function getName() {
return $this->torrent->result ['info'] ['name'];
}
/**
* Calculate the total length of the torrent data
*
* @return integer
*/
public function calculateLength() {
$files = $this->getFiles ();
if ($this->total_length != 0 || empty ( $files ))
return;
foreach ( $files as $fileinfo ) {
$this->total_length += $fileinfo ['length'];
}
return $this->total_length;
}
/**
* Return torrent data total length
*
* @return integer
*/
public function getTotalLength() {
return $this->total_length;
}
/**
* Returns an array with the filenames (fullpath) and size
*
* @param string $rootpath
* @return array
*/
public function getFilePathList($rootpath = null) {
$filelist = array();
foreach ( $this->getFiles() as $fileinfo ) {
$filelist[] = array('path'=>(is_null($rootpath) ? $this->getName() : $rootpath) . '/' . implode ( '/', $fileinfo ['path'] ), 'size'=>$fileinfo['length']);
}
return $filelist;
}
}
?>