forked from bobwol/magrocket-api
/
index.php
955 lines (804 loc) · 44.6 KB
/
index.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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
<?php
require 'Slim/Slim.php';
// Pimple Dependency Injection Container
require_once 'Pimple.php';
\Slim\Slim::registerAutoloader();
$app = new Slim\Slim(array(
'mode' => 'production',
'debug' => false
));
// Set Content Type and Encoding.
$app->contentType('text/html; charset=utf-8');
// Set Default Timezone to America/Los_Angeles as this is what all App Store dates are in.
date_default_timezone_set('America/Los_Angeles');
// Create Pimple Dependency Injection Container for DB Connection.
$dbContainer = new Pimple();
// Global Development Mode Setting
$developmentMode = '';
// Global iTunes Production Level Setting
$iTunesProductionLevel = '';
// Global iTunes Receipt Validation Caching Setting
$iTunesCachingDuration = -1;
// Global iTunes Receipt Validation Caching Setting
$subscriptionBehavior = '';
// API Version
$apiVersion = '1.1';
// DB Setup for Pimple Container
// MagRocket API SETUP CONFIGURATION SETTING
// ************************************************************
$dbContainer['db.options'] = array(
'host' => 'localhost', // CONFIGURE TO YOUR DB HOSTNAME
'username' => 'USERNAME', // CONFIGURE TO YOUR DB USERNAME
'password' => 'PASSWORD', // CONFIGURE TO YOUR DB USERNAME'S PASSWORD
'dbname' => 'DBNAME' // CONFIGURE TO YOUR DB INSTANCE NAME
);
//*************************************************************
// Using "share" method makes sure that the function is only called when 'db' is retrieved the first time.
$dbContainer['db'] = $dbContainer->share(function () use($dbContainer)
{
// Get DB handle and create new PDO DB object using configuration settings
$dbHandle = new PDO('mysql:host=' . $dbContainer['db.options']['host'] . ';dbname=' . $dbContainer['db.options']['dbname'], $dbContainer['db.options']['username'], $dbContainer['db.options']['password']);
// Set Character Set for DB connection to UTF-8
$dbHandle -> exec("SET CHARACTER SET utf8");
// Return DB handle
return $dbHandle;
});
// ************************************************
// SLIM PHP Methods for handling REST API Methods
// ************************************************
// GET route
$app->get('/', function () {
$SCRIPT_LOCATION = str_replace('index.php','',$_SERVER['SCRIPT_NAME']);
$template = "
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>MagRocket REST API</title>
<style>
html,body,div,span,object,iframe,
h1,h2,h3,h4,h5,h6,p,blockquote,pre,
abbr,address,cite,code,
del,dfn,em,img,ins,kbd,q,samp,
small,strong,sub,sup,var,
b,i,
dl,dt,dd,ol,ul,li,
fieldset,form,label,legend,
table,caption,tbody,tfoot,thead,tr,th,td,
article,aside,canvas,details,figcaption,figure,
footer,header,hgroup,menu,nav,section,summary,
time,mark,audio,video{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;}
body{line-height:1;}
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section{display:block;}
nav ul{list-style:none;}
blockquote,q{quotes:none;}
blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}
a{margin:0;padding:0;font-size:100%;vertical-align:baseline;background:transparent;}
ins{background-color:#ff9;color:#000;text-decoration:none;}
mark{background-color:#ff9;color:#000;font-style:italic;font-weight:bold;}
del{text-decoration:line-through;}
abbr[title],dfn[title]{border-bottom:1px dotted;cursor:help;}
table{border-collapse:collapse;border-spacing:0;}
hr{display:block;height:1px;border:0;border-top:1px solid #cccccc;margin:1em 0;padding:0;}
input,select{vertical-align:middle;}
html{ background: #EDEDED; height: 100%; }
body{background:#FFF;margin:0 auto;min-height:100%;padding:0 30px;width:440px;color:#666;font:14px/23px Arial,Verdana,sans-serif;}
h1,h2,h3,p,ul,ol,form,section{margin:0 0 20px 0;}
h1{color:#333;font-size:20px;}
h2,h3{color:#333;font-size:14px;}
h3{margin:0;font-size:12px;font-weight:bold;}
ul,ol{list-style-position:inside;color:#999;}
ul{list-style-type:square;}
code,kbd{background:#EEE;border:1px solid #DDD;border:1px solid #DDD;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;padding:0 4px;color:#666;font-size:12px;}
pre{background:#EEE;border:1px solid #DDD;border-radius:4px;-moz-border-radius:4px;-webkit-border-radius:4px;padding:5px 10px;color:#666;font-size:12px;}
pre code{background:transparent;border:none;padding:0;}
a{color:#0068ae;}
header{padding: 30px 0;text-align:center;}
</style>
</head>
<body>
<header>
<a href='http://www.magrocket.com'><img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHMAAABRCAYAAADhJ5nbAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3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N9YcMM33DRLe+aJimYNrGBZ4wAxDfLmGK800sut+8P2Os3S8BN4Wz82KmH7MifmWxRPKSRi1LxIiJUiryfJ7BQ07KPG8Z4MUUutYQWBgoDy1MbaQ2PNvyaqOibPN+L+L+1OrXGiPlTdfpCoV9vpiyZMxzlslPIsxXzs0Iu4qekvh0WP5wcCBL5YVRYtKGi0CzIVbRUZNq45i6jobvr2EQ1btvzhlGXT1inokuFPoEgw8pXR/x3bqH0IdqhrePj0v5szc9ap/JGS5I6quQb/cQyt43ngbQSOjTJqS20Zj4yjDDohK34p6S+O6fPU02MJsZ2APoKCqfl7mNYERjisbBzxwuxQjGUNeVUVsZo1b/k2DQ+w9T8E1uyewnGEl42lhuS01f3UAwnHansSA7p6A7lP5/ABmVkhjN1LvEAAAAAElFTkSuQmCC' alt='MagRocket'/></a>
</header>
<h1>Welcome to MagRocket!</h1>
<p>
Congratulations! Your MagRocket API is up and running. However, you should test the Database connectivity below. If you get a success message, then you should be good to go!
</p>
<section>
<h2>Get Started</h2>
<ol>
<li><a href='" . $SCRIPT_LOCATION . "checkinstall' target='_blank'>Check DB Connectivity</a></li>
<li>Read the <a href='http://support.magrocket.com/' target='_blank'>online documentation</a></li>
<li>Follow <a href='http://www.twitter.com/magrocket' target='_blank'>@magrocket</a> on Twitter</li>
</ol>
</section>
<section>
<h2>MagRocket Community</h2>
<h3>Support Forum and Knowledge Base</h3>
<p>
Visit the <a href='http://support.magrocket.com/' target='_blank'>MagRocket Support Forum and Knowledge Base</a>
to read announcements, chat with fellow MagRocket users, ask questions, help others, or show off your cool
Baker Newsstand apps. You might also check out the <a href='http://github.com/nin9creative/magrocket-backend' target='_blank'>Github Project</a> as there will probably be lots of discussion happening
over there.
</p>
<h3>Twitter</h3>
<p>
Follow <a href='http://www.twitter.com/magrocket' target='_blank'>@magrocket</a> on Twitter to receive the very latest news
and updates about the framework.
</p>
</section>
</body>
</html>";
echo $template;
});
// Check DB Connectivity
// *Makes connection to MagRocket DB and tries to make a select from the PUBLICATION table
$app->get('/checkinstall/', function ()
{
try {
global $dbContainer;
$db = $dbContainer['db'];
$result = $db->prepare("SELECT * FROM PUBLICATION");
$result->execute();
$checkInstall = $result->fetchAll();
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
echo '{"MagRocket API":{"Success":"Database Connection Test Successful"}}';
}
catch(PDOException $e) {
echo '{"MagRocket API":{"Error":"' . $e->getMessage() . '"}}';
}
});
// Output Debug Information
// *Outputs the settings for a given publication that the API is registering, useful when debugging
$app->get('/debuginformation/:app_id', function ($app_id)
{
global $apiVersion;
try {
echo 'MagRocket API: Debug Information';
echo '<BR>API Version: ' . $apiVersion;
echo '<BR>Development Mode: ' . isInDevelopmentMode($app_id);
echo '<BR>Subscription Behavior: ' . getSubscriptionBehavior($app_id);
echo '<BR>iTunes Production Level: ' . getiTunesProductionLevel($app_id);
echo '<BR>iTunes Caching Duration: ' . getiTunesCachingDuration($app_id);
}
catch(PDOException $e) {
}
});
// Issues List
// *Retrieves a list of available issues for the App ID, for population of Baker Shelf
$app->get('/issues/:app_id/:user_id', function ($app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
$SCRIPT_LOCATION = str_replace('index.php','',$_SERVER['SCRIPT_NAME']);
// Lookup Issue Download Security condition for Publication, if true, create secured API Issue download links
$result = $db->query("SELECT ISSUE_DOWNLOAD_SECURITY FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
$issueDownloadSecurity = $result->fetchColumn();
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Retrieving Issues for APP ID: " . $app_id . " USER ID: " . $user_id);}
// Query all issues for the incoming APP_ID
$sql = "SELECT * FROM ISSUES WHERE APP_ID = '$app_id' AND AVAILABILITY = 'published'";
try {
$IssuesArray = array();
$i = 0;
foreach($db->query($sql) as $row) {
$IssuesArray[$i]['name'] = $row['NAME'];
$IssuesArray[$i]['title'] = $row['TITLE'];
$IssuesArray[$i]['info'] = $row['INFO'];
$IssuesArray[$i]['date'] = $row['DATE'];
$IssuesArray[$i]['cover'] = $row['COVER'];
if ($issueDownloadSecurity == "TRUE") {
$IssuesArray[$i]['url'] = "http://" . $_SERVER['HTTP_HOST'] . $SCRIPT_LOCATION . "issue/" . $app_id . "/" . $user_id . "/" . $row['NAME'];
}
else{
$IssuesArray[$i]['url'] = $row['URL'];
}
if($row['PRICING'] != 'free')
{
$IssuesArray[$i]['product_id'] = $row['PRODUCT_ID'];
}
$i++;
}
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
echo json_encode($IssuesArray);
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
});
// Issue Download
// *Validates availability of download of a specific named issue, redirects to download if available
$app->get('/issue/:app_id/:user_id/:name', function ($app_id, $user_id, $name) use($app)
{
global $dbContainer;
$db = $dbContainer['db'];
try {
$result = $db->prepare("SELECT * FROM ISSUES WHERE APP_ID = '$app_id' AND NAME = '$name' LIMIT 0,1");
$result->execute();
$issue = $result->fetch();
// The Issue is not found for App_ID and Name. Throw 404 not found error.
if (!$issue) {
header('HTTP/1.1 404 Not Found');
die();
}
// Retrieve issue Issue Product ID to cross check with purchases
$product_id = $issue['PRODUCT_ID'];
// Default to not allow download.
$allow_download = false;
// Validate that the Product ID (from Issue Name) is an available download for given user
if ($product_id) {
// Allow download if the issue is marked as purchased
$result = $db->query("SELECT COUNT(*) FROM PURCHASES
WHERE APP_ID = '$app_id' AND USER_ID = '$user_id' AND PRODUCT_ID = '$product_id'");
$allow_download = ($result->fetchColumn() > 0);
} else if ($issue['PRICING'] == 'free') {
// Issue is marked as free, allow download
$allow_download = true;
}
if ($allow_download) {
if((isInDevelopmentMode($app_id)=="TRUE") && !($app->request()->isHead())){logMessage(LogType::Info,"Downloading ISSUE: " . $name . " for APP ID: " . $app_id . " USER ID: " . $user_id);}
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
if(!($app->request()->isHead())){logAnalyticMetric(AnalyticType::Download,1,$name,$app_id,$user_id);}
// Redirect to the downloadable file, nothing else needed in API call
$app->response()->redirect($issue['URL'], 303);
}
else {
header('HTTP/1.1 403 Forbidden');
die();
}
}
catch(PDOException $e) {
// Handle exception
logMessage(LogType::Error, $e->getMessage());
}
});
// Purchases List
// *Returns a list of Purchased Product ID's
$app->get('/purchases/:app_id/:user_id', function ($app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
$purchased_product_ids = array();
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Checking purchases for APP ID: " . $app_id . " USER ID: " . $user_id);}
try {
$subscribed = false;
// Retrieve latest receipt for Auto-Renewable-Subscriptions for the APP_ID, USER_ID combination
$result = $db->query("SELECT BASE64_RECEIPT FROM RECEIPTS
WHERE APP_ID = '$app_id' AND USER_ID = '$user_id' AND TYPE = 'auto-renewable-subscription'
ORDER BY TRANSACTION_ID DESC LIMIT 0, 1");
$base64_latest_receipt = $result->fetchColumn();
if($base64_latest_receipt)
{
$userSubscription = checkSubscription($app_id, $user_id);
$dateLastValidated = new DateTime($userSubscription["LAST_VALIDATED"]);
$dateExpiration = new DateTime($userSubscription["EXPIRATION_DATE"]);
$dateCurrent = new DateTime('now');
$interval = $dateCurrent->diff($dateLastValidated);
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Time since last validating receipt for APP ID: " . $app_id . " USER ID: " . $user_id . " = " . $interval->format('%h hours %i minutes') );}
// Only refresh and re-verify receipt if greater than the iTunesCachingDuration - or greater than 1 whole day
if ((getiTunesCachingDuration($app_id) == -1) || ($interval->format('%h') > getiTunesCachingDuration($app_id)) || ($interval->format('%a') > 1)) {
// Check the latest receipt from the subscription table
if ($base64_latest_receipt) {
// Verify Receipt - with logic to fall back to Sandbox test if Production Receipt fails (error code 21007)
try{
$data = verifyReceipt($base64_latest_receipt, $app_id, $user_id);
}
catch(Exception $e) {
if($e->getCode() == "21007"){
logMessage(LogType::Info,"Confirming purchase for APP ID - Sandbox Receipt used in Production, retrying against Sandbox iTunes API: " . $app_id . " USER ID: " . $user_id . " TYPE: " . $type);
$data = verifyReceipt($base64_latest_receipt, $app_id, $user_id, TRUE);
}
}
markIssuesAsPurchased($data, $app_id, $user_id);
// Check if there is an active subscription for the user. Status=0 is true.
$subscribed = ($data->status == 0);
}
else {
// There is no receipt for this user, there is no active subscription
$subscribed = false;
}
}
else {
// We aren't going to re-verify the receipt now, but we should determine if the Expiration Date is beyond now
if ($dateCurrent > $dateExpiration) {
$subscribed = false;
}
else {
$subscribed = true;
}
}
}
else
{
// There is no Auto-Renewable-Subscription for the APP_ID, USER_ID combination - check if there is a Free-Subscription
$result = $db->query("SELECT BASE64_RECEIPT FROM RECEIPTS
WHERE APP_ID = '$app_id' AND USER_ID = '$user_id' AND TYPE = 'free-subscription'
ORDER BY TRANSACTION_ID DESC LIMIT 0, 1");
$base64_latest_receipt = $result->fetchColumn();
// If there is a receipt for a free-subscription then we will return that the USER_ID is subscribed. Since a free
// subscription really doesn't have and valid term then we will just ignore any dates in the Apple receipt.
// However the list of purchased product_ids can still be blank because the issues should be marked as free.
if($base64_latest_receipt){
$subscribed = true;
}
}
// Return list of purchased product_ids for the user
$result = $db->query("SELECT PRODUCT_ID FROM PURCHASES
WHERE APP_ID = '$app_id' AND USER_ID = '$user_id'");
$purchased_product_ids = $result->fetchAll(PDO::FETCH_COLUMN);
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
echo json_encode(array(
'issues' => $purchased_product_ids,
'subscribed' => $subscribed
));
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
});
// iTunes List
// *Returns a list of Issues in an iTunes ATOM Feed XML Format. This can be hooked up to the FEED URL within
// iTunes connect to display up to date information in the Newsstand App Store listing
$app->get('/itunes/:app_id', function ($app_id)
{
global $dbContainer;
$db = $dbContainer['db'];
try {
$result = $db->query("SELECT ITUNES_UPDATED FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
$ITUNES_UPDATED = $result->fetchColumn();
if(!$ITUNES_UPDATED)
throw new Exception('Invalid APP ID');
$iTunesUpdateDate = new DateTime($ITUNES_UPDATED);
$sql = "SELECT * FROM ISSUES WHERE APP_ID = '$app_id' AND AVAILABILITY = 'published'";
$AtomXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"" . "?>";
$AtomXML.= "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:news=\"http://itunes.apple.com/2011/Newsstand\">";
$AtomXML.= "<updated>" . date_format($iTunesUpdateDate, DateTime::ATOM) . "</updated>";
foreach($db->query($sql) as $row) {
$iTunesIssueUpdateDate = new DateTime($row['ITUNES_UPDATED']);
$iTunesPublishedDate = new DateTime($row['DATE']);
$AtomXML.= "<entry>";
$AtomXML.= "<id>" . $row['NAME'] . "</id>";
$AtomXML.= "<updated>" . date_format($iTunesIssueUpdateDate, DateTime::ATOM) . "</updated>";
$AtomXML.= "<published>" . date_format($iTunesPublishedDate, DateTime::ATOM) . "</published>";
$AtomXML.= "<summary>" . $row['ITUNES_SUMMARY'] . "</summary>";
$AtomXML.= "<news:cover_art_icons>";
$AtomXML.= "<news:cover_art_icon size=\"SOURCE\" src=\"" . $row['ITUNES_COVERART_URL'] . "\"/>";
$AtomXML.= "</news:cover_art_icons>";
$AtomXML.= "</entry>";
}
$AtomXML.= "</feed>";
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
echo utf8_encode($AtomXML);
}
catch(Exception $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
});
// Confirm Purchase
// *Confirms the purchase by validating the Receipt_Data received for the in app purchase. Records the receipt data
// in the database and adds the available issues to the user's Purchased List
$app->post('/confirmpurchase/:app_id/:user_id', function ($app_id, $user_id) use($app)
{
global $dbContainer;
$db = $dbContainer['db'];
$body = $app->request()->getBody();
$receiptdata = $app->request()->post('receipt_data');
$type = $app->request()->post('type');
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Confirming purchase for APP ID: " . $app_id . " USER ID: " . $user_id . " TYPE: " . $type);}
try {
// Verify Receipt - with logic to fall back to Sandbox test if Production Receipt fails (error code 21007)
try{
$iTunesReceiptInfo = verifyReceipt($receiptdata, $app_id, $user_id);
}
catch(Exception $e) {
if($e->getCode() == "21007"){
logMessage(LogType::Info,"Confirming purchase for APP ID - Sandbox Receipt used in Production, retrying against Sandbox iTunes API: " . $app_id . " USER ID: " . $user_id . " TYPE: " . $type);
$iTunesReceiptInfo = verifyReceipt($receiptdata, $app_id, $user_id, TRUE);
}
}
$sql = "INSERT IGNORE INTO RECEIPTS (APP_ID, QUANTITY, PRODUCT_ID, TYPE, TRANSACTION_ID, USER_ID, PURCHASE_DATE,
ORIGINAL_TRANSACTION_ID, ORIGINAL_PURCHASE_DATE, APP_ITEM_ID, VERSION_EXTERNAL_IDENTIFIER, BID, BVRS, BASE64_RECEIPT)
VALUES (:app_id, :quantity, :product_id, :type, :transaction_id, :user_id, :purchase_date, :original_transaction_id,
:original_purchase_date, :app_item_id, :version_external_identifier, :bid, :bvrs, :base64_receipt)";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("app_id", $app_id);
$stmt->bindParam("quantity", $iTunesReceiptInfo->receipt->quantity);
$stmt->bindParam("product_id", $iTunesReceiptInfo->receipt->product_id);
$stmt->bindParam("type", $type);
$stmt->bindParam("transaction_id", $iTunesReceiptInfo->receipt->transaction_id);
$stmt->bindParam("user_id", $user_id);
$stmt->bindParam("purchase_date", $iTunesReceiptInfo->receipt->purchase_date);
$stmt->bindParam("original_transaction_id", $iTunesReceiptInfo->receipt->original_transaction_id);
$stmt->bindParam("original_purchase_date", $iTunesReceiptInfo->receipt->original_purchase_date);
$stmt->bindParam("app_item_id", $iTunesReceiptInfo->receipt->item_id);
$stmt->bindParam("version_external_identifier", $iTunesReceiptInfo->receipt->version_external_identifier);
$stmt->bindParam("bid", $iTunesReceiptInfo->receipt->bid);
$stmt->bindParam("bvrs", $iTunesReceiptInfo->receipt->bvrs);
$stmt->bindParam("base64_receipt", $receiptdata);
$stmt->execute();
// If successful, record the user's purchase
if($type == 'auto-renewable-subscription'){
markIssuesAsPurchased($iTunesReceiptInfo,$app_id,$user_id);
}else if($type == 'issue'){
markIssueAsPurchased($iTunesReceiptInfo->receipt->product_id, $app_id, $user_id);
}else if($type == 'free-subscription'){
// Nothing to do, as the server assumes free subscriptions don't need to be handled in this way
}
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
}
catch(Exception $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
});
// APNS Token
// *Stores the APNS Token in the database for the given App ID and User ID
$app->post('/apns/:app_id/:user_id', function ($app_id, $user_id) use($app)
{
global $dbContainer;
$db = $dbContainer['db'];
$apns_token = $app->request()->post('apns_token');
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Storing APNS Token for APP ID: " . $app_id . " USER ID: " . $user_id);}
$sql = "INSERT IGNORE INTO APNS_TOKENS (APP_ID, USER_ID, APNS_TOKEN)
VALUES (:app_id, :user_id, :apns_token)";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("app_id", $app_id);
$stmt->bindParam("user_id", $user_id);
$stmt->bindParam("apns_token", $apns_token);
$stmt->execute();
logAnalyticMetric(AnalyticType::ApiInteraction,1,NULL,$app_id,$user_id);
echo '{"success":{"message":"' . $apns_token . '"}}';
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
});
// ************************************************
// Utility Functions
// ************************************************
// Log Error Messages for tracking and debugging purposes, also displayed in the MagRocket Admin for issue debugging
function logMessage($logType, $logMessage)
{
global $dbContainer;
$db = $dbContainer['db'];
$sql = "INSERT INTO SYSTEM_LOG (TYPE, MESSAGE)
VALUES (:logtype, :logmessage)";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("logtype", $logType);
$stmt->bindParam("logmessage", $logMessage);
$stmt->execute();
}
catch(PDOException $e) {
// Error occurred, just ignore because if it failed in this logMessage method not much we can do, ignore
}
}
// Log Analytic Metrics for tracking purposes and basic display in the dashboard
function logAnalyticMetric($analytic_type, $analytic_value, $metadata, $app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
$sql = "INSERT INTO ANALYTICS (APP_ID, USER_ID, TYPE, VALUE, METADATA)
VALUES (:app_id, :user_id, :analytic_type, :analytic_value, :metadata)";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("app_id", $app_id);
$stmt->bindParam("user_id", $user_id);
$stmt->bindParam("analytic_type", $analytic_type);
$stmt->bindParam("analytic_value", $analytic_value);
$stmt->bindParam("metadata", $metadata);
$stmt->execute();
}
catch(PDOException $e) {
// Error occurred, just ignore because if it failed in this logAnalyticMetric method not much we can do, ignore
}
}
// Check if this publication is in development mode, useful for installation and non-production debugging
function isInDevelopmentMode($app_id)
{
global $developmentMode;
global $dbContainer;
$db = $dbContainer['db'];
if($developmentMode != ""){
return $developmentMode;
}
else{
$result = $db->query("SELECT DEVELOPMENT_MODE FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
return $result->fetchColumn();
}
}
// Retrieve the iTunes Production Level for Apple API calls
function getiTunesProductionLevel($app_id)
{
global $iTunesProductionLevel;
global $dbContainer;
$db = $dbContainer['db'];
if($iTunesProductionLevel != ""){
return $iTunesProductionLevel;
}
else{
$result = $db->query("SELECT ITUNES_PRODUCTION_LEVEL FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
return $result->fetchColumn();
}
}
// Retrieve the Subscription Behavior setting for marking issues as purchased
function getSubscriptionBehavior($app_id)
{
global $subscriptionBehavior;
global $dbContainer;
$db = $dbContainer['db'];
if($subscriptionBehavior != ""){
return $subscriptionBehavior;
}
else{
$result = $db->query("SELECT SUBSCRIPTION_BEHAVIOR FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
return $result->fetchColumn();
}
}
// Retrieve the iTunes Caching Duration for re-validating Apple Receipts
function getiTunesCachingDuration($app_id)
{
global $iTunesCachingDuration;
global $dbContainer;
$db = $dbContainer['db'];
if($iTunesCachingDuration != -1){
return $iTunesCachingDuration;
}
else{
$result = $db->query("SELECT ITUNES_REVALIDATION_DURATION FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
return $result->fetchColumn();
}
}
// Mark all available (paid) issues as purchased for a given user
function markIssuesAsPurchased($app_store_data, $app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Marking Issues as Purchased for APP ID: " . $app_id . " USER ID: " . $user_id);}
$receipt = $app_store_data->receipt;
$startDate = new DateTime($receipt->purchase_date_pst);
if ($app_store_data->status == 0) {
$endDate = new DateTime($app_store_data->latest_receipt_info->expires_date_formatted_pst);
}
else
if ($app_store_data->status == 21006) {
$endDate = new DateTime($app_store_data->latest_expired_receipt_info->expires_date_formatted_pst);
}
// Now update the Purchases table with all Issues that fall within the subscription start and expiration date
$startDateFormatted = $startDate->format('Y-m-d H:i:s');
$endDateFormatted = $endDate->format('Y-m-d H:i:s');
// Get First Day of the Month that the Receipt was generated for (Start)
$issuesStartDateFormatted = $startDate->format('Y-m-01 00:00:00');
// Get Last Day of the Month that the Receipt was generated for (Expiration)
$issuesEndDateFormatted = $endDate->format('Y-m-t 23:59:59');
// Update Subscriptions Table for user with current active subscription start and expiration date
updateSubscription($app_id, $user_id, $startDateFormatted, $endDateFormatted);
// If we are in Sandbox Mode, unlock all issues by default for testing purposes
if(getiTunesProductionLevel($app_id)=="sandbox"){
$result = $db->query("SELECT PRODUCT_ID FROM ISSUES
WHERE APP_ID = '$app_id'
AND PRICING = 'paid'");
}
else{
// If we are in Production - determine based on Subscription Behavior setting
if(getSubscriptionBehavior($app_id)=="all"){
$result = $db->query("SELECT PRODUCT_ID FROM ISSUES
WHERE APP_ID = '$app_id'
AND PRICING = 'paid'");
}else if(getSubscriptionBehavior($app_id)=="term"){
$result = $db->query("SELECT PRODUCT_ID FROM ISSUES
WHERE APP_ID = '$app_id'
AND `DATE` >= '$issuesStartDateFormatted'
AND `DATE` <= '$issuesEndDateFormatted'
AND PRICING = 'paid'
AND AVAILABILITY = 'published'");
}else{
//Default to 'term' if for some reason the above fails
$result = $db->query("SELECT PRODUCT_ID FROM ISSUES
WHERE APP_ID = '$app_id'
AND `DATE` >= '$issuesStartDateFormatted'
AND `DATE` <= '$issuesEndDateFormatted'
AND PRICING = 'paid'
AND AVAILABILITY = 'published'");
}
}
$product_ids_to_mark = $result->fetchAll(PDO::FETCH_COLUMN);
$insert = "INSERT IGNORE INTO PURCHASES (APP_ID, USER_ID, PRODUCT_ID)
VALUES ('$app_id', '$user_id', :product_id)";
$stmt = $db->prepare($insert);
foreach($product_ids_to_mark as $key => $product_id) {
$stmt->bindParam('product_id', $product_id);
$stmt->execute();
}
}
// Mark all available issues as purchased for a given user
function markIssueAsPurchased($product_id, $app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Marking single issue as purchased for APP ID: " . $app_id . " USER ID: " . $user_id . " PRODUCT ID: " . $product_id);}
$sql = "INSERT IGNORE INTO PURCHASES (APP_ID, USER_ID, PRODUCT_ID)
VALUES (:app_id, :user_id, :product_id)";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("app_id", $app_id);
$stmt->bindParam("user_id", $user_id);
$stmt->bindParam("product_id", $product_id);
$stmt->execute();
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
}
// Update the Subscription Record for a specific user with Effective Date and Expiration Date
function updateSubscription($app_id, $user_id, $effective_date, $expiration_date)
{
global $dbContainer;
$db = $dbContainer['db'];
$currentDate = new DateTime('now');
$lastValidated = $currentDate->format('Y-m-d H:i:s');
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Updating subscription effective dates for APP ID: " . $app_id . " USER ID: " . $user_id);}
$sql = "INSERT INTO SUBSCRIPTIONS (APP_ID, USER_ID, EFFECTIVE_DATE, EXPIRATION_DATE, LAST_VALIDATED)
VALUES (:app_id, :user_id, :effective_date, :expiration_date, :last_validated)
ON DUPLICATE KEY UPDATE EFFECTIVE_DATE=:effective_date, EXPIRATION_DATE=:expiration_date, LAST_VALIDATED=:last_validated";
try {
$stmt = $db->prepare($sql);
$stmt->bindParam("app_id", $app_id);
$stmt->bindParam("user_id", $user_id);
$stmt->bindParam("effective_date", $effective_date);
$stmt->bindParam("expiration_date", $expiration_date);
$stmt->bindParam("last_validated", $lastValidated);
$stmt->execute();
}
catch(PDOException $e) {
logMessage(LogType::Error, $e->getMessage());
echo '{"error":{"text":"' . $e->getMessage() . '"}}';
}
}
// Check if the user has a current active Subscription and determine expiration date
function checkSubscription($app_id, $user_id)
{
global $dbContainer;
$db = $dbContainer['db'];
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Checking subscription for APP ID: " . $app_id . " USER ID: " . $user_id);}
$result = $db->prepare("SELECT EFFECTIVE_DATE, EXPIRATION_DATE, LAST_VALIDATED FROM SUBSCRIPTIONS
WHERE APP_ID = '$app_id' AND USER_ID = '$user_id' LIMIT 0,1");
$result->execute();
$data = $result->fetch();
return $data;
}
// Validate InApp Purchase Receipt, by calling the Apple iTunes verifyReceipt method
// *Note that this seems to take between 2-4 seconds on average
function verifyReceipt($receipt, $app_id, $user_id, $sandbox_override = FALSE)
{
global $dbContainer;
$db = $dbContainer['db'];
if(isInDevelopmentMode($app_id)=="TRUE"){logMessage(LogType::Info,"Verifying receipt with Apple for APP ID: " . $app_id . " USER ID: " . $user_id);}
// Lookup shared secret from Publication table
$result = $db->query("SELECT ITUNES_SHARED_SECRET FROM PUBLICATION WHERE APP_ID = '$app_id' LIMIT 0, 1");
$sharedSecret = $result->fetchColumn();
if (getiTunesProductionLevel($app_id)=="sandbox" || $sandbox_override == TRUE) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
}
else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
}
// If no shared secret exists, don't send it to the verifyReceipt call, however it should exist!
if($sharedSecret){
$postData = json_encode(array(
'receipt-data' => $receipt,
'password' => $sharedSecret));
}else{
$postData = json_encode(array(
'receipt-data' => $receipt));
}
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
if ($errno != 0) {
throw new Exception($errmsg, $errno);
}
$data = json_decode($response);
if (!is_object($data)) {
throw new Exception('Invalid Response Data');
}
if (!isset($data->status) || ($data->status != 0 && $data->status != 21006)) {
$product_id = $data->receipt->product_id;
logMessage(LogType::Warning, "Invalid receipt for APP ID: " . $app_id . " USER ID: " . $user_id . " STATUS: " . $data->status);
throw new Exception('Invalid Receipt', $data->status);
}
return $data;
}
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();
// PHP doesn't support Enums so make a simple class for LogType
abstract class LogType
{
const Info = 'Info';
const Warning = 'Warning';
const Error = 'Error';
}
// PHP doesn't support Enums so make a simple class for AnalyticType
abstract class AnalyticType
{
const Download = 'download';
const ApiInteraction = 'api_interaction';
}
// Timer class for debugging and logging use
class timer
{
var $start;
var $pause_time;
/* start the timer */
function timer($start = 0)
{
if ($start) {
$this->start();
}
}
/* start the timer */
function start()
{
$this->start = $this->get_time();
$this->pause_time = 0;
}
/* pause the timer */
function pause()
{
$this->pause_time = $this->get_time();
}
/* unpause the timer */
function unpause()
{
$this->start+= ($this->get_time() - $this->pause_time);
$this->pause_time = 0;
}
/* get the current timer value */
function get($decimals = 8)
{
return round(($this->get_time() - $this->start) , $decimals);
}
/* format the time in seconds */
function get_time()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
}
?>