forked from filler00/etcg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.php
477 lines (314 loc) · 14.9 KB
/
func.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php if ( ! defined('VALID_INC') ) exit('No direct script access allowed');
require_once('etcg/config.php');
class Sanitize {
function clean ($data) {
$data = trim(htmlentities(strip_tags($data)));
return $data;
}
function for_db ($data) {
$database = new Database;
$link = $database->connect();
$data = $this->clean($data);
$data = mysqli_real_escape_string($link, $data);
return $data;
}
}
class Database {
function connect () {
$link = @mysqli_connect( Config::DB_SERVER , Config::DB_USER , Config::DB_PASSWORD, Config::DB_DATABASE )
or die( "Couldn't connect to MYSQL: ".mysqli_error($link) );
return $link;
}
function query ($query) {
$link = $this->connect();
$result = mysqli_query($link, $query);
return $result;
}
function get_assoc ($query) {
$link = $this->connect();
$result = mysqli_query($link, $query);
if ( !$result ) { die ( "Couldn't process query: ".mysqli_error($link) ); }
$assoc = mysqli_fetch_assoc($result);
return $assoc;
}
function get_array ($query) {
$link = $this->connect();
$result = mysqli_query($link, $query);
if ( !$result ) { die ( "Couldn't process query: ".mysqli_error($link) ); }
$array = mysqli_fetch_array($result);
return $array;
}
function num_rows ($query) {
$link = $this->connect();
$result = mysqli_query($link, $query);
if ( !$result ) { die ( "Couldn't process query: ".mysqli_error($link) ); }
$num_rows = mysqli_num_rows($result);
return $num_rows;
}
function error () {
$link = $this->connect();
$result = mysqli_error($link);
return $result;
}
}
// Returns contents of the specified log. $tcg = the name of the TCG as defined in the database; $type = activitylog, activitylogarch, tradelog, or tradelogarch.
function get_logs( $tcg, $type ) {
if ( $type == 'activitylog' || $type == 'activitylogarch' || $type == 'tradelog' || $type == 'tradelogarch' ) {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$log = $database->get_assoc("SELECT `$type` FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
return $log[$type];
}
else {
return false;
}
}
// Returns the value of an additional field. $tcg = the name of the TCG as defined in the database; $fieldname = the name of the additional field.
function get_additional( $tcg, $fieldname ) {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$fieldname = $sanitize->for_db($fieldname);
$result = $database->get_assoc("SELECT `id` FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $result['id'];
$result = $database->get_assoc("SELECT `value` FROM `additional` WHERE `tcg`='$tcgid' AND `name`='$fieldname'");
return $result['value'];
}
// Show cards from the given category. $tcg = the name of the TCG as defined in the database; $category = the card category to display.
function show_cards( $tcg, $category, $unique = 0 ) {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$category = $sanitize->for_db($category);
$altname = strtolower(str_replace(' ','',$tcg));
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
$cardsurl = $tcginfo['cardsurl'];
$format = $tcginfo['format'];
$cards = $database->get_assoc("SELECT `cards`, `format` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='$category' LIMIT 1");
if ( $cards['format'] != 'default' ) { $format = $cards['format']; }
if ( $cards['cards'] === '' ) { echo '<p><em>There are currently no cards under this category.</em></p>'; }
else {
$cards = explode(',',$cards['cards']);
$cards = array_map('trim', $cards);
if ( $unique == 1 ) { $cards = array_unique($cards); }
foreach ( $cards as $card ) {
$card = trim($card);
echo '<img src="'.$cardsurl.''.$card.'.'.$format.'" alt="" title="'.$card.'" /> ';
}
}
}
function show_doubles( $tcg, $category ) {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$category = $sanitize->for_db($category);
$altname = strtolower(str_replace(' ','',$tcg));
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
$cardsurl = $tcginfo['cardsurl'];
$format = $tcginfo['format'];
$cards = $database->get_assoc("SELECT `cards`, `format` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='$category' LIMIT 1");
if ( $cards['format'] != 'default' ) { $format = $cards['format']; }
if ( $cards['cards'] === '' ) { echo '<p><em>There are currently no cards under this category.</em></p>'; }
else {
$cards = explode(',',$cards['cards']);
$cards = array_map('trim', $cards);
$doubles = array_diff_assoc($cards, array_unique($cards));
if ( !empty($doubles) ) {
foreach ( $doubles as $double ) {
echo '<img src="'.$cardsurl.''.$double.'.'.$format.'" alt="" title="'.$double.'" /> ';
}
}
else {
echo '<p><em>There are currently no cards under this category.</em></p>';
}
}
}
function trim_value(&$value) { $value = trim($value); }
// Show all collecting decks, or optionally show collecting decks by worth or deck name. $tcg = the name of the TCG as defined in the database; $worth = card worth; $deckname = name of a collecting deck.
function show_collecting($tcg, $worth = '', $deckname = '') {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
$cardsurl = $tcginfo['cardsurl'];
$format = $tcginfo['format'];
if ( $worth !== '' ) { $worth = intval($worth); }
if ( $deckname !== '' ) { $deckname = $sanitize->for_db($deckname); }
if ( $worth !== '' ) { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '0' AND `worth` = '$worth' ORDER BY `sort`, `deck`"); }
else if ( $deckname !== '' ) { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '0' AND `deck` = '$deckname' ORDER BY `sort`, `worth`"); }
else { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '0' ORDER BY `sort`, `worth`, `deck`"); }
while ( $row = mysqli_fetch_assoc($result) ) {
$cards = explode(',',$row['cards']);
if ( $row['format'] != 'default' ) { $format = $row['format']; }
array_walk($cards, 'trim_value');
if ( $row['cards'] == '' ) { $count = 0; } else { $count = count($cards); }
?>
<h2><?php echo $row['deck']; ?> (<?php echo $count; ?>/<?php echo $row['count']; ?>)</h2>
<p align="center">
<?php
for ( $i = 1; $i <= $row['count']; $i++ ) {
$number = $i;
if ( $number < 10 ) { $number = "0$number"; }
$card = "".$row['deck']."$number";
$pending = $database->num_rows("SELECT * FROM `trades` WHERE `tcg`='$tcgid' AND `receiving` LIKE '%$card%'");
if ( in_array($card, $cards) ) echo '<img src="'.$tcginfo['cardsurl'].''.$card.'.'.$format.'" alt="" title="'.$card.'" />';
else if ( $pending > 0 ) { echo '<img src="'.$tcginfo['cardsurl'].''.$row['pending'].'.'.$format.'" alt="" title="'.$card.'" />'; }
else { echo '<img src="'.$tcginfo['cardsurl'].''.$row['filler'].'.'.$format.'" alt="" />'; }
if ( $row['puzzle'] == 0 ) { echo ' '; }
if ( $row['break'] !== '0' && $i % $row['break'] == 0 ) { echo '<br />'; }
}
?>
</p>
<?php
}
}
// Show all mastered decks (as badges), or optionally show mastered decks by worth or deck name. $tcg = the name of the TCG as defined in the database; $worth = card worth; $deckname = name of a mastered deck.
function show_mastered($tcg, $worth = '', $deckname = '') {
$database = new Database;
$sanitize = new Sanitize;
$tcg = $sanitize->for_db($tcg);
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
$cardsurl = $tcginfo['cardsurl'];
$format = $tcginfo['format'];
if ( $worth !== '' ) { $worth = intval($worth); }
if ( $deckname !== '' ) { $deckname = $sanitize->for_db($deckname); }
if ( $worth !== '' ) { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '1' AND `worth` = '$worth' ORDER BY `mastereddate`"); }
else if ( $deckname !== '' ) { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '1' AND `deck` = '$deckname' ORDER BY `mastereddate`"); }
else { $result = $database->query("SELECT * FROM `collecting` WHERE `tcg` = '$tcgid' AND `mastered` = '1' ORDER BY `mastereddate`"); }
while ( $row = mysqli_fetch_assoc($result) ) {
$mastered = date('F d, Y', strtotime($row['mastereddate']));
if ( $row['badge'] !== '' ) { echo '<img src="'.$tcginfo['cardsurl'].''.$row['badge'].'" alt="" title="Mastered '.$mastered.'" /> '; }
else { echo ''.$row['deck'].' '; }
}
}
// Show all pending trades. $tcg = the name of the TCG as defined in the database.
function show_pending($tcg) {
$database = new Database;
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
$cardsurl = $tcginfo['cardsurl'];
$format = $tcginfo['format'];
$result = $database->query("SELECT * FROM `trades` WHERE `tcg`='$tcgid' ORDER BY `date`,`trader`");
while ( $row = mysqli_fetch_assoc($result) ) {
$receiving = str_replace(';',',',$row['receiving']);
echo '- <strong>'.$row['trader'].'</strong> (pending since <em>'.date('F d, Y', strtotime($row['date'])).'</em>) <br />';
if ( $receiving !== '' ) { echo ''.$receiving.' <br />'; }
if ( $row['giving'] !== '' ) {
$cardgroups = explode(';',$row['giving']);
$cardcats = explode(',',$row['givingcat']);
array_walk($cardcats, 'trim_value');
$i = 0;
foreach ( $cardgroups as $group ) {
$group = explode(',',$group);
array_walk($group, 'trim_value');
if ( $cardcats[$i] === 'collecting' ) {
foreach ( $group as $card ) {
$exists = $database->num_rows("SELECT `id` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='0' AND `deck` LIKE '%".substr($card, -2)."%' LIMIT 1");
if ( $exists > 0 ) {
$deckinfo = $database->get_assoc("SELECT `format` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='0' AND `deck` LIKE '%".substr($card, -2)."%' LIMIT 1");
if ( $deckinfo['format'] != 'default' ) { $format = $deckinfo['format']; }
echo '<img src="'.$cardsurl.''.$card.'.'.$format.'" alt="" title="'.$card.'" /> ';
}
else {
echo '<img src="'.$cardsurl.''.$card.'.'.$format.'" alt="" title="'.$card.'" /> ';
}
}
}
else {
foreach ( $group as $card ) {
$catinfo = $database->get_assoc("SELECT `format` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='".$cardcats[$i]."' LIMIT 1");
if ( $catinfo['format'] != 'default' ) { $format = $catinfo['format']; }
else { $format = $tcginfo['format']; }
echo '<img src="'.$cardsurl.''.$card.'.'.$format.'" alt="" title="'.$card.'" /> ';
}
}
$i++;
}
}
echo '<br /><br />';
/**
$giving = explode(',',str_replace(';',',',$row['giving']));
$receiving = str_replace(';',',',$row['receiving']);
echo '- <strong>'.$row['trader'].'</strong> (pending since <em>'.date('F d, Y', strtotime($row['date'])).'</em>) <br />';
if ( $receiving !== '' ) { echo ''.$receiving.' <br />'; }
foreach( $giving as $card ) {
$card = trim($card);
echo '<img src="'.$cardsurl.''.$card.'.'.$format.'" alt="" title="'.$card.'" /> ';
}
echo '<br /><br />';
**/
}
}
// Generate a total card count/worth of all cards in the collection, or by category. $tcg = the name of the TCG as defined in the database; $type = worth OR empty to ignore worth; $cat = 'CATEGORY NAME' OR 'collecting' OR 'mastered' OR 'pending' OR empty to count all cards in collection
function cardcount ($tcg,$type = '',$cat = '') {
$database = new Database;
$tcginfo = $database->get_assoc("SELECT * FROM `tcgs` WHERE `name`='$tcg' LIMIT 1");
$tcgid = $tcginfo['id'];
// Count categories
$result = $database->query("SELECT `category`,`cards`,`worth` FROM `cards` WHERE `tcg`='$tcgid'");
while ( $row = mysqli_fetch_assoc($result) ) {
$categories[] = $row['category'];
if ( $row['cards'] === '' ) { $$row['category'] = 0; } else { $cards = explode(',',$row['cards']); $$row['category'] = count($cards); }
if ( $type === 'worth' ) { $$row['category'] = $$row['category'] * $row['worth']; }
$total = $total + $$row['category'];
}
// Count collecting
$result = $database->query("SELECT `cards`,`worth` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='0'");
while ( $row = mysqli_fetch_assoc($result) ) {
if ( $row['cards'] !== '' ) {
$cards = explode(',',$row['cards']);
if ( $type === 'worth' ) { $collecting = $collecting + count($cards) * $row['worth']; }
else { $collecting = $collecting + count($cards); }
}
}
$total = $total + $collecting;
// Count mastered
$result = $database->query("SELECT `cards`,`worth` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='1'");
while ( $row = mysqli_fetch_assoc($result) ) {
if ( $row['cards'] !== '' ) {
$cards = explode(',',$row['cards']);
if ( $type === 'worth' ) { $mastered = $mastered + count($cards) * $row['worth']; }
else { $mastered = $mastered + count($cards); }
}
}
$total = $total + $mastered;
// Count pending
$result = $database->query("SELECT `giving`,`givingcat` FROM `trades` WHERE `tcg`='$tcgid'");
while ( $row = mysqli_fetch_assoc($result) ) {
if ( $row['giving'] !== '' ) {
$cardgroups = explode(';',$row['giving']);
$cardcats = explode(',',$row['givingcat']);
array_walk($cardcats, 'trim_value');
$i = 0;
foreach ( $cardgroups as $group ) {
$group = explode(',',$group);
array_walk($group, 'trim_value');
if ( $cardcats[$i] === 'collecting' ) {
$exists = $database->num_rows("SELECT `worth` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='0' AND `deck` LIKE '%".$group[0]."%'");
if ( $exists > 0 ) {
$groupworth = $database->get_assoc("SELECT `worth` FROM `collecting` WHERE `tcg`='$tcgid' AND `mastered`='0' AND `deck` LIKE '%".$group[0]."%' LIMIT 1");
if ( $type === 'worth' ) { $pending = $pending + count($group) * $groupworth['worth']; } else { $pending = $pending + count($group); }
}
else {
$pending = $pending + count($group);
}
}
else {
$groupworth = $database->get_assoc("SELECT `worth` FROM `cards` WHERE `tcg`='$tcgid' AND `category`='".$cardcats[$i]."'");
if ( $type === 'worth' ) { $pending = $pending + count($group) * $groupworth['worth']; } else { $pending = $pending + count($group); }
}
$i++;
}
}
}
$total = $total + $pending;
if ( $cat === '' ) { return $total; }
else { return $$cat; }
}
if ( file_exists(__DIR__ . '/mods.php') ) { include_once('mods.php'); }
?>