1<?php
2
3    // +----------------------------------------------------------------------+
4    // | Decode and Encode data in Bittorrent format                          |
5    // +----------------------------------------------------------------------+
6    // | Copyright (C) 2004-2005 Markus Tacker <m@tacker.org>                 |
7    // +----------------------------------------------------------------------+
8    // | This library is free software; you can redistribute it and/or        |
9    // | modify it under the terms of the GNU Lesser General Public           |
10    // | License as published by the Free Software Foundation; either         |
11    // | version 2.1 of the License, or (at your option) any later version.   |
12    // |                                                                      |
13    // | This library is distributed in the hope that it will be useful,      |
14    // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
15    // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
16    // | Lesser General Public License for more details.                      |
17    // |                                                                      |
18    // | You should have received a copy of the GNU Lesser General Public     |
19    // | License along with this library; if not, write to the                |
20    // | Free Software Foundation, Inc.                                       |
21    // | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA               |
22    // +----------------------------------------------------------------------+
23
24    /**
25    * File_Bittorrent2 Example
26    * Get Info from a .torrent file
27    *
28    * Usage:
29    *   # php torrentinfo.php -t file.torrent
30    *
31    * @author Markus Tacker <m@tacker.org>
32    * @version $Id: torrentinfo.php 77 2007-08-26 09:42:22Z m $
33    */
34
35    // Includes
36    require_once 'File/Bittorrent2/Decode.php';
37    require_once 'Console/Getargs.php';
38
39    // Get filename from command line
40    $args_config = array(
41        'torrent' => array(
42            'short' => 't',
43            'min' => 1,
44            'max' => 1,
45            'desc' => 'Filename of the torrent'
46        ),
47    );
48    $args =& Console_Getargs::factory($args_config);
49    if (PEAR::isError($args) or !($torrent = $args->getValue('torrent'))) {
50        echo Console_Getargs::getHelp($args_config)."\n";
51        exit;
52    }
53
54    if (!is_readable($torrent)) {
55        echo 'ERROR: "' . $torrent . "\" is not readable.\n";
56        exit;
57    }
58
59    $File_Bittorrent2_Decode = new File_Bittorrent2_Decode;
60    $info = $File_Bittorrent2_Decode->decodeFile($torrent);
61
62    foreach ($info as $key => $val) {
63        echo str_pad($key . ': ', 20, ' ', STR_PAD_LEFT);
64        switch($key) {
65        case 'files':
66            $n = 1;
67            $files_n = count($val);
68            $n_length = strlen($files_n);
69            echo '(' . $files_n . ")\n";
70            foreach ($val as $file) {
71                echo str_repeat(' ', 20) . '' . str_pad($n, $n_length, ' ', PAD_LEFT) . ': ' . $file['filename'] . "\n";
72                $n++;
73            }
74            break;
75        case 'announce_list':
76            echo "\n";
77            foreach ($val as $list) {
78                echo str_repeat(' ', 20) . '- ' . join(', ', $list) . "\n";
79            }
80            break;
81        default:
82            echo $val . "\n";
83        }
84    }
85
86    echo "\n";
87
88?>