Gazelle/sections/torrents/notify.php

345 lines
13 KiB
PHP
Raw Normal View History

2013-05-28 08:01:02 +00:00
<?php
2012-10-27 08:00:09 +00:00
if (!check_perms('site_torrents_notify')) {
error(403);
}
2011-03-28 14:21:28 +00:00
define('NOTIFICATIONS_PER_PAGE', 50);
2012-10-27 08:00:09 +00:00
define('NOTIFICATIONS_MAX_SLOWSORT', 10000);
$OrderBys = array(
2013-05-25 08:01:03 +00:00
'time' => array('unt' => 'unt.TorrentID'),
'size' => array('t' => 't.Size'),
'snatches' => array('t' => 't.Snatched'),
'seeders' => array('t' => 't.Seeders'),
'leechers' => array('t' => 't.Leechers'),
'year' => array('tg' => 'tnt.Year'));
2012-10-27 08:00:09 +00:00
if (empty($_GET['order_by']) || !isset($OrderBys[$_GET['order_by']])) {
$_GET['order_by'] = 'time';
}
list($OrderTbl, $OrderCol) = each($OrderBys[$_GET['order_by']]);
2011-03-28 14:21:28 +00:00
2012-10-27 08:00:09 +00:00
if (!empty($_GET['order_way']) && $_GET['order_way'] == 'asc') {
$OrderWay = 'ASC';
} else {
$OrderWay = 'DESC';
}
if (!empty($_GET['filterid']) && is_number($_GET['filterid'])) {
$FilterID = $_GET['filterid'];
} else {
$FilterID = false;
}
2013-05-28 08:01:02 +00:00
list($Page, $Limit) = Format::page_limit(NOTIFICATIONS_PER_PAGE);
2012-06-09 08:00:19 +00:00
2012-10-27 08:00:09 +00:00
// The "order by x" links on columns headers
2013-05-25 08:01:03 +00:00
function header_link($SortKey, $DefaultWay = 'desc') {
2012-10-27 08:00:09 +00:00
global $OrderWay;
if ($SortKey == $_GET['order_by']) {
2013-05-25 08:01:03 +00:00
if ($OrderWay == 'DESC') {
$NewWay = 'asc';
2012-10-27 08:00:09 +00:00
} else {
2013-05-25 08:01:03 +00:00
$NewWay = 'desc';
2012-10-27 08:00:09 +00:00
}
} else {
$NewWay = $DefaultWay;
}
2013-05-25 08:01:03 +00:00
return "?action=notify&amp;order_way=$NewWay&amp;order_by=$SortKey&amp;".Format::get_url(array('page', 'order_way', 'order_by'));
2012-10-27 08:00:09 +00:00
}
2013-07-10 00:08:53 +00:00
//Perhaps this should be a feature at some point
if (check_perms('users_mod') && !empty($_GET['userid']) && is_number($_GET['userid']) && $_GET['userid'] != $LoggedUser['ID']) {
$UserID = $_GET['userid'];
$Sneaky = true;
} else {
$Sneaky = false;
$UserID = $LoggedUser['ID'];
}
2012-06-09 08:00:19 +00:00
2012-10-27 08:00:09 +00:00
// Sorting by release year requires joining torrents_group, which is slow. Using a temporary table
// makes it speedy enough as long as there aren't too many records to create
if ($OrderTbl == 'tg') {
2013-05-25 08:01:03 +00:00
$DB->query("
SELECT COUNT(*)
FROM users_notify_torrents AS unt
JOIN torrents AS t ON t.ID=unt.TorrentID
2012-10-27 08:00:09 +00:00
WHERE unt.UserID=$UserID".
($FilterID
? " AND FilterID=$FilterID"
2013-05-25 08:01:03 +00:00
: ''));
2012-10-27 08:00:09 +00:00
list($TorrentCount) = $DB->next_record();
if ($TorrentCount > NOTIFICATIONS_MAX_SLOWSORT) {
2013-05-25 08:01:03 +00:00
error('Due to performance issues, torrent lists with more than '.number_format(NOTIFICATIONS_MAX_SLOWSORT).' items cannot be ordered by release year.');
2012-10-27 08:00:09 +00:00
}
2013-05-25 08:01:03 +00:00
$DB->query("
CREATE TEMPORARY TABLE temp_notify_torrents
(TorrentID int, GroupID int, UnRead tinyint, FilterID int, Year smallint, PRIMARY KEY(GroupID, TorrentID), KEY(Year))
ENGINE=MyISAM");
$DB->query("
INSERT IGNORE INTO temp_notify_torrents (TorrentID, GroupID, UnRead, FilterID)
2012-10-27 08:00:09 +00:00
SELECT t.ID, t.GroupID, unt.UnRead, unt.FilterID
2013-05-25 08:01:03 +00:00
FROM users_notify_torrents AS unt
JOIN torrents AS t ON t.ID=unt.TorrentID
2012-10-27 08:00:09 +00:00
WHERE unt.UserID=$UserID".
($FilterID
? " AND unt.FilterID=$FilterID"
2013-05-25 08:01:03 +00:00
: ''));
$DB->query("
UPDATE temp_notify_torrents AS tnt
2013-07-10 00:08:53 +00:00
JOIN torrents_group AS tg ON tnt.GroupID = tg.ID
SET tnt.Year = tg.Year");
2012-10-27 08:00:09 +00:00
2013-05-25 08:01:03 +00:00
$DB->query("
SELECT TorrentID, GroupID, UnRead, FilterID
2012-10-27 08:00:09 +00:00
FROM temp_notify_torrents AS tnt
2013-05-25 08:01:03 +00:00
ORDER BY $OrderCol $OrderWay, GroupID $OrderWay
LIMIT $Limit");
2012-10-27 08:00:09 +00:00
$Results = $DB->to_array(false, MYSQLI_ASSOC, false);
} else {
2013-05-25 08:01:03 +00:00
$DB->query("
2013-05-29 08:00:51 +00:00
SELECT
SQL_CALC_FOUND_ROWS
unt.TorrentID,
unt.UnRead,
unt.FilterID,
t.GroupID
2011-03-28 14:21:28 +00:00
FROM users_notify_torrents AS unt
2013-05-25 08:01:03 +00:00
JOIN torrents AS t ON t.ID = unt.TorrentID
2013-07-10 00:08:53 +00:00
WHERE unt.UserID = $UserID".
2012-10-27 08:00:09 +00:00
($FilterID
2013-07-10 00:08:53 +00:00
? " AND unt.FilterID = $FilterID"
2013-05-25 08:01:03 +00:00
: '')."
ORDER BY $OrderCol $OrderWay
LIMIT $Limit");
2012-10-27 08:00:09 +00:00
$Results = $DB->to_array(false, MYSQLI_ASSOC, false);
2013-07-10 00:08:53 +00:00
$DB->query('SELECT FOUND_ROWS()');
2012-10-27 08:00:09 +00:00
list($TorrentCount) = $DB->next_record();
}
2012-06-09 08:00:19 +00:00
2012-10-27 08:00:09 +00:00
$GroupIDs = $FilterIDs = $UnReadIDs = array();
foreach ($Results as $Torrent) {
$GroupIDs[$Torrent['GroupID']] = 1;
$FilterIDs[$Torrent['FilterID']] = 1;
if ($Torrent['UnRead']) {
$UnReadIDs[] = $Torrent['TorrentID'];
}
}
2012-10-11 08:00:15 +00:00
$Pages = Format::get_pages($Page, $TorrentCount, NOTIFICATIONS_PER_PAGE, 9);
2011-03-28 14:21:28 +00:00
2012-10-27 08:00:09 +00:00
if (!empty($GroupIDs)) {
$GroupIDs = array_keys($GroupIDs);
$FilterIDs = array_keys($FilterIDs);
2012-10-11 08:00:15 +00:00
$TorrentGroups = Torrents::get_groups($GroupIDs);
2011-03-28 14:21:28 +00:00
2012-10-27 08:00:09 +00:00
// Get the relevant filter labels
2013-05-25 08:01:03 +00:00
$DB->query('
SELECT ID, Label, Artists
FROM users_notify_filters
WHERE ID IN ('.implode(',', $FilterIDs).')');
2012-10-30 08:00:18 +00:00
$Filters = $DB->to_array('ID', MYSQLI_ASSOC, array('Artists'));
2012-10-27 08:00:09 +00:00
foreach ($Filters as &$Filter) {
$Filter['Artists'] = explode('|', trim($Filter['Artists'], '|'));
2012-11-02 08:00:18 +00:00
foreach ($Filter['Artists'] as &$FilterArtist) {
$FilterArtist = mb_strtolower($FilterArtist, 'UTF-8');
}
$Filter['Artists'] = array_flip($Filter['Artists']);
2012-10-27 08:00:09 +00:00
}
unset($Filter);
if (!empty($UnReadIDs)) {
//Clear before header but after query so as to not have the alert bar on this page load
2013-05-25 08:01:03 +00:00
$DB->query("
UPDATE users_notify_torrents
2013-07-10 00:08:53 +00:00
SET UnRead = '0'
WHERE UserID = ".$LoggedUser['ID'].'
2013-05-25 08:01:03 +00:00
AND TorrentID IN ('.implode(',', $UnReadIDs).')');
2012-10-27 08:00:09 +00:00
$Cache->delete_value('notifications_new_'.$LoggedUser['ID']);
}
2012-06-09 08:00:19 +00:00
}
2013-07-10 00:08:53 +00:00
if ($Sneaky) {
$UserInfo = Users::user_info($UserID);
View::show_header($UserInfo['Username'].'\'s notifications', 'notifications');
} else {
View::show_header('My notifications', 'notifications');
}
2011-03-28 14:21:28 +00:00
?>
2012-12-19 08:00:21 +00:00
<div class="thin widethin">
2012-08-19 08:00:19 +00:00
<div class="header">
2012-10-27 08:00:09 +00:00
<h2>Latest notifications</h2>
</div>
<div class="linkbox">
2013-07-10 00:08:53 +00:00
<? if ($FilterID) { ?>
<a href="torrents.php?action=notify<?=($Sneaky ? "&amp;userid=$UserID" : '')?>" class="brackets">View all</a>&nbsp;&nbsp;&nbsp;
<? } elseif (!$Sneaky) { ?>
<a href="torrents.php?action=notify_clear&amp;auth=<?=$LoggedUser['AuthKey']?>" class="brackets">Clear all old</a>&nbsp;&nbsp;&nbsp;
<a href="#" onclick="clearSelected(); return false;" class="brackets">Clear selected</a>&nbsp;&nbsp;&nbsp;
2013-01-24 08:00:24 +00:00
<a href="torrents.php?action=notify_catchup&amp;auth=<?=$LoggedUser['AuthKey']?>" class="brackets">Catch up</a>&nbsp;&nbsp;&nbsp;
2013-07-10 00:08:53 +00:00
<? } ?>
2013-01-24 08:00:24 +00:00
<a href="user.php?action=notify" class="brackets">Edit filters</a>&nbsp;&nbsp;&nbsp;
2012-08-19 08:00:19 +00:00
</div>
2012-10-27 08:00:09 +00:00
<? if ($TorrentCount > NOTIFICATIONS_PER_PAGE) { ?>
2011-03-28 14:21:28 +00:00
<div class="linkbox">
<?=$Pages?>
</div>
2012-10-27 08:00:09 +00:00
<?
}
if (empty($Results)) {
?>
2012-09-01 08:00:24 +00:00
<table class="layout border">
2011-03-28 14:21:28 +00:00
<tr class="rowb">
2012-02-09 08:00:20 +00:00
<td colspan="8" class="center">
2013-01-24 08:00:24 +00:00
No new notifications found! <a href="user.php?action=notify" class="brackets">Edit notification filters</a>
2011-03-28 14:21:28 +00:00
</td>
</tr>
</table>
2012-10-27 08:00:09 +00:00
<?
} else {
2011-03-28 14:21:28 +00:00
$FilterGroups = array();
2012-10-27 08:00:09 +00:00
foreach ($Results as $Result) {
if (!isset($FilterGroups[$Result['FilterID']])) {
2011-03-28 14:21:28 +00:00
$FilterGroups[$Result['FilterID']] = array();
2012-10-27 08:00:09 +00:00
$FilterGroups[$Result['FilterID']]['FilterLabel'] = isset($Filters[$Result['FilterID']])
? $Filters[$Result['FilterID']]['Label']
: false;
2011-03-28 14:21:28 +00:00
}
2012-10-27 08:00:09 +00:00
$FilterGroups[$Result['FilterID']][] = $Result;
2011-03-28 14:21:28 +00:00
}
2012-10-27 08:00:09 +00:00
foreach ($FilterGroups as $FilterID => $FilterResults) {
2011-03-28 14:21:28 +00:00
?>
2012-10-27 08:00:09 +00:00
<div class="header">
<h3>
2013-07-10 00:08:53 +00:00
<? if ($FilterResults['FilterLabel'] !== false) { ?>
Matches for <a href="torrents.php?action=notify&amp;filterid=<?=$FilterID.($Sneaky ? "&amp;userid=$UserID" : '')?>"><?=$FilterResults['FilterLabel']?></a>
<? } else { ?>
Matches for unknown filter[<?=$FilterID?>]
<? } ?>
2012-10-27 08:00:09 +00:00
</h3>
</div>
2013-02-11 08:00:34 +00:00
<div class="linkbox notify_filter_links">
2013-07-10 00:08:53 +00:00
<? if (!$Sneaky) { ?>
<a href="#" onclick="clearSelected(<?=$FilterID?>); return false;" class="brackets">Clear selected in filter</a>
2013-02-11 08:00:34 +00:00
<a href="torrents.php?action=notify_clear_filter&amp;filterid=<?=$FilterID?>&amp;auth=<?=$LoggedUser['AuthKey']?>" class="brackets">Clear all old in filter</a>
2013-02-07 08:00:47 +00:00
<a href="torrents.php?action=notify_catchup_filter&amp;filterid=<?=$FilterID?>&amp;auth=<?=$LoggedUser['AuthKey']?>" class="brackets">Mark all in filter as read</a>
2013-07-10 00:08:53 +00:00
<? } ?>
2012-10-27 08:00:09 +00:00
</div>
2012-12-27 08:00:27 +00:00
<form class="manage_form" name="torrents" id="notificationform_<?=$FilterID?>" action="">
2012-09-01 08:00:24 +00:00
<table class="torrent_table cats checkboxes border">
2011-03-28 14:21:28 +00:00
<tr class="colhead">
2013-08-11 08:00:48 +00:00
<td style="text-align: center;"><input type="checkbox" name="toggle" onclick="toggleChecks('notificationform_<?=$FilterID?>', this, '.notify_box')" /></td>
2011-03-28 14:21:28 +00:00
<td class="small cats_col"></td>
2013-05-25 08:01:03 +00:00
<td style="width: 100%;">Name<?=$TorrentCount <= NOTIFICATIONS_MAX_SLOWSORT ? ' / <a href="'.header_link('year').'">Year</a>' : ''?></td>
2012-10-27 08:00:09 +00:00
<td>Files</td>
<td><a href="<?=header_link('time')?>">Time</a></td>
<td><a href="<?=header_link('size')?>">Size</a></td>
<td class="sign"><a href="<?=header_link('snatches')?>"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/snatched.png" alt="Snatches" title="Snatches" /></a></td>
<td class="sign"><a href="<?=header_link('seeders')?>"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/seeders.png" alt="Seeders" title="Seeders" /></a></td>
<td class="sign"><a href="<?=header_link('leechers')?>"><img src="static/styles/<?=$LoggedUser['StyleName']?>/images/leechers.png" alt="Leechers" title="Leechers" /></a></td>
2011-03-28 14:21:28 +00:00
</tr>
<?
unset($FilterResults['FilterLabel']);
2012-10-27 08:00:09 +00:00
foreach ($FilterResults as $Result) {
2012-06-09 08:00:19 +00:00
$TorrentID = $Result['TorrentID'];
$GroupID = $Result['GroupID'];
$GroupInfo = $TorrentGroups[$Result['GroupID']];
2012-10-27 08:00:09 +00:00
if (!isset($GroupInfo['Torrents'][$TorrentID]) || !isset($GroupInfo['ID'])) {
// If $GroupInfo['ID'] is unset, the torrent group associated with the torrent doesn't exist
2012-06-09 08:00:19 +00:00
continue;
2011-10-29 08:00:15 +00:00
}
2012-10-16 08:00:18 +00:00
$TorrentInfo = $GroupInfo['Torrents'][$TorrentID];
2012-06-09 08:00:19 +00:00
// generate torrent's title
$DisplayName = '';
2012-10-27 08:00:09 +00:00
if (!empty($GroupInfo['ExtendedArtists'])) {
$MatchingArtists = array();
foreach ($GroupInfo['ExtendedArtists'] as $GroupArtists) {
foreach ($GroupArtists as $GroupArtist) {
2012-11-02 08:00:18 +00:00
if (isset($Filters[$FilterID]['Artists'][mb_strtolower($GroupArtist['name'], 'UTF-8')])) {
$MatchingArtists[] = $GroupArtist['name'];
2012-10-27 08:00:09 +00:00
}
}
}
2013-05-21 08:01:09 +00:00
$MatchingArtistsText = (!empty($MatchingArtists) ? 'Caught by filter for '.implode(', ', $MatchingArtists) : '');
2012-10-11 08:00:15 +00:00
$DisplayName = Artists::display_artists($GroupInfo['ExtendedArtists'], true, true);
2011-03-28 14:21:28 +00:00
}
2013-05-21 08:01:09 +00:00
$DisplayName .= "<a href=\"torrents.php?id=$GroupID&amp;torrentid=$TorrentID#torrent$TorrentID\" title=\"View Torrent\" dir=\"ltr\">" . $GroupInfo['Name'] . '</a>';
2012-06-09 08:00:19 +00:00
2013-02-25 21:16:55 +00:00
$GroupCategoryID = $GroupInfo['CategoryID'];
2012-10-27 08:00:09 +00:00
if ($GroupCategoryID == 1) {
if ($GroupInfo['Year'] > 0) {
2012-06-09 08:00:19 +00:00
$DisplayName .= " [$GroupInfo[Year]]";
}
2012-10-27 08:00:09 +00:00
if ($GroupInfo['ReleaseType'] > 0) {
2013-08-28 23:08:41 +00:00
$DisplayName .= ' ['.$ReleaseTypes[$GroupInfo['ReleaseType']].']';
2012-06-09 08:00:19 +00:00
}
2012-02-10 08:00:19 +00:00
}
2012-06-09 08:00:19 +00:00
2011-03-28 14:21:28 +00:00
// append extra info to torrent title
2012-10-11 08:00:15 +00:00
$ExtraInfo = Torrents::torrent_info($TorrentInfo, true, true);
2012-06-09 08:00:19 +00:00
2013-02-25 21:16:55 +00:00
$TorrentTags = new Tags($GroupInfo['TagList']);
if ($GroupInfo['TagList'] == '')
2013-05-21 08:01:09 +00:00
$TorrentTags->set_primary($Categories[$GroupCategoryID - 1]);
2011-03-28 14:21:28 +00:00
// print row
?>
2013-05-21 08:01:09 +00:00
<tr class="torrent torrent_row<?=($TorrentInfo['IsSnatched'] ? ' snatched_torrent' : '') . ($GroupInfo['Flags']['IsSnatched'] ? ' snatched_group' : '')?>" id="torrent<?=$TorrentID?>"<?=($MatchingArtistsText ? 'title="'.display_str($MatchingArtistsText).'"' : '')?>>
2013-09-03 08:01:00 +00:00
<td style="text-align: center;"><input type="checkbox" class="notify_box notify_box_<?=$FilterID?>" value="<?=$TorrentID?>" id="clear_<?=$TorrentID?>" tabindex="1" /></td>
2013-02-25 21:16:55 +00:00
<td class="center cats_col"><div title="<?=$TorrentTags->title()?>"class="<?=Format::css_category($GroupCategoryID)?> <?=$TorrentTags->css_name()?>"></div></td>
<td class="big_info">
2013-08-28 23:08:41 +00:00
<? if ($LoggedUser['CoverArt']) { ?>
2013-02-25 21:16:55 +00:00
<div class="group_image float_left clear">
2013-04-30 18:18:07 +00:00
<? ImageTools::cover_thumb($GroupInfo['WikiImage'], $GroupCategoryID) ?>
2013-02-25 21:16:55 +00:00
</div>
2013-08-28 23:08:41 +00:00
<? } ?>
2013-02-25 21:16:55 +00:00
<div class="group_info clear">
<span>
[ <a href="torrents.php?action=download&amp;id=<?=$TorrentID?>&amp;authkey=<?=$LoggedUser['AuthKey']?>&amp;torrent_pass=<?=$LoggedUser['torrent_pass']?>" title="Download">DL</a>
2012-10-27 08:00:09 +00:00
<? if (Torrents::can_use_token($TorrentInfo)) { ?>
2013-08-28 23:08:41 +00:00
| <a href="torrents.php?action=download&amp;id=<?=$TorrentID?>&amp;authkey=<?=$LoggedUser['AuthKey']?>&amp;torrent_pass=<?=$LoggedUser['torrent_pass']?>&amp;usetoken=1" title="Use a FL Token" onclick="return confirm('Are you sure you want to use a freeleech token here?');" class="tooltip">FL</a>
2013-07-10 00:08:53 +00:00
<?
}
if (!$Sneaky) { ?>
2013-08-28 23:08:41 +00:00
| <a href="#" onclick="clearItem(<?=$TorrentID?>); return false;" class="tooltip" title="Remove from notifications list">CL</a>
2013-07-10 00:08:53 +00:00
<? } ?> ]
2013-02-25 21:16:55 +00:00
</span>
<strong><?=$DisplayName?></strong>
<div class="torrent_info">
<?=$ExtraInfo?>
<? if ($Result['UnRead']) {
echo '<strong class="new">New!</strong>';
} ?>
2013-07-10 00:08:53 +00:00
<span class="bookmark" style="float: right;">
<? if (Bookmarks::has_bookmarked('torrent', $GroupID)) { ?>
2013-08-28 23:08:41 +00:00
<a href="#" id="bookmarklink_torrent_<?=$GroupID?>" class="remove_bookmark" onclick="Unbookmark('torrent', <?=$GroupID?>, 'Bookmark'); return false;">Remove bookmark</a>
2013-05-27 08:00:58 +00:00
<? } else { ?>
2013-08-28 23:08:41 +00:00
<a href="#" id="bookmarklink_torrent_<?=$GroupID?>" class="add_bookmark" onclick="Bookmark('torrent', <?=$GroupID?>, 'Remove bookmark'); return false;">Bookmark</a>
2013-05-27 08:00:58 +00:00
<? } ?>
2013-07-10 00:08:53 +00:00
</span>
2013-02-25 21:16:55 +00:00
</div>
<div class="tags"><?=$TorrentTags->format()?></div>
2013-02-12 08:00:08 +00:00
</div>
2011-03-28 14:21:28 +00:00
</td>
2012-06-09 08:00:19 +00:00
<td><?=$TorrentInfo['FileCount']?></td>
2013-05-25 08:01:03 +00:00
<td style="text-align: right;" class="nobr"><?=time_diff($TorrentInfo['Time'])?></td>
<td class="nobr" style="text-align: right;"><?=Format::get_size($TorrentInfo['Size'])?></td>
<td style="text-align: right;"><?=number_format($TorrentInfo['Snatched'])?></td>
<td style="text-align: right;"><?=number_format($TorrentInfo['Seeders'])?></td>
<td style="text-align: right;"><?=number_format($TorrentInfo['Leechers'])?></td>
2011-03-28 14:21:28 +00:00
</tr>
<?
}
?>
</table>
2012-02-09 08:00:20 +00:00
</form>
2011-03-28 14:21:28 +00:00
<?
}
}
2013-05-21 08:01:09 +00:00
if ($Pages) { ?>
<div class="linkbox"><?=$Pages?></div>
<? } ?>
2012-12-02 08:00:19 +00:00
</div>
2012-10-11 08:00:15 +00:00
<? View::show_footer(); ?>