From 238326c6e7528571d0e8c68f310591206256ea77 Mon Sep 17 00:00:00 2001
From: Marius Burkard <m.burkard@pixcept.de>
Date: Thu, 31 Dec 2015 02:54:45 -0500
Subject: [PATCH] Merge branch 'master' into 'master'

---
 server/plugins-available/apache2_plugin.inc.php |    7 +++++--
 interface/lib/classes/aps_guicontroller.inc.php |   20 +++++++++++++++-----
 interface/lib/classes/remote.d/aps.inc.php      |   11 +++++++++--
 3 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/interface/lib/classes/aps_guicontroller.inc.php b/interface/lib/classes/aps_guicontroller.inc.php
index 3a5084c..f31cf81 100644
--- a/interface/lib/classes/aps_guicontroller.inc.php
+++ b/interface/lib/classes/aps_guicontroller.inc.php
@@ -43,7 +43,17 @@
 		parent::__construct($app);
 	}
 
-
+	/**
+	 * Removes www from Domains name
+	 *
+	 * @param $filename the file to read
+	 * @return $sxe a SimpleXMLElement handle
+	 */
+	public function getMainDomain($domain) {
+		if (substr($domain, 0, 4) == 'www.') $domain = substr($domain, 4);
+		return $domain;
+	}
+	
 
 	/**
 	 * Reads in a package metadata file and registers it's namespaces
@@ -344,9 +354,9 @@
 		$app->uses('tools_sites');
 
 		$webserver_id = 0;
-		$websrv = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = ?", $settings['main_domain']);
+		$websrv = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain = ?", $this->getMainDomain($settings['main_domain']));
 		if(!empty($websrv)) $webserver_id = $websrv['server_id'];
-		$customerid = $this->getCustomerIDFromDomain($settings['main_domain']);
+		$customerid = $this->getCustomerIDFromDomain($this->getMainDomain($settings['main_domain']));
 
 		if(empty($settings) || empty($webserver_id)) return false;
 
@@ -565,13 +575,13 @@
 			if(in_array($postinput['main_domain'], $domains))
 			{
 				$docroot = $app->db->queryOneRecord("SELECT document_root FROM web_domain
-                    WHERE domain = ?", $postinput['main_domain']);
+                    WHERE domain = ?", $this->getMainDomain($postinput['main_domain']));
 				$new_path = $docroot['document_root'];
 				if(substr($new_path, -1) != '/') $new_path .= '/';
 				$new_path .= $main_location;
 
 				// Get the $customerid which belongs to the selected domain
-				$customerid = $this->getCustomerIDFromDomain($postinput['main_domain']);
+				$customerid = $this->getCustomerIDFromDomain($this->getMainDomain($postinput['main_domain']));
 
 				// First get all domains used for an install, then their loop them
 				// and get the corresponding document roots as well as the defined
diff --git a/interface/lib/classes/remote.d/aps.inc.php b/interface/lib/classes/remote.d/aps.inc.php
index 6497da4..4db5f78 100644
--- a/interface/lib/classes/remote.d/aps.inc.php
+++ b/interface/lib/classes/remote.d/aps.inc.php
@@ -273,8 +273,15 @@
 			return false;
 		}
 	
-		$sql = "SELECT * FROM web_domain WHERE domain = ?";
-		$domain = $app->db->queryOneRecord($sql, $params['main_domain']);
+		if (substr($params['main_domain'], 0, 4) == 'www.') {
+			$domain = substr($params['main_domain'], 4);
+			$sql = "SELECT * FROM web_domain WHERE domain = ? AND subdomain=?";
+			$domain = $app->db->queryOneRecord($sql, $domain, 'www');
+		}
+		else {
+			$sql = "SELECT * FROM web_domain WHERE domain = ?";
+			$domain = $app->db->queryOneRecord($sql, $params['main_domain']);
+		}
 	
 		if (!$domain) {
 			$this->server->fault('invalid parameters', 'No valid domain given.');
diff --git a/server/plugins-available/apache2_plugin.inc.php b/server/plugins-available/apache2_plugin.inc.php
index 818cfd8..d5406b5 100644
--- a/server/plugins-available/apache2_plugin.inc.php
+++ b/server/plugins-available/apache2_plugin.inc.php
@@ -1233,6 +1233,7 @@
 
 		// Rewrite rules
 		$rewrite_rules = array();
+		$rewrite_wildcard_rules = array();
 		if($data['new']['redirect_type'] != '' && $data['new']['redirect_path'] != '') {
 			if(substr($data['new']['redirect_path'], -1) != '/' && !preg_match('/^(https?|\[scheme\]):\/\//', $data['new']['redirect_path'])) $data['new']['redirect_path'] .= '/';
 			if(substr($data['new']['redirect_path'], 0, 8) == '[scheme]'){
@@ -1264,7 +1265,7 @@
 					'rewrite_add_path' => (substr($rewrite_target, -1) == '/' ? 'y' : 'n'));
 				break;
 			case '*':
-				$rewrite_rules[] = array( 'rewrite_domain'  => '(^|\.)'.$this->_rewrite_quote($data['new']['domain']),
+				$rewrite_wildcard_rules[] = array( 'rewrite_domain'  => '(^|\.)'.$this->_rewrite_quote($data['new']['domain']),
 					'rewrite_type'   => ($data['new']['redirect_type'] == 'no')?'':'['.$data['new']['redirect_type'].']',
 					'rewrite_target'  => $rewrite_target,
 					'rewrite_target_ssl' => $rewrite_target_ssl,
@@ -1363,7 +1364,7 @@
 							'rewrite_add_path' => (substr($rewrite_target, -1) == '/' ? 'y' : 'n'));
 						break;
 					case '*':
-						$rewrite_rules[] = array( 'rewrite_domain'  => '(^|\.)'.$this->_rewrite_quote($alias['domain']),
+						$rewrite_wildcard_rules[] = array( 'rewrite_domain'  => '(^|\.)'.$this->_rewrite_quote($alias['domain']),
 							'rewrite_type'   => ($alias['redirect_type'] == 'no')?'':'['.$alias['redirect_type'].']',
 							'rewrite_target'  => $rewrite_target,
 							'rewrite_target_ssl' => $rewrite_target_ssl,
@@ -1400,6 +1401,8 @@
 		} else {
 			$tpl->setVar('alias', '');
 		}
+		
+		if (count($rewrite_wildcard_rules) > 0) $rewrite_rules = array_merge($rewrite_rules, $rewrite_wildcard_rules); // Append wildcard rules to the end of rules
 
 		if(count($rewrite_rules) > 0 || $vhost_data['seo_redirect_enabled'] > 0 || count($alias_seo_redirects) > 0) {
 			$tpl->setVar('rewrite_enabled', 1);

--
Gitblit v1.9.1