Changeset 84

Show
Ignore:
Timestamp:
12.06.2008 11:52:16 (7 months ago)
Author:
m
Message:

Tagging 2.2.0 release

Files:
1 added
10 modified
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/File/Bittorrent/Decode.php

    r76 r84  
    4848*/ 
    4949require_once 'PEAR.php'; 
    50 require_once 'PHP/Compat.php'; 
    5150require_once 'File/Bittorrent/Encode.php'; 
    52  
    53 /** 
    54 * Load replacement functions 
    55 */ 
    56 PHP_Compat::loadFunction('file_get_contents'); 
     51require_once 'File/Bittorrent/Exception.php'; 
    5752 
    5853/** 
     
    8176    * @var string   Name of the torrent 
    8277    */ 
    83     var $name = ''; 
     78    protected $name = ''; 
    8479 
    8580    /** 
    8681    * @var string   Filename of the torrent 
    8782    */ 
    88     var $filename = ''; 
     83    protected $filename = ''; 
    8984 
    9085    /** 
    9186    * @var string   Comment 
    9287    */ 
    93     var $comment = ''; 
     88    protected $comment = ''; 
    9489 
    9590    /** 
    9691    * @var int   Creation date as unix timestamp 
    9792    */ 
    98     var $date = 0; 
     93    protected $date = 0; 
    9994 
    10095    /** 
    10196    * @var array    Files in the torrent 
    10297    */ 
    103     var $files = array(); 
     98    protected $files = array(); 
    10499 
    105100    /** 
    106101    * @var int      Size of of the full torrent (after download) 
    107102    */ 
    108     var $size = 0; 
     103    protected $size = 0; 
    109104 
    110105    /** 
    111106    * @var string   Signature of the software which created the torrent 
    112107    */ 
    113     var $created_by = ''; 
     108    protected $created_by = ''; 
    114109 
    115110    /** 
    116111    * @var string    tracker (the tracker the torrent has been received from) 
    117112    */ 
    118     var $announce = ''; 
     113    protected $announce = ''; 
    119114 
    120115    /** 
    121116    * @var array     List of known trackers for the torrent 
    122117    */ 
    123     var $announce_list = array(); 
     118    protected $announce_list = array(); 
    124119 
    125120    /** 
    126121    * @var string   Source string 
    127     * @access private 
    128     */ 
    129     var $_source = ''; 
     122    */ 
     123    protected $source = ''; 
    130124 
    131125    /** 
    132126    * @var int      Source length 
    133     * @access private 
    134     */ 
    135     var $_source_length = 0; 
     127    */ 
     128    protected $source_length = 0; 
    136129 
    137130    /** 
    138131    * @var int      Current position of the string 
    139     * @access private 
    140     */ 
    141     var $_position = 0; 
     132    */ 
     133    protected $position = 0; 
    142134 
    143135    /** 
    144136    * @var string   Info hash 
    145137    */ 
    146     var $info_hash; 
    147  
    148     /** 
    149     * @var mixed    The last error object or null if no error has occurred. 
    150     */ 
    151     var $last_error; 
     138    protected $info_hash; 
    152139 
    153140    /** 
    154141    * @var array    Decoded data from File_Bittorrent_Decode::decodeFile() 
    155142    */ 
    156     var $decoded = array(); 
     143    protected $decoded = array(); 
    157144 
    158145    /** 
     
    161148    * @param string 
    162149    * @return mixed 
     150    * @throws File_Bittorrent_Exception if decoded data contains trailing garbage 
    163151    */ 
    164152    function decode($str) 
    165153    { 
    166         $this->_source = $str; 
    167         $this->_position  = 0; 
    168         $this->_source_length = strlen($this->_source); 
    169         $result = $this->_bdecode(); 
    170         if ($this->_position < $this->_source_length) { 
    171             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::decode() - Trailing garbage in file.'); 
    172             return false; 
     154        $this->source = $str; 
     155        $this->position  = 0; 
     156        $this->source_length = strlen($this->source); 
     157        $result = $this->bdecode(); 
     158        if ($this->position < $this->source_length) { 
     159            throw new File_Bittorrent_Exception('Trailing garbage in file.', File_Bittorrent_Exception::decode); 
    173160        } 
    174161        return $result; 
     
    180167    * @param string    Filename 
    181168    * @return mixed    Returns an arrayon success or false on error 
     169    * @throws File_Bittorrent_Exception if no file given or bencoded data is corrupt 
    182170    */ 
    183171    function decodeFile($file) 
     
    185173        // Check file 
    186174        if (!is_file($file)) { 
    187             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::decode() - Not a file.', null, null, "Given filename '$file' is not a valid file."); 
    188             return false; 
     175            throw new File_Bittorrent_Exception('Given filename \'' . $file . '\' is not a valid file.', File_Bittorrent_Exception::source); 
    189176        } 
    190177 
     
    199186        $this->announce      = ''; 
    200187        $this->announce_list = array(); 
    201         $this->_position     = 0; 
     188        $this->position     = 0; 
    202189        $this->info_hash     = ''; 
    203190 
    204191        // Decode .torrent 
    205         $this->_source = file_get_contents($file); 
    206         $this->_source_length = strlen($this->_source); 
    207         $this->decoded = $this->_bdecode(); 
     192        $this->source = file_get_contents($file); 
     193        $this->source_length = strlen($this->source); 
     194        $this->decoded = $this->bdecode(); 
    208195        if (!is_array($this->decoded)) { 
    209             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::decode() - Corrupted bencoded data.', null, null, "Failed to decode data from file '$file'."); 
    210             return false; 
     196            throw new File_Bittorrent_Exception('Corrupted bencoded data. Failed to decode data from file \'$file\'.', File_Bittorrent_Exception::decode); 
    211197        } 
    212198 
     
    281267            'announce'      => $this->announce, 
    282268            'announce_list' => $this->announce_list, 
     269            'info_hash'     => $this->info_hash, 
    283270        ); 
    284271    } 
     
    287274    * Decode a BEncoded String 
    288275    * 
    289     * @access private 
    290276    * @return mixed    Returns the representation of the data in the BEncoded string or false on error 
    291277    */ 
    292     function _bdecode() 
    293     { 
    294         switch ($this->_getChar()) { 
     278    protected function bdecode() 
     279    { 
     280        switch ($this->getChar()) { 
    295281        case 'i': 
    296             $this->_position++; 
    297             return $this->_decode_int(); 
     282            $this->position++; 
     283            return $this->decode_int(); 
    298284            break; 
    299285        case 'l': 
    300             $this->_position++; 
    301             return $this->_decode_list(); 
     286            $this->position++; 
     287            return $this->decode_list(); 
    302288            break; 
    303289        case 'd': 
    304             $this->_position++; 
    305             return $this->_decode_dict(); 
     290            $this->position++; 
     291            return $this->decode_dict(); 
    306292            break; 
    307293        default: 
    308             return $this->_decode_string(); 
     294            return $this->decode_string(); 
    309295        } 
    310296    } 
     
    318304    * would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee 
    319305    * 
    320     * @access private 
    321306    * @return array 
    322     */ 
    323     function _decode_dict() 
     307    * @throws File_Bittorrent_Exception if bencoded dictionary contains invalid data 
     308    */ 
     309    protected function decode_dict() 
    324310    { 
    325311        $return = array(); 
    326312        $ended = false; 
    327313        $lastkey = NULL; 
    328         while ($char = $this->_getChar()) { 
     314        while ($char = $this->getChar()) { 
    329315            if ($char == 'e') { 
    330316                $ended = true; 
     
    332318            } 
    333319            if (!ctype_digit($char)) { 
    334                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Invalid dictionary key.'); 
    335                 $return = false; 
    336                 break; 
    337             } 
    338             $key = $this->_decode_string(); 
     320                throw new File_Bittorrent_Exception('Invalid dictionary key.', File_Bittorrent_Exception::decode); 
     321            } 
     322            $key = $this->decode_string(); 
    339323            if (isset($return[$key])) { 
    340                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Duplicate dictionary key.'); 
    341                 $return = false; 
    342                 break; 
     324                throw new File_Bittorrent_Exception('Duplicate dictionary key.', File_Bittorrent_Exception::decode); 
    343325            } 
    344326            if ($key < $lastkey) { 
    345                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Missorted dictionary key.'); 
    346                 $return = false; 
    347                 break; 
    348             } 
    349             $val = $this->_bdecode(); 
     327                throw new File_Bittorrent_Exception('Missorted dictionary key.', File_Bittorrent_Exception::decode); 
     328            } 
     329            $val = $this->bdecode(); 
    350330            if ($val === false) { 
    351                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Invalid value.'); 
    352                 $return = false; 
    353                 break; 
     331                throw new File_Bittorrent_Exception('Invalid value.', File_Bittorrent_Exception::decode); 
    354332            } 
    355333            $return[$key] = $val; 
     
    357335        } 
    358336        if (!$ended) { 
    359             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Unterminated dictionary.'); 
    360             $return = false; 
    361         } 
    362         $this->_position++; 
     337            throw new File_Bittorrent_Exception('Unterminated dictionary.', File_Bittorrent_Exception::decode); 
     338        } 
     339        $this->position++; 
    363340        return $return; 
    364341    } 
     
    371348    * would bEncode to 11:BitTorrents. 
    372349    * 
    373     * @access private 
    374350    * @return string|false 
    375     */ 
    376     function _decode_string() 
     351    * @throws File_Bittorrent_Exception if bencoded data is invalid 
     352    */ 
     353    protected function decode_string() 
    377354    { 
    378355        // Check for bad leading zero 
    379         if (substr($this->_source, $this->_position, 1) == '0' and 
    380         substr($this->_source, $this->_position + 1, 1) != ':') { 
    381             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_string() - Leading zero in string length.'); 
    382             return false; 
     356        if (substr($this->source, $this->position, 1) == '0' and 
     357        substr($this->source, $this->position + 1, 1) != ':') { 
     358            throw new File_Bittorrent_Exception('Leading zero in string length.', File_Bittorrent_Exception::decode); 
    383359        } 
    384360        // Find position of colon 
    385361        // Supress error message if colon is not found which may be caused by a corrupted or wrong encoded string 
    386         if (!$pos_colon = @strpos($this->_source, ':', $this->_position)) { 
    387             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_string() - Colon not found.'); 
    388             return false; 
     362        if (!$pos_colon = @strpos($this->source, ':', $this->position)) { 
     363            throw new File_Bittorrent_Exception('Colon not found.', File_Bittorrent_Exception::decode); 
    389364        } 
    390365        // Get length of string 
    391         $str_length = intval(substr($this->_source, $this->_position, $pos_colon)); 
    392         if ($str_length + $pos_colon + 1 > $this->_source_length) { 
    393             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_string() - Input too short for string length.'); 
    394             return false; 
     366        $str_length = intval(substr($this->source, $this->position, $pos_colon)); 
     367        if ($str_length + $pos_colon + 1 > $this->source_length) { 
     368            throw new File_Bittorrent_Exception('Input too short for string length.', File_Bittorrent_Exception::decode); 
    395369        } 
    396370        // Get string 
     
    398372            $return = ''; 
    399373        } else { 
    400             $return = substr($this->_source, $pos_colon + 1, $str_length); 
     374            $return = substr($this->source, $pos_colon + 1, $str_length); 
    401375        } 
    402376        // Move Pointer after string 
    403         $this->_position = $pos_colon + $str_length + 1; 
     377        $this->position = $pos_colon + $str_length + 1; 
    404378        return $return; 
    405379    } 
     
    412386    * i-3272002e. 
    413387    * 
    414     * @access private 
    415388    * @return int 
    416     */ 
    417     function _decode_int() 
    418     { 
    419         $pos_e  = strpos($this->_source, 'e', $this->_position); 
    420         $p = $this->_position; 
     389    * @throws File_Bittorrent_Exception if bencoded data is invalid 
     390    */ 
     391    protected function decode_int() 
     392    { 
     393        $pos_e  = strpos($this->source, 'e', $this->position); 
     394        $p = $this->position; 
    421395        if ($p === $pos_e) { 
    422             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_int() - Empty integer.'); 
    423             return false; 
    424         } 
    425         if (substr($this->_source, $this->_position, 1) == '-') $p++; 
    426         if (substr($this->_source, $p, 1) == '0' and 
    427         ($p != $this->_position or $pos_e > $p+1)) { 
    428             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_int() - Leading zero in integer.'); 
    429             return false; 
     396            throw new File_Bittorrent_Exception('Empty integer.', File_Bittorrent_Exception::decode); 
     397        } 
     398        if (substr($this->source, $this->position, 1) == '-') $p++; 
     399        if (substr($this->source, $p, 1) == '0' and 
     400        ($p != $this->position or $pos_e > $p+1)) { 
     401            throw new File_Bittorrent_Exception('Leading zero in integer.', File_Bittorrent_Exception::decode); 
    430402        } 
    431403        for ($i = $p; $i < $pos_e-1; $i++) { 
    432             if (!ctype_digit(substr($this->_source, $i, 1))) { 
    433                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_int() - Non-digit characters in integer.'); 
    434                 return false; 
     404            if (!ctype_digit(substr($this->source, $i, 1))) { 
     405                throw new File_Bittorrent_Exception('Non-digit characters in integer.', File_Bittorrent_Exception::decode); 
    435406            } 
    436407        } 
     
    438409        // overflow. The "+ 0" accomplishes exactly that, using the internal casting 
    439410        // logic of PHP 
    440         $return = substr($this->_source, $this->_position, $pos_e - $this->_position) + 0; 
    441         $this->_position = $pos_e + 1; 
     411        $return = substr($this->source, $this->position, $pos_e - $this->position) + 0; 
     412        $this->position = $pos_e + 1; 
    442413        return $return; 
    443414    } 
     
    452423    * would bEncode to li1e7:Mondunai3el3:Sub4:Listee 
    453424    * 
    454     * @access private 
    455425    * @return array 
    456     */ 
    457     function _decode_list() 
     426    * @throws File_Bittorrent_Exception if bencoded data is invalid 
     427    */ 
     428    protected function decode_list() 
    458429    { 
    459430        $return = array(); 
    460         $char = $this->_getChar(); 
     431        $char = $this->getChar(); 
    461432        $p1 = $p2 = 0; 
    462433        if ($char === false) { 
    463             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_list() - Unterminated list.'); 
    464             return false; 
    465         } 
    466         while ($char !== false && substr($this->_source, $this->_position, 1) != 'e') { 
    467             $p1 = $this->_position; 
    468             $val = $this->_bdecode(); 
    469             $p2 = $this->_position; 
     434            throw new File_Bittorrent_Exception('Unterminated list.', File_Bittorrent_Exception::decode); 
     435        } 
     436        while ($char !== false && substr($this->source, $this->position, 1) != 'e') { 
     437            $p1 = $this->position; 
     438            $val = $this->bdecode(); 
     439            $p2 = $this->position; 
    470440            // Empty does not work here 
    471441            if($p1 == $p2)  { 
    472                 $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_list() - Unterminated list.'); 
    473                 return false; 
     442                throw new File_Bittorrent_Exception('Unterminated list.', File_Bittorrent_Exception::decode); 
    474443            } 
    475444            $return[] = $val; 
    476445        } 
    477         $this->_position++; 
     446        $this->position++; 
    478447        return $return; 
    479448    } 
     
    482451    * Get the char at the current position 
    483452    * 
    484     * @access private 
    485453    * @return string|false 
    486454    */ 
    487     function _getChar() 
    488     { 
    489         if (empty($this->_source)) return false; 
    490         if ($this->_position >= $this->_source_length) return false; 
    491         return substr($this->_source, $this->_position, 1); 
     455    protected function getChar() 
     456    { 
     457        if (empty($this->source)) return false; 
     458        if ($this->position >= $this->source_length) return false; 
     459        return substr($this->source, $this->position, 1); 
    492460    } 
    493461 
     
    496464    * 
    497465    * @return array|false 
     466    * @throws File_Bittorrent_Exception if allow_url_fopen is disabled or scrape data is invalid 
    498467    */ 
    499468    function getStats() 
     
    501470        // Check if we can access remote data 
    502471        if (!ini_get('allow_url_fopen')) { 
    503             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::getStats() - "allow_url_fopen" must be enabled.'); 
     472            throw new File_Bittorrent_Exception('\'allow_url_fopen\' must be enabled.', File_Bittorrent_Exception::source); 
    504473            return false; 
    505474        } 
     
    508477        $scrape_url = preg_replace('/\/announce$/', '/scrape', $this->announce) . '?info_hash=' . urlencode($packed_hash); 
    509478        $scrape_data = file_get_contents($scrape_url); 
    510         $stats = $this->decode($scrape_data); 
    511         if (!isset($stats['files'][$packed_hash])) { 
    512             $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::getStats() - Invalid scrape data: "' . $scrape_data . '"'); 
    513             return false; 
    514         } 
     479        try { 
     480            $stats = $this->decode($scrape_data); 
     481        } catch (File_Bittorrent_Exception $e) { 
     482            throw new File_Bittorrent_Exception('Invalid scrape data: \'' . $scrape_data . '\'', File_Bittorrent_Exception::decode); 
     483        } 
     484        if (!isset($stats['files'][$packed_hash])) { 
     485            throw new File_Bittorrent_Exception('Invalid scrape data: \'' . $scrape_data . '\'', File_Bittorrent_Exception::decode); 
     486        } 
    515487        return $stats['files'][$packed_hash]; 
    516488    } 
     489 
     490    /** 
     491    * Returns the Name of the torrent 
     492    * 
     493    * @return string 
     494    */ 
     495    function getName() 
     496    { 
     497        return $this->name; 
     498    } 
     499 
     500    /** 
     501    * Returns the Filename of the torrent 
     502    * 
     503    * @return string 
     504    */ 
     505    function getFilename() 
     506    { 
     507        return $this->filename; 
     508    } 
     509 
     510    /** 
     511    * Returns the Comment of the torrent 
     512    * 
     513    * @return string 
     514    */ 
     515    function getComment() 
     516    { 
     517        return $this->comment; 
     518    } 
     519 
     520    /** 
     521    * Returns the Date of the torrent 
     522    * 
     523    * @return string 
     524    */ 
     525    function getDate() 
     526    { 
     527        return $this->date; 
     528    } 
     529 
     530    /** 
     531    * Returns the Creator info of the torrent 
     532    * 
     533    * @return string 
     534    */ 
     535    function getCreator() 
     536    { 
     537        return $this->created_by; 
     538    } 
     539 
     540    /** 
     541    * Returns the Files of the torrent 
     542    * 
     543    * @return array 
     544    */ 
     545    function getFiles() 
     546    { 
     547        return $this->files; 
     548    } 
     549 
     550    /** 
     551    * Returns the the tracker the torrent has been received from 
     552    * 
     553    * @return string 
     554    */ 
     555    function getAnnounce() 
     556    { 
     557        return $this->announce; 
     558    } 
     559 
     560    /** 
     561    * Returns the known tracker list of the torrent 
     562    * 
     563    * @return array 
     564    */ 
     565    function getAnnounceList() 
     566    { 
     567        return $this->announe_list; 
     568    } 
     569 
     570    /** 
     571    * Returns the info hash of the torrent 
     572    * 
     573    * @return string 
     574    */ 
     575    function getInfoHash() 
     576    { 
     577        return $this->info_hash; 
     578    } 
    517579} 
    518580 
  • trunk/File/Bittorrent/Encode.php

    r76 r84  
    4646*/ 
    4747require_once 'PEAR.php'; 
     48require_once 'File/Bittorrent/Exception.php'; 
    4849 
    4950/** 
     
    7273    * @param mixed    Variable to encode 
    7374    * @return string 
     75    * @throws File_Bittorrent_Exception if unsupported type should be encoded 
    7476    */ 
    7577    function encode($mixed) 
     
    8688            return $this->encode_array($mixed); 
    8789        default: 
    88             PEAR::raiseError('File_Bittorrent_Encode::encode() - Unsupported type.', null, null, "Variable must be one of 'string', 'integer', 'double' or 'array'"); 
     90            throw new File_Bittorrent_Exception('Unsupported type. Variable must be one of \'string\', \'integer\', \'double\' or \'array\'', File_Bittorrent_Exception::encode); 
    8991        } 
    9092    } 
     
    139141    * @return string 
    140142    */ 
    141     function encode_array($array) 
     143    function encode_array(array $array) 
    142144    { 
    143145        // Check for strings in the keys 
  • trunk/File/Bittorrent/MakeTorrent.php

    r76 r84  
    4242require_once 'PEAR.php'; 
    4343require_once 'File/Bittorrent/Encode.php'; 
     44require_once 'File/Bittorrent/Exception.php'; 
    4445 
    4546/** 
     
    5859    /** 
    5960     * @var string Path to the file or directory to create the torrent from. 
    60      * @access private 
    61      */ 
    62     var $_path = ''; 
     61     */ 
     62    protected $path = ''; 
    6363 
    6464    /** 
    6565     * @var bool Whether or not $path is a file 
    66      * @access private 
    67      */ 
    68     var $_is_file = false; 
     66     */ 
     67    protected $is_file = false; 
    6968 </