deluge.class.php 20 KB

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