fold sections/torrents/functions.php into classes/util.php

This commit is contained in:
Spine
2021-01-01 18:24:33 +01:00
parent ef0efb5c44
commit 01943dea9c
16 changed files with 254 additions and 277 deletions

1
boris
View File

@@ -27,7 +27,6 @@ require_once(__DIR__ . '/classes/config.php');
require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/classes/util.php');
require_once(__DIR__ . '/vendor/d11wtq/boris/lib/autoload.php');
require_once(__DIR__ . '/sections/torrents/functions.php');
$Cache = new CACHE;
$DB = new DB_MYSQL;

View File

@@ -1,7 +1,6 @@
<?php
require_once(__DIR__ . '/util.php');
require_once(__DIR__ . '/../sections/torrents/functions.php');
class Text {
/**

View File

@@ -1,7 +1,5 @@
<?php
require_once(__DIR__ . '/../sections/torrents/functions.php');
class Torrents {
const FILELIST_DELIM = 0xF7; // Hex for &divide; Must be the same as phrase_boundary in sphinx.conf!
const SNATCHED_UPDATE_INTERVAL = 3600; // How often we want to update users' snatch lists

View File

@@ -665,3 +665,257 @@ function check_paranoia($Property, $Paranoia, $UserClass, $UserID = false) {
return false;
}
}
function get_group_info($GroupID, $RevisionID = 0, $PersonalProperties = true, $ApiCall = false) {
global $Cache, $DB;
if (!$RevisionID) {
$TorrentCache = $Cache->get_value("torrents_details_$GroupID");
}
if ($RevisionID || !is_array($TorrentCache)) {
// Fetch the group details
$SQL = 'SELECT ';
if (!$RevisionID) {
$SQL .= '
g.WikiBody,
g.WikiImage, ';
} else {
$SQL .= '
w.Body,
w.Image, ';
}
$SQL .= "
g.ID,
g.Name,
g.Year,
g.RecordLabel,
g.CatalogueNumber,
g.ReleaseType,
g.CategoryID,
g.Time,
g.VanityHouse,
GROUP_CONCAT(DISTINCT tags.Name SEPARATOR '|') as tagNames,
GROUP_CONCAT(DISTINCT tags.ID SEPARATOR '|'),
GROUP_CONCAT(tt.UserID SEPARATOR '|'),
GROUP_CONCAT(tt.PositiveVotes SEPARATOR '|'),
GROUP_CONCAT(tt.NegativeVotes SEPARATOR '|')
FROM torrents_group AS g
LEFT JOIN torrents_tags AS tt ON (tt.GroupID = g.ID)
LEFT JOIN tags ON (tags.ID = tt.TagID)";
$args = [];
if ($RevisionID) {
$SQL .= '
LEFT JOIN wiki_torrents AS w ON (w.PageID = ? AND w.RevisionID = ?)';
$args[] = $GroupID;
$args[] = $RevisionID;
}
$SQL .= '
WHERE g.ID = ?
GROUP BY g.ID';
$args[] = $GroupID;
$DB->prepared_query($SQL, ...$args);
$TorrentDetails = $DB->next_record(MYSQLI_ASSOC);
// Fetch the individual torrents
$columns = "
t.ID,
t.Media,
t.Format,
t.Encoding,
t.Remastered,
t.RemasterYear,
t.RemasterTitle,
t.RemasterRecordLabel,
t.RemasterCatalogueNumber,
t.Scene,
t.HasLog,
t.HasCue,
t.HasLogDB,
t.LogScore,
t.LogChecksum,
t.FileCount,
t.Size,
tls.Seeders,
tls.Leechers,
tls.Snatched,
t.FreeTorrent,
t.Time,
t.Description,
t.FileList,
t.FilePath,
t.UserID,
tls.last_action,
HEX(t.info_hash) AS InfoHash,
tbt.TorrentID AS BadTags,
tbf.TorrentID AS BadFolders,
tfi.TorrentID AS BadFiles,
ml.TorrentID AS MissingLineage,
ca.TorrentID AS CassetteApproved,
lma.TorrentID AS LossymasterApproved,
lwa.TorrentID AS LossywebApproved,
t.LastReseedRequest,
t.ID AS HasFile,
COUNT(tl.LogID) AS LogCount
";
$DB->prepared_query("
SELECT $columns
,0 as is_deleted
FROM torrents AS t
INNER JOIN torrents_leech_stats tls ON (tls.TorrentID = t.ID)
LEFT JOIN torrents_bad_tags AS tbt ON (tbt.TorrentID = t.ID)
LEFT JOIN torrents_bad_folders AS tbf ON (tbf.TorrentID = t.ID)
LEFT JOIN torrents_bad_files AS tfi ON (tfi.TorrentID = t.ID)
LEFT JOIN torrents_missing_lineage AS ml ON (ml.TorrentID = t.ID)
LEFT JOIN torrents_cassette_approved AS ca ON (ca.TorrentID = t.ID)
LEFT JOIN torrents_lossymaster_approved AS lma ON (lma.TorrentID = t.ID)
LEFT JOIN torrents_lossyweb_approved AS lwa ON (lwa.TorrentID = t.ID)
LEFT JOIN torrents_logs AS tl ON (tl.TorrentID = t.ID)
WHERE t.GroupID = ?
GROUP BY t.ID
UNION DISTINCT
SELECT $columns
,1 as is_deleted
FROM deleted_torrents AS t
INNER JOIN deleted_torrents_leech_stats tls ON (tls.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_tags AS tbt ON (tbt.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_folders AS tbf ON (tbf.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_files AS tfi ON (tfi.TorrentID = t.ID)
LEFT JOIN deleted_torrents_missing_lineage AS ml ON (ml.TorrentID = t.ID)
LEFT JOIN deleted_torrents_cassette_approved AS ca ON (ca.TorrentID = t.ID)
LEFT JOIN deleted_torrents_lossymaster_approved AS lma ON (lma.TorrentID = t.ID)
LEFT JOIN deleted_torrents_lossyweb_approved AS lwa ON (lwa.TorrentID = t.ID)
LEFT JOIN torrents_logs AS tl ON (tl.TorrentID = t.ID)
WHERE t.GroupID = ?
GROUP BY t.ID
ORDER BY Remastered ASC,
(RemasterYear != 0) DESC,
RemasterYear ASC,
RemasterTitle ASC,
RemasterRecordLabel ASC,
RemasterCatalogueNumber ASC,
Media ASC,
Format,
Encoding,
ID", $GroupID, $GroupID);
$TorrentList = $DB->to_array('ID', MYSQLI_ASSOC);
if (empty($TorrentDetails) || empty($TorrentList)) {
if ($ApiCall === false) {
header('Location: log.php?search='.(empty($_GET['torrentid']) ? "Group+$GroupID" : "Torrent+{$_GET['torrentid']}"));
die();
}
else {
return null;
}
}
if (in_array(0, $DB->collect('Seeders'))) {
$CacheTime = 600;
} else {
$CacheTime = 3600;
}
// Store it all in cache
if (!$RevisionID) {
$Cache->cache_value("torrents_details_$GroupID", [$TorrentDetails, $TorrentList], $CacheTime);
}
} else { // If we're reading from cache
$TorrentDetails = $TorrentCache[0];
$TorrentList = $TorrentCache[1];
}
if ($PersonalProperties) {
// Fetch all user specific torrent and group properties
$TorrentDetails['Flags'] = ['IsSnatched' => false];
foreach ($TorrentList as &$Torrent) {
Torrents::torrent_properties($Torrent, $TorrentDetails['Flags']);
}
}
return [$TorrentDetails, $TorrentList];
}
function get_torrent_info($TorrentID, $RevisionID = 0, $PersonalProperties = true, $ApiCall = false) {
$torMan = new \Gazelle\Manager\Torrent;
$GroupInfo = get_group_info($torMan->idToGroupId($TorrentID), $RevisionID, $PersonalProperties, $ApiCall);
if (!$GroupInfo) {
return null;
}
foreach ($GroupInfo[1] as &$Torrent) {
//Remove unneeded entries
if ($Torrent['ID'] != $TorrentID) {
unset($GroupInfo[1][$Torrent['ID']]);
}
return $GroupInfo;
}
}
function get_group_requests($GroupID) {
if (empty($GroupID) || !is_number($GroupID)) {
return [];
}
global $DB, $Cache;
$Requests = $Cache->get_value("requests_group_$GroupID");
if ($Requests === false) {
$DB->prepared_query("
SELECT ID
FROM requests
WHERE TimeFilled IS NULL
AND GroupID = ?
", $GroupID
);
$Requests = $DB->collect('ID');
$Cache->cache_value("requests_group_$GroupID", $Requests, 0);
}
return Requests::get_requests($Requests);
}
// Count the number of audio files in a torrent file list per audio type
function audio_file_map($fileList) {
$map = [];
foreach (explode("\n", strtolower($fileList)) as $file) {
$info = Torrents::filelist_get_file($file);
if (!isset($info['ext'])) {
continue;
}
$ext = substr($info['ext'], 1); // skip over period
if (in_array($ext, ['ac3', 'flac', 'm4a', 'mp3'])) {
if (!isset($map[$ext])) {
$map[$ext] = 0;
}
++$map[$ext];
}
}
return $map;
}
function set_source(
\OrpheusNET\BencodeTorrent\BencodeTorrent $torrent,
string $siteSource,
string $grandfatherSource,
int $grandfatherSourceDate,
int $grandfatherNoSourceDate
) {
$torrentSource = $torrent->getSource();
$creationDate = $torrent->getCreationDate();
if ($torrentSource === $siteSource) {
return false;
}
if (!is_null($creationDate)) {
if (is_null($torrentSource) && $creationDate <= $grandfatherNoSourceDate) {
return false;
}
elseif (!is_null($torrentSource) && $torrentSource === $grandfatherSource && $creationDate <= $grandfatherSourceDate) {
return false;
}
}
return $torrent->setSource($siteSource);
}

View File

@@ -5,7 +5,6 @@ parameters:
scanFiles:
- classes/util.php
- sections/artist/index.php
- sections/torrents/functions.php
paths:
- app
- classes

View File

@@ -1,7 +1,5 @@
<?php
require_once(__DIR__ . '/../sections/torrents/functions.php');
if (!empty($_GET['order_way']) && $_GET['order_way'] == 'asc') {
$OrderWay = 'asc';
} else {

View File

@@ -1,7 +1,5 @@
<?php
require_once(__DIR__ . '/../torrents/functions.php');
$GroupAllowed = ['WikiBody', 'WikiImage', 'ID', 'Name', 'Year', 'RecordLabel', 'CatalogueNumber', 'ReleaseType', 'CategoryID', 'Time', 'VanityHouse'];
$TorrentAllowed = ['ID', 'Media', 'Format', 'Encoding', 'Remastered', 'RemasterYear', 'RemasterTitle', 'RemasterRecordLabel', 'RemasterCatalogueNumber', 'Scene', 'HasLog', 'HasCue', 'LogScore', 'FileCount', 'Size', 'Seeders', 'Leechers', 'Snatched', 'FreeTorrent', 'Time', 'Description', 'FileList', 'FilePath', 'UserID', 'Username'];

View File

@@ -3,8 +3,6 @@ authorize();
$bookmark = new \Gazelle\Bookmark;
try {
// calls get_group_info from torrents/functions.php
require_once(__DIR__ . '/../torrents/functions.php'); // TODO: refactor this shit
$bookmark->create($LoggedUser['ID'], $_GET['type'], (int)$_GET['id']);
}
catch (Exception $e) {

View File

@@ -13,8 +13,6 @@ if (!check_perms('admin_reports')) {
error(403);
}
require_once(__DIR__ . '/../torrents/functions.php');
$reportMan = new Gazelle\Manager\ReportV2;
$Types = $reportMan->types();

View File

@@ -12,8 +12,6 @@ if (!check_perms('site_top10')) {
die();
}
require_once(__DIR__ . '/../torrents/functions.php'); //Has get_reports($TorrentID);
if (empty($_GET['type']) || $_GET['type'] == 'torrents') {
require_once('torrents.php');
} else {

View File

@@ -14,7 +14,6 @@ if (!empty($_GET['revisionid']) && is_number($_GET['revisionid'])) {
$RevisionID = 0;
}
require('functions.php');
$TorrentCache = get_group_info($GroupID, $RevisionID);
$TorrentDetails = $TorrentCache[0];
$TorrentList = $TorrentCache[1];

View File

@@ -5,7 +5,6 @@ if (empty($_GET['groupid']) || !is_numeric($_GET['groupid'])) {
}
$GroupID = intval($_GET['groupid']);
require_once('functions.php');
$TorrentCache = get_group_info($GroupID);
$TorrentDetails = $TorrentCache[0];
$TorrentList = $TorrentCache[1];

View File

@@ -1,255 +0,0 @@
<?php
function get_group_info($GroupID, $RevisionID = 0, $PersonalProperties = true, $ApiCall = false) {
global $Cache, $DB;
if (!$RevisionID) {
$TorrentCache = $Cache->get_value("torrents_details_$GroupID");
}
if ($RevisionID || !is_array($TorrentCache)) {
// Fetch the group details
$SQL = 'SELECT ';
if (!$RevisionID) {
$SQL .= '
g.WikiBody,
g.WikiImage, ';
} else {
$SQL .= '
w.Body,
w.Image, ';
}
$SQL .= "
g.ID,
g.Name,
g.Year,
g.RecordLabel,
g.CatalogueNumber,
g.ReleaseType,
g.CategoryID,
g.Time,
g.VanityHouse,
GROUP_CONCAT(DISTINCT tags.Name SEPARATOR '|') as tagNames,
GROUP_CONCAT(DISTINCT tags.ID SEPARATOR '|'),
GROUP_CONCAT(tt.UserID SEPARATOR '|'),
GROUP_CONCAT(tt.PositiveVotes SEPARATOR '|'),
GROUP_CONCAT(tt.NegativeVotes SEPARATOR '|')
FROM torrents_group AS g
LEFT JOIN torrents_tags AS tt ON (tt.GroupID = g.ID)
LEFT JOIN tags ON (tags.ID = tt.TagID)";
$args = [];
if ($RevisionID) {
$SQL .= '
LEFT JOIN wiki_torrents AS w ON (w.PageID = ? AND w.RevisionID = ?)';
$args[] = $GroupID;
$args[] = $RevisionID;
}
$SQL .= '
WHERE g.ID = ?
GROUP BY g.ID';
$args[] = $GroupID;
$DB->prepared_query($SQL, ...$args);
$TorrentDetails = $DB->next_record(MYSQLI_ASSOC);
// Fetch the individual torrents
$columns = "
t.ID,
t.Media,
t.Format,
t.Encoding,
t.Remastered,
t.RemasterYear,
t.RemasterTitle,
t.RemasterRecordLabel,
t.RemasterCatalogueNumber,
t.Scene,
t.HasLog,
t.HasCue,
t.HasLogDB,
t.LogScore,
t.LogChecksum,
t.FileCount,
t.Size,
tls.Seeders,
tls.Leechers,
tls.Snatched,
t.FreeTorrent,
t.Time,
t.Description,
t.FileList,
t.FilePath,
t.UserID,
tls.last_action,
HEX(t.info_hash) AS InfoHash,
tbt.TorrentID AS BadTags,
tbf.TorrentID AS BadFolders,
tfi.TorrentID AS BadFiles,
ml.TorrentID AS MissingLineage,
ca.TorrentID AS CassetteApproved,
lma.TorrentID AS LossymasterApproved,
lwa.TorrentID AS LossywebApproved,
t.LastReseedRequest,
t.ID AS HasFile,
COUNT(tl.LogID) AS LogCount
";
$DB->prepared_query("
SELECT $columns
,0 as is_deleted
FROM torrents AS t
INNER JOIN torrents_leech_stats tls ON (tls.TorrentID = t.ID)
LEFT JOIN torrents_bad_tags AS tbt ON (tbt.TorrentID = t.ID)
LEFT JOIN torrents_bad_folders AS tbf ON (tbf.TorrentID = t.ID)
LEFT JOIN torrents_bad_files AS tfi ON (tfi.TorrentID = t.ID)
LEFT JOIN torrents_missing_lineage AS ml ON (ml.TorrentID = t.ID)
LEFT JOIN torrents_cassette_approved AS ca ON (ca.TorrentID = t.ID)
LEFT JOIN torrents_lossymaster_approved AS lma ON (lma.TorrentID = t.ID)
LEFT JOIN torrents_lossyweb_approved AS lwa ON (lwa.TorrentID = t.ID)
LEFT JOIN torrents_logs AS tl ON (tl.TorrentID = t.ID)
WHERE t.GroupID = ?
GROUP BY t.ID
UNION DISTINCT
SELECT $columns
,1 as is_deleted
FROM deleted_torrents AS t
INNER JOIN deleted_torrents_leech_stats tls ON (tls.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_tags AS tbt ON (tbt.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_folders AS tbf ON (tbf.TorrentID = t.ID)
LEFT JOIN deleted_torrents_bad_files AS tfi ON (tfi.TorrentID = t.ID)
LEFT JOIN deleted_torrents_missing_lineage AS ml ON (ml.TorrentID = t.ID)
LEFT JOIN deleted_torrents_cassette_approved AS ca ON (ca.TorrentID = t.ID)
LEFT JOIN deleted_torrents_lossymaster_approved AS lma ON (lma.TorrentID = t.ID)
LEFT JOIN deleted_torrents_lossyweb_approved AS lwa ON (lwa.TorrentID = t.ID)
LEFT JOIN torrents_logs AS tl ON (tl.TorrentID = t.ID)
WHERE t.GroupID = ?
GROUP BY t.ID
ORDER BY Remastered ASC,
(RemasterYear != 0) DESC,
RemasterYear ASC,
RemasterTitle ASC,
RemasterRecordLabel ASC,
RemasterCatalogueNumber ASC,
Media ASC,
Format,
Encoding,
ID", $GroupID, $GroupID);
$TorrentList = $DB->to_array('ID', MYSQLI_ASSOC);
if (empty($TorrentDetails) || empty($TorrentList)) {
if ($ApiCall === false) {
header('Location: log.php?search='.(empty($_GET['torrentid']) ? "Group+$GroupID" : "Torrent+{$_GET['torrentid']}"));
die();
}
else {
return null;
}
}
if (in_array(0, $DB->collect('Seeders'))) {
$CacheTime = 600;
} else {
$CacheTime = 3600;
}
// Store it all in cache
if (!$RevisionID) {
$Cache->cache_value("torrents_details_$GroupID", [$TorrentDetails, $TorrentList], $CacheTime);
}
} else { // If we're reading from cache
$TorrentDetails = $TorrentCache[0];
$TorrentList = $TorrentCache[1];
}
if ($PersonalProperties) {
// Fetch all user specific torrent and group properties
$TorrentDetails['Flags'] = ['IsSnatched' => false];
foreach ($TorrentList as &$Torrent) {
Torrents::torrent_properties($Torrent, $TorrentDetails['Flags']);
}
}
return [$TorrentDetails, $TorrentList];
}
function get_torrent_info($TorrentID, $RevisionID = 0, $PersonalProperties = true, $ApiCall = false) {
$torMan = new \Gazelle\Manager\Torrent;
$GroupInfo = get_group_info($torMan->idToGroupId($TorrentID), $RevisionID, $PersonalProperties, $ApiCall);
if (!$GroupInfo) {
return null;
}
foreach ($GroupInfo[1] as &$Torrent) {
//Remove unneeded entries
if ($Torrent['ID'] != $TorrentID) {
unset($GroupInfo[1][$Torrent['ID']]);
}
return $GroupInfo;
}
}
function get_group_requests($GroupID) {
if (empty($GroupID) || !is_number($GroupID)) {
return [];
}
global $DB, $Cache;
$Requests = $Cache->get_value("requests_group_$GroupID");
if ($Requests === false) {
$DB->prepared_query("
SELECT ID
FROM requests
WHERE TimeFilled IS NULL
AND GroupID = ?
", $GroupID
);
$Requests = $DB->collect('ID');
$Cache->cache_value("requests_group_$GroupID", $Requests, 0);
}
return Requests::get_requests($Requests);
}
// Count the number of audio files in a torrent file list per audio type
function audio_file_map($fileList) {
$map = [];
foreach (explode("\n", strtolower($fileList)) as $file) {
$info = Torrents::filelist_get_file($file);
if (!isset($info['ext'])) {
continue;
}
$ext = substr($info['ext'], 1); // skip over period
if (in_array($ext, ['ac3', 'flac', 'm4a', 'mp3'])) {
if (!isset($map[$ext])) {
$map[$ext] = 0;
}
++$map[$ext];
}
}
return $map;
}
function set_source(
\OrpheusNET\BencodeTorrent\BencodeTorrent $torrent,
string $siteSource,
string $grandfatherSource,
int $grandfatherSourceDate,
int $grandfatherNoSourceDate
) {
$torrentSource = $torrent->getSource();
$creationDate = $torrent->getCreationDate();
if ($torrentSource === $siteSource) {
return false;
}
if (!is_null($creationDate)) {
if (is_null($torrentSource) && $creationDate <= $grandfatherNoSourceDate) {
return false;
}
elseif (!is_null($torrentSource) && $torrentSource === $grandfatherSource && $creationDate <= $grandfatherSourceDate) {
return false;
}
}
return $torrent->setSource($siteSource);
}

View File

@@ -7,7 +7,6 @@ if ($groupId < 1) {
error(404);
}
require(__DIR__ . '/functions.php');
$torrentCache = get_group_info($groupId, $RevisionID);
list(, , $groupId, $groupName, $year, , , , , , $VH) = array_values($torrentCache[0]);

View File

@@ -13,8 +13,6 @@ use OrpheusNET\BencodeTorrent\BencodeTorrent;
ini_set('max_file_uploads', 100);
define('MAX_FILENAME_LENGTH', 255);
require_once(__DIR__ . '/../torrents/functions.php');
enforce_login();
if (!defined('AJAX')) {

View File

@@ -12,8 +12,6 @@ if (!check_perms('users_mod') || !check_perms('users_view_ips')) {
error(403);
}
require_once(__DIR__ . '/../torrents/functions.php');
$userId = (int)$_GET['userid'] ?? null;
$ipAddr = $_GET['ip'] ?? null;