SearchEngineBase.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /* $Id: SearchEngineBase.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. This is the base search engine class that is inherited from to
  23. create specialized search engines.
  24. Each new Search must use the following nameing standards.
  25. ???Engine.php where ??? is the search engine name.
  26. !! All changes and customizations should be done in those files and not in this file. !!
  27. */
  28. // Created By Kboy
  29. class SearchEngineBase
  30. {
  31. var $engineName = ''; // The Engine Name Must be the same as the File name
  32. // minus Engine.
  33. var $mainTitle = ''; // The displayed Main Title for the engine.
  34. var $mainURL = ''; // The Primary URL used in searching or paging.
  35. var $altURL = ''; // The alternate URL used in searching or paging.
  36. var $author = ''; // the author of this engine
  37. var $version = '';
  38. var $updateURL = 'http://www.torrentflux.com/forum/index.php/board,14.0.html';
  39. // ------------------------------------------------------------------------------
  40. // You should only need to set the above variables in each of the custom classes.
  41. // ------------------------------------------------------------------------------
  42. var $cfg = array(); // The config array that holds the config settings of
  43. // TorrentFlux at time of initialization
  44. // This may contain the mainCatalog and catFilter
  45. // as assigned by the admin tool.
  46. // If it doesn't then we ask the individual engines for there
  47. // mainCatalog and catFilter.
  48. var $mainCatalogName = ''; // The name of the Main Catalog
  49. var $mainCatalog = array(); // An array of Main Catalog entries.
  50. var $subCatalog = array(); // An array of Sub Catalog entries.
  51. var $catFilterName = ''; // Name of Filter used to retrieve from DB.
  52. var $catFilter = array(); // An array of categories to Filter out from DB.
  53. var $curRequest =''; // The actual Request sent to the Search Engine
  54. var $hideSeedless = false; // Boolean to determine if we should hide or show seedless torrents
  55. var $searchTerm = ''; // Search term passed into the engine
  56. var $htmlPage = ''; // HTML created by the engine for displaying
  57. var $msg = ''; // Message to be displayed
  58. var $pg = ''; // Page Variable used in Paging
  59. var $fp = ''; // Pointer to a socket connection
  60. var $initialized = false; // Boolean to determine if the search engine initialized ok.
  61. /**
  62. * Constructor
  63. */
  64. function SearchEngineBase() {
  65. die('Virtual Class -- cannot instantiate');
  66. }
  67. //----------------------------------------------------------------
  68. // Initialize the Search Engine setting up the Catalog and Filters.
  69. // and Testing the connection.
  70. function Initialize($cfg) {
  71. $rtnValue = false;
  72. $this->cfg = unserialize($cfg);
  73. $this->pg = tfb_getRequestVar('pg');
  74. if (empty($this->altURL))
  75. $this->altURL = $this->mainURL;
  76. if (empty($this->cfg)) {
  77. $this->msg = "Config not passed";
  78. $this->initialized = false;
  79. return;
  80. }
  81. $this->catFilterName = $this->engineName."GenreFilter";
  82. $this->mainCatalogName = $this->engineName."_catalog";
  83. if (array_key_exists('hideSeedless',$_SESSION))
  84. $this->hideSeedless = $_SESSION['hideSeedless'];
  85. $this->catFilter = (array_key_exists($this->catFilterName,$this->cfg))
  86. ? $this->cfg[$this->catFilterName]
  87. : array();
  88. if (array_key_exists($this->mainCatalogName,$this->cfg))
  89. $this->mainCatalog = $this->cfg[$this->mainCatalogName];
  90. else
  91. $this->populateMainCategories();
  92. if ($this->getConnection())
  93. $rtnValue = true;
  94. $this->closeConnection();
  95. // in PHP 5 use
  96. //$this->curRequest = http_build_query($_REQUEST);
  97. $this->curRequest = $this->http_query_builder($_REQUEST);
  98. $this->initialized = $rtnValue;
  99. }
  100. //------------------------------------------------------------------
  101. // This is for backward compatibility.
  102. function http_query_builder( $formdata, $numeric_prefix = null, $key = null ) {
  103. $res = array();
  104. foreach ((array)$formdata as $k=>$v) {
  105. $tmp_key = urlencode(is_int($k) ? $numeric_prefix.$k : $k);
  106. if ($key) $tmp_key = $key.'['.$tmp_key.']';
  107. $res[] = (is_array($v) || is_object($v))
  108. ? $this->http_query_builder($v, null, $tmp_key)
  109. : $tmp_key."=".urlencode($v);
  110. }
  111. $separator = ini_get('arg_separator.output');
  112. return implode($separator, $res);
  113. }
  114. //----------------------------------------------------------------
  115. // Function to populate the mainCatalog
  116. function populateMainCategories() {
  117. return;
  118. }
  119. //----------------------------------------------------------------
  120. // Function to Get Sub Categories
  121. function getSubCategories($mainGenre) {
  122. return array();
  123. }
  124. //----------------------------------------------------------------
  125. // Function to test Connection.
  126. function getConnection() {
  127. // Try to connect
  128. if (!$this->fp = @fsockopen ($this->mainURL, 80, $errno, $errstr, 30)) {
  129. // Error Connecting
  130. $this->msg = "Error connecting to ".$this->mainURL."!";
  131. return false;
  132. }
  133. return true;
  134. }
  135. //----------------------------------------------------------------
  136. // Function to Close Connection.
  137. function closeConnection() {
  138. if($this->fp)
  139. fclose($this->fp);
  140. }
  141. //----------------------------------------------------------------
  142. // Function to return the URL needed by tf
  143. function searchURL() {
  144. return "index.php?iid=torrentSearch&searchEngine=".$this->engineName;
  145. }
  146. //----------------------------------------------------------------
  147. // Function to Make the GetRequest
  148. function makeRequest($request, $useAlt = false) {
  149. $refererURI = (isset($_SESSION['lastOutBoundURI']))
  150. ? $_SESSION['lastOutBoundURI']
  151. : "http://".$this->mainURL;
  152. $request = ($useAlt)
  153. ? "http://".$this->altURL. $request
  154. : "http://".$this->mainURL. $request;
  155. // require SimpleHTTP
  156. require_once("inc/classes/SimpleHTTP.php");
  157. // get data
  158. $this->htmlPage = SimpleHTTP::getData($request, $refererURI);
  159. // return
  160. return (SimpleHTTP::getState() == SIMPLEHTTP_STATE_OK);
  161. }
  162. //----------------------------------------------------------------
  163. // Function to Get Main Categories
  164. function getMainCategories($filtered = true) {
  165. $output = array();
  166. foreach ($this->mainCatalog as $mainId => $mainName) {
  167. if ($filtered) {
  168. // see if this is filtered out.
  169. if (!(@in_array($mainId, $this->catFilter)))
  170. $output[$mainId] = $mainName;
  171. } else {
  172. $output[$mainId] = $mainName;
  173. }
  174. }
  175. return $output;
  176. }
  177. //----------------------------------------------------------------
  178. // Function to Get Main Category Name
  179. function GetMainCatName($mainGenre) {
  180. $mainGenreName = '';
  181. foreach ($this->getMainCategories() as $mainId => $mainName) {
  182. if ($mainId == $mainGenre)
  183. $mainGenreName = $mainName;
  184. }
  185. return $mainGenreName;
  186. }
  187. //----------------------------------------------------------------
  188. // Function to setup the table header
  189. function tableHeader() {
  190. $output = "<table width=\"100%\" cellpadding=3 cellspacing=0 border=0>";
  191. $output .= "<br>\n";
  192. $output .= "<tr bgcolor=\"".$this->cfg["table_header_bg"]."\">";
  193. $output .= " <td>&nbsp;</td>";
  194. $output .= " <td><strong>Torrent Name</strong> &nbsp;(";
  195. $tmpURI = str_replace(array("?hideSeedless=yes","&hideSeedless=yes","?hideSeedless=no","&hideSeedless=no"),"",$_SERVER["REQUEST_URI"]);
  196. // Check to see if Question mark is there.
  197. $tmpURI .= (strpos($tmpURI,'?'))
  198. ? "&"
  199. : "?";
  200. $output .= ($this->hideSeedless == "yes")
  201. ? "<a href=\"". $tmpURI . "hideSeedless=no\">Show Seedless</a>"
  202. : "<a href=\"". $tmpURI . "hideSeedless=yes\">Hide Seedless</a>";
  203. $output .= ")</td>";
  204. $output .= " <td><strong>Category</strong></td>";
  205. $output .= " <td align=center><strong>&nbsp;&nbsp;Size</strong></td>";
  206. $output .= " <td><strong>Seeds</strong></td>";
  207. $output .= " <td><strong>Peers</strong></td>";
  208. $output .= "</tr>\n";
  209. return $output;
  210. }
  211. }
  212. ?>