Skip to content

Commit

Permalink
Merge branch '7.x.x' into Livewire-3
Browse files Browse the repository at this point in the history
  • Loading branch information
HDVinnie committed Aug 1, 2023
2 parents a70518a + 12382d6 commit f0e6794
Show file tree
Hide file tree
Showing 86 changed files with 3,997 additions and 3,839 deletions.
4 changes: 2 additions & 2 deletions app/Console/Commands/AutoDeleteStoppedPeers.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class AutoDeleteStoppedPeers extends Command
*/
public function handle(): void
{
Peer::where('active', '=', 0)->delete();
Peer::where('active', '=', 0)->where('updated_at', '>', now()->subHours(2))->delete();

$this->comment('Automated upsert histories command complete');
$this->comment('Automated delete stopped peers command complete');
}
}
14 changes: 12 additions & 2 deletions app/Console/Commands/AutoFlushPeers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class AutoFlushPeers extends Command
public function handle(): void
{
$carbon = new Carbon();
$peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at'])->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())->get();
$peers = Peer::select(['id', 'torrent_id', 'user_id', 'updated_at'])
->where('updated_at', '<', $carbon->copy()->subHours(2)->toDateTimeString())
->where('active', '=', 1)
->get();

foreach ($peers as $peer) {
$history = History::where('torrent_id', '=', $peer->torrent_id)->where('user_id', '=', $peer->user_id)->first();
Expand All @@ -57,9 +60,16 @@ public function handle(): void
$history->save();
}

$peer->delete();
$peer->active = false;
$peer->timestamps = false;
$peer->save();
}

// Keep peers that stopped being announced without a `stopped` event
// in case a user has internet issues and comes back online within the
// next 2 days
Peer::where('updated_at', '<', $carbon->copy()->subDays(2))->where('active', '=', 0)->delete();

$this->comment('Automated Flush Ghost Peers Command Complete');
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/AutoRemovePersonalFreeleech.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function handle(): void
// Delete The Record From DB
$pfl->delete();

cache()->forget('personal_freeleech:'.$pfl->user_id);
cache()->put('personal_freeleech:'.$pfl->user_id, false);
Unit3dAnnounce::removePersonalFreeleech($pfl->user_id);
}

Expand Down
18 changes: 9 additions & 9 deletions app/Console/Commands/AutoUpsertHistories.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use App\Models\History;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Exception;

Expand Down Expand Up @@ -75,20 +76,19 @@ public function handle(): void
$histories,
['user_id', 'torrent_id'],
[
'user_id',
'torrent_id',
'agent',
'uploaded',
'actual_uploaded',
'uploaded' => DB::raw('uploaded + VALUES(uploaded)'),
'actual_uploaded' => DB::raw('actual_uploaded + VALUES(actual_uploaded)'),
'client_uploaded',
'downloaded',
'actual_downloaded',
'downloaded' => DB::raw('downloaded + VALUES(downloaded)'),
'actual_downloaded' => DB::raw('actual_downloaded + VALUES(actual_downloaded)'),
'client_downloaded',
'seeder',
'active',
'seedtime',
'immune',
'completed_at',
// 5400 is the max announce interval defined in the announce controller
'seedtime' => DB::raw('IF(DATE_ADD(updated_at, INTERVAL 5400 SECOND) > VALUES(updated_at) AND seeder = 1 AND VALUES(seeder) = 1, seedtime + TIMESTAMPDIFF(SECOND, updated_at, VALUES(updated_at)), seedtime)'),
'immune' => DB::raw('immune AND VALUES(immune)'),
'completed_at' => DB::raw('COALESCE(completed_at, VALUES(completed_at))'),
],
);
}
Expand Down
7 changes: 7 additions & 0 deletions app/Helpers/Bbcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ class Bbcode
'closeHtml' => '</pre>',
'block' => true,
],
'pre' => [
'openBbcode' => '/^\[pre\]/i',
'closeBbcode' => '[/pre]',
'openHtml' => '<code>',
'closeHtml' => '</code>',
'block' => false,
],
'alert' => [
'openBbcode' => '/^\[alert\]/i',
'closeBbcode' => '[/alert]',
Expand Down
17 changes: 17 additions & 0 deletions app/Helpers/Bencode.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ public static function get_meta($t): array
return $result;
}

public static function get_name($t): ?string
{
$name = null;

if (
\array_key_exists('info', $t)
&& \is_array($t['info'])
&& \array_key_exists('name', $t['info'])
&& \is_string($t['info']['name'])
&& \array_key_exists('files', $t['info'])
) {
$name = $t['info']['name'];
}

return $name;
}

public static function is_v2_or_hybrid($t): bool
{
return isset($t['piece layers']);
Expand Down
29 changes: 14 additions & 15 deletions app/Helpers/TorrentTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public static function normalizeTorrent($torrentFile)
'pieces' => '',
]);

// The PID will be set if an user downloads the torrent, but for
// security purposes it's better to overwrite the user-provided
// announce URL.
$result['announce'] = config('app.url').'/announce/PID';
$result['info']['source'] = config('torrent.source');
$result['info']['private'] = 1;

Expand Down Expand Up @@ -200,18 +196,21 @@ public static function getNfo($inputFile): bool|string|null
/**
* Check if the filename is valid or not.
*/
public static function isValidFilename($filename): bool
public static function isValidFilename(string $filename): bool
{
$result = true;

if (\strlen((string) $filename) > 255 ||
preg_match('#[/?<>\\:*|"\x00-\x1f]#', (string) $filename) ||
preg_match('#(^\.+|[\. ]+)$#', (string) $filename) ||
preg_match('#^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$#i', (string) $filename)) {
$result = false;
}

return $result;
return !(
\strlen($filename) > 255
// nodes containing: `\`, `/`, `?`, `<`, `>`, `:`, `8`, `|`, and ascii characters from 0 through 31
|| preg_match('/[\\\\\\/?<>:*|"\x00-\x1f]/', $filename)
// nodes only containing one or many: `.`; or only containing one or many `.`, ` `.
|| preg_match('/(^\\.+|[. ]+)$/', $filename)
// Special windows filenames.
|| preg_match('/^(con|prn|aux|nul|com\d|lpt\d)(\\..*)?$/i', $filename)
// BitComet padding files
|| preg_match('/^\.?____padding.*$/i', $filename)
// BEP 47 torrent padding files that many clients aren't able to handle
|| str_starts_with($filename, '.pad')
);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/API/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function store(Request $request): \Illuminate\Http\JsonResponse
$torrent->info_hash = $infohash;
$torrent->file_name = $fileName;
$torrent->num_file = $meta['count'];
$torrent->announce = $decodedTorrent['announce'];
$torrent->folder = Bencode::get_name($decodedTorrent);
$torrent->size = $meta['size'];
$torrent->nfo = ($request->hasFile('nfo')) ? TorrentTools::getNfo($request->file('nfo')) : '';
$torrent->category_id = $category->id;
Expand Down Expand Up @@ -210,7 +210,6 @@ public function store(Request $request): \Illuminate\Http\JsonResponse
'info_hash' => 'required|unique:torrents',
'file_name' => 'required',
'num_file' => 'required|numeric',
'announce' => 'required',
'size' => 'required',
'category_id' => 'required|exists:categories,id',
'type_id' => 'required|exists:types,id',
Expand Down
21 changes: 13 additions & 8 deletions app/Http/Controllers/AnnounceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ private function checkAnnounceFields(Request $request): array
);
}

$queries['event'] = strtolower($queries['event']);

throw_if(
! \in_array(strtolower($queries['event']), ['started', 'completed', 'stopped', 'paused', '']),
new TrackerException(136, [':event' => strtolower($queries['event'])])
! \in_array($queries['event'], ['started', 'completed', 'stopped', 'paused', '']),
new TrackerException(136, [':event' => $queries['event']])
);

// Part.3 check Port is Valid and Allowed
Expand All @@ -274,8 +276,8 @@ private function checkAnnounceFields(Request $request): array
* However, in some case , When `&event=stopped` the port may set to 0.
*/
throw_if(
$queries['port'] === 0 && strtolower($queries['event']) !== 'stopped',
new TrackerException(137, [':event' => strtolower($queries['event'])])
$queries['port'] === 0 && $queries['event'] !== 'stopped',
new TrackerException(137, [':event' => $queries['event']])
);

throw_if(! is_numeric($queries['port']) || $queries['port'] < 0 || $queries['port'] > 0xFFFF
Expand Down Expand Up @@ -425,7 +427,7 @@ protected function checkTorrent($infoHash): object
// If we use eager loading, then laravel will use `where torrent_id in (123)` instead of `where torrent_id = ?`
$torrent->setRelation(
'peers',
Peer::select(['id', 'torrent_id', 'peer_id', 'user_id', 'left', 'seeder', 'port', 'updated_at'])
Peer::select(['id', 'torrent_id', 'peer_id', 'user_id', 'left', 'seeder', 'active', 'port', 'updated_at'])
->selectRaw('INET6_NTOA(ip) as ip')
->where('torrent_id', '=', $torrent->id)
->get()
Expand All @@ -443,7 +445,7 @@ protected function checkTorrent($infoHash): object
private function checkPeer($torrent, $queries, $user): void
{
throw_if(
strtolower($queries['event']) === 'completed'
$queries['event'] === 'completed'
&& $torrent->peers
->where('peer_id', '=', base64_decode($queries['peer_id']))
->where('user_id', '=', $user->id)
Expand All @@ -469,7 +471,7 @@ private function checkMinInterval($torrent, $queries, $user): void
$randomMinInterval = random_int($setMin, $setMin * 2);
throw_if(
$prevAnnounce && $prevAnnounce->updated_at->greaterThan(now()->subSeconds($randomMinInterval))
&& strtolower($queries['event']) !== 'completed' && strtolower($queries['event']) !== 'stopped',
&& $queries['event'] !== 'completed' && $queries['event'] !== 'stopped',
new TrackerException(162, [':min' => $randomMinInterval])
);
}
Expand All @@ -485,6 +487,7 @@ private function checkMaxConnections($torrent, $user): void
// Pull Count On Users Peers Per Torrent For Rate Limiting
$connections = $torrent->peers
->where('user_id', '=', $user->id)
->where('active', '=', true)
->count();

// If Users Peer Count On A Single Torrent Is Greater Than X Return Error to Client
Expand All @@ -509,6 +512,7 @@ private function checkDownloadSlots($queries, $user, $group): void
->where('user_id', '=', $user->id)
->where('peer_id', '!=', base64_decode($queries['peer_id']))
->where('seeder', '=', 0)
->where('active', '=', true)
->count();

throw_if(
Expand Down Expand Up @@ -539,13 +543,14 @@ private function generateSuccessAnnounceResponse($queries, $torrent, $user): arr
* For non `stopped` event only
* We query peers from database and send peerlist, otherwise just quick return.
*/
if (strtolower($queries['event']) !== 'stopped') {
if ($queries['event'] !== 'stopped') {
$limit = (min($queries['numwant'], 25));

// Get Torrents Peers (Only include leechers in a seeder's peerlist)
$peers = $torrent->peers
->when($queries['left'] == 0, fn ($query) => $query->where('seeder', '=', 0))
->where('user_id', '!=', $user->id)
->where('active', '=', true)
->take($limit)
->map
->only(['ip', 'port'])
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ApprovedRequestFillController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function store(Request $request, TorrentRequest $torrentRequest): \Illumi
}

return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->withSuccess(sprintf(trans('request.approved-user'), $torrentRequest->filled_anon ? 'Anonymous' : $torrentRequest->name, $filler->username));
->withSuccess(sprintf(trans('request.approved-user'), $torrentRequest->name, $torrentRequest->filled_anon ? 'Anonymous' : $filler->username));
}

/**
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Controllers/BountyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
namespace App\Http\Controllers;

use App\Http\Requests\StoreTorrentRequestBountyRequest;
use App\Http\Requests\UpdateTorrentRequestBountyRequest;
use App\Models\BonTransactions;
use App\Models\TorrentRequest;
use App\Models\TorrentRequestBounty;
use App\Notifications\NewRequestBounty;
use App\Repositories\ChatRepository;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -91,4 +93,13 @@ public function store(StoreTorrentRequestBountyRequest $request, TorrentRequest
return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->withSuccess(trans('request.added-bonus'));
}

public function update(UpdateTorrentRequestBountyRequest $request, TorrentRequest $torrentRequest, TorrentRequestBounty $torrentRequestBounty): \Illuminate\Http\RedirectResponse
{
abort_unless($request->user()->group->is_modo || $request->user()->id === $torrentRequestBounty->user_id, 403);

$torrentRequestBounty->update($request->validated());

return back();
}
}
42 changes: 36 additions & 6 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
'user' => $user,
'personal_freeleech' => cache()->get('personal_freeleech:'.$user->id),
'users' => cache()->remember(
'online_users',
'online_users:by-group:'.auth()->user()->group_id,
$expiresAt,
fn () => User::with('group', 'privacy')
->withCount([
'warnings' => function (Builder $query): void {
$query->whereNotNull('torrent')->where('active', '1');
},
])
->where('last_action', '>', now()->subMinutes(5))
->where('last_action', '>', now()->subDays(19))
->orderByRaw('(select position from `groups` where `groups`.id = users.group_id), group_id, username')
->get()
->sortBy(fn ($user) => $user->hidden || ! $user->isVisible($user, 'other', 'show_online'))
),
'groups' => cache()->remember(
'user-groups',
Expand Down Expand Up @@ -403,15 +405,41 @@ function () use ($user) {
}
),
'topics' => cache()->remember(
'latest_topics',
'latest_topics:by-group:'.auth()->user()->group_id,
$expiresAt,
fn () => Topic::with('forum', 'user', 'latestPoster')->latest()->take(5)->get()
fn () => Topic::query()
->with('user', 'user.group', 'latestPoster')
->whereRelation('forumPermissions', [['show_forum', '=', 1], ['group_id', '=', auth()->user()->group_id]])
->latest()
->take(5)
->get()
),
'posts' => cache()->remember(
'latest_posts',
'latest_posts:by-group:'.auth()->user()->group_id,
$expiresAt,
fn () => Post::with('topic', 'user')
fn () => Post::query()
->with('user', 'user.group', 'topic:id,name')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'cost')
->withExists([
'likes' => fn ($query) => $query->where('user_id', '=', auth()->id()),
'dislikes' => fn ($query) => $query->where('user_id', '=', auth()->id()),
])
->whereNotIn(
'topic_id',
Topic::query()
->whereRelation(
'forumPermissions',
fn ($query) => $query
->where('group_id', '=', auth()->user()->group_id)
->where(
fn ($query) => $query
->where('show_forum', '!=', 1)
->orWhere('read_topic', '!=', 1)
)
)
->select('id')
)
->latest()
->take(5)
->get()
Expand All @@ -431,13 +459,15 @@ function () use ($user) {
'poll' => cache()->remember('latest_poll', $expiresAt, fn () => Poll::latest()->first()),
'uploaders' => cache()->remember('top_uploaders', $expiresAt, fn () => Torrent::with(['user', 'user.group'])
->select(DB::raw('user_id, count(*) as value'))
->where('anon', '=', false)
->groupBy('user_id')
->latest('value')
->take(10)
->get()),
'past_uploaders' => cache()->remember('month_uploaders', $expiresAt, fn () => Torrent::with(['user', 'user.group'])
->where('created_at', '>', now()->subDays(30)->toDateTimeString())
->select(DB::raw('user_id, count(*) as value'))
->where('anon', '=', false)
->groupBy('user_id')
->latest('value')
->take(10)
Expand Down

0 comments on commit f0e6794

Please sign in to comment.