1
0

TorrentSpyEngine.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /* $Id: TorrentSpyEngine.php 3282 2007-11-18 17:21:19Z warion $ */
  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 - Changed Main Categories. (removed TV, changed Movies to Video)
  23. v 1.02 - Mar 19, 06 - Updated pageing.
  24. v 1.03 - May 16, 06 - They changed there URL for Searching back to their domain.
  25. v 1.04 - Sep 21, 06 - Fix for Bug found by batmark
  26. v 1.04a - Nov 18, 07 - Quick fix for 'download.asp?id=' URLs not working anymore.
  27. */
  28. class SearchEngine extends SearchEngineBase
  29. {
  30. function SearchEngine($cfg)
  31. {
  32. $this->mainURL = "torrentspy.com";
  33. $this->altURL = "www.torrentspy.com";
  34. $this->mainTitle = "TorrentSpy";
  35. $this->engineName = "TorrentSpy";
  36. $this->author = "kboy";
  37. $this->version = "1.04-tfb";
  38. $this->updateURL = "http://www.torrentflux.com/forum/index.php/topic,874.0.html";
  39. $this->Initialize($cfg);
  40. }
  41. function populateMainCategories()
  42. {
  43. $this->mainCatalog["11"] = "Adult";
  44. $this->mainCatalog["6"] = "Anime";
  45. $this->mainCatalog["1"] = "Applications";
  46. $this->mainCatalog["2"] = "Games";
  47. $this->mainCatalog["13"] = "Handheld";
  48. $this->mainCatalog["7"] = "Hentai";
  49. $this->mainCatalog["8"] = "Linux";
  50. $this->mainCatalog["9"] = "Macintosh";
  51. $this->mainCatalog["10"] = "Misc";
  52. $this->mainCatalog["3"] = "Music";
  53. $this->mainCatalog["14"] = "Non-English";
  54. $this->mainCatalog["12"] = "Unsorted/Other";
  55. $this->mainCatalog["4"] = "Videos";
  56. }
  57. //----------------------------------------------------------------
  58. // Function to Get Sub Categories
  59. function getSubCategories($mainGenre)
  60. {
  61. $output = array();
  62. if (strpos($mainGenre,'/'))
  63. {
  64. $request = '/directory/' . $mainGenre;
  65. }
  66. else
  67. {
  68. $request = '/directory.asp?mode=main&id=' . $mainGenre;
  69. }
  70. $mainGenreName = $this->GetMainCatName($mainGenre);
  71. if ($this->makeRequest($request))
  72. {
  73. $thing = $this->htmlPage;
  74. while (is_integer(strpos($thing,"href=\"/directory/")))
  75. {
  76. $thing = substr($thing,strpos($thing,"href=\"/directory/")+strlen("href=\"/directory/"));
  77. $tmpStr = str_replace(array("%2f","+")," ",substr($thing,0,strpos($thing,"\"")));
  78. $tmpStr = str_replace("%2d","-",$tmpStr);
  79. $subid = $tmpStr;
  80. $thing = substr($thing,strpos($thing,">")+strlen(">"));
  81. $subname = trim(substr($thing,0,strpos($thing,"<")));
  82. if($subname != $mainGenreName)
  83. {
  84. $output[$subid] = $subname;
  85. }
  86. }
  87. }
  88. return $output;
  89. }
  90. //----------------------------------------------------------------
  91. // Function to Make the Request (overriding base)
  92. function makeRequest($request)
  93. {
  94. if (strpos($request,"search.asp"))
  95. {
  96. return parent::makeRequest($request, true);
  97. }
  98. else
  99. {
  100. return parent::makeRequest($request, false);
  101. }
  102. }
  103. //----------------------------------------------------------------
  104. // Function to get Latest..
  105. function getLatest()
  106. {
  107. $request = '/latest.asp';
  108. // Added mode to support yesterday request.
  109. if (array_key_exists("mode",$_REQUEST))
  110. {
  111. $request .='?mode='.$_REQUEST["mode"];
  112. if (array_key_exists("id",$_REQUEST))
  113. {
  114. $request .= '&id=' . $_REQUEST["id"];
  115. }
  116. if (!empty($this->pg))
  117. {
  118. $request .= '&pg=' . $this->pg;
  119. }
  120. }
  121. elseif (array_key_exists("subGenre",$_REQUEST))
  122. {
  123. if (strpos($_REQUEST["subGenre"],"/")>0)
  124. {
  125. $request ='/directory.asp?mode=sub&id=' . substr($_REQUEST["subGenre"],0,strpos($_REQUEST["subGenre"],"/"));
  126. }
  127. else
  128. {
  129. $request ='/directory.asp?mode=sub&id=' . $_REQUEST["subGenre"];
  130. }
  131. if (!empty($this->pg))
  132. {
  133. $request .= '&pg=' . $this->pg;
  134. }
  135. }
  136. elseif (!empty($this->pg))
  137. {
  138. $request .= '?pg=' . $this->pg;
  139. }
  140. if ($this->makeRequest($request))
  141. {
  142. return $this->parseResponse();
  143. }
  144. else
  145. {
  146. return $this->msg;
  147. }
  148. }
  149. //----------------------------------------------------------------
  150. // Function to perform Search.
  151. function performSearch($searchTerm)
  152. {
  153. if (array_key_exists("directory",$_REQUEST))
  154. {
  155. $request = "/directory/" . str_replace("%2F","/",urlencode($_REQUEST["directory"]));
  156. if (!empty($this->pg))
  157. {
  158. $request .= "?pg=" . $this->pg;
  159. }
  160. }
  161. elseif (array_key_exists("getMain",$_REQUEST))
  162. {
  163. $request = '/directory';
  164. }
  165. elseif (array_key_exists("mainGenre",$_REQUEST))
  166. {
  167. if (strpos($_REQUEST["mainGenre"],'/'))
  168. {
  169. $request .= '/' . $_REQUEST["mainGenre"];
  170. }
  171. else
  172. {
  173. if (!empty($this->pg))
  174. {
  175. $request = '/directory.asp?mode=main&id=' . $_REQUEST["mainGenre"] . '&pg=' . $this->pg;
  176. }
  177. else
  178. {
  179. $request = '/directory.asp?mode=main&id=' . $_REQUEST["mainGenre"];
  180. }
  181. }
  182. }
  183. elseif (array_key_exists("subGenre",$_REQUEST))
  184. {
  185. if (strpos($_REQUEST["subGenre"],'/'))
  186. {
  187. $request = "/directory/" . $_REQUEST["subGenre"];
  188. }
  189. else
  190. {
  191. if ($_REQUEST["subGenre"] != "")
  192. {
  193. if (!empty($this->pg))
  194. {
  195. $request = '/directory.asp?mode=sub&id=' . $_REQUEST["subGenre"] . '&pg=' . $this->pg;
  196. }
  197. else
  198. {
  199. $request = '/directory.asp?mode=sub&id=' . $_REQUEST["subGenre"];
  200. }
  201. }
  202. }
  203. }
  204. else
  205. {
  206. $searchTerm = str_replace(" ", "+", $searchTerm);
  207. if ( $this->pg != '' && array_key_exists("db",$_REQUEST))
  208. {
  209. $request = '/search.asp?h=&query=' . $searchTerm . '&pg=' . $this->pg . '&db=' . $_REQUEST["db"] . '&submit.x=24&submit.y=10';
  210. }
  211. else
  212. {
  213. $request = '/search.asp?h=&query=' . $searchTerm . '&submit.x=24&submit.y=10';
  214. }
  215. }
  216. if ($this->makeRequest($request))
  217. {
  218. return $this->parseResponse();
  219. }
  220. else
  221. {
  222. return $this->msg;
  223. }
  224. }
  225. //----------------------------------------------------------------
  226. // Function to parse the response.
  227. function parseResponse()
  228. {
  229. $output = $this->tableHeader();
  230. $thing = $this->htmlPage;
  231. if (strpos($thing, "experiencing high load") > 0)
  232. {
  233. $output .= "<b>We're experiencing high load at this time. Please use the directory until further notice.</b>";
  234. }else{
  235. // We got a response so display it.
  236. // Chop the front end off.
  237. while (is_integer(strpos($thing,">Health<")))
  238. {
  239. $thing = substr($thing,strpos($thing,">Health<"));
  240. $thing = substr($thing,strpos($thing,"<tr"));
  241. $tmpList = substr($thing,0,strpos($thing,"</table>"));
  242. // ok so now we have the listing.
  243. $tmpListArr = explode("</tr>",$tmpList);
  244. $langFile = $this->cfg['_FILE'];
  245. $bg = $this->cfg["bgLight"];
  246. foreach($tmpListArr as $key =>$value)
  247. {
  248. $buildLine = true;
  249. if (strpos($value,"/torrent/"))
  250. {
  251. $ts = new tSpy($value);
  252. // Determine if we should build this output
  253. if (is_int(array_search($ts->MainCategory,$this->catFilter)))
  254. {
  255. $buildLine = false;
  256. }
  257. if ($this->hideSeedless == "yes")
  258. {
  259. if($ts->Seeds == "N/A" || $ts->Seeds == "0")
  260. {
  261. $buildLine = false;
  262. }
  263. }
  264. if (!empty($ts->torrentFile) && $buildLine) {
  265. $output .= trim($ts->BuildOutput($bg,$langFile,$this->searchURL()));
  266. // ok switch colors.
  267. if ($bg == $this->cfg["bgLight"])
  268. {
  269. $bg = $this->cfg["bgDark"];
  270. }
  271. else
  272. {
  273. $bg = $this->cfg["bgLight"];
  274. }
  275. }
  276. }
  277. }
  278. // set thing to end of this table.
  279. $thing = substr($thing,strpos($thing,"</table>"));
  280. }
  281. }
  282. $output .= "</table>";
  283. // is there paging at the bottom?
  284. if (strpos($thing, "<p class=\"pagenav\">Pages (") !== false)
  285. {
  286. // Yes, then lets grab it and display it! ;)
  287. $thing = substr($thing,strpos($thing,"<p class=\"pagenav\">Pages (")+strlen("<p class=\"pagenav\">"));
  288. $pages = substr($thing,0,strpos($thing,"</p>"));
  289. $page1 = substr($pages,0,strpos($pages,"<img"));
  290. $page2 = substr($pages,strlen($page1));
  291. $page2 = substr($page2,strpos($page2,'>')+1);
  292. $pages = $page1.$page2;
  293. if (strpos($pages,"directory"))
  294. {
  295. $pages = str_replace("?","&",$pages);
  296. $pages = str_replace("/directory/", $this->searchURL()."&directory=", $pages);
  297. }
  298. elseif (strpos($pages, "search.asp?"))
  299. {
  300. $pages = str_replace("search.asp?", $this->searchURL()."&", $pages);
  301. }
  302. elseif (strpos($pages, "/search/"))
  303. {
  304. $pages = str_replace("&amp;","&",$pages);
  305. $pages = str_replace("?","&",$pages);
  306. $pages = str_replace("/search/", $this->searchURL()."&searchterm=", $pages);
  307. }
  308. elseif (strpos($pages,"latest.asp?"))
  309. {
  310. $pages = str_replace("latest.asp?mode=", $this->searchURL()."&LATEST=1&mode=", $pages);
  311. if (!array_key_exists("mode",$_REQUEST))
  312. {
  313. $pages .= "<br><a href=\"".$this->searchURL()."&LATEST=1&mode=yesterday\" title=\"Yesterday's Latest\">Yesterday's Latest</a>";
  314. }
  315. elseif (! $_REQUEST["mode"] == "yesterday")
  316. {
  317. $pages .= "<br><a href=\"".$this->searchURL()."&LATEST=1&mode=yesterday\" title=\"Yesterday's Latest\">Yesterday's Latest</a>";
  318. }
  319. else
  320. {
  321. $pages .= "<br><b>Yesterday's Latest</b>";
  322. }
  323. }
  324. $output .= "<br><div align=center>".$pages."</div><br>";
  325. }
  326. elseif(array_key_exists("LATEST",$_REQUEST))
  327. {
  328. $pages = '';
  329. if (!array_key_exists("mode",$_REQUEST))
  330. {
  331. $pages .= "<br><a href=\"".$this->searchURL()."&LATEST=1&mode=yesterday\" title=\"Yesterday's Latest\">Yesterday's Latest</a>";
  332. }
  333. elseif (! $_REQUEST["mode"] == "yesterday")
  334. {
  335. $pages .= "<br><a href=\"".$this->searchURL()."&LATEST=1&mode=yesterday\" title=\"Yesterday's Latest\">Yesterday's Latest</a>";
  336. }
  337. else
  338. {
  339. $pages .= "<br><b>Yesterday's Latest</b>";
  340. }
  341. $output .= "<br><div align=center>".$pages."</div><br>";
  342. }
  343. return $output;
  344. }
  345. }
  346. // This is a worker class that takes in a row in a table and parses it.
  347. class tSpy
  348. {
  349. var $torrentName = "";
  350. var $torrentDisplayName = "";
  351. var $torrentFile = "";
  352. var $torrentStatus = "";
  353. var $MainId = "";
  354. var $MainCategory = "";
  355. var $SubId = "";
  356. var $SubCategory = "";
  357. var $torrentSize = "";
  358. var $fileCount = "";
  359. var $Seeds = "";
  360. var $Peers = "";
  361. var $Data = "";
  362. function tSpy( $htmlLine )
  363. {
  364. if (strlen($htmlLine) > 0)
  365. {
  366. $this->Data = $htmlLine;
  367. // Chunck up the row into columns.
  368. $tmpListArr = explode("</td>",$htmlLine);
  369. if(count($tmpListArr) > 5)
  370. {
  371. //$tmpListArr["0"]; // Torrent Name, Download Link, Status
  372. $this->torrentDisplayName = $this->cleanLine($tmpListArr["0"]); // TorrentName
  373. $tmpStr = substr($tmpListArr["0"],strpos($tmpListArr["0"],"/torrent/")+strlen("/torrent/")); // Download Link
  374. $this->torrentFile = "http://www.torrentspy.com/torrent/" . substr($tmpStr,0,strpos($tmpStr,"/")) . "/";
  375. $tmpStatus = substr($tmpStr,strpos($tmpStr,"title=\"")+strlen("title=\""));
  376. $tmpStatus = substr($tmpStatus,0,strpos($tmpStatus,">"));
  377. if (strpos($tmpStatus,"password"))
  378. {
  379. $this->torrentStatus = "P";
  380. }elseif (strpos($tmpStatus,"register"))
  381. {
  382. $this->torrentStatus = "R";
  383. }
  384. $tmpStr = substr($tmpStr,strpos($tmpStr,">")+strlen(">"));
  385. if(strpos($tmpStr,"/torrent/") > 0)
  386. {
  387. $tmpStr = substr($tmpStr,strpos($tmpStr,"/torrent/")+strlen("/torrent/"));
  388. }
  389. $tmpStr = substr($tmpStr,strpos($tmpStr,"title=\"")+strlen("title=\""));
  390. $this->torrentName = substr($tmpStr,0,strpos($tmpStr,"\""));
  391. $tmpStr = $tmpListArr["1"]; // Categories
  392. if(strpos($tmpStr,"mode=")){
  393. $tmpStr = substr($tmpStr,strpos($tmpStr,"mode=main&id=")+strlen("mode=main&id="));
  394. $this->MainId = substr($tmpStr,0,strpos($tmpStr,"\""));
  395. }else{
  396. $tmpStr = substr($tmpStr,strpos($tmpStr,"/directory/")+strlen("/directory/"));
  397. $this->MainId = substr($tmpStr,0,strpos($tmpStr,"/"));
  398. }
  399. $tmpStr = substr($tmpStr,strpos($tmpStr,"title=\"")+strlen("title=\""));
  400. $this->MainCategory = substr($tmpStr,0,strpos($tmpStr,"\""));
  401. if(strpos($tmpStr,"mode=")){
  402. $tmpStr = substr($tmpStr,strpos($tmpStr,"mode=sub&id=")+strlen("mode=sub&id="));
  403. $this->SubId = substr($tmpStr,0,strpos($tmpStr,"\""));
  404. }else{
  405. $tmpStr = substr($tmpStr,strpos($tmpStr,"/directory/")+strlen("/directory/"));
  406. $this->SubId = substr($tmpStr,0,strpos($tmpStr,"/"));
  407. }
  408. $tmpStr = substr($tmpStr,strpos($tmpStr,"title=\"")+strlen("title=\""));
  409. $this->SubCategory = substr($tmpStr,0,strpos($tmpStr,"\""));
  410. $this->torrentSize = $this->cleanLine($tmpListArr["2"]); // Size of File
  411. $this->fileCount = $this->cleanLine($tmpListArr["3"]); // File Count
  412. $this->Seeds = $this->cleanLine($tmpListArr["4"]); // Seeds
  413. $this->Peers = $this->cleanLine($tmpListArr["5"]); // Peers
  414. //$tmpListArr["6"] = $this->cleanLine($tmpListArr["6"]); // Health
  415. if ($this->Peers == '')
  416. {
  417. $this->Peers = "N/A";
  418. if (empty($this->Seeds)) $this->Seeds = "N/A";
  419. }
  420. if ($this->Seeds == '') $this->Seeds = "N/A";
  421. if(strlen($this->torrentDisplayName) > 50)
  422. {
  423. $this->torrentDisplayName = substr($this->torrentDisplayName,0,50)."...";
  424. }
  425. }
  426. }
  427. }
  428. function cleanLine($stringIn,$tags='')
  429. {
  430. if(empty($tags))
  431. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn)));
  432. else
  433. return trim(str_replace(array("&nbsp;","&nbsp")," ",strip_tags($stringIn,$tags)));
  434. }
  435. //----------------------------------------------------------------
  436. // Function to build output for the table.
  437. function BuildOutput($bg,$langFILE, $searchURL = '')
  438. {
  439. $output = "<tr>\n";
  440. $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";
  441. $output .= " <td bgcolor=\"".$bg."\"><a href=\"dispatcher.php?action=urlUpload&type=torrent&url=".$this->torrentFile."\" title=\"".$this->torrentName."\">".$this->torrentDisplayName."</a>";
  442. switch ($this->torrentStatus)
  443. {
  444. case "R":
  445. $output .= " <span title='Registration needed'><b>R</b></span>";
  446. break;
  447. case "P":
  448. $output .= " <span title='Password needed'><b>P</b></span>";
  449. break;
  450. }
  451. $output .= "</td>\n";
  452. if (strlen($this->MainCategory) > 1){
  453. if (strlen($this->SubCategory) > 1){
  454. $mainGenre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  455. $subGenre = "<a href=\"".$searchURL."&subGenre=".$this->SubId."\">".$this->SubCategory."</a>";
  456. $genre = $mainGenre."-".$subGenre;
  457. }else{
  458. $genre = "<a href=\"".$searchURL."&mainGenre=".$this->MainId."\">".$this->MainCategory."</a>";
  459. }
  460. }else{
  461. $genre = "<a href=\"".$searchURL."&subGenre=".$this->SubId."\">".$this->SubCategory."</a>";
  462. }
  463. $output .= " <td bgcolor=\"".$bg."\">". $genre ."</td>\n";
  464. $output .= " <td bgcolor=\"".$bg."\" align=right>".$this->torrentSize."</td>\n";
  465. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Seeds."</td>\n";
  466. $output .= " <td bgcolor=\"".$bg."\" align=center>".$this->Peers."</td>\n";
  467. $output .= "</tr>\n";
  468. return $output;
  469. }
  470. }
  471. ?>