-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUespPatreonUpdate.php
389 lines (282 loc) · 11.6 KB
/
UespPatreonUpdate.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
if (php_sapi_name() != "cli") die("Can only be run from command line!");
require_once("/home/uesp/secrets/patreon.secrets");
require_once("/home/uesp/secrets/wiki.secrets");
require_once("Patreon/API2.php");
require_once("Patreon/OAuth2.php");
require_once("UespPatreonCommon.php");
class UespPatreonUpdate {
public $patreonUsers = array();
public $existingUsers = array();
public $tierChanges = array();
public $db = null;
public $lastQuery = "";
public $accessToken = "";
public $refreshToken = "";
public $tokenExpires = "";
public function __construct() {
$this->initDatabase();
$this->loadInfo();
}
public function initDatabase() {
global $uespWikiDB;
global $uespWikiUser;
global $uespWikiPW;
global $UESP_SERVER_DB1;
$this->db = new mysqli($UESP_SERVER_DB1, $uespWikiUser, $uespWikiPW, $uespWikiDB);
if ($this->db->connect_error) exit("Could not connect to mysql database!");
}
public function loadInfo() {
$this->lastQuery = "SELECT * FROM patreon_info;";
$result = $this->db->query($this->lastQuery);
if ($result === false) {
print("\tError: Failed to load patreon info records! " . $this->db->error . "\n\t" . $this->lastQuery . "\n");
return false;
}
while ($row = $result->fetch_assoc()) {
$key = $row['k'];
$value = $row['v'];
if ($key == "access_token")
$this->accessToken = $value;
else if ($key == "refresh_token")
$this->refreshToken = $value;
else if ($key == "token_expires")
$this->tokenExpires = intval($value);
}
$timeDiff = $this->tokenExpires - time();
if ($timeDiff <= 86400) $this->refreshToken();
return true;
}
public function refreshToken() {
global $uespPatreonClientId2;
global $uespPatreonClientSecret2;
print("\tAttempting to refresh access/refresh tokens!\n");
if ($this->refreshToken == "") {
print("\tError: Can't renew the access token as the refresh token is empty!\n");
return false;
}
$oauth = new Patreon\OAuth($uespPatreonClientId2, $uespPatreonClientSecret2);
$result = $oauth->refresh_token($this->refreshToken, "https://en.uesp.net/wiki/Special:UespPatreon/callback2");
print("OAuth Refresh Tokens:\n");
print_r($result);
if ($result['access_token'] == null) {
print("\tError: Failed to fresh access token!\n");
return false;
}
$this->accessToken = $result['access_token'];
$this->refreshToken = $result['refresh_token'];
$this->tokenExpires = time() + intval($result['expires_in']);
$safeValue = $this->db->real_escape_string($this->accessToken);
$this->lastQuery = "UPDATE patreon_info SET v='$safeValue' WHERE k='access_token';";
$result = $this->db->query($this->lastQuery);
if ($result === false) print("\tError: Failed to save access_token to database! " . $this->db->error . "\n\t" . $this->lastQuery . "\n");
$safeValue = $this->db->real_escape_string($this->refreshToken);
$this->lastQuery = "UPDATE patreon_info SET v='$safeValue' WHERE k='refresh_token';";
$result = $this->db->query($this->lastQuery);
if ($result === false) print("\tError: Failed to save refresh_token to database! " . $this->db->error . "\n\t" . $this->lastQuery . "\n");
$safeValue = $this->db->real_escape_string(strval($this->tokenExpires));
$this->lastQuery = "UPDATE patreon_info SET v='$safeValue' WHERE k='token_expires';";
$result = $this->db->query($this->lastQuery);
if ($result === false) print("\tError: Failed to save token_expires to database! " . $this->db->error . "\n\t" . $this->lastQuery . "\n");
print("\tSuccessfully refresh access/refresh tokens!\n");
return true;
}
public function loadAllPatreonUsers($quiet = true) {
if (!$quiet) print("\tLoading user data from Patreon...\n");
$api = new Patreon\API($this->accessToken);
$nextCursor = null;
$responses = [];
$pageCount = 0;
do {
$response = $api->fetch_page_of_members_from_campaign(UespPatreonCommon::$UESP_CAMPAIGN_ID, 2000, $nextCursor);
if ($response == null || count($response) == 0 || is_string($response)) {
if (!$quiet) print("\tError: Failed to load user data from Patreon: $response\n");
return false;
}
$responses[] = $response;
$nextCursor = null;
$meta = $response['meta'];
if ($meta)
{
$pagination = $meta['pagination'];
if ($pagination)
{
$cursors = $pagination['cursors'];
if ($cursors)
{
$nextCursor = $cursors['next'];
}
}
}
++$pageCount;
if ($nextCursor != null) print("\t$pageCount) Found next cursor: $nextCursor\n");
} while ($nextCursor != null);
//* Debug Output
//$output = print_r($responses, true);
//print($output);
//file_put_contents("/tmp/response.json", $output);
//exit(); //*/
$this->patreonUsers = [];
foreach ($responses as $response)
{
$this->patreonUsers = array_merge($this->patreonUsers, UespPatreonCommon::parsePatronData($response, false, false));
}
$count = count($this->patreonUsers);
if (!$quiet) print("\tLoaded $count patron records from Patreon!\n");
return true;
}
public function loadAllExistingUsers($quiet = true) {
$this->lastQuery = "SELECT * FROM patreon_user;";
$result = $this->db->query($this->lastQuery);
if ($result === false) {
if (!$quiet) print("\tError: Failed to load data from patreon_user table!\n\t" . $this->db->error . "\n\t" . $this->lastQuery . "\n");
return false;
}
while ($user = $result->fetch_assoc()) {
$id = intval($user['patreon_id']);
if ($id <= 0) continue;
$this->existingUsers[$id] = $user;
}
$count = count($this->existingUsers);
if (!$quiet) print("\tLoaded $count existing user data from database!\n");
return true;
}
public function mergeUsers() {
foreach ($this->patreonUsers as $user) {
$id = $user['patreon_id'];
$existingUser = &$this->existingUsers[$id];
if ($existingUser == null) {
$this->existingUsers[$id] = array( '__isnew' => true, 'id' => -1, 'patreon_id' => $id);
$existingUser = &$this->existingUsers[$id];
}
$existingUser['__isupdated'] = true;
// TODO: Tier Check
if ($existingUser['tier'] != $user['tier'] && $existingUser['tier'] != "" && $user['tier'] != "" && $existingUser['tier'] != "Free" && $user['tier'] != "Free") {
// Don't update tier changes on new or non-active users
if ( !$existingUser['__isnew'] && !($user['tier'] == "" && $user['status'] != "active_patron")) {
$newTier = array();
$newTier['user_id'] = $existingUser['id'];
$newTier['patreon_id'] = $id;
$newTier['oldTier'] = $existingUser['tier'];
$newTier['newTier'] = $user['tier'];
$newTier['date'] = time();
$this->tierChanges[] = $newTier;
}
}
$existingUser['name'] = $user['name'];
$existingUser['email'] = $user['email'];
$existingUser['discord'] = $user['discord'];
//print("{$user['name']} : {$user['status']} : {$user['tier']} : {$existingUser['tier']}\n");
// Don't change tiers for non-active users
//if ( !($user['tier'] == "" && $user['status'] != "active_patron") ) {
//if ($user['tier'] != "" || $user['status'] == "active_patron") {
if ($user['tier'] != "") {
//print("\tUpdated User Tier\n");
$existingUser['tier'] = $user['tier'];
}
$lastChargeDate = new DateTime($user['lastChargeDate']);
$startDate = new DateTime($user['startDate']);
$existingUser['status'] = $user['status'];
$existingUser['pledgeCadence'] = $user['pledgeCadence'];
$existingUser['note'] = $user['note'];
$existingUser['lifetimePledgeCents'] = $user['lifetimePledgeCents'];
$existingUser['startDate'] = $startDate->format('Y-m-d H:i:s');
$existingUser['lastChargeDate'] = $lastChargeDate->format('Y-m-d H:i:s');
$existingUser['addressName'] = $user['addressName'];
$existingUser['addressLine1'] = $user['addressLine1'];
$existingUser['addressLine2'] = $user['addressLine2'];
$existingUser['addressCity'] = $user['addressCity'];
$existingUser['addressState'] = $user['addressState'];
$existingUser['addressZip'] = $user['addressZip'];
$existingUser['addressCountry'] = $user['addressCountry'];
$existingUser['addressPhone'] = $user['addressPhone'];
//print("Name: " . $user['full_name'] . "\n");
}
/* Check for deleted users */
foreach ($this->existingUsers as &$user)
{
if ($user['__isupdated']) continue;
//print("\tSetting {$user['name']} to deleted\n");
$user['status'] = 'deleted_patron';
}
return true;
}
public function updateTierChanges() {
$count = 0;
foreach ($this->tierChanges as $tierChange) {
$cols = "patreon_id,oldTier,newTier,date";
$vals = "'" . $this->db->real_escape_string($tierChange['patreon_id']) . "'";
$vals .= ", '" . $this->db->real_escape_string($tierChange['oldTier']) . "'";
$vals .= ", '" . $this->db->real_escape_string($tierChange['newTier']) . "'";
$vals .= ", FROM_UNIXTIME('" . intval($tierChange['date']) . "')";
$this->lastQuery = "INSERT INTO patreon_tierchange($cols) VALUES($vals);";
$result = $this->db->query($this->lastQuery);
if ($result === false) print("\tError: Failed to update tier change! " . $this->db->error . "\n" . $this->lastQuery . "\n");
++$count;
}
print("\tUpdated $count tier changes!\n");
return true;
}
public function updateUsers() {
$COLNAMES = array("patreon_id", "name", "email", "discord", "tier", "status", "pledgeCadence", "note", "lifetimePledgeCents", "startDate", "lastChargeDate", "addressName", "addressLine1", "addressLine2",
"addressCity", "addressState", "addressZip", "addressCountry", "addressPhone");
$newUsers = 0;
$updatedUsers = 0;
foreach ($this->existingUsers as $id => $user)
{
$colNames = $COLNAMES;
if ($user['__isnew']) {
$colNames[] = "appCode";
$user['appCode'] = UespPatreonCommon::generateRandomAppCode();
$newUsers++;
$cols = array();
$vals = array();
foreach ($colNames as $field) {
$cols[] = $field;
$vals[] = $this->db->real_escape_string(strval($user[$field]));
}
$cols = implode(",", $cols);
$vals = "'" . implode("','", $vals) . "'";
$this->lastQuery = "INSERT INTO patreon_user($cols) VALUES($vals);";
}
else {
$updatedUsers++;
$vars = array();
if ($user['appCode'] == '')
{
$colNames[] = "appCode";
$user['appCode'] = UespPatreonCommon::generateRandomAppCode();
}
foreach ($colNames as $field) {
$value = $this->db->real_escape_string(strval($user[$field]));
$vars[] = "$field='$value'";
}
$vars = implode(",", $vars);
$userId = $user['id'];
$this->lastQuery = "UPDATE patreon_user SET $vars WHERE id='$userId';";
}
$result = $this->db->query($this->lastQuery);
if ($result === false) print("Error: Failed to update user $id in database! " . $this->db->error . "\n" . $this->lastQuery);
}
print("\tUpdated $updatedUsers existing users and added $newUsers new users!\n");
return true;
}
public function updateInfo() {
$now = time();
$this->lastQuery = "UPDATE patreon_info SET v='$now' WHERE k='last_update';";
$result = $this->db->query($this->lastQuery);
if ($result === false) print("Error: Failed to update last_update record database! " . $this->db->error . "\n" . $this->lastQuery);
return true;
}
public function run() {
if (!$this->loadAllPatreonUsers(false)) return false;
if (!$this->loadAllExistingUsers(false)) return false;
$this->mergeUsers();
$this->updateUsers();
$this->updateTierChanges();
$this->updateInfo();
return true;
}
};
$update = new UespPatreonUpdate();
$update->run();