sabnzbd.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. trait SabNZBdHomepageItem
  3. {
  4. public function sabNZBdSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'SabNZBD',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sabnzbd.png',
  10. 'category' => 'Downloader',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageSabnzbdEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageSabnzbdEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageSabnzbdAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageSabnzbdAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'sabnzbdURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['sabnzbdURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'sabnzbdToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['sabnzbdToken']
  41. )
  42. ),
  43. 'Misc Options' => array(
  44. array(
  45. 'type' => 'select',
  46. 'name' => 'homepageDownloadRefresh',
  47. 'label' => 'Refresh Seconds',
  48. 'value' => $this->config['homepageDownloadRefresh'],
  49. 'options' => $this->timeOptions()
  50. ),
  51. array(
  52. 'type' => 'switch',
  53. 'name' => 'sabnzbdCombine',
  54. 'label' => 'Add to Combined Downloader',
  55. 'value' => $this->config['sabnzbdCombine']
  56. ),
  57. ),
  58. 'Test Connection' => array(
  59. array(
  60. 'type' => 'blank',
  61. 'label' => 'Please Save before Testing'
  62. ),
  63. array(
  64. 'type' => 'button',
  65. 'label' => '',
  66. 'icon' => 'fa fa-flask',
  67. 'class' => 'pull-right',
  68. 'text' => 'Test Connection',
  69. 'attr' => 'onclick="testAPIConnection(\'sabnzbd\')"'
  70. ),
  71. )
  72. )
  73. );
  74. }
  75. public function testConnectionSabNZBd()
  76. {
  77. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  78. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  79. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  80. try {
  81. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  82. $response = Requests::get($url, array(), $options);
  83. if ($response->success) {
  84. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  85. return true;
  86. }
  87. } catch (Requests_Exception $e) {
  88. $this->setAPIResponse('error', $e->getMessage(), 500);
  89. return false;
  90. };
  91. } else {
  92. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  93. return 'URL and/or Token not setup';
  94. }
  95. }
  96. public function getSabNZBdHomepageQueue()
  97. {
  98. if (!$this->config['homepageSabnzbdEnabled']) {
  99. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  100. return false;
  101. }
  102. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  103. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  104. return false;
  105. }
  106. if (empty($this->config['sabnzbdURL'])) {
  107. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  108. return false;
  109. }
  110. if (empty($this->config['sabnzbdToken'])) {
  111. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  112. return false;
  113. }
  114. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  115. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  116. try {
  117. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  118. $response = Requests::get($url, array(), $options);
  119. if ($response->success) {
  120. $api['content']['queueItems'] = json_decode($response->body, true);
  121. }
  122. } catch (Requests_Exception $e) {
  123. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  124. $this->setAPIResponse('error', $e->getMessage(), 500);
  125. return false;
  126. };
  127. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  128. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  129. try {
  130. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  131. $response = Requests::get($url, array(), $options);
  132. if ($response->success) {
  133. $api['content']['historyItems'] = json_decode($response->body, true);
  134. }
  135. } catch (Requests_Exception $e) {
  136. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  137. $this->setAPIResponse('error', $e->getMessage(), 500);
  138. return false;
  139. };
  140. $api['content'] = isset($api['content']) ? $api['content'] : false;
  141. $this->setAPIResponse('success', null, 200, $api);
  142. return $api;
  143. }
  144. public function pauseSabNZBdQueue($target = null)
  145. {
  146. if (!$this->config['homepageSabnzbdEnabled']) {
  147. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  148. return false;
  149. }
  150. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  151. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  152. return false;
  153. }
  154. if (empty($this->config['sabnzbdURL'])) {
  155. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  156. return false;
  157. }
  158. if (empty($this->config['sabnzbdToken'])) {
  159. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  160. return false;
  161. }
  162. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  163. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  164. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  165. try {
  166. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  167. $response = Requests::get($url, array(), $options);
  168. if ($response->success) {
  169. $api['content'] = json_decode($response->body, true);
  170. }
  171. } catch (Requests_Exception $e) {
  172. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  173. $this->setAPIResponse('error', $e->getMessage(), 500);
  174. return false;
  175. };
  176. $api['content'] = isset($api['content']) ? $api['content'] : false;
  177. $this->setAPIResponse('success', null, 200, $api);
  178. return $api;
  179. }
  180. public function resumeSabNZBdQueue($target = null)
  181. {
  182. if (!$this->config['homepageSabnzbdEnabled']) {
  183. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  184. return false;
  185. }
  186. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  187. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  188. return false;
  189. }
  190. if (empty($this->config['sabnzbdURL'])) {
  191. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  192. return false;
  193. }
  194. if (empty($this->config['sabnzbdToken'])) {
  195. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  196. return false;
  197. }
  198. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  199. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  200. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  201. try {
  202. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  203. $response = Requests::get($url, array(), $options);
  204. if ($response->success) {
  205. $api['content'] = json_decode($response->body, true);
  206. }
  207. } catch (Requests_Exception $e) {
  208. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  209. $this->setAPIResponse('error', $e->getMessage(), 500);
  210. return false;
  211. };
  212. $api['content'] = isset($api['content']) ? $api['content'] : false;
  213. $this->setAPIResponse('success', null, 200, $api);
  214. return $api;
  215. }
  216. }