Skip to content
nginx_plugin.inc.php 53.2 KiB
Newer Older
Falko Timme's avatar
Falko Timme committed
					// Remove trailing slash
					if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
					// create the symlinks, if not exist
					if(is_link($tmp_symlink)) {
						unlink($tmp_symlink);
						$app->log('Removing symlink: '.$tmp_symlink,LOGLEVEL_DEBUG);
					}
				}
			}
			// end removing symlinks
Falko Timme's avatar
Falko Timme committed
			$vhost_logfile_dir = escapeshellcmd('/var/log/ispconfig/httpd/'.$data['old']['domain']);
			if($data['old']['domain'] != '' && !stristr($vhost_logfile_dir,'..')) exec('rm -rf '.$vhost_logfile_dir);
			$app->log('Removing website logfile directory: '.$vhost_logfile_dir,LOGLEVEL_DEBUG);

Falko Timme's avatar
Falko Timme committed
			//delete the web user
			$command = 'userdel';
			$command .= ' '.$data['old']['system_user'];
			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);
			}
			
			if($data['old']['php'] == 'fast-cgi') {
				$this->php_fpm_pool_delete($data,$web_config);
			}
Falko Timme's avatar
Falko Timme committed
	//* This function is called when a IP on the server is inserted, updated or deleted
	function server_ip($event_name,$data) {
Falko Timme's avatar
Falko Timme committed

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

Falko Timme's avatar
Falko Timme committed
		$tpl->newTemplate('apache_ispconfig.conf.master');
		$records = $app->db->queryAllRecords('SELECT * FROM server_ip WHERE server_id = '.$conf['server_id']." AND virtualhost = 'y'");
Falko Timme's avatar
Falko Timme committed
		if(count($records) > 0) {
			$tpl->setLoop('ip_adresses',$records);
		}
Falko Timme's avatar
Falko Timme committed
		$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'];
Falko Timme's avatar
Falko Timme committed
		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);
		}
	//* Update the PHP-FPM pool configuration file
	private function php_fpm_pool_update ($data,$web_config,$pool_dir,$pool_name,$socket_dir) {
		global $app, $conf;
		//$reload = false;
		
		if($data['new']['php'] == 'no'){
			if(@is_file($pool_dir.$pool_name.'.conf')){
				unlink($pool_dir.$pool_name.'.conf');
				//$reload = true;
			}
			//if($reload == true) $app->services->restartService('php-fpm','reload');
			return;
		}
			
		$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');

		if($data['new']['php_fpm_use_socket'] == 'y'){
			$use_tcp = ';';
			$use_socket = '';
			if(!is_dir($socket_dir)) exec('mkdir -p '.$socket_dir);
		} else {
			$use_tcp = '';
			$use_socket = ';';
		}
		$tpl->setVar('use_tcp', $use_tcp);
		$tpl->setVar('use_socket', $use_socket);
			
		$fpm_socket = $socket_dir.$pool_name.'.sock';
		$tpl->setVar('fpm_socket', $fpm_socket);
			
		$tpl->setVar('fpm_pool', $pool_name);
		$tpl->setVar('fpm_port', $web_config['php_fpm_start_port'] + $data['new']['domain_id'] + 1);
		$tpl->setVar('fpm_user', $data['new']['system_user']);
		$tpl->setVar('fpm_group', $data['new']['system_group']);
		$tpl->setVar('security_level',$web_config['security_level']);
		$php_open_basedir = ($data['new']['php_open_basedir'] == '')?escapeshellcmd($data['new']['document_root']):escapeshellcmd($data['new']['php_open_basedir']);
		$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', ';');
		}
		// 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){
							$value = escapeshellcmd(trim($value));
							$key = escapeshellcmd(trim($key));
							switch (strtolower($value)) {
								case 'on':
								case 'off':
								case '1':
								case '0':
									// PHP-FPM might complain about invalid boolean value if you use 0
									$value = 'off';
								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);
		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);
		unset($tpl);
		//$reload = true;

		//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 = escapeshellcmd($web_config['php_fpm_pool_dir']);
		if(substr($pool_dir,-1) != '/') $pool_dir .= '/';
		$pool_name = 'web'.$data['new']['domain_id'];
		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');
Falko Timme's avatar
Falko Timme committed
	function client_delete($event_name,$data) {
Falko Timme's avatar
Falko Timme committed
		$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);
Falko Timme's avatar
Falko Timme committed
	private function _checkTcp ($host,$port) {

		$fp = @fsockopen ($host, $port, $errno, $errstr, 2);
Falko Timme's avatar
Falko Timme committed
		if ($fp) {
			fclose($fp);
			return true;
		} else {
			return false;
		}
Falko Timme's avatar
Falko Timme committed
?>