From 8309d8765bc5285405892d5fa35504d747c5c21c Mon Sep 17 00:00:00 2001
From: ftimme <ft@falkotimme.com>
Date: Tue, 30 Oct 2012 11:21:04 -0400
Subject: [PATCH] - nginx alias/subdomain redirects: Added parenthese around non-capturing parentheses so that $2 captures the correct content.

---
 server/plugins-available/nginx_plugin.inc.php |  109 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 61 insertions(+), 48 deletions(-)

diff --git a/server/plugins-available/nginx_plugin.inc.php b/server/plugins-available/nginx_plugin.inc.php
index 056d83a..dd26e94 100644
--- a/server/plugins-available/nginx_plugin.inc.php
+++ b/server/plugins-available/nginx_plugin.inc.php
@@ -115,7 +115,7 @@
 		$crt_file = $ssl_dir.'/'.$domain.'.crt';
 
 		//* Create a SSL Certificate
-		if($data['new']['ssl_action'] == 'create') {
+		if($data['new']['ssl_action'] == 'create' && $conf['mirror_server_id'] == 0) {
 		
 			$this->ssl_certificate_changed = true;
 			
@@ -951,7 +951,7 @@
 			switch($data['new']['subdomain']) {
 				case 'www':
 					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
-						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+						$rewrite_exclude = '((?!'.substr($data['new']['redirect_path'],0,-1).'))';
 					} else { // URL - check if URL is local
 						$tmp_redirect_path = $data['new']['redirect_path'];
 						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -959,20 +959,20 @@
 						if($tmp_redirect_path_parts['host'] == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+							$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 						} else {
 							$rewrite_exclude = '(.?)';
 						}
 						unset($tmp_redirect_path);
 						unset($tmp_redirect_path_parts);
 					}
-					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$data['new']['domain'],
+					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$this->_rewrite_quote($data['new']['domain']),
 					'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
 					'rewrite_target' 	=> $data['new']['redirect_path'],
 					'rewrite_exclude'	=> $rewrite_exclude);
 					
 					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
-						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+						$rewrite_exclude = '((?!'.substr($data['new']['redirect_path'],0,-1).'))';
 					} else { // URL - check if URL is local
 						$tmp_redirect_path = $data['new']['redirect_path'];
 						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -980,21 +980,21 @@
 						if($tmp_redirect_path_parts['host'] == 'www.'.$data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+							$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 						} else {
 							$rewrite_exclude = '(.?)';
 						}
 						unset($tmp_redirect_path);
 						unset($tmp_redirect_path_parts);
 					}
-					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^www.'.$data['new']['domain'],
+					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^' . $this->_rewrite_quote('www.'.$data['new']['domain']),
 							'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
 							'rewrite_target' 	=> $data['new']['redirect_path'],
 							'rewrite_exclude'	=> $rewrite_exclude);
 					break;
 				case '*':
 					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
-						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+						$rewrite_exclude = '((?!'.substr($data['new']['redirect_path'],0,-1).'))';
 					} else { // URL - check if URL is local
 						$tmp_redirect_path = $data['new']['redirect_path'];
 						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1002,21 +1002,21 @@
 						if(substr($tmp_redirect_path_parts['host'],-strlen($data['new']['domain'])) == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+							$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 						} else {
 							$rewrite_exclude = '(.?)';
 						}
 						unset($tmp_redirect_path);
 						unset($tmp_redirect_path_parts);
 					}
-					$rewrite_rules[] = array(	'rewrite_domain' 	=> $data['new']['domain'],
+					$rewrite_rules[] = array(	'rewrite_domain' 	=> '(^|\.)'.$this->_rewrite_quote($data['new']['domain']),
 						'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
 						'rewrite_target' 	=> $data['new']['redirect_path'],
 						'rewrite_exclude'	=> $rewrite_exclude);
 					break;
 				default:
 					if(substr($data['new']['redirect_path'],0,1) == '/'){ // relative path
-						$rewrite_exclude = '(?!'.substr($data['new']['redirect_path'],0,-1).')';
+						$rewrite_exclude = '((?!'.substr($data['new']['redirect_path'],0,-1).'))';
 					} else { // URL - check if URL is local
 						$tmp_redirect_path = $data['new']['redirect_path'];
 						if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1024,14 +1024,14 @@
 						if($tmp_redirect_path_parts['host'] == $data['new']['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 							if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 							if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-							$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+							$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 						} else {
 							$rewrite_exclude = '(.?)';
 						}
 						unset($tmp_redirect_path);
 						unset($tmp_redirect_path_parts);
 					}
-					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$data['new']['domain'],
+					$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$this->_rewrite_quote($data['new']['domain']),
 					'rewrite_type' 		=> ($data['new']['redirect_type'] == 'no')?'':$data['new']['redirect_type'],
 					'rewrite_target' 	=> $data['new']['redirect_path'],
 					'rewrite_exclude'	=> $rewrite_exclude);
@@ -1051,7 +1051,7 @@
 			unset($client);
 			unset($aa_search);
 			unset($aa_replace);
-			$server_alias[] .= $auto_alias;
+			$server_alias[] .= $auto_alias.' ';
 		}
 		
 		// get alias domains (co-domains and subdomains)
@@ -1092,7 +1092,7 @@
 					switch($alias['subdomain']) {
 						case 'www':
 							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
-								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+								$rewrite_exclude = '((?!'.substr($alias['redirect_path'],0,-1).'))';
 							} else { // URL - check if URL is local
 								$tmp_redirect_path = $alias['redirect_path'];
 								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1100,20 +1100,20 @@
 								if($tmp_redirect_path_parts['host'] == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+									$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 								} else {
 									$rewrite_exclude = '(.?)';
 								}
 								unset($tmp_redirect_path);
 								unset($tmp_redirect_path_parts);
 							}
-							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$alias['domain'],
+							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$this->_rewrite_quote($alias['domain']),
 								'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
 								'rewrite_target' 	=> $alias['redirect_path'],
 								'rewrite_exclude'	=> $rewrite_exclude);
 								
 							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
-								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+								$rewrite_exclude = '((?!'.substr($alias['redirect_path'],0,-1).'))';
 							} else { // URL - check if URL is local
 								$tmp_redirect_path = $alias['redirect_path'];
 								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1121,21 +1121,21 @@
 								if($tmp_redirect_path_parts['host'] == 'www.'.$alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+									$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 								} else {
 									$rewrite_exclude = '(.?)';
 								}
 								unset($tmp_redirect_path);
 								unset($tmp_redirect_path_parts);
 							}
-							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^www.'.$alias['domain'],
+							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^' . $this->_rewrite_quote('www.'.$alias['domain']),
 									'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
 									'rewrite_target' 	=> $alias['redirect_path'],
 									'rewrite_exclude'	=> $rewrite_exclude);
 							break;
 						case '*':
 							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
-								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+								$rewrite_exclude = '((?!'.substr($alias['redirect_path'],0,-1).'))';
 							} else { // URL - check if URL is local
 								$tmp_redirect_path = $alias['redirect_path'];
 								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1143,21 +1143,21 @@
 								if(substr($tmp_redirect_path_parts['host'],-strlen($alias['domain'])) == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+									$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 								} else {
 									$rewrite_exclude = '(.?)';
 								}
 								unset($tmp_redirect_path);
 								unset($tmp_redirect_path_parts);
 							}
-							$rewrite_rules[] = array(	'rewrite_domain' 	=> $alias['domain'],
+							$rewrite_rules[] = array(	'rewrite_domain' 	=> '(^|\.)' . $this->_rewrite_quote($alias['domain']),
 								'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
 								'rewrite_target' 	=> $alias['redirect_path'],
 								'rewrite_exclude'	=> $rewrite_exclude);
 							break;
 						default:
 							if(substr($alias['redirect_path'],0,1) == '/'){ // relative path
-								$rewrite_exclude = '(?!'.substr($alias['redirect_path'],0,-1).')';
+								$rewrite_exclude = '((?!'.substr($alias['redirect_path'],0,-1).'))';
 							} else { // URL - check if URL is local
 								$tmp_redirect_path = $alias['redirect_path'];
 								if(substr($tmp_redirect_path,0,7) == '$scheme') $tmp_redirect_path = 'http'.substr($tmp_redirect_path,7);
@@ -1165,14 +1165,16 @@
 								if($tmp_redirect_path_parts['host'] == $alias['domain'] && ($tmp_redirect_path_parts['port'] == '80' || $tmp_redirect_path_parts['port'] == '443' || !isset($tmp_redirect_path_parts['port']))){
 									if(substr($tmp_redirect_path_parts['path'],-1) == '/') $tmp_redirect_path_parts['path'] = substr($tmp_redirect_path_parts['path'],0,-1);
 									if(substr($tmp_redirect_path_parts['path'],0,1) != '/') $tmp_redirect_path_parts['path'] = '/'.$tmp_redirect_path_parts['path'];
-									$rewrite_exclude = '(?!'.$tmp_redirect_path_parts['path'].')';
+									$rewrite_exclude = '((?!'.$tmp_redirect_path_parts['path'].'))';
 								} else {
 									$rewrite_exclude = '(.?)';
 								}
 								unset($tmp_redirect_path);
 								unset($tmp_redirect_path_parts);
 							}
-							$rewrite_rules[] = array(	'rewrite_domain' 	=> '^'.$alias['domain'],
+                            if(substr($alias['domain'], 0, 2) === '*.') $domain_rule = '(^|\.)'.$this->_rewrite_quote(substr($alias['domain'], 2));
+                            else $domain_rule = '^'.$this->_rewrite_quote($alias['domain']);
+							$rewrite_rules[] = array(	'rewrite_domain' 	=> $domain_rule,
 							'rewrite_type' 		=> ($alias['redirect_type'] == 'no')?'':$alias['redirect_type'],
 							'rewrite_target' 	=> $alias['redirect_path'],
 							'rewrite_exclude'	=> $rewrite_exclude);
@@ -1790,7 +1792,11 @@
 		}
 		
 		if(is_file($data['new']['document_root']."/" . $web_folder . "/stats/index.html")) $app->system->unlink($data['new']['document_root']."/" . $web_folder . "/stats/index.html");
-		$app->system->copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$data['new']['document_root']."/" . $web_folder . "/stats/index.php");
+		if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
+			$app->system->copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master",$data['new']['document_root']."/" . $web_folder . "/stats/index.php");
+		} else {
+			$app->system->copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$data['new']['document_root']."/" . $web_folder . "/stats/index.php");
+		}
 	}
 	
 	//* Delete the awstats configuration file
@@ -1902,27 +1908,31 @@
 			$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 '0':
-									// PHP-FPM might complain about invalid boolean value if you use 0
-									$value = 'off';
-								case '1':
-								case 'on':
-								case '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);
-							}
+					$ini_setting = trim($ini_setting);
+					if(substr($ini_setting,0,1) == ';') continue;
+					if(substr($ini_setting,0,1) == '#') continue;
+					if(substr($ini_setting,0,2) == '//') continue;
+					list($key, $value) = explode('=', $ini_setting);
+					if($value){
+						$value = trim($value);
+						$key = trim($key);
+						switch (strtolower($value)) {
+							case '0':
+								// PHP-FPM might complain about invalid boolean value if you use 0
+								$value = 'off';
+							case '1':
+							case 'on':
+							case '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);
 						}
+					}
 				}
 			}
 		}
@@ -2163,7 +2173,10 @@
 
 		return symlink($cfrom, $to);
 	}
-
+    
+    private function _rewrite_quote($string) {
+        return str_replace(array('.', '*', '?', '+'), array('\\.', '\\*', '\\?', '\\+'), $string);
+    }
 
 } // end class
 

--
Gitblit v1.9.1