/
Subdomain.pm
296 lines (241 loc) · 10.8 KB
/
Subdomain.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
=head1 NAME
Modules::Subdomain - i-MSCP Subdomain module
=cut
# i-MSCP - internet Multi Server Control Panel
# Copyright (C) 2010-2019 by Laurent Declercq <l.declercq@nuxwin.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package Modules::Subdomain;
use strict;
use warnings;
use File::Spec;
use iMSCP::Boolean;
use iMSCP::Debug qw/ debug error getLastError warning /;
use Net::LibIDN 'idn_to_unicode';
use Servers::httpd;
use parent 'Modules::Abstract';
=head1 DESCRIPTION
i-MSCP Subdomain module.
=head1 PUBLIC METHODS
=over 4
=item getType( )
Get module type
Return string Module type
=cut
sub getType
{
'Sub';
}
=item process( \%data )
Process module
Param hashref \%data Subdomain unique data
Return int 0 on success, other on failure
=cut
sub process
{
my ( $self, $data ) = @_;
my $rs = $self->_loadData( $data->{'id'} );
return $rs if $rs;
my @sql;
if ( $self->{'subdomain_status'} =~ /^to(?:add|change|enable)$/ ) {
$rs = $self->add();
@sql = ( 'UPDATE subdomain SET subdomain_status = ? WHERE subdomain_id = ?', undef,
( $rs ? getLastError( 'error' ) || 'Unknown error' : 'ok' ), $data->{'id'} );
} elsif ( $self->{'subdomain_status'} eq 'todelete' ) {
$rs = $self->delete();
@sql = $rs
? ( 'UPDATE subdomain SET subdomain_status = ? WHERE subdomain_id = ?', undef,
( getLastError( 'error' ) || 'Unknown error' ), $data->{'id'} )
: ( 'DELETE FROM subdomain WHERE subdomain_id = ?', undef, $data->{'id'} );
} elsif ( $self->{'subdomain_status'} eq 'todisable' ) {
$rs = $self->disable();
@sql = ( 'UPDATE subdomain SET subdomain_status = ? WHERE subdomain_id = ?', undef,
( $rs ? getLastError( 'error' ) || 'Unknown error' : 'disabled' ), $data->{'id'} );
} elsif ( $self->{'subdomain_status'} eq 'torestore' ) {
$rs = $self->restore();
@sql = ( 'UPDATE subdomain SET subdomain_status = ? WHERE subdomain_id = ?', undef,
( $rs ? getLastError( 'error' ) || 'Unknown error' : 'ok' ), $data->{'id'} );
} else {
warning(
sprintf( 'Unknown action (%s) for subdomain (ID %d)', $self->{'subdomain_alias_status'}, $data->{'id'} )
);
return 0;
}
local $@;
eval { $self->{'_dbh'}->do( @sql ); };
if ( $@ ) {
error( $@ );
return 1;
}
$rs;
}
=back
=head1 PRIVATE METHODS
=over 4
=item _loadData( $subdomainId )
Load data
Param int $subdomainId Subdomain unique identifier
Return int 0 on success, other on failure
=cut
sub _loadData
{
my ( $self, $subdomainId ) = @_;
local $@;
eval {
my $row = $self->{'_dbh'}->selectrow_hashref(
"
SELECT t1.*,
t2.domain_name AS user_home, t2.domain_admin_id, t2.domain_mailacc_limit, t2.domain_php, t2.domain_cgi,
t2.external_mail, t2.web_folder_protection,
IFNULL(t3.ip_number, '0.0.0.0') AS ip_number,
t4.private_key, t4.certificate, t4.ca_bundle, t4.allow_hsts, t4.hsts_max_age,
t4.hsts_include_subdomains,
t5.mail_on_domain
FROM subdomain AS t1
JOIN domain AS t2 USING(domain_id)
JOIN server_ips AS t3 ON (t3.ip_id = t2.domain_ip_id)
LEFT JOIN ssl_certs AS t4 ON(t4.domain_id = t1.subdomain_id AND t4.domain_type = 'sub' AND t4.status = 'ok')
LEFT JOIN (
SELECT sub_id, COUNT(sub_id) AS mail_on_domain
FROM mail_users
WHERE mail_type LIKE 'subdom\\_%'
GROUP BY sub_id
) AS t5 ON (t5.sub_id = t1.subdomain_id)
WHERE t1.subdomain_id = ?
",
undef,
$subdomainId
);
$row or die( sprintf( 'Data not found for subdomain (ID %d)', $subdomainId ));
%{ $self } = ( %{ $self }, %{ $row } );
};
if ( $@ ) {
error( $@ );
return 1;
}
0;
}
=item _getData( $action )
Data provider method for servers and packages
Param string $action Action
Return hashref Reference to a hash containing data, die on failure
=cut
sub _getData
{
my ( $self, $action ) = @_;
$self->{'_data'} = do {
my $httpd = Servers::httpd->factory();
my $groupName = my $userName = $::imscpConfig{'SYSTEM_USER_PREFIX'}
. ( $::imscpConfig{'SYSTEM_USER_MIN_UID'}+$self->{'domain_admin_id'} );
my $homeDir = File::Spec->canonpath( "$::imscpConfig{'USER_WEB_DIR'}/$self->{'user_home'}" );
my $webDir = File::Spec->canonpath( "$homeDir/$self->{'subdomain_mount'}" );
my $documentRoot = File::Spec->canonpath( "$webDir/$self->{'subdomain_document_root'}" );
my $confLevel = $httpd->{'phpConfig'}->{'PHP_CONFIG_LEVEL'};
$confLevel = $confLevel =~ /^per_(?:user|domain)$/ ? 'dmn' : 'sub';
my $phpini = $self->{'_dbh'}->selectrow_hashref(
'SELECT * FROM php_ini WHERE domain_id = ? AND domain_type = ?',
undef, ( $confLevel eq 'dmn' ? $self->{'domain_id'} : $self->{'subdomain_id'} ), $confLevel
) || {};
my $haveCert = defined $self->{'certificate'}
&& -f "$::imscpConfig{'GUI_ROOT_DIR'}/data/certs/$self->{'subdomain_name'}.$self->{'user_home'}.pem";
my $allowHSTS = $haveCert && $self->{'allow_hsts'} eq 'on';
my $hstsMaxAge = $allowHSTS ? $self->{'hsts_max_age'} : 0;
my $hstsIncludeSubDomains = $allowHSTS && $self->{'hsts_include_subdomains'} eq 'on'
? '; includeSubDomains'
: ( $allowHSTS ? '' : '; includeSubDomains' );
{
ACTION => $action,
STATUS => $self->{'subdomain_status'},
BASE_SERVER_VHOST => $::imscpConfig{'BASE_SERVER_VHOST'},
BASE_SERVER_IP => $::imscpConfig{'BASE_SERVER_IP'},
BASE_SERVER_PUBLIC_IP => $::imscpConfig{'BASE_SERVER_PUBLIC_IP'},
DOMAIN_ADMIN_ID => $self->{'domain_admin_id'},
DOMAIN_ID => $self->{'subdomain_id'},
DOMAIN_NAME => $self->{'subdomain_name'} . '.' . $self->{'user_home'},
DOMAIN_NAME_UNICODE => idn_to_unicode( $self->{'subdomain_name'} . '.' . $self->{'user_home'}, 'utf-8' ),
DOMAIN_IP => $self->{'ip_number'},
DOMAIN_TYPE => 'sub',
PARENT_DOMAIN_NAME => $self->{'user_home'},
ROOT_DOMAIN_NAME => $self->{'user_home'},
HOME_DIR => $homeDir,
WEB_DIR => $webDir,
MOUNT_POINT => $self->{'subdomain_mount'},
DOCUMENT_ROOT => $documentRoot,
SHARED_MOUNT_POINT => $self->_sharedMountPoint(),
PEAR_DIR => $httpd->{'phpConfig'}->{'PHP_PEAR_DIR'},
TIMEZONE => $::imscpConfig{'TIMEZONE'},
USER => $userName,
GROUP => $groupName,
PHP_SUPPORT => $self->{'domain_php'},
CGI_SUPPORT => $self->{'domain_cgi'},
WEB_FOLDER_PROTECTION => $self->{'web_folder_protection'},
SSL_SUPPORT => $haveCert,
HSTS_SUPPORT => $allowHSTS,
HSTS_MAX_AGE => $hstsMaxAge,
HSTS_INCLUDE_SUBDOMAINS => $hstsIncludeSubDomains,
FORWARD => $self->{'subdomain_url_forward'} || 'no',
FORWARD_TYPE => $self->{'subdomain_type_forward'} || '',
FORWARD_PRESERVE_HOST => $self->{'subdomain_host_forward'} || 'Off',
DISABLE_FUNCTIONS => $phpini->{'disable_functions'} || '',
MAX_EXECUTION_TIME => $phpini->{'max_execution_time'} || 30,
MAX_INPUT_TIME => $phpini->{'max_input_time'} || 60,
MEMORY_LIMIT => $phpini->{'memory_limit'} || 128,
ERROR_REPORTING => $phpini->{'error_reporting'} || 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
DISPLAY_ERRORS => $phpini->{'display_errors'} || 'off',
POST_MAX_SIZE => $phpini->{'post_max_size'} || 8,
UPLOAD_MAX_FILESIZE => $phpini->{'upload_max_filesize'} || 2,
ALLOW_URL_FOPEN => $phpini->{'allow_url_fopen'} || 'off',
PHP_FPM_LISTEN_PORT => ( $phpini->{'id'} // 1 )-1,
EXTERNAL_MAIL => $self->{'external_mail'},
MAIL_ENABLED => ( $self->{'external_mail'} eq 'off' && ( $self->{'mail_on_domain'} || $self->{'domain_mailacc_limit'} >= 0 ) ),
WILDCARD_ALIAS => $self->{'subdomain_wildcard_alias'} // 'no'
}
} unless %{ $self->{'_data'} };
$self->{'_data'};
}
=item _sharedMountPoint( )
Does this subdomain share mount point with another domain?
Return bool, die on failure
=cut
sub _sharedMountPoint
{
my ( $self ) = @_;
my $regexp = "^$self->{'subdomain_mount'}(/.*|\$)";
my ( $nbSharedMountPoints ) = $self->{'_dbh'}->selectrow_array(
"
SELECT COUNT(mount_point) AS nb_mount_points FROM (
SELECT alias_mount AS mount_point FROM domain_aliasses
WHERE domain_id = ? AND alias_status NOT IN ('todelete', 'ordered') AND alias_mount RLIKE ?
UNION ALL
SELECT subdomain_mount AS mount_point FROM subdomain
WHERE subdomain_id <> ? AND domain_id = ? AND subdomain_status <> 'todelete' AND subdomain_mount RLIKE ?
UNION ALL
SELECT subdomain_alias_mount AS mount_point FROM subdomain_alias
WHERE subdomain_alias_status <> 'todelete'
AND alias_id IN (SELECT alias_id FROM domain_aliasses WHERE domain_id = ?)
AND subdomain_alias_mount RLIKE ?
) AS tmp
",
undef, $self->{'domain_id'}, $regexp, $self->{'subdomain_id'}, $self->{'domain_id'}, $regexp,
$self->{'domain_id'}, $regexp
);
( $nbSharedMountPoints || $self->{'subdomain_mount'} eq '/' );
}
=back
=head1 AUTHOR
Laurent Declercq <l.declercq@nuxwin.com>
=cut
1;
__END__