/index.php
PHP | 252 lines | 228 code | 24 blank | 0 comment | 43 complexity | fb85f52437945cf388354907b47969eb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
- <?php
- $some_name = session_name("some_name");
- session_set_cookie_params(0, '/', '.hackerhub.org');
- session_start();
- spl_autoload_register(function($class) {
- $file = "lib/" . strtr($class, '\\', '/') . '.php';
- if (file_exists($file)) {
- require $file;
- return true;
- }
- });
- $redis = new Predis\Client(array('host' => '127.0.0.1', 'port' => 6379, 'database' => 1));
- if(!$_SESSION["theme"]) $_SESSION["theme"] = "dark";
- if(is_file($_GET['q'])){
- switch(substr($_GET['q'], strrpos($_GET['q'], '.') + 1)){
- case "js":
- header('Content-type: text/javascript');
- break;
- case "css":
- header('Content-type: text/css');
- break;
- case "png":
- header('Content-type: image/png');
- break;
- case "gif":
- header('Content-type: image/gif');
- break;
- case "jepg":
- case "jpg":
- header('Content-type: image/jpeg');
- break;
- case "ico":
- header('Content-type: image/x-icon');
- break;
- case 'json':
- header('Content-type: application/json');
- break;
- case 'php':
- include($_GET['q']);
- exit;
- }
- echo file_get_contents($_GET['q']);
- exit;
- } elseif($_GET['q'] == "sitemap.xml") {
- 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>';
- foreach($redis->keys("*:profile") as $key){
- $user = explode(":", $key);
- echo "<url><loc>http://$user[0].hackerhub.org</loc></url>";
- }
- echo '</urlset>';
- exit;
- }
- $url_data = explode(".", $_SERVER['HTTP_HOST']);
- $get_data = strlen($_GET['q']) ? explode("/", $_GET['q']) : NULL;
- if(count($url_data) == 2 || $url_data[0] == "www"){
- if($_POST){
- if($_POST["jsonurl"]){
- switch(addProfile($_POST["jsonurl"])){
- case "200":
- header("location: http://$profile_name.hackerhub.org");
- break;
- case "404":
- $error_message = "The profile could not be fetched.";
- break;
- case "406":
- $error_message = "The profile is not valid JSON.";
- break;
- case "409":
- $error_message = "The id is already taken.";
- break;
- default:
- $error_message = "Unknown Error!";
- }
- }
- if($_POST["s"]){
- header("location: /q/" . urlencode($_POST["s"]));
- exit;
- }
- }
- if($get_data[0] == "q"){
- $search_results = search($get_data[1]);
- include("search.php");
- exit;
- }
- if($get_data[0] == "r"){
- refresh($get_data[1]);
- header("location: http://$get_data[1].hackerhub.org");
- }
- if($get_data[0] == "t"){
- $_SESSION["theme"] = $get_data[1];
- if($_SESSION["referrer"]) header('location: ' . $_SESSION["referrer"]);
- else header('location: /');
- }
-
- if($get_data[0] == "buzz"){
- $terms = buzz();
- echo "<h1>". count($terms) ." searchterms</h1>";
- foreach($terms as $term => $matches){
- echo "<h4><a href=\"http://hackerhub.org/q/$term\">$term</a></h4>";
- foreach($matches as $match){
- $match = explode(":", $match);
- echo "<a href='http://$match[0].hackerhub.org/$match[1]'>$match[0].hackerhub.org/$match[1]</a><br>";
- }
- }
- exit;
- }
-
- if($get_data[0] == "xi"){
- $index = index();
- echo "<h1>". count($index) ." users</h1>";
- foreach($index as $name) echo "<p><a href='http://$name.hackerhub.org'>$name</a></p>";
- exit;
- }
-
- include("homepage.php");
- exit;
- }
- $_SESSION["referrer"] = 'http://' . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- include("lib/phpmarkdownextra/markdown.php");
- $profile_name = $url_data[0];
- if(!$redis->exists("$profile_name:profile")){
- header("Status: 404 Not Found");
- exit;
- }
- $profile_data = json_decode($redis->get("$profile_name:profile"));
- function index(){
- global $redis;
- $users = array();
- foreach($redis->keys("*:profile") as $key){
- $user = explode(":", $key);
- $users[] = $user[0];
- }
- return $users;
- }
- function buzz(){
- global $redis;
- foreach($redis->keys("search:*") as $key){
- $term = explode(":", $key);
- $terms[$term[1]] = $redis->smembers($key);
- }
- return $terms;
- }
- function search($q){
- global $redis;
- $words = explode(" ", strtolower($q));
- $match_count = array();
- foreach($words as $word){
- $matches = $redis->smembers("search:$word");
- if($matches){
- foreach($matches as $match){
- if(!$match_count[$match]) $match_count[$match] = 0;
- $match_count[$match] += 1;
- }
- }
- }
- arsort($match_count);
- foreach($match_count as $k => &$v){
- $_v = null;
- $temp = explode(":", $k);
- $_v->user_id = $temp[0];
- $_v->path = $temp[1];
- $_v->hits = $v;
- $key = $_v->user_id.':cache:'.$_v->path;
- $_v->content = substr(trim(strip_tags($redis->get($key))),0,200);
- if (!$_v->content){
- $redis->srem("search:$word", "$_v->user_id:$_v->path");
- unset($match_count[$k]);
- } else $v = $_v;
- }
- return $match_count;
- }
- function getDataFromUrl($url, $is_markdown=true, $add_to_search=true, $path_suffix=""){
- global $redis, $profile_name, $profile_data, $get_data;
- $key = "$profile_name:cache:" . implode("/", $get_data).$path_suffix;
- if($redis->exists($key)) return $redis->get($key);
- $data = @file_get_contents($url);
- $data = trim($data);
- if(!data) return false;
- if($is_markdown) {
- $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>');
- $data = Markdown($data);
- }
- if($profile_data->settings->enable_search && $add_to_search) fillSearchIndex($data, $path_suffix);
- $redis->set($key, $data);
- $redis->expire($key, 86400);
- return $data;
- }
- function refresh($userid){
- global $redis;
- foreach($redis->keys("$userid:*") as $key) $redis->del($key);
- addProfile($redis->get("profile_url:$userid"));
- }
- function fillSearchIndex($full_text, $path_suffix=""){
- global $redis, $profile_name, $get_data;
- $words = explode('#', strtolower(preg_replace('/[^a-z0-9]/i', '#', str_replace("\n", ' ', $full_text))));
- foreach($words as $word){
- if($word && $word != '' && strlen($word) >= 3){
- $key = "search:$word";
- $redis->sadd($key, "$profile_name:" . implode("/", $get_data) . $path_suffix);
- }
- }
- }
- function addProfile($jsonurl){
- global $redis, $profile_name;
- if(!@get_headers($jsonurl)) return 404;
- if(!@json_decode(file_get_contents($jsonurl))) return 406;
- $profile = json_decode(file_get_contents($jsonurl));
- $profile_name = strtolower($profile->settings->id);
- if(!preg_match('~^[a-z0-9]*\z~i', $profile_name)) return 403;
- if($redis->exists("$profile_name:profile")) return 409;
- $redis->set("$profile_name:profile", json_encode($profile));
- $redis->set("profile_url:$profile_name", $jsonurl);
- return 200;
- }
- if(count($get_data) != 0){
- $content_data = $profile_data->content->{$get_data[0]};
- if(!$content_data){
- header("Status: 404 Not Found");
- exit;
- }
- } else {
- if($profile_data->content && key($profile_data->content)) header('location: /' . key($profile_data->content));
- else{
- header("Status: 404 Not Found");
- exit;
- }
- }
- include("profile.php");
- ?>