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
| class Amazon{
public static $secretKey = la clé privé;
public static $publicKey = la clé public;
private static $_key;
private static $_hashAlgorithm;
/**
* Permet de creer une url pour les services amazon
* @param string $operation le paramètre opération de la requete
* @param array $options les autres paramètres.
* @return string l'url
*/
public static function createRequest($operation, array $options)
{
$options['AWSAccessKeyId'] = self::$publicKey;
$options['Service'] = 'AWSECommerceService';
$options['Operation'] = (string) $operation;
$baseUri = 'http://webservices.amazon.fr';
if(self::$secretKey !== null) {
$options['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");;
ksort($options);
$options['Signature'] = self::computeSignature($baseUri, self::$secretKey, $options);
}
return 'http://webservices.amazon.fr/onca/xml?'.http_build_query($options, null, '&');
}
static public function computeSignature($baseUri, $secretKey, array $options)
{
$signature = self::buildRawSignature($baseUri, $options);
return base64_encode(
self::compute($secretKey, 'sha256', $signature, 'binary')
);
}
static public function buildRawSignature($baseUri, $options)
{
ksort($options);
$params = array();
foreach($options AS $k => $v) {
$params[] = $k."=".rawurlencode($v);
}
return sprintf("GET\n%s\n/onca/xml\n%s",
str_replace('http://', '', $baseUri),
implode("&", $params)
);
}
public static function compute($key, $hash, $data, $output)
{
// set the key
if (!isset($key) || empty($key)) {
throw new Exception('provided key is null or empty');
}
self::$_key = $key;
// set the hash
self::_setHashAlgorithm($hash);
// perform hashing and return
return self::_hash($data, $output);
}
protected static function _setHashAlgorithm($hash)
{
if (!isset($hash) || empty($hash)) {
throw new Exception('provided hash string is null or empty');
}
$hash = strtolower($hash);
$hashSupported = false;
if (function_exists('hash_algos') && in_array($hash, hash_algos())) {
$hashSupported = true;
}
if ($hashSupported === false) {
throw new Exception('hash algorithm provided is not supported on this PHP installation; please enable the hash or mhash extensions');
}
self::$_hashAlgorithm = $hash;
}
protected static function _hash($data, $output = 'string', $internal = false)
{
if (function_exists('hash_hmac')) {
if ($output == 'binary') {
return hash_hmac(self::$_hashAlgorithm, $data, self::$_key, 1);
}
return hash_hmac(self::$_hashAlgorithm, $data, self::$_key);
}
if (function_exists('mhash')) {
if ($output == 'binary') {
return mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
}
$bin = mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
return bin2hex($bin);
}
}
protected static function _getMhashDefinition($hashAlgorithm)
{
for ($i = 0; $i <= mhash_count(); $i++)
{
$types[mhash_get_hash_name($i)] = $i;
}
return $types[strtoupper($hashAlgorithm)];
}
} |
Partager