Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
AJAX Torrent Uploader
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods in Progress

2020-05-11 02:58:23
I'll be adding this to jQuery pretty quick, but put this here as I improve (add form fields, etc).

So when called by AJAX/POST, this  will upload a .torrent from browse, and automatically parse & after parsing passes, inserts the data to the database.
Same idea as torrents-upload.php but not at all. Similar in some ways in terms of the parsing routines. This is a very good PHP class, and should be used by my opinion.

Works really well for me, and has worked on every torrent file I've thrown at it so far.

Testers
Send a file POST request to /ajax/ajax_ajax_torrent_upload.php

<form id="form-torupload" action="/ajax/ajax_torrent_upload.php" method="post" enctype="multipart/form-data">
   <input type="file" accept=".torrent" name="torrent" />
<input type="hidden" name="userid" value="<?php echo $CURUSER["id"]; ?>" required />
</form>
<!-- Status -->
<div id="err"></div>
<!-- Feedback -->
<div id="preview-torrent"></div>

[color=rgb(226, 80, 65)]<form id="form-torupload"[/color] will call jQuery and make the request. Otherwise, will post through form.
[color=rgb(226, 80, 65)]<div id="preview-torrent"></div>[/color] will return the message response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
[color=rgb(226, 80, 65)]<div id="err"></div>[/color] will return the POST response (data : success, else .. ) via FadeIn(), FadeOut() using set intervals.
Instructions
Create as /ajax/ajax_torrent_upload.php

<?php
/*
AJAX Torrent Uploading - Using Parsing class - Torrent()->object()
Replaces old torrents-upload.php and related classes: BDecode(), BEncode()
Triggered by AJAX call. Accepts .torrent, parses data and builds array, inserts to DB.

https://www.phpclasses.org/package/4896-PHP-Create-and-parse-Torrent-files.html
*/
error_reporting(E_ALL);
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_FILES['torrent'])) {
   $uploadedFile = $_FILES['torrent']['tmp_name'];
   $dirPath      = "../uploads/";
   $ext          = pathinfo($_FILES['torrent']['name'], PATHINFO_EXTENSION);
   move_uploaded_file($uploadedFile, '../uploads/' . $_FILES["torrent"]['name']);
   $dbHost     = 'localhost';
   $dbUsername = 'username';
   $dbPassword = 'password';
   $dbName     = 'database';
   $db         = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
   $userid     = isset($_POST['userid']) ? intval($_POST['userid']) : 0;
   $name       = $_FILES["torrent"]['name'];
   require('../backend/functions.php');
   require('parse.class.php');
   $torrent       = new Torrent("../uploads/$name");
   $torr_hash     = $torrent->hash_info();
   $torr_announce = $torrent->announce('http://tracker2.itzmx.com:6961/announce'); // Alternative tracker
   $torr_size     = $torrent->size();
   $torr_saveas   = $torrent->name();
   $torr_private  = $torrent->is_private() ? 'yes' : 'no';
   // Prep for form fields - input type = text, name = "torrentname"
   if (!empty($_POST['torrentname'])) {
       $torr_name = $_POST['torrentname'];
   } else {
       $torr_name = str_replace('.torrent', '', $torrent->name());
   }
   $error = '';
   // Check upload
   if (!is_array($_FILES['torrent'])) {
       $error = 'The file contains no array';
   } elseif (empty($_FILES['torrent'])) {
       $error = 'You did not select a torrent file';
   }
   // Proceed if no error
   if ($error === '') {
       $insert  = $db->query("INSERT INTO torrents (filename, owner, name, descr, image1, image2, category, added, info_hash, size, numfiles, save_as, announce, external, nfo, torrentlang, anon, last_action)
       VALUES ('" . $torr_saveas . "', " . sqlesc($userid) . ", '" . $torr_name . "', 'Torrent Description.', ''image1', 'image2', 1, '" . get_date_time() . "', '" . $torr_hash . "', '" . $torr_size . "', 3, '" . $torr_name . "', '" . $torr_announce . "', '" . $torr_private . "', 'no', 1, 'no', '" . get_date_time() . "')");
       $edit_id = mysqli_insert_id($db);
       mysqli_close($db);
       header("Location: " . $site_config["SITEURL"] . "/torrents-edit.php?id=" . $edit_id);
       die;
   } else {
       die($error);
   }
} else {
   echo 'Invalid request.';
}
?>


(jQuery) add to header.php

$(document).ready(function (e) {
   $("#form-torupload").on('change', (function (e) {
       e.preventDefault();
       $.ajax({
           url: "/ajax/ajax_torrent_upload.php",
           type: "POST",
           data: new FormData(this),
           contentType: false,
           cache: false,
           processData: false,
           beforeSend: function () {
               $("#preview-torrent").html("Uploading torrent...").delay(4000).fadeOut().delay(4000).fadeIn();
               $("#preview-torrent").fadeIn();
               $("#err").fadeOut();
           },
           success: function (data) {    
               if (data.redirect == true) {        
                  // Redirect to torrent editor
                  window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id(); ?>';
               }
               if (data == 'invalid') {
                   // Invalid format
                   $("#err").html("Invalid torrent!").fadeIn();
               } else {
                   $("#preview-torrent").fadeOut();
                   $("#updated-torrent").html("Torrent has been uploaded. Refresh to see all changes.").delay(1000).fadeIn().delay(2000).fadeOut();
               }
           },
           error: function (e) {
               $("#err").html(e).fadeIn();
           }
       });
   }));
});


Upload parse.class.php to /ajax/

Now in full working state, I will begin to push to jQuery and rework code to PDO.

** Note: Due to lack of <form> fully designed yet, things like category and language are at a default value of 1

In action on my end

[ATTACH type="full" alt="Tooltip.png"]5516[/ATTACH]


Green button triggers hidden file input that brings up the browse, and upon "onchange" the torrent is uploaded and torrent appears in same moment on homepage no refresh. Lots of things you can do with it. This is using Tooltipsy plugin.
parse.class.php.zip
Download
Tooltip.png
Tooltip.png
Unknown User
2020-05-11 10:00:59
Looks fancy and way better organized than the default torrent upload, but this requires serious sanitization and security checks.

Here's some suggestions:
1. Use $_SERVER['REQUEST_METHOD'] === 'POST' to verify that request is indeed a post.
2. Use require instead of require_once.
3. if (is_array($_FILES['torrent'])) should be moved as a separate if/else security measure.

$error = '';
// If/else checkings
if (!is_array($_FILES['torrent']))
{
    $error = 'The file contains no array';
}
elseif (empty($_FILES['torrent']))
{
    $error = 'You did not select a torrent file';
}
//Proceed with uploading if no errors are met.
if ($error === '')
{
$insert = $db->query("INSERT INTO torrents (filename, owner, name, descr,  image1, image2, category, added, info_hash, size, numfiles, save_as, announce, external, nfo, torrentlang, anon, last_action)
       VALUES ('" . $torr_saveas . "', " . sqlesc($userid) . ", '" . $torr_name . "', 'Torrent POST description', 'image1', 'image2', 1, '" . get_date_time() . "', '" . $torr_hash . "', '" . $torr_size . "', 3, '" . $torr_name . "', '" . $torr_announce . "', '" . $torr_private . "', 'no', 1, 'no', '" . get_date_time() . "')");
die;
}
else
{
die($error);
}
2020-05-11 10:13:25
"shadows, post: 68551, member: 20311":

Looks fancy and way better organized than the default torrent upload, but this requires serious sanitization and security checks.

Here's some suggestions:
1. Use $_SERVER['REQUEST_METHOD'] === 'POST' to verify that request is indeed a post.
2. Use require instead of require_once.
3. if (is_array($_FILES['torrent'])) should be moved as a separate if/else security measure.

$error = '';
// If/else checkings
if (!is_array($_FILES['torrent']))
{
    $error = 'The file contains no array';
}
elseif (empty($_FILES['torrent']))
{
    $error = 'You did not select a torrent file';
}
//Proceed with uploading if no errors are met.
if ($error === '')
{
$insert = $db->query("INSERT INTO torrents (filename, owner, name, descr,  image1, image2, category, added, info_hash, size, numfiles, save_as, announce, external, nfo, torrentlang, anon, last_action)
       VALUES ('" . $torr_saveas . "', " . sqlesc($userid) . ", '" . $torr_name . "', 'Torrent POST description', 'image1', 'image2', 1, '" . get_date_time() . "', '" . $torr_hash . "', '" . $torr_size . "', 3, '" . $torr_name . "', '" . $torr_announce . "', '" . $torr_private . "', 'no', 1, 'no', '" . get_date_time() . "')");
die;
}
else
{
die($error);
}



Great, will make those changes. The script has now a header redirect in the PHP code, which grabs the inserted ID using mysqli_insert_id(), but the problem is its difficult getting the browser to follow redirect response codes from PHP over to JS to tell the browser what to do. So a redirect to torrents-edit.php?id=5 to continue filling the rest of the fields out isn't possible atm.
Unknown User
2020-05-11 20:44:49
After successful ajax request, include window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id() ?>';
Set redirect in json to be true.

 success: function(data) {
       if (data.redirect == true) {
           // data.redirect contains the string URL to redirect to
           window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id() ?>';
       }
2020-05-11 23:25:44
"shadows, post: 68555, member: 20311":

After successful ajax request, include window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id() ?>';
Set redirect in json to be true.

 success: function(data) {
       if (data.redirect == true) {
           // data.redirect contains the string URL to redirect to
           window.location.href = '/torrents-edit.php?id=<?= mysqli_insert_id() ?>';
       }


Wow thank you, that worked perfect! Both security and redirect changes.  :LOL:
2020-05-14 09:50:57
@testers - changes coming to script. Also, its pending usability updates.

Current issue: redirect to torrents-details.php breaks layout and cascaded the page over itself. I've tried every routine, and the closed I've been is a naked form from torrents-details.php & its still less than ideal. It is fixed to the centre of the page and scrolling is a nightmare, its been repeated visits to stack overflow.

Solution above works but no true redirect in url. window.open _parent outside of response condition is as close as this gets, and you get no ID.
Script does not parse file size correctly, bytes are wrong, reads metafile size instead.
Script does not retrieve primary/backup trackers from .torrent - big issue

Only so much i can do on my end, can someone port this into their project and post torrents? What is your success? Speed? Parsing?
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728