Bug 24031: Add safety checks in Koha::Plugins::call
[koha.git] / C4 / External / BakerTaylor.pm
blobdbb307cab9f0f87f253d56f35284f5b7d545a1a1
1 package C4::External::BakerTaylor;
3 # Copyright (C) 2008 LibLime
4 # <jmf at liblime dot com>
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use XML::Simple;
22 use LWP::Simple;
23 use HTTP::Request::Common;
25 use C4::Context;
26 use C4::Debug;
28 use Modern::Perl;
30 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
32 BEGIN {
33 require Exporter;
34 @ISA = qw(Exporter);
35 $VERSION = 3.07.00.049;
36 @EXPORT_OK = qw(&availability &content_cafe &image_url &link_url &http_jacket_link);
37 %EXPORT_TAGS = (all=>\@EXPORT_OK);
40 # These variables are plack safe: they are initialized each time
41 my ( $user, $pass, $agent, $image_url, $link_url );
43 sub _initialize {
44 $user = (@_ ? shift : C4::Context->preference('BakerTaylorUsername') ) || ''; # LL17984
45 $pass = (@_ ? shift : C4::Context->preference('BakerTaylorPassword') ) || ''; # CC82349
46 $link_url = (@_ ? shift : C4::Context->preference('BakerTaylorBookstoreURL'));
47 $image_url = "https://contentcafe2.btol.com/ContentCafe/Jacket.aspx?UserID=$user&Password=$pass&Options=Y&Return=T&Type=S&Value=";
48 $agent = "Koha/$VERSION [en] (Linux)";
49 #"Mozilla/4.76 [en] (Win98; U)", # if for some reason you want to go stealth, you might prefer this
52 sub image_url {
53 _initialize();
54 ($user and $pass) or return;
55 my $isbn = (@_ ? shift : '');
56 $isbn =~ s/(p|-)//g; # sanitize
57 return $image_url . $isbn;
60 sub link_url {
61 _initialize();
62 my $isbn = (@_ ? shift : '');
63 $isbn =~ s/(p|-)//g; # sanitize
64 $link_url or return;
65 return $link_url . $isbn;
68 sub content_cafe_url {
69 _initialize();
70 ($user and $pass) or return;
71 my $isbn = (@_ ? shift : '');
72 $isbn =~ s/(p|-)//g; # sanitize
73 return "https://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=$user&Password=$pass&Options=Y&ItemKey=$isbn";
76 sub http_jacket_link {
77 _initialize();
78 my $isbn = shift or return;
79 $isbn =~ s/(p|-)//g; # sanitize
80 my $image = availability($isbn);
81 my $alt = "Buy this book";
82 $image and $image = qq(<img class="btjacket" alt="$alt" src="$image" />);
83 my $link = &link_url($isbn);
84 unless ($link) {return $image || '';}
85 return sprintf qq(<a class="btlink" href="%s">%s</a>),$link,($image||$alt);
88 sub availability {
89 _initialize();
90 my $isbn = shift or return;
91 ($user and $pass) or return;
92 $isbn =~ s/(p|-)//g; # sanitize
93 my $url = "https://contentcafe2.btol.com/ContentCafe/InventoryAvailability.asmx/CheckInventory?UserID=$user&Password=$pass&Value=$isbn";
94 $debug and warn __PACKAGE__ . " request:\n$url\n";
95 my $content = get($url);
96 $debug and print STDERR $content, "\n";
97 warn "could not retrieve $url" unless $content;
98 my $xmlsimple = XML::Simple->new();
99 my $result = $xmlsimple->XMLin($content);
100 if ($result->{Error}) {
101 warn "Error returned to " . __PACKAGE__ . " : " . $result->{Error};
103 my $avail = $result->{Availability};
104 return ($avail and $avail !~ /^false$/i) ? &image_url($isbn) : 0;
109 __END__
111 =head1 NAME
113 C4::External::BakerTaylor
115 =head1 DESCRIPTION
117 Functions for retrieving content from Baker and Taylor, inventory availability and "Content Cafe".
119 The settings for this module are controlled by System Preferences:
121 These can be overridden for testing purposes using the initialize function.
123 =head1 FUNCTIONS
125 =head2 availability($isbn);
127 $isbn is a isbn string
129 =head1 NOTES
131 A request with failed authentication might see this back from Baker + Taylor:
133 <?xml version="1.0" encoding="utf-8"?>
134 <InventoryAvailability xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DateTime="2008-03-07T22:01:25.6520429-05:00" xmlns="http://ContentCafe2.btol.com">
135 <Key Type="Undefined">string</Key>
136 <Availability>false</Availability>
137 <Error>Invalid UserID</Error>
138 </InventoryAvailability>
140 Such response will trigger a warning for each request (potentially many). Point being, do not leave this module configured with incorrect username and password in production.
142 =head1 SEE ALSO
144 LWP::UserAgent
146 =head1 AUTHOR
148 Joe Atzberger
149 atz AT liblime DOT com
151 =cut