deluge.class.php 18 KB

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