|
Line 0
Link Here
|
|
|
1 |
=head IN THIS FILE |
| 2 |
|
| 3 |
This module extends the SMS::Send::Driver interface |
| 4 |
to implement a driver compatible with the Labyrintti SMS Gateway HTTP interface. |
| 5 |
|
| 6 |
Module parameters are sanitated against injection attacks. |
| 7 |
|
| 8 |
Labyrintti responds: |
| 9 |
|
| 10 |
success |
| 11 |
|
| 12 |
phone-number OK message-count description |
| 13 |
+358401234567 OK 1 message accepted for sending |
| 14 |
|
| 15 |
failure |
| 16 |
|
| 17 |
phone-number ERROR error-code message-count description |
| 18 |
e.g: 12345 ERROR 2 1 message failed: Too short phone number |
| 19 |
|
| 20 |
=cut |
| 21 |
|
| 22 |
package SMS::Send::Labyrintti::Driver; |
| 23 |
#use Modern::Perl; #Can't use this since SMS::Send uses hash keys starting with _ |
| 24 |
use utf8; |
| 25 |
use SMS::Send::Driver (); |
| 26 |
use LWP::Curl; |
| 27 |
use LWP::UserAgent; |
| 28 |
use URI::Escape; |
| 29 |
use C4::Context; |
| 30 |
use Encode; |
| 31 |
use Koha::Exception::ConnectionFailed; |
| 32 |
use Koha::Exception::SMSDeliveryFailure; |
| 33 |
|
| 34 |
use vars qw{$VERSION @ISA}; |
| 35 |
BEGIN { |
| 36 |
$VERSION = '0.06'; |
| 37 |
@ISA = 'SMS::Send::Driver'; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
##################################################################### |
| 42 |
# Constructor |
| 43 |
|
| 44 |
sub new { |
| 45 |
my $class = shift; |
| 46 |
my $params = {@_}; |
| 47 |
my $username = $params->{_login} ? $params->{_login} : C4::Context->config('smsProviders')->{'labyrintti'}->{'user'}; |
| 48 |
my $password = $params->{_password} ? $params->{_password} : C4::Context->config('smsProviders')->{'labyrintti'}->{'passwd'}; |
| 49 |
|
| 50 |
if (! defined $username ) { |
| 51 |
warn "->send_sms(_login) must be defined!"; |
| 52 |
return; |
| 53 |
} |
| 54 |
if (! defined $password ) { |
| 55 |
warn "->send_sms(_password) must be defined!"; |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
#Prevent injection attack |
| 60 |
$self->{_login} =~ s/'//g; |
| 61 |
$self->{_password} =~ s/'//g; |
| 62 |
|
| 63 |
# Create the object |
| 64 |
my $self = bless {}, $class; |
| 65 |
|
| 66 |
$self->{UserAgent} = LWP::UserAgent->new(timeout => 5); |
| 67 |
$self->{_login} = $username; |
| 68 |
$self->{_password} = $password; |
| 69 |
|
| 70 |
return $self; |
| 71 |
} |
| 72 |
|
| 73 |
sub send_sms { |
| 74 |
my $self = shift; |
| 75 |
my $params = {@_}; |
| 76 |
my $message = $params->{text}; |
| 77 |
my $recipientNumber = $params->{to}; |
| 78 |
|
| 79 |
if (! defined $message ) { |
| 80 |
warn "->send_sms(text) must be defined!"; |
| 81 |
return; |
| 82 |
} |
| 83 |
if (! defined $recipientNumber ) { |
| 84 |
warn "->send_sms(to) must be defined!"; |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
#Prevent injection attack! |
| 89 |
$recipientNumber =~ s/'//g; |
| 90 |
$message =~ s/(")|(\$\()|(`)/\\"/g; #Sanitate " so it won't break the system( iconv'ed curl command ) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
my $base_url = "https://gw.labyrintti.com:28443/sendsms"; |
| 95 |
my $parameters = { |
| 96 |
'user' => $self->{_login}, |
| 97 |
'password' => $self->{_password}, |
| 98 |
'dests' => $recipientNumber, |
| 99 |
'text' => $message, |
| 100 |
'unicode' => 'yes', |
| 101 |
}; |
| 102 |
|
| 103 |
my $report_url = C4::Context->config('smsProviders')->{'labyrintti'}->{'reportUrl'}; |
| 104 |
if ($report_url) { |
| 105 |
my $msg_id = $params->{_message_id}; |
| 106 |
$report_url =~ s/\{messagenumber\}/$msg_id/g; |
| 107 |
$parameters->{'report'} = $report_url; |
| 108 |
} |
| 109 |
|
| 110 |
my $lwpcurl = LWP::Curl->new(auto_encode => 0); |
| 111 |
my $return = $lwpcurl->post($base_url, $parameters); |
| 112 |
|
| 113 |
if ($lwpcurl->{retcode} == 6) { |
| 114 |
Koha::Exception::ConnectionFailed->throw(error => "Connection failed"); |
| 115 |
} |
| 116 |
|
| 117 |
my $delivery_note = $return; |
| 118 |
|
| 119 |
return 1 if ($return =~ m/OK [1-9](\d*)?/); |
| 120 |
|
| 121 |
# remove everything except the delivery note |
| 122 |
$delivery_note =~ s/^(.*)message\sfailed:\s*//g; |
| 123 |
|
| 124 |
# pass on the error by throwing an exception - it will be eventually caught |
| 125 |
# in C4::Letters::_send_message_by_sms() |
| 126 |
Koha::Exception::SMSDeliveryFailure->throw(error => $delivery_note); |
| 127 |
} |
| 128 |
|
| 129 |
1; |