deluge.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. class deluge {
  3. private $ch;
  4. private $url;
  5. private $request_id;
  6. function __construct($host, $password)
  7. {
  8. $this->url = $host . (substr($host, -1) == "/" ? "" : "/") . "json";
  9. $this->request_id = 0;
  10. $this->ch = curl_init($this->url);
  11. $curl_options = array(
  12. CURLOPT_RETURNTRANSFER => true,
  13. CURLOPT_HTTPHEADER => array("Accept: application/json", "Content-Type: application/json"),
  14. CURLOPT_ENCODING => "",
  15. CURLOPT_COOKIEJAR => "",
  16. CURLOPT_CONNECTTIMEOUT => 10,
  17. CURLOPT_TIMEOUT => 10,
  18. CURLOPT_CAINFO => getCert(),
  19. CURLOPT_SSL_VERIFYHOST => localURL($host) ? 0 : 2,
  20. CURLOPT_SSL_VERIFYPEER => localURL($host) ? 0 : 2,
  21. //CURLOPT_SSL_VERIFYPEER => false, THIS IS INSECURE!! However, deluge appears not to send intermediate certificates, so it can be necessary. Use with caution!
  22. );
  23. curl_setopt_array($this->ch, $curl_options);
  24. //Log in and get cookies
  25. try {
  26. $result = $this->makeRequest("auth.login", array($password));
  27. if ($result === false)
  28. throw new Exception("Login failed");
  29. }
  30. catch (Exception $e) {
  31. throw new Exception("Failed to initiate deluge api", 0, $e);
  32. }
  33. }
  34. /////////////////////////////
  35. //
  36. //webapi functions (https://github.com/idlesign/deluge-webapi)
  37. //
  38. /////////////////////////////
  39. //ids is an array of hashes, or null for all
  40. //params is an array of params:
  41. //active_time, all_time_download, comment, compact, distributed_copies, download_payload_rate, eta, file_priorities, file_progress, files, hash, is_auto_managed, is_finished, is_seed, max_connections, max_download_speed, max_upload_slots, max_upload_speed, message, move_completed, move_completed_path, move_on_completed, move_on_completed_path, name, next_announce, num_files, num_peers, num_pieces, num_seeds, paused, peers, piece_length, prioritize_first_last, private, progress, queue, ratio, remove_at_ratio, save_path, seed_rank, seeding_time, seeds_peers_ratio, state, stop_at_ratio, stop_ratio, time_added, total_done, total_payload_download, total_payload_upload, total_peers, total_seeds, total_size, total_uploaded, total_wanted, tracker, tracker_host, tracker_status, trackers, upload_payload_rate
  42. //or an empty array for all (or null, which returns comment, hash, name, save_path)
  43. public function getTorrents($ids, $params) {
  44. return $this->makeRequest("webapi.get_torrents", array($ids, $params))->torrents;
  45. }
  46. //metainfo is base64 encoded torrent data or a magnet url
  47. //returns a torrent hash or null if the torrent wasn't aded
  48. //params are torrent addition parameters like download_location?
  49. public function addTorrent($metaInfo, $params) {
  50. return $this->makeRequest("webapi.add_torrent", array($metaInfo, $params));
  51. }
  52. /*implemented in core
  53. public function removeTorrent($hash, $removeData) {
  54. return $this->makeRequest("webapi.remove_torrent", array($hash, $removeData));
  55. }*/
  56. public function getWebAPIVersion() {
  57. return $this->makeRequest("webapi.get_api_version", array());
  58. }
  59. /////////////////////////////
  60. //
  61. //core functions
  62. //
  63. //parsed from https://web.archive.org/web/20150423162855/http://deluge-torrent.org:80/docs/master/core/rpc.html
  64. //
  65. /////////////////////////////
  66. //Adds a torrent file to the session.
  67. //Parameters:
  68. //filename (string) – the filename of the torrent
  69. //filedump (string) – a base64 encoded string of the torrent file contents
  70. //options (dict) – the options to apply to the torrent on add
  71. public function addTorrentFile($filename, $filedump, $options) {
  72. return $this->makeRequest("core.add_torrent_file", array($filename, $filedump, $options));
  73. }
  74. //Adds a torrent from a magnet link.
  75. //Parameters:
  76. //uri (string) – the magnet link
  77. //options (dict) – the options to apply to the torrent on add
  78. public function addTorrentMagnet($uri, $options) {
  79. return $this->makeRequest("core.add_torrent_magnet", array($uri, $options));
  80. }
  81. //Adds a torrent from a url. Deluge will attempt to fetch the torrentfrom url prior to adding it to the session.
  82. //Parameters:
  83. //url (string) – the url pointing to the torrent file
  84. //options (dict) – the options to apply to the torrent on add
  85. //headers (dict) – any optional headers to send
  86. public function addTorrentUrl($url, $options, $headers) {
  87. return $this->makeRequest("core.add_torrent_url", array($url, $options, $headers));
  88. }
  89. public function connectPeer($torrentId, $ip, $port) {
  90. return $this->makeRequest("core.connect_peer", array($torrentId, $ip, $port));
  91. }
  92. public function createTorrent($path, $tracker, $pieceLength, $comment, $target, $webseeds, $private, $createdBy, $trackers, $addToSession) {
  93. return $this->makeRequest("core.create_torrent", array($path, $tracker, $pieceLength, $comment, $target, $webseeds, $private, $createdBy, $trackers, $addToSession));
  94. }
  95. public function disablePlugin($plugin) {
  96. return $this->makeRequest("core.disable_plugin", array($plugin));
  97. }
  98. public function enablePlugin($plugin) {
  99. return $this->makeRequest("core.enable_plugin", array($plugin));
  100. }
  101. public function forceReannounce($torrentIds) {
  102. return $this->makeRequest("core.force_reannounce", array($torrentIds));
  103. }
  104. //Forces a data recheck on torrent_ids
  105. public function forceRecheck($torrentIds) {
  106. return $this->makeRequest("core.force_recheck", array($torrentIds));
  107. }
  108. //Returns a list of plugins available in the core
  109. public function getAvailablePlugins() {
  110. return $this->makeRequest("core.get_available_plugins", array());
  111. }
  112. //Returns a dictionary of the session’s cache status.
  113. public function getCacheStatus() {
  114. return $this->makeRequest("core.get_cache_status", array());
  115. }
  116. //Get all the preferences as a dictionary
  117. public function getConfig() {
  118. return $this->makeRequest("core.get_config", array());
  119. }
  120. //Get the config value for key
  121. public function getConfigValue($key) {
  122. return $this->makeRequest("core.get_config_value", array($key));
  123. }
  124. //Get the config values for the entered keys
  125. public function getConfigValues($keys) {
  126. return $this->makeRequest("core.get_config_values", array($keys));
  127. }
  128. //Returns a list of enabled plugins in the core
  129. public function getEnabledPlugins() {
  130. return $this->makeRequest("core.get_enabled_plugins", array());
  131. }
  132. //returns {field: [(value,count)] }for use in sidebar(s)
  133. public function getFilterTree($showZeroHits, $hideCat) {
  134. return $this->makeRequest("core.get_filter_tree", array($showZeroHits, $hideCat));
  135. }
  136. //Returns the number of free bytes at path
  137. public function getFreeSpace($path) {
  138. return $this->makeRequest("core.get_free_space", array($path));
  139. }
  140. //Returns the libtorrent version.
  141. public function getLibtorrentVersion() {
  142. return $this->makeRequest("core.get_libtorrent_version", array());
  143. }
  144. //Returns the active listen port
  145. public function getListenPort() {
  146. return $this->makeRequest("core.get_listen_port", array());
  147. }
  148. //Returns the current number of connections
  149. public function getNumConnections() {
  150. return $this->makeRequest("core.get_num_connections", array());
  151. }
  152. public function getPathSize($path) {
  153. return $this->makeRequest("core.get_path_size", array($path));
  154. }
  155. //Returns a list of torrent_ids in the session.
  156. public function getSessionState() {
  157. return $this->makeRequest("core.get_session_state", array());
  158. }
  159. //Gets the session status values for ‘keys’, these keys are takingfrom libtorrent’s session status.
  160. public function getSessionStatus($keys) {
  161. return $this->makeRequest("core.get_session_status", array($keys));
  162. }
  163. public function getTorrentStatus($torrentId, $keys, $diff) {
  164. return $this->makeRequest("core.get_torrent_status", array($torrentId, $keys, $diff));
  165. }
  166. //returns all torrents , optionally filtered by filter_dict.
  167. public function getTorrentsStatus($filterDict, $keys, $diff) {
  168. return $this->makeRequest("core.get_torrents_status", array($filterDict, $keys, $diff));
  169. }
  170. public function glob($path) {
  171. return $this->makeRequest("core.glob", array($path));
  172. }
  173. public function moveStorage($torrentIds, $dest) {
  174. return $this->makeRequest("core.move_storage", array($torrentIds, $dest));
  175. }
  176. //Pause all torrents in the session
  177. public function pauseAllTorrents() {
  178. return $this->makeRequest("core.pause_all_torrents", array());
  179. }
  180. public function pauseTorrent($torrentIds) {
  181. return $this->makeRequest("core.pause_torrent", array($torrentIds));
  182. }
  183. public function queueBottom($torrentIds) {
  184. return $this->makeRequest("core.queue_bottom", array($torrentIds));
  185. }
  186. public function queueDown($torrentIds) {
  187. return $this->makeRequest("core.queue_down", array($torrentIds));
  188. }
  189. public function queueTop($torrentIds) {
  190. return $this->makeRequest("core.queue_top", array($torrentIds));
  191. }
  192. public function queueUp($torrentIds) {
  193. return $this->makeRequest("core.queue_up", array($torrentIds));
  194. }
  195. //Removes a torrent from the session.
  196. //Parameters:
  197. //torrentId (string) – the torrentId of the torrent to remove
  198. //removeData (boolean) – if True, remove the data associated with this torrent
  199. public function removeTorrent($torrentId, $removeData) {
  200. return $this->makeRequest("core.remove_torrent", array($torrentId, $removeData));
  201. }
  202. //Rename files in torrent_id. Since this is an asynchronous operation bylibtorrent, watch for the TorrentFileRenamedEvent to know when thefiles have been renamed.
  203. //Parameters:
  204. //torrentId (string) – the torrentId to rename files
  205. //filenames (((index, filename), ...)) – a list of index, filename pairs
  206. public function renameFiles($torrentId, $filenames) {
  207. return $this->makeRequest("core.rename_files", array($torrentId, $filenames));
  208. }
  209. //Renames the ‘folder’ to ‘new_folder’ in ‘torrent_id’. Watch for theTorrentFolderRenamedEvent which is emitted when the folder has beenrenamed successfully.
  210. //Parameters:
  211. //torrentId (string) – the torrent to rename folder in
  212. //folder (string) – the folder to rename
  213. //newFolder (string) – the new folder name
  214. public function renameFolder($torrentId, $folder, $newFolder) {
  215. return $this->makeRequest("core.rename_folder", array($torrentId, $folder, $newFolder));
  216. }
  217. //Rescans the plugin folders for new plugins
  218. public function rescanPlugins() {
  219. return $this->makeRequest("core.rescan_plugins", array());
  220. }
  221. //Resume all torrents in the session
  222. public function resumeAllTorrents() {
  223. return $this->makeRequest("core.resume_all_torrents", array());
  224. }
  225. public function resumeTorrent($torrentIds) {
  226. return $this->makeRequest("core.resume_torrent", array($torrentIds));
  227. }
  228. //Set the config with values from dictionary
  229. public function setConfig($config) {
  230. return $this->makeRequest("core.set_config", array($config));
  231. }
  232. //Sets the auto managed flag for queueing purposes
  233. public function setTorrentAutoManaged($torrentId, $value) {
  234. return $this->makeRequest("core.set_torrent_auto_managed", array($torrentId, $value));
  235. }
  236. //Sets a torrents file priorities
  237. public function setTorrentFilePriorities($torrentId, $priorities) {
  238. return $this->makeRequest("core.set_torrent_file_priorities", array($torrentId, $priorities));
  239. }
  240. //Sets a torrents max number of connections
  241. public function setTorrentMaxConnections($torrentId, $value) {
  242. return $this->makeRequest("core.set_torrent_max_connections", array($torrentId, $value));
  243. }
  244. //Sets a torrents max download speed
  245. public function setTorrentMaxDownloadSpeed($torrentId, $value) {
  246. return $this->makeRequest("core.set_torrent_max_download_speed", array($torrentId, $value));
  247. }
  248. //Sets a torrents max number of upload slots
  249. public function setTorrentMaxUploadSlots($torrentId, $value) {
  250. return $this->makeRequest("core.set_torrent_max_upload_slots", array($torrentId, $value));
  251. }
  252. //Sets a torrents max upload speed
  253. public function setTorrentMaxUploadSpeed($torrentId, $value) {
  254. return $this->makeRequest("core.set_torrent_max_upload_speed", array($torrentId, $value));
  255. }
  256. //Sets the torrent to be moved when completed
  257. public function setTorrentMoveCompleted($torrentId, $value) {
  258. return $this->makeRequest("core.set_torrent_move_completed", array($torrentId, $value));
  259. }
  260. //Sets the path for the torrent to be moved when completed
  261. public function setTorrentMoveCompletedPath($torrentId, $value) {
  262. return $this->makeRequest("core.set_torrent_move_completed_path", array($torrentId, $value));
  263. }
  264. //Sets the torrent options for torrent_ids
  265. public function setTorrentOptions($torrentIds, $options) {
  266. return $this->makeRequest("core.set_torrent_options", array($torrentIds, $options));
  267. }
  268. //Sets a higher priority to the first and last pieces
  269. public function setTorrentPrioritizeFirstLast($torrentId, $value) {
  270. return $this->makeRequest("core.set_torrent_prioritize_first_last", array($torrentId, $value));
  271. }
  272. //Sets the torrent to be removed at ‘stop_ratio’
  273. public function setTorrentRemoveAtRatio($torrentId, $value) {
  274. return $this->makeRequest("core.set_torrent_remove_at_ratio", array($torrentId, $value));
  275. }
  276. //Sets the torrent to stop at ‘stop_ratio’
  277. public function setTorrentStopAtRatio($torrentId, $value) {
  278. return $this->makeRequest("core.set_torrent_stop_at_ratio", array($torrentId, $value));
  279. }
  280. //Sets the ratio when to stop a torrent if ‘stop_at_ratio’ is set
  281. public function setTorrentStopRatio($torrentId, $value) {
  282. return $this->makeRequest("core.set_torrent_stop_ratio", array($torrentId, $value));
  283. }
  284. //Sets a torrents tracker list. trackers will be [{“url”, “tier”}]
  285. public function setTorrentTrackers($torrentId, $trackers) {
  286. return $this->makeRequest("core.set_torrent_trackers", array($torrentId, $trackers));
  287. }
  288. //Checks if the active port is open
  289. public function testListenPort() {
  290. return $this->makeRequest("core.test_listen_port", array());
  291. }
  292. public function uploadPlugin($filename, $filedump) {
  293. return $this->makeRequest("core.upload_plugin", array($filename, $filedump));
  294. }
  295. //Returns a list of the exported methods.
  296. public function getMethodList() {
  297. return $this->makeRequest("daemon.get_method_list", array());
  298. }
  299. //Returns some info from the daemon.
  300. public function info() {
  301. return $this->makeRequest("daemon.info", array());
  302. }
  303. public function shutdown(...$params) {
  304. return $this->makeRequest("daemon.shutdown", $params);
  305. }
  306. /////////////////////////////
  307. //
  308. //web ui functions
  309. //
  310. //parsed from https://web.archive.org/web/20150423143401/http://deluge-torrent.org:80/docs/master/modules/ui/web/json_api.html#module-deluge.ui.web.json_api
  311. //
  312. /////////////////////////////
  313. //Parameters:
  314. //host (string) – the hostname
  315. //port (int) – the port
  316. //username (string) – the username to login as
  317. //password (string) – the password to login with
  318. public function addHost($host, $port, $username, $password) {
  319. return $this->makeRequest("web.add_host", array($host, $port, $username, $password));
  320. }
  321. //Usage
  322. public function addTorrents($torrents) {
  323. return $this->makeRequest("web.add_torrents", array($torrents));
  324. }
  325. public function connect($hostId) {
  326. return $this->makeRequest("web.connect", array($hostId));
  327. }
  328. public function connected() {
  329. return $this->makeRequest("web.connected", array());
  330. }
  331. public function deregisterEventListener($event) {
  332. return $this->makeRequest("web.deregister_event_listener", array($event));
  333. }
  334. public function disconnect() {
  335. return $this->makeRequest("web.disconnect", array());
  336. }
  337. public function downloadTorrentFromUrl($url, $cookie) {
  338. return $this->makeRequest("web.download_torrent_from_url", array($url, $cookie));
  339. }
  340. /* in core
  341. public function getConfig() {
  342. return $this->makeRequest("web.get_config", array());
  343. }*/
  344. public function getEvents() {
  345. return $this->makeRequest("web.get_events", array());
  346. }
  347. public function getHost($hostId) {
  348. return $this->makeRequest("web.get_host", array($hostId));
  349. }
  350. public function getHostStatus($hostId) {
  351. return $this->makeRequest("web.get_host_status", array($hostId));
  352. }
  353. public function getHosts() {
  354. return $this->makeRequest("web.get_hosts", array());
  355. }
  356. public function getTorrentFiles($torrentId) {
  357. return $this->makeRequest("web.get_torrent_files", array($torrentId));
  358. }
  359. public function getTorrentInfo($filename) {
  360. return $this->makeRequest("web.get_torrent_info", array($filename));
  361. }
  362. public function registerEventListener($event) {
  363. return $this->makeRequest("web.register_event_listener", array($event));
  364. }
  365. public function removeHost($connectionId) {
  366. return $this->makeRequest("web.remove_host", array($connectionId));
  367. }
  368. /*in core
  369. public function setConfig($config) {
  370. return $this->makeRequest("web.set_config", array($config));
  371. }*/
  372. public function startDaemon($port) {
  373. return $this->makeRequest("web.start_daemon", array($port));
  374. }
  375. public function stopDaemon($hostId) {
  376. return $this->makeRequest("web.stop_daemon", array($hostId));
  377. }
  378. //Parameters:
  379. //keys (list) – the information about the torrents to gather
  380. //filterDict (dictionary) – the filters to apply when selecting torrents.
  381. public function updateUi($keys, $filterDict) {
  382. return $this->makeRequest("web.update_ui", array($keys, $filterDict));
  383. }
  384. private function makeRequest($method, $params) {
  385. $post_data = array("id" => $this->request_id, "method" => $method, "params" => $params);
  386. curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($post_data));
  387. $result = curl_exec($this->ch);
  388. if ($result === false)
  389. throw new Exception("Could not log in due to curl error (no. " . curl_errno($this->ch) . "): " . curl_error($this->ch));
  390. $http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
  391. if ($http_code != 200)
  392. throw new Exception("Request for method $method returned http code $http_code");
  393. $result = json_decode($result);
  394. if (!is_null($result->error))
  395. throw new Exception("Method request returned an error (no. " . $result->error->code . "): " . $result->error->message);
  396. if ($result->id != $this->request_id)
  397. throw new Exception("Response id did not match request id");
  398. $this->request_id++;
  399. return $result->result;
  400. }
  401. }
  402. ?>