395 lines
9.3 KiB
PHP
395 lines
9.3 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks.
|
|
*
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
*
|
|
* Copyright (C) 2015 TechFuze
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* @author TechFuze
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
*
|
|
* @link http://techfuze.net/fuzeworks
|
|
* @since Version 0.0.1
|
|
*
|
|
* @version Version 1.0.0
|
|
*/
|
|
|
|
use FuzeWorks\Logger;
|
|
|
|
/**
|
|
* FuzeWorks Common Functions
|
|
*
|
|
* Converted from CodeIgniter.
|
|
*
|
|
* Loads the base classes and executes the request.
|
|
*
|
|
* @package FuzeWorks
|
|
* @subpackage FuzeWorks
|
|
* @category Common Functions
|
|
* @author EllisLab Dev Team
|
|
* @link https://codeigniter.com/user_guide/
|
|
*/
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('is_php'))
|
|
{
|
|
/**
|
|
* Determines if the current version of PHP is equal to or greater than the supplied value
|
|
*
|
|
* @param string
|
|
* @return bool TRUE if the current version is $version or higher
|
|
*/
|
|
function is_php($version)
|
|
{
|
|
static $_is_php;
|
|
$version = (string) $version;
|
|
|
|
if ( ! isset($_is_php[$version]))
|
|
{
|
|
$_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
|
|
}
|
|
|
|
return $_is_php[$version];
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('get_mimes'))
|
|
{
|
|
/**
|
|
* Returns the MIME types array from config/mimes.php
|
|
*
|
|
* @return array
|
|
*/
|
|
function &get_mimes()
|
|
{
|
|
static $_mimes;
|
|
|
|
if (empty($_mimes))
|
|
{
|
|
$_mimes = FuzeWorks\Factory::getInstance()->config->get('mimes');
|
|
}
|
|
|
|
return $_mimes;
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('is_https'))
|
|
{
|
|
/**
|
|
* Is HTTPS?
|
|
*
|
|
* Determines if the application is accessed via an encrypted
|
|
* (HTTPS) connection.
|
|
*
|
|
* @return bool
|
|
*/
|
|
function is_https()
|
|
{
|
|
if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
|
|
{
|
|
return TRUE;
|
|
}
|
|
elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
|
|
{
|
|
return TRUE;
|
|
}
|
|
elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('is_cli'))
|
|
{
|
|
|
|
/**
|
|
* Is CLI?
|
|
*
|
|
* Test to see if a request was made from the command line.
|
|
*
|
|
* @return bool
|
|
*/
|
|
function is_cli()
|
|
{
|
|
return (PHP_SAPI === 'cli' OR defined('STDIN'));
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('set_status_header'))
|
|
{
|
|
/**
|
|
* Set HTTP Status Header
|
|
*
|
|
* @param int the status code
|
|
* @param string
|
|
* @return void
|
|
*/
|
|
function set_status_header($code = 200, $text = '')
|
|
{
|
|
if (is_cli())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (empty($code) OR ! is_numeric($code))
|
|
{
|
|
throw new \FuzeWorks\Exception\Exception('Status codes must be numeric', 1);
|
|
}
|
|
|
|
if (empty($text))
|
|
{
|
|
is_int($code) OR $code = (int) $code;
|
|
$stati = array(
|
|
100 => 'Continue',
|
|
101 => 'Switching Protocols',
|
|
|
|
200 => 'OK',
|
|
201 => 'Created',
|
|
202 => 'Accepted',
|
|
203 => 'Non-Authoritative Information',
|
|
204 => 'No Content',
|
|
205 => 'Reset Content',
|
|
206 => 'Partial Content',
|
|
|
|
300 => 'Multiple Choices',
|
|
301 => 'Moved Permanently',
|
|
302 => 'Found',
|
|
303 => 'See Other',
|
|
304 => 'Not Modified',
|
|
305 => 'Use Proxy',
|
|
307 => 'Temporary Redirect',
|
|
|
|
400 => 'Bad Request',
|
|
401 => 'Unauthorized',
|
|
402 => 'Payment Required',
|
|
403 => 'Forbidden',
|
|
404 => 'Not Found',
|
|
405 => 'Method Not Allowed',
|
|
406 => 'Not Acceptable',
|
|
407 => 'Proxy Authentication Required',
|
|
408 => 'Request Timeout',
|
|
409 => 'Conflict',
|
|
410 => 'Gone',
|
|
411 => 'Length Required',
|
|
412 => 'Precondition Failed',
|
|
413 => 'Request Entity Too Large',
|
|
414 => 'Request-URI Too Long',
|
|
415 => 'Unsupported Media Type',
|
|
416 => 'Requested Range Not Satisfiable',
|
|
417 => 'Expectation Failed',
|
|
422 => 'Unprocessable Entity',
|
|
|
|
500 => 'Internal Server Error',
|
|
501 => 'Not Implemented',
|
|
502 => 'Bad Gateway',
|
|
503 => 'Service Unavailable',
|
|
504 => 'Gateway Timeout',
|
|
505 => 'HTTP Version Not Supported'
|
|
);
|
|
|
|
if (isset($stati[$code]))
|
|
{
|
|
$text = $stati[$code];
|
|
}
|
|
else
|
|
{
|
|
show_error('No status text available. Please check your status code number or supply your own message text.', 500);
|
|
}
|
|
}
|
|
|
|
if (strpos(PHP_SAPI, 'cgi') === 0)
|
|
{
|
|
header('Status: '.$code.' '.$text, TRUE);
|
|
}
|
|
else
|
|
{
|
|
$server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
|
|
header($server_protocol.' '.$code.' '.$text, TRUE, $code);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('remove_invisible_characters'))
|
|
{
|
|
/**
|
|
* Remove Invisible Characters
|
|
*
|
|
* This prevents sandwiching null characters
|
|
* between ascii characters, like Java\0script.
|
|
*
|
|
* @param string
|
|
* @param bool
|
|
* @return string
|
|
*/
|
|
function remove_invisible_characters($str, $url_encoded = TRUE)
|
|
{
|
|
$non_displayables = array();
|
|
|
|
// every control character except newline (dec 10),
|
|
// carriage return (dec 13) and horizontal tab (dec 09)
|
|
if ($url_encoded)
|
|
{
|
|
$non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15
|
|
$non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31
|
|
}
|
|
|
|
$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127
|
|
|
|
do
|
|
{
|
|
$str = preg_replace($non_displayables, '', $str, -1, $count);
|
|
}
|
|
while ($count);
|
|
|
|
return $str;
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('html_escape'))
|
|
{
|
|
/**
|
|
* Returns HTML escaped variable.
|
|
*
|
|
* @param mixed $var The input string or array of strings to be escaped.
|
|
* @param bool $double_encode $double_encode set to FALSE prevents escaping twice.
|
|
* @return mixed The escaped string or array of strings as a result.
|
|
*/
|
|
function html_escape($var, $double_encode = TRUE)
|
|
{
|
|
if (empty($var))
|
|
{
|
|
return $var;
|
|
}
|
|
|
|
if (is_array($var))
|
|
{
|
|
foreach (array_keys($var) as $key)
|
|
{
|
|
$var[$key] = html_escape($var[$key], $double_encode);
|
|
}
|
|
|
|
return $var;
|
|
}
|
|
|
|
return htmlspecialchars($var, ENT_QUOTES, FuzeWorks\Factory::getInstance()->config->get('main')->charset, $double_encode);
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('_stringify_attributes'))
|
|
{
|
|
/**
|
|
* Stringify attributes for use in HTML tags.
|
|
*
|
|
* Helper function used to convert a string, array, or object
|
|
* of attributes to a string.
|
|
*
|
|
* @param mixed string, array, object
|
|
* @param bool
|
|
* @return string
|
|
*/
|
|
function _stringify_attributes($attributes, $js = FALSE)
|
|
{
|
|
$atts = NULL;
|
|
|
|
if (empty($attributes))
|
|
{
|
|
return $atts;
|
|
}
|
|
|
|
if (is_string($attributes))
|
|
{
|
|
return ' '.$attributes;
|
|
}
|
|
|
|
$attributes = (array) $attributes;
|
|
|
|
foreach ($attributes as $key => $val)
|
|
{
|
|
$atts .= ($js) ? $key.'='.$val.',' : ' '.$key.'="'.$val.'"';
|
|
}
|
|
|
|
return rtrim($atts, ',');
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
if ( ! function_exists('function_usable'))
|
|
{
|
|
/**
|
|
* 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.
|
|
*
|
|
* @link http://www.hardened-php.net/suhosin/
|
|
* @param string $function_name Function to check for
|
|
* @return bool TRUE if the function exists and is safe to call,
|
|
* FALSE otherwise.
|
|
*/
|
|
function function_usable($function_name)
|
|
{
|
|
static $_suhosin_func_blacklist;
|
|
|
|
if (function_exists($function_name))
|
|
{
|
|
if ( ! isset($_suhosin_func_blacklist))
|
|
{
|
|
$_suhosin_func_blacklist = extension_loaded('suhosin')
|
|
? explode(',', trim(ini_get('suhosin.executor.func.blacklist')))
|
|
: array();
|
|
}
|
|
|
|
return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
}
|