PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://bitbucket.org/saibotd/hackerhub/
PHP | 252 lines | 228 code | 24 blank | 0 comment | 43 complexity | fb85f52437945cf388354907b47969eb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. $some_name = session_name("some_name");
  3. session_set_cookie_params(0, '/', '.hackerhub.org');
  4. session_start();
  5. spl_autoload_register(function($class) {
  6. $file = "lib/" . strtr($class, '\\', '/') . '.php';
  7. if (file_exists($file)) {
  8. require $file;
  9. return true;
  10. }
  11. });
  12. $redis = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379, 'database' => 1));
  13. if(!$_SESSION["theme"]) $_SESSION["theme"] = "dark";
  14. if(is_file($_GET['q'])){
  15. switch(substr($_GET['q'], strrpos($_GET['q'], '.') + 1)){
  16. case "js":
  17. header('Content-type: text/javascript');
  18. break;
  19. case "css":
  20. header('Content-type: text/css');
  21. break;
  22. case "png":
  23. header('Content-type: image/png');
  24. break;
  25. case "gif":
  26. header('Content-type: image/gif');
  27. break;
  28. case "jepg":
  29. case "jpg":
  30. header('Content-type: image/jpeg');
  31. break;
  32. case "ico":
  33. header('Content-type: image/x-icon');
  34. break;
  35. case 'json':
  36. header('Content-type: application/json');
  37. break;
  38. case 'php':
  39. include($_GET['q']);
  40. exit;
  41. }
  42. echo file_get_contents($_GET['q']);
  43. exit;
  44. } elseif($_GET['q'] == "sitemap.xml") {
  45. echo '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://hackerhub.org/buzz</loc></url>';
  46. foreach($redis->keys("*:profile") as $key){
  47. $user = explode(":", $key);
  48. echo "<url><loc>http://$user[0].hackerhub.org</loc></url>";
  49. }
  50. echo '</urlset>';
  51. exit;
  52. }
  53. $url_data = explode(".", $_SERVER['HTTP_HOST']);
  54. $get_data = strlen($_GET['q']) ? explode("/", $_GET['q']) : NULL;
  55. if(count($url_data) == 2 || $url_data[0] == "www"){
  56. if($_POST){
  57. if($_POST["jsonurl"]){
  58. switch(addProfile($_POST["jsonurl"])){
  59. case "200":
  60. header("location: http://$profile_name.hackerhub.org");
  61. break;
  62. case "404":
  63. $error_message = "The profile could not be fetched.";
  64. break;
  65. case "406":
  66. $error_message = "The profile is not valid JSON.";
  67. break;
  68. case "409":
  69. $error_message = "The id is already taken.";
  70. break;
  71. default:
  72. $error_message = "Unknown Error!";
  73. }
  74. }
  75. if($_POST["s"]){
  76. header("location: /q/" . urlencode($_POST["s"]));
  77. exit;
  78. }
  79. }
  80. if($get_data[0] == "q"){
  81. $search_results = search($get_data[1]);
  82. include("search.php");
  83. exit;
  84. }
  85. if($get_data[0] == "r"){
  86. refresh($get_data[1]);
  87. header("location: http://$get_data[1].hackerhub.org");
  88. }
  89. if($get_data[0] == "t"){
  90. $_SESSION["theme"] = $get_data[1];
  91. if($_SESSION["referrer"]) header('location: ' . $_SESSION["referrer"]);
  92. else header('location: /');
  93. }
  94. if($get_data[0] == "buzz"){
  95. $terms = buzz();
  96. echo "<h1>". count($terms) ." searchterms</h1>";
  97. foreach($terms as $term => $matches){
  98. echo "<h4><a href=\"http://hackerhub.org/q/$term\">$term</a></h4>";
  99. foreach($matches as $match){
  100. $match = explode(":", $match);
  101. echo "<a href='http://$match[0].hackerhub.org/$match[1]'>$match[0].hackerhub.org/$match[1]</a><br>";
  102. }
  103. }
  104. exit;
  105. }
  106. if($get_data[0] == "xi"){
  107. $index = index();
  108. echo "<h1>". count($index) ." users</h1>";
  109. foreach($index as $name) echo "<p><a href='http://$name.hackerhub.org'>$name</a></p>";
  110. exit;
  111. }
  112. include("homepage.php");
  113. exit;
  114. }
  115. $_SESSION["referrer"] = 'http://' . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  116. include("lib/phpmarkdownextra/markdown.php");
  117. $profile_name = $url_data[0];
  118. if(!$redis->exists("$profile_name:profile")){
  119. header("Status: 404 Not Found");
  120. exit;
  121. }
  122. $profile_data = json_decode($redis->get("$profile_name:profile"));
  123. function index(){
  124. global $redis;
  125. $users = array();
  126. foreach($redis->keys("*:profile") as $key){
  127. $user = explode(":", $key);
  128. $users[] = $user[0];
  129. }
  130. return $users;
  131. }
  132. function buzz(){
  133. global $redis;
  134. foreach($redis->keys("search:*") as $key){
  135. $term = explode(":", $key);
  136. $terms[$term[1]] = $redis->smembers($key);
  137. }
  138. return $terms;
  139. }
  140. function search($q){
  141. global $redis;
  142. $words = explode(" ", strtolower($q));
  143. $match_count = array();
  144. foreach($words as $word){
  145. $matches = $redis->smembers("search:$word");
  146. if($matches){
  147. foreach($matches as $match){
  148. if(!$match_count[$match]) $match_count[$match] = 0;
  149. $match_count[$match] += 1;
  150. }
  151. }
  152. }
  153. arsort($match_count);
  154. foreach($match_count as $k => &$v){
  155. $_v = null;
  156. $temp = explode(":", $k);
  157. $_v->user_id = $temp[0];
  158. $_v->path = $temp[1];
  159. $_v->hits = $v;
  160. $key = $_v->user_id.':cache:'.$_v->path;
  161. $_v->content = substr(trim(strip_tags($redis->get($key))),0,200);
  162. if (!$_v->content){
  163. $redis->srem("search:$word", "$_v->user_id:$_v->path");
  164. unset($match_count[$k]);
  165. } else $v = $_v;
  166. }
  167. return $match_count;
  168. }
  169. function getDataFromUrl($url, $is_markdown=true, $add_to_search=true, $path_suffix=""){
  170. global $redis, $profile_name, $profile_data, $get_data;
  171. $key = "$profile_name:cache:" . implode("/", $get_data).$path_suffix;
  172. if($redis->exists($key)) return $redis->get($key);
  173. $data = @file_get_contents($url);
  174. $data = trim($data);
  175. if(!data) return false;
  176. if($is_markdown) {
  177. $data = strip_tags($data, '<p><a><br><hr><h1><h2><h3><h4><h5><h6><abbr><cite><code><ul><ol><li><table><thead><tbody><tr><th><td>');
  178. $data = Markdown($data);
  179. }
  180. if($profile_data->settings->enable_search && $add_to_search) fillSearchIndex($data, $path_suffix);
  181. $redis->set($key, $data);
  182. $redis->expire($key, 86400);
  183. return $data;
  184. }
  185. function refresh($userid){
  186. global $redis;
  187. foreach($redis->keys("$userid:*") as $key) $redis->del($key);
  188. addProfile($redis->get("profile_url:$userid"));
  189. }
  190. function fillSearchIndex($full_text, $path_suffix=""){
  191. global $redis, $profile_name, $get_data;
  192. $words = explode('#', strtolower(preg_replace('/[^a-z0-9]/i', '#', str_replace("\n", ' ', $full_text))));
  193. foreach($words as $word){
  194. if($word && $word != '' && strlen($word) >= 3){
  195. $key = "search:$word";
  196. $redis->sadd($key, "$profile_name:" . implode("/", $get_data) . $path_suffix);
  197. }
  198. }
  199. }
  200. function addProfile($jsonurl){
  201. global $redis, $profile_name;
  202. if(!@get_headers($jsonurl)) return 404;
  203. if(!@json_decode(file_get_contents($jsonurl))) return 406;
  204. $profile = json_decode(file_get_contents($jsonurl));
  205. $profile_name = strtolower($profile->settings->id);
  206. if(!preg_match('~^[a-z0-9]*\z~i', $profile_name)) return 403;
  207. if($redis->exists("$profile_name:profile")) return 409;
  208. $redis->set("$profile_name:profile", json_encode($profile));
  209. $redis->set("profile_url:$profile_name", $jsonurl);
  210. return 200;
  211. }
  212. if(count($get_data) != 0){
  213. $content_data = $profile_data->content->{$get_data[0]};
  214. if(!$content_data){
  215. header("Status: 404 Not Found");
  216. exit;
  217. }
  218. } else {
  219. if($profile_data->content && key($profile_data->content)) header('location: /' . key($profile_data->content));
  220. else{
  221. header("Status: 404 Not Found");
  222. exit;
  223. }
  224. }
  225. include("profile.php");
  226. ?>