/**
  * Кодує дані в torrent файл
  * @param Torrent $torrent
  * @return string
  */
 public static function encode(Torrent $torrent)
 {
     $torrentArray = array();
     $data = $torrent->getData();
     foreach ($data as $itemKey => $item) {
         switch ($itemKey) {
             case 'announce-list':
                 foreach ($item as $announce) {
                     if ($announce) {
                         $torrentArray[$itemKey][][0] = $announce;
                     }
                 }
                 break;
             case 'url-list':
                 foreach ($item as $url) {
                     if ($url) {
                         $torrentArray[$itemKey][] = $url;
                     }
                 }
                 break;
             case 'info':
                 if (empty($item)) {
                     continue;
                 }
                 $torrentArray[$itemKey] = array();
                 foreach ($item as $infoKeyItem => $itemInfo) {
                     switch ($infoKeyItem) {
                         case 'files':
                             $torrentArray[$itemKey][$infoKeyItem] = array();
                             foreach ($itemInfo as $fileItem) {
                                 $newFileItem = array();
                                 foreach ($fileItem as $keyAttrFileItem => $attrFileItem) {
                                     switch ($keyAttrFileItem) {
                                         case 'path':
                                         case 'length':
                                         default:
                                             $newFileItem[$keyAttrFileItem] = $attrFileItem;
                                     }
                                 }
                                 $torrentArray[$itemKey][$infoKeyItem][] = $newFileItem;
                             }
                             break;
                         default:
                             $torrentArray[$itemKey][$infoKeyItem] = $itemInfo;
                     }
                 }
                 break;
             default:
                 if ($item) {
                     $torrentArray[$itemKey] = $item;
                 }
         }
     }
     $data = \Rych\Bencode\Bencode::encode($torrentArray);
     return $data;
 }
示例#2
0
 public function announce($tid, $uid)
 {
     // torrent klients (utorrent) atver šādu adresi
     // http://xxx/announce/2/2?info_hash=xxxx&peer_id=xxxx&numwant=50&event=started&port=123&left=0
     // parametru skaidrojums
     // https://wiki.theory.org/BitTorrentSpecification (Tracker Request Parameters)
     $torrent = Torrent::findOrFail($tid);
     $user = User::findOrFail($uid);
     Eloquent::unguard();
     // atlasa ierakstu vai izveido jaunu
     $data = TorrentUser::firstOrCreate(array('torrent_id' => $torrent->id, 'user_id' => $user->id, 'peer_id' => @$_GET['peer_id']));
     $data->ip = $_SERVER['REMOTE_ADDR'];
     // utorrent ip adrese
     $data->port = (int) @$_GET['port'];
     // utorrent ports
     $data->peer_id = @$_GET['peer_id'];
     // utorrent kaut kāds id
     $data->seeding = @$_GET['left'] == 0;
     // cik baiti atlikuši lejuplādēt (0 = pabeigts)
     if (@$_GET['event'] == 'stopped') {
         $data->delete();
         // nospieda stop pogu, dzēšam
     } else {
         $data->save();
     }
     $numwant = isset($_GET['numwant']) ? (int) $_GET['numwant'] : 50;
     // cik datus par citiem lietotājiem grib
     $complete = TorrentUser::where('torrent_id', $torrent->id)->where('seeding', true)->count();
     // saskaita šim torrentam seederus
     $incomplete = TorrentUser::where('torrent_id', $torrent->id)->where('seeding', false)->count();
     // saskaita šim torrentam leecherus
     $peers = TorrentUser::where('torrent_id', $torrent->id)->select('peer_id', 'ip', 'port')->take($numwant)->get()->toArray();
     // https://wiki.theory.org/BitTorrentSpecification (Tracker Response)
     $response = array('interval' => 60, 'tracker id' => 'tracker', 'complete' => $complete, 'incomplete' => $incomplete, 'peers' => $peers);
     $response = Response::make(Bencode::encode($response), 200);
     $response->header('Content-Type', 'text/plain');
     return $response;
 }
示例#3
0
 public function getDownload($id)
 {
     $torrent = Torrent::find($id);
     if (!$torrent) {
         App::abort(404);
     }
     $torrent->downloads += 1;
     $torrent->save();
     $decoded = Bencode::decode($torrent->file);
     $decoded['announce'] = Config::get('app.url') . '/announce/' . $torrent->id . '/' . Auth::user()->id;
     unset($decoded['announce-list']);
     $torrent->file = Bencode::encode($decoded);
     $response = Response::make($torrent->file, 200);
     $response->header('Content-Type', 'application/x-bittorrent');
     // http://x-bittorrent.mime-application.com/
     $response->header('Content-Disposition', 'attachment; filename=' . $torrent->name . '.torrent');
     // http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http
     return $response;
 }