Batch Upload Torrents
Return to Admin Page
");
mysql_select_db($database) or die(errorMessage() . "Can't open the database.");
while ($zip_entry = zip_read($zip))
{
echo "Name: " . zip_entry_name($zip_entry) . "
\n";
if (substr(zip_entry_name($zip_entry), -8) == ".torrent")
{
$error_status = true;
if (zip_entry_open($zip, $zip_entry, "r"))
{
//read in file from zip
$buffer = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
//go through each torrent file and add it if possible
require_once ("BDecode.php");
require_once ("BEncode.php");
$tracker_url = $website_url . substr($_SERVER['REQUEST_URI'], 0, -16) . "announce.php";
$array = BDecode($buffer);
if (!$array)
{
echo errorMessage() . "Error: The parser was unable to load this torrent.\n";
$error_status = false;
}
if (strtolower($array["announce"]) != $tracker_url)
{
echo errorMessage() . "Error: The tracker announce URL does not match this:
$tracker_url
Please re-create and re-upload the torrent.\n";
$error_status = false;
}
if (function_exists("sha1"))
$hash = @sha1(BEncode($array["info"]));
else
{
echo errorMessage() . "Error: It looks like you do not have a hash function available, this will not work.\n";
$error_status = false;
}
//figure out total size of all files in torrent, needed for insertion into database
$info = $array["info"];
$total_size = 0;
if (isset($info["files"]))
{
foreach ($info["files"] as $file)
{
$total_size = $total_size + $file["length"];
}
}
else
{
$total_size = $info["length"];
}
//Validate torrent file, make sure everything is correct
$filename = $array["info"]["name"];
$filename = mysql_escape_string($filename);
$filename = clean($filename);
if ((strlen($hash) != 40) || !verifyHash($hash))
{
echo errorMessage() . "Error: Info hash must be exactly 40 hex bytes.\n";
$error_status = false;
}
if ($error_status == true)
{
$query = "INSERT INTO " . $prefix . "namemap (info_hash, filename, url, size, pubDate) VALUES (\"$hash\", \"$filename\", \"$url\", \"$total_size\", \"" . date('D, j M Y h:i:s') . "\")";
$status = makeTorrent($hash, true);
quickQuery($query);
if ($status == true)
{
//create torrent file in folder, at this point we assume it's valid
if (!$handle = fopen("torrents/" . $filename . ".torrent", 'w'))
{
echo errorMessage() . "Error: Can't write to file.\n";
break;
}
//populate file with contents
if (fwrite($handle, $buffer) === FALSE)
{
echo errorMessage() . "Error: Can't write to file.\n";
break;
}
fclose($handle);
//make torrent file readable by all
chmod("torrents/" . $filename . ".torrent", 0644);
echo "Torrent was added successfully.
\n";
}
else
{
echo errorMessage() . "There were some errors. Check if this torrent has been added previously.\n";
}
}
zip_entry_close($zip_entry);
}
}
else
echo errorMessage() . "Unable to add torrent, it doesn't end in .torrent\n";
echo "
";
}
zip_close($zip);
}
//finished reading zip file
//run RSS generator because we have new torrents in database
require_once("rss_generator.php");
}
else
{
//display upload box
?>
This page lets you upload a zip file containing multiple torrents and add them into the database. The
zip file cannot have any folders in it. This requires that you are running PHP with compiled zip support.
If you are unsure, check with your system administrator or phpinfo(). Any torrents that already exist in
the database will be skipped. If you want to use HTTP seeding you'll need to add this feature to the torrent
files before you zip and upload the file. If you are uploading a very large zip file this may take some time...
\n";
}
?>
Return to Admin Page