Merge pull request #892 from narfbg/develop-core-security

Improve the core Security library
diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php
index cb5d439..7af3c48 100755
--- a/system/core/CodeIgniter.php
+++ b/system/core/CodeIgniter.php
@@ -267,7 +267,7 @@
 	$method = $RTR->fetch_method();
 
 	if ( ! class_exists($class)
-		OR strpos($method, '_', 1) === 0
+		OR strpos($method, '_') === 0
 		OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
 		)
 	{
diff --git a/system/core/Common.php b/system/core/Common.php
index 6ef2296..1f59c02 100644
--- a/system/core/Common.php
+++ b/system/core/Common.php
@@ -235,7 +235,7 @@
 		}
 
 		// Is the config file in the environment folder?
-		if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT..'/config.php'))
+		if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
 		{
 			$file_path = APPPATH.'config/config.php';
 		}
diff --git a/system/core/Lang.php b/system/core/Lang.php
index 088cb6c..c40a685 100755
--- a/system/core/Lang.php
+++ b/system/core/Lang.php
@@ -130,7 +130,7 @@
 		}
 
 		$this->is_loaded[] = $langfile;
-		$this->language = $this->language + $lang;
+		$this->language = array_merge($this->language, $lang);
 		unset($lang);
 
 		log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
diff --git a/system/core/Output.php b/system/core/Output.php
index 1beee73..abd8a0e 100755
--- a/system/core/Output.php
+++ b/system/core/Output.php
@@ -129,7 +129,6 @@
 	 *
 	 * Sets the output string
 	 *
-	 * @access	public
 	 * @param	string
 	 * @return	void
 	 */
@@ -282,7 +281,7 @@
 	 * @param	integer
 	 * @return	void
 	 */
-	publi function cache($time)
+	public function cache($time)
 	{
 		$this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
 		return $this;
@@ -397,15 +396,13 @@
 
 			// If the output data contains closing </body> and </html> tags
 			// we will remove them and add them back after we insert the profile data
-			$output = preg_replace('|</body>.*?</html>|is', '', $output, $count).$CI->profiler->run();
+			$output = preg_replace('|</body>.*?</html>|is', '', $output, -1, $count).$CI->profiler->run();
 			if ($count > 0)
 			{
 				$output .= '</body></html>';
 			}
 		}
 
-		// --------------------------------------------------------------------
-
 		// Does the controller contain a function named _output()?
 		// If so send the output there.  Otherwise, echo it.
 		if (method_exists($CI, '_output'))
@@ -414,7 +411,7 @@
 		}
 		else
 		{
-			echo $output;  // Send it to the browser!
+			echo $output; // Send it to the browser!
 		}
 
 		log_message('debug', 'Final output sent to browser');
diff --git a/system/core/URI.php b/system/core/URI.php
index eaf7b75..b28ee19 100755
--- a/system/core/URI.php
+++ b/system/core/URI.php
@@ -186,11 +186,12 @@
 			return '';
 		}
 
-		if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0)
+		$uri = $_SERVER['REQUEST_URI'];
+		if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
 		{
 			$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
 		}
-		elseif (strpos($_SERVER['REQUEST_URI'], dirname($_SERVER['SCRIPT_NAME'])) === 0)
+		elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
 		{
 			$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
 		}
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index 71762a4..4247351 100644
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -1127,15 +1127,14 @@
 		$result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows')));
 		$this->_reset_select();
 
-		if ($query->num_rows() === 0)
+		if ($result->num_rows() === 0)
 		{
 			return 0;
 		}
 
-		$row = $query->row();
+		$row = $result->row();
 		return (int) $row->numrows;
 	}
-
 	// --------------------------------------------------------------------
 
 	/**
diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php
index 9b39b8c..2d4b10e 100644
--- a/system/helpers/file_helper.php
+++ b/system/helpers/file_helper.php
@@ -196,16 +196,16 @@
 
 			while (FALSE !== ($file = readdir($fp)))
 			{
-				if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
+				if (@is_dir($source_dir.$file) && $file[0] !== '.')
 				{
 					get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
 				}
-				elseif (strncmp($file, '.', 1) !== 0)
+				elseif ($file[0] !== '.')
 				{
 					$_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
 				}
 			}
-			closedir($source_dir);
+			closedir($fp);
 
 			return $_filedata;
 		}
@@ -249,17 +249,17 @@
 			// foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast
 			while (FALSE !== ($file = readdir($fp)))
 			{
-				if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE)
+				if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
 				{
 					get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
 				}
-				elseif (strncmp($file, '.', 1) !== 0)
+				elseif ($file[0] !== '.')
 				{
 					$_filedata[$file] = get_file_info($source_dir.$file);
 					$_filedata[$file]['relative_path'] = $relative_path;
 				}
 			}
-			closedir($source_dir);
+			closedir($fp);
 
 			return $_filedata;
 		}
@@ -359,7 +359,7 @@
 
 		if ( ! is_array($mimes))
 		{
-			if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
+			if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
 			{
 				include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
 			}