TorrentPortalEngine.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /* $Id: TorrentPortalEngine.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.01 - change in parsing routine
  23. v 1.02 - Mar 19, 06 - another change in the parsing. and updated paging
  24. v 1.03 - Updated to check for a count on the array
  25. */
  26. class SearchEngine extends SearchEngineBase
  27. {
  28. function SearchEngine($cfg)
  29. {
  30. $this->mainURL = "torrentportal.com";
  31. $this->altURL = "tp.searching.com";
  32. $this->mainTitle = "TorrentPortal";
  33. $this->engineName = "TorrentPortal";
  34. $this->author = "kboy";
  35. $this->version = "1.03-tfb";
  36. $this->updateURL = "http://www.torrentflux.com/forum/index.php?topic=875.0.html";
  37. $this->Initialize($cfg);
  38. }
  39. //----------------------------------------------------------------
  40. // Function to Get Main Categories
  41. function populateMainCategories()
  42. {
  43. $this->mainCatalog["0"] = "(all types)";
  44. $this->mainCatalog["1"] = "Games";
  45. $this->mainCatalog["2"] = "Movies";
  46. $this->mainCatalog["3"] = "TV";
  47. $this->mainCatalog["4"] = "Videos";
  48. $this->mainCatalog["5"] = "Apps";
  49. $this->mainCatalog["6"] = "Anime";
  50. $this->mainCatalog["7"] = "Audio";
  51. $this->mainCatalog["8"] = "Comics";
  52. $this->mainCatalog["9"] = "Unsorted";
  53. $this->mainCatalog["10"] = "Porn";
  54. }
  55. //----------------------------------------------------------------
  56. // Function to Make the Request (overriding base)
  57. function makeRequest($request)
  58. {
  59. return parent::makeRequest($request, false);
  60. }
  61. //----------------------------------------------------------------
  62. // Function to get Latest..
  63. function getLatest()
  64. {
  65. $cat = tfb_getRequestVar('subGenre');
  66. $count = tfb_getRequestVar('count');
  67. if (empty($cat)) $cat = tfb_getRequestVar('cat');
  68. if(empty($cat) && empty($this->pg))
  69. {
  70. $request = "/new-torrents.php";
  71. }
  72. else
  73. {
  74. $request = "/torrents.php";
  75. }
  76. if(!empty($cat))
  77. {
  78. if(strpos($request,"?"))
  79. {
  80. $request .= "&cat=".$cat;
  81. }
  82. else
  83. {
  84. $request .= "?cat=".$cat;
  85. }
  86. }
  87. if(!empty($count))
  88. {
  89. if(strpos($request,"?"))
  90. {
  91. $request .= "&count=".$count;
  92. }
  93. else
  94. {
  95. $request .= "?count=".$count;
  96. }
  97. }
  98. if (!empty($this->pg))
  99. {
  100. if(strpos($request,"?"))
  101. {
  102. $request .= "&page=" . $this->pg;
  103. }
  104. else
  105. {
  106. $request .= "?page=" . $this->pg;
  107. }
  108. }
  109. if ($this->makeRequest($request))
  110. {
  111. return $this->parseResponse();
  112. }
  113. else
  114. {
  115. return $this->msg;
  116. }
  117. }
  118. //----------------------------------------------------------------
  119. // Function to perform Search.
  120. function performSearch($searchTerm)
  121. {
  122. $searchTerm = str_replace(" ", "+", $searchTerm);
  123. $request = "/torrents-search.php?search=".$searchTerm;
  124. if(!empty($cat))
  125. {
  126. $request .= "&cat=".$cat;
  127. }
  128. $count = tfb_getRequestVar('count');
  129. if(!empty($count))
  130. {
  131. $request .= "&count=".$count;
  132. }
  133. $onlyname = tfb_getRequestVar('onlyname');
  134. if (empty($onlyname)) $onlyname = "no";
  135. $request .= "&onlyname=".$onlyname;
  136. $incldead = tfb_getRequestVar('incldead');
  137. if (empty($incldead)) $incldead = "0";
  138. $request .= "&incldead=".$incldead;
  139. $request .= "&submit=";
  140. if (!empty($this->pg))
  141. {
  142. $request .= "&page=" . $this->pg;
  143. }
  144. if ($this->makeRequest($request,true))
  145. {
  146. return $this->parseResponse();
  147. }
  148. else
  149. {
  150. return $this->msg;
  151. }
  152. }
  153. //----------------------------------------------------------------
  154. // Override the base to show custom table header.
  155. // Function to setup the table header
  156. function tableHeader()
  157. {
  158. $output = "<table width=\"100%\" cellpadding=3 cellspacing=0 border=0>";
  159. $output .= "<br>\n";
  160. $output .= "<tr bgcolor=\"".$this->cfg["table_header_bg"]."\">";
  161. $output .= " <td>&nbsp;</td>";
  162. $output .= " <td><strong>Torrent Name</strong> &nbsp;(";
  163. $tmpURI = str_replace(array("?hideSeedless=yes","&hideSeedless=yes","?hideSeedless=no","&hideSeedless=no"),"",$_SERVER["REQUEST_URI"]);
  164. // Check to see if Question mark is there.
  165. if (strpos($tmpURI,'?'))
  166. {
  167. $tmpURI .= "&";
  168. }
  169. else
  170. {
  171. $tmpURI .= "?";
  172. }
  173. if($this->hideSeedless == "yes")
  174. {
  175. $output .= "<a href=\"". $tmpURI . "hideSeedless=no\">Show Seedless</a>";
  176. }
  177. else
  178. {
  179. $output .= "<a href=\"". $tmpURI . "hideSeedless=yes\">Hide Seedless</a>";
  180. }
  181. $output .= ")</td>";
  182. $output .= " <td><strong>Category</strong></td>";
  183. $output .= " <td align=center><strong>&nbsp;&nbsp;Size</strong></td>";
  184. $output .= " <td><strong>Seeds</strong></td>";
  185. $output .= " <td><strong>Peers</strong></td>";
  186. $output .= " <td><strong>Health</strong></td>";
  187. $output .= "</tr>\n";
  188. return $output;
  189. }
  190. //----------------------------------------------------------------
  191. // Function to parse the response.
  192. function parseResponse()
  193. {
  194. $output = $this->tableHeader();
  195. $thing = $this->htmlPage;
  196. // We got a response so display it.
  197. // Chop the front end off.
  198. while (is_integer(strpos($thing,">Health")))
  199. {
  200. $thing = substr($thing,strpos($thing,">Health"));
  201. $thing = substr($thing,strpos($thing,"<tr"));
  202. $tmpList = substr($thing,0,strpos($thing,"</table>"));
  203. // ok so now we have the listing.
  204. $tmpListArr = explode("<tr>",$tmpList);
  205. $bg = $this->cfg["bgLight"];
  206. foreach($tmpListArr as $key =>$value)
  207. {
  208. $buildLine = true;
  209. if (strpos($value,"/download/"))
  210. {
  211. $ts = new tPort($value);
  212. // Determine if we should build this output
  213. if (is_int(array_search($ts->MainCategory,$this->catFilter)))
  214. {
  215. $buildLine = false;
  216. }
  217. if ($this->hideSeedless == "yes")
  218. {
  219. if($ts->Seeds == "N/A" || $ts->Seeds == "0")
  220. {
  221. $buildLine = false;
  222. }
  223. }
  224. if (!empty($ts->torrentFile) && $buildLine) {
  225. $output .= trim($ts->BuildOutput($bg, $this->searchURL()));
  226. // ok switch colors.
  227. if ($bg == $this->cfg["bgLight"])
  228. {
  229. $bg = $this->cfg["bgDark"];
  230. }
  231. else
  232. {
  233. $bg = $this->cfg["bgLight"];
  234. }
  235. }
  236. }
  237. }
  238. // set thing to end of this table.
  239. $thing = substr($thing,strpos($thing,"</table>"));
  240. }
  241. $output .= "</table>";
  242. // is there paging at the bottom?
  243. /*
  244. <p align="center"><b>1&nbsp;-&nbsp;25</b> | <a href="?search=test&amp;count=87&amp;page=1"><b>26&nbsp;-&nbsp;50</b></a> | <a href="?search=test&amp;count=87&amp;page=2"><b>51&nbsp;-&nbsp;75</b></a> | <a href="?search=test&amp;count=87&amp;page=3"><b>76&nbsp;-&nbsp;87</b></a><br /><b>&lt;&lt;&nbsp;Prev</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="?search=test&amp;count=87&amp;page=1"><b>Next&nbsp;&gt;&gt;</b></a></p>
  245. */
  246. if (strpos($thing, "page=") != false)
  247. {
  248. // Yes, then lets grab it and display it! ;)
  249. $thing = substr($thing,strpos($thing,"<p align=\"center\">")+strlen("<p align=\"center\">"));
  250. $pages = substr($thing,0,strpos($thing,"</p>"));
  251. //$output .= $pages;
  252. if(strpos($this->curRequest,"LATEST"))
  253. {
  254. if(strpos($pages,"cat="))
  255. {
  256. $pages = str_replace("page=","pg=",str_replace("?",$this->searchURL()."&LATEST=1&",$pages));
  257. }
  258. else
  259. {
  260. $pages = str_replace("?page=",$this->searchURL()."&LATEST=1&pg=",$pages);
  261. }
  262. }
  263. else
  264. {
  265. if(strpos($pages,"\"?"))
  266. {
  267. $pages = str_replace("?",$this->searchURL()."&",$pages);
  268. }
  269. if(strpos($pages,"?search="))
  270. {
  271. $pages = str_replace("?search=",$this->searchURL()."&searchterm=",$pages);
  272. }
  273. if(strpos($pages,"search="))
  274. {
  275. $pages = str_replace("search=","searchterm=",$pages);
  276. }
  277. }
  278. if(strpos($pages,"torrents.php?"))
  279. {
  280. $pages = str_replace("torrents.php?",$this->searchURL()."&",$pages);
  281. }
  282. if(strpos($pages,"torrents-search.php?"))
  283. {
  284. $pages = str_replace("torrents-search.php?",$this->searchURL()."&",$pages);
  285. }
  286. $pages = str_replace("page=","pg=",$pages);
  287. $output .= "<div align=center>".$pages."</div>";
  288. }
  289. return $output;
  290. }
  291. }
  292. // This is a worker class that takes in a row in a table and parses it.
  293. class tPort
  294. {
  295. var $torrentName = "";
  296. var $torrentDisplayName = "";
  297. var $torrentFile = "";
  298. var $torrentSize = "";
  299. var $torrentStatus = "";
  300. var $MainId = "";
  301. var $MainCategory = "";
  302. var $fileCount = "";
  303. var $Seeds = "";
  304. var $Peers = "";
  305. var $Data = "";
  306. var $torrentRating = "";
  307. function tPort( $htmlLine )
  308. {
  309. if (strlen($htmlLine) > 0)
  310. {
  311. $this->Data = $htmlLine;
  312. // Cleanup any bugs in the HTML
  313. $htmlLine = preg_replace("/</td>\n</td>/i",'</td>',$htmlLine);
  314. // Chunck up the row into columns.
  315. $tmpListArr = explode("<td ",$htmlLine);
  316. if(count($tmpListArr) > 8)
  317. {
  318. $tmpStr = substr($tmpListArr["1"],strpos($tmpListArr["1"],"href=\"")+strlen("href=\"")); // Download Link
  319. $this->torrentFile = "http://www.torrentportal.com".substr($tmpStr,0,strpos($tmpStr,"\""));
  320. $this->MainCategory = $this->cleanLine("<td ".$tmpListArr["3"]."</td>"); // MainCategory
  321. $tmpStr = substr($tmpListArr["2"],strpos($tmpListArr["2"],"cat=")+strlen("cat=")); // Main Id
  322. $this->MainId = substr($tmpStr,0,strpos($tmpStr,"\""));
  323. $this->torrentName = $this->cleanLine("<td ".$tmpListArr["4"]."</td>"); // TorrentName
  324. $this->torrentRating = $this->cleanLine("<td ".$tmpListArr["5"]."</td>"); // Rating
  325. $this->torrentSize = $this->cleanLine("<td ".$tmpListArr["6"]."</td>"); // Size of File
  326. $this->Seeds = $this->cleanLine("<td ".$tmpListArr["7"]."</td>"); // Seeds
  327. $this->Peers = $this->cleanLine("<td ".$tmpListArr["8"]."</td>"); // Leech
  328. $tmpStr = substr($tmpListArr["9"],strpos($tmpListArr["9"],"Health ")+strlen("Health ")); // Health
  329. $tmpStr = substr($tmpStr,0,strpos($tmpStr,"\""));
  330. $tmpArr = explode("/",$tmpStr);
  331. if (count($tmpArr) > 1 && $tmpArr["1"] > 0 )
  332. {
  333. $this->torrentStatus = ($tmpArr["0"] / $tmpArr["1"]) * 100 . "%";
  334. }
  335. else
  336. {
  337. $this->torrentStatus = "0%";
  338. }
  339. if ($this->Peers == '')
  340. {
  341. $this->Peers = "N/A";
  342. if (empty($this->Seeds)) $this->Seeds = "N/A";
  343. }
  344. if ($this->Seeds == '') $this->Seeds = "N/A";
  345. $this->torrentDisplayName = $this->torrentName;
  346. if(strlen($this->torrentDisplayName) > 50)
  347. {
  348. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  349. }
  350. }
  351. }
  352. }
  353. function cleanLine($stringIn,$tags='')
  354. {
  355. if(empty($tags))
  356. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn)));
  357. else
  358. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn,$tags)));
  359. }
  360. //----------------------------------------------------------------
  361. // Function to build output for the table.
  362. function BuildOutput($bg, $searchURL = '')
  363. {
  364. $output = "<tr>\n";
  365. $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";
  366. $output .= " <td bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\" title=\"".$this->torrentName."\">".$this->torrentDisplayName."</a></td>\n";
  367. if (strlen($this->MainCategory) > 1){
  368. $genre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  369. }else{
  370. $genre = "";
  371. }
  372. $output .= " <td bgcolor=\"".$bg."\">". $genre ."</td>\n";
  373. $output .= " <td bgcolor=\"".$bg."\" align=right>".$this->torrentSize."</td>\n";
  374. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Seeds."</td>\n";
  375. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Peers."</td>\n";
  376. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->torrentStatus."</td>\n";
  377. $output .= "</tr>\n";
  378. return $output;
  379. }
  380. }
  381. ?>