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