Gazelle/sections/collages/download.php

197 lines
7.9 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
WHEN t.Format='MP3' AND t.Encoding='V0 (VBR)' THEN 1
WHEN t.Format='MP3' AND t.Encoding='V2 (VBR)' THEN 2
ELSE 100
END AS Rank,
2011-03-28 14:21:28 +00:00
t.GroupID,
t.Media,
t.Format,
t.Encoding,
IF(t.Year=0,tg.Year,t.Year),
tg.Name,
a.Name,
t.Size
2013-02-16 08:00:57 +00:00
FROM torrents AS t
2011-03-28 14:21:28 +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
ORDER BY t.GroupID ASC, Rank DESC, t.Seeders ASC
*/
if(
2013-02-16 08:00:57 +00:00
!isset($_REQUEST['collageid']) ||
!isset($_REQUEST['preference']) ||
!is_number($_REQUEST['preference']) ||
!is_number($_REQUEST['collageid']) ||
2011-03-28 14:21:28 +00:00
$_REQUEST['preference'] > 2 ||
count($_REQUEST['list']) == 0
) { error(0); }
if(!check_perms('zip_downloader')){ error(403); }
$Preferences = array('RemasterTitle DESC','Seeders ASC','Size ASC');
$CollageID = $_REQUEST['collageid'];
$Preference = $Preferences[$_REQUEST['preference']];
$DB->query("SELECT Name FROM collages WHERE ID='$CollageID'");
list($CollageName) = $DB->next_record(MYSQLI_NUM,false);
$SQL = "SELECT CASE ";
foreach ($_REQUEST['list'] as $Priority => $Selection) {
2011-11-07 08:00:19 +00:00
if(!is_number($Priority)) {
continue;
}
2011-03-28 14:21:28 +00:00
$SQL .= "WHEN ";
switch ($Selection) {
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;
2011-12-02 08:00:15 +00:00
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;
2011-03-28 14:21:28 +00:00
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;
default: error(0);
}
$SQL .= " THEN $Priority ";
}
$SQL .= "ELSE 100 END AS Rank,
t.GroupID,
2011-11-06 08:00:31 +00:00
t.ID,
2011-03-28 14:21:28 +00:00
t.Media,
t.Format,
t.Encoding,
IF(t.RemasterYear=0,tg.Year,t.RemasterYear),
tg.Name,
2011-11-06 08:00:31 +00:00
t.Size
2013-02-16 08:00:57 +00:00
FROM torrents AS t
2011-03-28 14:21:28 +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'
ORDER BY t.GroupID ASC, Rank DESC, t.$Preference";
$DB->query($SQL);
$Downloads = $DB->to_array('1',MYSQLI_NUM,false);
2012-10-11 08:00:15 +00:00
$Artists = Artists::get_artists($DB->collect('GroupID'), false);
2011-03-28 14:21:28 +00:00
$Skips = array();
$TotalSize = 0;
2011-11-06 08:00:31 +00:00
if(count($Downloads)) {
foreach($Downloads as $Download) {
$TorrentIDs[] = $Download[2];
}
$DB->query("SELECT TorrentID, file FROM torrents_files WHERE TorrentID IN (".implode(',', $TorrentIDs).")");
$Torrents = $DB->to_array('TorrentID',MYSQLI_ASSOC,false);
}
2011-03-28 14:21:28 +00:00
require(SERVER_ROOT.'/classes/class_torrent.php');
require(SERVER_ROOT.'/classes/class_zip.php');
2012-10-11 08:00:15 +00:00
$Zip = new ZIP(Misc::file_string($CollageName));
2011-03-28 14:21:28 +00:00
foreach($Downloads as $Download) {
2011-11-06 08:00:31 +00:00
list($Rank, $GroupID, $TorrentID, $Media, $Format, $Encoding, $Year, $Album, $Size) = $Download;
2012-10-11 08:00:15 +00:00
$Artist = Artists::display_artists($Artists[$GroupID],false,true,false);
2011-03-28 14:21:28 +00:00
if ($Rank == 100) {
$Skips[] = $Artist.$Album.' '.$Year;
continue;
}
$TotalSize += $Size;
2011-11-06 08:00:31 +00:00
$Contents = unserialize(base64_decode($Torrents[$TorrentID]['file']));
2011-03-28 14:21:28 +00:00
$Tor = new TORRENT($Contents, true);
$Tor->set_announce_url(ANNOUNCE_URL.'/'.$LoggedUser['torrent_pass'].'/announce');
unset($Tor->Val['announce-list']);
2013-02-16 08:00:57 +00:00
2012-02-28 08:00:21 +00:00
// We need this section for long file names :/
$TorrentName='';
$TorrentInfo='';
2012-10-11 08:00:15 +00:00
$TorrentName = Misc::file_string($Artist.$Album);
if ($Year > 0) { $TorrentName.=' - '.Misc::file_string($Year); }
if ($Media != '') { $TorrentInfo .= Misc::file_string($Media); }
2012-02-28 08:00:21 +00:00
if ($Format != '') {
if ($TorrentInfo!='') { $TorrentInfo .= ' - '; }
2012-10-11 08:00:15 +00:00
$TorrentInfo .= Misc::file_string($Format);
2012-02-28 08:00:21 +00:00
}
if ($Encoding!='') {
if ($TorrentInfo != '') { $TorrentInfo.=' - '; }
2012-10-11 08:00:15 +00:00
$TorrentInfo .= Misc::file_string($Encoding);
2012-02-28 08:00:21 +00:00
}
if ($TorrentInfo != '') { $TorrentInfo = " ($TorrentInfo)"; }
if (strlen($TorrentName) + strlen($TorrentInfo) + 3 > 200) {
2012-10-11 08:00:15 +00:00
$TorrentName = Misc::file_string($Album).(($Year>0)?(' - '.Misc::file_string($Year)):'');
2012-02-28 08:00:21 +00:00
}
$FileName = $TorrentName.$TorrentInfo;
2012-10-11 08:00:15 +00:00
$FileName = Format::cut_string($FileName, 192, true, false);
2013-02-16 08:00:57 +00:00
2012-02-28 08:00:21 +00:00
$Zip->add_file($Tor->enc(), $FileName.'.torrent');
2011-03-28 14:21:28 +00:00
}
$Analyzed = count($Downloads);
$Skipped = count($Skips);
$Downloaded = $Analyzed - $Skipped;
$Time = number_format(((microtime(true)-$ScriptStartTime)*1000),5).' ms';
2012-10-11 08:00:15 +00:00
$Used = Format::get_size(memory_get_usage(true));
2011-03-28 14:21:28 +00:00
$Date = date('M d Y, H:i');
2012-10-11 08:00:15 +00:00
$Zip->add_file('Collector Download Summary - '.SITE_NAME."\r\n\r\nUser:\t\t$LoggedUser[Username]\r\nPasskey:\t$LoggedUser[torrent_pass]\r\n\r\nTime:\t\t$Time\r\nUsed:\t\t$Used\r\nDate:\t\t$Date\r\n\r\nTorrents Analyzed:\t\t$Analyzed\r\nTorrents Filtered:\t\t$Skipped\r\nTorrents Downloaded:\t$Downloaded\r\n\r\nTotal Size of Torrents (Ratio Hit): ".Format::get_size($TotalSize)."\r\n\r\nAlbums Unavailable within your criteria (consider making a request for your desired format):\r\n".implode("\r\n",$Skips), 'Summary.txt');
2011-03-28 14:21:28 +00:00
$Settings = array(implode(':',$_REQUEST['list']),$_REQUEST['preference']);
$Zip->close_stream();
$Settings = array(implode(':',$_REQUEST['list']),$_REQUEST['preference']);
if(!isset($LoggedUser['Collector']) || $LoggedUser['Collector'] != $Settings) {
$DB->query("SELECT SiteOptions FROM users_info WHERE UserID='$LoggedUser[ID]'");
list($Options) = $DB->next_record(MYSQLI_NUM,false);
$Options = unserialize($Options);
$Options['Collector'] = $Settings;
$DB->query("UPDATE users_info SET SiteOptions='".db_string(serialize($Options))."' WHERE UserID='$LoggedUser[ID]'");
$Cache->begin_transaction('user_info_heavy_'.$LoggedUser['ID']);
$Cache->insert('Collector',$Settings);
$Cache->commit_transaction(0);
}
2012-08-25 08:00:15 +00:00
define('IE_WORKAROUND_NO_CACHE_HEADERS', 1);
2011-03-28 14:21:28 +00:00
?>