PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/tracker/scrape2.php

https://gitlab.com/rambeer/ppkbb3cker2
PHP | 415 lines | 335 code | 68 blank | 12 comment | 73 complexity | 5c0495a1697ab62b46263a55a7aee0c3 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package ppkBB3cker
  5. * @version $Id: scrape2.php 1.000 2009-11-09 13:36:00 PPK $
  6. * @copyright (c) 2009 PPK
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. error_reporting(0);
  11. @ini_set('register_globals', 0);
  12. @ini_set('magic_quotes_runtime', 0);
  13. @ini_set('magic_quotes_sybase', 0);
  14. function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get') ? date_default_timezone_set(@date_default_timezone_get()) : '';
  15. define('IN_PHPBB', true);
  16. define('IS_GUESTS', 1);
  17. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
  18. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  19. $tcachedir="{$phpbb_root_path}cache/";
  20. $tincludedir="{$phpbb_root_path}tracker/tinc/";
  21. if(isset($_SERVER['HTTP_ACCEPT_CHARSET'])/* || isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])*/ || isset($_SERVER['HTTP_REFERER'])/* || isset($_SERVER['HTTP_COOKIE'])*/ || isset($_SERVER['HTTP_X_REQUESTED_WITH']))//ut 3.2.2, iis
  22. {
  23. err('Unknown Client');
  24. }
  25. require($phpbb_root_path . 'config.'.$phpEx);
  26. if(!in_array($dbms, array('mysql', 'mysqli')))
  27. {
  28. err('Only mysql(i) supported');
  29. }
  30. $c=@mysql_connect($dbhost.($dbport ? ":{$dbport}" : ''), $dbuser, $dbpasswd);
  31. if(!$c)
  32. {
  33. err('Error connecting database: '.mysql_error().' ['.mysql_errno().']');
  34. }
  35. $s=@mysql_select_db($dbname, $c);
  36. if(!$s)
  37. {
  38. err('Error selecting database: '.mysql_error($c));
  39. }
  40. //my_sql_query("SET sql_mode='NO_UNSIGNED_SUBTRACTION'");
  41. my_sql_query("SET NAMES 'utf8'");
  42. unset($dbpasswd);
  43. define('TRACKER_TORRENTS_TABLE', $table_prefix . 'tracker_torrents');
  44. define('LOG_TABLE', $table_prefix . 'log');
  45. define('TRACKER_GUESTS_TABLE', $table_prefix . 'tracker_guests');
  46. $config=array();
  47. $cache_config=t_getcache('tracker_sconfig');
  48. if($cache_config===false)
  49. {
  50. include($tincludedir.'tsconf.'.$phpEx);
  51. }
  52. else
  53. {
  54. foreach($cache_config as $k => $v)
  55. {
  56. $config[$k]=$v;
  57. }
  58. unset($cache_config);
  59. }
  60. if(!$config['ppkbb_gscrape_enabled'][0])
  61. {
  62. err("Scrape functions disabled");
  63. }
  64. define('STRIP', (get_magic_quotes_gpc()) ? true : false);
  65. if($config['ppkbb_gtcallow_unregtorr'] && empty($config['ppkbb_gtcunregtorr_sessid']))
  66. {
  67. $config['ppkbb_gscrape_enabled'][1]=0;
  68. }
  69. if($config['ppkbb_gscrape_enabled'][1])
  70. {
  71. $session_id=STRIP ? @$_GET['passkey'] : mysql_real_escape_string(@$_GET['passkey'], $c);
  72. if(!$session_id)
  73. {
  74. err('Passkey not defined');
  75. }
  76. $sql="SELECT user_id FROM ".TRACKER_GUESTS_TABLE." WHERE session_id='{$session_id}' LIMIT 1";
  77. $result=my_sql_query($sql);
  78. $user=mysql_fetch_array($result);
  79. mysql_free_result($result);
  80. $userid=@$user['user_id'];
  81. if(!$userid)
  82. {
  83. err('Not found user with this passkey');
  84. }
  85. }
  86. preg_match_all('/info_hash=([^&]*)/i', $_SERVER['QUERY_STRING'], $info_hashs);
  87. if(isset($info_hashs[1]) && $info_hashs[1])
  88. {
  89. foreach($info_hashs[1] as $k => $info_hash)
  90. {
  91. $info_hash=urldecode($info_hash);
  92. STRIP ? $info_hash=stripslashes($info_hash) : '';
  93. $l_info_hash=strlen($info_hash);
  94. if($l_info_hash!=20)
  95. {
  96. err("invalid info_hash: {$info_hash} ({$l_info_hash})");
  97. }
  98. $info_hashs[1][$k] = mysql_real_escape_string($info_hash, $c);
  99. }
  100. }
  101. else
  102. {
  103. err("Invalid info hash(s)");
  104. }
  105. $sql = "SELECT info_hash, seeders, leechers, times_completed FROM ".TRACKER_TORRENTS_TABLE." WHERE info_hash IN('".implode("', '", $info_hashs[1])."')";
  106. $result=my_sql_query($sql);
  107. $torrent = mysql_num_rows($result);
  108. if (!$torrent)
  109. {
  110. err("Torrent(s) not found on this tracker - hash(s): " . implode(", ", array_map('bin2hex', $info_hashs[1])));
  111. }
  112. $resp='d5:filesd';
  113. while($row = mysql_fetch_array($result))
  114. {
  115. $resp.='20:'.$row['info_hash'].'d';
  116. $resp.='8:completei'.$row['seeders'].'e';
  117. $resp.='10:downloadedi'.$row['times_completed'].'e';
  118. $resp.='10:incompletei'.$row['leechers'].'e';
  119. $resp.='e';
  120. }
  121. mysql_free_result($result);
  122. $resp.='ee';
  123. //resp.="5:flagsd20:min_request_intervali{$config['ppkbb_minscrape_interval']}eee";
  124. benc_resp_raw($resp, $config['ppkbb_gtcgz_rewrite']);
  125. if($c)
  126. {
  127. mysql_close($c);
  128. }
  129. exit();
  130. //############################################################
  131. function err($msg)
  132. {
  133. global $c;
  134. if($msg)
  135. {
  136. benc_resp(array("failure reason" => array('type' => "string", 'value' => $msg)));
  137. }
  138. if($c)
  139. {
  140. mysql_close($c);
  141. }
  142. exit();
  143. }
  144. function warn($msg)
  145. {
  146. global $c;
  147. if($msg)
  148. {
  149. benc_resp(array("warning message" => array('type' => "string", 'value' => $msg)));
  150. }
  151. if($c)
  152. {
  153. mysql_close($c);
  154. }
  155. exit();
  156. }
  157. function benc($obj)
  158. {
  159. if (!is_array($obj) || !isset($obj['type']) || !isset($obj['value']))
  160. {
  161. return;
  162. }
  163. $c = $obj['value'];
  164. switch ($obj['type'])
  165. {
  166. case "string":
  167. return benc_str($c);
  168. case "integer":
  169. return benc_int($c);
  170. case "list":
  171. return benc_list($c);
  172. case "dictionary":
  173. return benc_dict($c);
  174. default:
  175. return;
  176. }
  177. }
  178. function benc_str($s)
  179. {
  180. return strlen($s) . ":$s";
  181. }
  182. function benc_int($i)
  183. {
  184. return "i" . $i . "e";
  185. }
  186. function benc_list($a)
  187. {
  188. $s = "l";
  189. foreach ($a as $e)
  190. {
  191. $s .= benc($e);
  192. }
  193. $s .= "e";
  194. return $s;
  195. }
  196. function benc_dict($d)
  197. {
  198. $s = "d";
  199. $keys = array_keys($d);
  200. sort($keys);
  201. foreach ($keys as $k)
  202. {
  203. $v = $d[$k];
  204. $s .= benc_str($k);
  205. $s .= benc($v);
  206. }
  207. $s .= "e";
  208. return $s;
  209. }
  210. function benc_resp($d)
  211. {
  212. global $config;
  213. benc_resp_raw(benc(array('type' => "dictionary", 'value' => $d)), $config['ppkbb_gtcgz_rewrite']);
  214. }
  215. function benc_resp_raw($x, $c=0)
  216. {
  217. $gz_enc=strstr(@$_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && @extension_loaded('zlib') && (ini_get('zlib.output_compression')=='Off' || !ini_get('zlib.output_compression')) ? 1 : 0;
  218. header("Content-Type: text/plain");
  219. if($c==1 || (!$c && $gz_enc))
  220. {
  221. header("Content-Encoding: gzip");
  222. print gzencode($x, 9, FORCE_GZIP);
  223. }
  224. else if($c==2 || (!$c && !$gz_enc) || !$gz_enc)
  225. {
  226. header("Pragma: no-cache");
  227. print($x);
  228. }
  229. }
  230. function my_sql_query($query, $log=true)
  231. {
  232. global $c;
  233. $result=@mysql_query($query, $c);
  234. if(!$result)
  235. {
  236. if($log)
  237. {
  238. global $tincludedir, $userid, $phpEx;
  239. include($tincludedir.'tlog.'.$phpEx);
  240. }
  241. err('Unknown sql error');
  242. // err('Unknown sql error: ('.mysql_errno($c).') '.mysql_error($c));
  243. mysql_close($c);
  244. }
  245. return $result;
  246. }
  247. function my_split_config($config, $count=0, $type=false, $split='')
  248. {
  249. $count=intval($count);
  250. if(!$count && $config==='')
  251. {
  252. return array();
  253. }
  254. $s_config=$count > 0 ? @explode($split ? $split : ' ', $config, $count) : @explode($split ? $split : ' ', $config);
  255. $count=$count > 0 ? $count : sizeof($s_config);
  256. if($count)
  257. {
  258. for($i=0;$i<$count;$i++)
  259. {
  260. if($type)
  261. {
  262. if(is_array($type) && @function_exists(@$type[$i]))
  263. {
  264. $s_config[$i]=call_user_func($type[$i], @$s_config[$i]);
  265. }
  266. else if(@function_exists($type))
  267. {
  268. $s_config[$i]=call_user_func($type, @$s_config[$i]);
  269. }
  270. else
  271. {
  272. $s_config[$i]=@$s_config[$i];
  273. }
  274. }
  275. else
  276. {
  277. $s_config[$i]=@$s_config[$i];
  278. }
  279. }
  280. }
  281. return $s_config;
  282. }
  283. function my_int_val($v=0, $max=0, $drop=false, $negative=false)
  284. {
  285. if(!$v || ($v < 0 && !$negative))
  286. {
  287. return 0;
  288. }
  289. else if($drop && $v>$max)
  290. {
  291. return 0;
  292. }
  293. else if($max && $v>$max)
  294. {
  295. return $max;
  296. }
  297. return @number_format($v+0, 0, '', '');
  298. }
  299. function my_float_val($v=0, $n=3, $max=0, $drop=false, $negative=false)
  300. {
  301. if(!$v || ($v < 0 && !$negative))
  302. {
  303. return "0.".str_repeat('0', $n);
  304. }
  305. else if($drop && $v>$max)
  306. {
  307. return "0.".str_repeat('0', $n);
  308. }
  309. else if($max && $v>$max)
  310. {
  311. return $max;
  312. }
  313. return @number_format($v+0, $n, '.', '');
  314. }
  315. function t_getcache($cname, $ctime=0, $non_php=false, $var='')
  316. {
  317. global $tcachedir, $phpEx;
  318. $cache_data=array();
  319. $f_name="{$tcachedir}data_ppkbb3cker_{$cname}.{$phpEx}";
  320. if(@file_exists($f_name))
  321. {
  322. if($ctime)
  323. {
  324. $m_time=@filemtime($f_name);
  325. if($m_time && time()-$m_time > $ctime)
  326. {
  327. return false;
  328. }
  329. }
  330. if($non_php)
  331. {
  332. $cache_data=@file($f_name);
  333. if(sizeof($cache_data))
  334. {
  335. unset($cache_data[0]);
  336. }
  337. return implode("\n", $cache_data);
  338. }
  339. else
  340. {
  341. include($f_name);
  342. return $var ? $$var : $cache_data;
  343. }
  344. }
  345. return false;
  346. }
  347. ?>