Advertisement
DaRealNG

Untitled

Nov 27th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. diff -u app/Http/Controllers/GetNzbController.php.BAK app/Http/Controllers/GetNzbController.php
  2. --- app/Http/Controllers/GetNzbController.php.BAK 2024-11-27 22:55:49.015108033 +0000
  3. +++ app/Http/Controllers/GetNzbController.php 2024-11-27 23:33:38.085508734 +0000
  4. @@ -16,117 +16,138 @@
  5. /**
  6. * @throws \Exception
  7. */
  8. - public function getNzb(Request $request)
  9. - {
  10. - $this->setPreferences();
  11. -
  12. - // Page is accessible only by the rss token, or logged in users.
  13. - if ($request->user()) {
  14. - $uid = $this->userdata->id;
  15. - $maxDownloads = $this->userdata->role->downloadrequests;
  16. - $rssToken = $this->userdata->api_token;
  17. - if ($this->userdata->hasRole('Disabled')) {
  18. - return Utility::showApiError(101);
  19. - }
  20. - } else {
  21. - if ($request->missing('r')) {
  22. - return Utility::showApiError(200);
  23. - }
  24. -
  25. - $res = User::getByRssToken($request->input('r'));
  26. - if (! $res) {
  27. - return Utility::showApiError(100);
  28. - }
  29.  
  30. - $uid = $res['id'];
  31. - $rssToken = $res['api_token'];
  32. - $maxDownloads = $res->role->downloadrequests;
  33. - if ($res->hasRole('Disabled')) {
  34. - return Utility::showApiError(101);
  35. - }
  36. +public function getNzb(Request $request)
  37. +{
  38. + $this->setPreferences();
  39. +
  40. + // User validation (logged in or RSS token)
  41. + if ($request->user()) {
  42. + $uid = $this->userdata->id;
  43. + $maxDownloads = $this->userdata->role->downloadrequests;
  44. + $rssToken = $this->userdata->api_token;
  45. + if ($this->userdata->hasRole('Disabled')) {
  46. + return Utility::showApiError(101);
  47. + }
  48. + } else {
  49. + if ($request->missing('r')) {
  50. + return Utility::showApiError(200);
  51. + }
  52. +
  53. + $res = User::getByRssToken($request->input('r'));
  54. + if (!$res) {
  55. + return Utility::showApiError(100);
  56. + }
  57. +
  58. + $uid = $res['id'];
  59. + $rssToken = $res['api_token'];
  60. + $maxDownloads = $res->role->downloadrequests;
  61. + if ($res->hasRole('Disabled')) {
  62. + return Utility::showApiError(101);
  63. }
  64. + }
  65.  
  66. - // Check download limit on user role.
  67. - $requests = UserDownload::getDownloadRequests($uid);
  68. - if ($requests > $maxDownloads) {
  69. - return Utility::showApiError(501);
  70. - }
  71. + // Check user download limits
  72. + $requests = UserDownload::getDownloadRequests($uid);
  73. + if ($requests >= $maxDownloads) {
  74. + return Utility::showApiError(501);
  75. + }
  76.  
  77. - if (! $request->input('id')) {
  78. - return Utility::showApiError(200, 'Parameter id is required');
  79. - }
  80. + // Validate release ID
  81. + $releaseId = $request->input('id');
  82. + if (!$releaseId) {
  83. + return Utility::showApiError(200, 'Parameter id is required');
  84. + }
  85.  
  86. - // Remove any suffixed id with .nzb which is added to help weblogging programs see nzb traffic.
  87. - $request->merge(['id' => str_ireplace('.nzb', '', $request->input('id'))]);
  88. + // Clean up the release ID (remove ".nzb" suffix)
  89. + $releaseId = str_ireplace('.nzb', '', $releaseId);
  90.  
  91. - // User requested a zip of guid,guid,guid releases.
  92. - if ($request->has('zip') && $request->input('zip') === '1') {
  93. - $guids = explode(',', $request->input('id'));
  94. - if ($requests + \count($guids) > $maxDownloads) {
  95. - return Utility::showApiError(501);
  96. - }
  97. + // Handle ZIP downloads
  98. + if ($request->has('zip') && $request->input('zip') === '1') {
  99. + $guids = explode(',', $releaseId);
  100. + if ($requests + count($guids) > $maxDownloads) {
  101. + return Utility::showApiError(501);
  102. + }
  103.  
  104. - $zip = getStreamingZip($guids);
  105. - if ($zip !== '') {
  106. - User::incrementGrabs($uid, \count($guids));
  107. - foreach ($guids as $guid) {
  108. - Release::updateGrab($guid);
  109. - UserDownload::addDownloadRequest($uid, $guid);
  110. -
  111. - if ($request->has('del') && (int) $request->input('del') === 1) {
  112. - UsersRelease::delCartByUserAndRelease($guid, $uid);
  113. - }
  114. - }
  115. + $zip = getStreamingZip($guids);
  116. + if ($zip !== '') {
  117. + User::incrementGrabs($uid, count($guids));
  118. + foreach ($guids as $guid) {
  119. + Release::updateGrab($guid);
  120. + UserDownload::addDownloadRequest($uid, $guid);
  121.  
  122. - return $zip;
  123. + if ($request->has('del') && (int)$request->input('del') === 1) {
  124. + UsersRelease::delCartByUserAndRelease($guid, $uid);
  125. + }
  126. }
  127.  
  128. - return response()->json(['message' => 'Unable to create .zip file'], 404);
  129. + return $zip;
  130. }
  131.  
  132. - $nzbPath = (new NZB)->getNZBPath($request->input('id'));
  133. + return response()->json(['message' => 'Unable to create .zip file'], 404);
  134. + }
  135.  
  136. - if (! File::exists($nzbPath)) {
  137. - return Utility::showApiError(300, 'NZB file not found!');
  138. - }
  139. + // Get NZB file path
  140. + $nzbPath = (new NZB())->getNZBPath($releaseId);
  141. + if (!$nzbPath || !File::exists($nzbPath)) {
  142. + return Utility::showApiError(300, 'NZB file not found!');
  143. + }
  144.  
  145. - $relData = Release::getByGuid($request->input('id'));
  146. - if ($relData !== null) {
  147. - Release::updateGrab($request->input('id'));
  148. - UserDownload::addDownloadRequest($uid, $relData['id']);
  149. - User::incrementGrabs($uid);
  150. - if ($request->has('del') && (int) $request->input('del') === 1) {
  151. - UsersRelease::delCartByUserAndRelease($request->input('id'), $uid);
  152. - }
  153. - } else {
  154. - return Utility::showApiError(300, 'Release not found!');
  155. - }
  156. + // Fetch release details
  157. + $relData = Release::getByGuid($releaseId);
  158. + if (!$relData) {
  159. + return Utility::showApiError(300, 'Release not found!');
  160. + }
  161.  
  162. - $headers = [
  163. - 'Content-Type' => 'application/x-nzb',
  164. - 'Expires' => date('r', now()->addDays(365)->timestamp),
  165. - 'X-DNZB-Failure' => url('/').'/failed'.'?guid='.$request->input('id').'&userid='.$uid.'&api_token='.$rssToken,
  166. - 'X-DNZB-Category' => $relData['category_name'],
  167. - 'X-DNZB-Details' => url('/').'/details/'.$request->input('id'),
  168. - ];
  169. + // Update release and user activity
  170. + Release::updateGrab($releaseId);
  171. + UserDownload::addDownloadRequest($uid, $relData['id']);
  172. + User::incrementGrabs($uid);
  173.  
  174. - if (! empty($relData['imdbid']) && $relData['imdbid'] > 0) {
  175. - $headers += ['X-DNZB-MoreInfo' => 'http://www.imdb.com/title/tt'.$relData['imdbid']];
  176. - } elseif (! empty($relData['tvdb']) && $relData['tvdb'] > 0) {
  177. - $headers += ['X-DNZB-MoreInfo' => 'http://www.thetvdb.com/?tab=series&id='.$relData['tvdb']];
  178. - }
  179. + if ($request->has('del') && (int)$request->input('del') === 1) {
  180. + UsersRelease::delCartByUserAndRelease($releaseId, $uid);
  181. + }
  182.  
  183. - if ((int) $relData['nfostatus'] === 1) {
  184. - $headers += ['X-DNZB-NFO' => url('/').'/nfo/'.$request->input('id')];
  185. + // Generate a clean filename
  186. + $cleanName = str_replace([',', ' ', '/', '\\'], '_', $relData['searchname']);
  187. +
  188. + // Add metadata headers
  189. + $headers = [
  190. + 'Content-Type' => 'application/x-nzb',
  191. + 'Expires' => date('r', now()->addDays(365)->timestamp),
  192. + 'X-DNZB-Failure' => url('/') . '/failed?guid=' . $releaseId . '&userid=' . $uid . '&api_token=' . $rssToken,
  193. + 'X-DNZB-Category' => $relData['category_name'],
  194. + 'X-DNZB-Details' => url('/') . '/details/' . $releaseId,
  195. + ];
  196. +
  197. + if (!empty($relData['imdbid']) && $relData['imdbid'] > 0) {
  198. + $headers['X-DNZB-MoreInfo'] = 'http://www.imdb.com/title/tt' . $relData['imdbid'];
  199. + } elseif (!empty($relData['tvdb']) && $relData['tvdb'] > 0) {
  200. + $headers['X-DNZB-MoreInfo'] = 'http://www.thetvdb.com/?tab=series&id=' . $relData['tvdb'];
  201. + }
  202. +
  203. + if ((int)$relData['nfostatus'] === 1) {
  204. + $headers['X-DNZB-NFO'] = url('/') . '/nfo/' . $releaseId;
  205. + }
  206. +
  207. + // Stream the decompressed NZB file
  208. + return response()->streamDownload(function () use ($nzbPath) {
  209. + $tempFile = sys_get_temp_dir() . '/' . uniqid('nzb_', true) . '.nzb';
  210. + $gzFile = gzopen($nzbPath, 'rb');
  211. + if (!$gzFile) {
  212. + throw new \Exception('Failed to open compressed NZB file: ' . $nzbPath);
  213. }
  214.  
  215. - $headers += ['X-DNZB-RCode' => '200',
  216. - 'X-DNZB-RText' => 'OK, NZB content follows.', ];
  217. + $outFile = fopen($tempFile, 'wb');
  218. + while (!gzeof($gzFile)) {
  219. + fwrite($outFile, gzread($gzFile, 4096));
  220. + }
  221.  
  222. - $cleanName = str_replace([',', ' ', '/', '\\'], '_', $relData['searchname']);
  223. + gzclose($gzFile);
  224. + fclose($outFile);
  225.  
  226. - return response()->streamDownload(function () use ($nzbPath) {
  227. - echo $nzbPath;
  228. - }, $cleanName.'.nzb', $headers);
  229. - }
  230. + readfile($tempFile);
  231. + unlink($tempFile);
  232. + }, $cleanName . '.nzb', $headers);
  233. +}
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement