sabnzbd.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. trait SabNZBdHomepageItem
  3. {
  4. public function sabNZBdSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'SabNZBD',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sabnzbd.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = array(
  17. 'debug' => true,
  18. 'settings' => array(
  19. 'Enable' => array(
  20. array(
  21. 'type' => 'switch',
  22. 'name' => 'homepageSabnzbdEnabled',
  23. 'label' => 'Enable',
  24. 'value' => $this->config['homepageSabnzbdEnabled']
  25. ),
  26. array(
  27. 'type' => 'select',
  28. 'name' => 'homepageSabnzbdAuth',
  29. 'label' => 'Minimum Authentication',
  30. 'value' => $this->config['homepageSabnzbdAuth'],
  31. 'options' => $this->groupOptions
  32. )
  33. ),
  34. 'Connection' => array(
  35. array(
  36. 'type' => 'input',
  37. 'name' => 'sabnzbdURL',
  38. 'label' => 'URL',
  39. 'value' => $this->config['sabnzbdURL'],
  40. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  41. 'placeholder' => 'http(s)://hostname:port'
  42. ),
  43. array(
  44. 'type' => 'password-alt',
  45. 'name' => 'sabnzbdToken',
  46. 'label' => 'Token',
  47. 'value' => $this->config['sabnzbdToken']
  48. )
  49. ),
  50. 'API SOCKS' => array(
  51. array(
  52. 'type' => 'html',
  53. 'override' => 12,
  54. 'label' => '',
  55. 'html' => '
  56. <div class="panel panel-default">
  57. <div class="panel-wrapper collapse in">
  58. <div class="panel-body">' . $this->socksHeadingHTML('sabnzbd') . '</div>
  59. </div>
  60. </div>'
  61. ),
  62. array(
  63. 'type' => 'switch',
  64. 'name' => 'sabnzbdSocksEnabled',
  65. 'label' => 'Enable',
  66. 'value' => $this->config['sabnzbdSocksEnabled']
  67. ),
  68. array(
  69. 'type' => 'select',
  70. 'name' => 'sabnzbdSocksAuth',
  71. 'label' => 'Minimum Authentication',
  72. 'value' => $this->config['sabnzbdSocksAuth'],
  73. 'options' => $this->groupOptions
  74. ),
  75. ),
  76. 'Misc Options' => array(
  77. array(
  78. 'type' => 'select',
  79. 'name' => 'sabnzbdRefresh',
  80. 'label' => 'Refresh Seconds',
  81. 'value' => $this->config['sabnzbdRefresh'],
  82. 'options' => $this->timeOptions()
  83. ),
  84. array(
  85. 'type' => 'switch',
  86. 'name' => 'sabnzbdCombine',
  87. 'label' => 'Add to Combined Downloader',
  88. 'value' => $this->config['sabnzbdCombine']
  89. ),
  90. ),
  91. 'Test Connection' => array(
  92. array(
  93. 'type' => 'blank',
  94. 'label' => 'Please Save before Testing'
  95. ),
  96. array(
  97. 'type' => 'button',
  98. 'label' => '',
  99. 'icon' => 'fa fa-flask',
  100. 'class' => 'pull-right',
  101. 'text' => 'Test Connection',
  102. 'attr' => 'onclick="testAPIConnection(\'sabnzbd\')"'
  103. ),
  104. )
  105. )
  106. );
  107. return array_merge($homepageInformation, $homepageSettings);
  108. }
  109. public function testConnectionSabNZBd()
  110. {
  111. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  112. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  113. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  114. try {
  115. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  116. $response = Requests::get($url, array(), $options);
  117. if ($response->success) {
  118. $data = json_decode($response->body, true);
  119. $status = 'success';
  120. $responseCode = 200;
  121. $message = 'API Connection succeeded';
  122. if (isset($data['error'])) {
  123. $status = 'error';
  124. $responseCode = 500;
  125. $message = $data['error'];
  126. }
  127. $this->setAPIResponse($status, $message, $responseCode, $data);
  128. return true;
  129. } else {
  130. $this->setAPIResponse('error', $response->body, 500);
  131. return false;
  132. }
  133. } catch (Requests_Exception $e) {
  134. $this->setAPIResponse('error', $e->getMessage(), 500);
  135. return false;
  136. };
  137. } else {
  138. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  139. return 'URL and/or Token not setup';
  140. }
  141. }
  142. public function sabNZBdHomepagePermissions($key = null)
  143. {
  144. $permissions = [
  145. 'main' => [
  146. 'enabled' => [
  147. 'homepageSabnzbdEnabled'
  148. ],
  149. 'auth' => [
  150. 'homepageSabnzbdAuth'
  151. ],
  152. 'not_empty' => [
  153. 'sabnzbdURL',
  154. 'sabnzbdToken'
  155. ]
  156. ]
  157. ];
  158. if (array_key_exists($key, $permissions)) {
  159. return $permissions[$key];
  160. } elseif ($key == 'all') {
  161. return $permissions;
  162. } else {
  163. return [];
  164. }
  165. }
  166. public function homepageOrdersabnzbd()
  167. {
  168. if ($this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'))) {
  169. $loadingBox = ($this->config['sabnzbdCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  170. $builder = ($this->config['sabnzbdCombine']) ? 'buildDownloaderCombined(\'sabnzbd\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sabnzbd"));';
  171. return '
  172. <div id="' . __FUNCTION__ . '">
  173. ' . $loadingBox . '
  174. <script>
  175. // homepageOrdersabnzbd
  176. ' . $builder . '
  177. homepageDownloader("sabnzbd", "' . $this->config['sabnzbdRefresh'] . '");
  178. // End homepageOrdersabnzbd
  179. </script>
  180. </div>
  181. ';
  182. }
  183. }
  184. public function getSabNZBdHomepageQueue()
  185. {
  186. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  187. return false;
  188. }
  189. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  190. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  191. try {
  192. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  193. $response = Requests::get($url, array(), $options);
  194. if ($response->success) {
  195. $api['content']['queueItems'] = json_decode($response->body, true);
  196. }
  197. } catch (Requests_Exception $e) {
  198. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  199. $this->setAPIResponse('error', $e->getMessage(), 500);
  200. return false;
  201. };
  202. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  203. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  204. try {
  205. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  206. $response = Requests::get($url, array(), $options);
  207. if ($response->success) {
  208. $api['content']['historyItems'] = json_decode($response->body, true);
  209. }
  210. } catch (Requests_Exception $e) {
  211. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  212. $this->setAPIResponse('error', $e->getMessage(), 500);
  213. return false;
  214. };
  215. $api['content'] = isset($api['content']) ? $api['content'] : false;
  216. $this->setAPIResponse('success', null, 200, $api);
  217. return $api;
  218. }
  219. public function pauseSabNZBdQueue($target = null)
  220. {
  221. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  222. return false;
  223. }
  224. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  225. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  226. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  227. try {
  228. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  229. $response = Requests::get($url, array(), $options);
  230. if ($response->success) {
  231. $api['content'] = json_decode($response->body, true);
  232. }
  233. } catch (Requests_Exception $e) {
  234. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  235. $this->setAPIResponse('error', $e->getMessage(), 500);
  236. return false;
  237. };
  238. $api['content'] = isset($api['content']) ? $api['content'] : false;
  239. $this->setAPIResponse('success', null, 200, $api);
  240. return $api;
  241. }
  242. public function resumeSabNZBdQueue($target = null)
  243. {
  244. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  245. return false;
  246. }
  247. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  248. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  249. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  250. try {
  251. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  252. $response = Requests::get($url, array(), $options);
  253. if ($response->success) {
  254. $api['content'] = json_decode($response->body, true);
  255. }
  256. } catch (Requests_Exception $e) {
  257. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  258. $this->setAPIResponse('error', $e->getMessage(), 500);
  259. return false;
  260. };
  261. $api['content'] = isset($api['content']) ? $api['content'] : false;
  262. $this->setAPIResponse('success', null, 200, $api);
  263. return $api;
  264. }
  265. }