transmission.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. trait TransmissionHomepageItem
  3. {
  4. public function testConnectionTransmission()
  5. {
  6. if (empty($this->config['transmissionURL'])) {
  7. $this->setAPIResponse('error', 'Transmission URL is not defined', 422);
  8. return false;
  9. }
  10. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  11. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . $this->decrypt($this->config['transmissionPassword']) . "@" : '';
  12. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  13. try {
  14. $options = ($this->localURL($this->config['transmissionURL'])) ? array('verify' => false) : array();
  15. $response = Requests::get($url, array(), $options);
  16. if ($response->headers['x-transmission-session-id']) {
  17. $headers = array(
  18. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  19. 'Content-Type' => 'application/json'
  20. );
  21. $data = array(
  22. 'method' => 'torrent-get',
  23. 'arguments' => array(
  24. 'fields' => array(
  25. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString"
  26. ),
  27. ),
  28. 'tags' => ''
  29. );
  30. $response = Requests::post($url, $headers, json_encode($data), $options);
  31. if ($response->success) {
  32. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  33. return true;
  34. } else {
  35. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Unknown', 500);
  36. return false;
  37. }
  38. } else {
  39. $this->writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  40. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  41. return false;
  42. }
  43. } catch (Requests_Exception $e) {
  44. $this->writeLog('error', 'Transmission Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  45. $this->setAPIResponse('error', $e->getMessage(), 500);
  46. return false;
  47. }
  48. }
  49. public function getTransmissionHomepageQueue()
  50. {
  51. if (!$this->config['homepageTransmissionEnabled']) {
  52. $this->setAPIResponse('error', 'Transmission homepage item is not enabled', 409);
  53. return false;
  54. }
  55. if (!$this->qualifyRequest($this->config['homepageTransmissionAuth'])) {
  56. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  57. return false;
  58. }
  59. if (empty($this->config['transmissionURL'])) {
  60. $this->setAPIResponse('error', 'Transmission URL is not defined', 422);
  61. return false;
  62. }
  63. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  64. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . $this->decrypt($this->config['transmissionPassword']) . "@" : '';
  65. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  66. try {
  67. $options = ($this->localURL($this->config['transmissionURL'])) ? array('verify' => false) : array();
  68. $response = Requests::get($url, array(), $options);
  69. if ($response->headers['x-transmission-session-id']) {
  70. $headers = array(
  71. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  72. 'Content-Type' => 'application/json'
  73. );
  74. $data = array(
  75. 'method' => 'torrent-get',
  76. 'arguments' => array(
  77. 'fields' => array(
  78. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString"
  79. ),
  80. ),
  81. 'tags' => ''
  82. );
  83. $response = Requests::post($url, $headers, json_encode($data), $options);
  84. if ($response->success) {
  85. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  86. if ($this->config['transmissionHideSeeding'] || $this->config['transmissionHideCompleted']) {
  87. $filter = array();
  88. $torrents = array();
  89. if ($this->config['transmissionHideSeeding']) {
  90. array_push($filter, 6, 5);
  91. }
  92. if ($this->config['transmissionHideCompleted']) {
  93. array_push($filter, 0);
  94. }
  95. foreach ($torrentList as $key => $value) {
  96. if (!in_array($value['status'], $filter)) {
  97. $torrents[] = $value;
  98. }
  99. }
  100. } else {
  101. $torrents = json_decode($response->body, true);
  102. }
  103. $api['content']['queueItems'] = $torrents;
  104. $api['content']['historyItems'] = false;
  105. }
  106. } else {
  107. $this->writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  108. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  109. return false;
  110. }
  111. } catch (Requests_Exception $e) {
  112. $this->writeLog('error', 'Transmission Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  113. $this->setAPIResponse('error', $e->getMessage(), 500);
  114. return false;
  115. };
  116. $api['content'] = isset($api['content']) ? $api['content'] : false;
  117. $this->setAPIResponse('success', null, 200, $api);
  118. return $api;
  119. }
  120. }