deluge.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. trait DelugeHomepageItem
  3. {
  4. public function delugeSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Deluge',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/deluge.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'FYI' => [
  20. $this->settingsOption('html', null, ['override' => 12, 'html' => '
  21. <div class="row">
  22. <div class="col-lg-12">
  23. <div class="panel panel-info">
  24. <div class="panel-heading">
  25. <span lang="en">Notice</span>
  26. </div>
  27. <div class="panel-wrapper collapse in" aria-expanded="true">
  28. <div class="panel-body">
  29. <ul class="list-icons">
  30. <li><i class="fa fa-chevron-right text-danger"></i> <a href="https://github.com/idlesign/deluge-webapi/tree/master/dist" target="_blank">Download Plugin</a></li>
  31. <li><i class="fa fa-chevron-right text-danger"></i> Open Deluge Web UI, go to "Preferences -> Plugins -> Install plugin" and choose egg file.</li>
  32. <li><i class="fa fa-chevron-right text-danger"></i> Activate WebAPI plugin </li>
  33. </ul>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>']
  39. )
  40. ],
  41. 'Enable' => [
  42. $this->settingsOption('enable', 'homepageDelugeEnabled'),
  43. $this->settingsOption('auth', 'homepageDelugeAuth'),
  44. ],
  45. 'Connection' => [
  46. $this->settingsOption('url', 'delugeURL'),
  47. $this->settingsOption('password', 'delugePassword', ['help' => 'Note that using a blank password might not work correctly.']),
  48. $this->settingsOption('disable-cert-check', 'delugeDisableCertCheck'),
  49. $this->settingsOption('use-custom-certificate', 'delugeUseCustomCertificate'),
  50. ],
  51. 'Misc Options' => [
  52. $this->settingsOption('hide-seeding', 'delugeHideSeeding'),
  53. $this->settingsOption('hide-completed', 'delugeHideCompleted'),
  54. $this->settingsOption('refresh', 'delugeRefresh'),
  55. $this->settingsOption('combine', 'delugeCombine'),
  56. ],
  57. 'Test Connection' => [
  58. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing. Note that using a blank password might not work correctly.']),
  59. $this->settingsOption('test', 'deluge'),
  60. ]
  61. ]
  62. ];
  63. return array_merge($homepageInformation, $homepageSettings);
  64. }
  65. public function testConnectionDeluge()
  66. {
  67. if (empty($this->config['delugeURL'])) {
  68. $this->setAPIResponse('error', 'Deluge URL is not defined', 422);
  69. return false;
  70. }
  71. try {
  72. $options = $this->requestOptions($this->config['delugeURL'], $this->config['delugeRefresh'], $this->config['delugeDisableCertCheck'], $this->config['delugeUseCustomCertificate'], ['organizr_cert' => $this->getCert(), 'custom_cert' => $this->getCustomCert()]);
  73. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']), $options);
  74. $torrents = $deluge->getTorrents(null, 'comment, download_payload_rate, eta, hash, is_finished, is_seed, message, name, paused, progress, queue, state, total_size, upload_payload_rate');
  75. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  76. return true;
  77. } catch (Exception $e) {
  78. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  79. $this->setAPIResponse('error', $e->getMessage(), 500);
  80. return false;
  81. }
  82. }
  83. public function delugeHomepagePermissions($key = null)
  84. {
  85. $permissions = [
  86. 'main' => [
  87. 'enabled' => [
  88. 'homepageDelugeEnabled'
  89. ],
  90. 'auth' => [
  91. 'homepageDelugeAuth'
  92. ],
  93. 'not_empty' => [
  94. 'delugeURL'
  95. ]
  96. ]
  97. ];
  98. if (array_key_exists($key, $permissions)) {
  99. return $permissions[$key];
  100. } elseif ($key == 'all') {
  101. return $permissions;
  102. } else {
  103. return [];
  104. }
  105. }
  106. public function homepageOrderdeluge()
  107. {
  108. if ($this->homepageItemPermissions($this->delugeHomepagePermissions('main'))) {
  109. $loadingBox = ($this->config['delugeCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  110. $builder = ($this->config['delugeCombine']) ? 'buildDownloaderCombined(\'deluge\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("deluge"));';
  111. return '
  112. <div id="' . __FUNCTION__ . '">
  113. ' . $loadingBox . '
  114. <script>
  115. // homepageOrderdeluge
  116. ' . $builder . '
  117. homepageDownloader("deluge", "' . $this->config['delugeRefresh'] . '");
  118. // End homepageOrderdeluge
  119. </script>
  120. </div>
  121. ';
  122. }
  123. }
  124. public function getDelugeHomepageQueue()
  125. {
  126. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  127. return false;
  128. }
  129. try {
  130. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']));
  131. $torrents = $deluge->getTorrents(null, 'comment, download_payload_rate, eta, hash, is_finished, is_seed, message, name, paused, progress, queue, state, total_size, upload_payload_rate');
  132. foreach ($torrents as $key => $value) {
  133. $tempStatus = $this->delugeStatus($value->queue, $value->state, $value->progress);
  134. if ($tempStatus == 'Seeding' && $this->config['delugeHideSeeding']) {
  135. //do nothing
  136. } elseif ($tempStatus == 'Finished' && $this->config['delugeHideCompleted']) {
  137. //do nothing
  138. } else {
  139. $api['content']['queueItems'][] = $value;
  140. }
  141. }
  142. $api['content']['queueItems'] = (empty($api['content']['queueItems'])) ? [] : $api['content']['queueItems'];
  143. $api['content']['historyItems'] = false;
  144. } catch (Excecption $e) {
  145. $this->writeLog('error', 'Deluge Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  146. $this->setAPIResponse('error', $e->getMessage(), 500);
  147. return false;
  148. }
  149. $api['content'] = isset($api['content']) ? $api['content'] : false;
  150. $this->setAPIResponse('success', null, 200, $api);
  151. return $api;
  152. }
  153. public function delugeStatus($queued, $status, $state)
  154. {
  155. if ($queued == '-1' && $state == '100' && ($status == 'Seeding' || $status == 'Queued' || $status == 'Paused')) {
  156. $state = 'Seeding';
  157. } elseif ($state !== '100') {
  158. $state = 'Downloading';
  159. } else {
  160. $state = 'Finished';
  161. }
  162. return ($state) ? $state : $status;
  163. }
  164. }