/
10_php_confoptions_override.pl
119 lines (102 loc) · 4.67 KB
/
10_php_confoptions_override.pl
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
# i-MSCP Listener::Php::ConfOptions::Override listener file
# Copyright (C) 2016-2017 Laurent Declercq <l.declercq@nuxwin.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
## Allows to add or override PHP configuration options globally or per domain.
##
## Be aware that only Fcgid and PHP-FPM Apache2 httpd server implementations are supported.
## Note: When you want operate on a per domain basis, don't forget to set the PHP configuration level to 'per_site'.
## You can do this by running: perl /var/www/imscp/engine/setup/imscp-reconfigure -dar php
#
package Listener::Php::ConfOptions::Override;
use strict;
use warnings;
use iMSCP::EventManager;
#
## Configuration parameters
#
# Add or overrides the PHP configuration options globally or per domain.
# - The per domain PHP configuration options take precedence over global PHP configuration options.
# - The PHP configuration options take precedence over those which are defined through the i-MSCP PHP editor.
#
# Placeholders that can be used in PHP option values:
#
# {HOME_DIR} Will be replaced by client homedir path
# {PEAR_DIR} Will be replaced by PHP Pear directory path
# {TMPDIR} Will be replaced by PHP temporary directory
#
# Note that domain names must be in ASCII format.
my %configOptions = (
# Any PHP configuration option added here will apply globally (to all domains).
'*' => {
'<option_name1>' => '<option_value1>',
'<option_name2>' => '<option_value2>'
},
# Any PHP configuration added here will apply to domain1.tld only
'test.domain.tld' => {
'option_name1' => 'option_value1',
'option_name2' => 'option_value2'
}
);
#
## Please, don't edit anything below this line
#
iMSCP::EventManager->getInstance()->register(
'beforeHttpdBuildConfFile',
sub {
my ($tplContent, $tplName, $data) = @_;
if ($tplName eq 'php.ini' && $main::imscpConfig{'HTTPD_SERVER'} eq 'apache_fcgid') {
# Apply global PHP configuration options overriding if any
if (exists $configOptions{'*'}) {
while(my ($option, $value) = each( %{$configOptions{'*'}} )) {
$$tplContent .= "$option = $value\n" unless $$tplContent =~ s/^$option\s+=.*/$option = $value/gim;
}
}
# Apply per domain PHP configuration options overriding if any
if (exists $configOptions{$data->{'DOMAIN_NAME'}}) {
while(my ($option, $value) = each( %{$configOptions{$data->{'DOMAIN_NAME'}}} )) {
$$tplContent .= "$option = $value\n" unless $$tplContent =~ s/^$option\s+=.*/$option = $value/gim;
}
}
return 0;
}
return 0 unless $tplName eq 'pool.conf' && $main::imscpConfig{'HTTPD_SERVER'} eq 'apache_php_fpm';
# Apply global PHP configuration options overriding if any
if (exists $configOptions{'*'}) {
while(my ($option, $value) = each( %{$configOptions{'*'}} )) {
next if $$tplContent =~ s/^(php_(?:admin_)?(?:value|flag)\[$option\]).*/$1 = $value/gim;
if (grep($_ eq lc( $value ), ( 'on', 'off', '1', '0', 'true', 'false', 'yes', 'no' ))) {
$$tplContent .= "php_admin_flag[$option] = $value\n";
} else {
$$tplContent .= "php_admin_value[$option] = $value\n";
}
}
}
return 0 unless exists $configOptions{$data->{'DOMAIN_NAME'}};
# Apply per domain PHP configuration options overriding if any
while(my ($option, $value) = each( %{$configOptions{$data->{'DOMAIN_NAME'}}} )) {
next if $$tplContent =~ s/^(php_(?:admin_)?(?:value|flag)\[$option\]).*/$1 = $value/gim;
if (grep($_ eq lc( $value ), ( 'on', 'off', '1', '0', 'true', 'false', 'yes', 'no' ))) {
$$tplContent .= "php_admin_flag[$option] = $value\n";
} else {
$$tplContent .= "php_admin_value[$option] = $value\n";
}
}
0;
}
);
1;
__END__