From 4adc19c1784141faa5329b416db39e50c2b28e53 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 31 Mar 2023 16:57:41 +0200 Subject: [PATCH] Fix issues in API controllers --- Blacklight/utility/Utility.php | 9 +--- app/Http/Controllers/Api/ApiController.php | 57 ++++++++++---------- app/Http/Controllers/Api/ApiV2Controller.php | 56 ++++++++++--------- app/Support/Google2FAAuthenticator.php | 4 +- 4 files changed, 64 insertions(+), 62 deletions(-) diff --git a/Blacklight/utility/Utility.php b/Blacklight/utility/Utility.php index 8bf8106ee..bd131f728 100755 --- a/Blacklight/utility/Utility.php +++ b/Blacklight/utility/Utility.php @@ -164,7 +164,7 @@ class Utility return ''; } - public static function showApiError(int $errorCode = 900, string $errorText = ''): void + public static function showApiError(int $errorCode = 900, string $errorText = '') { $errorHeader = 'HTTP 1.1 400 Bad Request'; if ($errorText === '') { @@ -243,12 +243,7 @@ class Utility $response = "\n". '\n"; - header('Content-type: text/xml'); - header('Content-Length: '.\strlen($response)); - header('X-NNTmux: API ERROR ['.$errorCode.'] '.$errorText); - header($errorHeader); - - exit($response); + return response($response)->header('Content-type', 'text/xml')->header('Content-Length', strlen($response))->header('X-NNTmux', 'API ERROR ['.$errorCode.'] '.$errorText)->header('HTTP/1.1', $errorHeader); } public static function getRange($tableName): LengthAwarePaginator diff --git a/app/Http/Controllers/Api/ApiController.php b/app/Http/Controllers/Api/ApiController.php index b1a0ad7c6..a3a714a46 100644 --- a/app/Http/Controllers/Api/ApiController.php +++ b/app/Http/Controllers/Api/ApiController.php @@ -14,6 +14,7 @@ use App\Models\UserDownload; use App\Models\UserRequest; use Blacklight\Releases; use Blacklight\utility\Utility; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Routing\Redirector; @@ -26,9 +27,11 @@ class ApiController extends BasePageController private string $type; /** + * @param Request $request + * @return Application|\Illuminate\Foundation\Application|RedirectResponse|Redirector|StreamedResponse|void * @throws \Throwable */ - public function api(Request $request): StreamedResponse|Redirector|RedirectResponse + public function api(Request $request) { // API functions. $function = 's'; @@ -134,12 +137,12 @@ class ApiController extends BasePageController switch ($function) { // Search releases. case 's': - $this->verifyEmptyParameter('q'); - $maxAge = $this->maxAge(); - $groupName = $this->group(); + $this->verifyEmptyParameter($request, 'q'); + $maxAge = $this->maxAge($request); + $groupName = $this->group($request); UserRequest::addApiRequest($apiKey, $request->getRequestUri()); - $categoryID = $this->categoryID(); - $limit = $this->limit(); + $categoryID = $this->categoryID($request); + $limit = $this->limit($request); $searchArr = [ 'searchname' => $request->input('q') ?? -1, 'name' => -1, @@ -181,17 +184,17 @@ class ApiController extends BasePageController break; // Search tv releases. case 'tv': - $this->verifyEmptyParameter('q'); - $this->verifyEmptyParameter('vid'); - $this->verifyEmptyParameter('tvdbid'); - $this->verifyEmptyParameter('traktid'); - $this->verifyEmptyParameter('rid'); - $this->verifyEmptyParameter('tvmazeid'); - $this->verifyEmptyParameter('imdbid'); - $this->verifyEmptyParameter('tmdbid'); - $this->verifyEmptyParameter('season'); - $this->verifyEmptyParameter('ep'); - $maxAge = $this->maxAge(); + $this->verifyEmptyParameter($request, 'q'); + $this->verifyEmptyParameter($request, 'vid'); + $this->verifyEmptyParameter($request,'tvdbid'); + $this->verifyEmptyParameter($request,'traktid'); + $this->verifyEmptyParameter($request,'rid'); + $this->verifyEmptyParameter($request,'tvmazeid'); + $this->verifyEmptyParameter($request,'imdbid'); + $this->verifyEmptyParameter($request,'tmdbid'); + $this->verifyEmptyParameter($request,'season'); + $this->verifyEmptyParameter($request,'ep'); + $maxAge = $this->maxAge($request); UserRequest::addApiRequest($apiKey, $request->getRequestUri()); $siteIdArr = [ @@ -219,9 +222,9 @@ class ApiController extends BasePageController $episode, $airDate ?? '', $this->offset($request), - $this->limit(), + $this->limit($request), $request->input('q') ?? '', - $this->categoryID(), + $this->categoryID($request), $maxAge, $minSize, $catExclusions @@ -232,9 +235,9 @@ class ApiController extends BasePageController // Search movie releases. case 'm': - $this->verifyEmptyParameter('q'); - $this->verifyEmptyParameter('imdbid'); - $maxAge = $this->maxAge(); + $this->verifyEmptyParameter($request,'q'); + $this->verifyEmptyParameter($request,'imdbid'); + $maxAge = $this->maxAge($request); UserRequest::addApiRequest($apiKey, $request->getRequestUri()); $imdbId = $request->has('imdbid') && $request->filled('imdbid') ? (int) $request->input('imdbid') : -1; @@ -246,9 +249,9 @@ class ApiController extends BasePageController $tmdbId, $traktId, $this->offset($request), - $this->limit(), + $this->limit($request), $request->input('q') ?? '', - $this->categoryID(), + $this->categoryID($request), $maxAge, $minSize, $catExclusions @@ -266,7 +269,7 @@ class ApiController extends BasePageController // Get NZB. case 'g': - $this->verifyEmptyParameter('g'); + $this->verifyEmptyParameter($request,'g'); UserRequest::addApiRequest($apiKey, $request->getRequestUri()); $relData = Release::checkGuidForApi($request->input('id')); if ($relData) { @@ -298,8 +301,8 @@ class ApiController extends BasePageController $rel = Release::query()->where('guid', $request->input('id'))->first(['id', 'searchname']); $data = ReleaseNfo::getReleaseNfo($rel['id']); - if ($rel !== null) { - if ($data !== null) { + if ($rel->isNotEmpty()) { + if ($data->isNotEmpty()) { if ($request->has('o') && $request->input('o') === 'file') { return response()->streamDownload(function () use ($data) { echo $data['nfo']; diff --git a/app/Http/Controllers/Api/ApiV2Controller.php b/app/Http/Controllers/Api/ApiV2Controller.php index 0a8551b1b..d1ec1f5e5 100644 --- a/app/Http/Controllers/Api/ApiV2Controller.php +++ b/app/Http/Controllers/Api/ApiV2Controller.php @@ -22,6 +22,13 @@ use Illuminate\Support\Carbon; class ApiV2Controller extends BasePageController { + private ApiController $api; + + public function __construct() + { + $this->api = new ApiController(); + } + public function capabilities(): JsonResponse { $category = Category::getForApi(); @@ -58,11 +65,10 @@ class ApiV2Controller extends BasePageController */ public function movie(Request $request): JsonResponse { - $api = new API(); $releases = new Releases(); $user = User::query()->where('api_token', $request->input('api_token'))->first(); $minSize = $request->has('minsize') && $request->input('minsize') > 0 ? $request->input('minsize') : 0; - $maxAge = $api->maxAge(); + $maxAge = $this->api->maxAge($request); $catExclusions = User::getCategoryExclusionForApi($request); UserRequest::addApiRequest($request->input('api_token'), $request->getRequestUri()); event(new UserAccessedApi($user)); @@ -75,10 +81,10 @@ class ApiV2Controller extends BasePageController $imdbId, $tmdbId, $traktId, - $api->offset(), - $api->limit(), + $this->api->offset($request), + $this->api->limit($request), $request->input('id') ?? '', - $api->categoryID(), + $this->api->categoryID($request), $maxAge, $minSize, $catExclusions @@ -109,18 +115,17 @@ class ApiV2Controller extends BasePageController */ public function apiSearch(Request $request): JsonResponse { - $api = new API(); $releases = new Releases(); $user = User::query()->where('api_token', $request->input('api_token'))->first(); - $offset = $api->offset(); + $offset = $this->api->offset($request); $catExclusions = User::getCategoryExclusionForApi($request); $minSize = $request->has('minsize') && $request->input('minsize') > 0 ? $request->input('minsize') : 0; - $maxAge = $api->maxAge(); - $groupName = $api->group(); + $maxAge = $this->api->maxAge($request); + $groupName = $this->api->group($request); UserRequest::addApiRequest($request->input('api_token'), $request->getRequestUri()); event(new UserAccessedApi($user)); - $categoryID = $api->categoryID(); - $limit = $api->limit(); + $categoryID = $this->api->categoryID($request); + $limit = $this->api->limit($request); if ($request->has('id')) { $relData = $releases->apiSearch( @@ -172,22 +177,21 @@ class ApiV2Controller extends BasePageController */ public function tv(Request $request): JsonResponse { - $api = new API(); $releases = new Releases(); $user = User::query()->where('api_token', $request->input('api_token'))->first(); $catExclusions = User::getCategoryExclusionForApi($request); $minSize = $request->has('minsize') && $request->input('minsize') > 0 ? $request->input('minsize') : 0; - $api->verifyEmptyParameter('id'); - $api->verifyEmptyParameter('vid'); - $api->verifyEmptyParameter('tvdbid'); - $api->verifyEmptyParameter('traktid'); - $api->verifyEmptyParameter('rid'); - $api->verifyEmptyParameter('tvmazeid'); - $api->verifyEmptyParameter('imdbid'); - $api->verifyEmptyParameter('tmdbid'); - $api->verifyEmptyParameter('season'); - $api->verifyEmptyParameter('ep'); - $maxAge = $api->maxAge(); + $this->api->verifyEmptyParameter($request,'id'); + $this->api->verifyEmptyParameter($request,'vid'); + $this->api->verifyEmptyParameter($request,'tvdbid'); + $this->api->verifyEmptyParameter($request,'traktid'); + $this->api->verifyEmptyParameter($request,'rid'); + $this->api->verifyEmptyParameter($request,'tvmazeid'); + $this->api->verifyEmptyParameter($request,'imdbid'); + $this->api->verifyEmptyParameter($request,'tmdbid'); + $this->api->verifyEmptyParameter($request,'season'); + $this->api->verifyEmptyParameter($request,'ep'); + $maxAge = $this->api->maxAge($request); UserRequest::addApiRequest($request->input('api_token'), $request->getRequestUri()); event(new UserAccessedApi($user)); @@ -215,10 +219,10 @@ class ApiV2Controller extends BasePageController $series, $episode, $airDate ?? '', - $api->offset(), - $api->limit(), + $this->api->offset($request), + $this->api->limit($request), $request->input('id') ?? '', - $api->categoryID(), + $this->api->categoryID($request), $maxAge, $minSize, $catExclusions diff --git a/app/Support/Google2FAAuthenticator.php b/app/Support/Google2FAAuthenticator.php index c72876050..425dd571a 100644 --- a/app/Support/Google2FAAuthenticator.php +++ b/app/Support/Google2FAAuthenticator.php @@ -23,13 +23,13 @@ class Google2FAAuthenticator extends Authenticator /** * @return mixed * - * @throws \PragmaRX\Google2FALaravel\Exceptions\InvalidSecretKey + * @throws InvalidSecretKey */ protected function getGoogle2FASecretKey() { $secret = $this->getUser()->passwordSecurity->{$this->config('otp_secret_column')}; - if (is_null($secret) || empty($secret)) { + if (empty($secret)) { throw new InvalidSecretKey('Secret key cannot be empty.'); }