Merge branch 2.1-stable into develop
diff --git a/system/core/Input.php b/system/core/Input.php
index b986c49..162e40c 100755
--- a/system/core/Input.php
+++ b/system/core/Input.php
@@ -376,14 +376,26 @@
 	/**
 	 * Validate IP Address
 	 *
-	 * Updated version suggested by Geert De Deckere
-	 *
 	 * @param	string
+	 * @param	string	'ipv4' or 'ipv6'
 	 * @return	bool
 	 */
-	public function valid_ip($ip)
+	public function valid_ip($ip, $which = '')
 	{
-		return (bool) filter_var($ip, FILTER_VALIDATE_IP);
+		switch (strtolower($which))
+		{
+			case 'ipv4':
+				$which = FILTER_FLAG_IPV4;
+				break;
+			case 'ipv6':
+				$which = FILTER_FLAG_IPV6;
+				break;
+			default:
+				$which = NULL;
+				break;
+		}
+
+		return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index 65f1f18..f5a7e2a 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -1307,14 +1307,16 @@
 		}
 
 		// Convert tabs or multiple spaces into single spaces
-		$item = preg_replace('/[\t ]+/', ' ', $item);
+		$item = preg_replace('/\s+/', ' ', $item);
 
 		// If the item has an alias declaration we remove it and set it aside.
 		// Basically we remove everything to the right of the first space
-		if (strpos($item, ' ') !== FALSE)
+		if (preg_match('/^([^\s]+) (AS )*(.+)$/i', $item, $matches))
 		{
-			$alias = strstr($item, ' ');
-			$item = substr($item, 0, - strlen($alias));
+			$item = $matches[1];
+
+			// Escape the alias
+			$alias = ' '.$matches[2].$this->escape_identifiers($matches[3]);
 		}
 		else
 		{
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index 5d0a2ae..3b45bba 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -1985,7 +1985,7 @@
 		if (strpos($table, ' ') !== FALSE)
 		{
 			// if the alias is written with the AS keyword, remove it
-			$table = preg_replace('/ AS /i', ' ', $table);
+			$table = preg_replace('/\s+AS\s+/i', ' ', $table);
 
 			// Grab the alias
 			$table = trim(strrchr($table, ' '));
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 225325d..77c968e 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -1089,11 +1089,12 @@
 	 * Validate IP Address
 	 *
 	 * @param	string
+	 * @param	string	'ipv4' or 'ipv6' to validate a specific IP format
 	 * @return	bool
 	 */
-	public function valid_ip($ip)
+	public function valid_ip($ip, $which = '')
 	{
-		return $this->CI->input->valid_ip($ip);
+		return $this->CI->input->valid_ip($ip, $which);
 	}
 
 	// --------------------------------------------------------------------