/** * Заповняє торент об'єкт даними з моделі */ private function selfModelToTorrent() { $torrent = self::$torrent; if ($this->nameFileTorrent != $torrent->getNameFileTorrent()) { $torrent->setNameFileTorrent($this->nameFileTorrent); } if ($this->comment != $torrent->getComment()) { $torrent->setComment($this->comment); } if ($this->created_by != $torrent->getCreated_by()) { $torrent->setCreated_by($this->created_by); } if ($this->creation_date != $torrent->getCreation_date()) { $torrent->setCreation_date($this->creation_date); } if ($this->rss != $torrent->getRss()) { $torrent->setRss($this->rss); } if ($this->similar_torrents != $torrent->getSimilar_torrents()) { $torrent->setSimilar_torrents($this->similar_torrents); } if ($this->website != $torrent->getWebsite()) { $torrent->setWebsite($this->website); } if ($this->info->length != $torrent->getInfo()->getLength()) { $torrent->getInfo()->setLength($this->info->length); } if ($this->info->name != $torrent->getInfo()->getName()) { $torrent->getInfo()->setName($this->info->name); } if ($this->info->piece_length != $torrent->getInfo()->getPiece_length()) { $torrent->getInfo()->setPiece_length($this->info->piece_length); } if ($this->info->private != $torrent->getInfo()->getPrivate()) { $torrent->getInfo()->setPrivate($this->info->private); } $announces = $torrent->getAnnounce_list(); $length = max(count($this->announce_list), count($announces)); $newAnnounces = array(); for ($i = 0; $i < $length; $i++) { if (!isset($this->announce_list[$i])) { continue; } elseif (!isset($announces[$i])) { $newItemAnnounces = new Announce(); $newItemAnnounces->setAnnounce($this->announce_list[$i]->announce); $newAnnounces[] = $newItemAnnounces; continue; } if ($this->announce_list[$i]->announce != $announces[$i]->getAnnounce()) { $announces[$i]->setAnnounce($this->announce_list[$i]->announce); } $newAnnounces[] = $announces[$i]; } $torrent->clearAnnounce_list(); foreach ($newAnnounces as $announces) { $torrent->addAnnounce($announces); } $urls = $torrent->getUrl_list(); $length = max(count($this->url_list), count($urls)); $newUrls = array(); for ($i = 0; $i < $length; $i++) { if (!isset($this->url_list[$i])) { continue; } elseif (!isset($urls[$i])) { $newItemUrl = new Url(); $newItemUrl->setUrl($this->url_list[$i]->url); $newUrls[] = $newItemUrl; continue; } if ($this->url_list[$i]->url != $urls[$i]->getUrl()) { $urls[$i]->setUrl($this->url_list[$i]->url); } $newUrls[] = $urls[$i]; } $torrent->clearurl_list(); foreach ($newUrls as $urls) { $torrent->addUrl($urls); } $files = $torrent->getInfo()->getFiles(); $length = max(count($this->info->files), count($files)); $newFiles = array(); for ($i = 0; $i < $length; $i++) { if (!isset($this->info->files[$i])) { continue; } elseif (!isset($files[$i])) { $newFile = new File(); $newFile->setLength($this->info->files[$i]->length); $newFile->setPath($this->info->files[$i]->path); $newFiles[] = $newFile; continue; } if ($this->info->files[$i]->length != $files[$i]->getLength()) { $files[$i]->setLength($this->info->files[$i]->length); } if ($this->info->files[$i]->path != $files[$i]->getPath()) { $files[$i]->setPath($this->info->files[$i]->path); } $newFiles[] = $files[$i]; } $torrent->getInfo()->clearFiles(); foreach ($newFiles as $files) { $torrent->getInfo()->addFile($files); } $this->setTorrent($torrent); }
/** * Декодує дані з torrent файла * @param TorrentFile $torrentFile * @param $data */ public static function decode(TorrentFile $torrentFile, $data) { $torrent = $torrentFile->getTorrent(); $data = \Rych\Bencode\Bencode::decode($data); foreach ($data as $itemKey => $item) { switch ($itemKey) { case 'announce-list': foreach ($item as $announceItem) { $announce = new Announce(); $announce->setAnnounce($announceItem[0]); $torrent->addAnnounce($announce); } break; case 'comment': $torrent->setComment($item); break; case 'created by': $torrent->setCreated_by($item); break; case 'creation date': $torrent->setCreation_date($item); break; case 'encoding': $torrent->setEncoding($item); break; case 'rss': $torrent->setRss($item); break; case 'similar_torrents': $torrent->setSimilar_torrents($item); break; case 'url-list': foreach ($item as $urlItem) { $url = new Url(); $url->setUrl($urlItem); $torrent->addUrl($url); } break; case 'website': $torrent->setWebsite($item); break; case 'info': foreach ($item as $infoKeyItem => $itemInfo) { switch ($infoKeyItem) { case 'files': foreach ($itemInfo as $fileItem) { $file = new File(); $file->setLength($fileItem['length']); $file->setPath(implode('/', $fileItem['path'])); $torrent->getInfo()->addFile($file); } break; case 'name': $torrent->getInfo()->setName($itemInfo); break; case 'piece length': $torrent->getInfo()->setPiece_length($itemInfo); break; case 'pieces': $torrent->getInfo()->setPieces($itemInfo); break; case 'private': $torrent->getInfo()->setPrivate($itemInfo); break; case 'length': $torrent->getInfo()->setPrivate($itemInfo); break; default: $torrent->getInfo()->addOtherItem($infoKeyItem, $itemInfo); } } break; default: $torrent->addOtherItem($itemKey, $item); } } }