3bc13860e67b1ce3ed2417460141954fd88b34e8
[kivitendo-erp.git] / SL / CTI.pm
1 package SL::CTI;
2
3 use strict;
4
5 use String::ShellQuote;
6
7 use SL::MoreCommon qw(uri_encode);
8
9 sub call {
10   my ($class, %params) = @_;
11
12   my $config           = $::lx_office_conf{cti}  || {};
13   my $command          = $config->{dial_command} || die $::locale->text('Dial command missing in kivitendo configuration\'s [cti] section');
14   my $external_prefix  = $params{internal} ? '' : ($config->{external_prefix} // '');
15
16   my %command_args     = (
17     phone_extension    => $::myconfig{phone_extension} || die($::locale->text('Phone extension missing in user configuration')),
18     phone_password     => $::myconfig{phone_password}  || die($::locale->text('Phone password missing in user configuration')),
19     number             => $external_prefix . $class->sanitize_number(%params),
20   );
21
22   foreach my $key (keys %command_args) {
23     my $value = shell_quote($command_args{$key});
24     $command  =~ s{<\% ${key} \%>}{$value}gx;
25   }
26
27   return `$command`;
28 }
29
30 sub call_link {
31   my ($class, %params) = @_;
32
33   return "controller.pl?action=CTI/call&number=" . uri_encode($class->sanitize_number(number => $params{number})) . ($params{internal} ? '&internal=1' : '');
34 }
35
36 sub sanitize_number {
37   my ($class, %params) = @_;
38
39   my $config           = $::lx_office_conf{cti} || {};
40   my $idp              = $config->{international_dialing_prefix} // '00';
41
42   my $number           = $params{number} // '';
43   $number              =~ s/[^0-9+]//g;                                        # delete unsupported characters
44   my $countrycode      = $number =~ s/^(?: $idp | \+ ) ( \d{2} )//x ? $1 : ''; # TODO: countrycodes can have more or less than 2 digits
45   $number              =~ s/^0//x if $countrycode;                             # kill non standard optional zero after global identifier
46   $number              =~ s{[^0-9]+}{}g;
47
48   return '' unless $number;
49
50   return ($countrycode ? $idp . $countrycode : '') . $number;
51 }
52
53 1;