PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/rannfunc.php

https://gitlab.com/protoneutron/xbtbb3cker
PHP | 421 lines | 350 code | 51 blank | 20 comment | 49 complexity | 25df3640edab2e1fd8d944bc9c01e82a MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package xbtBB3cker
  5. * @copyright (c) 2015 PPK
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. if (!defined('IN_PHPBB'))
  13. {
  14. exit;
  15. }
  16. function remote_announce($url, $info_hash, $scrape=false)
  17. {
  18. global $phpEx, $phpbb_root_path, $config;
  19. if(ini_get('allow_url_fopen')!=1 || (!function_exists('file_get_contents') && !function_exists('fsockopen') && !function_exists('curl_version')))
  20. {
  21. return array('a_message' => "Remote announce: functions 'file_get_contents/fsockopen/curl_version' or 'allow_url_fopen' disabled");
  22. }
  23. $info_hash=substr($info_hash, 0, 20);
  24. $a_url = @parse_url($url);
  25. $scheme = isset($a_url['scheme']) ? $a_url['scheme'] : 'http';
  26. $host = $a_url['host'];
  27. $port = isset($a_url['port']) ? $a_url['port'] : '';
  28. $path = isset($a_url['path']) ? $a_url['path'] : '';
  29. $q_string = isset($a_url['query']) ? explode('&', $a_url['query']) : array();
  30. $get_opt=array();
  31. foreach($q_string as $value)
  32. {
  33. @list($q_key, $q_value)=explode('=', $value);
  34. $get_opt[$q_key] = $q_value;
  35. }
  36. $get_opt = array_merge($get_opt, array(
  37. 'peer_id' => urlencode($config['ppkbb_tcrannounces_options'][10].(substr(md5(date('d-m-Y')), 0, 12))),
  38. 'port' => rand(1024, 65535),
  39. 'uploaded' => 0,
  40. 'downloaded' => 0,
  41. 'left' => 1,
  42. 'corrupt' => 0,
  43. 'key' => substr(md5(date('Y-m-d')), 0, 8),
  44. 'event' => 'started',
  45. 'supportcrypto' => 1,
  46. 'numwant' => $config['ppkbb_tcrannounces_options'][2] ? $config['ppkbb_tcrannounces_options'][2] : 50,
  47. 'compact' => 1,
  48. 'no_peer_id' => 1,
  49. 'info_hash' => rawurlencode($info_hash),
  50. )
  51. );
  52. //$query = http_build_query($get_opt);
  53. $query='';
  54. $i=0;
  55. $q=count($get_opt);
  56. foreach($get_opt as $k => $v)
  57. {
  58. $i+=1;
  59. $query.="{$k}={$v}".($i<$q ? '&' : '');
  60. }
  61. $timeout=$config['ppkbb_tcrannounces_options'][4] ? $config['ppkbb_tcrannounces_options'][4] : 5;
  62. $result=array();
  63. $error='';
  64. if(in_array($scheme, array('http')))
  65. {
  66. if(function_exists('file_get_contents'))
  67. {
  68. $opt = array('http' =>
  69. array(
  70. 'method' => 'GET',
  71. 'header' => "User-Agent: {$config['ppkbb_tcrannounces_options'][9]}\r\n".
  72. "Host: {$host}\r\n".
  73. "Connection: close\r\n",
  74. 'timeout' => $timeout
  75. )
  76. );
  77. $context = stream_context_create($opt);
  78. $url='http://'.$host.($port ? ':'.$port : '').$path.($query ? '?'.$query : '');
  79. $result = @file_get_contents($url, false, $context);
  80. }
  81. else if(function_exists('curl_version'))
  82. {
  83. $ch = curl_init();
  84. curl_setopt($ch, CURLOPT_URL, 'http://'.$host.($port ? ':'.$port : '').$path.($query ? '?'.$query : ''));
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  86. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  87. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  88. curl_setopt($ch, CURLOPT_USERAGENT, $config['ppkbb_tcrannounces_options'][9]);
  89. $result = curl_exec($ch);
  90. curl_close ($ch);
  91. }
  92. else if(function_exists('fsockopen'))
  93. {
  94. $fp=@fsockopen($host, ($port ? $port : 80), $errno, $errstr, $timeout);
  95. if(!$fp)
  96. {
  97. $error=$errstr.($errno ? " ({$errno})" : '');
  98. }
  99. else
  100. {
  101. $request="GET {$path}".($query ? '?'.$query : '')." HTTP/1.1\r\n";
  102. $request.="Host: $host\r\n";
  103. $request.="User-Agent: {$config['ppkbb_tcrannounces_options'][9]}\r\n";
  104. $request.="Connection: close\r\n\r\n";
  105. fputs($fp, $request);
  106. //stream_set_timeout($fp, $timeout);
  107. $result='';
  108. while(!feof($fp))
  109. {
  110. $line=fgets($fp, 4096);
  111. if($line=="\r\n" && !isset($header))
  112. {
  113. $header=true;
  114. }
  115. if(isset($header))
  116. {
  117. $result.=$line;
  118. }
  119. }
  120. fclose($fp);
  121. isset($header) ? $result=trim($result) : '';
  122. }
  123. }
  124. }
  125. else if($scheme=='udp')
  126. {
  127. include_once("{$phpbb_root_path}ext/ppk/xbtbb3cker/include/udptscraper.{$phpEx}");
  128. try
  129. {
  130. $scraper = new udptscraper($timeout);
  131. $info_hash2=bin2hex($info_hash);
  132. $url="udp://".$host.($port ? ':'.$port : '').$path.($query ? '?'.$query : '');
  133. $ret = $scraper->scrape($url, array($info_hash2));
  134. $seeders=isset($ret[$info_hash2]['seeders']) ? my_int_val($ret[$info_hash2]['seeders'], 10000, true) : 0;
  135. $leechers=isset($ret[$info_hash2]['leechers']) ? my_int_val($ret[$info_hash2]['leechers'], 10000, true) : 0;
  136. $result=array(
  137. 'interval' => 0,
  138. 'seeders' => $seeders,
  139. 'leechers' => $leechers,
  140. 'times_completed' => isset($ret[$info_hash2]['completed']) ? my_int_val($ret[$info_hash2]['completed'], 100000, true) : 0,
  141. 'peers' => my_int_val($seeders+$leechers, 20000, true),
  142. 'a_message'=> '',
  143. );
  144. return $result;
  145. }
  146. catch(ScraperException $e)
  147. {
  148. return array('a_message' => 'UDP announce error: '.$e->getMessage().($e->isConnectionError() ? ' (connection error)' : ''));
  149. }
  150. //return array('a_message' => 'UDP announce not supported');
  151. }
  152. if(!$result)
  153. {
  154. return array('a_message' => ($result===false ? 'Query timeout' : ($error ? $error : 'Empty result')));
  155. }
  156. include_once($phpbb_root_path.'ext/ppk/xbtbb3cker/include/bencoding.'.$phpEx);
  157. $result = bdecode($result);
  158. if(!is_array($result))
  159. {
  160. return array('a_message' => 'BDecoding error');
  161. }
  162. $complete=isset($result['complete']) ? my_int_val($result['complete']) : 0;
  163. $incomplete=isset($result['incomplete']) ? my_int_val($result['incomplete']) : 0;
  164. $downloaded=isset($result['downloaded']) ? my_int_val($result['downloaded']) : 0;
  165. $peers=isset($result['peers']) ? $result['peers'] : 0;
  166. if(!is_numeric($peers))
  167. {
  168. $bin_peers=@unpack('N*', $peers);
  169. if(is_array($bin_peers))
  170. {
  171. $peers=count($bin_peers);
  172. }
  173. else
  174. {
  175. $peers=my_int_val(strlen($peers)/6);
  176. }
  177. }
  178. if($peers && $complete+$incomplete==0)
  179. {
  180. !$config['ppkbb_tcrannounces_options'][3] || $config['ppkbb_tcrannounces_options'][3] > 100 ? $config['ppkbb_tcrannounces_options'][3]=100 : '';
  181. $complete=my_int_val($config['ppkbb_tcrannounces_options'][3]*$peers/100);
  182. $incomplete=$peers-$complete;
  183. }
  184. else
  185. {
  186. $peers=$complete+$incomplete;
  187. }
  188. $return=array(
  189. 'interval' => isset($result['interval']) ? my_int_val($result['interval'], 10000, true) : 0,
  190. 'seeders' => my_int_val($complete, 10000, true),
  191. 'leechers' => my_int_val($incomplete, 10000, true),
  192. 'times_completed' => my_int_val($downloaded, 100000, true),
  193. 'peers' => my_int_val($peers, 100000, true),
  194. 'a_message'=> isset($result['failure reason']) ? my_utf8_convert_message($result['failure reason']) : '',
  195. );
  196. $scrape ? $return['s_message']='Scrape not supported' : '';
  197. return $return;
  198. }
  199. function remote_scrape($url, $info_hash)
  200. {
  201. global $phpEx, $phpbb_root_path, $config;
  202. if(ini_get('allow_url_fopen')!=1 || (!function_exists('file_get_contents') && !function_exists('fsockopen')) && !function_exists('curl_version'))
  203. {
  204. return array('a_message' => "Remote announce: functions 'file_get_contents/fsockopen/curl_version' or 'allow_url_fopen' disabled");
  205. }
  206. $url_scrape=str_replace('announce', 'scrape', $url);
  207. if($config['ppkbb_tcrannounces_options'][7] || $url_scrape==$url)
  208. {
  209. return remote_announce($url, $info_hash, true);
  210. }
  211. else
  212. {
  213. $info_hash=substr($info_hash, 0, 20);
  214. $url=$url_scrape;
  215. $a_url = @parse_url($url);
  216. $scheme = isset($a_url['scheme']) ? $a_url['scheme'] : 'http';
  217. $host = $a_url['host'];
  218. $port = isset($a_url['port']) ? $a_url['port'] : '';
  219. $path = isset($a_url['path']) ? $a_url['path'] : '';
  220. $q_string = isset($a_url['query']) ? explode('&', $a_url['query']) : array();
  221. $get_opt=array();
  222. foreach($q_string as $value)
  223. {
  224. @list($q_key, $q_value)=explode('=', $value);
  225. $get_opt[$q_key] = $q_value;
  226. }
  227. $get_opt = array_merge($get_opt, array(
  228. /*'peer_id' => urlencode($config['ppkbb_tcrannounces_options'][10].(substr(md5(date('d-m-Y')), 0, 12))),
  229. 'port' => rand(1024, 65535),
  230. 'uploaded' => 0,
  231. 'downloaded' => 0,
  232. 'left' => 1,*/
  233. 'info_hash' => rawurlencode($info_hash),
  234. )
  235. );
  236. //$query = http_build_query($get_opt);
  237. $query='';
  238. $i=0;
  239. $q=count($get_opt);
  240. foreach($get_opt as $k => $v)
  241. {
  242. $i+=1;
  243. $query.="{$k}={$v}".($i<$q ? '&' : '');
  244. }
  245. $timeout=$config['ppkbb_tcrannounces_options'][4] ? $config['ppkbb_tcrannounces_options'][4] : 5;
  246. $result=array();
  247. $error='';
  248. if(in_array($scheme, array('http')))
  249. {
  250. if(function_exists('file_get_contents'))
  251. {
  252. $opt = array('http' =>
  253. array(
  254. 'method' => 'GET',
  255. 'header' => "User-Agent: {$config['ppkbb_tcrannounces_options'][9]}\r\n".
  256. "Host: {$host}\r\n".
  257. "Connection: close\r\n",
  258. 'timeout' => $timeout
  259. )
  260. );
  261. $context = @stream_context_create($opt);
  262. $url='http://'.$host.($port ? ':'.$port : '').$path.($query ? '?'.$query : '');
  263. $result = @file_get_contents($url, false, $context);
  264. }
  265. else if(function_exists('curl_version'))
  266. {
  267. $ch = curl_init();
  268. curl_setopt($ch, CURLOPT_URL, 'http://'.$host.($port ? ':'.$port : '').$path.($query ? '?'.$query : ''));
  269. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  270. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  271. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  272. curl_setopt($ch, CURLOPT_USERAGENT, $config['ppkbb_tcrannounces_options'][9]);
  273. $result = curl_exec($ch);
  274. curl_close ($ch);
  275. }
  276. else if(function_exists('fsockopen'))
  277. {
  278. $fp=@fsockopen($host, ($port ? $port : 80), $errno, $errstr, $timeout);
  279. if(!$fp)
  280. {
  281. $error=$errstr.($errno ? " ({$errno})" : '');
  282. }
  283. else
  284. {
  285. $request="GET {$path}".($query ? '?'.$query : '')." HTTP/1.1\r\n";
  286. $request.="Host: $host\r\n";
  287. $request.="User-Agent: {$config['ppkbb_tcrannounces_options'][9]}\r\n";
  288. $request.="Connection: close\r\n\r\n";
  289. fputs($fp, $request);
  290. //stream_set_timeout($fp, $timeout);
  291. $result='';
  292. while(!feof($fp))
  293. {
  294. $line=fgets($fp, 4096);
  295. if($line=="\r\n" && !isset($header))
  296. {
  297. $header=true;
  298. }
  299. if(isset($header))
  300. {
  301. $result.=$line;
  302. }
  303. }
  304. fclose($fp);
  305. isset($header) ? $result=trim($result) : '';
  306. }
  307. }
  308. }
  309. else if($scheme=='udp')
  310. {
  311. include_once("{$phpbb_root_path}ext/ppk/xbtbb3cker/include/udptscraper.{$phpEx}");
  312. try
  313. {
  314. $scraper = new udptscraper($timeout);
  315. $info_hash2=bin2hex($info_hash);
  316. $url="udp://".$host.$port.$path.($query ? '?'.$query : '');
  317. $ret = $scraper->scrape($url, array($info_hash2));
  318. $seeders=isset($ret[$info_hash2]['seeders']) ? my_int_val($ret[$info_hash2]['seeders'], 10000, true) : 0;
  319. $leechers=isset($ret[$info_hash2]['leechers']) ? my_int_val($ret[$info_hash2]['leechers'], 10000, true) : 0;
  320. $result=array(
  321. 'interval' => 0,
  322. 'seeders' => $seeders,
  323. 'leechers' => $leechers,
  324. 'times_completed' => isset($ret[$info_hash2]['completed']) ? my_int_val($ret[$info_hash2]['completed'], 100000, true) : 0,
  325. 'peers' => my_int_val($seeders+$leechers, 20000, true),
  326. 's_message'=> '',
  327. );
  328. return $result;
  329. }
  330. catch(ScraperException $e)
  331. {
  332. return array('s_message' => 'UDP scrape error: '.$e->getMessage().($e->isConnectionError() ? ' (connection error)' : ''));
  333. }
  334. }
  335. if(!$result)
  336. {
  337. return array('s_message' => ($result===false ? 'Query timeout' : ($error ? $error : 'Empty result')));
  338. }
  339. include_once($phpbb_root_path.'ext/ppk/xbtbb3cker/include/bencoding.'.$phpEx);
  340. $result = bdecode($result);
  341. if(!is_array($result))
  342. {
  343. return array('s_message' => 'BDecoding error');
  344. }
  345. if(!isset($result['failure reason']) && !isset($result['files'][$info_hash]['complete']) && !isset($result['files'][$info_hash]['incomplete']) && !isset($result['files'][$info_hash]['downloaded']))
  346. {
  347. return array('s_message' => 'Empty result');
  348. }
  349. $seeders=isset($result['files'][$info_hash]['complete']) ? $result['files'][$info_hash]['complete'] : 0;
  350. $leechers=isset($result['files'][$info_hash]['incomplete']) ? $result['files'][$info_hash]['incomplete'] : 0;
  351. $return=array(
  352. 'interval' => isset($result['flags']['min_request_interval']) ? my_int_val($result['flags']['min_request_interval'], 10000, true) : 0,
  353. 'seeders' => my_int_val($seeders, 10000, true),
  354. 'leechers' => my_int_val($leechers, 10000, true),
  355. 'times_completed' => isset($result['files'][$info_hash]['downloaded']) ? my_int_val($result['files'][$info_hash]['downloaded'], 100000, true) : 0,
  356. 'peers' => my_int_val($seeders+$leechers, 20000, true),
  357. 's_message'=> isset($result['failure reason']) ? my_utf8_convert_message($result['failure reason']) : '',
  358. );
  359. return $return;
  360. }
  361. }
  362. function my_utf8_convert_message($message)
  363. {
  364. if (!preg_match('/[\x80-\xFF]/', $message))
  365. {
  366. return htmlspecialchars($message, ENT_COMPAT, 'UTF-8');
  367. }
  368. return htmlspecialchars($message);
  369. }
  370. ?>