/app/classes/util.php
PHP | 2206 lines | 1172 code | 217 blank | 817 comment | 312 complexity | ee486821d664e86ae6894f5f25fbd5b9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * util.php
- *
- * util.php is a library of helper functions for common tasks such as
- * formatting bytes as a string or displaying a date in terms of how long ago
- * it was in human readable terms (E.g. 4 minutes ago). The library is entirely
- * contained within a single file and hosts no dependencies. The library is
- * designed to avoid any possible conflicts.
- *
- * @author Brandon Wamboldt
- * @link http://github.com/brandonwamboldt/utilphp/ Official Documentation
- * @version 1.0.003
- */
- if ( ! class_exists( 'util' ) ) {
- class util
- {
- /**
- * A constant representing the number of seconds in a minute, for
- * making code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_MINUTE = 60;
- /**
- * A constant representing the number of seconds in an hour, for making
- * code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_HOUR = 3600;
- const SECONDS_IN_AN_HOUR = 3600;
- /**
- * A constant representing the number of seconds in a day, for making
- * code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_DAY = 86400;
- /**
- * A constant representing the number of seconds in a week, for making
- * code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_WEEK = 604800;
- /**
- * A constant representing the number of seconds in a month (30 days),
- * for making code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_MONTH = 2592000;
- /**
- * A constant representing the number of seconds in a year (365 days),
- * for making code more verbose
- *
- * @since 1.0.000
- * @var int
- */
- const SECONDS_IN_A_YEAR = 31536000;
- /**
- * A collapse icon, using in the dump_var function to allow collapsing
- * an array or object
- *
- * @access public
- * @since 1.0.000
- * @static
- * @var string
- */
- public static $icon_collapse = 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAMAAADXT/YiAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo3MjlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFNzFDNDQyNEMyQzkxMUUxOTU4MEM4M0UxRDA0MUVGNSIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFNzFDNDQyM0MyQzkxMUUxOTU4MEM4M0UxRDA0MUVGNSIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo3NDlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo3MjlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PuF4AWkAAAA2UExURU9t2DBStczM/1h16DNmzHiW7iNFrypMvrnD52yJ4ezs7Onp6ejo6P///+Tk5GSG7D9h5SRGq0Q2K74AAAA/SURBVHjaLMhZDsAgDANRY3ZISnP/y1ZWeV+jAeuRSky6cKL4ryDdSggP8UC7r6GvR1YHxjazPQDmVzI/AQYAnFQDdVSJ80EAAAAASUVORK5CYII=';
- /**
- * A collapse icon, using in the dump_var function to allow collapsing
- * an array or object
- *
- * @access public
- * @since 1.0.000
- * @static
- * @var string
- */
- public static $icon_expand = 'iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAMAAADXT/YiAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2RpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo3MTlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpFQzZERTJDNEMyQzkxMUUxODRCQzgyRUNDMzZEQkZFQiIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpFQzZERTJDM0MyQzkxMUUxODRCQzgyRUNDMzZEQkZFQiIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo3MzlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo3MTlFRjQ2NkM5QzJFMTExOTA0MzkwRkI0M0ZCODY4RCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PkmDvWIAAABIUExURU9t2MzM/3iW7ubm59/f5urq85mZzOvr6////9ra38zMzObm5rfB8FZz5myJ4SNFrypMvjBStTNmzOvr+mSG7OXl8T9h5SRGq/OfqCEAAABKSURBVHjaFMlbEoAwCEPRULXF2jdW9r9T4czcyUdA4XWB0IgdNSybxU9amMzHzDlPKKu7Fd1e6+wY195jW0ARYZECxPq5Gn8BBgCr0gQmxpjKAwAAAABJRU5ErkJggg==';
- /**
- * Retrieve a value from the $_POST array, or return a given default if
- * the index isn't set
- *
- * The first n parameters represent the fields to retrieve, being a
- * single index or an array of indexes to access multi-level arrays.
- *
- * Calling the function as util::post_var( ['tags', '1412'] ) is
- * identical to using $_POST['tags']['1412'].
- *
- * @param string $fields The name of the field to retrieve
- * @param mixed $default A default value to return if the
- * requested variable isn't set
- * @return mixed
- *
- * @see array_get()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function post_var( $fields, $default = NULL )
- {
- return self::array_get( $_POST, $fields, $default );
- }
- /**
- * Retrieve a value from the $_GET array, or return a given default if
- * the index isn't set
- *
- * The first n parameters represent the fields to retrieve, being a
- * single index or an array of indexes to access multi-level arrays.
- *
- * Calling the function as util::get_var( ['tags', '1412'] ) is
- * identical to using $_GET['tags']['1412'].
- *
- * @param string $fields The name of the field to retrieve
- * @param mixed $default A default value to return if the
- * requested variable isn't set
- * @return mixed
- *
- * @see array_get()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function get_var( $fields, $default = NULL )
- {
- return self::array_get( $_GET, $fields, $default );
- }
- /**
- * Retrieve a value from the $_GET or the $_POST array, or return a
- * given default if the index isn't set. You may expect this function
- * to check the $_REQUEST variable, but the desired behavior is often
- * to check $_GET or $_POST. To avoid screwing stuff up if $_COOKIE is
- * set, and to avoid relying on the user to set the request_order
- * option, we just make that assumption for them.
- *
- * The first n parameters represent the fields to retrieve, being a
- * single index or an array of indexes to access multi-level arrays.
- *
- * Calling the function as util::request_var( ['tags', '1412'] ) is
- * identical to using $_REQUEST['tags']['1412'].
- *
- * @param string $fields The name of the field to retrieve
- * @param mixed $default A default value to return if the requested variable isn't set
- * @return mixed
- *
- * @see array_get()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function request_var( $fields, $default = NULL )
- {
- if ( strstr( ini_get( 'request_order' ), 'GP' ) ) {
- return self::array_get( array_merge( $_POST, $_GET ), $fields, $default );
- } else {
- return self::array_get( array_merge( $_GET, $_POST ), $fields, $default );
- }
- }
- /**
- * Retrieve a value from the $_SESSION array, or return a given default
- * if the index isn't set
- *
- * The first n parameters represent the fields to retrieve, being a
- * single index or an array of indexes to access multi-level arrays.
- *
- * Calling the function as util::session_var( ['tags', '1412'] ) is
- * identical to using $_SESSION['tags']['1412'].
- *
- * @param string $fields The name of the field to retrieve
- * @param mixed $default A default value to return if the
- * requested variable isn't set
- * @return mixed
- *
- * @see array_get()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function session_var( $fields, $default = NULL )
- {
- return self::array_get( $_SESSION, $fields, $default );
- }
- /**
- * Retrieve a value from the $_COOKIE array, or return a given default
- * if the index isn't set
- *
- * The first n parameters represent the fields to retrieve, being a
- * single index or an array of indexes to access multi-level arrays.
- *
- * Calling the function as util::cookie_var( ['tags', '1412'] ) is
- * identical to using $_COOKIE['tags']['1412'].
- *
- * @param string $fields The name of the field to retrieve
- * @param mixed $default A default value to return if the
- * requested variable isn't set
- * @return mixed
- *
- * @see array_get()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function cookie_var( $fields, $default = NULL )
- {
- return self::array_get( $_COOKIE, $fields, $default );
- }
- /**
- * Access an array index, retrieving the value stored there if it
- * exists or a default if it does not. This function allows you to
- * concisely access an index which may or may not exist without
- * raising a warning
- *
- * @param array $var Array to access
- * @param string $field Index to access in the array
- * @param mixed $default Default value to return if the key is not
- * present in the array
- * @return mixed
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function array_get( array $array, $fields, $default = NULL )
- {
- if ( ! is_array( $array ) ) {
- return $default;
- } else if ( ! is_array( $fields ) ) {
- if ( isset( $array[$fields] ) ) {
- return $array[$fields];
- } else {
- return $default;
- }
- } else {
- foreach ( $fields as $field ) {
- $found_it = false;
- if ( ! is_array( $array ) ) {
- break;
- }
- foreach ( $array as $key => $value ) {
- if ( $key == $field ) {
- $found_it = true;
- $array = $value;
- break;
- }
- }
- }
- if ( $found_it ) {
- return $array;
- } else {
- return $default;
- }
- }
- }
- /**
- * Display a variable's contents using nice HTML formatting and will
- * properly display the value of booleans as true or false
- *
- * @param mixed $var The variable to dump
- * @return string
- *
- * @see var_dump_plain()
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function var_dump( $var, $return = false )
- {
- $html = '<pre style="margin-bottom: 18px;' .
- 'background: #f7f7f9;' .
- 'border: 1px solid #e1e1e8;' .
- 'padding: 8px;' .
- 'border-radius: 4px;' .
- '-moz-border-radius: 4px;' .
- '-webkit-border radius: 4px;' .
- 'display: block;' .
- 'font-size: 12.05px;' .
- 'white-space: pre-wrap;' .
- 'word-wrap: break-word;' .
- 'color: #333;' .
- 'text-align: left;' .
- 'font-family: Menlo,Monaco,Consolas,\'Courier New\',monospace;">';
- $html .= self::var_dump_plain( $var, true );
- $html .= '</pre>';
- if ( ! $return ) {
- echo $html;
- } else {
- return $html;
- }
- }
- /**
- * Display a variable's contents using nice HTML formatting (Without
- * the <pre> tag) and will properly display the values of variables
- * like booleans and resources. Supports collapsable arrays and objects
- * as well.
- *
- * @param mixed $var The variable to dump
- * @return string
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function var_dump_plain( $var, $traversedeeper = false )
- {
- // Don't traverse into Closures or Silex / Symfony objects..
- if (is_object($var)) {
- list($root) = explode("\\", get_class($var));
- // echo "[ " .$root . " - " .get_class($var)." ]";
- if ( !$traversedeeper && ( (get_class($var) == "Bolt\\Application") || in_array($root, array('Closure', 'Silex', 'Symfony')) || substr($root, 0, 5)=="Twig_" )) {
- $html = '<span style="color:#588bff;">object</span>(' . get_class( $var ) . ') ';
- // echo "[return]\n";
- return $html;
- }
- // echo "[stay]\n";
- }
- $html = '';
- if ( is_bool( $var ) ) {
- $html .= '<span style="color:#588bff;">boo</span><span style="color:#999;">(</span><strong>' . ( ( $var ) ? 'true' : 'false' ) . '</strong><span style="color:#999;">)</span>';
- } else if ( is_int( $var ) ) {
- $html .= '<span style="color:#588bff;">int</span><span style="color:#999;">(</span><strong>' . $var . '</strong><span style="color:#999;">)</span>';
- } else if ( is_float( $var ) ) {
- $html .= '<span style="color:#588bff;">flo</span><span style="color:#999;">(</span><strong>' . $var . '</strong><span style="color:#999;">)</span>';
- } else if ( is_string( $var ) ) {
- $html .= '<span style="color:#588bff;">str</span><span style="color:#999;">(</span>' . strlen( $var ) . '<span style="color:#999;">)</span> <strong>"' . self::htmlentities( $var ) . '"</strong>';
- } else if ( is_null( $var ) ) {
- $html .= '<strong>NULL</strong>';
- } else if ( is_resource( $var ) ) {
- $html .= '<span style="color:#588bff;">res</span>("' . get_resource_type( $var ) . '") <strong>"' . $var . '"</strong>';
- } else if ( is_array( $var ) ) {
- $uuid = 'include-php-' . uniqid();
- $html .= '<span style="color:#588bff;">arr</span>(' . count( $var ) . ')';
- if ( ! empty( $var ) ) {
- $html .= ' <img id="' . $uuid . '" data-collapse="data:image/png;base64,' . self::$icon_collapse . '" style="position:relative;left:-5px;top:-1px;cursor:pointer;width:9px!important;height:9px!important;" src="data:image/png;base64,' . self::$icon_expand . '" /> <span id="' . $uuid . '-collapsable" style="display: none;">[<br />';
- $indent = 4;
- $longest_key = 0;
- foreach( $var as $key => $value ) {
- if ( is_string( $key ) ) {
- $longest_key = max( $longest_key, strlen( $key ) + 2 );
- } else {
- $longest_key = max( $longest_key, strlen( $key ) );
- }
- }
- foreach ( $var as $key => $value ) {
- if ( is_numeric( $key ) ) {
- $html .= str_repeat( ' ', $indent ) . str_pad( $key, $longest_key, '_');
- } else {
- $html .= str_repeat( ' ', $indent ) . str_pad( '"' . self::htmlentities( $key ) . '"', $longest_key, ' ' );
- }
- $html .= ' => ';
- $value = explode( '<br />', self::var_dump_plain( $value ) );
- foreach ( $value as $line => $val ) {
- if ( $line != 0 ) {
- $value[$line] = str_repeat( ' ', $indent * 2 ) . $val;
- }
- }
- $html .= implode( '<br />', $value ) . '<br />';
- }
- $html .= ']</span>';
- $html .= preg_replace( '/ +/', ' ', '<script type="text/javascript">(function() {
- var img = document.getElementById("' . $uuid . '");
- img.onclick = function() {
- if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
- document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
- img.setAttribute( "data-expand", img.getAttribute("src") );
- img.src = img.getAttribute("data-collapse");
- var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
- } else {
- document.getElementById("' . $uuid . '-collapsable").style.display = "none";
- img.src = img.getAttribute("data-expand");
- var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
- }
- };
- })();
- </script>' );
- }
- } else if ( is_object( $var ) ) {
- $uuid = 'include-php-' . uniqid();
- $html .= '<span style="color:#588bff;">object</span>(' . get_class( $var ) . ') <img id="' . $uuid . '" data-collapse="data:image/png;base64,' . self::$icon_collapse . '" style="position:relative;left:-5px;top:-1px;cursor:pointer;width:9px!important;height:9px!important;" src="data:image/png;base64,' . self::$icon_expand . '" /> <span id="' . $uuid . '-collapsable" style="display: none;">[<br />';
- $original = $var;
- $var = (array) $var;
- $indent = 4;
- $longest_key = 0;
- foreach( $var as $key => $value ) {
- if ( substr( $key, 0, 2 ) == "\0*" ) {
- unset( $var[$key] );
- $key = 'protected:' . substr( $key, 2 );
- $var[$key] = $value;
- } else if ( substr( $key, 0, 1 ) == "\0" ) {
- unset( $var[$key] );
- $key = 'private:' . substr( $key, 1, strpos( substr( $key, 1 ), "\0" ) ) . ':' . substr( $key, strpos( substr( $key, 1 ), "\0" ) + 1 );
- $var[$key] = $value;
- }
- if ( is_string( $key ) ) {
- $longest_key = max( $longest_key, strlen( $key ) + 2 );
- } else {
- $longest_key = max( $longest_key, strlen( $key ) );
- }
- }
- foreach ( $var as $key => $value ) {
- if ( is_numeric( $key ) ) {
- $html .= str_repeat( ' ', $indent ) . str_pad( $key, $longest_key, ' ');
- } else {
- $html .= str_repeat( ' ', $indent ) . str_pad( '"' . self::htmlentities( $key ) . '"', $longest_key, ' ' );
- }
- $html .= ' => ';
- $value = explode( '<br />', self::var_dump_plain( $value ) );
- foreach ( $value as $line => $val ) {
- if ( $line != 0 ) {
- $value[$line] = str_repeat( ' ', $indent * 2 ) . $val;
- }
- }
- $html .= implode( '<br />', $value ) . '<br />';
- }
- $html .= ']</span>';
- $html .= preg_replace( '/ +/', ' ', '<script type="text/javascript">(function() {
- var img = document.getElementById("' . $uuid . '");
- img.onclick = function() {
- if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
- document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
- img.setAttribute( "data-expand", img.getAttribute("src") );
- img.src = img.getAttribute("data-collapse");
- var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
- } else {
- document.getElementById("' . $uuid . '-collapsable").style.display = "none";
- img.src = img.getAttribute("data-expand");
- var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
- }
- };
- })();
- </script>' );
- }
- return $html;
- }
- /**
- * Converts any accent characters to their equivalent normal characters
- * and converts any other non-alphanumeric characters to dashes, then
- * converts any sequence of two or more dashes to a single dash. This
- * function generates slugs safe for use as URLs, and if you pass true
- * as the second parameter, it will create strings safe for use as CSS
- * classes or IDs
- *
- * @param string $string A string to convert to a slug
- * @param bool $css_mode Whether or not to generate strings safe
- * for CSS classes/IDs (Default to false)
- * @return string
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function slugify( $string, $css_mode = false )
- {
- $slug = preg_replace( '/([^A-Za-z0-9\-]+)/', '-', strtolower( self::remove_accents( $string ) ) );
- $slug = preg_replace( '/(\-+)/', '-', $slug );
- if ( $css_mode ) {
- $digits = array( 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' );
- if ( is_numeric( substr( $slug, 0, 1 ) ) ) {
- $slug = $digits[substr( $slug, 0, 1 )] . substr( $slug, 1 );
- }
- }
- return $slug;
- }
- /**
- * Converts a string to UTF-8 without the need to specify the source
- * encoding
- *
- * @param string $string A string that may or may not be UTF-8
- * @return string
- *
- * @link https://github.com/facebook/libphutil/blob/master/src/utils/utf8.php
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function str_to_utf8( $string )
- {
- // Don't re-encode a UTF-8 string since that will mess it up
- if ( self::seems_utf8( $string ) ) {
- return $string;
- } else {
- // There is no function to do this in iconv, mbstring or ICU to
- // do this, so do it (very very slowly) in pure PHP.
- $result = array();
- $regex = "/([\x01-\x7F]" .
- "|[\xC2-\xDF][\x80-\xBF]" .
- "|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]" .
- "|[\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF])" .
- "|(.)/";
- $offset = 0;
- $matches = NULL;
- while ( preg_match( $regex, $string, $matches, 0, $offset ) ) {
- if ( ! isset( $matches[2] ) ) {
- $result[] = $matches[1];
- } else {
- // Unicode replacement character, U+FFFD.
- $result[] = "\xEF\xBF\xBD";
- }
- $offset += strlen( $matches[0] );
- }
- return implode( '', $result );
- }
- }
- /**
- * Checks to see if a string is utf8 encoded.
- *
- * NOTE: This function checks for 5-Byte sequences, UTF8
- * has Bytes Sequences with a maximum length of 4.
- *
- * @param string $string The string to be checked
- * @return bool
- *
- * @link https://github.com/facebook/libphutil/blob/master/src/utils/utf8.php
- *
- * @access public
- * @author bmorel@ssi.fr
- * @since 1.0.000
- * @static
- */
- public static function seems_utf8( $string )
- {
- if ( function_exists( 'mb_check_encoding' ) ) {
- // If mbstring is available, this is significantly faster than
- // using PHP regexps.
- return mb_check_encoding( $string, 'UTF-8' );
- }
- $regex = '/(
- | [\xF8-\xFF] # Invalid UTF-8 Bytes
- | [\xC0-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
- | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
- | [\xF0-\xF7](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
- | (?<=[\x0-\x7F\xF8-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
- | (?<![\xC0-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF7]|[\xF0-\xF7][\x80-\xBF]|[\xF0-\xF7][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
- | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
- | (?<=[\xF0-\xF7])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
- | (?<=[\xF0-\xF7][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
- )/x';
- return ! preg_match( $regex, $string );
- }
- /**
- * Nice formatting for computer sizes (Bytes)
- *
- * @param int $bytes The number in bytes to format
- * @param int $decimals The number of decimal points to include
- * @return string
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function size_format( $bytes, $decimals = 0 )
- {
- $bytes = floatval( $bytes );
- if ( $bytes < 1024 ) {
- return $bytes . ' B';
- } else if ( $bytes < pow( 1024, 2 ) ) {
- return number_format( $bytes / 1024, $decimals, '.', '' ) . ' KiB';
- } else if ( $bytes < pow( 1024, 3 ) ) {
- return number_format( $bytes / pow( 1024, 2 ), $decimals, '.', '' ) . ' MiB';
- } else if ( $bytes < pow( 1024, 4 ) ) {
- return number_format( $bytes / pow( 1024, 3 ), $decimals, '.', '' ) . ' GiB';
- } else if ( $bytes < pow( 1024, 5 ) ) {
- return number_format( $bytes / pow( 1024, 4 ), $decimals, '.', '' ) . ' TiB';
- } else if ( $bytes < pow( 1024, 6 ) ) {
- return number_format( $bytes / pow( 1024, 5 ), $decimals, '.', '' ) . ' PiB';
- } else {
- return number_format( $bytes / pow( 1024, 5 ), $decimals, '.', '' ) . ' PiB';
- }
- }
- /**
- * Serialize data, if needed.
- *
- * @param mixed $data Data that might need to be serialized
- * @return mixed
- *
- * @link http://codex.wordpress.org/Function_Reference/maybe_serialize
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function maybe_serialize( $data )
- {
- if ( is_array( $data ) || is_object( $data ) ) {
- return serialize( $data );
- }
- return $data;
- }
- /**
- * Unserialize value only if it is serialized
- *
- * @param string $data A variable that may or may not be serialized
- * @return mixed
- *
- * @link http://codex.wordpress.org/Function_Reference/maybe_unserialize
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function maybe_unserialize( $data )
- {
- // Don't attempt to unserialize data that isn't serialized
- if ( self::is_serialized( $data ) ) {
- return @unserialize( $data );
- }
- return $data;
- }
- /**
- * Check value to find if it was serialized.
- *
- * If $data is not an string, then returned value will always be false.
- * Serialized data is always a string.
- *
- * @param mixed $data Value to check to see if was serialized
- * @return bool
- *
- * @link http://codex.wordpress.org/Function_Reference/is_serialized
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function is_serialized( $data )
- {
- // If it isn't a string, it isn't serialized
- if ( ! is_string( $data ) ) {
- return false;
- }
- $data = trim( $data );
- if ( 'N;' == $data ) {
- return true;
- }
- $length = strlen( $data );
- if ( $length < 4 ) {
- return false;
- }
- if ( ':' !== $data[1] ) {
- return false;
- }
- $lastc = $data[$length - 1];
- if ( ';' !== $lastc && '}' !== $lastc ) {
- return false;
- }
- $token = $data[0];
- switch ( $token ) {
- case 's' :
- if ( '"' !== $data[$length-2] ) {
- return false;
- }
- case 'a' :
- case 'O' :
- return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
- case 'b' :
- case 'i' :
- case 'd' :
- return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
- }
- return false;
- }
- /**
- * Checks to see if the page is being server over SSL or not
- *
- * @return bool
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function is_https()
- {
- if ( isset( $_SERVER['HTTPS'] ) && ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Retrieve a modified URL query string.
- *
- * You can rebuild the URL and append a new query variable to the URL
- * query by using this function. You can also retrieve the full URL
- * with query data.
- *
- * Adding a single key & value or an associative array. Setting a key
- * value to an empty string removes the key. Omitting oldquery_or_uri
- * uses the $_SERVER value. Additional values provided are expected
- * to be encoded appropriately with urlencode() or rawurlencode().
- *
- * @param mixed $newkey Either newkey or an associative
- * array
- * @param mixed $newvalue Either newvalue or oldquery or uri
- * @param mixed $oldquery_or_uri Optionally the old query or uri
- * @return string
- *
- * @link http://codex.wordpress.org/Function_Reference/add_query_arg
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function add_query_arg()
- {
- $ret = '';
- // Was an associative array of key => value pairs passed?
- if ( is_array( func_get_arg( 0 ) ) ) {
- // Was the URL passed as an argument?
- if ( func_num_args() == 2 && func_get_arg( 1 ) ) {
- $uri = func_get_arg( 1 );
- } else if ( func_num_args() == 3 && func_get_arg( 2 ) ) {
- $uri = func_get_arg( 2 );
- } else {
- $uri = $_SERVER['REQUEST_URI'];
- }
- } else {
- // Was the URL passed as an argument?
- if ( func_num_args() == 3 && func_get_arg( 2 ) ) {
- $uri = func_get_arg( 2 );
- } else {
- $uri = $_SERVER['REQUEST_URI'];
- }
- }
- // Does the URI contain a fragment section (The part after the #)
- if ( $frag = strstr( $uri, '#' ) ) {
- $uri = substr( $uri, 0, -strlen( $frag ) );
- } else {
- $frag = '';
- }
- // Get the URI protocol if possible
- if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
- $protocol = $matches[0];
- $uri = substr( $uri, strlen( $protocol ) );
- } else {
- $protocol = '';
- }
- // Does the URI contain a query string?
- if ( strpos( $uri, '?' ) !== false ) {
- $parts = explode( '?', $uri, 2 );
- if ( 1 == count( $parts ) ) {
- $base = '?';
- $query = $parts[0];
- } else {
- $base = $parts[0] . '?';
- $query = $parts[1];
- }
- } else if ( ! empty( $protocol ) || strpos( $uri, '=' ) === false ) {
- $base = $uri . '?';
- $query = '';
- } else {
- $base = '';
- $query = $uri;
- }
- // Parse the query string into an array
- parse_str( $query, $qs );
- // This re-URL-encodes things that were already in the query string
- $qs = self::array_map_deep( $qs, 'urlencode' );
- if ( is_array( func_get_arg( 0 ) ) ) {
- $kayvees = func_get_arg( 0 );
- $qs = array_merge( $qs, $kayvees );
- } else {
- $qs[func_get_arg( 0 )] = func_get_arg( 1 );
- }
- foreach ( (array) $qs as $k => $v ) {
- if ( $v === false )
- unset( $qs[$k] );
- }
- $ret = http_build_query( $qs );
- $ret = trim( $ret, '?' );
- $ret = preg_replace( '#=(&|$)#', '$1', $ret );
- $ret = $protocol . $base . $ret . $frag;
- $ret = rtrim( $ret, '?' );
- return $ret;
- }
- /**
- * Removes an item or list from the query string.
- *
- * @param string|array $keys Query key or keys to remove.
- * @param bool $uri When false uses the $_SERVER value
- * @return string
- *
- * @link http://codex.wordpress.org/Function_Reference/remove_query_arg
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function remove_query_arg( $keys, $uri = false )
- {
- if ( is_array( $keys ) ) {
- foreach ( $keys as $key ) {
- $uri = self::add_query_arg( $key, false, $uri );
- }
- return $uri;
- }
- return self::add_query_arg( $keys, false, $uri );
- }
- /**
- * Converts many english words that equate to true or false to boolean
- *
- * Supports 'y', 'n', 'yes', 'no' and a few other variations
- *
- * @param string $string The string to convert to boolean
- * @param bool $default The value to return if we can't match any
- * yes/no words
- * @return bool
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function str_to_bool( $string, $default = false )
- {
- $yes_words = 'affirmative|all right|aye|indubitably|most assuredly|ok|of course|okay|sure thing|y|yes+|yea|yep|sure|yeah|true|t';
- $no_words = 'no*|no way|nope|nah|na|never|absolutely not|by no means|negative|never ever|false|f';
- if ( preg_match( '/^(' . $yes_words . ')$/i', $string ) ) {
- return true;
- } else if ( preg_match( '/^(' . $no_words . ')$/i', $string ) ) {
- return false;
- } else {
- return $default;
- }
- }
- /**
- * Return the absolute integer value of a given variable
- *
- * @param mixed $maybeint A variable that could be a string,
- * integer or other value
- * @return int
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function absint( $maybeint )
- {
- return abs( intval( $maybeint ) );
- }
- /**
- * Convert entities, while preserving already-encoded entities
- *
- * @param string $string The text to be converted
- * @return string
- *
- * @link http://ca2.php.net/manual/en/function.htmlentities.php#90111
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function htmlentities( $string, $preserve_encoded_entities = false )
- {
- if ( $preserve_encoded_entities ) {
- $translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
- $translation_table[chr(38)] = '&';
- return preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/', '&', strtr( $string, $translation_table ) );
- } else {
- return htmlentities( $string, ENT_QUOTES );
- }
- }
- /**
- * Convert >, <, ', " and & to html entities, but preserves entities
- * that are already encoded
- *
- * @param string $string The text to be converted
- * @return string
- *
- * @link http://ca2.php.net/manual/en/function.htmlentities.php#90111
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function htmlspecialchars( $string, $preserve_encoded_entities = false )
- {
- if ( $preserve_encoded_entities ) {
- $translation_table = get_html_translation_table( HTML_SPECIALCHARS, ENT_QUOTES );
- $translation_table[chr( 38 )] = '&';
- return preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/', '&', strtr( $string, $translation_table ) );
- } else {
- return htmlentities( $string, ENT_QUOTES );
- }
- }
- /**
- * Converts all accent characters to ASCII characters
- *
- * If there are no accent characters, then the string given is just
- * returned
- *
- * @param string $string Text that might have accent characters
- * @return string Filtered string with replaced "nice" characters
- *
- * @link http://codex.wordpress.org/Function_Reference/remove_accents
- *
- * @access public
- * @since 1.0.000
- * @static
- */
- public static function remove_accents( $string )
- {
- if ( ! preg_match( '/[\x80-\xff]/', $string ) ) {
- return $string;
- }
- if ( self::seems_utf8( $string ) ) {
- $chars = array(
- // Decompositions for Latin-1 Supplement
- chr(194).chr(170) => 'a', chr(194).chr(186) => 'o',
- chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
- chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
- chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
- chr(195).chr(134) => 'AE',chr(195).chr(135) => 'C',
- chr(195).chr(136) => 'E', chr(195).chr(137) => 'E',
- chr(195).chr(138) => 'E', chr(195).chr(139) => 'E',
- chr(195).chr(140) => 'I', chr(195).chr(141) => 'I',
- chr(195).chr(142) => 'I', chr(195).chr(143) => 'I',
- chr(195).chr(144) => 'D', chr(195).chr(145) => 'N',
- chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
- chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
- chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
- chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
- chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
- chr(195).chr(158) => 'TH',chr(195).chr(159) => 's',
- chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
- chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
- chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
- chr(195).chr(166) => 'ae',chr(195).chr(167) => 'c',
- chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
- chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
- chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
- chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
- chr(195).chr(176) => 'd', chr(195).chr(177) => 'n',
- chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
- chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
- chr(195).chr(182) => 'o', chr(195).chr(184) => 'o',
- chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
- chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
- chr(195).chr(189) => 'y', chr(195).chr(190) => 'th',
- chr(195).chr(191) => 'y', chr(195).chr(152) => 'O',
- // Decompositions for Latin Extended-A
- chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
- chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
- chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
- chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
- chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
- chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
- chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
- chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
- chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
- chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
- chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
- chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
- chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
- chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
- chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
- chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
- chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
- chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
- chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
- chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
- chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
- chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
- chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
- chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
- chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
- chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
- chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
- chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
- chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
- chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
- chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
- chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
- chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
- chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
- chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
- chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
- chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
- chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
- chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
- chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
- chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
- chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
- chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
- chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
- chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
- chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
- chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
- chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
- chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
- chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
- chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
- chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
- chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
- chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
- chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
- chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
- chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
- chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
- chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
- chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
- chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
- chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
- chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
- chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
- // Decompositions for Latin Extended-B
- chr(200).chr(152) => 'S', chr(200).chr(153) => 's',
- chr(200).chr(154) => 'T', chr(200).chr(155) => 't',
- // Euro Sign
- chr(226).chr(130).chr(172) => 'E',
- // GBP (Pound) Sign
- chr(194).chr(163) => ''
- );
- $string = strtr( $string, $chars );
- } else {
- // Assume ISO-8859-1 if not UTF-8
- $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
- .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
- .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
- .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
- .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
- .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
- .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
- .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
- .chr(244).c…
Large files files are truncated, but you can click here to view the full file