PirateBayEngine.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /* $Id: PirateBayEngine.php 3103 2007-06-15 22:00:26Z munk $ */
  3. /*************************************************************
  4. * TorrentFlux PHP Torrent Manager
  5. * www.torrentflux.com
  6. **************************************************************/
  7. /*
  8. This file is part of TorrentFlux.
  9. TorrentFlux is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. TorrentFlux is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with TorrentFlux; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /*
  22. v 1.06 - Feb 16, 07 - Update parsing.
  23. v 1.05 - Oct 18, 06 - Seeds and Peers were off.
  24. v 1.04 - Oct 16, 06 - fix paging
  25. v 1.03 - Aug 23, 06 - Added Top 100
  26. v 1.02 - Jun 29, 06 - fix to paging..
  27. v 1.01 - Apr 11, 06 - bug in parsing paging.
  28. */
  29. class SearchEngine extends SearchEngineBase
  30. {
  31. function SearchEngine($cfg)
  32. {
  33. $this->mainURL = "thepiratebay.org";
  34. $this->altURL = "thepiratebay.org";
  35. $this->mainTitle = "The PirateBay";
  36. $this->engineName = "PirateBay";
  37. $this->author = "kboy";
  38. $this->version = "1.06-tfb";
  39. $this->updateURL = "http://www.torrentflux.com/forum/index.php/topic,1125.0.html";
  40. $this->Initialize($cfg);
  41. }
  42. function populateMainCategories()
  43. {
  44. $this->mainCatalog["000"] = "Top100";
  45. $this->mainCatalog["100"] = "Audio";
  46. $this->mainCatalog["200"] = "Video";
  47. $this->mainCatalog["300"] = "Applications";
  48. $this->mainCatalog["400"] = "Games";
  49. $this->mainCatalog["500"] = "Porn";
  50. $this->mainCatalog["600"] = "Other";
  51. }
  52. //----------------------------------------------------------------
  53. // Function to Get Sub Categories
  54. function getSubCategories($mainGenre)
  55. {
  56. $output = array();
  57. switch ($mainGenre)
  58. {
  59. case "100" :
  60. $output["101"] = "Music";
  61. $output["102"] = "Audio books";
  62. $output["103"] = "Sound clips";
  63. $output["199"] = "Other";
  64. break;
  65. case "200" :
  66. $output["201"] = "Movies";
  67. $output["202"] = "Movies DVDR";
  68. $output["203"] = "Music videos";
  69. $output["204"] = "Movie clips";
  70. $output["205"] = "TV shows";
  71. $output["206"] = "Handheld";
  72. $output["299"] = "Other";
  73. break;
  74. case "300" :
  75. $output["301"] = "Windows";
  76. $output["302"] = "Mac";
  77. $output["303"] = "UNIX";
  78. $output["304"] = "Handheld";
  79. $output["399"] = "Other OS";
  80. break;
  81. case "400" :
  82. $output["401"] = "PC";
  83. $output["402"] = "Mac";
  84. $output["403"] = "PS2";
  85. $output["404"] = "XBOX";
  86. $output["405"] = "Gamecube";
  87. $output["406"] = "Handheld";
  88. $output["499"] = "Other";
  89. break;
  90. case "500" :
  91. $output["501"] = "Movies";
  92. $output["502"] = "Movies DVDR";
  93. $output["503"] = "Pictures";
  94. $output["504"] = "Games";
  95. $output["599"] = "Other";
  96. break;
  97. case "600" :
  98. $output["601"] = "E-books";
  99. $output["602"] = "Comics";
  100. $output["603"] = "Pictures";
  101. $output["604"] = "Covers";
  102. $output["699"] = "Other";
  103. break;
  104. }
  105. return $output;
  106. }
  107. //----------------------------------------------------------------
  108. // Function to Make the Request (overriding base)
  109. function makeRequest($request)
  110. {
  111. return parent::makeRequest($request, false);
  112. }
  113. //----------------------------------------------------------------
  114. // Function to get Latest..
  115. function getLatest()
  116. {
  117. //recent.php
  118. //top100.php
  119. if ($_REQUEST["mainGenre"] == "000")
  120. {
  121. $request = "/top/all";
  122. }
  123. else
  124. {
  125. if (array_key_exists("subGenre",$_REQUEST))
  126. {
  127. $request = "/brwsearch.php?b=1&c=".$_REQUEST["subGenre"];
  128. }
  129. elseif (array_key_exists("mainGenre",$_REQUEST))
  130. {
  131. if ($_REQUEST["mainGenre"] == "000")
  132. {
  133. $request = "/top/all";
  134. } else {
  135. $request = "/brwsearch.php?b=1&c=0&d=".$_REQUEST["mainGenre"];
  136. }
  137. }
  138. else
  139. {
  140. $request = "/recent.php?orderby=se";
  141. }
  142. if (!empty($this->pg))
  143. {
  144. $request .= "&page=" . $this->pg;
  145. }
  146. }
  147. if ($this->makeRequest($request))
  148. {
  149. return $this->parseResponse();
  150. }
  151. else
  152. {
  153. return $this->msg;
  154. }
  155. }
  156. //----------------------------------------------------------------
  157. // Function to perform Search.
  158. function performSearch($searchTerm)
  159. {
  160. if (array_key_exists("subGenre",$_REQUEST))
  161. {
  162. $request = "/brwsearch.php?b=1&c=".$_REQUEST["subGenre"];
  163. }
  164. elseif (array_key_exists("mainGenre",$_REQUEST))
  165. {
  166. $request = "/brwsearch.php?b=1&c=0&d=".$_REQUEST["mainGenre"];
  167. }
  168. else
  169. {
  170. $request = "/search.php?orderby=".$searchTerm;
  171. }
  172. if(strlen($searchTerm) > 0)
  173. {
  174. $searchTerm = str_replace(" ", "+", $searchTerm);
  175. $request .= "&q=".$searchTerm;
  176. }
  177. $request .= "&audio=&video=&apps=&games=&porn=&other=&what=search&orderby=";
  178. if (!empty($this->pg))
  179. {
  180. $request .= "&page=" . $this->pg;
  181. }
  182. if ($this->makeRequest($request))
  183. {
  184. return $this->parseResponse();
  185. }
  186. else
  187. {
  188. return $this->msg;
  189. }
  190. }
  191. //----------------------------------------------------------------
  192. // Override the base to show custom table header.
  193. // Function to setup the table header
  194. function tableHeader()
  195. {
  196. $output = "<table width=\"100%\" cellpadding=3 cellspacing=0 border=0>";
  197. $output .= "<br>\n";
  198. $output .= "<tr bgcolor=\"".$this->cfg["table_header_bg"]."\">";
  199. $output .= " <td>&nbsp;</td>";
  200. $output .= " <td><strong>Torrent Name</strong> &nbsp;(";
  201. $tmpURI = str_replace(array("?hideSeedless=yes","&hideSeedless=yes","?hideSeedless=no","&hideSeedless=no"),"",$_SERVER["REQUEST_URI"]);
  202. // Check to see if Question mark is there.
  203. if (strpos($tmpURI,'?'))
  204. {
  205. $tmpURI .= "&";
  206. }
  207. else
  208. {
  209. $tmpURI .= "?";
  210. }
  211. if($this->hideSeedless == "yes")
  212. {
  213. $output .= "<a href=\"". $tmpURI . "hideSeedless=no\">Show Seedless</a>";
  214. }
  215. else
  216. {
  217. $output .= "<a href=\"". $tmpURI . "hideSeedless=yes\">Hide Seedless</a>";
  218. }
  219. $output .= ")</td>";
  220. $output .= " <td><strong>Category</strong></td>";
  221. $output .= " <td align=center><strong>&nbsp;&nbsp;Size</strong></td>";
  222. $output .= " <td><strong>Date Added</strong></td>";
  223. $output .= " <td><strong>Seeds</strong></td>";
  224. $output .= " <td><strong>Peers</strong></td>";
  225. $output .= "</tr>\n";
  226. return $output;
  227. }
  228. //----------------------------------------------------------------
  229. // Function to parse the response.
  230. function parseResponse()
  231. {
  232. $output = $this->tableHeader();
  233. $thing = $this->htmlPage;
  234. // We got a response so display it.
  235. // Chop the front end off.
  236. if (is_integer(strpos($thing,"Your search did not match any torrents")))
  237. {
  238. $this->msg = "Your search did not match any torrents";
  239. } else {
  240. while (is_integer(strpos($thing,"searchResult")))
  241. {
  242. $thing = substr($thing,strpos($thing,"searchResult"));
  243. $thing = substr($thing,strpos($thing,"</thead>"));
  244. $thing = substr($thing,strpos($thing,"<tr>"));
  245. $tmpList = substr($thing,0,strpos($thing,"</table>"));
  246. // ok so now we have the listing.
  247. $tmpListArr = explode("</tr>",$tmpList);
  248. $bg = $this->cfg["bgLight"];
  249. foreach($tmpListArr as $key =>$value)
  250. {
  251. $buildLine = true;
  252. if (strpos($value,"static.thepiratebay.org"))
  253. {
  254. $ts = new pBay($value);
  255. // Determine if we should build this output
  256. if (is_int(array_search($ts->CatName,$this->catFilter)))
  257. {
  258. $buildLine = false;
  259. }
  260. if ($this->hideSeedless == "yes")
  261. {
  262. if($ts->Seeds == "N/A" || $ts->Seeds == "0")
  263. {
  264. $buildLine = false;
  265. }
  266. }
  267. if (!empty($ts->torrentFile) && $buildLine) {
  268. $output .= trim($ts->BuildOutput($bg, $this->searchURL()));
  269. // ok switch colors.
  270. if ($bg == $this->cfg["bgLight"])
  271. {
  272. $bg = $this->cfg["bgDark"];
  273. }
  274. else
  275. {
  276. $bg = $this->cfg["bgLight"];
  277. }
  278. }
  279. } elseif (strpos($value,"torrents.thepiratebay.org")) {
  280. $ts = new pBay($value);
  281. // Determine if we should build this output
  282. if (is_int(array_search($ts->CatName,$this->catFilter)))
  283. {
  284. $buildLine = false;
  285. }
  286. if ($this->hideSeedless == "yes")
  287. {
  288. if($ts->Seeds == "N/A" || $ts->Seeds == "0")
  289. {
  290. $buildLine = false;
  291. }
  292. }
  293. if (!empty($ts->torrentFile) && $buildLine) {
  294. $output .= trim($ts->BuildOutput($bg, $this->searchURL()));
  295. // ok switch colors.
  296. if ($bg == $this->cfg["bgLight"])
  297. {
  298. $bg = $this->cfg["bgDark"];
  299. }
  300. else
  301. {
  302. $bg = $this->cfg["bgLight"];
  303. }
  304. }
  305. }
  306. }
  307. }
  308. $output .= "</table>";
  309. // is there paging at the bottom?
  310. if (strpos($thing, "&page=") != false)
  311. {
  312. // Yes, then lets grab it and display it! ;)
  313. $thing = substr($thing,strpos($thing,"<tr><td colspan")+strlen("<tr><td colspan"));
  314. $thing = substr($thing,strpos($thing,">")+1);
  315. $pages = substr($thing,0,strpos($thing,"</td>"));
  316. if (strpos($pages,"prev") > 0)
  317. {
  318. $tmpStr = substr($pages,0,strpos($pages,"<img"));
  319. $pages = substr($pages,strpos($pages,"<img"));
  320. $pages = substr($pages,strpos($pages,">")+1);
  321. $pages = $tmpStr."Prev".$pages;
  322. if (strpos($pages,"next") > 0)
  323. {
  324. $pages = substr($pages,0,strpos($pages,"<img"))."Next</a>";
  325. }
  326. }
  327. elseif (strpos($pages,"next") > 0)
  328. {
  329. $pages = substr($pages,0,strpos($pages,"<img"))."Next</a>";
  330. }
  331. if(strpos($this->curRequest,"LATEST"))
  332. {
  333. $pages = str_replace("?",$this->searchURL()."&LATEST=1&",$pages);
  334. $pages = str_replace("/recent.php","",$pages);
  335. }
  336. else
  337. {
  338. $pages = str_replace("?",$this->searchURL()."&",$pages);
  339. $pages = str_replace("/search.php",'',$pages);
  340. }
  341. $pages = str_replace("page=","pg=",$pages);
  342. $pages = str_replace("d=","cat=",$pages);
  343. $pages = str_replace("c=","subGenre=",$pages);
  344. $pages = str_replace("q=","searchterm=", $pages);
  345. $pages = str_replace("orderby=","",$pages);
  346. $pages = str_replace("&&","&",$pages);
  347. $pages = str_replace("/brwsearch.php","",$pages);
  348. $output .= "<div align=center>".$pages."</div>";
  349. }
  350. }
  351. return $output;
  352. }
  353. }
  354. // This is a worker class that takes in a row in a table and parses it.
  355. class pBay
  356. {
  357. var $torrentName = "";
  358. var $torrentDisplayName = "";
  359. var $torrentFile = "";
  360. var $torrentSize = "";
  361. var $torrentStatus = "";
  362. var $CatName = "";
  363. var $CatId = "";
  364. var $MainId = "";
  365. var $MainCategory = "";
  366. var $SubId = "";
  367. var $SubCategory = "";
  368. var $Seeds = "";
  369. var $Peers = "";
  370. var $Data = "";
  371. var $dateAdded = "";
  372. var $dwnldCount = "";
  373. function pBay( $htmlLine )
  374. {
  375. if (strlen($htmlLine) > 0)
  376. {
  377. $this->Data = $htmlLine;
  378. // Chunck up the row into columns.
  379. $tmpListArr = explode("</td>",$htmlLine);
  380. if(count($tmpListArr) > 6)
  381. {
  382. // Cat Id
  383. $tmpStr = substr($tmpListArr["0"],strpos($tmpListArr["0"],"c=")+2);
  384. $this->CatId = substr($tmpStr,0,strpos($tmpStr,"\""));
  385. $this->CatName = $this->cleanLine($tmpListArr["0"]); // Cat Name
  386. if (strpos($this->CatName,">"))
  387. {
  388. $this->MainCategory = trim(substr($this->CatName,0,strpos($this->CatName,">")));
  389. $this->MainId = substr($this->CatId,0,1) . "00";
  390. $this->SubCategory = trim(substr($this->CatName,strpos($this->CatName,">")+1));
  391. $this->SubId = $this->CatId;
  392. }
  393. else
  394. {
  395. $this->MainCategory = $this->CatName;
  396. $this->MainId = $this->CatId;
  397. }
  398. $this->torrentName = $this->cleanLine($tmpListArr["1"]); // TorrentName
  399. $this->dateAdded = $this->cleanLine($tmpListArr["2"]); // Date Added
  400. $tmpStr = "";
  401. $tmpStr = substr($tmpListArr["3"],strpos($tmpListArr["3"],"href=\"")+strlen("href=\"")); // Download Link
  402. $this->torrentFile = substr($tmpStr,0,strpos($tmpStr,"\""));
  403. $this->torrentSize = $this->cleanLine($tmpListArr["4"]); // Size of File
  404. $this->Seeds = $this->cleanLine($tmpListArr["5"]); // Seeds
  405. $this->Peers = $this->cleanLine($tmpListArr["6"]); // Peers
  406. if ($this->Peers == '')
  407. {
  408. $this->Peers = "N/A";
  409. if (empty($this->Seeds)) $this->Seeds = "N/A";
  410. }
  411. if ($this->Seeds == '') $this->Seeds = "N/A";
  412. $this->torrentDisplayName = $this->torrentName;
  413. if(strlen($this->torrentDisplayName) > 50)
  414. {
  415. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  416. }
  417. }
  418. }
  419. }
  420. function cleanLine($stringIn,$tags='')
  421. {
  422. if(empty($tags))
  423. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn)));
  424. else
  425. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn,$tags)));
  426. }
  427. //----------------------------------------------------------------
  428. // Function to build output for the table.
  429. function BuildOutput($bg, $searchURL)
  430. {
  431. $output = "<tr>\n";
  432. $output .= " <td width=16 bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\"><img src=\"".getImagesPath()."download_owner.gif\" width=\"16\" height=\"16\" title=\"".$this->torrentName."\" border=0></a></td>\n";
  433. $output .= " <td bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\" title=\"".$this->torrentName."\">".$this->torrentDisplayName."</a></td>\n";
  434. if (strlen($this->MainCategory) > 1){
  435. if (strlen($this->SubCategory) > 1){
  436. $mainGenre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  437. $subGenre = "<a href=\"".$searchURL."&subGenre=".$this->SubId."\">".$this->SubCategory."</a>";
  438. $genre = $mainGenre."-".$subGenre;
  439. }else{
  440. $genre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  441. }
  442. }else{
  443. $genre = "<a href=\"".$searchURL."&subGenre=".$this->SubId."\">".$this->SubCategory."</a>";
  444. }
  445. $output .= " <td bgcolor=\"".$bg."\">". $genre ."</td>\n";
  446. $output .= " <td bgcolor=\"".$bg."\" align=right>".$this->torrentSize."</td>\n";
  447. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->dateAdded."</td>\n";
  448. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Seeds."</td>\n";
  449. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Peers."</td>\n";
  450. $output .= "</tr>\n";
  451. return $output;
  452. }
  453. }
  454. ?>