epic-s6ts
[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   my $config           = $::lx_office_conf{cti} || {};
34
35   if ($config->{dial_command}) {
36     return "controller.pl?action=CTI/call&number=" . uri_encode($class->sanitize_number(number => $params{number})) . ($params{internal} ? '&internal=1' : '');
37   } else {
38     return 'callto://' . uri_encode($class->sanitize_number(number => $params{number}));
39   }
40 }
41
42 sub sanitize_number {
43   my ($class, %params) = @_;
44
45   my $config           = $::lx_office_conf{cti} || {};
46   my $idp              = $config->{international_dialing_prefix} // '00';
47
48   my $number           = $params{number} // '';
49   $number              =~ s/[^0-9+]//g;                                        # delete unsupported characters
50   my $countrycode      = $number =~ s/^(?: $idp | \+ ) ( \d{2} )//x ? $1 : ''; # TODO: countrycodes can have more or less than 2 digits
51   $number              =~ s/^0//x if $countrycode;                             # kill non standard optional zero after global identifier
52   $number              =~ s{[^0-9]+}{}g;
53
54   return '' unless $number;
55
56   return ($countrycode ? $idp . $countrycode : '') . $number;
57 }
58
59 1;