BitSoupEngine.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. /* $Id: BitSoupEngine.php 2833 2007-04-08 11:58:47Z b4rt $ */
  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.05 - Apr 25, 06 - Fixed Paging when getting latest on a cat.
  23. v 1.04 - Apr 11, 06 - Fix catagories for improved filtering.
  24. v 1.03 - Apr 10, 06 - Fixed Paging.
  25. v 1.02 - Apr 07, 06 - Added Wait Column.
  26. v 1.01 - Apr 05, 06 - page was not parsing correctly everytime and updated pageing.
  27. */
  28. class SearchEngine extends SearchEngineBase
  29. {
  30. function SearchEngine($cfg)
  31. {
  32. $this->mainURL = "bitsoup.org";
  33. $this->altURL = "www.bitsoup.org";
  34. $this->mainTitle = "BitSoup";
  35. $this->engineName = "BitSoup";
  36. $this->author = "kboy";
  37. $this->version = "1.05-tfb";
  38. $this->updateURL = "http://www.torrentflux.com/forum/index.php/topic,1035.0.html";
  39. $this->Initialize($cfg);
  40. }
  41. //----------------------------------------------------------------
  42. // Function to Get Main Categories
  43. function populateMainCategories()
  44. {
  45. $this->mainCatalog["0"] = "(all types)";
  46. $this->mainCatalog["23"] = "Anime";
  47. $this->mainCatalog["22"] = "Appz/Misc";
  48. $this->mainCatalog["1"] = "Appz/PC ISO";
  49. $this->mainCatalog["24"] = "Ebooks";
  50. $this->mainCatalog["4"] = "Games/PC ISO";
  51. $this->mainCatalog["21"] = "Games/PC Rips";
  52. $this->mainCatalog["17"] = "Games/PS2";
  53. $this->mainCatalog["26"] = "Games/Xbox";
  54. $this->mainCatalog["12"] = "GBA/Gamecube";
  55. $this->mainCatalog["20"] = "Movies/DVD-R";
  56. $this->mainCatalog["27"] = "Movies/Other";
  57. $this->mainCatalog["5"] = "Movies/SVCD";
  58. $this->mainCatalog["19"] = "Movies/XviD";
  59. $this->mainCatalog["6"] = "Music";
  60. $this->mainCatalog["29"] = "Music Videos";
  61. $this->mainCatalog["28"] = "Other/MISC";
  62. $this->mainCatalog["30"] = "PSP/Handheld";
  63. $this->mainCatalog["7"] = "TV-Episodes";
  64. $this->mainCatalog["9"] = "Pron";
  65. }
  66. //----------------------------------------------------------------
  67. // Function to get Latest..
  68. function getLatest()
  69. {
  70. $cat = tfb_getRequestVar('mainGenre');
  71. if (empty($cat)) $cat = tfb_getRequestVar('cat');
  72. $request = "/browse.php";
  73. if(!empty($cat))
  74. {
  75. if(strpos($request,"?"))
  76. {
  77. $request .= "&cat=".$cat;
  78. }
  79. else
  80. {
  81. $request .= "?cat=".$cat;
  82. }
  83. }
  84. if (!empty($this->pg))
  85. {
  86. if(strpos($request,"?"))
  87. {
  88. $request .= "&page=" . $this->pg;
  89. }
  90. else
  91. {
  92. $request .= "?page=" . $this->pg;
  93. }
  94. }
  95. if ($this->makeRequest($request,true))
  96. {
  97. if (strlen($this->htmlPage) > 0 )
  98. {
  99. return $this->parseResponse();
  100. }
  101. else
  102. {
  103. return 'Unable to Browse at this time.';
  104. }
  105. }
  106. else
  107. {
  108. return $this->msg;
  109. }
  110. }
  111. //----------------------------------------------------------------
  112. // Function to perform Search.
  113. function performSearch($searchTerm)
  114. {
  115. // This is what bitsoup is looking for in a request.
  116. // http://www.bitsoup.org/browse.php?search=test&cat=23&incldead=1
  117. // create the request string.
  118. $searchTerm = str_replace(" ", "+", $searchTerm);
  119. $request = "/browse.php?search=".$searchTerm;
  120. if(!empty($cat))
  121. {
  122. $request .= "&cat=".$cat;
  123. }
  124. $incldead = tfb_getRequestVar('incldead');
  125. if (empty($incldead)) $incldead = "0";
  126. $request .= "&incldead=".$incldead;
  127. if (!empty($this->pg))
  128. {
  129. $request .= "&page=" . $this->pg;
  130. }
  131. if ($this->makeRequest($request,true))
  132. {
  133. return $this->parseResponse();
  134. }
  135. else
  136. {
  137. return $this->msg;
  138. }
  139. }
  140. //----------------------------------------------------------------
  141. // Override the base to show custom table header.
  142. // Function to setup the table header
  143. function tableHeader()
  144. {
  145. $tmpStr = $this->htmlPage;
  146. if (strpos($tmpStr,">Wait</") > 0)
  147. {
  148. $needWait = true;
  149. }
  150. else
  151. {
  152. $needWait = false;
  153. }
  154. $output = "<table width=\"100%\" cellpadding=3 cellspacing=0 border=0>";
  155. $output .= "<br>\n";
  156. $output .= "<tr bgcolor=\"".$this->cfg["bgLight"]."\">";
  157. if ($needWait)
  158. {
  159. $output .= " <td colspan=8 align=center>";
  160. }
  161. else
  162. {
  163. $output .= " <td colspan=7 align=center>";
  164. }
  165. $tmpStr = substr($tmpStr,strpos($tmpStr,"\"statusbar\""));
  166. $tmpStr = substr($tmpStr,strpos($tmpStr,"<font"));
  167. $output .= "<font size=5px> Current Stats : ".strip_tags(substr("<td>".$tmpStr,0,strpos($tmpStr,"</td>")))."</font>";
  168. $output .= "</td>";
  169. $output .= "<tr bgcolor=\"".$this->cfg["table_header_bg"]."\">";
  170. $output .= " <td>&nbsp;</td>";
  171. $output .= " <td><strong>Torrent Name</strong> &nbsp;(";
  172. $tmpURI = str_replace(array("?hideSeedless=yes","&hideSeedless=yes","?hideSeedless=no","&hideSeedless=no"),"",$_SERVER["REQUEST_URI"]);
  173. // Check to see if Question mark is there.
  174. if (strpos($tmpURI,'?'))
  175. {
  176. $tmpURI .= "&";
  177. }
  178. else
  179. {
  180. $tmpURI .= "?";
  181. }
  182. if($this->hideSeedless == "yes")
  183. {
  184. $output .= "<a href=\"". $tmpURI . "hideSeedless=no\">Show Seedless</a>";
  185. }
  186. else
  187. {
  188. $output .= "<a href=\"". $tmpURI . "hideSeedless=yes\">Hide Seedless</a>";
  189. }
  190. $output .= ")</td>";
  191. $output .= " <td><strong>Category</strong></td>";
  192. $output .= " <td align=center><strong>&nbsp;&nbsp;Size</strong></td>";
  193. $output .= " <td><strong>Seeds</strong></td>";
  194. $output .= " <td><strong>Peers</strong></td>";
  195. if ($needWait)
  196. {
  197. $output .= " <td><strong>Wait</strong></td>";
  198. }
  199. $output .= " <td><strong>Snatched</strong></td>";
  200. $output .= "</tr>\n";
  201. return $output;
  202. }
  203. //----------------------------------------------------------------
  204. // Function to parse the response.
  205. function parseResponse($latest = true)
  206. {
  207. $output = $this->tableHeader();
  208. $thing = $this->htmlPage;
  209. if(strpos($thing,"Error:") > 0)
  210. {
  211. $tmpStr = substr($thing,strpos($thing,"Error:")+strlen("Error:"));
  212. $tmpStr = substr($tmpStr,0,strpos($tmpStr,"</p>"));
  213. $this->msg = strip_tags($tmpStr);
  214. return $output . "<center>".$this->msg."</center><br>";
  215. }
  216. // We got a response so display it.
  217. // Chop the front end off.
  218. $thing = substr($thing,strpos($thing,">Upped&nbsp;by<"));
  219. $thing = substr($thing,strpos($thing,"</tr>")+strlen("</tr>"));
  220. $tmpList = substr($thing,0,strpos($thing,"</table>"));
  221. // ok so now we have the listing.
  222. $tmpListArr = explode("</tr>",$tmpList);
  223. $bg = $this->cfg["bgLight"];
  224. foreach($tmpListArr as $key =>$value)
  225. {
  226. //echo $value;
  227. $buildLine = true;
  228. if (strpos($value,"id="))
  229. {
  230. $ts = new bitSoup($value, $this->mainURL);
  231. // Determine if we should build this output
  232. if (is_int(array_search($ts->MainId,$this->catFilter)))
  233. {
  234. $buildLine = false;
  235. }
  236. if ($this->hideSeedless == "yes")
  237. {
  238. if($ts->Seeds == "N/A" || $ts->Seeds == "0")
  239. {
  240. $buildLine = false;
  241. }
  242. }
  243. if (!empty($ts->torrentFile) && $buildLine) {
  244. $output .= trim($ts->BuildOutput($bg,$this->searchURL()));
  245. // ok switch colors.
  246. if ($bg == $this->cfg["bgLight"])
  247. {
  248. $bg = $this->cfg["bgDark"];
  249. }
  250. else
  251. {
  252. $bg = $this->cfg["bgLight"];
  253. }
  254. }
  255. }
  256. }
  257. // set thing to end of this table.
  258. $thing = substr($thing,strpos($thing,"</table>"));
  259. $output .= "</table>";
  260. // is there paging at the bottom?
  261. if (strpos($thing, "page=") != false)
  262. {
  263. // Yes, then lets grab it and display it! ;)
  264. $pages = substr($thing,strpos($thing,"<p"));
  265. $pages = substr($pages,strpos($pages,">"));
  266. $pages = substr($pages,0,strpos($pages,"</p>"));
  267. $pages = str_replace("&nbsp; ",'',$pages);
  268. $tmpPageArr = explode("</a>",$pages);
  269. array_pop($tmpPageArr);
  270. $pagesout = '';
  271. foreach($tmpPageArr as $key => $value)
  272. {
  273. $value .= "</a> &nbsp;";
  274. //$tmpVal = substr($value,strpos($value,"browse.php?"));
  275. $tmpVal = substr($value,strpos($value,"browse.php?"),strpos($value,"\">")-2);
  276. $pgNum = substr($tmpVal,strpos($tmpVal,"page=")+strlen("page="));
  277. $pagesout .= str_replace($tmpVal,"XXXURLXXX".$pgNum,$value);
  278. }
  279. $pagesout = str_replace("se.php?page=","",$pagesout);
  280. $cat = tfb_getRequestVar('mainGenre');
  281. if (empty($cat)) $cat = tfb_getRequestVar('cat');
  282. if(strpos($this->curRequest,"LATEST"))
  283. {
  284. if (!empty($cat))
  285. {
  286. $pages = str_replace("XXXURLXXX",$this->searchURL()."&LATEST=1&cat=".$cat."&pg=",$pagesout);
  287. }
  288. else
  289. {
  290. $pages = str_replace("XXXURLXXX",$this->searchURL()."&LATEST=1&pg=",$pagesout);
  291. }
  292. }
  293. else
  294. {
  295. if(!empty($cat))
  296. {
  297. $pages = str_replace("XXXURLXXX",$this->searchURL()."&searchterm=".$_REQUEST["searchterm"]."&cat=".$cat."&pg=",$pagesout);
  298. }
  299. else
  300. {
  301. $pages = str_replace("XXXURLXXX",$this->searchURL()."&searchterm=".$_REQUEST["searchterm"]."&pg=",$pagesout);
  302. }
  303. }
  304. $output .= "<div align=center>".substr($pages,1)."</div>";
  305. }
  306. return $output;
  307. }
  308. }
  309. // This is a worker class that takes in a row in a table and parses it.
  310. class bitSoup
  311. {
  312. var $torrentName = "";
  313. var $torrentDisplayName = "";
  314. var $torrentFile = "";
  315. var $torrentSize = "";
  316. var $torrentStatus = "";
  317. var $MainId = "";
  318. var $MainCategory = "";
  319. var $fileCount = "";
  320. var $Seeds = "";
  321. var $Peers = "";
  322. var $needsWait = false;
  323. var $waitTime = "";
  324. var $Data = "";
  325. var $torrentRating = "";
  326. function bitSoup( $htmlLine, $dwnURL )
  327. {
  328. if (strlen($htmlLine) > 0)
  329. {
  330. $this->Data = $htmlLine;
  331. // Cleanup any bugs in the HTML
  332. $htmlLine = preg_replace("/</td>\n</td>/i",'</td>',$htmlLine);
  333. // Chunck up the row into columns.
  334. $tmpListArr = explode("<td ",$htmlLine);
  335. if(count($tmpListArr) > 12)
  336. {
  337. $tmpStr = substr($tmpListArr["1"],strpos($tmpListArr["1"],"alt=\"")+strlen("alt=\"")); // MainCategory
  338. $this->MainCategory = substr($tmpStr,0,strpos($tmpStr,"\""));
  339. $tmpStr = substr($tmpListArr["1"],strpos($tmpListArr["1"],"cat=")+strlen("cat=")); // Main Id
  340. $this->MainId = trim(substr($tmpStr,0,strpos($tmpStr,"\"")));
  341. if (empty($this->MainId))
  342. {
  343. if(strpos($tmpListArr["1"],"pron") > 0 )
  344. {
  345. $this->MainId = "9";
  346. $this->MainCategory = "Pron";
  347. }
  348. }
  349. $this->torrentName = $this->cleanLine("<td ".$tmpListArr["2"]."</td>"); // TorrentName
  350. $tmpStr = substr($tmpListArr["2"],strpos($tmpListArr["2"],"id=")+strlen("id=")); // File Id
  351. $tmpStr = substr($tmpStr,0,strpos($tmpStr,"&"));
  352. $this->torrentFile = "http://".$dwnURL."/download.php/".$tmpStr."/";
  353. if($this->MainId == "9")
  354. {
  355. $this->torrentFile .= $this->torrentName.".torrent";
  356. }
  357. else
  358. {
  359. $this->torrentFile .= str_replace(" ","_",$this->torrentName).".torrent";
  360. }
  361. $this->needsWait = true;
  362. $this->waitTime = $this->cleanLine("<td ".$tmpListArr["4"]."</td>"); // Wait Time
  363. $this->fileCount = $this->cleanLine("<td ".$tmpListArr["5"]."</td>"); // File Count
  364. $this->torrentSize = $this->cleanLine("<td ".$tmpListArr["8"]."</td>"); // Size of File
  365. $this->torrentStatus = $this->cleanLine(str_replace("<br>"," ","<td ".$tmpListArr["9"]."</td>")); // Snatched
  366. $this->Seeds = $this->cleanLine("<td ".$tmpListArr["10"]."</td>"); // Seeds
  367. $this->Peers = $this->cleanLine("<td ".$tmpListArr["11"]."</td>"); // Leech
  368. if ($this->Peers == '')
  369. {
  370. $this->Peers = "N/A";
  371. if (empty($this->Seeds)) $this->Seeds = "N/A";
  372. }
  373. if ($this->Seeds == '') $this->Seeds = "N/A";
  374. $this->torrentDisplayName = $this->torrentName;
  375. if(strlen($this->torrentDisplayName) > 50)
  376. {
  377. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  378. }
  379. }
  380. elseif (count($tmpListArr) > 11)
  381. {
  382. $tmpStr = substr($tmpListArr["1"],strpos($tmpListArr["1"],"alt=\"")+strlen("alt=\"")); // MainCategory
  383. $this->MainCategory = substr($tmpStr,0,strpos($tmpStr,"\""));
  384. $tmpStr = substr($tmpListArr["1"],strpos($tmpListArr["1"],"cat=")+strlen("cat=")); // Main Id
  385. $this->MainId = substr($tmpStr,0,strpos($tmpStr,"\""));
  386. if (empty($this->MainId))
  387. {
  388. if(strpos($tmpListArr["1"],"pron") > 0 )
  389. {
  390. $this->MainId = "9";
  391. $this->MainCategory = "Pron";
  392. }
  393. }
  394. $this->torrentName = $this->cleanLine("<td ".$tmpListArr["2"]."</td>"); // TorrentName
  395. $tmpStr = substr($tmpListArr["2"],strpos($tmpListArr["2"],"id=")+strlen("id=")); // File Id
  396. $tmpStr = substr($tmpStr,0,strpos($tmpStr,"&"));
  397. $this->torrentFile = "http://".$dwnURL."/download.php/".$tmpStr."/";
  398. if($this->MainId == "9")
  399. {
  400. $this->torrentFile .= $this->torrentName.".torrent";
  401. }
  402. else
  403. {
  404. $this->torrentFile .= str_replace(" ","_",$this->torrentName).".torrent";
  405. }
  406. $this->fileCount = $this->cleanLine("<td ".$tmpListArr["4"]."</td>"); // File Count
  407. $this->torrentSize = $this->cleanLine("<td ".$tmpListArr["7"]."</td>"); // Size of File
  408. $this->torrentStatus = $this->cleanLine(str_replace("<br>"," ","<td ".$tmpListArr["8"]."</td>")); // Snatched
  409. $this->Seeds = $this->cleanLine("<td ".$tmpListArr["9"]."</td>"); // Seeds
  410. $this->Peers = $this->cleanLine("<td ".$tmpListArr["10"]."</td>"); // Leech
  411. if ($this->Peers == '')
  412. {
  413. $this->Peers = "N/A";
  414. if (empty($this->Seeds)) $this->Seeds = "N/A";
  415. }
  416. if ($this->Seeds == '') $this->Seeds = "N/A";
  417. $this->torrentDisplayName = $this->torrentName;
  418. if(strlen($this->torrentDisplayName) > 50)
  419. {
  420. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  421. }
  422. }
  423. }
  424. }
  425. function cleanLine($stringIn,$tags='')
  426. {
  427. if(empty($tags))
  428. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn)));
  429. else
  430. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn,$tags)));
  431. }
  432. //----------------------------------------------------------------
  433. // Function to build output for the table.
  434. function BuildOutput($bg, $searchURL = '')
  435. {
  436. $output = "<tr>\n";
  437. $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";
  438. $output .= " <td bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\" title=\"".$this->torrentName."\">".$this->torrentDisplayName."</a></td>\n";
  439. if (strlen($this->MainCategory) > 1){
  440. $genre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  441. }else{
  442. $genre = "";
  443. }
  444. $output .= " <td bgcolor=\"".$bg."\">". $genre ."</td>\n";
  445. $output .= " <td bgcolor=\"".$bg."\" align=right>".$this->torrentSize."</td>\n";
  446. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Seeds."</td>\n";
  447. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Peers."</td>\n";
  448. if ($this->needsWait)
  449. {
  450. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->waitTime."</td>\n";
  451. }
  452. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->torrentStatus."</td>\n";
  453. $output .= "</tr>\n";
  454. return $output;
  455. }
  456. }
  457. ?>