Subversion Repositories cheapmusic

Rev

Rev 137 | Rev 143 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
81 - 1
<?php
2
include_once ('php/constants.php');
3
 
4
error_reporting(E_ALL);
5
 
6
// Get itunes listings
7
function get_amazon($query, $searchCondition) {
8
    $vendors = Vendors::getInstance();
9
    $config = $vendors->getVendor(Vendors::AMAZON);
10
 
11
    $needMatches = empty($_SESSION["discogs"]);
12
    $arrMusic = [];
13
    $arrDigital = [];
82 - 14
    $arrBooks = [];
81 - 15
 
16
    // Music
17
    $arrMusic = get_amazonCategory($config, $query, "Music", $needMatches);
18
 
19
    // Digital Music
20
    if ($_SESSION["filterMediaType"]["Digital"]) {
99 - 21
        $arrDigital = get_amazonCategory($config, $query, "DigitalMusic");
81 - 22
    }
23
 
24
    if ($_SESSION["filterMediaType"]["Book"]) {
25
        // Books
26
        $arrBooks = get_amazonCategory($config, $query, "Books");
27
    }
28
 
29
    return (array_merge($arrMusic, $arrDigital, $arrBooks));
30
}
31
 
32
// Get Amazon listings
33
function get_amazonCategory($config, $query, $searchIndex, $needMatches = false) {
94 - 34
    $numResults = $config['numResults'];
81 - 35
 
36
    if ($needMatches) {
37
        $_SESSION["discogs"] = "";
38
    }
39
 
99 - 40
    $response = getSearchCache("amazon", $query, $searchIndex);
41
    if ($response === false) {
42
        $serviceName = "ProductAdvertisingAPI";
43
        $region = "us-east-1";
44
        $accessKey = $config["access_key_id"];
45
        $secretKey = $config["secret_key"];
46
        $associate_tag = $config['associate_tag'];
47
$payload="{"
48
        ." \"Keywords\": \"$query\","
49
        ." \"Resources\": ["
50
        ."  \"Images.Primary.Medium\","
51
        ."  \"Images.Primary.Large\","
52
        ."  \"ItemInfo.ByLineInfo\","
53
        ."  \"ItemInfo.ContentInfo\","
54
        ."  \"ItemInfo.Classifications\","
55
        ."  \"ItemInfo.ExternalIds\","
56
        ."  \"ItemInfo.ManufactureInfo\","
57
        ."  \"ItemInfo.ProductInfo\","
58
        ."  \"ItemInfo.TechnicalInfo\","
59
        ."  \"ItemInfo.Title\","
60
        ."  \"Offers.Listings.Condition\","
61
        ."  \"Offers.Listings.Condition.SubCondition\","
62
        ."  \"Offers.Listings.DeliveryInfo.IsAmazonFulfilled\","
63
        ."  \"Offers.Listings.DeliveryInfo.IsFreeShippingEligible\","
64
        ."  \"Offers.Listings.DeliveryInfo.IsPrimeEligible\","
65
        ."  \"Offers.Listings.DeliveryInfo.ShippingCharges\","
66
        ."  \"Offers.Listings.MerchantInfo\","
67
        ."  \"Offers.Listings.Price\""
68
        ." ],"
69
        ." \"SearchIndex\": \"$searchIndex\","
70
        ." \"Availability\": \"Available\","
71
        ." \"Condition\": \"Any\","
72
        ." \"OfferCount\": 3,"
73
        ." \"SortBy\": \"Price:LowToHigh\","
74
        ." \"PartnerTag\": \"$associate_tag\","
75
        ." \"PartnerType\": \"Associates\","
76
        ." \"Marketplace\": \"www.amazon.com\""
77
        ."}";
78
        $host = "webservices.amazon.com";
79
        $uriPath = "/paapi5/searchitems";
81 - 80
 
99 - 81
        $awsv4 = new AwsV4($accessKey, $secretKey);
82
        $awsv4->setRegionName($region);
83
        $awsv4->setServiceName($serviceName);
84
        $awsv4->setPath($uriPath);
85
        $awsv4->setPayload($payload);
86
        $awsv4->setRequestMethod("POST");
87
        $awsv4->addHeader('content-encoding', 'amz-1.0');
88
        $awsv4->addHeader('content-type', 'application/json; charset=utf-8');
89
        $awsv4->addHeader('host', $host);
90
        $awsv4->addHeader('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems');
91
        $headers = $awsv4->getHeaders();
92
        $headerArr = [];
93
        foreach ($headers as $key => $value) {
94
            $headerArr[] = $key . ': ' . $value;
95
        }
81 - 96
 
99 - 97
        $ch = curl_init();
98
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
99
        curl_setopt($ch, CURLOPT_URL, 'https://' . $host . $uriPath);
100
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
101
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
102
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
103
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
104
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
105
        curl_setopt($ch, CURLOPT_REFERER, 'https://' . $host . $uriPath);
106
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
107
        curl_setopt($ch, CURLOPT_POST, 1);
108
        $response = curl_exec($ch);
109
        curl_close($ch);
81 - 110
 
99 - 111
        saveSearchCache("amazon", $query, $searchIndex, $response);
81 - 112
    }
113
 
99 - 114
    $parsed_json = json_decode($response);
115
//echo "<br><pre>";print_r($parsed_json);echo "</pre>";
116
    $arr = [];
81 - 117
 
99 - 118
    if (isset($parsed_json->Errors)) {
119
        foreach ($parsed_json->Errors as $error) {
120
            if ($error->Code != "NoResults") {
121
                my_error_log("Amazon Search - " . $error->Message . " (" . $error->Code . ")");
122
            }
123
        }
124
        return [];
106 - 125
    } else if (!isset($parsed_json->SearchResult)) {
126
        my_error_log("Amazon Search - No Search Result.");
127
        return [];
99 - 128
    }
81 - 129
 
99 - 130
    if ($needMatches) {
131
        $cnt = 0;
127 - 132
 
133
        $xh = new Html;
134
        $xh->init($_SESSION["htmlIndent"]);
135
        startMatches($xh);
99 - 136
    }
81 - 137
 
99 - 138
    $listings = 0;
139
    // If the response was loaded, parse it and store in array
140
    foreach ($parsed_json->SearchResult->Items as $current) {
141
        if (isset($current->ItemInfo->ExternalIds->UPCs)) {
142
            $barcode = (string)$current->ItemInfo->ExternalIds->UPCs->DisplayValues[0];
143
        }
144
        else if (isset($current->ItemInfo->ExternalIds->EANs)) {
145
            $barcode = (string)$current->ItemInfo->ExternalIds->EANs->DisplayValues[0];
146
        }
147
        else if (isset($current->ItemInfo->ExternalIds->ISBNs)) {
148
            $barcode = (string)$current->ItemInfo->ExternalIds->ISBNs->DisplayValues[0];
149
        }
150
        else if (isset($current->ItemInfo->ExternalIds->EISBNs)) {
151
            $barcode = (string)$current->ItemInfo->ExternalIds->EISBNs->DisplayValues[0];
152
        }
153
        else {
154
            $barcode = "";
155
        }
156
        $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
81 - 157
 
102 - 158
        $pic = !empty($current->Images->Primary->Medium->URL) ? (string)$current->Images->Primary->Medium->URL : "images/no-image-available.jpg";
100 - 159
        $pic = str_replace('http://', 'https://', $pic);
99 - 160
        if (empty($pic)) {
161
            continue;
162
        }
81 - 163
 
99 - 164
        if (strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "Audio CD") !== false) {
165
            $mediaType = "CD";
166
        }
167
        else if (strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "MP3 Music") !== false) {
168
            $mediaType = "Digital";
169
        }
170
        else if (strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "Vinyl") !== false) {
171
            $mediaType = "Record";
172
        }
173
        else if (strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "Paperback") !== false || strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "Sheet") !== false || strpos($current->ItemInfo->Classifications->Binding->DisplayValue, "Hardcover") !== false) {
174
            $mediaType = "Book";
175
        }
176
        else {
177
            continue;
178
        }
81 - 179
 
180
        if ($needMatches) {
99 - 181
            // bugbug: check maxMasterCnt?
130 - 182
            addMatch($xh, $current, ++$cnt, $mediaType);
81 - 183
        }
184
 
99 - 185
        $title = (string)$current->ItemInfo->Title->DisplayValue;
186
        $artists = getArtists($current);
187
        $contributers = getContributers($current);
188
        if (!empty($artists)) {
189
            $title .= " by " . join(", ", $artists);
190
        }
191
        else if (!empty($contributers)) {
192
            $title .= " by " . join(", ", $contributers);
193
        }
81 - 194
 
99 - 195
        $url = str_replace('http://', 'https://', (string)$current->DetailPageURL);
81 - 196
 
99 - 197
        if (empty($url)) {
198
            continue;
199
        }
81 - 200
 
99 - 201
        $bestOffer = false;
81 - 202
 
99 - 203
        $url = str_replace('http://', 'https://', (string)$current->DetailPageURL);
204
        $url .= "&condition=";
81 - 205
 
99 - 206
        if (!empty($current->Offers->Listings)) {
207
            foreach ($current->Offers->Listings as $offer) {
208
                $country = 'US';
209
                if (isset($offer->MerchantInfo->DefaultShippingCountry)) {
210
                    $country = $offer->MerchantInfo->DefaultShippingCountry;
211
                }
212
                $merchantName = "Amazon";
213
                if (strpos($offer->MerchantInfo->Name, "Amazon") === false) {
214
                    $merchantName .= " Marketplace";
215
                }
81 - 216
 
99 - 217
                $condition = (string)$offer->Condition->Value;
218
                $url .= $condition;
219
                $detailCondition = $condition;
220
                if ($condition == "Collectible") {
221
                    $condition = 'Used';
222
                    $detailCondition = "Collectible";
223
                }
224
                if ($condition == "Refurbished") {
225
                    $condition = 'Used';
226
                    $detailCondition = "Refurbished";
227
                }
228
                $currency = (string)$offer->Price->Currency;
229
                $price = number_format(floatval($offer->Price->Amount) , 2, '.', '');
81 - 230
 
99 - 231
                if ($price <= "0.00") {
232
                    continue;
233
                }
81 - 234
 
99 - 235
                $timeLeft = 0;
236
                $listingType = 'Fixed';
237
                $location = 'US';
238
                $zip = '';
239
                $feedbackScore = - 1;
240
                $feedbackPercent = - 1;
241
                $sellerName = "";
242
                $handlingTime = 0;
243
                if ($offer->DeliveryInfo->IsPrimeEligible === true) {
244
                    $sellerName = "Prime";
81 - 245
                }
99 - 246
                if ($mediaType == "Digital") {
247
                    $shippingCost = 0.00;
248
                    $shippingEstimated = false;
249
                    $shippingCurrency = 'USD';
250
                }
251
                else {
252
                    $shippingCost = 3.99; // bugbug
253
                    $shippingEstimated = true; // bugbug
254
                    $shippingCurrency = 'USD';
255
                }
256
                $freeShippingCap = !empty($offer->DeliveryInfo->IsFreeShippingEligible) ? 25 : 0;
81 - 257
 
99 - 258
                if (++$listings > $numResults) {
259
                    continue;
260
                }
81 - 261
 
99 - 262
                $arr[] = array(
263
                    "Merchant" => $merchantName,
264
                    "Condition" => $condition,
265
                    "Title" => $title,
266
                    "Barcode" => $barcode,
267
                    "BarcodeType" => $barcodeType,
268
                    "Image" => $pic,
269
                    "URL" => $url,
270
                    "MediaType" => $mediaType,
271
                    "DetailCondition" => $detailCondition,
272
                    "Country" => $country,
273
                    "BestOffer" => $bestOffer,
274
                    "TimeLeft" => $timeLeft,
275
                    "Price" => $price,
276
                    "Currency" => $currency,
277
                    "ListingType" => $listingType,
278
                    "Location" => $location,
279
                    "Zip" => $zip,
280
                    "FeedbackScore" => $feedbackScore,
281
                    "FeedbackPercent" => $feedbackPercent,
282
                    "SellerName" => $sellerName,
283
                    "HandlingTime" => $handlingTime,
284
                    "ShippingCost" => $shippingCost,
285
                    "ShippingEstimated" => $shippingEstimated,
286
                    "ShippingCurrency" => $shippingCurrency,
287
                    "FreeShippingCap" => $freeShippingCap,
288
                    "Show" => true
289
                );
81 - 290
            }
291
        }
99 - 292
    }
81 - 293
 
99 - 294
    if ($needMatches) {
295
        if ($cnt = 0) {
296
            $_SESSION["discogs"] = "";
81 - 297
        }
99 - 298
        else {
130 - 299
            endMatches($xh);
127 - 300
 
301
            $_SESSION["discogs"] = $xh->flush();
302
            error_log(print_r($_SESSION["discogs"], 1));
99 - 303
        }
81 - 304
    }
99 - 305
 
81 - 306
    // If the response does not indicate 'Success,' log the error(s)
99 - 307
    /******************
81 - 308
    else {
96 - 309
        my_error_log($url);
99 - 310
        if (!empty($parsed_json->OperationRequest->Errors)) {
311
            foreach($parsed_json->OperationRequest->Errors->Error as $error){
96 - 312
                my_error_log($error->Message . " (" . $error->Code . ")");
81 - 313
            }
99 - 314
        } else if (!empty($parsed_json->Errors)) {
315
            foreach($parsed_json->OperationRequest->Errors->Error as $error){
96 - 316
                my_error_log($error->Message . " (" . $error->Code . ")");
81 - 317
            }
99 - 318
        } else if (!empty($parsed_json->Error)) {
319
            my_error_log($parsed_json->Error->Message . " (" . $parsed_json->Error->Code . ")");
81 - 320
        } else {
96 - 321
            my_error_log(print_r($result, 1));
81 - 322
        }
323
    }
99 - 324
    ******************/
81 - 325
 
326
    return $arr;
327
}
328
 
127 - 329
function startMatches(&$xh) {
330
    $xh->add_attribute("class", "container-fluid bg-light");
141 - 331
    $xh->add_attribute("id", "discogsTable");
127 - 332
    $xh->tag('div');
333
    $xh->add_attribute("class", "text-center py-2");
334
    $xh->tag('h2', "Matching Albums");
81 - 335
 
137 - 336
    $xh->add_attribute("id", "discogsDeckForm");
337
    $xh->add_attribute("method", "post");
338
    $xh->add_attribute("action", "/index.php");
127 - 339
    $xh->tag('form');
340
    $xh->insert_code(inputSessionTab());
341
    $xh->insert_code(inputNonce());
342
    $xh->add_attribute("id", "discogsTitle");
343
    $xh->add_attribute("type", "hidden");
344
    $xh->add_attribute("name", "discogsTitle");
345
    $xh->add_attribute("value", "");
346
    $xh->single_tag('input');
347
    $xh->add_attribute("id", "discogsArtist");
348
    $xh->add_attribute("type", "hidden");
349
    $xh->add_attribute("name", "discogsArtist");
350
    $xh->add_attribute("value", "");
351
    $xh->single_tag('input');
352
    $xh->add_attribute("id", "discogsBarcode");
353
    $xh->add_attribute("type", "hidden");
354
    $xh->add_attribute("name", "discogsBarcode");
355
    $xh->add_attribute("value", "");
356
    $xh->single_tag('input');
357
    $xh->add_attribute("id", "discogsDeck");
358
    $xh->add_attribute("class", "card-deck");
359
    $xh->tag('div');
81 - 360
}
361
 
130 - 362
function endMatches(&$xh) {
363
    $xh->insert_code(discogsEvents());
81 - 364
 
127 - 365
    $xh->close(); // div
366
    $xh->close(); // form>";
367
    $xh->close(); // div>";
81 - 368
}
369
 
130 - 370
function addMatch(&$xh, $item, $cnt, $mediaType) {
127 - 371
    $xhmod = new Html;
372
    $xhmod->init($_SESSION["htmlIndent"]);
373
 
81 - 374
    $artists = getArtists($item);
99 - 375
    $contributers = getContributers($item);
81 - 376
 
99 - 377
    $label = "";
378
    if (!empty($item->ItemInfo->ByLineInfo->Manufacturer)) {
100 - 379
        $label = $item->ItemInfo->ByLineInfo->Manufacturer->DisplayValue;
81 - 380
    }
381
 
382
    $languages = [];
99 - 383
    if (!empty($item->ItemInfo->ContentInfo->Languages)) {
384
        foreach ($item->ItemInfo->ContentInfo->Languages->DisplayValues as $language) {
81 - 385
            if ((string)$language->Type != "Unknown") {
99 - 386
                $languages[] = $language->DisplayValue . " (" . $language->Type . ")";
81 - 387
            }
388
        }
389
    }
390
 
101 - 391
    $genres = [];
392
    if (!empty($item->ItemInfo->Genre)) {
393
        foreach($item->ItemInfo->Genre as $genre) {
394
            $genres[] = join(" ", array_map('ucfirst', explode('-', $genre)));
395
        }
396
    }
397
 
398
    $runningTime = "";
399
    if (!empty($item->ItemInfo->RunningTime)) {
400
        if ($mediaType != 'Digital') {
401
            $runningTime = (string)$item->ItemInfo->RunningTime . " minutes";
402
        } else {
403
            $runningTime = gmdate("H:i:s", (int)$item->ItemInfo->RunningTime);
404
        }
405
    }
406
 
127 - 407
    $xh->add_attribute("class", "card mx-auto discogs-card");
408
    $xh->tag('div');
409
    $xh->add_attribute("class", "card-header bg-secondary d-flex");
410
    $xh->tag('div');
81 - 411
    $searchArtists = "";
105 - 412
    $wlArtists = "";
99 - 413
    if (!empty($artists)) {
414
        if (count($artists) < 5) {
415
            $wlArtists = join(", ", $artists);
416
            if (!empty($artists) && $artists[0] != 'Various') {
417
                $searchArtists = join(" ", $artists);
418
            }
419
        } else {
420
            $wlArtists = "Various Artists";
81 - 421
        }
422
    }
99 - 423
    else if (!empty($contributers)) {
424
        if (count($contributers) < 5) {
425
            $wlArtists = join(", ", $contributers);
426
            if (!empty($contributers) && $contributers[0] != 'Various') {
427
                $searchArtists = join(" ", $contributers);
428
            }
429
        } else {
430
            $wlArtists = "Various Artists";
431
        }
81 - 432
    }
127 - 433
 
434
    $str = htmlentities((string)$item->ItemInfo->Title->DisplayValue);
124 - 435
    if (!empty($wlArtists)) {
125 - 436
        $str .= " by " . htmlentities($wlArtists);
124 - 437
    }
127 - 438
    $xh->add_attribute("class", "card-title font-weight-bold small flex-grow-1");
439
    $xh->tag('p', $str);
440
    $xh->close(); // div
81 - 441
 
99 - 442
    $thumbnail = !empty($item->Images->Primary->Medium->URL) ? (string)$item->Images->Primary->Medium->URL : "images/no-image-available.jpg";
81 - 443
 
127 - 444
    $xh->add_attribute("class", "card-body d-flex justify-content-center align-items-center p-1 m-0");
445
    $xh->tag('div');
446
    $xh->add_attribute("class", "btn p-0 m-0 lazyload");
447
    $xh->add_attribute("src", PIXEL);
448
    $xh->add_attribute("data-src",  $thumbnail);
449
    $xh->add_attribute("data-toggle", "modal");
450
    $xh->add_attribute("data-target", "#masterModal" . $cnt);
451
    $xh->add_attribute("title", "Album Information");
452
    $xh->add_attribute("data-toggle2", "tooltip");
453
    $xh->add_attribute("alt", "Cover Thumbnail");
454
    $xh->single_tag('img');
455
    $xh->close(); // div
141 - 456
    $xh->add_attribute("class", "card-footer form-row btn-group-sm bg-secondary p-0 m-0");
127 - 457
    $xh->tag('div');
141 - 458
    $xh->add_attribute("class", "col-3 btn-group-sm p-0 m-0");
127 - 459
    $xh->tag('div');
81 - 460
 
461
    $barcode = "";
462
    $tmpBarcode = "";
99 - 463
    if (isset($item->ItemInfo->ExternalIds->UPCs)) {
464
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->UPCs->DisplayValues[0];
81 - 465
    }
99 - 466
    else if (isset($item->ItemInfo->ExternalIds->EANs)) {
467
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->EANs->DisplayValues[0];
468
    }
469
    else if (isset($item->ItemInfo->ExternalIds->ISBNs)) {
470
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->ISBNs->DisplayValues[0];
471
    }
472
    else if (isset($item->ItemInfo->ExternalIds->EISBNs)) {
473
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->EISBNs->DisplayValues[0];
474
    }
81 - 475
    $barcodeType = clsLibGTIN::GTINCheck($tmpBarcode, false, 1);
476
    if (!empty($barcodeType)) {
477
        $barcode = $tmpBarcode;
478
    }
479
 
141 - 480
    if (isLoggedIn()) {
481
        $checkWlFlag = checkWishlist('asin', $item->ASIN);
81 - 482
        $wlArr = array(
483
            'asin' => (string)$item->ASIN,
99 - 484
            'title' => (string)$item->ItemInfo->Title->DisplayValue,
81 - 485
            'artist' => $wlArtists,
486
            'barcode' => $barcode,
487
            'thumbnail' => $thumbnail,
488
            'url' => (string)$item->DetailPageURL
489
        );
490
        $wl = base64_encode(json_encode($wlArr));
141 - 491
 
492
        if (!$checkWlFlag) {
493
            $xh->add_attribute("id", "wl" . $cnt . "Btn");
494
            $xh->add_attribute("data-user", $_SESSION['sessData']['userID']);
495
            $xh->add_attribute("data-cnt", $cnt);
496
            $xh->add_attribute("data-wl", $wl);
497
        }
498
 
127 - 499
        $xh->add_attribute("type", "button");
500
        $xh->add_attribute("class", "btn btn-info btn-sm");
141 - 501
        $xh->add_attribute("title", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
502
        $xh->add_attribute("aria-label", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
127 - 503
        $xh->add_attribute("data-toggle", "tooltip");
504
        $xh->tag('button');
505
        $xh->add_attribute("class", "material-icons");
141 - 506
        $xh->tag('i', $checkWlFlag ? "library_add_check" : "library_add");
127 - 507
        $xh->close(); // button
81 - 508
    }
141 - 509
    $xh->close(); // div
81 - 510
 
141 - 511
    $xh->add_attribute("class", "col-3 btn-group-sm p-0 m-0");
512
    $xh->tag('div');
513
    $xh->close(); // div
514
 
125 - 515
    $searchTitle = 'Searching for:<br><br><strong>' . htmlentities((string)$item->ItemInfo->Title->DisplayValue) . ' by ' . htmlentities($wlArtists) . '</strong>';
81 - 516
    if (!empty($barcode)) {
517
        $searchTitle .= " (" . displayBarcode($barcode) . ")";
518
    }
519
 
141 - 520
    $xh->add_attribute("class", "col-6 btn-group p-0 m-0");
521
    $xh->tag('div');
127 - 522
    $xh->add_attribute("id", "discogsSearch" . $cnt . "Btn");
523
    $xh->add_attribute("type", "submit");
134 - 524
    $xh->add_attribute("name", "submitBtn");
127 - 525
    $xh->add_attribute("value", "discogsSearch");
526
    $xh->add_attribute("class", "btn btn-success btn-sm");
527
    $xh->add_attribute("title", "Search for Store Offers");
528
    $xh->add_attribute("aria-label", "Search for Store Offers");
529
    $xh->add_attribute("data-toggle", "tooltip");
130 - 530
    $xh->add_attribute("data-title", htmlentities((string)$item->ItemInfo->Title->DisplayValue));
531
    $xh->add_attribute("data-artist", htmlentities($searchArtists));
532
    $xh->add_attribute("data-barcode", $barcode);
533
    $xh->add_attribute("data-search-title", $searchTitle);
127 - 534
    $xh->tag('button');
141 - 535
    $xh->add_attribute("class", "material-icons material-text");
127 - 536
    $xh->tag('i', "search");
141 - 537
    $xh->tag('span', "Offer");
127 - 538
    $xh->close(); // button
539
    $xh->close(); // div
120 - 540
 
127 - 541
    $xh->add_attribute("id", "wishlistAdd" . $cnt);
542
    $xh->tag('span', "");
543
    $xh->close(); // div
81 - 544
 
127 - 545
    $xhmod->add_attribute("id", "masterModal" . $cnt);
546
    $xhmod->add_attribute("class", "modal");
547
    $xhmod->tag('div');
548
    $xhmod->add_attribute("class", "modal-dialog");
549
    $xhmod->tag('div');
550
    $xhmod->add_attribute("class", "modal-content");
551
    $xhmod->tag('div');
552
    $xhmod->add_attribute("class", "modal-header bg-secondary");
553
    $xhmod->tag('div');
554
    $xhmod->add_attribute("class", "modal-title mx-auto display-6");
555
    $xhmod->tag('p', htmlentities((string)$item->ItemInfo->Title->DisplayValue) . " by " . htmlentities($wlArtists));
556
    $xhmod->add_attribute("type", "button");
557
    $xhmod->add_attribute("class", "close");
558
    $xhmod->add_attribute("data-dismiss", "modal");
559
    $xhmod->tag('button');
560
    $xhmod->add_attribute("class", "material-icons btn-dismiss");
561
    $xhmod->tag('i', "cancel_presentation");
562
    $xhmod->close(); // button
563
    $xhmod->close(); // div
81 - 564
 
127 - 565
    $xhmod->add_attribute("class", "modal-body mx-auto");
566
    $xhmod->tag('div');
81 - 567
 
99 - 568
    if (!empty($item->Images->Primary->Large->URL)) {
141 - 569
        $xhmod->add_attribute("class", "img-fluid d-flex mx-auto mb-4 lazyload");
127 - 570
        $xhmod->add_attribute("src", PIXEL);
571
        $xhmod->add_attribute("data-src", (string)$item->Images->Primary->Large->URL);
572
        $xhmod->add_attribute("alt", "Cover Image");
573
        $xhmod->single_tag('img');
81 - 574
    }
575
 
127 - 576
    $xhmod->add_attribute("class", "table-borderless table-condensed small mx-auto");
577
    $xhmod->tag('table');
81 - 578
 
127 - 579
    $xhmod->tag('tr');
580
    $xhmod->add_attribute("class", "px-1");
581
    $xhmod->tag('td', "Title:");
582
    $xhmod->add_attribute("class", "px-1");
583
    $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Title->DisplayValue));
584
    $xhmod->close(); // tr
585
 
586
    $xhmod->tag('tr');
587
    $xhmod->add_attribute("class", "px-1");
588
    $xhmod->tag('td', "Artist:");
589
    $xhmod->add_attribute("class", "px-1");
590
    $xhmod->tag('td', htmlentities(join(", ", $artists)));
591
    $xhmod->close(); // tr
592
 
99 - 593
    if (!empty($contributers)) {
127 - 594
        $xhmod->tag('tr');
595
        $xhmod->add_attribute("class", "px-1");
596
        $xhmod->tag('td', "Creator:");
597
        $xhmod->add_attribute("class", "px-1");
598
        $xhmod->tag('td', htmlentities(join(", ", $contributers)));
599
        $xhmod->close(); // tr
81 - 600
    }
601
 
602
    if (!empty($actors)) {
127 - 603
        $xhmod->tag('tr');
604
        $xhmod->add_attribute("class", "px-1");
605
        $xhmod->tag('td', "Actor:");
606
        $xhmod->add_attribute("class", "px-1");
607
        $xhmod->tag('td', htmlentities(join(", ", $actors)));
608
        $xhmod->close(); // tr
81 - 609
    }
610
 
611
    if (!empty($directors)) {
127 - 612
        $xhmod->tag('tr');
613
        $xhmod->add_attribute("class", "px-1");
614
        $xhmod->tag('td', "Director:");
615
        $xhmod->add_attribute("class", "px-1");
616
        $xhmod->tag('td', htmlentities(join(", ", $directors)));
617
        $xhmod->close(); // tr
81 - 618
    }
619
 
620
    if (!empty($authors)) {
127 - 621
        $xhmod->tag('tr');
622
        $xhmod->add_attribute("class", "px-1");
623
        $xhmod->tag('td', "Author:");
624
        $xhmod->add_attribute("class", "px-1");
625
        $xhmod->tag('td', htmlentities(join(", ", $authors)));
626
        $xhmod->close(); // tr
81 - 627
    }
628
 
99 - 629
    if (!empty($item->ItemInfo->Classifications->Binding)) {
127 - 630
        $xhmod->tag('tr');
631
        $xhmod->add_attribute("class", "px-1");
632
        $xhmod->tag('td', "Type:");
633
        $xhmod->add_attribute("class", "px-1");
634
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Classifications->Binding->DisplayValue));
635
        $xhmod->close(); // tr
81 - 636
    }
637
 
101 - 638
    if (!empty($item->ItemInfo->MediaType)) { //
127 - 639
        $xhmod->tag('tr');
640
        $xhmod->add_attribute("class", "px-1");
641
        $xhmod->tag('td', "Media Type:");
642
        $xhmod->add_attribute("class", "px-1");
643
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->MediaType));
644
        $xhmod->close(); // tr
81 - 645
    }
646
 
647
    if (!empty($barcode)) {
127 - 648
        $xhmod->tag('tr');
649
        $xhmod->add_attribute("class", "px-1");
650
        $xhmod->tag('td', "Barcode:");
651
        $xhmod->add_attribute("class", "px-1");
652
        $xhmod->tag('td', displayBarcode($barcode));
653
        $xhmod->close(); // tr
81 - 654
    }
655
 
99 - 656
    if (!empty($label)) {
127 - 657
        $xhmod->tag('tr');
658
        $xhmod->add_attribute("class", "px-1");
659
        $xhmod->tag('td', "Label:");
660
        $xhmod->add_attribute("class", "px-1");
661
        $xhmod->tag('td', htmlentities($label));
662
        $xhmod->close(); // tr
81 - 663
    }
664
 
665
    if (!empty($languages)) {
127 - 666
        $xhmod->tag('tr');
667
        $xhmod->add_attribute("class", "px-1");
668
        $xhmod->tag('td', "Languages:");
669
        $xhmod->add_attribute("class", "px-1");
670
        $xhmod->tag('td', htmlentities(join(", ", $languages)));
671
        $xhmod->close(); // tr
81 - 672
    }
673
 
99 - 674
    if (!empty($item->ItemInfo->ContentInfo->PublicationDate)) {
127 - 675
        $xhmod->tag('tr');
676
        $xhmod->add_attribute("class", "px-1");
677
        $xhmod->tag('td', "Publication Date:");
678
        $xhmod->add_attribute("class", "px-1");
679
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->PublicationDate->DisplayValue));
680
        $xhmod->close(); // tr
81 - 681
    }
682
 
99 - 683
    if (!empty($item->ItemInfo->ContentInfo->ReleaseDate)) {
127 - 684
        $xhmod->tag('tr');
685
        $xhmod->add_attribute("class", "px-1");
686
        $xhmod->tag('td', "Release Date:");
687
        $xhmod->add_attribute("class", "px-1");
688
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->ReleaseDate->DisplayValue));
689
        $xhmod->close(); // tr
81 - 690
    }
691
 
101 - 692
    if (!empty($genres)) {
127 - 693
        $xhmod->tag('tr');
694
        $xhmod->add_attribute("class", "px-1");
695
        $xhmod->tag('td', "Genre:");
696
        $xhmod->add_attribute("class", "px-1");
697
        $xhmod->tag('td', htmlentities(join(", ", $genres)));
698
        $xhmod->close(); // tr
101 - 699
    }
700
 
99 - 701
    if (!empty($item->ItemInfo->ContentInfo->UnitCount->DisplayValue)) {
127 - 702
        $xhmod->tag('tr');
703
        $xhmod->add_attribute("class", "px-1");
704
        $xhmod->tag('td', "Number of Discs:");
705
        $xhmod->add_attribute("class", "px-1");
706
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->UnitCount->DisplayValue));
707
        $xhmod->close(); // tr
81 - 708
    }
709
 
99 - 710
    if (!empty($item->ItemInfo->ContentInfo->PagesCount->DisplayValue)) {
127 - 711
        $xhmod->tag('tr');
712
        $xhmod->add_attribute("class", "px-1");
713
        $xhmod->tag('td', "Number of Pages:");
714
        $xhmod->add_attribute("class", "px-1");
715
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->PagesCount->DisplayValue));
716
        $xhmod->close(); // tr
81 - 717
    }
718
 
101 - 719
    if (!empty($item->ItemInfo->RunningTime)) {
127 - 720
        $xhmod->tag('tr');
721
        $xhmod->add_attribute("class", "px-1");
722
        $xhmod->tag('td', "Running Time:");
723
        $xhmod->add_attribute("class", "px-1");
724
        $xhmod->tag('td', htmlentities($runningTime));
725
        $xhmod->close(); // tr
101 - 726
    }
727
 
728
    if (!empty($item->ItemInfo->Edition)) {
127 - 729
        $xhmod->tag('tr');
730
        $xhmod->add_attribute("class", "px-1");
731
        $xhmod->tag('td', "Edition:");
732
        $xhmod->add_attribute("class", "px-1");
733
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Edition));
734
        $xhmod->close(); // tr
101 - 735
    }
736
 
737
    if (!empty($item->Tracks)) {
127 - 738
        $xhmod->tag('tr');
739
        $xhmod->add_attribute("colspan", "2");
740
        $xhmod->add_attribute("class", "px-1");
741
        $xhmod->tag('td', "Tracks:");
742
        $xhmod->close(); // tr
743
 
744
        $xhmod->tag('tr');
745
        $xhmod->add_attribute("colspan", "2");
746
        $xhmod->tag('td');
747
        $xhmod->add_attribute("class", "pl-3 pt-0 small list-unstyled");
748
        $xhmod->tag('ul');
101 - 749
        foreach ($item->Tracks->Disc as $disc) {
750
            if ((int)$item->ItemInfo->ContentInfo->UnitCount->DisplayValue > 1 && !empty($disc->attributes())) {
127 - 751
                $xhmod->add_attribute("class", "font-weight-bold");
752
                $xhmod->tag('li', "Disc " . $disc->attributes() . ":");
101 - 753
            }
754
 
755
            foreach($disc->Track as $track) {
127 - 756
                $xhmod->tag('li', (!empty($track->attributes()) ? $track->attributes() . ") " : "") . htmlentities($track));
101 - 757
            }
758
        }
127 - 759
        $xhmod->close(); // ul
760
        $xhmod->close(); // td
761
        $xhmod->close(); // tr
101 - 762
    }
763
 
127 - 764
    $xhmod->close(); // table
765
    $xhmod->close(); // div
766
    $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
767
    $xhmod->tag('div');
768
    $xhmod->add_attribute("class", "float-right");
769
    $xhmod->tag('span');
770
    $xhmod->add_attribute("type", "button");
771
    $xhmod->add_attribute("class", "btn btn-danger");
772
    $xhmod->add_attribute("data-dismiss", "modal");
773
    $xhmod->tag('button', "Close");
774
    $xhmod->close(); // span
775
    $xhmod->close(); // div
776
    $xhmod->close(); // div
777
    $xhmod->close(); // div
778
    $xhmod->close(); // div
779
    $html = $xhmod->flush();
780
    //error_log(print_r($html, 1));
81 - 781
 
127 - 782
    $xh->insert_code($html);
783
    $xh->close(); // div
784
 
785
    return;
81 - 786
}
787
 
788
function getArtists($item) {
789
    $artists = [];
790
 
101 - 791
    if (!empty($item->ItemInfo->Artist)){
792
        $artists[] = $item->ItemInfo->Artist;
793
    } else if (!empty($item->ItemInfo->ByLineInfo->Contributors)) {
99 - 794
        foreach ($item->ItemInfo->ByLineInfo->Contributors as $artist) {
795
            if ((string)$artist->Role == "Artist") {
796
                $artists[] = $artist->Name;
797
            }
81 - 798
        }
799
    }
800
 
801
    return $artists;
802
}
803
 
99 - 804
function getContributers($item) {
805
    $contributers = [];
806
 
807
    if (!empty($item->ItemInfo->ByLineInfo->Contributors)) {
808
        foreach ($item->ItemInfo->ByLineInfo->Contributors as $creator) {
809
            if ((string)$creator->Role == "Primary Contributor") {
810
                $contributers[] = $creator->Name;
81 - 811
            }
99 - 812
            else {
813
                $contributers[] = $creator->Name . " (" . $creator->Role . ")";
814
            }
81 - 815
        }
816
    }
817
 
99 - 818
    return $contributers;
81 - 819
}
99 - 820
 
821
class AwsV4 {
822
 
823
    private $accessKey = null;
824
    private $secretKey = null;
825
    private $path = null;
826
    private $regionName = null;
827
    private $serviceName = null;
828
    private $httpMethodName = null;
829
    private $queryParametes = array();
830
    private $awsHeaders = array();
831
    private $payload = "";
832
 
833
    private $HMACAlgorithm = "AWS4-HMAC-SHA256";
834
    private $aws4Request = "aws4_request";
835
    private $strSignedHeader = null;
836
    private $xAmzDate = null;
837
    private $currentDate = null;
838
 
839
    public function __construct($accessKey, $secretKey) {
840
        $this->accessKey = $accessKey;
841
        $this->secretKey = $secretKey;
842
        $this->xAmzDate = $this->getTimeStamp();
843
        $this->currentDate = $this->getDate();
844
    }
845
 
846
    function setPath($path) {
847
        $this->path = $path;
848
    }
849
 
850
    function setServiceName($serviceName) {
851
        $this->serviceName = $serviceName;
852
    }
853
 
854
    function setRegionName($regionName) {
855
        $this->regionName = $regionName;
856
    }
857
 
858
    function setPayload($payload) {
859
        $this->payload = $payload;
860
    }
861
 
862
    function setRequestMethod($method) {
863
        $this->httpMethodName = $method;
864
    }
865
 
866
    function addHeader($headerName, $headerValue) {
867
        $this->awsHeaders[$headerName] = $headerValue;
868
    }
869
 
870
    private function prepareCanonicalRequest() {
871
        $canonicalURL = "";
872
        $canonicalURL .= $this->httpMethodName . "\n";
873
        $canonicalURL .= $this->path . "\n" . "\n";
874
        $signedHeaders = '';
875
        foreach ($this->awsHeaders as $key => $value) {
876
            $signedHeaders .= $key . ";";
877
            $canonicalURL .= $key . ":" . $value . "\n";
878
        }
879
        $canonicalURL .= "\n";
880
        $this->strSignedHeader = substr($signedHeaders, 0, -1);
881
        $canonicalURL .= $this->strSignedHeader . "\n";
882
        $canonicalURL .= $this->generateHex($this->payload);
883
        return $canonicalURL;
884
    }
885
 
886
    private function prepareStringToSign($canonicalURL) {
887
        $stringToSign = '';
888
        $stringToSign .= $this->HMACAlgorithm . "\n";
889
        $stringToSign .= $this->xAmzDate . "\n";
890
        $stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
891
        $stringToSign .= $this->generateHex($canonicalURL);
892
        return $stringToSign;
893
    }
894
 
895
    private function calculateSignature($stringToSign) {
896
        $signatureKey = $this->getSignatureKey($this->secretKey, $this->currentDate, $this->regionName, $this->serviceName);
897
        $signature = hash_hmac("sha256", $stringToSign, $signatureKey, true);
898
        $strHexSignature = strtolower(bin2hex($signature));
899
        return $strHexSignature;
900
    }
901
 
902
    public function getHeaders() {
903
        $this->awsHeaders['x-amz-date'] = $this->xAmzDate;
904
        ksort($this->awsHeaders);
905
 
906
        // Step 1: CREATE A CANONICAL REQUEST
907
        $canonicalURL = $this->prepareCanonicalRequest();
908
 
909
        // Step 2: CREATE THE STRING TO SIGN
910
        $stringToSign = $this->prepareStringToSign($canonicalURL);
911
 
912
        // Step 3: CALCULATE THE SIGNATURE
913
        $signature = $this->calculateSignature($stringToSign);
914
 
915
        // Step 4: CALCULATE AUTHORIZATION HEADER
916
        if ($signature) {
917
            $this->awsHeaders['Authorization'] = $this->buildAuthorizationString($signature);
918
            return $this->awsHeaders;
919
        }
920
    }
921
 
922
    private function buildAuthorizationString($strSignature) {
923
        return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate() . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
924
    }
925
 
926
    private function generateHex($data) {
927
        return strtolower(bin2hex(hash("sha256", $data, true)));
928
    }
929
 
930
    private function getSignatureKey($key, $date, $regionName, $serviceName) {
931
        $kSecret = "AWS4" . $key;
932
        $kDate = hash_hmac("sha256", $date, $kSecret, true);
933
        $kRegion = hash_hmac("sha256", $regionName, $kDate, true);
934
        $kService = hash_hmac("sha256", $serviceName, $kRegion, true);
935
        $kSigning = hash_hmac("sha256", $this->aws4Request, $kService, true);
936
 
937
        return $kSigning;
938
    }
939
 
940
    private function getTimeStamp() {
941
        return gmdate("Ymd\THis\Z");
942
    }
943
 
944
    private function getDate() {
945
        return gmdate("Ymd");
946
    }
947
}