getMessage(); trigger_error($msg,E_USER_ERROR); } set_exception_handler('exception_handler'); function getrand() { $fd = fopen("/dev/urandom","r"); $data = fread($fd,16); fclose($fd); return md5($data); } function isLoggedIn() { global $User, $_COOKIE; if (isset($User)) return true; if (isset($_COOKIE['auth'])) { $r = redisLink(); $authcookie = $_COOKIE['auth']; if ($userid = $r->hget("auths",$authcookie)) { if ($r->hget("user:$userid","auth") != $authcookie) return false; loadUserInfo($userid); return true; } } return false; } function loadUserInfo($userid) { global $User; $r = redisLink(); $User['id'] = $userid; $User['username'] = $r->hget("user:$userid","username"); return true; } function redisLink() { static $r = false; global $redis_options; if ($r) return $r; $r = new Predis\Client($redis_options); return $r; } # Access to GET/POST/COOKIE parameters the easy way function g($param) { global $_GET, $_POST, $_COOKIE; if (isset($_COOKIE[$param])) return $_COOKIE[$param]; if (isset($_POST[$param])) return $_POST[$param]; if (isset($_GET[$param])) return $_GET[$param]; return false; } function gt($param) { $val = g($param); if ($val === false) return false; return trim($val); } function utf8entities($s) { return htmlentities($s,ENT_COMPAT,'UTF-8'); } function goback($msg) { include("header.php"); echo('
'.utf8entities($msg).'
'); echo('Please return back and try again
'); include("footer.php"); exit; } function strElapsed($t) { $d = time()-$t; if ($d < 60) return "$d seconds"; if ($d < 3600) { $m = (int)($d/60); return "$m minute".($m > 1 ? "s" : ""); } if ($d < 3600*24) { $h = (int)($d/3600); return "$h hour".($h > 1 ? "s" : ""); } $d = (int)($d/(3600*24)); return "$d day".($d > 1 ? "s" : ""); } function showPost($id) { $r = redisLink(); $post = $r->hgetall("post:$id"); if (empty($post)) return false; $userid = $post['user_id']; $username = $r->hget("user:$userid","username"); $elapsed = strElapsed($post['time']); $userlink = "".utf8entities($username).""; echo('
'.$userlink.' '.utf8entities($post['body'])."
"); echo('posted '.$elapsed.' ago via web
'); return true; } function showUserPosts($userid,$start,$count) { $r = redisLink(); $key = ($userid == -1) ? "timeline" : "posts:$userid"; $posts = $r->lrange($key,$start,$start+$count); $c = 0; foreach($posts as $p) { if (showPost($p)) $c++; if ($c == $count) break; } return count($posts) == $count+1; } function showUserPostsWithPagination($username,$userid,$start,$count) { global $_SERVER; $thispage = $_SERVER['PHP_SELF']; $navlink = ""; $next = $start+10; $prev = $start-10; $nextlink = $prevlink = false; if ($prev < 0) $prev = 0; $u = $username ? "&u=".urlencode($username) : ""; if (showUserPosts($userid,$start,$count)) $nextlink = "Older posts »"; if ($start > 0) { $prevlink = "« Newer posts".($nextlink ? " | " : ""); } if ($nextlink || $prevlink) echo("
$prevlink $nextlink
"); } function showLastUsers() { $r = redisLink(); $users = $r->zrevrange("users_by_time",0,9); echo("
"); foreach($users as $u) { echo("".utf8entities($u)." "); } echo("

"); } ?>