Advertisement
Guest User

Untitled

a guest
Apr 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  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 $from = $params->{_from};
  48. my $dbh=C4::Context->dbh;
  49. my $branches=$dbh->prepare("SELECT branchcode FROM branches WHERE branchemail = ?;");
  50. $branches->execute($from);
  51. my $branch = $branches->fetchrow;
  52. my $prefix = substr($branch, 0, 3);
  53. my $group_branch = C4::Context->config('smsProviders')->{'labyrintti'}->{$prefix}->{'user'};
  54. my $single_branch = C4::Context->config('smsProviders')->{'labyrintti'}->{$branch}->{'user'};
  55.  
  56. my $username;
  57. my $password;
  58.  
  59. if($single_branch) {
  60. $username = $params->{_login} ? $params->{_login} : C4::Context->config('smsProviders')->{'labyrintti'}->{$branch}->{'user'};
  61. $password = $params->{_password} ? $params->{_password} : C4::Context->config('smsProviders')->{'labyrintti'}->{$branch}->{'passwd'};
  62. }elsif($group_branch) {
  63. $username = $params->{_login} ? $params->{_login} : C4::Context->config('smsProviders')->{'labyrintti'}->{$prefix}->{'user'};
  64. $password = $params->{_password} ? $params->{_password} : C4::Context->config('smsProviders')->{'labyrintti'}->{$prefix}->{'passwd'};
  65. }else{
  66. $username = $params->{_login} ? $params->{_login} : C4::Context->config('smsProviders')->{'labyrintti'}->{'user'};
  67. $password = $params->{_password} ? $params->{_password} : C4::Context->config('smsProviders')->{'labyrintti'}->{'passwd'};
  68. }
  69.  
  70.  
  71. if (! defined $username ) {
  72. warn "->send_sms(_login) must be defined!";
  73. return;
  74. }
  75. if (! defined $password ) {
  76. warn "->send_sms(_password) must be defined!";
  77. return;
  78. }
  79.  
  80. #Prevent injection attack
  81. $self->{_login} =~ s/'//g;
  82. $self->{_password} =~ s/'//g;
  83.  
  84. # Create the object
  85. my $self = bless {}, $class;
  86.  
  87. $self->{UserAgent} = LWP::UserAgent->new(timeout => 5);
  88. $self->{_login} = $username;
  89. $self->{_password} = $password;
  90.  
  91. return $self;
  92. }
  93.  
  94. sub send_sms {
  95. my $self = shift;
  96. my $params = {@_};
  97. my $message = $params->{text};
  98. my $recipientNumber = $params->{to};
  99.  
  100. if (! defined $message ) {
  101. warn "->send_sms(text) must be defined!";
  102. return;
  103. }
  104. if (! defined $recipientNumber ) {
  105. warn "->send_sms(to) must be defined!";
  106. return;
  107. }
  108.  
  109. #Prevent injection attack!
  110. $recipientNumber =~ s/'//g;
  111. $message =~ s/(")|(\$\()|(`)/\\"/g; #Sanitate " so it won't break the system( iconv'ed curl command )
  112.  
  113. my $base_url = "https://gw.labyrintti.com:28443/sendsms";
  114. my $parameters = {
  115. 'user' => $self->{_login},
  116. 'password' => $self->{_password},
  117. 'dests' => $recipientNumber,
  118. 'text' => Encode::encode( "utf8", $message),
  119. };
  120.  
  121. # check if we need to use unicode
  122. # -> if unicode => yes, maxlength for 1 sms = 70 chars
  123. # -> else maxlenght = 160 chars (140 bytes, GSM 03.38)
  124. my $gsm0388 = decode("gsm0338",encode("gsm0338", $message));
  125.  
  126. if ($message ne $gsm0388 and C4::Context->config('smsProviders')->{'labyrintti'}->{'Unicode'} eq "yes"){
  127. $parameters->{'unicode'} = 'yes';
  128. C4::Letters::UpdateQueuedMessage({
  129. message_id => $params->{_message_id},
  130. metadata => 'UTF-16',
  131. });
  132. } else {
  133. $parameters->{'unicode'} = 'no';
  134. }
  135.  
  136. if (C4::Context->config('smsProviders')->{'labyrintti'}->{'sourceName'}) {
  137. $parameters->{'source-name'} = C4::Context->config('smsProviders')->{'labyrintti'}->{'sourceName'};
  138. }
  139.  
  140. my $report_url = C4::Context->config('smsProviders')->{'labyrintti'}->{'reportUrl'};
  141. if ($report_url) {
  142. my $msg_id = $params->{_message_id};
  143. $report_url =~ s/\{messagenumber\}/$msg_id/g;
  144. $parameters->{'report'} = $report_url;
  145. }
  146.  
  147. my $lwpcurl = LWP::Curl->new();
  148. my $return = $lwpcurl->post($base_url, $parameters);
  149.  
  150. if ($lwpcurl->{retcode} == 6) {
  151. Koha::Exception::ConnectionFailed->throw(error => "Connection failed");
  152. }
  153.  
  154. my $delivery_note = $return;
  155.  
  156. return 1 if ($return =~ m/OK [1-9](\d*)?/);
  157.  
  158. # remove everything except the delivery note
  159. $delivery_note =~ s/^(.*)message\sfailed:\s*//g;
  160.  
  161. # pass on the error by throwing an exception - it will be eventually caught
  162. # in C4::Letters::_send_message_by_sms()
  163. Koha::Exception::SMSDeliveryFailure->throw(error => $delivery_note);
  164. }
  165.  
  166. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement