/** * Bootstrap any application services. * * @return void */ public function boot() { Validator::extend('torrentFile', function ($attribute, $value, $parameters, $validator) { $dictionary = BencodeHelper::decodeFile($value); list($announce, $info) = BencodeHelper::checkDictionary($dictionary, "announce(string):info", "global dictionary"); list($dname, $plen, $pieces) = BencodeHelper::checkDictionary($info, "name(string):piece length(integer):pieces(string)", "Info dictionary"); $total_length = BencodeHelper::getDictionaryValue($info, "length", "integer"); $dicttionary_is_valid = BencodeHelper::checkDictionary($dictionary, "announce(string):info", "global dictionary") == BencodeHelper::_OK; return $dicttionary_is_valid && (int) $total_length > 0 && (int) $plen > 0 && $pieces !== "" && $announce !== ""; }); }
public function uploadPost(TorrentUploadRequest $request) { if ($request->isMethod('POST')) { // Global config file $config = config('settings'); // Get files from request $torrent_file = $request->file('torrent_file'); $picture_file = $request->file('torrent_picture'); $nfo_file = $request->file('torrent_nfo'); // Torrent file name $filename = strip_tags($torrent_file->getClientOriginalName()); // Torrent name $name = strip_tags($request->get('torrent_name')); // Torrent description $description = strip_tags($request->get('torrent_description')); // Torrent category $category = $request->get('torrent_category'); // Set the torrent name to be as the file name, if the name is empty if ($name == "" || strlen($name) < 10) { // Remove the extension $name = str_replace(".torrent", "", $filename); // Replace dots with spaces $name = str_replace(".", " ", $name); } // Load dictionary from the torrent file $dictionary = BencodeHelper::decodeFile($torrent_file); // Retrieve the announce and the info values from the dictionary list($announce, $info) = BencodeHelper::checkDictionary($dictionary, "announce(string):info"); // Torrent name from the file, number of files and the files list list($dictionary_torrent_name, $pieces_length, $pieces) = BencodeHelper::checkDictionary($info, "name(string):piece length(integer):pieces(string)"); // Init files list array $file_list = array(); // Get torrent size from dictionary $total_length = BencodeHelper::getDictionaryValue($info, "length", "integer"); // Default torrent type (i.e. the torrent has one file) $type = "single"; if (isset($total_length)) { $file_list[] = array($dictionary_torrent_name, $total_length); } else { // Torrent has multiple files $f_list = BencodeHelper::getDictionaryValue($info, "files", "list"); $total_length = 0; foreach ($f_list as $fn) { list($ll, $ff) = BencodeHelper::checkDictionary($fn, "length(integer):path(list)"); $total_length += $ll; $ffa = array(); foreach ($ff as $ffe) { $ffa[] = $ffe["value"]; } $ffe = implode("/", $ffa); $file_list[] = array($ffe, $ll); } $type = "multi"; } // Join files list into a string $files_list = BencodeHelper::getFileListAsString($file_list); // Hash $info_hash = strtoupper(sha1($info["string"])); // Torrent file hash $hash = md5($torrent_file); // Save poster column $poster = ""; // Save picture if ($picture_file !== null) { $extension = $picture_file->getClientOriginalExtension(); $poster = sprintf("%s.%s", $hash, $extension); $picture_file->move($config['torrents_image_upload_dir'], $poster); } // Create the torrent in DB $torrent = Torrent::create(['name' => $name, 'description' => $description, 'filename' => $filename, 'category_id' => $category, 'info_hash' => $info_hash, 'hash' => $hash, 'size' => $total_length, 'picture' => $poster, 'files_list' => $files_list, 'user_id' => Auth::id()]); // Get global announce urls $announce_url = $config['global_announce_urls']; // Set them to the dictionary $dictionary["value"]["announce"] = array("type" => "list", "value" => $announce_url); // Add custom comment $dictionary["value"]["comment"] = array("type" => "string", "value" => sprintf("Torrent downloaded from %s", $config['site_name'])); // Set it as private $dictionary["value"]["info"]["value"]["private"] = array("type" => "integer", "value" => "1"); // Save the torrent file $path_to_new_file = sprintf("%s%s.torrent", $config['torrents_upload_dir'], $torrent->hash); $contents = BencodeHelper::bencode($dictionary); $new_file = File::put($path_to_new_file, $contents); // Save nfo $nfo_file->move($config['torrents_nfos_upload_dir'], sprintf("%s.nfo", $torrent->hash)); return Redirect::to('/torrents'); } }
/** * Returns the decoded dictionary of the torrent file * * @return array|void */ public function getDictionary() { return BencodeHelper::decodeFile($this->getFilePath()); }