public function uploaded()
{
$db = JFactory::getDBO();
$user = JFactory::getUser();
$params = JComponentHelper::getParams('com_tracker');
$app = JFactory::getApplication();
// Let's start to play with it
$temp_torrent['name'] = $_POST['jform']['name'];
$temp_torrent['categoryID'] = $_POST['jform']['categoryID'];
$temp_torrent['description'] = $_POST['jform']['description'];
if ($params->get('torrent_tags') == 1) {
$temp_torrent['tags'] = $_POST['jform']['tags'];
} else {
$temp_torrent['tags'] = '';
}
if ($params->get('enable_licenses') == 1) {
$licenseID = $_POST['jform']['licenseID'];
} else {
$licenseID = 0;
}
if ($params->get('forum_post_id') == 1) {
$forum_post = $_POST['jform']['forum_post'];
} else {
$forum_post = 0;
}
if ($params->get('torrent_information') == 1) {
$info_post = $_POST['jform']['info_post'];
} else {
$info_post = 0;
}
if ($params->get('allow_upload_anonymous') == 1) {
$uploader_anonymous = $_POST['jform']['uploader_anonymous'];
} else {
$uploader_anonymous = 0;
}
if ($params->get('freeleech') == 1) {
$download_multiplier = 0;
} else {
$download_multiplier = 1;
}
// ------------------------------------------------------------------------------------------------------------------------
// Let's take care of the .torrent file first
$temp_torrent['filename'] = $_FILES['jform']['name']['filename'];
$temp_torrent['temp_file'] = $_FILES['jform']['tmp_name']['filename'];
// Sanitize the filename
$temp_torrent['filename'] = TrackerHelper::sanitize_filename($temp_torrent['filename']);
// If something wrong happened during the file upload, we bail out
if (!is_uploaded_file($_FILES['jform']['tmp_name']['filename'])) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_OPS_SOMETHING_HAPPENED'), 'error');
}
// If we try to upload an empty file (0 bytes size)
if ($_FILES['jform']['size']['filename'] == 0) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_EMPTY_FILE'), 'error');
}
// Check if the torrent file is really a valid torrent file
if (!Torrent::is_torrent($_FILES['jform']['tmp_name']['filename'])) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_NOT_BENCODED_FILE'), 'error');
}
// Let's create our new torrent object
$torrent = new Torrent($_FILES['jform']['tmp_name']['filename']);
// And check for errors. Need to find a way to test them all :)
if ($errors = $torrent->errors()) {
var_dump($errors);
}
// Private Torrents
if ($params->get('make_private') == 1 && !$torrent->is_private()) {
$torrent->is_private(true);
}
// If the user didnt wrote a name for the torrent, we get it from the filename
if (empty($_POST['jform']['name'])) {
$filename = pathinfo($_FILES['jform']['name']['filename']);
$torrent->name($filename['filename']);
} else {
$torrent->name($_POST['jform']['name']);
}
$query = $db->getQuery(true);
$query->select('count(fid)');
$query->from('#__tracker_torrents');
$query->where('info_hash = UNHEX("' . $torrent->hash_info() . '")');
$db->setQuery($query);
if ($db->loadResult() > 0) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_ALREADY_EXISTS'), 'error');
}
// ------------------------------------------------------------------------------------------------------------------------
// The .torrent file is valid, let's continue to our image file (if we choose to use it)
if ($params->get('use_image_file')) {
// When image_type is don't use image
if ($_POST['jform']['image_type'] == 0) {
$image_file_query_value = "";
}
// When image file is an uploaded file
if ($_POST['jform']['image_type'] == 1) {
if (!is_uploaded_file($_FILES['jform']['tmp_name']['image_file'])) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_OPS_SOMETHING_HAPPENED_IMAGE'), 'error');
}
if (!filesize($_FILES['jform']['tmp_name']['image_file']) || $_FILES['jform']['size']['image_file'] == 0) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_EMPTY_FILE_IMAGE'), 'error');
}
if (!TrackerHelper::is_image($_FILES['jform']['tmp_name']['image_file'])) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_NOT_AN_IMAGE_FILE'), 'error');
}
$image_file_extension = end(explode(".", $_FILES['jform']['name']['image_file']));
$image_file_query_value = $torrent->hash_info() . '.' . $image_file_extension;
$image_file_file = $_FILES['jform']['tmp_name']['image_file'];
}
// When image file is an external link
if ($_POST['jform']['image_type'] == 2) {
// If the remote file is unavailable
if (@(!file_get_contents($_POST['jform']['image_link'], 0, NULL, 0, 1))) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_REMOTE_IMAGE_INVALID_FILE'), 'error');
}
// check if the remote file is not an image
if (!is_array(@getimagesize($_POST['jform']['image_link']))) {
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_REMOTE_IMAGE_NOT_IMAGE'), 'error');
}
$image_file_query_value = $_POST['jform']['image_link'];
}
} else {
$image_file_query_value = "";
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// All is good, let's insert the record in the database
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//TODO: INSERT THE ORDERING EQUAL TO THE TORRENT ID
//Insert the torrent into the table
$query->clear();
$query = $db->getQuery(true);
$query->insert('#__tracker_torrents');
$query->set('info_hash = UNHEX("' . $torrent->hash_info() . '")');
$query->set('ctime = unix_timestamp()');
$query->set('name = ' . $db->quote($torrent->name()));
$query->set('alias = ' . $db->quote($torrent->name()));
$query->set('filename = ' . $db->quote($_FILES['jform']['name']['filename']));
$query->set('description = ' . $db->quote($_POST['jform']['description']));
$query->set('categoryID = ' . $db->quote($_POST['jform']['categoryID']));
$query->set('size = ' . $db->quote($torrent->size()));
$query->set('created_time = ' . $db->quote(date("Y-m-d H:i:s")));
$query->set('uploader = ' . $db->quote($user->id));
$query->set('number_files = ' . $db->quote(count($torrent->content())));
$query->set('uploader_anonymous = ' . $db->quote($uploader_anonymous));
$query->set('forum_post = ' . $db->quote($forum_post));
$query->set('info_post = ' . $db->quote($info_post));
$query->set('licenseID = ' . $db->quote($licenseID));
$query->set('upload_multiplier = 1');
$query->set('download_multiplier = ' . $db->quote($download_multiplier));
$query->set('image_file = ' . $db->quote($image_file_query_value));
$query->set('tags = ' . $db->quote($temp_torrent['tags']));
$query->set('state = 1');
$db->setQuery($query);
if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
}
// Get the torrent ID that we've just inserted in the database
$torrent_id = $db->insertid();
/* Need to check this.
Wrong info for single file torrent
Wrong filenames for multi file torrent
*/
// Insert the list of files of the torrent in the database
foreach ($torrent->content() as $filename => $filesize) {
$query->clear();
$query = $db->getQuery(true);
$query->insert('#__tracker_files_in_torrents');
$query->set('torrentID = ' . $db->quote($torrent_id));
$query->set('filename = ' . $db->quote($filename));
$query->set('size = ' . $db->quote($filesize));
$db->setQuery($query);
if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
}
}
// If we're in freeleech we need to add the record of the new torrent to the freeleech table
if ($params->get('freeleech') == 1) {
$query->clear();
$query = $db->getQuery(true);
$query->insert('#__tracker_torrents_freeleech');
$query->set('fid = ' . $db->quote($torrent_id));
$query->set('download_multiplier = 1');
$db->setQuery($query);
if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
}
}
$upload_error = 0;
// Lets try to save the torrent before we continue
if (!move_uploaded_file($_FILES['jform']['tmp_name']['filename'], JPATH_SITE . DS . $params->get('torrent_dir') . $torrent_id . "_" . $_FILES['jform']['name']['filename'])) {
$upload_error = 1;
}
// And we should also move the image file if we're using it with the option of uploading an image file
if ($params->get('use_image_file') && $_POST['jform']['image_type'] == 1) {
if (!move_uploaded_file($_FILES['jform']['tmp_name']['image_file'], JPATH_SITE . DS . 'images/tracker/torrent_image/' . $image_file_query_value)) {
$upload_error = 1;
}
}
if ($upload_error == 0) {
JFactory::getApplication()->setUserState('com_tracker.uploaded.torrent.data', 0);
$app->redirect(JRoute::_('index.php?option=com_tracker&view=torrent&id=' . $torrent_id), JText::_('COM_TRACKER_UPLOAD_OK'), 'message');
} else {
$query->clear();
$query = $db->getQuery(true);
$query->delete('#__tracker_files_in_torrents');
$query->where('torrent=' . $db->quote($torrent_id));
$db->setQuery($query);
$db->query();
if ($error = $db->getErrorMsg()) {
$this->setError($error);
return false;
}
$query->clear();
$query = $db->getQuery(true);
$query->delete('#__tracker_torrents');
$query->where('fid=' . $db->quote($torrent_id));
$db->setQuery($query);
$db->query();
unlink(JPATH_SITE . DS . $params->get('torrent_dir') . $torrent_id . "_*");
$app->redirect(JRoute::_('index.php?option=com_tracker&view=upload'), JText::_('COM_TRACKER_UPLOAD_PROBLEM_MOVING_FILE'), 'error');
}
}