Skip to content

Commit 126bdac

Browse files
author
mcramer
committed
Implemented: FS#2546 - Update all custom php.ini files for webs on main php.ini change
1 parent 68ec437 commit 126bdac

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed

server/plugins-available/apache2_plugin.inc.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,97 @@ function onLoad() {
8888

8989
$app->plugins->registerEvent('ftp_user_delete',$this->plugin_name,'ftp_user_delete');
9090

91+
$app->plugins->registerAction('php_ini_changed', $this->plugin_name, 'php_ini_changed');
9192
}
93+
94+
// check for php.ini changes
95+
96+
97+
// Handle php.ini changes
98+
function php_ini_changed($event_name, $data) {
99+
global $app, $conf;
100+
101+
$app->uses('getconf');
102+
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
103+
$fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
104+
105+
/* $data contains an array with these keys:
106+
* file -> full path of changed php_ini
107+
* mode -> web_domain php modes to change (mod, fast-cgi, php-fpm or '' for all except 'mod')
108+
* php_version -> php ini path that changed (additional php versions)
109+
*/
110+
111+
$qrystr = "SELECT * FROM web_domain WHERE custom_php_ini != ''";
112+
if($data['mode'] == 'mod') {
113+
$qrystr .= " AND php = 'mod'";
114+
} elseif($data['mode'] == 'fast-cgi') {
115+
$qrystr .= " AND php = 'fast-cgi'";
116+
if($data['php_version']) {
117+
$qrystr .= " AND fastcgi_php_version LIKE '%:" . $app->db->quote($data['php_version']) . "'";
118+
}
119+
} elseif($data['mode'] == 'php-fpm') {
120+
$qrystr .= " AND php = 'php-fpm'";
121+
if($data['php_version']) {
122+
$qrystr .= " AND fastcgi_php_version LIKE '%:" . $app->db->quote($data['php_version']) . ":%'";
123+
}
124+
} else {
125+
$qrystr .= " AND php != 'mod' AND php != 'fast-cgi'";
126+
}
127+
128+
129+
//** Get all the webs
130+
$web_domains = $app->db->queryAllRecords($qrystr);
131+
foreach($web_domains as $web_data) {
132+
$custom_php_ini_dir = $web_config['website_basedir'].'/conf/'.$web_data['system_user'];
133+
$web_folder = 'web';
134+
if($web_data['type'] == 'vhostsubdomain') {
135+
$web_folder = $web_data['web_folder'];
136+
$custom_php_ini_dir .= '_' . $web_folder;
137+
}
138+
if(!is_dir($web_config['website_basedir'].'/conf')) $app->system->mkdir($web_config['website_basedir'].'/conf');
139+
92140

141+
if(!is_dir($custom_php_ini_dir)) $app->system->mkdir($custom_php_ini_dir);
142+
$php_ini_content = '';
143+
if($web_data['php'] == 'mod') {
144+
$master_php_ini_path = $web_config['php_ini_path_apache'];
145+
} else {
146+
if($web_data['php'] == 'fast-cgi' && file_exists($fastcgi_config["fastcgi_phpini_path"])) {
147+
$master_php_ini_path = $fastcgi_config["fastcgi_phpini_path"];
148+
} else {
149+
$master_php_ini_path = $web_config['php_ini_path_cgi'];
150+
}
151+
}
152+
if($master_php_ini_path != '' && substr($master_php_ini_path,-7) == 'php.ini' && is_file($master_php_ini_path)) {
153+
$php_ini_content .= $app->system->file_get_contents($master_php_ini_path)."\n";
154+
}
155+
$php_ini_content .= str_replace("\r",'',trim($web_data['custom_php_ini']));
156+
$app->system->file_put_contents($custom_php_ini_dir.'/php.ini',$php_ini_content);
157+
$app->log('Info: rewrote custom php.ini for web ' . $web_data['domain_id'] . ' (' . $web_data['domain'] . ').',LOGLEVEL_DEBUG);
158+
}
159+
160+
if(count($web_domains) > 0) {
161+
//* We do not check the apache config here - we only changed the php.ini
162+
//* Check if this is a chrooted setup
163+
if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
164+
$apache_chrooted = true;
165+
$app->log('Info: Apache is chrooted.',LOGLEVEL_DEBUG);
166+
} else {
167+
$apache_chrooted = false;
168+
}
169+
170+
$app->log('Info: rewrote all php.ini and reloading apache now.',LOGLEVEL_DEBUG);
171+
if($apache_chrooted) {
172+
$app->services->restartServiceDelayed('httpd','restart');
173+
} else {
174+
// request a httpd reload when all records have been processed
175+
$app->services->restartServiceDelayed('httpd','reload');
176+
}
177+
} else {
178+
$app->log('Info: No webs affected by php.ini change.',LOGLEVEL_DEBUG);
179+
}
180+
}
181+
93182
// Handle the creation of SSL certificates
94183
function ssl($event_name,$data) {
95184
global $app, $conf;
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh and Oliver Vogel www.muv.com
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class webserver_plugin {
32+
33+
var $plugin_name = 'webserver_plugin';
34+
var $class_name = 'webserver_plugin';
35+
36+
/**
37+
* This function is called during ispconfig installation to determine
38+
* if a symlink shall be created for this plugin.
39+
*/
40+
public function onInstall() {
41+
global $conf;
42+
43+
if($conf['services']['web'] == true) {
44+
return true;
45+
} else {
46+
return false;
47+
}
48+
}
49+
50+
/**
51+
* This function is called when the module is loaded
52+
*/
53+
public function onLoad() {
54+
global $app;
55+
56+
$app->plugins->registerAction('server_plugins_loaded', $this->plugin_name, 'check_phpini_changes');
57+
}
58+
59+
/**
60+
* This function is called when a change in one of the registered tables is detected.
61+
* The function then raises the events for the plugins.
62+
*/
63+
public function process($tablename, $action, $data) {
64+
// not needed
65+
}
66+
67+
/**
68+
* The method checks for a change of a php.ini file
69+
*/
70+
public function check_phpini_changes() {
71+
global $app, $conf;
72+
73+
//** check if the main php.ini of the system changed so we need to regenerate all custom php.inis
74+
$app->uses('getconf');
75+
76+
//** files to check
77+
$check_files = array();
78+
79+
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
80+
$fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
81+
82+
//** add default php.ini files to check
83+
$check_files[] = array('file' => $web_config['php_ini_path_apache'],
84+
'mode' => 'mod',
85+
'php_version' => ''); // default;
86+
87+
$check_files[] = array('file' => $web_config['php_ini_path_cgi'],
88+
'mode' => '', // all but 'mod' and 'fast-cgi'
89+
'php_version' => ''); // default;
90+
91+
if($fastcgi_config["fastcgi_phpini_path"] && $fastcgi_config["fastcgi_phpini_path"] != $web_config['php_ini_path_cgi']) {
92+
$check_files[] = array('file' => $fastcgi_config["fastcgi_phpini_path"],
93+
'mode' => 'fast-cgi',
94+
'php_version' => ''); // default;
95+
} else {
96+
$check_files[] = array('file' => $web_config['php_ini_path_cgi'],
97+
'mode' => 'fast-cgi', // all but 'mod'
98+
'php_version' => ''); // default;
99+
}
100+
101+
102+
//** read additional php versions of this server
103+
$php_versions = $app->db->queryAllRecords('SELECT server_php_id, php_fastcgi_ini_dir, php_fpm_ini_dir FROM server_php WHERE server_id = ' . intval($conf['server_id']));
104+
foreach($php_versions as $php) {
105+
if($php['php_fastcgi_ini_dir'] && $php['php_fastcgi_ini_dir'] . '/php.ini' != $web_config['php_ini_path_cgi']) {
106+
$check_files[] = array('file' => $php['php_fastcgi_ini_dir'] . '/php.ini',
107+
'mode' => 'fast-cgi',
108+
'php_version' => $php['php_fastcgi_ini_dir']);
109+
} elseif($php['php_fpm_ini_dir'] && $php['php_fpm_ini_dir'] . '/php.ini' != $web_config['php_ini_path_cgi']) {
110+
$check_files[] = array('file' => $php['php_fpm_ini_dir'] . '/php.ini',
111+
'mode' => 'php-fpm',
112+
'php_version' => $php['php_fpm_ini_dir']);
113+
}
114+
}
115+
unset($php_versions);
116+
117+
//** read md5sum status file
118+
$new_php_ini_md5 = array();
119+
$php_ini_md5 = array();
120+
$php_ini_changed = false;
121+
if(file_exists(SCRIPT_PATH . '/php.ini.md5sum')) $php_ini_md5 = unserialize(base64_decode(trim($app->system->file_get_contents(SCRIPT_PATH . '/php.ini.md5sum'))));
122+
if(!is_array($php_ini_md5)) $php_ini_md5 = array();
123+
124+
$processed = array();
125+
foreach($check_files as $file) {
126+
$file_path = $file['file'];
127+
if(substr($file_path, -8) !== '/php.ini') $file_path .= (substr($file_path, -1) !== '/' ? '/' : '') . 'php.ini';
128+
if(!file_exists($file_path)) continue;
129+
130+
//** check if this php.ini file was already processed (if additional php version uses same php.ini)
131+
$ident = $file_path . '::' . $file['mode'] . '::' . $file['php_version'];
132+
if(in_array($ident, $processed) == true) continue;
133+
$processed[] = $ident;
134+
135+
//** check if md5sum of file changed
136+
$file_md5 = md5_file($file_path);
137+
if(array_key_exists($file_path, $php_ini_md5) == false || $php_ini_md5[$file_path] != $file_md5) {
138+
$php_ini_changed = true;
139+
140+
$app->log('Info: PHP.ini changed: ' . $file_path . ', mode ' . $file['mode'] . ' vers ' . $file['php_version'] . '.',LOGLEVEL_DEBUG);
141+
// raise action for this file
142+
$app->plugins->raiseAction('php_ini_changed', $file);
143+
}
144+
145+
$new_php_ini_md5[$file_path] = $file_md5;
146+
}
147+
148+
//** write new md5 sums if something changed
149+
if($php_ini_changed == true) $app->system->file_put_contents(SCRIPT_PATH . '/php.ini.md5sum', base64_encode(serialize($new_php_ini_md5)));
150+
unset($new_php_ini_md5);
151+
unset($php_ini_md5);
152+
unset($processed);
153+
}
154+
}
155+
156+
?>

server/server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
$app->modules->loadModules('all');
154154
//** Load the plugins that are in the plugins-enabled folder
155155
$app->plugins->loadPlugins('all');
156+
157+
$app->plugins->raiseAction('server_plugins_loaded', '');
158+
156159
if ($tmp_num_records > 0) {
157160
$app->log("Found $tmp_num_records changes, starting update process.", LOGLEVEL_DEBUG);
158161
//** Go through the sys_datalog table and call the processing functions

0 commit comments

Comments
 (0)