Gazelle/sections/collages/download.php

169 lines
6.0 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
2013-02-16 08:00:57 +00:00
/*
This page is something of a hack so those
easily scared off by funky solutions, don't
2011-03-28 14:21:28 +00:00
touch it! :P
2013-02-16 08:00:57 +00:00
There is a central problem to this page, it's
2011-03-28 14:21:28 +00:00
impossible to order before grouping in SQL, and
2013-02-16 08:00:57 +00:00
it's slow to run sub queries, so we had to get
2011-03-28 14:21:28 +00:00
creative for this one.
2013-02-16 08:00:57 +00:00
The solution I settled on abuses the way
$DB->to_array() works. What we've done, is
2011-03-28 14:21:28 +00:00
backwards ordering. The results returned by the
2013-02-16 08:00:57 +00:00
query have the best one for each GroupID last,
and while to_array traverses the results, it
2011-03-28 14:21:28 +00:00
overwrites the keys and leaves us with only the
2013-02-16 08:00:57 +00:00
desired result. This does mean however, that
the SQL has to be done in a somewhat backwards
2011-03-28 14:21:28 +00:00
fashion.
2013-02-16 08:00:57 +00:00
Thats all you get for a disclaimer, just
remember, this page isn't for the faint of
2011-03-28 14:21:28 +00:00
heart. -A9
SQL template:
2013-02-16 08:00:57 +00:00
SELECT
CASE
2013-07-13 08:00:46 +00:00
WHEN t.Format = 'MP3' AND t.Encoding = 'V0 (VBR)'
THEN 1
WHEN t.Format = 'MP3' AND t.Encoding = 'V2 (VBR)'
THEN 2
ELSE 100
2013-02-16 08:00:57 +00:00
END AS Rank,
2011-03-28 14:21:28 +00:00
t.GroupID,
t.Media,
t.Format,
t.Encoding,
2013-07-13 08:00:46 +00:00
IF(t.Year = 0, tg.Year, t.Year),
2011-03-28 14:21:28 +00:00
tg.Name,
a.Name,
t.Size
2013-02-16 08:00:57 +00:00
FROM torrents AS t
2013-07-13 08:00:46 +00:00
INNER JOIN collages_torrents AS c ON t.GroupID = c.GroupID AND c.CollageID = '8'
INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
LEFT JOIN artists_group AS a ON a.ArtistID = tg.ArtistID
LEFT JOIN torrents_files AS f ON t.ID = f.TorrentID
2011-03-28 14:21:28 +00:00
ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
*/
2013-02-21 08:00:30 +00:00
if (
2013-07-13 08:00:46 +00:00
!isset($_REQUEST['collageid'])
|| !isset($_REQUEST['preference'])
|| !is_number($_REQUEST['preference'])
|| !is_number($_REQUEST['collageid'])
|| $_REQUEST['preference'] > 2
|| count($_REQUEST['list']) === 0
2013-02-21 08:00:30 +00:00
) {
error(0);
}
if (!check_perms('zip_downloader')) {
error(403);
}
2011-03-28 14:21:28 +00:00
2013-02-21 08:00:30 +00:00
$Preferences = array('RemasterTitle DESC', 'Seeders ASC', 'Size ASC');
2011-03-28 14:21:28 +00:00
$CollageID = $_REQUEST['collageid'];
$Preference = $Preferences[$_REQUEST['preference']];
2013-07-13 08:00:46 +00:00
$DB->query("
SELECT Name
FROM collages
WHERE ID = '$CollageID'");
2013-02-21 08:00:30 +00:00
list($CollageName) = $DB->next_record(MYSQLI_NUM, false);
2011-03-28 14:21:28 +00:00
2013-07-13 08:00:46 +00:00
$SQL = 'SELECT CASE ';
2011-03-28 14:21:28 +00:00
foreach ($_REQUEST['list'] as $Priority => $Selection) {
2013-02-21 08:00:30 +00:00
if (!is_number($Priority)) {
2011-11-07 08:00:19 +00:00
continue;
}
2013-07-13 08:00:46 +00:00
$SQL .= 'WHEN ';
2011-03-28 14:21:28 +00:00
switch ($Selection) {
2013-07-13 08:00:46 +00:00
case '00': $SQL .= "t.Format = 'MP3' AND t.Encoding = 'V0 (VBR)'"; break;
case '01': $SQL .= "t.Format = 'MP3' AND t.Encoding = 'APX (VBR)'"; break;
case '02': $SQL .= "t.Format = 'MP3' AND t.Encoding = '256 (VBR)'"; break;
case '03': $SQL .= "t.Format = 'MP3' AND t.Encoding = 'V1 (VBR)'"; break;
case '10': $SQL .= "t.Format = 'MP3' AND t.Encoding = '224 (VBR)'"; break;
case '11': $SQL .= "t.Format = 'MP3' AND t.Encoding = 'V2 (VBR)'"; break;
case '12': $SQL .= "t.Format = 'MP3' AND t.Encoding = 'APS (VBR)'"; break;
case '13': $SQL .= "t.Format = 'MP3' AND t.Encoding = '192 (VBR)'"; break;
case '20': $SQL .= "t.Format = 'MP3' AND t.Encoding = '320'"; break;
case '21': $SQL .= "t.Format = 'MP3' AND t.Encoding = '256'"; break;
case '22': $SQL .= "t.Format = 'MP3' AND t.Encoding = '224'"; break;
case '23': $SQL .= "t.Format = 'MP3' AND t.Encoding = '192'"; break;
case '30': $SQL .= "t.Format = 'FLAC' AND t.Encoding = '24bit Lossless' AND t.Media = 'Vinyl'"; break;
case '31': $SQL .= "t.Format = 'FLAC' AND t.Encoding = '24bit Lossless' AND t.Media = 'DVD'"; break;
case '32': $SQL .= "t.Format = 'FLAC' AND t.Encoding = '24bit Lossless' AND t.Media = 'SACD'"; break;
case '33': $SQL .= "t.Format = 'FLAC' AND t.Encoding = '24bit Lossless' AND t.Media = 'WEB'"; break;
case '34': $SQL .= "t.Format = 'FLAC' AND t.Encoding = 'Lossless' AND HasLog = '1' AND LogScore = '100' AND HasCue = '1'"; break;
case '35': $SQL .= "t.Format = 'FLAC' AND t.Encoding = 'Lossless' AND HasLog = '1' AND LogScore = '100'"; break;
case '36': $SQL .= "t.Format = 'FLAC' AND t.Encoding = 'Lossless' AND HasLog = '1'"; break;
case '37': $SQL .= "t.Format = 'FLAC' AND t.Encoding = 'Lossless'"; break;
case '40': $SQL .= "t.Format = 'DTS'"; break;
case '42': $SQL .= "t.Format = 'AAC' AND t.Encoding = '320'"; break;
case '43': $SQL .= "t.Format = 'AAC' AND t.Encoding = '256'"; break;
case '44': $SQL .= "t.Format = 'AAC' AND t.Encoding = 'q5.5'"; break;
case '45': $SQL .= "t.Format = 'AAC' AND t.Encoding = 'q5'"; break;
case '46': $SQL .= "t.Format = 'AAC' AND t.Encoding = '192'"; break;
2011-03-28 14:21:28 +00:00
default: error(0);
}
$SQL .= " THEN $Priority ";
}
2013-07-13 08:00:46 +00:00
$SQL .= "
ELSE 100
END AS Rank,
t.GroupID,
t.ID AS TorrentID,
t.Media,
t.Format,
t.Encoding,
IF(t.RemasterYear = 0, tg.Year, t.RemasterYear) AS Year,
tg.Name,
t.Size
2013-02-16 08:00:57 +00:00
FROM torrents AS t
2013-07-13 08:00:46 +00:00
INNER JOIN collages_torrents AS c ON t.GroupID = c.GroupID AND c.CollageID = '$CollageID'
INNER JOIN torrents_group AS tg ON tg.ID = t.GroupID AND tg.CategoryID = '1'
2011-03-28 14:21:28 +00:00
ORDER BY t.GroupID ASC, Rank DESC, t.$Preference";
2013-02-21 08:00:30 +00:00
$DownloadsQ = $DB->query($SQL);
$Collector = new TorrentsDL($DownloadsQ, $CollageName);
while (list($Downloads, $GroupIDs) = $Collector->get_downloads('GroupID')) {
$Artists = Artists::get_artists($GroupIDs);
2013-07-13 08:00:46 +00:00
$TorrentFilesQ = $DB->query("
SELECT TorrentID, File
FROM torrents_files
WHERE TorrentID IN (".implode(',', array_keys($GroupIDs)).')', false);
2013-02-21 08:00:30 +00:00
if (is_int($TorrentFilesQ)) {
// Query failed. Let's not create a broken zip archive
foreach ($GroupIDs as $GroupID) {
$Download =& $Downloads[$GroupID];
$Download['Artist'] = Artists::display_artists($Artists[$GroupID], false, true, false);
$Collector->fail_file($Download);
}
2011-03-28 14:21:28 +00:00
continue;
}
2013-02-21 08:00:30 +00:00
while (list($TorrentID, $TorrentFile) = $DB->next_record(MYSQLI_NUM, false)) {
$GroupID = $GroupIDs[$TorrentID];
$Download =& $Downloads[$GroupID];
$Download['Artist'] = Artists::display_artists($Artists[$Download['GroupID']], false, true, false);
if ($Download['Rank'] == 100) {
$Collector->skip_file($Download);
continue;
}
2013-02-25 21:16:55 +00:00
$Collector->add_file($TorrentFile, $Download);
2013-02-21 08:00:30 +00:00
unset($Download);
2012-02-28 08:00:21 +00:00
}
2011-03-28 14:21:28 +00:00
}
2013-02-21 08:00:30 +00:00
$Collector->finalize();
$Settings = array(implode(':', $_REQUEST['list']), $_REQUEST['preference']);
if (!isset($LoggedUser['Collector']) || $LoggedUser['Collector'] != $Settings) {
Users::update_site_options($LoggedUser['ID'], array('Collector' => $Settings));
2011-03-28 14:21:28 +00:00
}
2012-08-25 08:00:15 +00:00
2013-08-22 08:00:54 +00:00
define('SKIP_NO_CACHE_HEADERS', 1);
2011-03-28 14:21:28 +00:00
?>