PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/dead/takeupload.php

https://bitbucket.org/nexea/x00n
PHP | 271 lines | 165 code | 47 blank | 59 comment | 41 complexity | 8218a87cb23f23d4e3e167781a258b26 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?
  2. require_once("include/benc.php");
  3. require_once("include/bittorrent.php");
  4. require "rconpasswords.php";
  5. ini_set("upload_max_filesize",$max_torrent_size);
  6. function bark($msg) {
  7. genbark($msg, "Upload failed!");
  8. }
  9. dbconn();
  10. loggedinorreturn();
  11. if (get_user_class() < UC_UPLOADER)
  12. die;
  13. foreach(explode(":","descr:type:name") as $v) {
  14. if (!isset($_POST[$v]))
  15. bark("missing form data");
  16. }
  17. if (!isset($_FILES["file"]))
  18. bark("missing form data");
  19. $f = $_FILES["file"];
  20. $fname = unesc($f["name"]);
  21. if (empty($fname))
  22. bark("Empty filename!");
  23. $descr = unesc($_POST["descr"]);
  24. if (!$descr)
  25. bark("You must enter a description!");
  26. $catid = (0 + $_POST["type"]);
  27. if (!is_valid_id($catid))
  28. bark("You must select a category to put the torrent in!");
  29. if (!validfilename($fname))
  30. bark("Invalid filename!");
  31. if (!preg_match('/^(.+)\.torrent$/si', $fname, $matches))
  32. bark("Invalid filename (not a .torrent).");
  33. $shortfname = $torrent = $matches[1];
  34. if (!empty($_POST["name"]))
  35. $torrent = unesc($_POST["name"]);
  36. $tmpname = $f["tmp_name"];
  37. if (!is_uploaded_file($tmpname))
  38. bark("eek");
  39. if (!filesize($tmpname))
  40. bark("Empty file!");
  41. $dict = bdec_file($tmpname, $max_torrent_size);
  42. if (!isset($dict))
  43. bark("What the hell did you upload? This is not a bencoded file!");
  44. function dict_check($d, $s) {
  45. if ($d["type"] != "dictionary")
  46. bark("not a dictionary");
  47. $a = explode(":", $s);
  48. $dd = $d["value"];
  49. $ret = array();
  50. foreach ($a as $k) {
  51. unset($t);
  52. if (preg_match('/^(.*)\((.*)\)$/', $k, $m)) {
  53. $k = $m[1];
  54. $t = $m[2];
  55. }
  56. if (!isset($dd[$k]))
  57. bark("dictionary is missing key(s)");
  58. if (isset($t)) {
  59. if ($dd[$k]["type"] != $t)
  60. bark("invalid entry in dictionary");
  61. $ret[] = $dd[$k]["value"];
  62. }
  63. else
  64. $ret[] = $dd[$k];
  65. }
  66. return $ret;
  67. }
  68. function dict_get($d, $k, $t) {
  69. if ($d["type"] != "dictionary")
  70. bark("not a dictionary");
  71. $dd = $d["value"];
  72. if (!isset($dd[$k]))
  73. return;
  74. $v = $dd[$k];
  75. if ($v["type"] != $t)
  76. bark("invalid dictionary entry type");
  77. return $v["value"];
  78. }
  79. list($ann, $info) = dict_check($dict, "announce(string):info");
  80. list($dname, $plen, $pieces) = dict_check($info, "name(string):piece length(integer):pieces(string)");
  81. if (!in_array($ann, $announce_urls, 1))
  82. bark("invalid announce url! must be <b>" . $announce_urls[0] . "</b>");
  83. if (strlen($pieces) % 20 != 0)
  84. bark("invalid pieces");
  85. $filelist = array();
  86. $totallen = dict_get($info, "length", "integer");
  87. if (isset($totallen)) {
  88. $filelist[] = array($dname, $totallen);
  89. $type = "single";
  90. }
  91. else {
  92. $flist = dict_get($info, "files", "list");
  93. if (!isset($flist))
  94. bark("missing both length and files");
  95. if (!count($flist))
  96. bark("no files");
  97. $totallen = 0;
  98. foreach ($flist as $fn) {
  99. list($ll, $ff) = dict_check($fn, "length(integer):path(list)");
  100. $totallen += $ll;
  101. $ffa = array();
  102. foreach ($ff as $ffe) {
  103. if ($ffe["type"] != "string")
  104. bark("filename error");
  105. $ffa[] = $ffe["value"];
  106. }
  107. if (!count($ffa))
  108. bark("filename error");
  109. $ffe = implode("/", $ffa);
  110. $filelist[] = array($ffe, $ll);
  111. }
  112. $type = "multi";
  113. }
  114. $infohash = pack("H*", sha1($info["string"]));
  115. // Replace punctuation characters with spaces
  116. $torrent = str_replace("_", " ", $torrent);
  117. $ret = mysql_query("INSERT INTO torrents (search_text, filename, owner, visible, info_hash, name, size, numfiles, type, descr, ori_descr, category, save_as, added, last_action) VALUES (" .
  118. implode(",", array_map("sqlesc", array(searchfield("$shortfname $dname $torrent"), $fname, $CURUSER["id"], "no", $infohash, $torrent, $totallen, count($filelist), $type, $descr, $descr, 0 + $_POST["type"], $dname))) .
  119. ", '" . get_date_time() . "', '" . get_date_time() . "')");
  120. if (!$ret) {
  121. if (mysql_errno() == 1062)
  122. bark("torrent already uploaded!");
  123. bark("mysql puked: ".mysql_error());
  124. }
  125. $id = mysql_insert_id();
  126. @mysql_query("DELETE FROM files WHERE torrent = $id");
  127. foreach ($filelist as $file) {
  128. @mysql_query("INSERT INTO files (torrent, filename, size) VALUES ($id, ".sqlesc($file[0]).",".$file[1].")");
  129. }
  130. move_uploaded_file($tmpname, "$torrent_dir/$id.torrent");
  131. write_log("Torrent $id ($torrent) was uploaded by " . $CURUSER["username"]);
  132. /* Game server notif */
  133. $f = fsockopen("udp://62.212.84.221", 28960);
  134. socket_set_timeout($f, 1);
  135. fwrite($f, "\xFF\xFF\xFF\xFFrcon $rconpassword say Torrent uploaded: $torrent\n");
  136. fread($f, 8192);
  137. fclose($f);
  138. /* RSS feeds */
  139. if (($fd1 = @fopen("rss.xml", "w")) && ($fd2 = fopen("rssdd.xml", "w")))
  140. {
  141. $cats = "";
  142. $res = mysql_query("SELECT id, name FROM categories");
  143. while ($arr = mysql_fetch_assoc($res))
  144. $cats[$arr["id"]] = $arr["name"];
  145. $s = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<rss version=\"0.91\">\n<channel>\n" .
  146. "<title>TorrentBits</title>\n<description>0-week torrents</description>\n<link>$DEFAULTBASEURL/</link>\n";
  147. @fwrite($fd1, $s);
  148. @fwrite($fd2, $s);
  149. $r = mysql_query("SELECT id,name,descr,filename,category FROM torrents ORDER BY added DESC LIMIT 15") or sqlerr(__FILE__, __LINE__);
  150. while ($a = mysql_fetch_assoc($r))
  151. {
  152. $cat = $cats[$a["category"]];
  153. $s = "<item>\n<title>" . htmlspecialchars($a["name"] . " ($cat)") . "</title>\n" .
  154. "<description>" . htmlspecialchars($a["descr"]) . "</description>\n";
  155. @fwrite($fd1, $s);
  156. @fwrite($fd2, $s);
  157. @fwrite($fd1, "<link>$DEFAULTBASEURL/details.php?id=$a[id]&amp;hit=1</link>\n</item>\n");
  158. $filename = htmlspecialchars($a["filename"]);
  159. @fwrite($fd2, "<link>$DEFAULTBASEURL/download/$a[id]/$filename</link>\n</item>\n");
  160. }
  161. $s = "</channel>\n</rss>\n";
  162. @fwrite($fd1, $s);
  163. @fwrite($fd2, $s);
  164. @fclose($fd1);
  165. @fclose($fd2);
  166. }
  167. /* Email notifs */
  168. /*******************
  169. $res = mysql_query("SELECT name FROM categories WHERE id=$catid") or sqlerr();
  170. $arr = mysql_fetch_assoc($res);
  171. $cat = $arr["name"];
  172. $res = mysql_query("SELECT email FROM users WHERE enabled='yes' AND notifs LIKE '%[cat$catid]%'") or sqlerr();
  173. $uploader = $CURUSER['username'];
  174. $size = mksize($totallen);
  175. $description = ($html ? strip_tags($descr) : $descr);
  176. $body = <<<EOD
  177. A new torrent has been uploaded.
  178. Name: $torrent
  179. Size: $size
  180. Category: $cat
  181. Uploaded by: $uploader
  182. Description
  183. -------------------------------------------------------------------------------
  184. $description
  185. -------------------------------------------------------------------------------
  186. You can use the URL below to download the torrent (you may have to login).
  187. $DEFAULTBASEURL/details.php?id=$id&hit=1
  188. --
  189. $SITENAME
  190. EOD;
  191. $to = "";
  192. $nmax = 100; // Max recipients per message
  193. $nthis = 0;
  194. $ntotal = 0;
  195. $total = mysql_num_rows($res);
  196. while ($arr = mysql_fetch_row($res))
  197. {
  198. if ($nthis == 0)
  199. $to = $arr[0];
  200. else
  201. $to .= "," . $arr[0];
  202. ++$nthis;
  203. ++$ntotal;
  204. if ($nthis == $nmax || $ntotal == $total)
  205. {
  206. if (!mail("Multiple recipients <$SITEEMAIL>", "New torrent - $torrent", $body,
  207. "From: $SITEEMAIL\r\nBcc: $to", "-f$SITEEMAIL"))
  208. stderr("Error", "Your torrent has been been uploaded. DO NOT RELOAD THE PAGE!\n" .
  209. "There was however a problem delivering the e-mail notifcations.\n" .
  210. "Please let an administrator know about this error!\n");
  211. $nthis = 0;
  212. }
  213. }
  214. *******************/
  215. header("Location: $DEFAULTBASEURL/details.php?id=$id&uploaded=1");
  216. ?>