PageRenderTime 78ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/podpress/podpress_torrent_functions.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 524 lines | 493 code | 25 blank | 6 comment | 42 complexity | a37a04b8796e0c0194e1845bba2df79e MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?
  2. function installTables() {
  3. GLOBAL $dbhost, $dbuser, $dbpass, $database, $nav;
  4. require_once("config.php");
  5. require_once("funcsv2.php");
  6. if ($nav == "install") {
  7. $makenamemap= 'CREATE TABLE BTPHP_namemap (info_hash char(40) NOT NULL default "", filename varchar(250) NOT NULL default "", url varchar(250) NOT NULL default "", info varchar(250) NOT NULL default "", PRIMARY KEY(info_hash))';
  8. $makesummary = 'CREATE TABLE BTPHP_summary (info_hash char(40) NOT NULL default "", dlbytes bigint unsigned NOT NULL default 0, seeds int unsigned NOT NULL default 0, leechers int unsigned NOT NULL default 0, finished int unsigned NOT NULL default 0, lastcycle int unsigned NOT NULL default "0", lastSpeedCycle int unsigned NOT NULL DEFAULT "0", speed bigint unsigned NOT NULL default 0, PRIMARY KEY (info_hash))';
  9. $maketimestamps = 'CREATE TABLE BTPHP_timestamps (info_hash char(40) not null, sequence int unsigned not null auto_increment, bytes bigint unsigned not null, delta smallint unsigned not null, primary key(sequence), key sorting (info_hash))';
  10. $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Can't connect to database: ".mysql_error());
  11. mysql_select_db($database) or die("Can't select database: ".mysql_error());
  12. mysql_query($makesummary) or die("Can't make the summary table: ".mysql_error());
  13. mysql_query($makenamemap) or die("Can't make the namemap table: ".mysql_error());
  14. mysql_query($maketimestamps) or die("Can't make the timestamps table: ".mysql_error());
  15. print "<p>Tables installed in database, your tracker should now function.</p>";
  16. }
  17. else
  18. print "</p>Tracker tables not found in the database, click <a href=\"?nav=install\">here</a> to install them.</p>";
  19. }
  20. function cleanUp () {
  21. GLOBAL $dbhost, $dbuser, $dbpass, $database;
  22. require_once("config.php");
  23. require_once("funcsv2.php");
  24. $summaryupdate = array();
  25. // Non-persistant: we lock tables!
  26. $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("<p class=\"error\">Tracker error: can't connect to database - ".mysql_error() . "</p>");
  27. mysql_select_db($database) or die("<p class=\"error\">Tracker error: can't open database $database - ".mysql_error() . "</p>");
  28. if (isset($_GET["nolock"]))
  29. $locking = false;
  30. else
  31. $locking = true;
  32. // Assumes success
  33. if ($locking)
  34. quickQuery("LOCK TABLES BTPHP_summary WRITE, BTPHP_namemap READ");
  35. ?>
  36. <table class="torrentlist" cellspacing="5">
  37. <!-- Column Headers -->
  38. <tr>
  39. <th>Name/Hash</th>
  40. <th>Leechs</th>
  41. <th>Seeds</th>
  42. <th>Bytes Transfered</th>
  43. <th>Stale clients</th>
  44. <th>Peer Cache</th>
  45. </tr>
  46. <tr>
  47. <td colspan="5" class="nodata"></td>
  48. </tr>
  49. <?php
  50. $results = mysql_query("SELECT BTPHP_summary.info_hash, seeds, leechers, dlbytes, BTPHP_namemap.filename FROM BTPHP_summary LEFT JOIN BTPHP_namemap ON BTPHP_summary.info_hash = BTPHP_namemap.info_hash");
  51. $i = 0;
  52. while ($row = mysql_fetch_row($results))
  53. {
  54. $writeout = "row" . $i % 2;
  55. list($hash, $seeders, $leechers, $bytes, $filename) = $row;
  56. if ($locking)
  57. {
  58. if ($GLOBALS["peercaching"])
  59. quickQuery("LOCK TABLES x$hash WRITE, y$hash WRITE, summary WRITE");
  60. else
  61. quickQuery("LOCK TABLES x$hash WRITE, summary WRITE");
  62. }
  63. $results2 = mysql_query("SELECT status, COUNT(status) from x$hash GROUP BY status");
  64. echo "<tr class=\"$writeout\"><td>";
  65. if (!is_null($filename))
  66. echo $filename;
  67. else
  68. echo $hash;
  69. echo "</td>";
  70. if (!$results2)
  71. {
  72. echo "<td colspan=\"4\">Unable to process: ".mysql_error()."</td></tr>";
  73. continue;
  74. }
  75. $counts = array();
  76. while ($row = mysql_fetch_row($results2))
  77. $counts[$row[0]] = $row[1];
  78. if (!isset($counts["leecher"]))
  79. $counts["leecher"] = 0;
  80. if (!isset($counts["seeder"]))
  81. $counts["seeder"] = 0;
  82. if ($counts["leecher"] != $leechers)
  83. {
  84. quickQuery("UPDATE BTPHP_summary SET leechers=".$counts["leecher"]." WHERE info_hash=\"$hash\"");
  85. echo "<td>$leechers -> ".$counts["leecher"]."</td>";
  86. }
  87. else
  88. echo "<td align=center>$leechers</td>";
  89. if ($counts["seeder"] != $seeders)
  90. {
  91. quickQuery("UPDATE BTPHP_summary SET seeds=".$counts["seeder"]." WHERE info_hash=\"$hash\"");
  92. echo "<td align=center>$seeders -> ".$counts["seeder"]."</td>";
  93. }
  94. else
  95. echo "<td align=center>$seeders</td>";
  96. // echo "<td align=center>$finished</td>";
  97. if ($bytes < 0)
  98. {
  99. quickQuery("UPDATE BTPHP_summary SET dlbytes=0 WHERE info_hash=\"$hash\"");
  100. echo "<td>$bytes -> Zero</td>";
  101. }
  102. else
  103. echo "<td align=center>". round($bytes/1048576/1024,3) ." GB</td>";
  104. myTrashCollector($hash, $report_interval, time(), $writeout);
  105. echo "</td><td>";
  106. if ($GLOBALS["peercaching"])
  107. {
  108. $result = mysql_query("SELECT x$hash.sequence FROM x$hash LEFT JOIN y$hash ON x$hash.sequence=y$hash.sequence WHERE y$hash.sequence IS NULL") or die(mysql_error());
  109. if (mysql_num_rows($result) > 0)
  110. {
  111. echo "Added ", mysql_num_rows($result);
  112. $row = array();
  113. while ($data = mysql_fetch_row($result))
  114. $row[] = "sequence=\"${data[0]}\"";
  115. $where = implode(" OR ", $row);
  116. $query = mysql_query("SELECT * FROM x$hash WHERE $where");
  117. while ($row = mysql_fetch_assoc($query))
  118. {
  119. $compact = mysql_escape_string(pack('Nn', ip2long($row["ip"]), $row["port"]));
  120. $peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . '7:peer id20:' . hex2bin($row["peer_id"]) . "4:porti{$row["port"]}e");
  121. $no_peerid = mysql_escape_string('2:ip' . strlen($row["ip"]) . ':' . $row["ip"] . "4:porti{$row["port"]}e");
  122. mysql_query("INSERT INTO y$hash SET sequence=\"{$row["sequence"]}\", compact=\"$compact\", with_peerid=\"$peerid\", without_peerid=\"$no_peerid\"");
  123. }
  124. }
  125. else
  126. echo "Added: none";
  127. $result = mysql_query("SELECT y$hash.sequence FROM y$hash LEFT JOIN x$hash ON y$hash.sequence=x$hash.sequence WHERE x$hash.sequence IS NULL");
  128. if (mysql_num_rows($result) > 0)
  129. {
  130. echo ", Deleted: ",mysql_num_rows($result);
  131. $row = array();
  132. while ($data = mysql_fetch_row($result))
  133. $row[] = "sequence=\"${data[0]}\"";
  134. $where = implode(" OR ", $row);
  135. $query = mysql_query("DELETE FROM y$hash WHERE $where");
  136. }
  137. else
  138. echo "<br>Deleted: none";
  139. }
  140. else
  141. echo "N/A";
  142. echo "</td>";
  143. echo "</tr>\n";
  144. $i ++;
  145. // Disabled because it's kinda not that important.
  146. // quickQuery("OPTIMIZE TABLE x$hash");
  147. if ($locking)
  148. quickQuery("UNLOCK TABLES");
  149. // Finally, it's time to do stuff to the summary table.
  150. if (!empty($summaryupdate))
  151. {
  152. $stuff = "";
  153. foreach ($summaryupdate as $column => $value)
  154. {
  155. $stuff .= ', '.$column. ($value[1] ? "=" : "=$column+") . $value[0];
  156. }
  157. mysql_query("UPDATE BTPHP_summary SET ".substr($stuff, 1)." WHERE info_hash=\"$hash\"");
  158. $summaryupdate = array();
  159. }
  160. }
  161. }
  162. function myTrashCollector($hash, $timeout, $now, $writeout)
  163. {
  164. $peers = loadLostPeers($hash, $timeout);
  165. for ($i=0; $i < $peers["size"]; $i++)
  166. killPeer($peers[$i]["peer_id"], $hash, $peers[$i]["bytes"], $peers[$i]);
  167. if ($i != 0)
  168. echo "<td>Removed $i</td>";
  169. else
  170. echo "<td>Removed 0</td>";
  171. quickQuery("UPDATE BTPHP_summary SET lastcycle='$now' WHERE info_hash='$hash'");
  172. }
  173. function delTorrent() {
  174. GLOBAL $dbhost, $dbuser, $dbpass, $database;
  175. GLOBAL $_POST, $_FILES;
  176. require_once("config.php");
  177. require_once("funcsv2.php");
  178. $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("<p class=\"error\">Couldn't connect to database. contact the administrator</p>");
  179. mysql_select_db($database) or die("Error selecting database.");
  180. print " <form enctype=\"multipart/form-data\" method=\"post\" action=\"".$_SERVER['PHP_SELF']."?nav=delete\"> ";
  181. foreach ($_POST as $left => $right)
  182. {
  183. if (strlen($left) == 41 && $left[0] == 'x')
  184. {
  185. if (!stristr($right,'y') || !verifyHash(substr($left, 1)))
  186. continue;
  187. $hash = substr($left, 1);
  188. @mysql_query("DELETE FROM BTPHP_summary WHERE info_hash=\"$hash\"");
  189. @mysql_query("DELETE FROM BTPHP_namemap WHERE info_hash=\"$hash\"");
  190. @mysql_query("DELETE FROM BTPHP_timestamps WHERE info_hash=\"$hash\"");
  191. @mysql_query("DROP TABLE y$hash");
  192. @mysql_query("DROP TABLE x$hash");
  193. }
  194. }
  195. ?>
  196. <h1>Torrents</h1>
  197. <table class="torrentlist" cellspacing="5" cellpadding=0 border=0>
  198. <tr>
  199. <th>Name/Hash</th>
  200. <th>Seeds</th>
  201. <th>Leeches</th>
  202. <th>Completed</th>
  203. <th>Bytes Transfered</th>
  204. <th>Delete?</th>
  205. </tr>
  206. <tr>
  207. <td style="background-color: #ffffff" colspan="6"></td>
  208. </tr>
  209. <?php
  210. $results = mysql_query("SELECT BTPHP_summary.info_hash, BTPHP_summary.seeds, BTPHP_summary.leechers, format(BTPHP_summary.finished,0), format(BTPHP_summary.dlbytes/1073741824,3),BTPHP_namemap.filename FROM BTPHP_summary LEFT JOIN BTPHP_namemap ON BTPHP_summary.info_hash = BTPHP_namemap.info_hash ORDER BY BTPHP_namemap.filename")
  211. or die(mysql_error());
  212. $i = 0;
  213. while ($data = mysql_fetch_row($results)) {
  214. $writeout = "row" . $i % 2;
  215. $hash = $data[0];
  216. if (is_null($data[5]))
  217. $data[5] = $data[0];
  218. if (strlen($data[5]) == 0)
  219. $data[5] = $data[0];
  220. echo "<tr class=\"$writeout\">\n";
  221. echo "\t<td>".$data[5]."</td>\n";
  222. for ($j=1; $j < 4; $j++)
  223. echo "\t<td align=center>$data[$j]</td>\n";
  224. echo "\t<td align=center>$data[4] GB</td>\n";
  225. echo "\t<td align=center><input type=\"checkbox\" name=\"x$hash\" value=\"y\" /></td>\n";
  226. echo "</tr>\n";
  227. $i++;
  228. }
  229. ?>
  230. </table>
  231. <p class="error">Warning: there is <u>no confirmation</u> when deleting torrents.<br>
  232. .torrent files will not be deleted from server directory, only removed from the tracker.<br>
  233. Clicking this button is final.</p>
  234. <p class="center"><input type="submit" value="Delete" /></p>
  235. </form>
  236. <? // End Function
  237. }
  238. function doCrash($msg)
  239. {
  240. echo "</table></table><p class=\"error\">Script error: $msg</p></body></html>";
  241. exit(1);
  242. }
  243. function clean($input)
  244. {
  245. if (get_magic_quotes_gpc())
  246. return stripslashes($input);
  247. return $input;
  248. }
  249. function addTorrent() {
  250. GLOBAL $dbhost, $dbuser, $dbpass, $database;
  251. GLOBAL $_POST, $_FILES;
  252. require_once ("funcsv2.php");
  253. require_once ("BDecode.php");
  254. require_once ("BEncode.php");
  255. $hash = strtolower($_POST["hash"]);
  256. $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("<p class=\"error\">Couldn't connect to database. contact the administrator</p>");
  257. mysql_select_db($database) or die("<p class=\"error\">Can't open the database.</p>");
  258. if (isset($_FILES["torrent"]))
  259. {
  260. if ($_FILES["torrent"]["error"] != 4)
  261. {
  262. $fd = fopen($_FILES["torrent"]["tmp_name"], "rb") or die("<p class=\"error\">File upload error 1</p>\n");
  263. is_uploaded_file($_FILES["torrent"]["tmp_name"]) or die("<p class=\"error\">File upload error 2</p>\n");
  264. $alltorrent = fread($fd, filesize($_FILES["torrent"]["tmp_name"]));
  265. $array = BDecode($alltorrent);
  266. if (!$array)
  267. {
  268. echo "<p class=\"error\">There was an error handling your uploaded torrent. The parser didn't like it.</p>";
  269. endOutput();
  270. exit;
  271. }
  272. $hash = @sha1(BEncode($array["info"]));
  273. fclose($fd);
  274. unlink($_FILES["torrent"]["tmp_name"]);
  275. }
  276. }
  277. if (isset($_POST["filename"]))
  278. $filename= clean($_POST["filename"]);
  279. else
  280. $filename = "";
  281. if (isset($_POST["url"]))
  282. $url = clean($_POST["url"]);
  283. else
  284. $url = "";
  285. if (isset($_POST["info"]))
  286. $info = clean($_POST["info"]);
  287. else
  288. $info = "";
  289. if (isset($_POST["autoset"])) {
  290. if (strcmp($_POST["autoset"], "enabled") == 0)
  291. {
  292. if (strlen($filename) == 0 && isset($array["info"]["name"]))
  293. $filename = $array["info"]["name"];
  294. if (strlen($info) == 0 && isset($array["info"]["piece length"]))
  295. {
  296. $info = $array["info"]["piece length"] / 1024 * (strlen($array["info"]["pieces"]) / 20) /1024;
  297. $info = round($info, 2) . " MB";
  298. if (isset($array["comment"]))
  299. $info .= " - ".$array["comment"];
  300. }
  301. }
  302. $filename = mysql_escape_string($filename);
  303. $url = mysql_escape_string($url);
  304. $info = mysql_escape_string($info);
  305. if ((strlen($hash) != 40) || !verifyHash($hash))
  306. {
  307. echo("<p class=\"error\">Error: Info hash must be exactly 40 hex bytes.</p>");
  308. endOutput();
  309. }
  310. $query = "INSERT INTO BTPHP_namemap (info_hash, filename, url, info) VALUES (\"$hash\", \"$filename\", \"$url\", \"$info\")";
  311. $status = makeTorrent($hash, true);
  312. quickQuery($query);
  313. if ($status)
  314. echo "<p class=\"error\">Torrent was added successfully.</p>";
  315. else
  316. echo "<p class=\"error\">There were some errors. Check if this torrent had been added previously.</p>";
  317. }
  318. endOutput();
  319. }
  320. function endOutput() {
  321. // Switch out of PHP mode. Much easier to output a large wad of HTML.
  322. ?>
  323. <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?nav=add">
  324. <table>
  325. <tr>
  326. <td class="right">Torrent file:</td>
  327. <td class="left"><?php
  328. if (function_exists("sha1"))
  329. echo "<input type=\"file\" name=\"torrent\" size=\"30\"/>";
  330. else
  331. echo '<i>File uploading not available - no SHA1 function.</i>';
  332. ?></td>
  333. </tr>
  334. <?php if (function_exists("sha1"))
  335. echo "<tr><td class=\"center\" colspan=\"2\"><input type=\"checkbox\" name=\"autoset\" value=\"enabled\" checked=\"checked\" /> Fill in fields below automatically using data from the torrent file.</td></tr>\n"; ?>
  336. <tr>
  337. <td class="right">Info Hash:</td>
  338. <td class="left"><input type="text" name="hash" size="40"/></td>
  339. </tr>
  340. <tr>
  341. <td class="right">File name (optional): </td>
  342. <td class="left"><input type="text" name="filename" size="50" maxlength="200"/></td>
  343. </tr>
  344. <tr>
  345. <td class="right">Torrent's URL (optional): </td>
  346. <td class="left"><input type="text" name="url" size="50" maxlength="200"/></td>
  347. </tr>
  348. <tr>
  349. <td class="right">Short description(optional): </td>
  350. <td class="left"><input type="text" name="info" size="50" maxlength="200"/></td>
  351. </tr>
  352. <tr>
  353. <td class="right"><input type="submit" value="Create"></td>
  354. <td class="left"><input type="reset" value="Clear Settings"/></td>
  355. </tr>
  356. </table>
  357. </form>
  358. </div>
  359. </body></html>
  360. <?php
  361. exit;
  362. }
  363. function myStats() {
  364. GLOBAL $dbhost, $dbuser, $dbpass, $database;
  365. $scriptname = $_SERVER["PHP_SELF"];
  366. if (!isset($GLOBALS["countbytes"]))
  367. $GLOBALS["countbytes"] = true;
  368. ?>
  369. <table>
  370. <tr>
  371. <?php
  372. if (!isset($_GET["activeonly"]))
  373. echo "<td><a href=\"$scriptname?activeonly=yes\">Show only active torrents</a></td>\n";
  374. else echo "<td><a href=\"$scriptname\">Show all torrents</a></td>\n";
  375. if (!isset($_GET["seededonly"]))
  376. echo "<td style=\"text-align: right;\"><a href=\"$scriptname?seededonly=yes\">Show only seeded torrents</a></td>\n";
  377. else echo "<td style=\"text-align: right;\"><a href=\"$scriptname\">Show all torrents</a></td>\n";
  378. ?>
  379. </tr>
  380. <tr>
  381. <td colspan="2">
  382. <table class="torrentlist">
  383. <!-- Column Headers -->
  384. <tr>
  385. <th>Name/Info Hash</th><th>Seeds</th><th>Leeches</th><th>Completed D/Ls</th>
  386. <?php
  387. // Bytes mode off? Ignore the columns
  388. if ($GLOBALS["countbytes"])
  389. echo '<th>Bytes Transferred</th><th>Speed</th>';
  390. ?>
  391. </tr>
  392. <?php
  393. $db = mysql_connect($dbhost, $dbuser, $dbpass) or doCrash("Tracker error: can't connect to database - ".mysql_error());
  394. mysql_select_db($database) or doCrash("Tracker error: can't open database $database - ".mysql_error());
  395. if (isset($_GET["seededonly"]))
  396. $where = " WHERE seeds > 0";
  397. else if (isset($_GET["activeonly"]))
  398. $where = " WHERE leechers+seeds > 0";
  399. else
  400. $where = " ";
  401. // Grab dummy column for dlbytes so we can skip doing format()
  402. if ($GLOBALS["countbytes"])
  403. $bytes = 'format(BTPHP_summary.dlbytes/1073741824,3)';
  404. else
  405. $bytes = '0';
  406. $query = "SELECT BTPHP_summary.info_hash, BTPHP_summary.seeds, BTPHP_summary.leechers, format(BTPHP_summary.finished,0), $bytes, BTPHP_namemap.filename, BTPHP_namemap.url, BTPHP_namemap.info, BTPHP_summary.speed FROM BTPHP_summary LEFT JOIN BTPHP_namemap ON BTPHP_summary.info_hash = BTPHP_namemap.info_hash $where ORDER BY BTPHP_namemap.filename";
  407. $results = mysql_query($query) or doCrash("Can't do SQL query - ".mysql_error());
  408. $i = 0;
  409. while ($data = mysql_fetch_row($results)) {
  410. // NULLs are such a pain at times. isset($nullvar) == false
  411. if (is_null($data[5]))
  412. $data[5] = $data[0];
  413. if (is_null($data[6]))
  414. $data[6] = "";
  415. if (is_null($data[7]))
  416. $data[7]="";
  417. if (strlen($data[5]) == 0)
  418. $data[5]=$data[0];
  419. $myhash = $data[0];
  420. $writeout = "row" . $i % 2;
  421. echo "<tr class=\"$writeout\">\n";
  422. echo "\t<td align=center>";
  423. if (strlen($data[6]) > 0)
  424. echo "<a href=\"${data[6]}\">${data[5]}</a>";
  425. else
  426. echo $data[5];
  427. if (strlen($data[7]) > 0)
  428. echo "<br/>(${data[7]})";
  429. echo "</td>\n";
  430. for ($j=1; $j < 4; $j++)
  431. echo "\t<td class=\"center\">$data[$j]</td>\n";
  432. if ($GLOBALS["countbytes"])
  433. {
  434. echo "\t<td align=center>$data[4] GB</td>\n";
  435. // The SPEED column calcultions.
  436. if ($data[8] <= 0)
  437. $speed = "Zero";
  438. else if ($data[8] > 2097152)
  439. $speed = round($data[8]/1048576,2) . " MB/sec";
  440. else
  441. $speed = round($data[8] / 1024, 2) . " KB/sec";
  442. echo "\t<td align=center>$speed</td>\n";
  443. }
  444. echo "</tr>\n";
  445. $i++;
  446. }
  447. if ($i == 0)
  448. echo "<tr class=\"row0\"><td style=\"text-align: center;\" colspan=\"6\">No data</td></tr>";
  449. ?>
  450. </table></td></tr>
  451. </table>
  452. <?
  453. // End Function
  454. };
  455. ?>