Торрент трекер для Wordpress. Разработка не планируется со смертью модуля для PHP от которого зависит проект.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

torrent_tracker.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /*
  3. Plugin Name: 21h Torrent Tracker
  4. Text Domain: blindage.org
  5. Domain Path: /
  6. Plugin URI: http://blindage.org/?p=7686
  7. Description: Torrent tracker
  8. Author: Vladimir Smagin
  9. Author URI: http://blindage.org/
  10. Version: 1.1
  11. License: GPL
  12. Donate URI: http://blindage.org
  13. */
  14. /**
  15. License:
  16. ==============================================================================
  17. Copyright 2015 Vladimir Smaign (email : 21h@blindage.org)
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. function ttdb_install() {
  31. global $wpdb;
  32. $table_name = $wpdb->prefix . "torrent_tracker";
  33. $charset_collate = $wpdb->get_charset_collate();
  34. $sql = "CREATE TABLE $table_name (
  35. id mediumint(9) NOT NULL AUTO_INCREMENT,
  36. file_id mediumint(9) NOT NULL,
  37. title varchar(255) DEFAULT '' NOT NULL,
  38. url varchar(255) DEFAULT '' NOT NULL,
  39. cached_path varchar(255) DEFAULT '' NOT NULL,
  40. size_mb mediumint(9) NOT NULL,
  41. sha1 varchar(255) NOT NULL,
  42. announce varchar(255),
  43. scrape varchar(255),
  44. cached datetime DEFAULT '00-00-0000 00:00:00' NOT NULL,
  45. seeders mediumint(9) NOT NULL,
  46. leechers mediumint(9) NOT NULL,
  47. UNIQUE KEY id (id)
  48. ) $charset_collate;";
  49. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  50. dbDelta($sql);
  51. add_option("21h_torrent_tracker_db_version", "1.0");
  52. add_option('21h_torrent_tracker', array(
  53. "torrents_per_page" => 0,
  54. "viewas" => 'checked',
  55. "showseed" => 'checked',
  56. "showleech" => 'checked',
  57. "cachetime" => 600,
  58. "html_before" => "<div id='torrent_list'>",
  59. "html_after" => "</div>",
  60. "html_tracker_title" => "<h3>All torrent files</h3>",
  61. "html_in_post_tracker_title" => "<h3>All torrent files in this post</h3>"
  62. ));
  63. }
  64. function ttdb_select_all() {
  65. global $wpdb;
  66. $table_name = $wpdb->prefix . 'torrent_tracker';
  67. return $wpdb->get_results("SELECT * FROM $table_name order by title asc", ARRAY_A);
  68. }
  69. function ttdb_select_id($id) {
  70. global $wpdb;
  71. $table_name = $wpdb->prefix . 'torrent_tracker';
  72. return $wpdb->get_results("SELECT * FROM $table_name where file_id=$id order by title asc", ARRAY_A);
  73. }
  74. function ttdb_insert($data) {
  75. global $wpdb;
  76. $table_name = $wpdb->prefix . 'torrent_tracker';
  77. $wpdb->insert($table_name, $data);
  78. }
  79. function ttdb_update($id, $data) {
  80. global $wpdb;
  81. $table_name = $wpdb->prefix . 'torrent_tracker';
  82. $wpdb->update($table_name, $data, array('file_id' => $id));
  83. }
  84. function ttdb_delete($id) {
  85. global $wpdb;
  86. $table_name = $wpdb->prefix . 'torrent_tracker';
  87. $wpdb->delete($table_name, array('file_id' => $id));
  88. }
  89. function search_for_torrents($post_id) {
  90. $attachs = get_attached_media('application/torrent', $post_id);
  91. $return = array();
  92. foreach ($attachs as $attach) {
  93. $title = $attach->post_title;
  94. $url = $attach->guid;
  95. $file_id = $attach->ID;
  96. array_push($return, array('title' => $title, 'url' => $url, 'file_id' => $file_id));
  97. }
  98. return $return;
  99. }
  100. function db_torrent_exists($db, $file_id) {
  101. global $wpdb;
  102. $table_name = $wpdb->prefix . 'torrent_tracker';
  103. $count = $wpdb->get_var("SELECT COUNT(id) FROM $table_name where file_id=" . $file_id);
  104. return $count;
  105. }
  106. function torrent_add_attachment($post_ID) {
  107. $post = get_post($post_ID);
  108. $upload_dir = wp_upload_dir()['path'];
  109. if (strpos($post->post_mime_type, 'torrent') and db_torrent_exists($db, $post_ID) == 0) {
  110. $torrent_file_contents = file_get_contents($upload_dir . '/' . basename($post->guid));
  111. $cached_path = 'cache/' . sha1($torrent_file_contents);
  112. $info = bdecode($torrent_file_contents);
  113. $sha1 = sha1(bencode($info['info']), true);
  114. $announce = $info['announce'];
  115. $scrape = str_replace("announce ", "scrape", $announce);
  116. $cached = date("Y-m-d H:i:s");
  117. $size_mb = 0;
  118. if (isset($info['info']['files'])) {
  119. foreach ($info['info']['files'] as $file) {
  120. $size_mb = $size_mb + round($file['length'] / 1024 / 1024);
  121. }
  122. } else {
  123. $size_mb = $size_mb + round($info['info']['length'] / 1024 / 1024);
  124. }
  125. file_put_contents(plugin_dir_path(__FILE__) . $cached_path, $torrent_file_contents);
  126. $data = array(
  127. 'file_id' => $post_ID,
  128. 'title' => $post->post_title,
  129. 'url' => $post->guid,
  130. 'cached_path' => $cached_path,
  131. 'sha1' => base64_encode($sha1),
  132. 'size_mb' => $size_mb,
  133. 'announce' => $announce,
  134. 'scrape' => $scrape,
  135. 'cached' => $cached
  136. );
  137. ttdb_insert($data);
  138. }
  139. }
  140. function torrent_delete_attachment($post_ID) {
  141. ttdb_delete($post_ID);
  142. }
  143. function torrent_edit_attachment($post_ID) {
  144. $post = get_post($post_ID);
  145. $data = array(
  146. 'title' => $post->post_title
  147. );
  148. ttdb_update($post_ID, $data);
  149. }
  150. function selectFormOfView() {
  151. $options = get_option('21h_torrent_tracker');
  152. if (isset($options['viewas'])) { return generateTrackerHTMLasTable(); } else { return generateTrackerHTMLasList(); }
  153. }
  154. function generateTrackerHTMLasList() {
  155. $options = get_option('21h_torrent_tracker');
  156. $torrents = ttdb_select_all();
  157. $htmlcode = $options['html_before'];
  158. $htmlcode.= $options['html_tracker_title'];
  159. $htmlcode.="<ol>";
  160. foreach ($torrents as $torrent) {
  161. if (time() - strtotime($torrent['cached']) > $options['cachetime']) {
  162. $scrape_url = $torrent['scrape'] . '?info_hash=' . urlencode(base64_decode($torrent['sha1']));
  163. if (($torrent_file_contents = @file_get_contents($scrape_url)) === FALSE) {
  164. //tracker not available
  165. } else {
  166. $sha1 = base64_decode($torrent['sha1']);
  167. $scrape_data = bdecode($torrent_file_contents);
  168. if (isset($scrape_data['files'][$sha1]['complete'])) {
  169. $seeders = $scrape_data['files'][$sha1]['complete'];
  170. $leechers = $scrape_data['files'][$sha1]['incomplete'];
  171. } else {
  172. $seeders = $scrape_data['complete'];
  173. $leechers = $scrape_data['incomplete'];
  174. }
  175. $data = array(
  176. 'seeders' => $seeders,
  177. 'leechers' => $leechers,
  178. 'cached' => date('Y-m-d H:i:s')
  179. );
  180. ttdb_update($torrent['file_id'], $data);
  181. $htmlcode.="<li>";
  182. $htmlcode.="<a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a> ";
  183. $htmlcode.=" (" . $torrent['size_mb'] . " Mb) ";
  184. if (isset($options['showseed'])) $htmlcode.="<span class='tt_seeds'>Seeders: $seeders</span> ";
  185. if (isset($options['showleech'])) $htmlcode.="<span class='tt_leech'>Leechers: $leechers</span> ";
  186. $htmlcode.="</li>";
  187. }
  188. } else {
  189. $sha1 = base64_decode($torrent['sha1']);
  190. $scrape_data = bdecode($torrent_file_contents);
  191. $seeders = $torrent['seeders'];
  192. $leechers = $torrent['leechers'];
  193. $htmlcode.="<li>";
  194. $htmlcode.="<a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a> ";
  195. $htmlcode.=" (" . $torrent['size_mb'] . " Mb) ";
  196. if (isset($options['showseed'])) $htmlcode.="<span class='tt_seeds'>Seeders: $seeders</span> ";
  197. if (isset($options['showleech'])) $htmlcode.="<span class='tt_leech'>Leechers: $leechers</span> ";
  198. $htmlcode.="</li>";
  199. }
  200. }
  201. $htmlcode.="</ol>";
  202. $htmlcode.=$options['html_after'];
  203. return $htmlcode;
  204. }
  205. function generateTrackerHTMLasTable() {
  206. $options = get_option('21h_torrent_tracker');
  207. $torrents = ttdb_select_all();
  208. $htmlcode = $options['html_before'];
  209. $htmlcode.= $options['html_tracker_title'];
  210. $htmlcode.="<table border='1' cellspacing='1'>";
  211. $htmlcode.="<tr><th>Title</th><th>Size (Mb)</th>";
  212. if (isset($options['showseed'])) $htmlcode.="<th>Seeders</th>";
  213. if (isset($options['showleech'])) $htmlcode.="<th>Leechers</th>";
  214. $htmlcode.="</tr>";
  215. foreach ($torrents as $torrent) {
  216. if (time() - strtotime($torrent['cached']) > $options['cachetime']) {
  217. $scrape_url = $torrent['scrape'] . '?info_hash=' . urlencode(base64_decode($torrent['sha1']));
  218. if (($torrent_file_contents = @file_get_contents($scrape_url)) === FALSE) {
  219. //tracker not available
  220. } else {
  221. $sha1 = base64_decode($torrent['sha1']);
  222. $scrape_data = bdecode($torrent_file_contents);
  223. if (isset($scrape_data['files'][$sha1]['complete'])) {
  224. $seeders = $scrape_data['files'][$sha1]['complete'];
  225. $leechers = $scrape_data['files'][$sha1]['incomplete'];
  226. } else {
  227. $seeders = $scrape_data['complete'];
  228. $leechers = $scrape_data['incomplete'];
  229. }
  230. $data = array(
  231. 'seeders' => $seeders,
  232. 'leechers' => $leechers,
  233. 'cached' => date('Y-m-d H:i:s')
  234. );
  235. ttdb_update($torrent['file_id'], $data);
  236. $htmlcode.="<tr>";
  237. $htmlcode.="<td><a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a></td>";
  238. $htmlcode.="<td>" . $torrent['size_mb'] . "</td>";
  239. if (isset($options['showseed'])) $htmlcode.="<td>$seeders</td>";
  240. if (isset($options['showleech'])) $htmlcode.="<td>$leechers</td>";
  241. $htmlcode.="</tr>";
  242. }
  243. } else {
  244. $sha1 = base64_decode($torrent['sha1']);
  245. $scrape_data = bdecode($torrent_file_contents);
  246. $seeders = $torrent['seeders'];
  247. $leechers = $torrent['leechers'];
  248. $htmlcode.="<tr>";
  249. $htmlcode.="<td><a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a></td>";
  250. $htmlcode.="<td>" . $torrent['size_mb'] . "</td>";
  251. if (isset($options['showseed'])) $htmlcode.="<td>$seeders</td>";
  252. if (isset($options['showleech'])) $htmlcode.="<td>$leechers</td>";
  253. $htmlcode.="</tr>";
  254. }
  255. }
  256. $htmlcode.="</table>";
  257. $htmlcode.=$options['html_after'];
  258. return $htmlcode;
  259. }
  260. function generateTrackerPostHTML() {
  261. $post_ID = $GLOBALS['post']->ID;
  262. $options = get_option('21h_torrent_tracker');
  263. $htmlcode = $options['html_before'];
  264. $htmlcode.= $options['html_in_post_tracker_title'];
  265. $htmlcode.="<ol>";
  266. $torrents_in_post = search_for_torrents($post_ID);
  267. foreach ($torrents_in_post as $torrent_in_post) {
  268. if (db_torrent_exists($db, $torrent_in_post['file_id']) > 0) {
  269. //get only registered in DB torrent files
  270. $torrent = ttdb_select_id($torrent_in_post['file_id']);
  271. if (time() - strtotime($torrent['cached']) > $options['cachetime']) {
  272. $scrape_url = $torrent['scrape'] . '?info_hash=' . urlencode(base64_decode($torrent['sha1']));
  273. if (($torrent_file_contents = @file_get_contents($scrape_url)) === FALSE) {
  274. //tracker not available
  275. } else {
  276. $sha1 = base64_decode($torrent['sha1']);
  277. $scrape_data = bdecode($torrent_file_contents);
  278. if (isset($scrape_data['files'][$sha1]['complete'])) {
  279. $seeders = $scrape_data['files'][$sha1]['complete'];
  280. $leechers = $scrape_data['files'][$sha1]['incomplete'];
  281. } else {
  282. $seeders = $scrape_data['complete'];
  283. $leechers = $scrape_data['incomplete'];
  284. }
  285. $data = array(
  286. 'seeders' => $seeders,
  287. 'leechers' => $leechers,
  288. 'cached' => date('Y-m-d H:i:s')
  289. );
  290. ttdb_update($torrent['file_id'], $data);
  291. $htmlcode.="<li>";
  292. $htmlcode.="<a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a>: ";
  293. $htmlcode.="(" . $torrent['size_mb'] . " Мб) ";
  294. $htmlcode.="Раздают: " . $seeders;
  295. $htmlcode.=" <span style = 'color:silver'>Неполностью: " . $leechers . "</span>";
  296. $htmlcode.="</li>";
  297. }
  298. } else {
  299. $sha1 = base64_decode($torrent['sha1']);
  300. $scrape_data = bdecode($torrent_file_contents);
  301. $seeders = $torrent['seeders'];
  302. $leechers = $torrent['leechers'];
  303. $htmlcode.="<li>";
  304. $htmlcode.="<a href = '" . $torrent['url'] . "' > " . $torrent['title'] . "</a>: ";
  305. $htmlcode.="(" . $torrent['size_mb'] . " Мб) ";
  306. $htmlcode.="Раздают: " . $seeders;
  307. $htmlcode.=" <span style = 'color:silver'>Неполностью: " . $leechers . "</span>";
  308. $htmlcode.="</li>";
  309. }
  310. }
  311. }
  312. $htmlcode.="</ol>";
  313. $htmlcode.=$options['html_after'];
  314. return $htmlcode;
  315. }
  316. function replace_content(
  317. $content) {
  318. $content = str_replace('<!-- TorrentTracker -->', selectFormOfView(), $content);
  319. $content = str_replace('<!-- TorrentTrackerPost -->', generateTrackerPostHTML(), $content);
  320. return $content;
  321. }
  322. add_action('add_attachment', 'torrent_add_attachment');
  323. add_action('delete_attachment', 'torrent_delete_attachment');
  324. add_action('edit_attachment', 'torrent_edit_attachment');
  325. add_filter('the_content', 'replace_content');
  326. register_activation_hook(__FILE__, 'ttdb_install');
  327. include "torrent_tracker_adm.php";
  328. ?>