-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathamazon.php
380 lines (314 loc) · 11 KB
/
amazon.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
<?php
/**
* Handles the querying of the Amazon product database
* @package now-reading
*/
/*
* Returns the CDATA of an amazon search result attribute, if available.
* The result is sanitized.
*/
function get_attribute_cdata($attribute)
{
$value = '';
if (isset($attribute) && is_object($attribute))
{
$value = $attribute->CDATA();
$value = stripslashes($value);
}
return $value;
}
function url_get_contents($request)
{
// Fetch the data using either Snoopy or cURL, depending on our options.
if ($options['httpLib'] == 'curl')
{
if (!function_exists('curl_init'))
{
return new WP_Error('curl-not-installed', __('cURL is not installed correctly.', NRTD));
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Now Reading Redux ' . NOW_READING_VERSION);
curl_setopt($ch, CURLOPT_HEADER, 0);
if (!empty($options['proxyHost']))
{
$proxy = $options['proxyHost'];
if (!empty($options['proxyPort']))
{
$proxy .= ":{$options['proxyPort']}";
}
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
else
{
require_once ABSPATH . WPINC . '/class-snoopy.php';
$snoopy = new snoopy;
$snoopy->agent = 'Now Reading Redux ' . NOW_READING_VERSION;
if (!empty($options['proxyHost']))
{
$snoopy->proxy_host = $options['proxyHost'];
if (!empty($options['proxyPort']))
{
$snoopy->proxy_port = $options['proxyPort'];
}
}
$snoopy->fetch($request);
return $snoopy->results;
}
}
/**
* Fetches and parses XML from Amazon for the given query.
* @param string $query Query string containing variables to search Amazon for. Valid variables: $isbn, $title, $author
* @return array Array containing each book's information.
*/
function query_amazon($query)
{
require_once dirname(__FILE__) . '/sha256.inc.php';
if (!function_exists('hmac'))
{
function hmac($key, $data, $hashfunc='sha256')
{
$blocksize=64;
if (strlen($key) > $blocksize) $key=pack('H*', $hashfunc($key));
$key=str_pad($key, $blocksize, chr(0x00));
$ipad=str_repeat(chr(0x36), $blocksize);
$opad=str_repeat(chr(0x5c), $blocksize);
$hmac = pack('H*', $hashfunc(($key^$opad) . pack('H*', $hashfunc(($key^$ipad) . $data))));
return $hmac;
}
}
global $item, $items;
$options = get_option(NOW_READING_OPTIONS);
$using_isbn = false;
parse_str($query);
if ( empty($isbn) && empty($title) && empty($author) )
return false;
if ( !empty($isbn) )
$using_isbn = true;
// Our query needs different vars depending on whether or not we're searching by ISBN, so build it here.
if ( $using_isbn ) {
$isbn = preg_replace('#([^0-9x]+)#i', '', $isbn);
$query = "isbn:$isbn";
} else {
$query='';
if ( !empty($title) )
$query = 'title:' . urlencode($title);
if ( !empty($author) )
$query .= 'author:' . urlencode($author);
}
// these items MUST be set in the Options screen
$AWSAccessKeyId = trim($options['AWSAccessKeyId']);
$SecretAccessKey = trim($options['SecretAccessKey']);
# // some paramters
$method = "GET";
$host = "ecs.amazonaws".$options['domain'];
$uri = "/onca/xml";
// additional parameters
$params["Service"] = "AWSECommerceService";
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";
$params["AssociateTag"] = urlencode($options['associate']);
$params["Power"] = $query;
$params["Operation"] = "ItemSearch";
$params["SearchIndex"] = "Books";
$params["ResponseGroup"] = "Request,Large,Images,AlternateVersions";
$params["AWSAccessKeyId"] = $AWSAccessKeyId;
// Sort paramters
ksort($params);
// re-build the request
$request = array();
foreach ($params as $parameter=>$value)
{
$parameter = str_replace("_", ".", $parameter);
$parameter = str_replace("%7E", "~", rawurlencode($parameter));
$value = str_replace("%7E", "~", rawurlencode($value));
$request[] = $parameter . "=" . $value;
}
$request = implode("&", $request);
$signatureString = $method . chr(10) . $host . chr(10) . $uri . chr(10) . $request;
$signature = urlencode(base64_encode(hmac($SecretAccessKey, $signatureString)));
$request = "http://" . $host . $uri . "?" . $request . "&Signature=" . $signature;
// Fetch the XML.
$xmlString = url_get_contents($request);
if ($options['debugMode'])
{
robm_dump("Amazon Search XML:", htmlentities(str_replace(">", ">\n", $xmlString)));
}
if (empty($xmlString))
{
do_action('nr_search_error', $query);
echo '
<div id="message" class="error fade">
<p><strong>' . __("Oops!") . '</strong></p>
<p>' . sprintf(__("For some reason, I couldn't search for your book on amazon%s.", NRTD), $options['domain']) . '</p>
<p>' . __("Amazon's Web Services may be down, or your Amazon Web Services Keys may be invalid.") . '</p>
<p>' . __("Try changing the HTTP Library setting and try again.", NRTD) . '</p>
</div>
';
return false;
}
require_once 'xml/IsterXmlSimpleXMLImpl.php';
$impl = new IsterXmlSimpleXMLImpl;
$xml = $impl->load_string($xmlString);
if (!isset($xml->ItemSearchResponse) || !isset($xml->ItemSearchResponse->Items))
{
do_action('nr_search_error', $query);
echo '
<div id="message" class="error fade">
<p><strong>' . __("Oops!") . '</strong></p>
';
if (isset($xml->ItemSearchResponse) &&
isset($xml->ItemSearchResponse->Error))
{
echo '
<p>Amazon error: <b>' . get_attribute_cdata($xml->ItemSearchResponse->Error->Message) . '</b></p>
';
}
else
{
echo 'Amazon returned:
<p>' . htmlentities(str_replace(">", ">\n", $xml->asXML())) . '</p>';
}
echo '
</div>
';
return false;
}
$items = $xml->ItemSearchResponse->Items->children();
if (count($items) == 0)
{
return false;
}
$results = array();
foreach ($items as $item)
{
if (!isset($item->ItemAttributes))
{
continue;
}
$asin = get_attribute_cdata($item->ASIN);
if (empty($asin))
{
continue;
}
// Get full meta-data given the current ISBN. Used to get all editions.
$metaData = getMetadataFromIsbn($asin, $AWSAccessKeyId, $SecretAccessKey, urlencode($options['associate']));
if ($options['debugMode'])
{
robm_dump("Amazon Lookup XML:", htmlentities(str_replace(">", ">\n", $metaData)));
}
$metaDataParser = new IsterXmlSimpleXMLImpl;
$metaDataXml = $metaDataParser->load_string($metaData);
if (!isset($metaDataXml->ItemLookupResponse->Items) ||
isset($metaDataXml->ItemLookupResponse->Items->Request->Errors))
{
continue;
}
$editions = $metaDataXml->ItemLookupResponse->Items->children();
if (count($editions) == 0)
{
continue;
}
// For each edition, add an entry.
foreach ($editions as $edition)
{
$asin = get_attribute_cdata($edition->ASIN);
if (empty($asin))
{
continue;
}
$title = get_attribute_cdata($edition->ItemAttributes->Title);
if (empty($title))
{
$title = __('Untitled', NRTD);
}
$author = get_attribute_cdata($edition->ItemAttributes->Author);
if (is_array($edition->ItemAttributes->Author))
{
foreach ($edition->ItemAttributes->Author as $a)
{
$author .= get_attribute_cdata($a) . ', ';
}
$author = substr($author, 0, -2);
}
if (empty($author))
{
$author = apply_filters('default_book_author', 'Unknown');
}
$size = "{$options['imageSize']}Image";
if (empty($item->$size))
{
continue;
}
$image = get_attribute_cdata($item->$size->URL);
if (empty($image))
{
$image = plugin_dir_url(__FILE__) . "no-image.png";
}
$binding = get_attribute_cdata($edition->ItemAttributes->Binding);
$ed = get_attribute_cdata($edition->ItemAttributes->Edition);
$date = get_attribute_cdata($edition->ItemAttributes->PublicationDate);
$publisher = get_attribute_cdata($edition->ItemAttributes->Publisher);
if ($options['debugMode'])
{
robm_dump("book:", $author, $title, $binding, $ed, $date, $publisher, $asin);
}
$results[] = apply_filters('raw_amazon_results', compact('author', 'title', 'binding', 'ed', 'date', 'publisher', 'image', 'asin'));
}
}
$results = apply_filters('returned_books', $results);
if ($options['debugMode'])
{
robm_dump("Results: ", $results);
}
return $results;
}
function getMetadataFromIsbn($isbn, $awsAccessKeyID, $awsSecretKey, $awsAssociateTag)
{
$host = 'ecs.amazonaws.com';
$path = '/onca/xml';
$args = array(
'AssociateTag' => $awsAssociateTag,
'AWSAccessKeyId' => $awsAccessKeyID,
'IdType' => 'ISBN',
'ItemId' => $isbn,
'Operation' => 'ItemLookup',
'ResponseGroup' => 'Large',
'SearchIndex' => 'Books',
'Service' => 'AWSECommerceService',
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
'Version'=> '2009-01-06'
);
ksort($args);
$parts = array();
foreach(array_keys($args) as $key) {
$parts[] = $key . "=" . $args[$key];
}
// Construct the string to sign
$stringToSign = "GET\n" . $host . "\n" . $path . "\n" . implode("&", $parts);
$stringToSign = str_replace('+', '%20', $stringToSign);
$stringToSign = str_replace(':', '%3A', $stringToSign);
$stringToSign = str_replace(';', urlencode(';'), $stringToSign);
// Sign the request
$signature = hash_hmac("sha256", $stringToSign, $awsSecretKey, TRUE);
// Base64 encode the signature and make it URL safe
$signature = base64_encode($signature);
$signature = str_replace('+', '%2B', $signature);
$signature = str_replace('=', '%3D', $signature);
// Construct the URL
$url = 'http://' . $host . $path . '?' . implode("&", $parts) . "&Signature=" . $signature;
$rawData = url_get_contents($url);
if ($options['debugMode'])
{
robm_dump("raw XML:", htmlentities(str_replace(">", ">\n", $rawData)));
}
return $rawData;
}
?>