nginx_plugin.inc.php 52.6 KB
Newer Older
Falko Timme's avatar
Falko Timme committed
1001
1002
1003
1004
1005
1006
1007
			exec($command);
			if($apache_chrooted) $this->_exec('chroot '.escapeshellcmd($web_config['website_basedir']).' '.$command);
			
			//* Remove the awstats configuration file
			if($data['old']['stats_type'] == 'awstats') {
				$this->awstats_delete($data,$web_config);
			}
1008
1009
1010
1011
			
			if($data['old']['php'] == 'fast-cgi') {
				$this->php_fpm_pool_delete($data,$web_config);
			}
Falko Timme's avatar
Falko Timme committed
1012

1013
1014
1015
		}
	}

Falko Timme's avatar
Falko Timme committed
1016
1017
	//* This function is called when a IP on the server is inserted, updated or deleted
	function server_ip($event_name,$data) {
1018
		global $app, $conf;
Falko Timme's avatar
Falko Timme committed
1019
1020

		// load the server configuration options
1021
		$app->uses('getconf');
Falko Timme's avatar
Falko Timme committed
1022
1023
		$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');

1024
		$app->load('tpl');
Falko Timme's avatar
Falko Timme committed
1025

1026
		$tpl = new tpl();
Falko Timme's avatar
Falko Timme committed
1027
1028
		$tpl->newTemplate('apache_ispconfig.conf.master');
		$records = $app->db->queryAllRecords('SELECT * FROM server_ip WHERE server_id = '.$conf['server_id']." AND virtualhost = 'y'");
1029

Falko Timme's avatar
Falko Timme committed
1030
1031
1032
		if(count($records) > 0) {
			$tpl->setLoop('ip_adresses',$records);
		}
1033

Falko Timme's avatar
Falko Timme committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
		$vhost_file = escapeshellcmd($web_config['nginx_vhost_conf_dir'].'/ispconfig.conf');
		file_put_contents($vhost_file,$tpl->grab());
		$app->log('Writing the conf file: '.$vhost_file,LOGLEVEL_DEBUG);
		unset($tpl);

	}
	
	//* Update the awstats configuration file
	private function awstats_update ($data,$web_config) {
		global $app;
		
		$awstats_conf_dir = $web_config['awstats_conf_dir'];
		
		if(!@is_file($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf') || ($data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain'])) {
			if ( @is_file($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf') ) {
				unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');
			}
			
			$content = '';
			$content .= "Include \"".$awstats_conf_dir."/awstats.conf\"\n";
			$content .= "LogFile=\"/var/log/ispconfig/httpd/".$data['new']['domain']."/access.log\"\n";
			$content .= "SiteDomain=\"".$data['new']['domain']."\"\n";
			$content .= "HostAliases=\"www.".$data['new']['domain']."  localhost 127.0.0.1\"\n";
			
			file_put_contents($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf',$content);
			$app->log('Created AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf',LOGLEVEL_DEBUG);
		}
	}
	
	//* Delete the awstats configuration file
	private function awstats_delete ($data,$web_config) {
		global $app;
		
		$awstats_conf_dir = $web_config['awstats_conf_dir'];
1068
		
Falko Timme's avatar
Falko Timme committed
1069
1070
1071
1072
		if ( @is_file($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf') ) {
			unlink($awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf');
			$app->log('Removed AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['old']['domain'].'.conf',LOGLEVEL_DEBUG);
		}
1073
1074
	}
	
1075
1076
1077
1078
1079
	//* Update the PHP-FPM pool configuration file
	private function php_fpm_pool_update ($data,$web_config) {
		global $app, $conf;
		
		$pool_dir = $web_config['php_fpm_pool_dir'];
1080
		$pool_name = 'web'.$data['new']['domain_id'];
1081
1082
1083
		//$reload = false;
		
		if($data['new']['php'] == 'no'){
1084
1085
			if(@is_file($pool_dir.'/'.$pool_name.'.conf')){
				unlink($pool_dir.'/'.$pool_name.'.conf');
1086
1087
1088
1089
1090
1091
				//$reload = true;
			}
			//if($reload == true) $app->services->restartService('php-fpm','reload');
			return;
		}
		
1092
		//if(!@is_file($pool_dir.'/'.$data['new']['domain'].'.conf') || ($data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain'])) {
1093
1094
1095
			//if ( @is_file($pool_dir.'/'.$pool_name.'.conf') ) {
			//	unlink($pool_dir.'/'.$pool_name.'.conf');
			//}
1096
1097
1098
1099
1100
1101
1102
1103
			
			$app->uses("getconf");
			$web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
			
			$app->load('tpl');
			$tpl = new tpl();
			$tpl->newTemplate('php_fpm_pool.conf.master');

1104
			$tpl->setVar('fpm_pool', $pool_name);
1105
			$tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id'] + 1);
1106
1107
			$tpl->setVar('fpm_user', $data['new']['system_user']);
			$tpl->setVar('fpm_group', $data['new']['system_group']);
1108
			$tpl->setVar('security_level',$web_config['security_level']);
1109
			$php_open_basedir = ($data['new']['php_open_basedir'] == '')?escapeshellcmd($data['new']['document_root']):escapeshellcmd($data['new']['php_open_basedir']);
1110
1111
1112
1113
1114
1115
			$tpl->setVar('php_open_basedir', $php_open_basedir);
			if($php_open_basedir != ''){
				$tpl->setVar('enable_php_open_basedir', '');
			} else {
				$tpl->setVar('enable_php_open_basedir', ';');
			}
1116
			
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
			// Custom php.ini settings
			$final_php_ini_settings = array();
			$custom_php_ini_settings = trim($data['new']['custom_php_ini']);
			if($custom_php_ini_settings != ''){
				// Make sure we only have Unix linebreaks
				$custom_php_ini_settings = str_replace("\r\n", "\n", $custom_php_ini_settings);
				$custom_php_ini_settings = str_replace("\r", "\n", $custom_php_ini_settings);
				$ini_settings = explode("\n", $custom_php_ini_settings);
				if(is_array($ini_settings) && !empty($ini_settings)){
					foreach($ini_settings as $ini_setting){
							list($key, $value) = explode('=', $ini_setting);
							if($value){
1129
1130
								$value = escapeshellcmd(trim($value));
								$key = escapeshellcmd(trim($key));
1131
1132
1133
1134
1135
								switch (strtolower($value)) {
									case 'on':
									case 'off':
									case '1':
									case '0':
1136
1137
										// PHP-FPM might complain about invalid boolean value if you use 0
										$value = 'off';
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
									case 'true':
									case 'false':
									case 'yes':
									case 'no':
										$final_php_ini_settings[] = array('ini_setting' => 'php_admin_flag['.$key.'] = '.$value);
										break;
									default:
										$final_php_ini_settings[] = array('ini_setting' => 'php_admin_value['.$key.'] = '.$value);
								}
							}
					}
				}
			}
			
			$tpl->setLoop('custom_php_ini_settings', $final_php_ini_settings);
			
1154
1155
			file_put_contents($pool_dir.'/'.$pool_name.'.conf',$tpl->grab());
			$app->log('Writing the PHP-FPM config file: '.$pool_dir.'/'.$pool_name.'.conf',LOGLEVEL_DEBUG);
1156
1157
			unset($tpl);
			//$reload = true;
1158
		//}
1159
1160
1161
1162
1163
1164
1165
1166
		//if($reload == true) $app->services->restartService('php-fpm','reload');
	}
	
	//* Delete the PHP-FPM pool configuration file
	private function php_fpm_pool_delete ($data,$web_config) {
		global $app;
		
		$pool_dir = $web_config['php_fpm_pool_dir'];
1167
		$pool_name = 'web'.$data['old']['domain_id'];
1168
		
1169
1170
1171
1172
		if ( @is_file($pool_dir.'/'.$pool_name.'.conf') ) {
			unlink($pool_dir.'/'.$pool_name.'.conf');
			$app->log('Removed PHP-FPM config file: '.$pool_dir.'/'.$pool_name.'.conf',LOGLEVEL_DEBUG);
			//$app->services->restartService('php-fpm','reload');
1173
1174
1175
		}
	}
	
Falko Timme's avatar
Falko Timme committed
1176
	function client_delete($event_name,$data) {
1177
1178
		global $app, $conf;
		
Falko Timme's avatar
Falko Timme committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
		$app->uses("getconf");
		$web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
		
		$client_id = intval($data['old']['client_id']);
		if($client_id > 0) {
			
			$client_dir = $web_config['website_basedir'].'/clients/client'.$client_id;
			if(is_dir($client_dir) && !stristr($client_dir,'..')) {
				@rmdir($client_dir);
				$app->log('Removed client directory: '.$client_dir,LOGLEVEL_DEBUG);
			}
			
			$this->_exec('groupdel client'.$client_id);
			$app->log('Removed group client'.$client_id,LOGLEVEL_DEBUG);
		}
		
	}

	//* Wrapper for exec function for easier debugging
	private function _exec($command) {
		global $app;
		$app->log('exec: '.$command,LOGLEVEL_DEBUG);
		exec($command);
1202
1203
	}
	
Falko Timme's avatar
Falko Timme committed
1204
1205
1206
	private function _checkTcp ($host,$port) {

		$fp = @fsockopen ($host, $port, $errno, $errstr, 2);
1207

Falko Timme's avatar
Falko Timme committed
1208
1209
1210
1211
1212
1213
		if ($fp) {
			fclose($fp);
			return true;
		} else {
			return false;
		}
1214
1215
	}

Falko Timme's avatar
Falko Timme committed
1216

1217
1218
} // end class

Falko Timme's avatar
Falko Timme committed
1219
?>