| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- trait ProwlarrHomepageItem
- {
- public function prowlarrSettingsArray($infoOnly = false)
- {
- $homepageInformation = [
- 'name' => 'Prowlarr',
- 'enabled' => true,
- 'image' => 'plugins/images/tabs/prowlarr.png',
- 'category' => 'Utility',
- 'settingsArray' => __FUNCTION__
- ];
- if ($infoOnly) {
- return $homepageInformation;
- }
- $homepageSettings = [
- 'debug' => true,
- 'settings' => [
- 'Enable' => [
- $this->settingsOption('enable', 'homepageProwlarrEnabled'),
- $this->settingsOption('auth', 'homepageProwlarrAuth'),
- ],
- 'Connection' => [
- $this->settingsOption('url', 'prowlarrURL'),
- $this->settingsOption('token', 'prowlarrToken'),
- $this->settingsOption('disable-cert-check', 'prowlarrDisableCertCheck'),
- $this->settingsOption('use-custom-certificate', 'prowlarrUseCustomCertificate'),
- ],
- 'Options' => [
- $this->settingsOption('switch', 'homepageProwlarrBackholeDownload', ['label' => 'Prefer black hole download', 'help' => 'Prefer black hole download link instead of direct/magnet download']),
- ],
- 'Test Connection' => [
- $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
- $this->settingsOption('test', 'prowlarr'),
- ]
- ]
- ];
- return array_merge($homepageInformation, $homepageSettings);
- }
- public function prowlarrHomepagePermissions($key = null)
- {
- $permissions = [
- 'main' => [
- 'enabled' => [
- 'homepageProwlarrEnabled'
- ],
- 'auth' => [
- 'homepageProwlarrAuth'
- ],
- 'not_empty' => [
- 'prowlarrURL',
- 'prowlarrToken'
- ]
- ],
- 'test' => [
- 'auth' => [
- 'homepageProwlarrAuth'
- ],
- 'not_empty' => [
- 'prowlarrURL',
- 'prowlarrToken'
- ]
- ]
- ];
- return $this->homepageCheckKeyPermissions($key, $permissions);
- }
- public function homepageOrderProwlarr()
- {
- if ($this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'))) {
- return '
- <div id="' . __FUNCTION__ . '">
- <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Prowlarr...</h2></div>
- <script>
- // Prowlarr
- homepageProwlarr();
- // End Prowlarr
- </script>
- </div>
- ';
- }
- }
- public function testConnectionProwlarr()
- {
- if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('test'), true)) {
- return false;
- }
- $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
- $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'] . '&query=this-is-just-a-test-for-organizr';
- try {
- $headers = [];
- $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
- $response = Requests::get($endpoint, $headers, $options);
- if ($response->success) {
- $apiData = json_decode($response->body, true);
- $api['content'] = $apiData;
- unset($apiData);
- } else {
- $this->setResponse(403, 'Error connecting to Prowlarr');
- return false;
- }
- } catch (Requests_Exception $e) {
- $this->setResponse(500, $e->getMessage());
- return false;
- };
- $api['content'] = $api['content'] ?? false;
- $this->setResponse(200, null, $api);
- return $api;
- }
- public function searchProwlarrIndexers($query = null)
- {
- if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'), true)) {
- return false;
- }
- if (!$query) {
- $this->setAPIResponse('error', 'Query was not supplied', 422);
- return false;
- }
- $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
- $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'] . '&query=' . urlencode($query);
- try {
- $headers = [];
- $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
- $response = Requests::get($endpoint, $headers, $options);
- if ($response->success) {
- $apiData = json_decode($response->body, true);
- $api['content'] = $apiData;
- unset($apiData);
- }
- } catch (Requests_Exception $e) {
- $this->setResponse(500, $e->getMessage());
- return false;
- };
- $api['content'] = isset($api['content']) ? $api['content'] : false;
- $this->setAPIResponse('success', null, 200, $api);
- return $api;
- }
- public function performProwlarrBackHoleDownload($guid = null, $indexerId = null)
- {
- if (!$this->homepageItemPermissions($this->prowlarrHomepagePermissions('main'), true)) {
- return false;
- }
- if (!$guid) {
- $this->setAPIResponse('error', 'guid was not supplied', 422);
- return false;
- }
- if (!$indexerId) {
- $this->setAPIResponse('error', 'indexerId was not supplied', 422);
- return false;
- }
- $apiURL = $this->qualifyURL($this->config['prowlarrURL']);
- $endpoint = $apiURL . '/api/v1/search?apikey=' . $this->config['prowlarrToken'];
- try {
- $headers = [];
- $data = ['guid'=>$guid,'indexerId'=>$indexerId];
- $options = $this->requestOptions($apiURL, 120, $this->config['prowlarrDisableCertCheck'], $this->config['prowlarrUseCustomCertificate']);
- $ch = curl_init($endpoint);
- $payload = json_encode($data);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
- if ($response) {
- $api['content'] = $response;
- }
- } catch (Requests_Exception $e) {
- $this->setLoggerChannel('Prowlarr')->error($e);
- $this->setResponse(500, $e->getMessage());
- return false;
- };
- $api['content'] = isset($api['content']) ? $api['content'] : false;
- if ($api['content']) {
- $this->setAPIResponse('success', null, 200, $api);
- } else {
- $this->setAPIResponse('error', 'Unknown error', 400, $api);
- }
- return $api;
- }
- }
|