deluge.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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('hide-status', 'delugeHideStatus'),
  55. $this->settingsOption('combine', 'delugeCombine'),
  56. $this->settingsOption('refresh', 'delugeRefresh'),
  57. ],
  58. 'Test Connection' => [
  59. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing. Note that using a blank password might not work correctly.']),
  60. $this->settingsOption('test', 'deluge'),
  61. ]
  62. ]
  63. ];
  64. return array_merge($homepageInformation, $homepageSettings);
  65. }
  66. public function testConnectionDeluge()
  67. {
  68. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  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->setLoggerChannel('Deluge')->error($e);
  79. $this->setResponse(500, $e->getMessage());
  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. return $this->homepageCheckKeyPermissions($key, $permissions);
  99. }
  100. public function homepageOrderdeluge()
  101. {
  102. if ($this->homepageItemPermissions($this->delugeHomepagePermissions('main'))) {
  103. $loadingBox = ($this->config['delugeCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  104. $builder = ($this->config['delugeCombine']) ? 'buildDownloaderCombined(\'deluge\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("deluge"));';
  105. return '
  106. <div id="' . __FUNCTION__ . '">
  107. ' . $loadingBox . '
  108. <script>
  109. // homepageOrderdeluge
  110. ' . $builder . '
  111. homepageDownloader("deluge", "' . $this->config['delugeRefresh'] . '");
  112. // End homepageOrderdeluge
  113. </script>
  114. </div>
  115. ';
  116. }
  117. }
  118. public function getDelugeHomepageQueue()
  119. {
  120. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  121. return false;
  122. }
  123. try {
  124. $options = $this->requestOptions($this->config['delugeURL'], $this->config['delugeRefresh'], $this->config['delugeDisableCertCheck'], $this->config['delugeUseCustomCertificate'], ['organizr_cert' => $this->getCert(), 'custom_cert' => $this->getCustomCert()]);
  125. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']), $options);
  126. $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, tracker_status');
  127. foreach ($torrents as $key => $value) {
  128. $tempStatus = $this->delugeStatus($value->queue, $value->state, $value->progress);
  129. if ($tempStatus == 'Seeding' && $this->config['delugeHideSeeding']) {
  130. //do nothing
  131. } elseif ($tempStatus == 'Finished' && $this->config['delugeHideCompleted']) {
  132. //do nothing
  133. } else {
  134. if ($this->config['delugeHideStatus']) {
  135. $value->tracker_status = "";
  136. }
  137. $api['content']['queueItems'][] = $value;
  138. }
  139. }
  140. $api['content']['queueItems'] = (empty($api['content']['queueItems'])) ? [] : $api['content']['queueItems'];
  141. $api['content']['historyItems'] = false;
  142. } catch (Exception $e) {
  143. $this->setLoggerChannel('Deluge')->error($e);
  144. $this->setResponse(500, $e->getMessage());
  145. return false;
  146. }
  147. $api['content'] = $api['content'] ?? false;
  148. $this->setAPIResponse('success', null, 200, $api);
  149. return $api;
  150. }
  151. public function delugeStatus($queued, $status, $state)
  152. {
  153. if ($queued == '-1' && $state == '100' && ($status == 'Seeding' || $status == 'Queued' || $status == 'Paused')) {
  154. $state = 'Seeding';
  155. } elseif ($state !== '100') {
  156. $state = 'Downloading';
  157. } else {
  158. $state = 'Finished';
  159. }
  160. return ($state) ? $state : $status;
  161. }
  162. }