Advertisement
Guest User

torrent

a guest
Sep 28th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.95 KB | None | 0 0
  1. <?php
  2. /*
  3. TorrentEditor.com API - Simple API to modify torrents
  4. Copyright (C) 2009  Tyler Alvord
  5.  
  6. Web: http://torrenteditor.com
  7. IRC: #torrenteditor.com on efnet.org  
  8.  
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. */
  22.  
  23. include_once('bencode.php');
  24.  
  25. class Torrent
  26. {
  27.     // Private class members
  28.     private $torrent;
  29.     private $info;
  30.    
  31.     // Public error message, $error is set if load() returns false
  32.     public $error;
  33.    
  34.     // Load torrent file data
  35.     // $data - raw torrent file contents
  36.     public function load( &$data )
  37.     {
  38.         $this->torrent = BEncode::decode( $data );
  39.  
  40.         if ( $this->torrent->get_type() == 'error' )
  41.         {
  42.             $this->error = $this->torrent->get_plain();
  43.             return false;
  44.         }
  45.         else if ( $this->torrent->get_type() != 'dictionary' )
  46.         {
  47.             $this->error = 'The file was not a valid torrent file.';
  48.             return false;
  49.         }
  50.        
  51.         $this->info = $this->torrent->get_value('info');
  52.         if ( !$this->info )
  53.         {
  54.             $this->error = 'Could not find info dictionary.';
  55.             return false;
  56.         }
  57.        
  58.         return true;
  59.     }
  60.    
  61.     // Get comment
  62.     // return - string
  63.     public function getComment() {
  64.         return $this->torrent->get_value('comment') ? $this->torrent->get_value('comment')->get_plain() : null;
  65.     }
  66.    
  67.     // Get creatuion date
  68.     // return - php date
  69.     public function getCreationDate() {
  70.         return  $this->torrent->get_value('creation date') ? $this->torrent->get_value('creation date')->get_plain() : null;
  71.     }
  72.    
  73.     // Get created by
  74.     // return - string
  75.     public function getCreatedBy() {
  76.         return $this->torrent->get_value('created by') ? $this->torrent->get_value('created by')->get_plain() : null;
  77.     }
  78.    
  79.     // Get name
  80.     // return - filename (single file torrent)
  81.     //          directory (multi-file torrent)
  82.     // see also - getFiles()
  83.     public function getName() {
  84.         return $this->info->get_value('name')->get_plain();
  85.     }
  86.    
  87.     // Get piece length
  88.     // return - int
  89.     public function getPieceLength() {
  90.         return $this->info->get_value('piece length')->get_plain();
  91.     }
  92.    
  93.     // Get pieces
  94.     // return - raw binary of peice hashes
  95.     public function getPieces() {
  96.         return $this->info->get_value('pieces')->get_plain();
  97.     }
  98.    
  99.     // Get private flag
  100.     // return - -1 public, implicit
  101.     //           0 public, explicit
  102.     //           1 private
  103.     public function getPrivate() {
  104.         if ( $this->info->get_value('private') )
  105.         {
  106.             return $this->info->get_value('private')->get_plain();
  107.         }
  108.         return -1;
  109.     }
  110.    
  111.     // Get a list of files
  112.     // return - array of Torrent_File
  113.     public function getFiles() {
  114.         // Load files
  115.         $filelist = array();
  116.         $length = $this->info->get_value('length');
  117.        
  118.         if ( $length )
  119.         {
  120.             $file = new Torrent_File();
  121.             $file->name = $this->info->get_value('name')->get_plain();
  122.             $file->length =  $this->info->get_value('length')->get_plain();
  123.             array_push( $filelist, $file );
  124.         }
  125.         else if ( $this->info->get_value('files') )
  126.         {
  127.             $files = $this->info->get_value('files')->get_plain();
  128.             while ( list( $key, $value ) = each( $files ) )
  129.             {
  130.                 $file = new Torrent_File();
  131.  
  132.                 $path = $value->get_value('path')->get_plain();
  133.                 while ( list( $key, $value2 ) = each( $path ) )
  134.                 {
  135.                     $file->name .= "/" . $value2->get_plain();
  136.                 }
  137.                 $file->name = ltrim( $file->name, '/' );
  138.                 $file->length =  $value->get_value('length')->get_plain();
  139.  
  140.                 array_push( $filelist, $file );
  141.             }
  142.         }
  143.        
  144.         return $filelist;
  145.     }
  146.    
  147.     // Get a list of trackers
  148.     // return - array of strings
  149.     public function getTrackers() {
  150.         // Load tracker list
  151.         $trackerlist = array();
  152.        
  153.         if ( $this->torrent->get_value('announce-list') )
  154.         {
  155.             $trackers = $this->torrent->get_value('announce-list')->get_plain();
  156.             while ( list( $key, $value ) = each( $trackers ) )
  157.             {
  158.                 if ( is_array( $value->get_plain() ) ) {
  159.                     while ( list( $key, $value2 ) = each( $value ) )
  160.                     {
  161.                         while ( list( $key, $value3 ) = each( $value2 ) )
  162.                         {
  163.                             array_push( $trackerlist, $value3->get_plain() );
  164.                         }
  165.                     }
  166.                 } else {
  167.                     array_push( $trackerlist, $value->get_plain() );
  168.                 }
  169.             }
  170.         }
  171.         else if ( $this->torrent->get_value('announce') )
  172.         {
  173.             array_push( $trackerlist, $this->torrent->get_value('announce')->get_plain() );
  174.         }
  175.        
  176.         return $trackerlist;
  177.     }
  178.      
  179.     // Helper function to make adding a tracker easier
  180.     // $tracker_url - string
  181.     public function addTracker( $tracker_url )
  182.     {
  183.         $trackers = $this->getTrackers();
  184.         $trackers[] = $tracker_url;
  185.         $this->setTrackers( $trackers );
  186.     }
  187.    
  188.     // Replace the current trackers with the supplied list
  189.     // $trackerlist - array of strings
  190.     public function setTrackers( $trackerlist )
  191.     {
  192.         if ( count( $trackerlist ) >= 1 )
  193.         {
  194.             $this->torrent->remove('announce-list');
  195.             $string = new BEncode_String( $trackerlist[0] );
  196.             $this->torrent->set( 'announce', $string );
  197.         }
  198.            
  199.         if ( count( $trackerlist ) > 1 )
  200.         {
  201.             $list = new BEncode_List();
  202.            
  203.  
  204.             while ( list( $key, $value ) = each( $trackerlist ) )
  205.             {
  206.                 $list2 = new BEncode_List();
  207.                 $string = new BEncode_String( $value );
  208.                 $list2->add( $string );
  209.                 $list->add( $list2 );
  210.             }
  211.            
  212.             $this->torrent->set( 'announce-list', $list );
  213.         }
  214.     }
  215.    
  216.     // Update the list of files
  217.     // $filelist - array of Torrent_File
  218.     public function setFiles( $filelist )
  219.     {
  220.         // Load files
  221.         $length = $this->info->get_value('length');
  222.  
  223.         if ( $length )
  224.         {
  225.             $filelist[0] = str_replace( '\\', '/', $filelist[0] );
  226.             $string = new BEncode_String( $filelist[0] );
  227.             $this->info->set( 'name', $string );
  228.         }
  229.         else if ( $this->info->get_value('files') )
  230.         {
  231.             $files = $this->info->get_value('files')->get_plain();
  232.             for ( $i = 0; $i < count( $files ); ++$i )
  233.             {
  234.                 $file_parts = split( '/', $filelist[$i] );
  235.                 $path = new BEncode_List();
  236.                 foreach ( $file_parts as $part )
  237.                 {
  238.                     $string = new BEncode_String( $part );
  239.                     $path->add( $string );
  240.                 }
  241.                 $files[$i]->set( 'path', $path );
  242.             }
  243.         }
  244.     }
  245.    
  246.     // Set the comment field
  247.     // $value - string
  248.     public function setComment( $value )
  249.     {
  250.         $type = 'comment';
  251.         $key = $this->torrent->get_value( $type );
  252.         if ( $value == '' ) {
  253.             $this->torrent->remove( $type );
  254.         } elseif ( $key ) {
  255.             $key->set( $value );
  256.         } else {
  257.             $string = new BEncode_String( $value );
  258.             $this->torrent->set( $type, $string );
  259.         }
  260.     }
  261.    
  262.     // Set the created by field
  263.     // $value - string
  264.     public function setCreatedBy( $value )
  265.     {
  266.         $type = 'created by';
  267.         $key = $this->torrent->get_value( $type );
  268.         if ( $value == '' ) {
  269.             $this->torrent->remove( $type );
  270.         } elseif ( $key ) {
  271.             $key->set( $value );
  272.         } else {
  273.             $string = new BEncode_String( $value );
  274.             $this->torrent->set( $type, $string );
  275.         }
  276.     }
  277.    
  278.    
  279.     // Set the creation date
  280.     // $value - php date
  281.     public function setCreationDate( $value )
  282.     {
  283.         $type = 'creation date';
  284.         $key = $this->torrent->get_value( $type );
  285.         if ( $value == '' ) {
  286.             $this->torrent->remove( $type );
  287.         } elseif ( $key ) {
  288.             $key->set( $value );
  289.         } else {
  290.             $int = new BEncode_Int( $value );
  291.             $this->torrent->set( $type, $int );
  292.         }
  293.     }
  294.    
  295.     // Change the private flag
  296.     // $value - -1 public, implicit
  297.     //           0 public, explicit
  298.     //           1 private
  299.     public function setPrivate( $value )
  300.     {
  301.         if ( $value == -1 ) {
  302.             $this->info->remove( 'private' );
  303.         } else {
  304.             $int = new BEncode_Int( $value );
  305.             $this->info->set( 'private', $int );
  306.         }
  307.     }
  308.    
  309.     // Bencode the torrent
  310.     public function bencode()
  311.     {
  312.         return $this->torrent->encode();
  313.     }
  314.    
  315.     // Return the torrent's hash
  316.     public function getHash()
  317.     {
  318.         return strtoupper( sha1( $this->info->encode() ) );
  319.     }
  320. }
  321.  
  322. // Simple class to encapsulate filename and length
  323. class Torrent_File
  324. {
  325.     public $name;
  326.     public $length;
  327. }
  328.  
  329. ?>
  330.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement