-
Notifications
You must be signed in to change notification settings - Fork 2
/
user-tokens.php
221 lines (168 loc) · 7.04 KB
/
user-tokens.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
global $wpdb;
define("USER_TOKENS_TABLE_NAME", $wpdb->prefix . IFLPM_TABLE_PREFIX . "user_tokens");
define("USER_TOKENS_DB_VERSION", "1.0");
Class UserTokens {
// Token Color Hex Values
public static $token_colors = array(
0x00FF00,
0xFFFF00,
0xFF0000,
0x0000FF,
0xFF00FF,
0xFFFFFF
);
// Token Color Classes
// Should be G Y R B (& secret purple)
public static $token_color_classes = array(
'greentoken',
'yellowtoken',
'redtoken',
'bluetoken',
'purpletoken',
'whitetoken'
);
public static function get_token_color_class($token_id) {
// we think idcode is always even... this is mostly because we read the little-endian id as if it were big-endian and are getting kinda lucky. But this /2 mod5 thing works so ok for now. dvb 2019.
$token_id /= 2;
return self::$token_color_classes[$token_id % 5];
}
// if the token ID is in the tokens table, returns associated user ID as string, otherwise returns false.
public static function token_id_exists_in_table($token_id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME)) return false;
$result = $wpdb->get_results("SELECT user_id FROM " . USER_TOKENS_TABLE_NAME . " WHERE token_id = '" . $token_id . "'");
if ($wpdb->num_rows == 0) return false;
return $result[0];
}
// if the token ID is in the tokens table, returns associated user ID as string, otherwise returns an error message.
public static function get_user_id_from_token_id($token_id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME))
return "Tokens table does not exist in database";
$result = $wpdb->get_results("SELECT user_id FROM " . USER_TOKENS_TABLE_NAME . " WHERE token_id = '" . $token_id . "'");
if ($wpdb->num_rows == 0) return "Token id " . $token_id . " not found in database, you need to register it with a user ID";
return $result[0]->user_id;
}
// if the user ID is in the tokens table, returns associated token ID(s) as array, otherwise returns false
public static function get_token_ids_by_user_id($user_id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME)) return false;
$result = $wpdb->get_results("SELECT token_id FROM " . USER_TOKENS_TABLE_NAME . " WHERE user_id = " . $user_id);
if ($wpdb->num_rows == 0) return false;
return array_map(function ($token) {return $token->token_id;}, $result);
}
// Get all user tokens and their users_ids
public static function get_all_user_tokens() {
global $wpdb;
$result = $wpdb->get_results("SELECT user_id, token_id FROM " . USER_TOKENS_TABLE_NAME );
if ($wpdb->num_rows == 0) return false;
return json_encode($result, JSON_UNESCAPED_SLASHES);
return array_map(function ($token) {return $token_id->user_id;}, $result);
}
public static function add_token_id_and_user_id_to_tokens_table($token_id, $user_id) {
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME)) {
return "Error - tokens table does not exist in database";
}
if ($token_id == "") {
return "Error - empty token ID";
}
if ($user_id == "") {
return "Error - empty user ID";
}
$result = $wpdb->get_results("SELECT * FROM " . USER_TOKENS_TABLE_NAME . " WHERE token_id = '" . $token_id . "'");
if ($wpdb->num_rows != 0) {
$user_id_already_registered = $result[0]->user_id;
if ($user_id_already_registered != $user_id) {
return "Error - token ID " . $token_id . " is already registered to a different userID (" . $user_id_already_registered . ")";
}
return "Token ID " . $token_id . " is already registered to that user ID (" . $user_id . ")";
}
$wpdb->insert(
USER_TOKENS_TABLE_NAME,
array(
'token_id' => $token_id,
'user_id' => $user_id,
)
);
$result = $wpdb->get_results("SELECT * FROM " . USER_TOKENS_TABLE_NAME . " WHERE token_id = '" . $token_id . "'");
if ($wpdb->num_rows != 0) {
return "Token ID " . $token_id . " and user ID " . $user_id . " pairing added to the tokens table";
} else {
return "Error adding token ID and user ID to the tokens table";
}
}
public static function delete_user_token($token_id, $user_id) {
// This function removes a token from the zone tokens table, so that that token is no longer registered to
// any user, but it does not affect any records in the plus one zones table, because those are tied to users
// rather than individual tokens.
global $wpdb;
if (!IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME)) {
return new WP_Error('no-token-table', "User tokens table does not exist in database");
}
$token_id = trim($token_id);
if ($token_id == "") {
return new WP_Error('no-token-id', "Empty User token ID");
} else if (!preg_match("/^\d+$/", $token_id)) {
return new WP_Error('non-numeric-token-id', "Non-numeric characters in user token ID");
}
$user_id = trim($user_id);
if ($user_id == "") {
return new WP_Error('no-user-id', "Empty User ID");
}
// if (!self::is_user_id_in_database($user_id)) {
// return new WP_Error('user-missing', "User ID " . $user_id . " is not a registered user");
// }
$response = self::get_user_id_from_token_id($token_id);
if (is_wp_error($response)) {
return $response;
} else {
$user_id_registered_to_that_token = $response;
}
if ($user_id_registered_to_that_token != $user_id) {
return new WP_Error('user-mismatch', "Token ID " . $token_id . " is registered to a different userID.");
}
$wpdb->delete(
USER_TOKENS_TABLE_NAME,
array(
'token_id' => $token_id,
)
);
$result = $wpdb->get_results("SELECT * FROM " . USER_TOKENS_TABLE_NAME . " WHERE token_id = '" . $token_id . "'");
if ($wpdb->num_rows == 0) {
return "Zone token ID " . $token_id . " successfully deleted from tokens table";
} else {
return new WP_Error('unknown-error', "Error deleting zone token ID " . $token_id . " from the tokens table");
}
}
// === DB Methods ===
public static function create_tokens_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
/// I had wanted to use token_id as the primary key but the table was not getting created,
/// even when I switched from tinytext to varchar(10)
$sql = "CREATE TABLE " . USER_TOKENS_TABLE_NAME . "(
id mediumint(9) NOT NULL AUTO_INCREMENT,
token_id tinytext NOT NULL,
user_id tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option('tokens_db_version', TOKENS_DB_VERSION);
}
public static function does_user_tokens_table_exist_in_database() {
return IFLPMDBManager::does_table_exist_in_database(USER_TOKENS_TABLE_NAME);
}
public static function is_tokens_table_empty() {
return IFLPMDBManager::is_table_empty(USER_TOKENS_TABLE_NAME);
}
public static function delete_all_tokens_from_tokens_table() {
IFLPMDBManager::delete_all_rows_from_table(USER_TOKENS_TABLE_NAME);
}
public static function drop_tokens_table() {
IFLPMDBManager::drop_table(USER_TOKENS_TABLE_NAME);
}
}
?>