/include/search.php

https://github.com/mlangill/biotorrents · PHP · 196 lines · 153 code · 31 blank · 12 comment · 38 complexity · 7e4518c68d9fac0f687191b348b36301 MD5 · raw file

  1. <?php
  2. require_once "bittorrent.php";
  3. require_once "user_functions.php";
  4. require_once "torrenttable_functions.php";
  5. require_once "pager_functions.php";
  6. function search($_GET,$CURUSER){
  7. $cats = genrelist();
  8. if(isset($_GET["search"])) {
  9. $searchstr = unesc($_GET["search"]);
  10. $cleansearchstr = searchfield($searchstr);
  11. if (empty($cleansearchstr))
  12. unset($cleansearchstr);
  13. }
  14. $orderby = "ORDER BY torrents.id DESC";
  15. $addparam = "";
  16. $wherea = array();
  17. $wherecatina = array();
  18. if (isset($_GET["incldead"]) && $_GET["incldead"] == 1)
  19. {
  20. $addparam .= "incldead=1&amp;";
  21. if (!isset($CURUSER) || get_user_class() < UC_ADMINISTRATOR)
  22. $wherea[] = "banned != 'yes'";
  23. }
  24. else
  25. if (isset($_GET["incldead"]) && $_GET["incldead"] == 2)
  26. {
  27. $addparam .= "incldead=2&amp;";
  28. $wherea[] = "visible = 'no'";
  29. }
  30. else
  31. $wherea[] = "visible = 'yes'";
  32. $category = (isset($_GET["cat"])) ? (int)$_GET["cat"] : false;
  33. $license = (isset($_GET["lic"])) ? (int)$_GET["lic"] : false;
  34. $version = (isset($_GET["ver"])) ? (int)$_GET["ver"] : false;
  35. $user = (isset($_GET["user"])) ? (int)$_GET["user"] : false;
  36. $all = isset($_GET["all"]) ? $_GET["all"] : false;
  37. $page_limit= isset($_GET["page_limit"]) ? $_GET["page_limit"] : false;
  38. if (!$all)
  39. if (!$_GET && $CURUSER["notifs"])
  40. {
  41. $all = True;
  42. foreach ($cats as $cat)
  43. {
  44. $all &= $cat['id'];
  45. if (strpos($CURUSER["notifs"], "[cat" . $cat['id'] . "]") !== False)
  46. {
  47. $wherecatina[] = $cat['id'];
  48. $addparam .= "c{$cat['id']}=1&amp;";
  49. }
  50. }
  51. }
  52. elseif ($category)
  53. {
  54. if (!is_valid_id($category))
  55. stderr("Error", "Invalid category ID.");
  56. $wherecatina[] = $category;
  57. $addparam .= "cat=$category&amp;";
  58. }
  59. else
  60. {
  61. $all = True;
  62. foreach ($cats as $cat)
  63. {
  64. $all &= isset($_GET["c{$cat['id']}"]);
  65. if (isset($_GET["c{$cat['id']}"]))
  66. {
  67. $wherecatina[] = $cat['id'];
  68. $addparam .= "c{$cat['id']}=1&amp;";
  69. }
  70. }
  71. }
  72. if ($all)
  73. {
  74. $wherecatina = array();
  75. $addparam = "";
  76. }
  77. if (count($wherecatina) > 1)
  78. $wherecatin = implode(",",$wherecatina);
  79. elseif (count($wherecatina) == 1)
  80. $wherea[] = "category = $wherecatina[0]";
  81. if($license >0){
  82. $wherea[] = "license = $license";
  83. }
  84. if($user > 0){
  85. $wherea[] = "owner = $user";
  86. }
  87. if($version > 0){
  88. $wherea[] = "version = $version";
  89. }
  90. $wherebase = $wherea;
  91. if (isset($cleansearchstr))
  92. {
  93. $wherea[] = "MATCH (search_text, ori_descr) AGAINST (" . sqlesc($searchstr) . ")";
  94. //$wherea[] = "0";
  95. $addparam .= "search=" . urlencode($searchstr) . "&amp;";
  96. $orderby = "";
  97. /////////////// SEARCH CLOUD MALARKY //////////////////////
  98. $searchcloud = sqlesc($cleansearchstr);
  99. // $r = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM searchcloud WHERE searchedfor = $searchcloud"), MYSQL_NUM);
  100. //$a = $r[0];
  101. //if ($a)
  102. // mysql_query("UPDATE searchcloud SET howmuch = howmuch + 1 WHERE searchedfor = $searchcloud");
  103. //else
  104. // mysql_query("INSERT INTO searchcloud (searchedfor, howmuch) VALUES ($searchcloud, 1)");
  105. mysql_query("INSERT INTO searchcloud (searchedfor, howmuch) VALUES ($searchcloud, 1)
  106. ON DUPLICATE KEY UPDATE howmuch=howmuch+1");
  107. /////////////// SEARCH CLOUD MALARKY END ///////////////////
  108. }
  109. $where = implode(" AND ", $wherea);
  110. if (isset($wherecatin))
  111. $where .= ($where ? " AND " : "") . "category IN(" . $wherecatin . ")";
  112. if ($where != "")
  113. $where = "WHERE $where";
  114. $res = mysql_query("SELECT COUNT(*) FROM torrents $where") or die(mysql_error());
  115. $row = mysql_fetch_array($res,MYSQL_NUM);
  116. $count = $row[0];
  117. if (!$count && isset($cleansearchstr)) {
  118. $wherea = $wherebase;
  119. $orderby = "ORDER BY id DESC";
  120. $searcha = explode(" ", $cleansearchstr);
  121. $sc = 0;
  122. foreach ($searcha as $searchss) {
  123. if (strlen($searchss) <= 1)
  124. continue;
  125. $sc++;
  126. if ($sc > 5)
  127. break;
  128. $ssa = array();
  129. foreach (array("search_text", "ori_descr") as $sss)
  130. $ssa[] = "$sss LIKE '%" . sqlwildcardesc($searchss) . "%'";
  131. $wherea[] = "(" . implode(" OR ", $ssa) . ")";
  132. }
  133. if ($sc) {
  134. $where = implode(" AND ", $wherea);
  135. if ($where != "")
  136. $where = "WHERE $where";
  137. $res = mysql_query("SELECT COUNT(*) FROM torrents $where");
  138. $row = mysql_fetch_array($res,MYSQL_NUM);
  139. $count = $row[0];
  140. }
  141. }
  142. $torrentsperpage = $CURUSER["torrentsperpage"];
  143. if($page_limit)
  144. $torrentsperpage = $page_limit;
  145. if (!$torrentsperpage)
  146. $torrentsperpage = 15;
  147. if ($count)
  148. //if(1)
  149. {
  150. //list($pagertop, $pagerbottom, $limit) = pager($torrentsperpage, $count, "browse.php?" . $addparam);
  151. $pager = pager($torrentsperpage, $count, "browse.php?" . $addparam);
  152. $query = "SELECT torrents.id, torrents.category, torrents.leechers, torrents.seeders, torrents.name, torrents.times_completed, torrents.size, torrents.added, torrents.type, torrents.comments,torrents.numfiles,torrents.filename,torrents.owner,IF(torrents.nfo <> '', 1, 0) as nfoav," .
  153. // "IF(torrents.numratings < $minvotes, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating, categories.name AS cat_name, categories.image AS cat_pic, users.username FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id $where $orderby $limit";
  154. "categories.name AS cat_name, categories.image AS cat_pic, users.username, torrents.version, torrents.descr,licenses.name AS lic_name,licenses.url AS lic_url,licenses.description AS lic_desc FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id LEFT JOIN licenses ON torrents.license = licenses.id $where $orderby {$pager['limit']}";
  155. $res = mysql_query($query) or die(mysql_error());
  156. }
  157. else
  158. unset($res);
  159. if($count){
  160. return array($res,$wherecatina, $pager);
  161. }else{
  162. return array("",$wherecatina,"");
  163. }
  164. }
  165. ?>