Bug 36207: (RM follow-up) CSRF correction
[koha.git] / C4 / SMS.pm
1 package C4::SMS;
2
3 # Copyright 2007 Liblime
4 # Copyright 2015 Biblibre
5 # Copyright 2016 Catalyst
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 =head1 NAME
23
24 C4::SMS - send SMS messages
25
26 =head1 SYNOPSIS
27
28 my ( $success, $error ) = C4::SMS->send_sms(
29     {
30         message     => 'This is my text message',
31         destination => '212-555-1212'
32     }
33 );
34
35 =head1 DESCRIPTION
36
37 A wrapper for SMS::Send.
38
39 Can use a yaml file for config, the path to which is in the koha-conf.xml
40 <sms_send_config>__KOHA_CONF_DIR__/sms_send/</sms_send_config>
41
42 Each file needs to be in the format of
43 __KOHA_CONF_DIR__/sms_send/<driver>.yaml
44
45 For example for SMS::Send::UK::Kapow the config would be
46
47 /etc/koha/sites/instancename/sms_send/UK/Kapow.yaml for package install
48 or
49 /etc/koha/sms_send/UK/Kapow.yaml for tarball
50
51 A underscore character is prepended to all parameter names so they are
52 treated as driver-specific options (leading underscore must not appear
53 in config file).
54
55 =cut
56
57 use strict;
58 use warnings;
59
60 use C4::Context;
61 use File::Spec;
62
63
64
65 =head1 METHODS
66
67 =cut
68
69 # The previous implmentation used username and password.
70 # our $user = C4::Context->config('smsuser');
71 # our $pwd  = C4::Context->config('smspass');
72
73 =head2 send_sms
74
75 =cut
76
77 sub send_sms {
78     my $self = shift;
79     my $params= shift;
80
81     foreach my $required_parameter ( qw( message destination ) ) {
82         # Should I warn in some way?
83         return unless defined $params->{ $required_parameter };
84     }
85
86     eval { require SMS::Send; };
87     if ( $@ ) {
88         # we apparently don't have SMS::Send. Return a failure.
89         return;
90     }
91
92     # This allows the user to override the driver. See SMS::Send::Test
93     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
94     return ( undef, 'SMS_SEND_DRIVER_MISSING' ) unless $driver;
95
96     my ($sent, $sender);
97
98     my $subpath = $driver;
99     $subpath =~ s|::|/|g;
100
101     # Extract additional SMS::Send arguments from file
102     my $sms_send_config = C4::Context->config('sms_send_config');
103     my $conf_file = defined $sms_send_config
104         ? File::Spec->catfile( $sms_send_config, $subpath )
105         : $subpath;
106     $conf_file .= q{.yaml};
107
108     my %args = ();
109     if ( -f $conf_file ) {
110         require YAML::XS;
111         my $conf = YAML::XS::LoadFile( $conf_file );
112         %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
113     }
114
115     # Extract additional SMS::Send arguments from the syspref
116     # Merge with any arguments from file with syspref taking precedence
117     if ( C4::Context->preference('SMSSendAdditionalOptions') ) {
118         my $sms_send_config_syspref = C4::Context->yaml_preference('SMSSendAdditionalOptions');
119         %args = ( %args, %$sms_send_config_syspref ) if $sms_send_config_syspref;
120     }
121
122     eval {
123         # Create a sender
124         $sender = SMS::Send->new(
125             $driver,
126             _login    => C4::Context->preference('SMSSendUsername'),
127             _password => C4::Context->preference('SMSSendPassword'),
128             %args,
129         );
130
131         # Send a message
132         $sent = $sender->send_sms(
133             to   => $params->{destination},
134             text => $params->{message},
135         );
136     };
137
138     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
139     #Catch those errors and fail the sms-sending gracefully.
140     if ($@) {
141         warn $@;
142         return ( undef, $@ );
143     }
144     # warn 'failure' unless $sent;
145     return $sent;
146 }
147
148 =head2 driver
149
150 =cut
151
152 sub driver {
153     my $self = shift;
154
155     # return 'US::SprintPCS';
156     return C4::Context->preference('SMSSendDriver');
157
158 }
159
160 1;
161
162 __END__
163