deluge.class.php 18 KB

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