system/coreCommon.php

CodeIgniter

An open source application development framework for PHP

This content is released under the MIT License (MIT)

Copyright (c) 2014 - 2016, British Columbia Institute of Technology

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Functions

is_php()

is_php(  $version) : boolean

Determines if the current version of PHP is equal to or greater than the supplied value

Parameters

$version

Returns

boolean —

TRUE if the current version is $version or higher

is_really_writable()

is_really_writable(  $file) : boolean

Tests for file writability

is_writable() returns TRUE on Windows servers when you really can't write to the file, based on the read-only attribute. is_writable() is also unreliable on Unix servers if safe_mode is on.

Parameters

$file

Returns

boolean

load_class()

load_class(  $class,   $directory = 'libraries',   $param = NULL) : object

Class registry

This function acts as a singleton. If the requested class does not exist it is instantiated and set to a static variable. If it has previously been instantiated the variable is returned.

Parameters

$class
$directory
$param

Returns

object

is_loaded()

is_loaded(  $class = '') : array

Keeps track of which libraries have been loaded. This function is called by the load_class() function above

Parameters

$class

Returns

array

get_config()

get_config(array  $replace = array()) : array

Loads the main config.php file

This function lets us grab the config file even if the Config class hasn't been instantiated yet

Parameters

array $replace

Returns

array

config_item()

config_item(  $item) : mixed

Returns the specified config item

Parameters

$item

Returns

mixed

get_mimes()

get_mimes() : array

Returns the MIME types array from config/mimes.php

Returns

array

is_https()

is_https() : boolean

Is HTTPS?

Determines if the application is accessed via an encrypted (HTTPS) connection.

Returns

boolean

is_cli()

is_cli() : boolean

Is CLI?

Test to see if a request was made from the command line.

Returns

boolean

show_error()

show_error(  $message,   $status_code = 500,   $heading = 'An Error Was Encountered') : void

Error Handler

This function lets us invoke the exception class and display errors using the standard error template located in application/views/errors/error_general.php This function will send the error page directly to the browser and exit.

Parameters

$message
$status_code
$heading

show_404()

show_404(  $page = '',   $log_error = TRUE) : void

404 Page Handler

This function is similar to the show_error() function above However, instead of the standard error template it displays 404 errors.

Parameters

$page
$log_error

log_message()

log_message(  $level,   $message) : void

Error Logging Interface

We use this as a simple mechanism to access the logging class and send messages to be logged.

Parameters

$level
$message

set_status_header()

set_status_header(  $code = 200,   $text = '') : void

Set HTTP Status Header

Parameters

$code
$text

_error_handler()

_error_handler(integer  $severity, string  $message, string  $filepath, integer  $line) : void

Error Handler

This is the custom error handler that is declared at the (relative) top of CodeIgniter.php. The main reason we use this is to permit PHP errors to be logged in our own log files since the user may not have access to server logs. Since this function effectively intercepts PHP errors, however, we also need to display errors based on the current error_reporting level. We do that with the use of a PHP error template.

Parameters

integer $severity
string $message
string $filepath
integer $line

_exception_handler()

_exception_handler(\Exception  $exception) : void

Exception Handler

Sends uncaught exceptions to the logger and displays them only if display_errors is On so that they don't show up in production environments.

Parameters

\Exception $exception

_shutdown_handler()

_shutdown_handler() : void

Shutdown Handler

This is the shutdown handler that is declared at the top of CodeIgniter.php. The main reason we use this is to simulate a complete custom exception handler.

E_STRICT is purposively neglected because such events may have been caught. Duplication or none? None is preferred for now.

remove_invisible_characters()

remove_invisible_characters(  $str,   $url_encoded = TRUE) : string

Remove Invisible Characters

This prevents sandwiching null characters between ascii characters, like Java\0script.

Parameters

$str
$url_encoded

Returns

string

html_escape()

html_escape(mixed  $var, boolean  $double_encode = TRUE) : mixed

Returns HTML escaped variable.

Parameters

mixed $var

The input string or array of strings to be escaped.

boolean $double_encode

$double_encode set to FALSE prevents escaping twice.

Returns

mixed —

The escaped string or array of strings as a result.

_stringify_attributes()

_stringify_attributes(  $attributes,   $js = FALSE) : string

Stringify attributes for use in HTML tags.

Helper function used to convert a string, array, or object of attributes to a string.

Parameters

$attributes
$js

Returns

string

function_usable()

function_usable(string  $function_name) : boolean

Function usable

Executes a function_exists() check, and if the Suhosin PHP extension is loaded - checks whether the function that is checked might be disabled in there as well.

This is useful as function_exists() will return FALSE for functions disabled via the disable_functions php.ini setting, but not for suhosin.executor.func.blacklist and suhosin.executor.disable_eval. These settings will just terminate script execution if a disabled function is executed.

The above described behavior turned out to be a bug in Suhosin, but even though a fix was commited for 0.9.34 on 2012-02-12, that version is yet to be released. This function will therefore be just temporary, but would probably be kept for a few years.

Parameters

string $function_name

Function to check for

Returns

boolean —

TRUE if the function exists and is safe to call, FALSE otherwise.

p()

p(  $arr) 

Parameters

$arr