/
redis.php
executable file
·195 lines (164 loc) · 3.85 KB
/
redis.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
define('SITEURL', 'http://localhost:8000/');
require('Predis.php');
$redis = new Predis\Client();
/**
* isLoggedIn
* checks to see if user is looged in
**/
function isLoggedIn() {
global $User, $_COOKIE;
$redis = new Predis\Client();
if (isset($User)) return true;
if (isset($_COOKIE['auth'])) {
$authcookie = $_COOKIE['auth'];
if ($userid = $redis->get("auth:$authcookie")) {
if ($redis->get("uid:$userid:auth") != $authcookie) return false;
loadUserInfo($userid);
return true;
}
}
return false;
}
/**
* g
* gets a $REQUEST value for the named $param
* returns false if not found
**/
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;
}
/**
* gt
* trims the param
* returns false if false
**/
function gt($param) {
$val = g($param);
if ($val === false) return false;
return trim($val);
}
/**
* goback
* outputs javascritp back link
* used for errors
**/
function goback($msg) {
$content = '<div id ="error">'.utf8entities($msg).'<br>';
$content .= '<a href="javascript:history.back()">Please return back and try again</a></div>';
include('static.php');
exit;
}
/**
* utf8entities
* encodes and html chars in utf-8 format
**/
function utf8entities($s) {
return htmlentities($s,ENT_COMPAT,'UTF-8');
}
/**
* getrand
* gets random string value
* used for auth secrets and gameids
**/
function getrand() {
$fd = fopen("/dev/urandom","r");
$data = fread($fd,16);
fclose($fd);
return md5($data);
}
/**
* loadUserinfo
* loads user info if they are logged in
**/
function loadUserInfo($userid) {
global $User;
$redis = new Predis\Client();
$User['id'] = $userid;
$User['username'] = $redis->get("uid:$userid:username");
return true;
}
/**
* setGameId
* sets a new unique gameid
* @param userid
* @return gameid
**/
function setGameId($uid){
$redis = new Predis\Client();
$game_id = getrand();
$redis->set("uid:$uid:gameid", $game_id);
//set this game as the latest
$redis->set("uid:$uid:latest", $game_id);
return $game_id;
}
/**
* isValidGameId
* checks the database if this game is
* associated with the user and if it is
* the current game
* @param userid
* @param gameid
* @return bool
**/
function isValidGameId($uid, $gameid){
$redis = new Predis\Client();
//is a valid game
$redis_id = $redis->get("uid:$uid:gameid");
//exists as a game
if($gameid == $redis_id){
if($redis->get("uid:$uid:latest") == $redis_id){
//is the latest
return 1;
}else{
//isn't the latest game
return 0;
}
}
//was never a game
return 0;
}
/**
* getCurrentValue
* gets the current overall total credits for the user
* @param userid
* @return total value
**/
function getCurrentValue($uid){
$redis = new Predis\Client();
return $redis->get("uid:$uid:value");
}
/**
* addCurrentValue
* adds a given value to the users
* overall total credits
* @param userid
* @param the value to be added
* @return the new total value
**/
function addCurrentValue($id, $value){
$redis = new Predis\Client();
$current = getCurrentValue($id);
$temp = $current + $value;
$redis->set("uid:$id:value", $temp);
return $temp;
}
/**
* subCurrentValue
* subtracts a given value from the users
* overall total credits
* @param userid
* @param the value to be subtracted
* @return the new total value
**/
function subCurrentValue($id, $value){
$redis = new Predis\Client();
$current = getCurrentValue($id);
$current -= $value;
$redis->set("uid:$id:value", $current);
return $current;
}