Merge branch 'develop' of github.com:EllisLab/CodeIgniter into develop
diff --git a/system/core/Common.php b/system/core/Common.php
index 90cc5b3..258cd49 100644
--- a/system/core/Common.php
+++ b/system/core/Common.php
@@ -316,11 +316,11 @@
 	{
 		static $_mimes = array();
 
-		if (is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
+		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
 		{
 			$_mimes = include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
 		}
-		elseif (is_file(APPPATH.'config/mimes.php'))
+		elseif (file_exists(APPPATH.'config/mimes.php'))
 		{
 			$_mimes = include(APPPATH.'config/mimes.php');
 		}
diff --git a/system/core/Hooks.php b/system/core/Hooks.php
index 59759e0..17f6a02 100644
--- a/system/core/Hooks.php
+++ b/system/core/Hooks.php
@@ -81,11 +81,12 @@
 		}
 
 		// Grab the "hooks" definition file.
-		if (is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
+		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
 		{
 			include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
 		}
-		elseif (is_file(APPPATH.'config/hooks.php'))
+
+		if (file_exists(APPPATH.'config/hooks.php'))
 		{
 			include(APPPATH.'config/hooks.php');
 		}
diff --git a/system/core/Router.php b/system/core/Router.php
index 4755b37..bb0ce16 100644
--- a/system/core/Router.php
+++ b/system/core/Router.php
@@ -133,15 +133,16 @@
 		}
 
 		// Load the routes.php file.
-		if (is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
-		{
-			include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
-		}
-		elseif (is_file(APPPATH.'config/routes.php'))
+		if (file_exists(APPPATH.'config/routes.php'))
 		{
 			include(APPPATH.'config/routes.php');
 		}
 
+		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
+		{
+			include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
+		}
+
 		$this->routes = (empty($route) OR ! is_array($route)) ? array() : $route;
 		unset($route);
 
diff --git a/system/helpers/download_helper.php b/system/helpers/download_helper.php
index 7294d50..4fe6a0e 100644
--- a/system/helpers/download_helper.php
+++ b/system/helpers/download_helper.php
@@ -58,7 +58,7 @@
 		}
 		elseif ($data === NULL)
 		{
-			if (@is_file($filename) && @file_exists($filename) && ($filesize = @filesize($filename)) !== FALSE)
+			if (@is_file($filename) && ($filesize = @filesize($filename)) !== FALSE)
 			{
 				$filepath = $filename;
 				$filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
diff --git a/system/helpers/html_helper.php b/system/helpers/html_helper.php
index 7a71eb8..80a2787 100644
--- a/system/helpers/html_helper.php
+++ b/system/helpers/html_helper.php
@@ -238,26 +238,30 @@
 	 */
 	function doctype($type = 'xhtml1-strict')
 	{
-		global $_doctypes;
+		static $doctypes;
 
-		if ( ! is_array($_doctypes))
+		if ( ! is_array($doctypes))
 		{
-			if (is_file(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
-			{
-				include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
-			}
-			elseif (is_file(APPPATH.'config/doctypes.php'))
+			if (file_exists(APPPATH.'config/doctypes.php'))
 			{
 				include(APPPATH.'config/doctypes.php');
 			}
 
-			if ( ! is_array($_doctypes))
+			if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
 			{
+				include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
+			}
+
+			if (empty($_doctypes) OR ! is_array($_doctypes))
+			{
+				$doctypes = array();
 				return FALSE;
 			}
+
+			$doctypes = $_doctypes;
 		}
 
-		return isset($_doctypes[$type]) ? $_doctypes[$type] : FALSE;
+		return isset($doctypes[$type]) ? $doctypes[$type] : FALSE;
 	}
 }
 
diff --git a/system/helpers/smiley_helper.php b/system/helpers/smiley_helper.php
index c2f50ec..d9a6934 100644
--- a/system/helpers/smiley_helper.php
+++ b/system/helpers/smiley_helper.php
@@ -213,16 +213,30 @@
 	 */
 	function _get_smiley_array()
 	{
-		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
+		static $_smileys;
+
+		if ( ! is_array($smileys))
 		{
-			include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
-		}
-		elseif (file_exists(APPPATH.'config/smileys.php'))
-		{
-			include(APPPATH.'config/smileys.php');
+			if (file_exists(APPPATH.'config/smileys.php'))
+			{
+				include(APPPATH.'config/smileys.php');
+			}
+
+			if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
+			{
+				include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
+			}
+
+			if (empty($smileys) OR ! is_array($smileys))
+			{
+				$_smileys = array();
+				return FALSE;
+			}
+
+			$_smileys = $smileys;
 		}
 
-		return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE;
+		return $_smileys;
 	}
 }
 
diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php
index c255c15..54db14f 100644
--- a/system/helpers/text_helper.php
+++ b/system/helpers/text_helper.php
@@ -358,31 +358,35 @@
 	/**
 	 * Convert Accented Foreign Characters to ASCII
 	 *
-	 * @param	string	the text string
+	 * @param	string	$str	Input string
 	 * @return	string
 	 */
 	function convert_accented_characters($str)
 	{
-		global $foreign_characters;
+		static $_foreign_characters;
 
-		if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
+		if ( ! is_array($_foreign_characters))
 		{
-			if (is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
-			{
-				include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
-			}
-			elseif (is_file(APPPATH.'config/foreign_chars.php'))
+			if (file_exists(APPPATH.'config/foreign_chars.php'))
 			{
 				include(APPPATH.'config/foreign_chars.php');
 			}
 
-			if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
+			if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
 			{
+				include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
+			}
+
+			if (empty($foreign_characters) OR ! is_array($foreign_characters))
+			{
+				$_foreign_characters = array();
 				return $str;
 			}
+
+			$_foreign_characters = $foreign_characters;
 		}
 
-		return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
+		return preg_replace(array_keys($_foreign_characters), array_values($_foreign_characters), $str);
 	}
 }
 
diff --git a/system/libraries/User_agent.php b/system/libraries/User_agent.php
index 3fe2e05..2f6f819 100644
--- a/system/libraries/User_agent.php
+++ b/system/libraries/User_agent.php
@@ -175,15 +175,18 @@
 	 */
 	protected function _load_agent_file()
 	{
-		if (is_file(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
-		{
-			include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
-		}
-		elseif (is_file(APPPATH.'config/user_agents.php'))
+		if (($found = file_exists(APPPATH.'config/user_agents.php')))
 		{
 			include(APPPATH.'config/user_agents.php');
 		}
-		else
+
+		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
+		{
+			include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
+			$found = TRUE;
+		}
+
+		if ($found !== TRUE)
 		{
 			return FALSE;
 		}