19458a23a42cb3e00d7ed34504e069bfe988ce2d
[koha.git] / admin / smtp_servers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2020 Theke Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use Scalar::Util qw(blessed);
24 use Try::Tiny;
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28
29 use Koha::Libraries;
30 use Koha::SMTP::Servers;
31
32 my $input = CGI->new;
33 my $op    = $input->param('op') || 'list';
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {   template_name   => "admin/smtp_servers.tt",
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { parameters => 'manage_smtp_servers' },
40     }
41 );
42
43 my @messages;
44
45 if ( $op eq 'add' ) {
46
47     my $name       = $input->param('smtp_name');
48     my $host       = $input->param('smtp_host');
49     my $port       = $input->param('smtp_port') || 25;
50     my $timeout    = $input->param('smtp_timeout') || 120;
51     my $ssl_mode   = $input->param('smtp_ssl_mode');
52     my $user_name  = $input->param('smtp_user_name') || undef;
53     my $password   = $input->param('smtp_password') || undef;
54     my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
55
56     try {
57         Koha::SMTP::Server->new(
58             {
59                 name       => $name,
60                 host       => $host,
61                 port       => $port,
62                 timeout    => $timeout,
63                 ssl_mode   => $ssl_mode,
64                 user_name  => $user_name,
65                 password   => $password,
66                 debug      => $debug,
67             }
68         )->store;
69
70         push @messages, { type => 'message', code => 'success_on_insert' };
71     }
72     catch {
73         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
74             push @messages,
75               {
76                 type   => 'alert',
77                 code   => 'error_on_insert',
78                 reason => 'duplicate_id'
79               };
80         }
81     };
82
83     # list servers after adding
84     $op = 'list';
85 }
86 elsif ( $op eq 'edit_form' ) {
87     my $smtp_server_id = $input->param('smtp_server_id');
88     my $smtp_server;
89
90     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
91         unless !$smtp_server_id;
92
93     if ( $smtp_server ) {
94         $template->param(
95             smtp_server => $smtp_server
96         );
97     }
98     else {
99         push @messages,
100             {
101                 type   => 'alert',
102                 code   => 'error_on_edit',
103                 reason => 'invalid_id'
104             };
105     }
106 }
107 elsif ( $op eq 'edit_save' ) {
108
109     my $smtp_server_id = $input->param('smtp_server_id');
110     my $smtp_server;
111
112     $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
113         unless !$smtp_server_id;
114
115     if ( $smtp_server ) {
116
117         my $name       = $input->param('smtp_name');
118         my $host       = $input->param('smtp_host');
119         my $port       = $input->param('smtp_port') || 25;
120         my $timeout    = $input->param('smtp_timeout') || 120;
121         my $ssl_mode   = $input->param('smtp_ssl_mode');
122         my $user_name  = $input->param('smtp_user_name') || undef;
123         my $password   = $input->param('smtp_password') || undef;
124         my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
125
126         try {
127             $smtp_server->password( $password )
128                 if defined $password and $password ne '****'
129                     or not defined $password;
130
131             $smtp_server->set(
132                 {
133                     name      => $name,
134                     host      => $host,
135                     port      => $port,
136                     timeout   => $timeout,
137                     ssl_mode  => $ssl_mode,
138                     user_name => $user_name,
139                     debug     => $debug
140                 }
141             )->store;
142
143             push @messages,
144             {
145                 type => 'message',
146                 code => 'success_on_update'
147             };
148         }
149         catch {
150             push @messages,
151             {
152                 type   => 'alert',
153                 code   => 'error_on_update'
154             };
155         };
156
157         # list servers after adding
158         $op = 'list';
159     }
160     else {
161         push @messages,
162             {
163                 type   => 'alert',
164                 code   => 'error_on_update',
165                 reason => 'invalid_id'
166             };
167     }
168 }
169
170 if ( $op eq 'list' ) {
171     my $smtp_servers = Koha::SMTP::Servers->search;
172     $template->param(
173         servers_count  => $smtp_servers->count,
174         default_config => $smtp_servers->get_default,
175     );
176 }
177
178 $template->param(
179     op       => $op,
180     messages => \@messages,
181 );
182
183 output_html_with_http_headers $input, $cookie, $template->output;