bug 7284: fix package build problem, move BEGIN into new
[koha_gimpoz] / C4 / External / Amazon.pm
index 47f4b5e..54141db 100644 (file)
@@ -13,15 +13,18 @@ package C4::External::Amazon;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use XML::Simple;
 use LWP::Simple;
 use LWP::UserAgent;
 use HTTP::Request::Common;
 use C4::Koha;
+use URI::Escape;
+use POSIX;
+use Digest::SHA qw(hmac_sha256_base64);
 
 use strict;
 use warnings;
@@ -129,18 +132,31 @@ sub get_amazon_details {
     my %hformat = ( a => 'Books', g => 'Video', j => 'Music' );
     my $search_index = $hformat{ substr($record->leader(),6,1) } || 'Books';
 
-    my $url =
-        "http://ecs.amazonaws" . get_amazon_tld() .
-        "/onca/xml?Service=AWSECommerceService" .
-        "&AWSAccessKeyId=" . C4::Context->preference('AWSAccessKeyID') .
-        "&Operation=ItemLookup" .
-        "&AssociateTag=" . C4::Context->preference('AmazonAssocTag') .
-        "&Version=2009-02-01" .
-        "&ItemId=$item_id" .
-        "&IdType=$id_type" .
-        "&ResponseGroup=" . join( ',',  @aws );
-       $url .= "&SearchIndex=$search_index" if $id_type ne 'ASIN';
-    #warn $url;
+    my $parameters={Service=>"AWSECommerceService" ,
+        "AWSAccessKeyId"=> C4::Context->preference('AWSAccessKeyID') ,
+        "Operation"=>"ItemLookup", 
+        "AssociateTag"=>  C4::Context->preference('AmazonAssocTag') ,
+        "Version"=>"2009-06-01",
+        "ItemId"=>$item_id,
+        "IdType"=>$id_type,
+        "ResponseGroup"=>  join( ',',  @aws ),
+        "Timestamp"=>strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
+    };
+    $$parameters{"SearchIndex"} = $search_index if $id_type ne 'ASIN';
+    my @params;
+    while (my ($key,$value)=each %$parameters){
+        push @params, qq{$key=}.uri_escape($value, "^A-Za-z0-9\-_.~" );
+    }
+
+    my $url;
+    if (C4::Context->preference('AWSPrivateKey')) {
+        $url = qq{http://webservices.amazon} . get_amazon_tld() . 
+               "/onca/xml?" . join("&",sort @params) . qq{&Signature=} . uri_escape(SignRequest(@params),"^A-Za-z0-9\-_.~" );
+    } else {
+        $url = qq{http://webservices.amazon} . get_amazon_tld() .  "/onca/xml?" .join("&",sort @params);
+        warn "MUST set AWSPrivateKey syspref after 2009-08-15 in order to access Amazon web services";
+    }
+
     my $content = get($url);
     warn "could not retrieve $url" unless $content;
     my $xmlsimple = XML::Simple->new();
@@ -151,6 +167,13 @@ sub get_amazon_details {
     return $response;
 }
 
+sub SignRequest{
+    my @params=@_;
+    my $tld=get_amazon_tld(); 
+    my $string = qq{GET\nwebservices.amazon$tld\n/onca/xml\n} . join("&",sort @params);
+    return hmac_sha256_base64($string,C4::Context->preference('AWSPrivateKey')) . '=';
+}
+
 sub check_search_inside {
         my $isbn = shift;
         my $ua = LWP::UserAgent->new(
@@ -178,6 +201,8 @@ __END__
 
 =head1 NOTES
 
+=cut
+
 =head1 AUTHOR
 
 Joshua Ferraro <jmf@liblime.com>