Subversion Repositories cheapmusic

Rev

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,
143 - 288
                    "Show" => true,
289
                    "Details" => ""
99 - 290
                );
81 - 291
            }
292
        }
99 - 293
    }
81 - 294
 
99 - 295
    if ($needMatches) {
296
        if ($cnt = 0) {
297
            $_SESSION["discogs"] = "";
81 - 298
        }
99 - 299
        else {
130 - 300
            endMatches($xh);
127 - 301
 
302
            $_SESSION["discogs"] = $xh->flush();
154 - 303
            //error_log(print_r($_SESSION["discogs"], 1));
99 - 304
        }
81 - 305
    }
99 - 306
 
81 - 307
    // If the response does not indicate 'Success,' log the error(s)
99 - 308
    /******************
81 - 309
    else {
96 - 310
        my_error_log($url);
99 - 311
        if (!empty($parsed_json->OperationRequest->Errors)) {
312
            foreach($parsed_json->OperationRequest->Errors->Error as $error){
96 - 313
                my_error_log($error->Message . " (" . $error->Code . ")");
81 - 314
            }
99 - 315
        } else if (!empty($parsed_json->Errors)) {
316
            foreach($parsed_json->OperationRequest->Errors->Error as $error){
96 - 317
                my_error_log($error->Message . " (" . $error->Code . ")");
81 - 318
            }
99 - 319
        } else if (!empty($parsed_json->Error)) {
320
            my_error_log($parsed_json->Error->Message . " (" . $parsed_json->Error->Code . ")");
81 - 321
        } else {
96 - 322
            my_error_log(print_r($result, 1));
81 - 323
        }
324
    }
99 - 325
    ******************/
81 - 326
 
327
    return $arr;
328
}
329
 
127 - 330
function startMatches(&$xh) {
331
    $xh->add_attribute("class", "container-fluid bg-light");
141 - 332
    $xh->add_attribute("id", "discogsTable");
127 - 333
    $xh->tag('div');
334
    $xh->add_attribute("class", "text-center py-2");
335
    $xh->tag('h2', "Matching Albums");
81 - 336
 
137 - 337
    $xh->add_attribute("id", "discogsDeckForm");
338
    $xh->add_attribute("method", "post");
339
    $xh->add_attribute("action", "/index.php");
127 - 340
    $xh->tag('form');
341
    $xh->insert_code(inputSessionTab());
342
    $xh->insert_code(inputNonce());
343
    $xh->add_attribute("id", "discogsTitle");
344
    $xh->add_attribute("type", "hidden");
345
    $xh->add_attribute("name", "discogsTitle");
346
    $xh->add_attribute("value", "");
347
    $xh->single_tag('input');
348
    $xh->add_attribute("id", "discogsArtist");
349
    $xh->add_attribute("type", "hidden");
350
    $xh->add_attribute("name", "discogsArtist");
351
    $xh->add_attribute("value", "");
352
    $xh->single_tag('input');
353
    $xh->add_attribute("id", "discogsBarcode");
354
    $xh->add_attribute("type", "hidden");
355
    $xh->add_attribute("name", "discogsBarcode");
356
    $xh->add_attribute("value", "");
357
    $xh->single_tag('input');
358
    $xh->add_attribute("id", "discogsDeck");
359
    $xh->add_attribute("class", "card-deck");
360
    $xh->tag('div');
81 - 361
}
362
 
130 - 363
function endMatches(&$xh) {
364
    $xh->insert_code(discogsEvents());
81 - 365
 
127 - 366
    $xh->close(); // div
367
    $xh->close(); // form>";
368
    $xh->close(); // div>";
81 - 369
}
370
 
130 - 371
function addMatch(&$xh, $item, $cnt, $mediaType) {
127 - 372
    $xhmod = new Html;
373
    $xhmod->init($_SESSION["htmlIndent"]);
374
 
81 - 375
    $artists = getArtists($item);
99 - 376
    $contributers = getContributers($item);
81 - 377
 
99 - 378
    $label = "";
379
    if (!empty($item->ItemInfo->ByLineInfo->Manufacturer)) {
100 - 380
        $label = $item->ItemInfo->ByLineInfo->Manufacturer->DisplayValue;
81 - 381
    }
382
 
383
    $languages = [];
99 - 384
    if (!empty($item->ItemInfo->ContentInfo->Languages)) {
385
        foreach ($item->ItemInfo->ContentInfo->Languages->DisplayValues as $language) {
81 - 386
            if ((string)$language->Type != "Unknown") {
99 - 387
                $languages[] = $language->DisplayValue . " (" . $language->Type . ")";
81 - 388
            }
389
        }
390
    }
391
 
101 - 392
    $genres = [];
393
    if (!empty($item->ItemInfo->Genre)) {
394
        foreach($item->ItemInfo->Genre as $genre) {
395
            $genres[] = join(" ", array_map('ucfirst', explode('-', $genre)));
396
        }
397
    }
398
 
399
    $runningTime = "";
400
    if (!empty($item->ItemInfo->RunningTime)) {
401
        if ($mediaType != 'Digital') {
402
            $runningTime = (string)$item->ItemInfo->RunningTime . " minutes";
403
        } else {
404
            $runningTime = gmdate("H:i:s", (int)$item->ItemInfo->RunningTime);
405
        }
406
    }
407
 
127 - 408
    $xh->add_attribute("class", "card mx-auto discogs-card");
409
    $xh->tag('div');
410
    $xh->add_attribute("class", "card-header bg-secondary d-flex");
411
    $xh->tag('div');
81 - 412
    $searchArtists = "";
105 - 413
    $wlArtists = "";
99 - 414
    if (!empty($artists)) {
415
        if (count($artists) < 5) {
416
            $wlArtists = join(", ", $artists);
417
            if (!empty($artists) && $artists[0] != 'Various') {
418
                $searchArtists = join(" ", $artists);
419
            }
420
        } else {
421
            $wlArtists = "Various Artists";
81 - 422
        }
423
    }
99 - 424
    else if (!empty($contributers)) {
425
        if (count($contributers) < 5) {
426
            $wlArtists = join(", ", $contributers);
427
            if (!empty($contributers) && $contributers[0] != 'Various') {
428
                $searchArtists = join(" ", $contributers);
429
            }
430
        } else {
431
            $wlArtists = "Various Artists";
432
        }
81 - 433
    }
127 - 434
 
435
    $str = htmlentities((string)$item->ItemInfo->Title->DisplayValue);
124 - 436
    if (!empty($wlArtists)) {
125 - 437
        $str .= " by " . htmlentities($wlArtists);
124 - 438
    }
127 - 439
    $xh->add_attribute("class", "card-title font-weight-bold small flex-grow-1");
440
    $xh->tag('p', $str);
441
    $xh->close(); // div
81 - 442
 
99 - 443
    $thumbnail = !empty($item->Images->Primary->Medium->URL) ? (string)$item->Images->Primary->Medium->URL : "images/no-image-available.jpg";
81 - 444
 
127 - 445
    $xh->add_attribute("class", "card-body d-flex justify-content-center align-items-center p-1 m-0");
446
    $xh->tag('div');
447
    $xh->add_attribute("class", "btn p-0 m-0 lazyload");
448
    $xh->add_attribute("src", PIXEL);
449
    $xh->add_attribute("data-src",  $thumbnail);
450
    $xh->add_attribute("data-toggle", "modal");
451
    $xh->add_attribute("data-target", "#masterModal" . $cnt);
452
    $xh->add_attribute("title", "Album Information");
453
    $xh->add_attribute("data-toggle2", "tooltip");
454
    $xh->add_attribute("alt", "Cover Thumbnail");
455
    $xh->single_tag('img');
456
    $xh->close(); // div
141 - 457
    $xh->add_attribute("class", "card-footer form-row btn-group-sm bg-secondary p-0 m-0");
127 - 458
    $xh->tag('div');
141 - 459
    $xh->add_attribute("class", "col-3 btn-group-sm p-0 m-0");
127 - 460
    $xh->tag('div');
81 - 461
 
462
    $barcode = "";
463
    $tmpBarcode = "";
99 - 464
    if (isset($item->ItemInfo->ExternalIds->UPCs)) {
465
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->UPCs->DisplayValues[0];
81 - 466
    }
99 - 467
    else if (isset($item->ItemInfo->ExternalIds->EANs)) {
468
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->EANs->DisplayValues[0];
469
    }
470
    else if (isset($item->ItemInfo->ExternalIds->ISBNs)) {
471
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->ISBNs->DisplayValues[0];
472
    }
473
    else if (isset($item->ItemInfo->ExternalIds->EISBNs)) {
474
        $tmpBarcode = (string)$item->ItemInfo->ExternalIds->EISBNs->DisplayValues[0];
475
    }
81 - 476
    $barcodeType = clsLibGTIN::GTINCheck($tmpBarcode, false, 1);
477
    if (!empty($barcodeType)) {
478
        $barcode = $tmpBarcode;
479
    }
480
 
141 - 481
    if (isLoggedIn()) {
482
        $checkWlFlag = checkWishlist('asin', $item->ASIN);
81 - 483
        $wlArr = array(
484
            'asin' => (string)$item->ASIN,
99 - 485
            'title' => (string)$item->ItemInfo->Title->DisplayValue,
81 - 486
            'artist' => $wlArtists,
487
            'barcode' => $barcode,
488
            'thumbnail' => $thumbnail,
489
            'url' => (string)$item->DetailPageURL
490
        );
491
        $wl = base64_encode(json_encode($wlArr));
141 - 492
 
493
        if (!$checkWlFlag) {
494
            $xh->add_attribute("id", "wl" . $cnt . "Btn");
495
            $xh->add_attribute("data-user", $_SESSION['sessData']['userID']);
496
            $xh->add_attribute("data-cnt", $cnt);
497
            $xh->add_attribute("data-wl", $wl);
498
        }
499
 
127 - 500
        $xh->add_attribute("type", "button");
501
        $xh->add_attribute("class", "btn btn-info btn-sm");
141 - 502
        $xh->add_attribute("title", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
503
        $xh->add_attribute("aria-label", $checkWlFlag ? "Already on Wishlist" : "Add to Wishlist");
127 - 504
        $xh->add_attribute("data-toggle", "tooltip");
505
        $xh->tag('button');
506
        $xh->add_attribute("class", "material-icons");
141 - 507
        $xh->tag('i', $checkWlFlag ? "library_add_check" : "library_add");
127 - 508
        $xh->close(); // button
81 - 509
    }
141 - 510
    $xh->close(); // div
81 - 511
 
141 - 512
    $xh->add_attribute("class", "col-3 btn-group-sm p-0 m-0");
513
    $xh->tag('div');
514
    $xh->close(); // div
515
 
125 - 516
    $searchTitle = 'Searching for:<br><br><strong>' . htmlentities((string)$item->ItemInfo->Title->DisplayValue) . ' by ' . htmlentities($wlArtists) . '</strong>';
81 - 517
    if (!empty($barcode)) {
518
        $searchTitle .= " (" . displayBarcode($barcode) . ")";
519
    }
520
 
141 - 521
    $xh->add_attribute("class", "col-6 btn-group p-0 m-0");
522
    $xh->tag('div');
127 - 523
    $xh->add_attribute("id", "discogsSearch" . $cnt . "Btn");
524
    $xh->add_attribute("type", "submit");
134 - 525
    $xh->add_attribute("name", "submitBtn");
127 - 526
    $xh->add_attribute("value", "discogsSearch");
527
    $xh->add_attribute("class", "btn btn-success btn-sm");
528
    $xh->add_attribute("title", "Search for Store Offers");
529
    $xh->add_attribute("aria-label", "Search for Store Offers");
530
    $xh->add_attribute("data-toggle", "tooltip");
130 - 531
    $xh->add_attribute("data-title", htmlentities((string)$item->ItemInfo->Title->DisplayValue));
532
    $xh->add_attribute("data-artist", htmlentities($searchArtists));
533
    $xh->add_attribute("data-barcode", $barcode);
534
    $xh->add_attribute("data-search-title", $searchTitle);
127 - 535
    $xh->tag('button');
141 - 536
    $xh->add_attribute("class", "material-icons material-text");
127 - 537
    $xh->tag('i', "search");
141 - 538
    $xh->tag('span', "Offer");
127 - 539
    $xh->close(); // button
540
    $xh->close(); // div
120 - 541
 
127 - 542
    $xh->add_attribute("id", "wishlistAdd" . $cnt);
543
    $xh->tag('span', "");
544
    $xh->close(); // div
81 - 545
 
127 - 546
    $xhmod->add_attribute("id", "masterModal" . $cnt);
547
    $xhmod->add_attribute("class", "modal");
548
    $xhmod->tag('div');
549
    $xhmod->add_attribute("class", "modal-dialog");
550
    $xhmod->tag('div');
551
    $xhmod->add_attribute("class", "modal-content");
552
    $xhmod->tag('div');
553
    $xhmod->add_attribute("class", "modal-header bg-secondary");
554
    $xhmod->tag('div');
555
    $xhmod->add_attribute("class", "modal-title mx-auto display-6");
556
    $xhmod->tag('p', htmlentities((string)$item->ItemInfo->Title->DisplayValue) . " by " . htmlentities($wlArtists));
557
    $xhmod->add_attribute("type", "button");
558
    $xhmod->add_attribute("class", "close");
559
    $xhmod->add_attribute("data-dismiss", "modal");
560
    $xhmod->tag('button');
561
    $xhmod->add_attribute("class", "material-icons btn-dismiss");
562
    $xhmod->tag('i', "cancel_presentation");
563
    $xhmod->close(); // button
564
    $xhmod->close(); // div
81 - 565
 
127 - 566
    $xhmod->add_attribute("class", "modal-body mx-auto");
567
    $xhmod->tag('div');
81 - 568
 
99 - 569
    if (!empty($item->Images->Primary->Large->URL)) {
141 - 570
        $xhmod->add_attribute("class", "img-fluid d-flex mx-auto mb-4 lazyload");
127 - 571
        $xhmod->add_attribute("src", PIXEL);
572
        $xhmod->add_attribute("data-src", (string)$item->Images->Primary->Large->URL);
573
        $xhmod->add_attribute("alt", "Cover Image");
574
        $xhmod->single_tag('img');
81 - 575
    }
576
 
127 - 577
    $xhmod->add_attribute("class", "table-borderless table-condensed small mx-auto");
578
    $xhmod->tag('table');
81 - 579
 
127 - 580
    $xhmod->tag('tr');
581
    $xhmod->add_attribute("class", "px-1");
582
    $xhmod->tag('td', "Title:");
583
    $xhmod->add_attribute("class", "px-1");
584
    $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Title->DisplayValue));
585
    $xhmod->close(); // tr
586
 
587
    $xhmod->tag('tr');
588
    $xhmod->add_attribute("class", "px-1");
589
    $xhmod->tag('td', "Artist:");
590
    $xhmod->add_attribute("class", "px-1");
591
    $xhmod->tag('td', htmlentities(join(", ", $artists)));
592
    $xhmod->close(); // tr
593
 
99 - 594
    if (!empty($contributers)) {
127 - 595
        $xhmod->tag('tr');
596
        $xhmod->add_attribute("class", "px-1");
597
        $xhmod->tag('td', "Creator:");
598
        $xhmod->add_attribute("class", "px-1");
599
        $xhmod->tag('td', htmlentities(join(", ", $contributers)));
600
        $xhmod->close(); // tr
81 - 601
    }
602
 
603
    if (!empty($actors)) {
127 - 604
        $xhmod->tag('tr');
605
        $xhmod->add_attribute("class", "px-1");
606
        $xhmod->tag('td', "Actor:");
607
        $xhmod->add_attribute("class", "px-1");
608
        $xhmod->tag('td', htmlentities(join(", ", $actors)));
609
        $xhmod->close(); // tr
81 - 610
    }
611
 
612
    if (!empty($directors)) {
127 - 613
        $xhmod->tag('tr');
614
        $xhmod->add_attribute("class", "px-1");
615
        $xhmod->tag('td', "Director:");
616
        $xhmod->add_attribute("class", "px-1");
617
        $xhmod->tag('td', htmlentities(join(", ", $directors)));
618
        $xhmod->close(); // tr
81 - 619
    }
620
 
621
    if (!empty($authors)) {
127 - 622
        $xhmod->tag('tr');
623
        $xhmod->add_attribute("class", "px-1");
624
        $xhmod->tag('td', "Author:");
625
        $xhmod->add_attribute("class", "px-1");
626
        $xhmod->tag('td', htmlentities(join(", ", $authors)));
627
        $xhmod->close(); // tr
81 - 628
    }
629
 
99 - 630
    if (!empty($item->ItemInfo->Classifications->Binding)) {
127 - 631
        $xhmod->tag('tr');
632
        $xhmod->add_attribute("class", "px-1");
633
        $xhmod->tag('td', "Type:");
634
        $xhmod->add_attribute("class", "px-1");
635
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Classifications->Binding->DisplayValue));
636
        $xhmod->close(); // tr
81 - 637
    }
638
 
101 - 639
    if (!empty($item->ItemInfo->MediaType)) { //
127 - 640
        $xhmod->tag('tr');
641
        $xhmod->add_attribute("class", "px-1");
642
        $xhmod->tag('td', "Media Type:");
643
        $xhmod->add_attribute("class", "px-1");
644
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->MediaType));
645
        $xhmod->close(); // tr
81 - 646
    }
647
 
648
    if (!empty($barcode)) {
127 - 649
        $xhmod->tag('tr');
650
        $xhmod->add_attribute("class", "px-1");
651
        $xhmod->tag('td', "Barcode:");
652
        $xhmod->add_attribute("class", "px-1");
653
        $xhmod->tag('td', displayBarcode($barcode));
654
        $xhmod->close(); // tr
81 - 655
    }
656
 
99 - 657
    if (!empty($label)) {
127 - 658
        $xhmod->tag('tr');
659
        $xhmod->add_attribute("class", "px-1");
660
        $xhmod->tag('td', "Label:");
661
        $xhmod->add_attribute("class", "px-1");
662
        $xhmod->tag('td', htmlentities($label));
663
        $xhmod->close(); // tr
81 - 664
    }
665
 
666
    if (!empty($languages)) {
127 - 667
        $xhmod->tag('tr');
668
        $xhmod->add_attribute("class", "px-1");
669
        $xhmod->tag('td', "Languages:");
670
        $xhmod->add_attribute("class", "px-1");
671
        $xhmod->tag('td', htmlentities(join(", ", $languages)));
672
        $xhmod->close(); // tr
81 - 673
    }
674
 
99 - 675
    if (!empty($item->ItemInfo->ContentInfo->PublicationDate)) {
127 - 676
        $xhmod->tag('tr');
677
        $xhmod->add_attribute("class", "px-1");
678
        $xhmod->tag('td', "Publication Date:");
679
        $xhmod->add_attribute("class", "px-1");
680
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->PublicationDate->DisplayValue));
681
        $xhmod->close(); // tr
81 - 682
    }
683
 
99 - 684
    if (!empty($item->ItemInfo->ContentInfo->ReleaseDate)) {
127 - 685
        $xhmod->tag('tr');
686
        $xhmod->add_attribute("class", "px-1");
687
        $xhmod->tag('td', "Release Date:");
688
        $xhmod->add_attribute("class", "px-1");
689
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->ReleaseDate->DisplayValue));
690
        $xhmod->close(); // tr
81 - 691
    }
692
 
101 - 693
    if (!empty($genres)) {
127 - 694
        $xhmod->tag('tr');
695
        $xhmod->add_attribute("class", "px-1");
696
        $xhmod->tag('td', "Genre:");
697
        $xhmod->add_attribute("class", "px-1");
698
        $xhmod->tag('td', htmlentities(join(", ", $genres)));
699
        $xhmod->close(); // tr
101 - 700
    }
701
 
99 - 702
    if (!empty($item->ItemInfo->ContentInfo->UnitCount->DisplayValue)) {
127 - 703
        $xhmod->tag('tr');
704
        $xhmod->add_attribute("class", "px-1");
705
        $xhmod->tag('td', "Number of Discs:");
706
        $xhmod->add_attribute("class", "px-1");
707
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->UnitCount->DisplayValue));
708
        $xhmod->close(); // tr
81 - 709
    }
710
 
99 - 711
    if (!empty($item->ItemInfo->ContentInfo->PagesCount->DisplayValue)) {
127 - 712
        $xhmod->tag('tr');
713
        $xhmod->add_attribute("class", "px-1");
714
        $xhmod->tag('td', "Number of Pages:");
715
        $xhmod->add_attribute("class", "px-1");
716
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->ContentInfo->PagesCount->DisplayValue));
717
        $xhmod->close(); // tr
81 - 718
    }
719
 
101 - 720
    if (!empty($item->ItemInfo->RunningTime)) {
127 - 721
        $xhmod->tag('tr');
722
        $xhmod->add_attribute("class", "px-1");
723
        $xhmod->tag('td', "Running Time:");
724
        $xhmod->add_attribute("class", "px-1");
725
        $xhmod->tag('td', htmlentities($runningTime));
726
        $xhmod->close(); // tr
101 - 727
    }
728
 
729
    if (!empty($item->ItemInfo->Edition)) {
127 - 730
        $xhmod->tag('tr');
731
        $xhmod->add_attribute("class", "px-1");
732
        $xhmod->tag('td', "Edition:");
733
        $xhmod->add_attribute("class", "px-1");
734
        $xhmod->tag('td', htmlentities((string)$item->ItemInfo->Edition));
735
        $xhmod->close(); // tr
101 - 736
    }
737
 
738
    if (!empty($item->Tracks)) {
127 - 739
        $xhmod->tag('tr');
740
        $xhmod->add_attribute("colspan", "2");
741
        $xhmod->add_attribute("class", "px-1");
742
        $xhmod->tag('td', "Tracks:");
743
        $xhmod->close(); // tr
744
 
745
        $xhmod->tag('tr');
746
        $xhmod->add_attribute("colspan", "2");
747
        $xhmod->tag('td');
748
        $xhmod->add_attribute("class", "pl-3 pt-0 small list-unstyled");
749
        $xhmod->tag('ul');
101 - 750
        foreach ($item->Tracks->Disc as $disc) {
751
            if ((int)$item->ItemInfo->ContentInfo->UnitCount->DisplayValue > 1 && !empty($disc->attributes())) {
127 - 752
                $xhmod->add_attribute("class", "font-weight-bold");
753
                $xhmod->tag('li', "Disc " . $disc->attributes() . ":");
101 - 754
            }
755
 
756
            foreach($disc->Track as $track) {
127 - 757
                $xhmod->tag('li', (!empty($track->attributes()) ? $track->attributes() . ") " : "") . htmlentities($track));
101 - 758
            }
759
        }
127 - 760
        $xhmod->close(); // ul
761
        $xhmod->close(); // td
762
        $xhmod->close(); // tr
101 - 763
    }
764
 
127 - 765
    $xhmod->close(); // table
766
    $xhmod->close(); // div
767
    $xhmod->add_attribute("class", "modal-footer bg-secondary justify-content-between");
768
    $xhmod->tag('div');
769
    $xhmod->add_attribute("class", "float-right");
770
    $xhmod->tag('span');
771
    $xhmod->add_attribute("type", "button");
772
    $xhmod->add_attribute("class", "btn btn-danger");
773
    $xhmod->add_attribute("data-dismiss", "modal");
774
    $xhmod->tag('button', "Close");
775
    $xhmod->close(); // span
776
    $xhmod->close(); // div
777
    $xhmod->close(); // div
778
    $xhmod->close(); // div
779
    $xhmod->close(); // div
780
    $html = $xhmod->flush();
781
    //error_log(print_r($html, 1));
81 - 782
 
127 - 783
    $xh->insert_code($html);
784
    $xh->close(); // div
785
 
786
    return;
81 - 787
}
788
 
789
function getArtists($item) {
790
    $artists = [];
791
 
101 - 792
    if (!empty($item->ItemInfo->Artist)){
793
        $artists[] = $item->ItemInfo->Artist;
794
    } else if (!empty($item->ItemInfo->ByLineInfo->Contributors)) {
99 - 795
        foreach ($item->ItemInfo->ByLineInfo->Contributors as $artist) {
796
            if ((string)$artist->Role == "Artist") {
797
                $artists[] = $artist->Name;
798
            }
81 - 799
        }
800
    }
801
 
802
    return $artists;
803
}
804
 
99 - 805
function getContributers($item) {
806
    $contributers = [];
807
 
808
    if (!empty($item->ItemInfo->ByLineInfo->Contributors)) {
809
        foreach ($item->ItemInfo->ByLineInfo->Contributors as $creator) {
810
            if ((string)$creator->Role == "Primary Contributor") {
811
                $contributers[] = $creator->Name;
81 - 812
            }
99 - 813
            else {
814
                $contributers[] = $creator->Name . " (" . $creator->Role . ")";
815
            }
81 - 816
        }
817
    }
818
 
99 - 819
    return $contributers;
81 - 820
}
99 - 821
 
822
class AwsV4 {
823
 
824
    private $accessKey = null;
825
    private $secretKey = null;
826
    private $path = null;
827
    private $regionName = null;
828
    private $serviceName = null;
829
    private $httpMethodName = null;
830
    private $queryParametes = array();
831
    private $awsHeaders = array();
832
    private $payload = "";
833
 
834
    private $HMACAlgorithm = "AWS4-HMAC-SHA256";
835
    private $aws4Request = "aws4_request";
836
    private $strSignedHeader = null;
837
    private $xAmzDate = null;
838
    private $currentDate = null;
839
 
840
    public function __construct($accessKey, $secretKey) {
841
        $this->accessKey = $accessKey;
842
        $this->secretKey = $secretKey;
843
        $this->xAmzDate = $this->getTimeStamp();
844
        $this->currentDate = $this->getDate();
845
    }
846
 
847
    function setPath($path) {
848
        $this->path = $path;
849
    }
850
 
851
    function setServiceName($serviceName) {
852
        $this->serviceName = $serviceName;
853
    }
854
 
855
    function setRegionName($regionName) {
856
        $this->regionName = $regionName;
857
    }
858
 
859
    function setPayload($payload) {
860
        $this->payload = $payload;
861
    }
862
 
863
    function setRequestMethod($method) {
864
        $this->httpMethodName = $method;
865
    }
866
 
867
    function addHeader($headerName, $headerValue) {
868
        $this->awsHeaders[$headerName] = $headerValue;
869
    }
870
 
871
    private function prepareCanonicalRequest() {
872
        $canonicalURL = "";
873
        $canonicalURL .= $this->httpMethodName . "\n";
874
        $canonicalURL .= $this->path . "\n" . "\n";
875
        $signedHeaders = '';
876
        foreach ($this->awsHeaders as $key => $value) {
877
            $signedHeaders .= $key . ";";
878
            $canonicalURL .= $key . ":" . $value . "\n";
879
        }
880
        $canonicalURL .= "\n";
881
        $this->strSignedHeader = substr($signedHeaders, 0, -1);
882
        $canonicalURL .= $this->strSignedHeader . "\n";
883
        $canonicalURL .= $this->generateHex($this->payload);
884
        return $canonicalURL;
885
    }
886
 
887
    private function prepareStringToSign($canonicalURL) {
888
        $stringToSign = '';
889
        $stringToSign .= $this->HMACAlgorithm . "\n";
890
        $stringToSign .= $this->xAmzDate . "\n";
891
        $stringToSign .= $this->currentDate . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "\n";
892
        $stringToSign .= $this->generateHex($canonicalURL);
893
        return $stringToSign;
894
    }
895
 
896
    private function calculateSignature($stringToSign) {
897
        $signatureKey = $this->getSignatureKey($this->secretKey, $this->currentDate, $this->regionName, $this->serviceName);
898
        $signature = hash_hmac("sha256", $stringToSign, $signatureKey, true);
899
        $strHexSignature = strtolower(bin2hex($signature));
900
        return $strHexSignature;
901
    }
902
 
903
    public function getHeaders() {
904
        $this->awsHeaders['x-amz-date'] = $this->xAmzDate;
905
        ksort($this->awsHeaders);
906
 
907
        // Step 1: CREATE A CANONICAL REQUEST
908
        $canonicalURL = $this->prepareCanonicalRequest();
909
 
910
        // Step 2: CREATE THE STRING TO SIGN
911
        $stringToSign = $this->prepareStringToSign($canonicalURL);
912
 
913
        // Step 3: CALCULATE THE SIGNATURE
914
        $signature = $this->calculateSignature($stringToSign);
915
 
916
        // Step 4: CALCULATE AUTHORIZATION HEADER
917
        if ($signature) {
918
            $this->awsHeaders['Authorization'] = $this->buildAuthorizationString($signature);
919
            return $this->awsHeaders;
920
        }
921
    }
922
 
923
    private function buildAuthorizationString($strSignature) {
924
        return $this->HMACAlgorithm . " " . "Credential=" . $this->accessKey . "/" . $this->getDate() . "/" . $this->regionName . "/" . $this->serviceName . "/" . $this->aws4Request . "," . "SignedHeaders=" . $this->strSignedHeader . "," . "Signature=" . $strSignature;
925
    }
926
 
927
    private function generateHex($data) {
928
        return strtolower(bin2hex(hash("sha256", $data, true)));
929
    }
930
 
931
    private function getSignatureKey($key, $date, $regionName, $serviceName) {
932
        $kSecret = "AWS4" . $key;
933
        $kDate = hash_hmac("sha256", $date, $kSecret, true);
934
        $kRegion = hash_hmac("sha256", $regionName, $kDate, true);
935
        $kService = hash_hmac("sha256", $serviceName, $kRegion, true);
936
        $kSigning = hash_hmac("sha256", $this->aws4Request, $kService, true);
937
 
938
        return $kSigning;
939
    }
940
 
941
    private function getTimeStamp() {
942
        return gmdate("Ymd\THis\Z");
943
    }
944
 
945
    private function getDate() {
946
        return gmdate("Ymd");
947
    }
948
}