Partially revert PR #2190

The core shouldn't depend on constants that are not defined by itself
diff --git a/index.php b/index.php
index 6a93242..8008211 100755
--- a/index.php
+++ b/index.php
@@ -68,7 +68,7 @@
 	default:
 		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
 		echo 'The application environment is not set correctly.';
-		exit(1); // EXIT_* constants not yet defined; 1 is EXIT_ERROR, a generic error.
+		exit(1); // EXIT_ERROR
 }
 
 /*
@@ -192,7 +192,7 @@
 	{
 		header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
 		echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
-		exit(3); // EXIT_* constants not yet defined; 3 is EXIT_CONFIG.
+		exit(3); // EXIT_CONFIG
 	}
 
 /*
@@ -228,7 +228,7 @@
 		{
 			header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
 			echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
-			exit(3); // EXIT_* constants not yet defined; 3 is EXIT_CONFIG.
+			exit(3); // EXIT_CONFIG
 		}
 
 		define('APPPATH', BASEPATH.$application_folder.DIRECTORY_SEPARATOR);
@@ -245,7 +245,7 @@
 		{
 			header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
 			echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
-			exit(3); // EXIT_* constants not yet defined; 3 is EXIT_CONFIG.
+			exit(3); // EXIT_CONFIG
 		}
 		else
 		{
diff --git a/system/core/Common.php b/system/core/Common.php
index 55f07a8..237bd42 100644
--- a/system/core/Common.php
+++ b/system/core/Common.php
@@ -94,17 +94,17 @@
 		if (is_dir($file))
 		{
 			$file = rtrim($file, '/').'/'.md5(mt_rand());
-			if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
+			if (($fp = @fopen($file, 'ab')) === FALSE)
 			{
 				return FALSE;
 			}
 
 			fclose($fp);
-			@chmod($file, DIR_WRITE_MODE);
+			@chmod($file, 0777);
 			@unlink($file);
 			return TRUE;
 		}
-		elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
+		elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)
 		{
 			return FALSE;
 		}
@@ -177,7 +177,7 @@
 			// self-referencing loop with the Exceptions class
 			set_status_header(503);
 			echo 'Unable to locate the specified class: '.$class.'.php';
-			exit(EXIT_UNKNOWN_CLASS);
+			exit(5); // EXIT_UNK_CLASS
 		}
 
 		// Keep track of what we just loaded
@@ -250,7 +250,7 @@
 			{
 				set_status_header(503);
 				echo 'The configuration file does not exist.';
-				exit(EXIT_CONFIG);
+				exit(3); // EXIT_CONFIG
 			}
 
 			// Does the $config array exist in the file?
@@ -258,7 +258,7 @@
 			{
 				set_status_header(503);
 				echo 'Your config file does not appear to be formatted correctly.';
-				exit(EXIT_CONFIG);
+				exit(3); // EXIT_CONFIG
 			}
 
 			// references cannot be directly assigned to static variables, so we use an array
@@ -397,16 +397,17 @@
 		$status_code = abs($status_code);
 		if ($status_code < 100)
 		{
-			$exit_status = $status_code + EXIT__AUTO_MIN;
-			if ($exit_status > EXIT__AUTO_MAX)
+			$exit_status = $status_code + 9; // 9 is EXIT__AUTO_MIN
+			if ($exit_status > 125) // 125 is EXIT__AUTO_MAX
 			{
-				$exit_status = EXIT_ERROR;
+				$exit_status = 1; // EXIT_ERROR
 			}
+
 			$status_code = 500;
 		}
 		else
 		{
-			$exit_status = EXIT_ERROR;
+			$exit_status = 1; // EXIT_ERROR
 		}
 
 		$_error =& load_class('Exceptions', 'core');
@@ -434,7 +435,7 @@
 	{
 		$_error =& load_class('Exceptions', 'core');
 		$_error->show_404($page, $log_error);
-		exit(EXIT_UNKNOWN_FILE);
+		exit(4); // EXIT_UNKNOWN_FILE
 	}
 }
 
@@ -612,7 +613,7 @@
 		// default error handling. See http://www.php.net/manual/en/errorfunc.constants.php
 		if ($is_error)
 		{
-			exit(EXIT_ERROR);
+			exit(1); // EXIT_ERROR
 		}
 	}
 }
diff --git a/system/core/Exceptions.php b/system/core/Exceptions.php
index 54a5bc4..0418696 100644
--- a/system/core/Exceptions.php
+++ b/system/core/Exceptions.php
@@ -125,7 +125,7 @@
 		}
 
 		echo $this->show_error($heading, $message, 'error_404', 404);
-		exit(EXIT_UNKNOWN_FILE);
+		exit(4); // EXIT_UNKNOWN_FILE
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/core/Input.php b/system/core/Input.php
index 1408da2..5ffe43d 100644
--- a/system/core/Input.php
+++ b/system/core/Input.php
@@ -754,7 +754,7 @@
 			{
 				set_status_header(503);
 				echo 'Disallowed Key Characters.';
-				exit(EXIT_USER_INPUT);
+				exit(7); // EXIT_USER_INPUT
 			}
 		}
 
diff --git a/system/core/Log.php b/system/core/Log.php
index 707964c..a949c3f 100644
--- a/system/core/Log.php
+++ b/system/core/Log.php
@@ -108,7 +108,7 @@
 		$this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
 			? ltrim($config['log_file_extension'], '.') : 'php';
 
-		file_exists($this->_log_path) OR mkdir($this->_log_path, DIR_WRITE_MODE, TRUE);
+		file_exists($this->_log_path) OR mkdir($this->_log_path, 0777, TRUE);
 
 		if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
 		{
@@ -170,7 +170,7 @@
 			}
 		}
 
-		if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
+		if ( ! $fp = @fopen($filepath, 'ab'))
 		{
 			return FALSE;
 		}
@@ -192,7 +192,7 @@
 
 		if (isset($newfile) && $newfile === TRUE)
 		{
-			@chmod($filepath, FILE_WRITE_MODE);
+			@chmod($filepath, 0666);
 		}
 
 		return is_int($result);
diff --git a/system/core/Output.php b/system/core/Output.php
index 7a35b02..df9ef07 100644
--- a/system/core/Output.php
+++ b/system/core/Output.php
@@ -555,7 +555,7 @@
 
 		$cache_path .= md5($uri);
 
-		if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
+		if ( ! $fp = @fopen($cache_path, 'w+b'))
 		{
 			log_message('error', 'Unable to write cache file: '.$cache_path);
 			return;
@@ -606,7 +606,7 @@
 
 		if (is_int($result))
 		{
-			@chmod($cache_path, FILE_WRITE_MODE);
+			@chmod($cache_path, 0666);
 			log_message('debug', 'Cache file written: '.$cache_path);
 
 			// Send HTTP cache-control headers to browser to match file cache settings.
@@ -639,7 +639,7 @@
 		$uri =	$CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
 		$filepath = $cache_path.md5($uri);
 
-		if ( ! file_exists($filepath) OR ! $fp = @fopen($filepath, FOPEN_READ))
+		if ( ! file_exists($filepath) OR ! $fp = @fopen($filepath, 'rb'))
 		{
 			return FALSE;
 		}
diff --git a/system/database/DB_cache.php b/system/database/DB_cache.php
index 0ab9c5d..b855ff2 100644
--- a/system/database/DB_cache.php
+++ b/system/database/DB_cache.php
@@ -158,12 +158,12 @@
 
 		if ( ! is_dir($dir_path))
 		{
-			if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
+			if ( ! @mkdir($dir_path, 0777))
 			{
 				return FALSE;
 			}
 
-			@chmod($dir_path, DIR_WRITE_MODE);
+			@chmod($dir_path, 0777);
 		}
 
 		if (write_file($dir_path.$filename, serialize($object)) === FALSE)
@@ -171,7 +171,7 @@
 			return FALSE;
 		}
 
-		@chmod($dir_path.$filename, FILE_WRITE_MODE);
+		@chmod($dir_path.$filename, 0666);
 		return TRUE;
 	}
 
diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php
index b004de3..fafce45 100644
--- a/system/database/DB_driver.php
+++ b/system/database/DB_driver.php
@@ -1676,7 +1676,7 @@
 
 		$error =& load_class('Exceptions', 'core');
 		echo $error->show_error($heading, $message, 'error_db');
-		exit(EXIT_DATABASE);
+		exit(8); // EXIT_DATABASE
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/libraries/Driver.php b/system/libraries/Driver.php
index d2e41d6..d15d34f 100644
--- a/system/libraries/Driver.php
+++ b/system/libraries/Driver.php
@@ -290,7 +290,7 @@
 
 		$trace = debug_backtrace();
 		_exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
-		exit(EXIT_UNKNOWN_METHOD);
+		exit(6); // EXIT_UNKNOWN_METHOD
 	}
 
 	// --------------------------------------------------------------------
diff --git a/system/libraries/Email.php b/system/libraries/Email.php
index fac8a49..1f2f6a9 100644
--- a/system/libraries/Email.php
+++ b/system/libraries/Email.php
@@ -732,7 +732,7 @@
 				return FALSE;
 			}
 
-			if ( ! $fp = @fopen($file, FOPEN_READ))
+			if ( ! $fp = @fopen($file, 'rb'))
 			{
 				$this->_set_error_message('lang:email_attachment_unreadable', $file);
 				return FALSE;
diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php
index ac3db41..df0df3f 100644
--- a/system/libraries/Image_lib.php
+++ b/system/libraries/Image_lib.php
@@ -734,7 +734,7 @@
 		{
 			if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
 			{
-				@chmod($this->full_dst_path, FILE_WRITE_MODE);
+				@chmod($this->full_dst_path, 0666);
 			}
 
 			return TRUE;
@@ -811,7 +811,7 @@
 		imagedestroy($src_img);
 
 		// Set the file to 666
-		@chmod($this->full_dst_path, FILE_WRITE_MODE);
+		@chmod($this->full_dst_path, 0666);
 
 		return TRUE;
 	}
@@ -881,7 +881,7 @@
 		}
 
 		// Set the file to 777
-		@chmod($this->full_dst_path, FILE_WRITE_MODE);
+		@chmod($this->full_dst_path, 0666);
 
 		return TRUE;
 	}
@@ -969,7 +969,7 @@
 		// we have to rename the temp file.
 		copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
 		unlink($this->dest_folder.'netpbm.tmp');
-		@chmod($this->full_dst_path, FILE_WRITE_MODE);
+		@chmod($this->full_dst_path, 0666);
 
 		return TRUE;
 	}
@@ -1014,7 +1014,7 @@
 		imagedestroy($src_img);
 
 		// Set the file to 777
-		@chmod($this->full_dst_path, FILE_WRITE_MODE);
+		@chmod($this->full_dst_path, 0666);
 
 		return TRUE;
 	}
@@ -1087,7 +1087,7 @@
 		imagedestroy($src_img);
 
 		// Set the file to 777
-		@chmod($this->full_dst_path, FILE_WRITE_MODE);
+		@chmod($this->full_dst_path, 0666);
 
 		return TRUE;
 	}
diff --git a/system/libraries/Zip.php b/system/libraries/Zip.php
index 43abfba..c634b11 100644
--- a/system/libraries/Zip.php
+++ b/system/libraries/Zip.php
@@ -398,7 +398,7 @@
 	 */
 	public function archive($filepath)
 	{
-		if ( ! ($fp = @fopen($filepath, FOPEN_WRITE_CREATE_DESTRUCTIVE)))
+		if ( ! ($fp = @fopen($filepath, 'w+b')))
 		{
 			return FALSE;
 		}