-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.php
847 lines (738 loc) · 22.6 KB
/
api.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
<?php
/**
* EchoMTG Basic API Wrapper for PHP
*
* API documentation: https://www.echomtg.com/api/
* Source: http://github.com/andrewgioia/echophp
*
* Note: Library is in beta and provided as-is
* Date: 2015-11-09
* @version 0.1
*/
class EchoPHP {
/**
* API constants */
private $api_host = 'https://www.echomtg.com/api/';
/**
* Class variables */
protected $auth_token;
protected $auth_message;
protected $auth_status;
public $config;
public $error = false;
public $debug = array();
/**
* @param string $email (user's email address)
* @param string $password (user's password)
*/
public function __construct()
{
$this->config = parse_ini_file( 'config.local.ini' );
$this->email = $this->config[ 'email' ];
$this->password = $this->config[ 'password' ];
}
/**
* Get the authentication token from the session,
* or create the session and authenticate
*
* @return boolean
*/
public function initSession()
{
$session = session_id();
if ( empty( $session ) )
{
session_start();
}
if ( ! isset( $_SESSION[ 'echophp_session' ] ) || $_SESSION[ 'echophp_session' ] == '' )
{
$token = $this->authenticate();
if ( $token )
{
$this->auth_token = $token;
$_SESSION[ 'echophp_session' ] = $token;
$this->debugInfo( [ 'session' => [ 'login', $_SESSION[ 'echophp_session' ] ] ] );
return true;
}
else
{
$this->postError( [
'status' => 'error',
'message' => 'Unable to authentication, incorrect email or password' ] );
$this->debugInfo( [ 'session' => [ 'error', $_SESSION[ 'echophp_session' ] ] ] );
return false;
}
}
else
{
$this->auth_token = $_SESSION[ 'echophp_session' ];
$this->debugInfo( [ 'session' => [ 'saved', $_SESSION[ 'echophp_session' ] ] ] );
return true;
}
return false;
}
/**
* Destroy the authentication token and "log out"
*
* @return void
*/
public function logout()
{
session_start();
unset( $_SESSION[ 'echophp_session' ] );
session_destroy();
$this->debugInfo( [ 'session' => $_SESSION[ 'echophp_session' ] ] );
}
/**
* MANAGE INVENTORY */
/**
* Add X of one card to Inventory
* @param int $m_id (card's multiverse ID)
* @param int $quantity (amount to add)
* @param float $acquired_price (set the purchase price)
* @param string $acquired_date (set the date of purchase, MM-DD-YYYY
* @param boolean $foil (flag for whether the card is foil)
* @return array (response)
*/
public function addCard(
$mid,
$quantity = 1,
$acquired_price = false,
$acquired_date = false,
$foil = 0 )
{
// check that the multiverse ID is correct
if ( is_numeric( $mid ) && strlen( $mid ) > 0 )
{
$card[ 'mid' ] = $mid;
}
else
{
$this->postError( [
'status' => 'error',
'message' => 'Invalid card multiverse ID' ] );
}
// add the remaining fields
$card[ 'quantity' ] = $quantity;
$card[ 'foil' ] = $foil;
if ( $acquired_price ) $card[ 'acquired_price' ] = $acquired_price;
if ( $acquired_date ) $card[ 'acquired_date' ] = $acquired_date;
// attempt to add the card
$response = $this->sendPost(
'inventory/add/',
$card,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'add_card' => $response ] );
return $response;
}
/**
* Remove card from inventory
*
* @param int $eid (echomtg inventory ID for card)
* @return boolean
*/
public function removeCard( $eid )
{
// check that we have an integer first
$this->checkInventoryID( $eid );
// attempt to remove the card
$response = $this->sendPost(
'inventory/remove/',
[ 'inventory_id' => $eid ],
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'remove_card' => $response ] );
return $response;
}
/**
* Adjust acquisition price of a card in inventory
* NM = Near Mint
* LP = Lightly Played
* MP = Moderately Played
* HP = Heavily Played
* D = Damaged
* ALT = Altered
* SGN = Signed
*
* @param int @eid (echo inventory ID for the card)
* @param float $condition (adjusted condition)
* @return array
*/
public function adjustCondition( $eid, $condition )
{
// check that we have an integer first
$this->checkInventoryID( $eid );
// set the parameters
$request = [
'id' => $eid,
'value' => $condition ];
// attempt to remove the card
$response = $this->sendPost(
'inventory/change_condition/',
$request,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'adjust_condition' => $response ] );
return $response;
}
/**
* Adjust acquisition price of a card in inventory
*
* @param int @eid (echo inventory ID for the card)
* @param float $price (adjusted acquisition price)
* @return array
*/
public function adjustAcquiredPrice( $eid, $price )
{
// check that we have an integer first
$this->checkInventoryID( $eid );
// set the parameters
$request = [
'id' => $eid,
'adjusted_price' => $price ];
// attempt to remove the card
$response = $this->sendPost(
'inventory/adjust/',
$request,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'adjust_price' => $response ] );
return $response;
}
/**
* Toggle the foil status of a card in inventory
*
* @param int @eid (echo inventory ID for the card)
* @param boolean $foil (0 for nonfoil, 1 for foil)
* @return array
*/
public function toggleFoil( $eid, $foil = 1 )
{
// check that we have an integer first
$this->checkInventoryID( $eid );
// make sure it's a 0 or 1
$foil = ( $foil == 0 ) ? $foil : 1;
// set the parameters
$request = [
'id' => $eid,
'foil' => $foil ];
// attempt to remove the card
$response = $this->sendPost(
'inventory/toggle_foil/',
$request,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'toggle_foil' => $response ] );
return $response;
}
/**
* Adjust acquisition price of a card in inventory
*
* @param int @eid (echo inventory ID for the card)
* @param string $date (date of card acquisition, in MM-DD-YYYY)
* @return array
*/
public function adjustAcquiredDate( $eid, $date )
{
// check that we have an integer first
$this->checkInventoryID( $eid );
// format the date correctly
$date = date( 'm-d-Y', strtotime( $date ) );
// set the parameters
$request = [
'id' => $eid,
'value' => $date ];
// attempt to remove the card
$response = $this->sendPost(
'inventory/adjust_date/',
$request,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'adjust_date' => $response ] );
return $response;
}
/**
* INVENTORY */
/**
* Get user's inventory
*
* @param int $start
* @param int $end
* @param string $sort (price, cmc, foil_price, date_aquired, set)
* @param string $order (asc, desc)
* @param string $search (card name to search)
* @param string $color (Colorless, Multicolor, White, Blue, Black, Red, Green, Land)
* @param string $type (Planeswalker, Sorcery, Instant, Creature, Artifact,
* Enchantment, Legendary, Land)
* @param string $set_code (if showing only from a set)
* @return array
*/
public function getInventory(
$start = 0,
$end = 9,
$sort = 'date_acquired',
$order = 'desc',
$search = false,
$color = false,
$type = false,
$set_code = false )
{
// check that start and end are integers
if ( ! is_int( $start ) || ! is_int( $end ) )
{
$this->postError( [
'status' => 'error',
'message' => 'Invalid card multiverse ID' ] );
}
// set the base data fields
$query[ 'start' ] = $start;
$query[ 'limit' ] = $end;
$query[ 'sort' ] = $sort;
$query[ 'order' ] = $order;
// searching by card name
if ( $search ) $query[ 'search' ] = $search;
// filtering by a color
$color_options = [
'Colorless', 'Multicolor', 'White', 'Blue', 'Black', 'Red',
'Green', 'Land' ];
if ( $color )
{
if ( in_array( $color, $color_options ) )
{
$query[ 'color' ] = $color;
}
}
// filtering by type
$type_options = [
'Planeswalker', 'Sorcery', 'Instant', 'Creature', 'Artifact',
'Enchantment', 'Legendary', 'Land' ];
if ( $type )
{
if ( in_array( $type, $type_options ) )
{
$query[ 'type' ] = $type;
}
}
// filtering by set code
if ( $set_code ) $query[ 'set_code' ] = $set_code;
// attempt to add the card
$response = $this->sendPost(
'inventory/view/',
$query,
true,
'get' );
// set some debug logging
$this->debugInfo( [ 'view_inventory' => $response ] );
return $response;
}
/**
* Return the user's inventory statistics
*
* @return array
*/
public function getStats()
{
// make the request
$response = $this->sendPost(
'inventory/stats/',
[],
true,
'get' );
// set some debug logging
$this->debugInfo( [ 'inventory_stats' => $response ] );
return $response;
}
/**
* CARD REFERENCE */
/**
* Search EchoMTG's card list to return the appropriate ID
*
* @param string $cardname (card name to search)
* @param string $setcode (optional set code, otherwise first match returns)
* @return int $mid
*/
public function cardReference( $cardname = '', $setcode = false )
{
// make sure a card name was passed in
if ( trim( $cardname ) == '' || strlen( trim( $cardname ) ) == 0 )
{
$this->postError( [
'status' => 'error',
'message' => 'You need to pass a card name to search' ] );
}
// set the request fields
$request[ 'type' ] = 'json';
$request[ 'name' ] = $cardname; // does not do anything
// pull in the master list
$cards = $this->sendPost(
'data/card_reference/',
$request,
true,
'get' );
// currently we have to iterate over this object to search
// the api call needs to take a parameter to search in the future
$results = [];
foreach ( $cards->cards as $mid => $card )
{
if ( $card->name == $cardname )
{
$results[ $mid ] = $card;
}
}
// if we have a set code passed in, return just that row;
// otherwise return the earliest printing (lowest ID)
if ( count( $results ) > 0 )
{
if ( $setcode )
{
foreach ( $results as $id => $result )
{
if ( strtolower( $result->set_code ) == strtolower( $setcode ) )
{
return $id;
}
}
$this->postError( [
'status' => 'error',
'message' => 'No cards matched your search.' ] );
}
else
{
ksort( $results );
return array_keys( $results )[ 0 ];
}
}
else
{
$this->postError( [
'status' => 'error',
'message' => 'No cards matched your search.' ] );
}
}
/**
* LISTS */
/**
* Get a specific list
*
* @param int $lid (list ID)
* @param $html (flag to return preformatted HTML view)
* @return array (list of lists)
*/
public function getList( $lid = false, $html = false )
{
// validate the list id
$this->checkListID( $lid );
// set the fields
$fields = [
'list' => $lid,
'view' => ( $html ) ? 'true' : 'false' ];
// make the request
$response = $this->sendPost(
'lists/get/',
$fields,
true,
'get' );
// set some debug logging
$this->debugInfo( [ 'get_list' => $response ] );
return $response;
}
/**
* Get all of the user's lists
*
* @param string $order (optional sort order)
* @return array
*/
public function getAllLists( $order = 'last_edited' )
{
// set the optional sort order
$order_options = [ 'created', 'alpha_desc', 'alpha_asc', 'last_edited' ];
$fields[ 'order' ] = ( ! in_array( $order, $order_options ) )
? 'last_edited'
: $order;
// send the request
$response = $this->sendPost(
'lists/all/',
$fields,
true,
'get' );
// set some debug logging
$this->debugInfo( [ 'all_lists' => $response ] );
// cast as array and return just the lists
return (array)$response->lists;
}
/**
* Create a new list
*
* @param string $name (name of the list)
* @param string $description (description of the list)
* @return int $id (of newly created list)
*/
public function createList( $name = '', $description = '' )
{
// make sure we have a name
if ( trim( $name ) == '' || strlen( trim( $name ) ) == 0 )
{
$this->postError( [
'status' => 'error',
'message' => 'You need to supply a name for the list' ] );
}
// set the request fields
$fields[ 'name' ] = $name;
$fields[ 'description' ] = $description;
// send the request
$response = $this->sendPost(
'lists/create/',
$fields,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'create_list' => $response ] );
// if we have a successful new list, get the ID
if ( isset( $response->status ) && $response->status == 'success' )
{
$all_lists = $this->getAllLists();
rsort( $all_lists );
if ( is_array( $all_lists ) && count( $all_lists ) > 0 )
{
$newest_list = array_values( $all_lists )[ 0 ];
return ( isset( $newest_list->id ) )
? $newest_list->id
: false;
}
else
{
$this->postError( [
'status' => 'error',
'message' => 'Error retreiving new list id; list was created.' ] );
}
}
else
{
$this->postError( [
'status' => 'error',
'message' => 'Error creating the new list' ] );
}
}
/**
* Edit the name or description of a list
*
* @param int $lid (ID of the list to edit)
* @param string $name (name of the list)
* @param string $description (description of the list)
* @return boolean
*/
public function editList( $lid, $name = false, $description = false )
{
// validate the list id
$this->checkListID( $lid );
// set the list ID
$fields[ 'list' ] = $lid;
// if a name is set, make sure it isn't blank
if ( $name )
{
if ( trim( $name ) == '' || strlen( trim( $name ) ) == 0 )
{
$this->postError( [
'status' => 'error',
'message' => 'You need to supply a name for the list' ] );
}
else
{
$fields[ 'name' ] = $name;
}
}
// no need to validate the description, just pass it if it exists
if ( $description )
{
$fields[ 'description' ] = $description;
}
// send the request
$response = $this->sendPost(
'lists/edit/',
$fields,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'edit_list' => $response ] );
// return true on success, otherwise false
return $this->returnStatus( $response );
}
/**
* Toggle a list's activated status (active or deactivated)
*
* @param int $lid (the ID of the list to toggle)
* @param boolean $status (0 for deactivated, 1 for active)
* @return boolean (true for success, false for failure)
*/
public function toggleListStatus( $lid, $status = 1 )
{
// validate the list id
$this->checkListID( $lid );
// create the field array
$fields[ 'list' ] = $lid;
$fields[ 'status' ] = ( in_array( $status, [ 0, 1 ] ) )
? $status
: 1;
// send the request
$response = $this->sendPost(
'lists/toggle_status/',
$fields,
true,
'post' );
// set some debug logging
$this->debugInfo( [ 'toggle_list_status' => $response ] );
// return true on success, otherwise false
return $this->returnStatus( $response );
}
/**
* UTILITIES */
/**
* Send an authentication request
*
* @return string (response token)
*/
private function authenticate()
{
// attempt to login
$response = $this->sendPost(
'user/auth/',
[ 'email' => $this->email, 'password' => $this->password ],
false,
'post' );
// set some debug logging
$this->debugInfo( [ 'auth' => $response ] );
// return the token if we have it
if ( isset( $response->status ) && $response->status == 'success' )
{
return $response->token;
}
else
{
return false;
}
}
/**
* Post to an EchoMTG API endpoint
*
* @param $endpoint (path to api call)
* @param $fields (array of data fields to send in the POST)
* @param $auth (flag for whether this request is authenticated)
* @param $post (flag for sending as a post, otherwise get)
* @return array (json response)
*/
private function sendPost(
$endpoint = false,
$fields = array(),
$auth = true,
$method = 'post' )
{
// build the query from the data fields
if ( $auth ) { $fields[ 'auth' ] = $this->auth_token; }
$data = http_build_query( $fields );
// check for the method
$method = ( ! in_array( $method, [ 'post', 'put', 'get' ] ) )
? 'post'
: $method;
// get the url to post to
$uri = ( $endpoint )
? $this->api_host.$endpoint
: $this->api_host;
// set some debug logging
$this->debugInfo( [ $method => [ $uri, $data ] ] );
// create the full request based on post/get/put status
$request = ( $method == 'get' )
? $uri.$data
: $uri;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt( $ch, CURLOPT_URL, $request );
// using SSL
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
// post headers
if ( $method == 'post' ) {
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
}
// put headers
else if ( $method == 'put' )
{
curl_setopt( $ch, CURLOPT_PUT, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
}
// query and header options
curl_setopt( $ch, CURLOPT_HEADER, false ) ;
curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// get it
$response = trim( curl_exec( $ch ) );
$response = json_decode( $response );
// close the connection
curl_close( $ch );
return $response;
}
/**
* Save to the error property
*
* @param array $object (error array with status and message)
* @return void
*/
private function postError( $object )
{
$this->error = json_encode( $object );
}
/**
* Save to the debug property
*
* @param array $object (debug array with status and message)
* @return void
*/
private function debugInfo( $object )
{
if ( $this->config[ 'debug_mode' ] )
{
$this->debug[] = $object;
}
}
private function checkInventoryID( $eid )
{
if ( ! is_int( $eid ) || $eid < 1 )
{
$this->postError( [
'status' => 'error',
'message' => 'The card ID is not an integer.' ] );
return false;
}
}
private function checkListID( $lid )
{
if ( ! is_int( $lid ) || $lid < 1 )
{
$this->postError( [
'status' => 'error',
'message' => 'The list ID is not an integer.' ] );
return false;
}
}
private function returnStatus( $response )
{
if ( is_object( $response ) && isset( $response->status ) )
{
return ( $response->status == 'success' )
? true
: false;
}
else
{
return false;
}
}
}