public function testInfoHash()
 {
     $File_Bittorrent2_Decode = new File_Bittorrent2_Decode();
     $File_Bittorrent2_Decode->decodeFile(self::$torrent);
     exec('torrentinfo-console ' . escapeshellarg(self::$torrent), $bt);
     $this->assertEquals($File_Bittorrent2_Decode->getInfoHash(), substr($bt[3], strpos($bt[3], ':') + 2));
 }
Example #2
0
 public static function upload($title, $description, $category_cid, $file, $user)
 {
     // Verify title wasn't garbage
     if (strlen($title) < 2) {
         return array('status' => false, 'message' => 'The torrent\'s title is too short!');
     }
     // Verify category was set properly
     if (!Category::find(array('cid' => $category_cid))) {
         return array('status' => false, 'message' => 'The specified category does not exist!');
     }
     // Grab the metainfo for the torrent
     $metainfo = new File_Bittorrent2_Decode();
     try {
         $metainfo->decodeFile($file['tmp_name']);
     } catch (File_Bittorrent2_Exception $e) {
         return array('status' => false, 'message' => 'The .torrent file was either invalid or corrupt!');
     }
     // Ensure metainfo is private
     if (!$metainfo->getPrivate()) {
         return array('status' => false, 'message' => 'Your torrent is not flagged as private! Please recreate it as a private torrent before attempting to add it again.');
     }
     // Ensure tracker was set properly
     if (!$metainfo->getAnnounce() == $user->url_announce) {
         return array('status' => false, 'message' => 'The announce URL is not properly set in the torrent!');
     }
     // Ensure metainfo is unique
     $info_hash = '0x' . $metainfo->getInfoHash();
     $query = 'SELECT * FROM xbt_files WHERE info_hash = ' . $info_hash;
     $results = DB::query($query);
     if (mysql_num_rows($results) > 0) {
         return array('status' => false, 'message' => 'Duplicate torrent detected! Either you or somebody else already added it.');
     }
     // Grab basic metainfo properties
     $path = DB::escape(basename($file['name']));
     $title = DB::escape($title);
     $description = sanitize_body_text($description);
     $description = DB::escape($description);
     $size = 0;
     $user_uid = $user->uid;
     // Get total torrent size
     foreach ($metainfo->getFiles() as $f) {
         $size += $f['size'];
     }
     // Insert metainfo into database
     $query = "INSERT INTO xbt_files (info_hash, mtime, ctime, path, title, description, size, user_uid, category_cid) VALUES (" . $info_hash . ", unix_timestamp(), unix_timestamp(), '" . $path . "', '" . $title . "', '" . $description . "', " . $size . ", " . $user_uid . ", " . $category_cid . ")";
     $results = DB::query($query, true);
     // Grab the metainfo_id of the newly inserted metainfo
     $handle = DB::get_handle();
     $metainfo_id = mysql_insert_id($handle);
     foreach ($metainfo->getFiles() as $f) {
         $f_path = DB::escape($f['filename']);
         $f_size = DB::escape($f['size']);
         $query = "INSERT INTO xbt_paths (torrent_fid, path, size) VALUES (" . $metainfo_id . ", '" . $f_path . "', " . $f_size . ")";
         $results = DB::query($query, true);
     }
     // Move .torrent to path using fid as filename
     $dest = TORRENT_BASE_PATH . '/' . $metainfo_id . '.torrent';
     move_uploaded_file($file['tmp_name'], $dest);
     return array('status' => true, 'message' => 'The torrent has been successfully uploaded. Congratulations!', 'id' => $metainfo_id);
 }
 *
 * Usage:
 *   # php scrape.php -t file
 *
 * @author Markus Tacker <*****@*****.**>
 * @version $Id$
 */
// Includes
require_once 'File/Bittorrent2/Decode.php';
require_once 'Console/Getargs.php';
// Get filename from command line
$args_config = array('torrent' => array('short' => 't', 'min' => 1, 'max' => 1, 'desc' => 'Filename of the torrent'));
$args =& Console_Getargs::factory($args_config);
if (PEAR::isError($args) or !($torrent = $args->getValue('torrent'))) {
    echo Console_Getargs::getHelp($args_config) . "\n";
    exit;
}
if (!is_readable($torrent)) {
    echo 'ERROR: "' . $torrent . "\" is not readable.\n";
    exit;
}
// Decode the torrent
$File_Bittorrent2_Decode = new File_Bittorrent2_Decode();
$File_Bittorrent2_Decode->decodeFile($torrent);
echo "\nStatistics\n";
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
echo 'Tracker:            ' . $File_Bittorrent2_Decode->getAnnounce() . "\n";
echo 'info hash:          ' . $File_Bittorrent2_Decode->getInfoHash() . "\n";
foreach ($File_Bittorrent2_Decode->getStats() as $key => $val) {
    echo str_pad($key . ':', 20) . $val . "\n";
}
 * Info-Hash Test
 * Compares the info_hash compution of this package to the original program implementation
 *
 * Usage:
 *   # php infohash.php -t file.torrent
 *
 * @author Markus Tacker <*****@*****.**>
 * @version $Id$
 */
error_reporting(E_ALL);
// Includes
require_once 'File/Bittorrent2/Decode.php';
require_once 'Console/Getargs.php';
// Get filename from command line
$args_config = array('torrent' => array('short' => 't', 'min' => 1, 'max' => 1, 'desc' => 'Filename of the torrent'));
$args =& Console_Getargs::factory($args_config);
if (PEAR::isError($args) or !($torrent = $args->getValue('torrent'))) {
    echo Console_Getargs::getHelp($args_config) . "\n";
    exit;
}
if (!is_readable($torrent)) {
    echo 'ERROR: "' . $torrent . "\" is not readable.\n";
    exit;
}
$File_Bittorrent2_Decode = new File_Bittorrent2_Decode();
$File_Bittorrent2_Decode->decodeFile($torrent);
echo "\nInfo Hash\n";
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
echo "This:                " . $File_Bittorrent2_Decode->getInfoHash() . "\n";
exec('torrentinfo-console ' . escapeshellarg($torrent), $bt);
echo "torrentinfo-console: " . substr($bt[3], strpos($bt[3], ':') + 2) . "\n";