Changeset 76 for trunk

Show
Ignore:
Timestamp:
25.08.2007 17:11:16 (17 months ago)
Author:
m
Message:

Reverting changes in trunk. Adding trunk2 for File_Bittorrent2

Location:
trunk
Files:
1 removed
10 modified

Legend:

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

    r74 r76  
    4848*/ 
    4949require_once 'PEAR.php'; 
     50require_once 'PHP/Compat.php'; 
    5051require_once 'File/Bittorrent/Encode.php'; 
    51 require_once 'File/Bittorrent/Exception.php'; 
     52 
     53/** 
     54* Load replacement functions 
     55*/ 
     56PHP_Compat::loadFunction('file_get_contents'); 
    5257 
    5358/** 
     
    7681    * @var string   Name of the torrent 
    7782    */ 
    78     protected $name = ''; 
     83    var $name = ''; 
    7984 
    8085    /** 
    8186    * @var string   Filename of the torrent 
    8287    */ 
    83     protected $filename = ''; 
     88    var $filename = ''; 
    8489 
    8590    /** 
    8691    * @var string   Comment 
    8792    */ 
    88     protected $comment = ''; 
     93    var $comment = ''; 
    8994 
    9095    /** 
    9196    * @var int   Creation date as unix timestamp 
    9297    */ 
    93     protected $date = 0; 
     98    var $date = 0; 
    9499 
    95100    /** 
    96101    * @var array    Files in the torrent 
    97102    */ 
    98     protected $files = array(); 
     103    var $files = array(); 
    99104 
    100105    /** 
    101106    * @var int      Size of of the full torrent (after download) 
    102107    */ 
    103     protected $size = 0; 
     108    var $size = 0; 
    104109 
    105110    /** 
    106111    * @var string   Signature of the software which created the torrent 
    107112    */ 
    108     protected $created_by = ''; 
     113    var $created_by = ''; 
    109114 
    110115    /** 
    111116    * @var string    tracker (the tracker the torrent has been received from) 
    112117    */ 
    113     protected $announce = ''; 
     118    var $announce = ''; 
    114119 
    115120    /** 
    116121    * @var array     List of known trackers for the torrent 
    117122    */ 
    118     protected $announce_list = array(); 
     123    var $announce_list = array(); 
    119124 
    120125    /** 
    121126    * @var string   Source string 
    122     */ 
    123     protected $source = ''; 
     127    * @access private 
     128    */ 
     129    var $_source = ''; 
    124130 
    125131    /** 
    126132    * @var int      Source length 
    127     */ 
    128     protected $source_length = 0; 
     133    * @access private 
     134    */ 
     135    var $_source_length = 0; 
    129136 
    130137    /** 
    131138    * @var int      Current position of the string 
    132     */ 
    133     protected $position = 0; 
     139    * @access private 
     140    */ 
     141    var $_position = 0; 
    134142 
    135143    /** 
    136144    * @var string   Info hash 
    137145    */ 
    138     protected $info_hash; 
     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; 
    139152 
    140153    /** 
    141154    * @var array    Decoded data from File_Bittorrent_Decode::decodeFile() 
    142155    */ 
    143     protected $decoded = array(); 
     156    var $decoded = array(); 
    144157 
    145158    /** 
     
    148161    * @param string 
    149162    * @return mixed 
    150     * @throws File_Bittorrent_Exception if decoded data contains trailing garbage 
    151163    */ 
    152164    function decode($str) 
    153165    { 
    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); 
     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; 
    160173        } 
    161174        return $result; 
     
    167180    * @param string    Filename 
    168181    * @return mixed    Returns an arrayon success or false on error 
    169     * @throws File_Bittorrent_Exception if no file given or bencoded data is corrupt 
    170182    */ 
    171183    function decodeFile($file) 
     
    173185        // Check file 
    174186        if (!is_file($file)) { 
    175             throw new File_Bittorrent_Exception('Given filename \'' . $file . '\' is not a valid file.', File_Bittorrent_Exception::source); 
     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; 
    176189        } 
    177190 
     
    186199        $this->announce      = ''; 
    187200        $this->announce_list = array(); 
    188         $this->position     = 0; 
     201        $this->_position     = 0; 
    189202        $this->info_hash     = ''; 
    190203 
    191204        // Decode .torrent 
    192         $this->source = file_get_contents($file); 
    193         $this->source_length = strlen($this->source); 
    194         $this->decoded = $this->bdecode(); 
     205        $this->_source = file_get_contents($file); 
     206        $this->_source_length = strlen($this->_source); 
     207        $this->decoded = $this->_bdecode(); 
    195208        if (!is_array($this->decoded)) { 
    196             throw new File_Bittorrent_Exception('Corrupted bencoded data. Failed to decode data from file \'$file\'.', File_Bittorrent_Exception::decode); 
     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; 
    197211        } 
    198212 
     
    267281            'announce'      => $this->announce, 
    268282            'announce_list' => $this->announce_list, 
    269             'info_hash'     => $this->info_hash, 
    270283        ); 
    271284    } 
     
    274287    * Decode a BEncoded String 
    275288    * 
     289    * @access private 
    276290    * @return mixed    Returns the representation of the data in the BEncoded string or false on error 
    277291    */ 
    278     protected function bdecode() 
    279     { 
    280         switch ($this->getChar()) { 
     292    function _bdecode() 
     293    { 
     294        switch ($this->_getChar()) { 
    281295        case 'i': 
    282             $this->position++; 
    283             return $this->decode_int(); 
     296            $this->_position++; 
     297            return $this->_decode_int(); 
    284298            break; 
    285299        case 'l': 
    286             $this->position++; 
    287             return $this->decode_list(); 
     300            $this->_position++; 
     301            return $this->_decode_list(); 
    288302            break; 
    289303        case 'd': 
    290             $this->position++; 
    291             return $this->decode_dict(); 
     304            $this->_position++; 
     305            return $this->_decode_dict(); 
    292306            break; 
    293307        default: 
    294             return $this->decode_string(); 
     308            return $this->_decode_string(); 
    295309        } 
    296310    } 
     
    304318    * would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee 
    305319    * 
     320    * @access private 
    306321    * @return array 
    307     * @throws File_Bittorrent_Exception if bencoded dictionary contains invalid data 
    308     */ 
    309     protected function decode_dict() 
     322    */ 
     323    function _decode_dict() 
    310324    { 
    311325        $return = array(); 
    312326        $ended = false; 
    313327        $lastkey = NULL; 
    314         while ($char = $this->getChar()) { 
     328        while ($char = $this->_getChar()) { 
    315329            if ($char == 'e') { 
    316330                $ended = true; 
     
    318332            } 
    319333            if (!ctype_digit($char)) { 
    320                 throw new File_Bittorrent_Exception('Invalid dictionary key.', File_Bittorrent_Exception::decode); 
    321             } 
    322             $key = $this->decode_string(); 
     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(); 
    323339            if (isset($return[$key])) { 
    324                 throw new File_Bittorrent_Exception('Duplicate dictionary key.', File_Bittorrent_Exception::decode); 
     340                $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Duplicate dictionary key.'); 
     341                $return = false; 
     342                break; 
    325343            } 
    326344            if ($key < $lastkey) { 
    327                 throw new File_Bittorrent_Exception('Missorted dictionary key.', File_Bittorrent_Exception::decode); 
    328             } 
    329             $val = $this->bdecode(); 
     345                $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Missorted dictionary key.'); 
     346                $return = false; 
     347                break; 
     348            } 
     349            $val = $this->_bdecode(); 
    330350            if ($val === false) { 
    331                 throw new File_Bittorrent_Exception('Invalid value.', File_Bittorrent_Exception::decode); 
     351                $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Invalid value.'); 
     352                $return = false; 
     353                break; 
    332354            } 
    333355            $return[$key] = $val; 
     
    335357        } 
    336358        if (!$ended) { 
    337             throw new File_Bittorrent_Exception('Unterminated dictionary.', File_Bittorrent_Exception::decode); 
    338         } 
    339         $this->position++; 
     359            $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_dict() - Unterminated dictionary.'); 
     360            $return = false; 
     361        } 
     362        $this->_position++; 
    340363        return $return; 
    341364    } 
     
    348371    * would bEncode to 11:BitTorrents. 
    349372    * 
     373    * @access private 
    350374    * @return string|false 
    351     * @throws File_Bittorrent_Exception if bencoded data is invalid 
    352     */ 
    353     protected function decode_string() 
     375    */ 
     376    function _decode_string() 
    354377    { 
    355378        // Check for bad leading zero 
    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); 
     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; 
    359383        } 
    360384        // Find position of colon 
    361385        // Supress error message if colon is not found which may be caused by a corrupted or wrong encoded string 
    362         if (!$pos_colon = @strpos($this->source, ':', $this->position)) { 
    363             throw new File_Bittorrent_Exception('Colon not found.', File_Bittorrent_Exception::decode); 
     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; 
    364389        } 
    365390        // Get length of string 
    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); 
     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; 
    369395        } 
    370396        // Get string 
     
    372398            $return = ''; 
    373399        } else { 
    374             $return = substr($this->source, $pos_colon + 1, $str_length); 
     400            $return = substr($this->_source, $pos_colon + 1, $str_length); 
    375401        } 
    376402        // Move Pointer after string 
    377         $this->position = $pos_colon + $str_length + 1; 
     403        $this->_position = $pos_colon + $str_length + 1; 
    378404        return $return; 
    379405    } 
     
    386412    * i-3272002e. 
    387413    * 
     414    * @access private 
    388415    * @return int 
    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; 
     416    */ 
     417    function _decode_int() 
     418    { 
     419        $pos_e  = strpos($this->_source, 'e', $this->_position); 
     420        $p = $this->_position; 
    395421        if ($p === $pos_e) { 
    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); 
     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; 
    402430        } 
    403431        for ($i = $p; $i < $pos_e-1; $i++) { 
    404             if (!ctype_digit(substr($this->source, $i, 1))) { 
    405                 throw new File_Bittorrent_Exception('Non-digit characters in integer.', File_Bittorrent_Exception::decode); 
     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; 
    406435            } 
    407436        } 
     
    409438        // overflow. The "+ 0" accomplishes exactly that, using the internal casting 
    410439        // logic of PHP 
    411         $return = substr($this->source, $this->position, $pos_e - $this->position) + 0; 
    412         $this->position = $pos_e + 1; 
     440        $return = substr($this->_source, $this->_position, $pos_e - $this->_position) + 0; 
     441        $this->_position = $pos_e + 1; 
    413442        return $return; 
    414443    } 
     
    423452    * would bEncode to li1e7:Mondunai3el3:Sub4:Listee 
    424453    * 
     454    * @access private 
    425455    * @return array 
    426     * @throws File_Bittorrent_Exception if bencoded data is invalid 
    427     */ 
    428     protected function decode_list() 
     456    */ 
     457    function _decode_list() 
    429458    { 
    430459        $return = array(); 
    431         $char = $this->getChar(); 
     460        $char = $this->_getChar(); 
    432461        $p1 = $p2 = 0; 
    433462        if ($char === false) { 
    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; 
     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; 
    440470            // Empty does not work here 
    441471            if($p1 == $p2)  { 
    442                 throw new File_Bittorrent_Exception('Unterminated list.', File_Bittorrent_Exception::decode); 
     472                $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::_decode_list() - Unterminated list.'); 
     473                return false; 
    443474            } 
    444475            $return[] = $val; 
    445476        } 
    446         $this->position++; 
     477        $this->_position++; 
    447478        return $return; 
    448479    } 
     
    451482    * Get the char at the current position 
    452483    * 
     484    * @access private 
    453485    * @return string|false 
    454486    */ 
    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); 
     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); 
    460492    } 
    461493 
     
    464496    * 
    465497    * @return array|false 
    466     * @throws File_Bittorrent_Exception if allow_url_fopen is disabled or scrape data is invalid 
    467498    */ 
    468499    function getStats() 
     
    470501        // Check if we can access remote data 
    471502        if (!ini_get('allow_url_fopen')) { 
    472             throw new File_Bittorrent_Exception('\'allow_url_fopen\' must be enabled.', File_Bittorrent_Exception::source); 
     503            $this->last_error = PEAR::raiseError('File_Bittorrent_Decode::getStats() - "allow_url_fopen" must be enabled.'); 
    473504            return false; 
    474505        } 
     
    477508        $scrape_url = preg_replace('/\/announce$/', '/scrape', $this->announce) . '?info_hash=' . urlencode($packed_hash); 
    478509        $scrape_data = file_get_contents($scrape_url); 
    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         } 
     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        } 
    487515        return $stats['files'][$packed_hash]; 
    488516    } 
    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     } 
    579517} 
    580518 
  • trunk/File/Bittorrent/Encode.php

    r74 r76  
    4646*/ 
    4747require_once 'PEAR.php'; 
    48 require_once 'File/Bittorrent/Exception.php'; 
    4948 
    5049/** 
     
    7372    * @param mixed    Variable to encode 
    7473    * @return string 
    75     * @throws File_Bittorrent_Exception if unsupported type should be encoded 
    7674    */ 
    7775    function encode($mixed) 
     
    8886            return $this->encode_array($mixed); 
    8987        default: 
    90             throw new File_Bittorrent_Exception('Unsupported type. Variable must be one of \'string\', \'integer\', \'double\' or \'array\'', File_Bittorrent_Exception::encode); 
     88            PEAR::raiseError('File_Bittorrent_Encode::encode() - Unsupported type.', null, null, "Variable must be one of 'string', 'integer', 'double' or 'array'"); 
    9189        } 
    9290    } 
     
    141139    * @return string 
    142140    */ 
    143     function encode_array(array $array) 
     141    function encode_array($array) 
    144142    { 
    145143        // Check for strings in the keys 
  • trunk/File/Bittorrent/MakeTorrent.php

    r74 r76  
    4242require_once 'PEAR.php'; 
    4343require_once 'File/Bittorrent/Encode.php'; 
    44 require_once 'File/Bittorrent/Exception.php'; 
    4544 
    4645/** 
     
    5958    /** 
    6059     * @var string Path to the file or directory to create the torrent from. 
    61      */ 
    62     protected $path = ''; 
     60     * @access private 
     61     */ 
     62    var $_path = ''; 
    6363 
    6464    /** 
    6565     * @var bool Whether or not $path is a file 
    66      */ 
    67     protected $is_file = false; 
     66     * @access private 
     67     */ 
     68    var $_is_file = false; 
    6869 
    6970    /** 
    7071     * @var bool Where or not $path is a directory 
    7172     */