Gazelle/sections/collages/add_torrent.php

137 lines
4.1 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
2012-07-11 08:00:16 +00:00
2011-03-28 14:21:28 +00:00
authorize();
include(SERVER_ROOT.'/classes/class_validate.php');
$Val = new VALIDATE;
2012-02-07 08:00:20 +00:00
function AddTorrent($CollageID, $GroupID) {
global $Cache, $LoggedUser, $DB;
$DB->query("SELECT MAX(Sort) FROM collages_torrents WHERE CollageID='$CollageID'");
list($Sort) = $DB->next_record();
$Sort+=10;
$DB->query("SELECT GroupID FROM collages_torrents WHERE CollageID='$CollageID' AND GroupID='$GroupID'");
2012-11-03 08:00:19 +00:00
if ($DB->record_count() == 0) {
2012-02-07 08:00:20 +00:00
$DB->query("INSERT IGNORE INTO collages_torrents
(CollageID, GroupID, UserID, Sort, AddedOn)
VALUES
('$CollageID', '$GroupID', '$LoggedUser[ID]', '$Sort', NOW())");
$DB->query("UPDATE collages SET NumTorrents=NumTorrents+1 WHERE ID='$CollageID'");
$Cache->delete_value('collage_'.$CollageID);
$Cache->delete_value('torrents_details_'.$GroupID);
$Cache->delete_value('torrent_collages_'.$GroupID);
2012-02-20 08:00:22 +00:00
$Cache->delete_value('torrent_collages_personal_'.$GroupID);
2012-02-07 08:00:20 +00:00
$DB->query("SELECT UserID FROM users_collage_subs WHERE CollageID=$CollageID");
while (list($CacheUserID) = $DB->next_record()) {
$Cache->delete_value('collage_subs_user_new_'.$CacheUserID);
}
}
}
2011-03-28 14:21:28 +00:00
$CollageID = $_POST['collageid'];
2012-11-03 08:00:19 +00:00
if (!is_number($CollageID)) {
error(404);
}
2011-03-28 14:21:28 +00:00
$DB->query("SELECT UserID, CategoryID, Locked, NumTorrents, MaxGroups, MaxGroupsPerUser FROM collages WHERE ID='$CollageID'");
list($UserID, $CategoryID, $Locked, $NumTorrents, $MaxGroups, $MaxGroupsPerUser) = $DB->next_record();
2012-11-03 08:00:19 +00:00
if (!check_perms('site_collages_delete')) {
if ($Locked) {
$Err = "This collage is locked";
}
if ($CategoryID == 0 && $UserID != $LoggedUser['ID']) {
$Err = "You cannot edit someone else's personal collage";
}
if ($MaxGroups > 0 && $NumTorrents >= $MaxGroups) {
$Err = "This collage already holds its maximum allowed number of torrents";
}
}
if ($MaxGroupsPerUser > 0) {
$DB->query("SELECT COUNT(*) FROM collages_torrents WHERE CollageID='$CollageID' AND UserID='$LoggedUser[ID]'");
list($GroupsForUser) = $DB->next_record();
if (!check_perms('site_collages_delete') && $GroupsForUser >= $MaxGroupsPerUser) {
2011-03-28 14:21:28 +00:00
error(403);
}
}
2012-02-07 08:00:20 +00:00
if ($_REQUEST['action'] == 'add_torrent') {
$URLRegex = '/^https?:\/\/(www\.|ssl\.)?'.NONSSL_SITE_URL.'\/torrents\.php\?(page=[0-9]+&)?id=([0-9]+)/i';
$Val->SetFields('url', '1','regex','The URL must be a link to a torrent on the site.',array('regex'=>$URLRegex));
$Err = $Val->ValidateForm($_POST);
2011-03-28 14:21:28 +00:00
2012-11-03 08:00:19 +00:00
if ($Err) {
2012-02-07 08:00:20 +00:00
error($Err);
}
2011-03-28 14:21:28 +00:00
2012-02-07 08:00:20 +00:00
$URL = $_POST['url'];
2011-03-28 14:21:28 +00:00
2012-02-07 08:00:20 +00:00
// Get torrent ID
$URLRegex = '/torrents\.php\?(page=[0-9]+&)?id=([0-9]+)/i';
preg_match($URLRegex, $URL, $Matches);
2012-11-03 08:00:19 +00:00
$TorrentID = $Matches[2];
if (!$TorrentID || (int)$TorrentID == 0) {
error(404);
}
2011-03-28 14:21:28 +00:00
2012-02-07 08:00:20 +00:00
$DB->query("SELECT ID FROM torrents_group WHERE ID='$TorrentID'");
list($GroupID) = $DB->next_record();
2012-11-03 08:00:19 +00:00
if (!$GroupID) {
2012-02-07 08:00:20 +00:00
error('The torrent was not found in the database.');
}
2011-03-28 14:21:28 +00:00
2012-02-07 08:00:20 +00:00
AddTorrent($CollageID, $GroupID);
} else {
$URLRegex = '/^https?:\/\/(www\.|ssl\.)?'.NONSSL_SITE_URL.'\/torrents\.php\?(page=[0-9]+&)?id=([0-9]+)/i';
2012-02-07 08:00:20 +00:00
$URLs = explode("\n",$_REQUEST['urls']);
$GroupIDs = array();
$Err = '';
2012-11-03 08:00:19 +00:00
foreach ($URLs as $Key => &$URL) {
2012-02-07 08:00:20 +00:00
$URL = trim($URL);
2012-11-03 08:00:19 +00:00
if ($URL == '') {
unset($URLs[$Key]);
}
}
unset($URL);
if (!check_perms('site_collages_delete')) {
if ($MaxGroups > 0 && ($NumTorrents + count($URLs) > $MaxGroups)) {
$Err = "This collage can only hold $MaxGroups torrents.";
}
if ($MaxGroupsPerUser > 0 && ($GroupsForUser + count($URLs) > $MaxGroupsPerUser)) {
$Err = "You may only have $MaxGroupsPerUser torrents in this collage.";
}
}
foreach ($URLs as $URL) {
2012-02-07 08:00:20 +00:00
$Matches = array();
if (preg_match($URLRegex, $URL, $Matches)) {
$GroupIDs[] = $Matches[3];
$GroupID = $Matches[3];
} else {
$Err = "One of the entered URLs ($URL) does not correspond to a torrent on the site.";
break;
}
$DB->query("SELECT ID FROM torrents_group WHERE ID='$GroupID'");
2012-11-03 08:00:19 +00:00
if (!$DB->record_count()) {
2012-02-07 08:00:20 +00:00
$Err = "One of the entered URLs ($URL) does not correspond to a torrent on the site.";
break;
}
}
2012-02-07 08:00:20 +00:00
2012-11-03 08:00:19 +00:00
if ($Err) {
2012-02-07 08:00:20 +00:00
error($Err);
}
foreach ($GroupIDs as $GroupID) {
AddTorrent($CollageID, $GroupID);
}
2011-03-28 14:21:28 +00:00
}
2012-07-11 08:00:16 +00:00
header('Location: collages.php?id='.$CollageID);