@@ -755,7 +755,7 @@ function countryCode(string $country): string
755755 function unzipGzipFile (string $ filePath ): false |string
756756 {
757757 $ string = '' ;
758- $ gzFile = @gzopen ($ filePath , 'rb ' , false );
758+ $ gzFile = @gzopen ($ filePath , 'rb ' , 0 );
759759 if ($ gzFile ) {
760760 while (! gzeof ($ gzFile )) {
761761 $ temp = gzread ($ gzFile , 1024 );
@@ -914,42 +914,68 @@ function imdb_trailers(mixed $imdbID): string
914914 }
915915}
916916
917+ if (! function_exists ('apiErrorDetails ' )) {
918+ /**
919+ * @return array{code:int,message:string,status:int,header:string}
920+ */
921+ function apiErrorDetails (int $ errorCode = 900 , string $ errorText = '' ): array
922+ {
923+ [$ defaultText , $ status , $ errorHeader ] = match ($ errorCode ) {
924+ 100 => ['Incorrect user credentials ' , 401 , 'HTTP/1.1 401 Unauthorized ' ],
925+ 101 => ['Account suspended ' , 403 , 'HTTP/1.1 403 Forbidden ' ],
926+ 102 => ['Insufficient privileges/not authorized ' , 401 , 'HTTP/1.1 401 Unauthorized ' ],
927+ 103 => ['Registration denied ' , 403 , 'HTTP/1.1 403 Forbidden ' ],
928+ 104 => ['Registrations are closed ' , 403 , 'HTTP/1.1 403 Forbidden ' ],
929+ 105 => ['Invalid registration (Email Address Taken) ' , 403 , 'HTTP/1.1 403 Forbidden ' ],
930+ 106 => ['Invalid registration (Email Address Bad Format) ' , 403 , 'HTTP/1.1 403 Forbidden ' ],
931+ 107 => ['Registration Failed (Data error) ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
932+ 200 => ['Missing parameter ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
933+ 201 => ['Incorrect parameter ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
934+ 202 => ['No such function ' , 404 , 'HTTP/1.1 404 Not Found ' ],
935+ 203 => ['Function not available ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
936+ 300 => ['No such item ' , 404 , 'HTTP/1.1 404 Not Found ' ],
937+ 310 => ['Item already exists ' , 409 , 'HTTP/1.1 409 Conflict ' ],
938+ 500 => ['Request limit reached ' , 429 , 'HTTP/1.1 429 Too Many Requests ' ],
939+ 501 => ['Download limit reached ' , 429 , 'HTTP/1.1 429 Too Many Requests ' ],
940+ 600 => ['Failed to load NZB ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
941+ 601 => ['NZB is duplicate ' , 409 , 'HTTP/1.1 409 Conflict ' ],
942+ 602 => ['NZB is for a non-existent group ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
943+ 603 => ['NZB failed to write to disk ' , 500 , 'HTTP/1.1 500 Internal Server Error ' ],
944+ 910 => ['API disabled ' , 401 , 'HTTP/1.1 401 Unauthorized ' ],
945+ default => ['Unknown error ' , 400 , 'HTTP/1.1 400 Bad Request ' ],
946+ };
947+
948+ return [
949+ 'code ' => $ errorCode ,
950+ 'message ' => $ errorText !== '' ? $ errorText : $ defaultText ,
951+ 'status ' => $ status ,
952+ 'header ' => $ errorHeader ,
953+ ];
954+ }
955+ }
956+
957+ if (! function_exists ('apiJsonError ' )) {
958+ function apiJsonError (int $ errorCode = 900 , string $ errorText = '' ): mixed
959+ {
960+ $ error = apiErrorDetails ($ errorCode , $ errorText );
961+
962+ return response ()
963+ ->json (['error ' => $ error ['message ' ]], $ error ['status ' ])
964+ ->header ('X-NNTmux ' , 'API ERROR [ ' .$ error ['code ' ].'] ' .$ error ['message ' ]);
965+ }
966+ }
967+
917968if (! function_exists ('showApiError ' )) {
918969 function showApiError (int $ errorCode = 900 , string $ errorText = '' ): mixed
919970 {
920- $ errorHeader = 'HTTP 1.1 400 Bad Request ' ;
921- if ($ errorText === '' ) {
922- [$ errorText , $ errorHeader ] = match ($ errorCode ) {
923- 100 => ['Incorrect user credentials ' , 'HTTP 1.1 401 Unauthorized ' ],
924- 101 => ['Account suspended ' , 'HTTP 1.1 403 Forbidden ' ],
925- 102 => ['Insufficient privileges/not authorized ' , 'HTTP 1.1 401 Unauthorized ' ],
926- 103 => ['Registration denied ' , 'HTTP 1.1 403 Forbidden ' ],
927- 104 => ['Registrations are closed ' , 'HTTP 1.1 403 Forbidden ' ],
928- 105 => ['Invalid registration (Email Address Taken) ' , 'HTTP 1.1 403 Forbidden ' ],
929- 106 => ['Invalid registration (Email Address Bad Format) ' , 'HTTP 1.1 403 Forbidden ' ],
930- 107 => ['Registration Failed (Data error) ' , 'HTTP 1.1 400 Bad Request ' ],
931- 200 => ['Missing parameter ' , 'HTTP 1.1 400 Bad Request ' ],
932- 201 => ['Incorrect parameter ' , 'HTTP 1.1 400 Bad Request ' ],
933- 202 => ['No such function ' , 'HTTP 1.1 404 Not Found ' ],
934- 203 => ['Function not available ' , 'HTTP 1.1 400 Bad Request ' ],
935- 300 => ['No such item ' , 'HTTP 1.1 404 Not Found ' ],
936- 310 => ['Item already exists ' , 'HTTP 1.1 409 Conflict ' ],
937- 500 => ['Request limit reached ' , 'HTTP 1.1 429 Too Many Requests ' ],
938- 501 => ['Download limit reached ' , 'HTTP 1.1 429 Too Many Requests ' ],
939- 600 => ['Failed to load NZB ' , 'HTTP 1.1 400 Bad Request ' ],
940- 601 => ['NZB is duplicate ' , 'HTTP 1.1 409 Conflict ' ],
941- 602 => ['NZB is for a non-existent group ' , 'HTTP 1.1 400 Bad Request ' ],
942- 603 => ['NZB failed to write to disk ' , 'HTTP 1.1 500 Internal Server Error ' ],
943- 910 => ['API disabled ' , 'HTTP 1.1 401 Unauthorized ' ],
944- default => ['Unknown error ' , 'HTTP 1.1 400 Bad Request ' ],
945- };
946- }
971+ $ error = apiErrorDetails ($ errorCode , $ errorText );
972+ $ errorText = $ error ['message ' ];
947973
948974 $ response =
949975 "<?xml version= \"1.0 \" encoding= \"UTF-8 \"?> \n" .
950976 '<error code=" ' .$ errorCode .'" description=" ' .$ errorText ."\"/> \n" ;
951977
952- return response ($ response )->header ('Content-type ' , 'text/xml ' )->header ('Content-Length ' , (string ) strlen ($ response ))->header ('X-NNTmux ' , 'API ERROR [ ' .$ errorCode .'] ' .$ errorText )->header ('HTTP/1.1 ' , $ errorHeader );
978+ return response ($ response, $ error [ ' status ' ] )->header ('Content-type ' , 'text/xml ' )->header ('Content-Length ' , (string ) strlen ($ response ))->header ('X-NNTmux ' , 'API ERROR [ ' .$ errorCode .'] ' .$ errorText )->header ('HTTP/1.1 ' , $ error [ ' header ' ] );
953979 }
954980}
955981
0 commit comments