prowlarr.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. trait ProwlarrHomepageItem
  3. {
  4. public function prowlarrSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Prowlarr',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/prowlarr.png',
  10. 'category' => 'Utility',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageProwlarrEnabled'),
  21. $this->settingsOption('auth', 'homepageProwlarrAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'prowlarrURL'),
  25. $this->settingsOption('token', 'prowlarrToken'),
  26. $this->settingsOption('disable-cert-check', 'prowlarrDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'prowlarrUseCustomCertificate'),
  28. ],
  29. 'Options' => [
  30. $this->settingsOption('switch', 'homepageProwlarrBackholeDownload', ['label' => 'Prefer black hole download', 'help' => 'Prefer black hole download link instead of direct/magnet download']),
  31. ],
  32. 'Test Connection' => [
  33. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  34. $this->settingsOption('test', 'prowlarr'),
  35. ]
  36. ]
  37. ];
  38. return array_merge($homepageInformation, $homepageSettings);
  39. }
  40. public function prowlarrHomepagePermissions($key = null)
  41. {
  42. $permissions = [
  43. 'main' => [
  44. 'enabled' => [
  45. 'homepageProwlarrEnabled'
  46. ],
  47. 'auth' => [
  48. 'homepageProwlarrAuth'
  49. ],
  50. 'not_empty' => [
  51. 'prowlarrURL',
  52. 'prowlarrToken'
  53. ]
  54. ],
  55. 'test' => [
  56. 'auth' => [
  57. 'homepageProwlarrAuth'
  58. ],
  59. 'not_empty' => [
  60. 'prowlarrURL',
  61. 'prowlarrToken'
  62. ]
  63. ]
  64. ];
  65. return $this->homepageCheckKeyPermissions($key, $permissions);
  66. }
  67. public function homepageOrderProwlarr()
  68. {
  69. if ($this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'))) {
  70. return '
  71. <div id="' . __FUNCTION__ . '">
  72. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Prowlarr...</h2></div>
  73. <script>
  74. // Prowlarr
  75. homepageProwlarr();
  76. // End Prowlarr
  77. </script>
  78. </div>
  79. ';
  80. }
  81. }
  82. public function testConnectionProwlarr()
  83. {
  84. if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('test'), true)) {
  85. return false;
  86. }
  87. $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
  88. $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'] . '&query=this-is-just-a-test-for-organizr';
  89. try {
  90. $headers = [];
  91. $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
  92. $response = Requests::get($endpoint, $headers, $options);
  93. if ($response->success) {
  94. $apiData = json_decode($response->body, true);
  95. $api['content'] = $apiData;
  96. unset($apiData);
  97. } else {
  98. $this->setResponse(403, 'Error connecting to Prowlarr');
  99. return false;
  100. }
  101. } catch (Requests_Exception $e) {
  102. $this->setResponse(500, $e->getMessage());
  103. return false;
  104. };
  105. $api['content'] = $api['content'] ?? false;
  106. $this->setResponse(200, null, $api);
  107. return $api;
  108. }
  109. public function searchProwlarrIndexers($query = null)
  110. {
  111. if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'), true)) {
  112. return false;
  113. }
  114. if (!$query) {
  115. $this->setAPIResponse('error', 'Query was not supplied', 422);
  116. return false;
  117. }
  118. $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
  119. $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'] . '&query=' . urlencode($query);
  120. try {
  121. $headers = [];
  122. $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
  123. $response = Requests::get($endpoint, $headers, $options);
  124. if ($response->success) {
  125. $apiData = json_decode($response->body, true);
  126. $api['content'] = $apiData;
  127. unset($apiData);
  128. }
  129. } catch (Requests_Exception $e) {
  130. $this->setResponse(500, $e->getMessage());
  131. return false;
  132. };
  133. $api['content'] = isset($api['content']) ? $api['content'] : false;
  134. $this->setAPIResponse('success', null, 200, $api);
  135. return $api;
  136. }
  137. public function performProwlarrBackHoleDownload($guid = null, $indexerId = null)
  138. {
  139. if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'), true)) {
  140. return false;
  141. }
  142. if (!$guid) {
  143. $this->setAPIResponse('error', 'guid was not supplied', 422);
  144. return false;
  145. }
  146. if (!$indexerId) {
  147. $this->setAPIResponse('error', 'indexerId was not supplied', 422);
  148. return false;
  149. }
  150. $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
  151. $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'];
  152. try {
  153. $headers = [];
  154. $data = ['guid'=>$guid,'indexerId'=>$indexerId];
  155. $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
  156. $ch = curl_init($endpoint);
  157. $payload = json_encode($data);
  158. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  159. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  160. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  161. $response = curl_exec($ch);
  162. curl_close($ch);
  163. if ($response) {
  164. $api['content'] = $response;
  165. }
  166. } catch (Requests_Exception $e) {
  167. $this->setLoggerChannel('Prowlarr')->error($e);
  168. $this->setResponse(500, $e->getMessage());
  169. return false;
  170. };
  171. $api['content'] = isset($api['content']) ? $api['content'] : false;
  172. if ($api['content']) {
  173. $this->setAPIResponse('success', null, 200, $api);
  174. } else {
  175. $this->setAPIResponse('error', 'Unknown error', 400, $api);
  176. }
  177. return $api;
  178. }
  179. }