-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathretwis.php
146 lines (123 loc) · 3.75 KB
/
retwis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?
require 'Predis/Autoloader.php';
Predis\Autoloader::register();
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;
if ($r) return $r;
$r = new Predis\Client();
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('<div id ="error">'.utf8entities($msg).'<br>');
echo('<a href="javascript:history.back()">Please return back and try again</a></div>');
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 = "<a class=\"username\" href=\"profile.php?u=".urlencode($username)."\">".utf8entities($username)."</a>";
echo('<div class="post">'.$userlink.' '.utf8entities($post['body'])."<br>");
echo('<i>posted '.$elapsed.' ago via web</i></div>');
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 = "<a href=\"$thispage?start=$next".$u."\">Older posts »</a>";
if ($start > 0) {
$prevlink = "<a href=\"$thispage?start=$prev".$u."\">« Newer posts</a>".($nextlink ? " | " : "");
}
if ($nextlink || $prevlink)
echo("<div class=\"rightlink\">$prevlink $nextlink</div>");
}
function showLastUsers() {
$r = redisLink();
$users = $r->zrevrange("users_by_time",0,9);
echo("<div>");
foreach($users as $u) {
echo("<a class=\"username\" href=\"profile.php?u=".urlencode($u)."\">".utf8entities($u)."</a> ");
}
echo("</div><br>");
}
?>