PHP Class Habari\Utils

Show file Open project: habari/system Class Usage Examples

Public Properties

Property Type Description
$debug_defined

Public Methods

Method Description
WSSE ( string | array $nonce = '', string $timestamp = '' ) : array Returns an array of tokens used for WSSE authentication http://www.xml.com/pub/a/2003/12/17/dive.html http://www.sixapart.com/developers/atom/protocol/atom_authentication.html
addslashes ( $value ) Adds slashes to escape strings, including strings in arrays
amp ( string $value ) : string Escape the ampersands in a URL for ouptut
archive_pages ( integer $item_total, integer $items_per_page = null ) : integer Returns the number of pages in an archive using the number of items per page set in options
array_map_field ( Countable $array, string $field, string $key = null ) : array Given an array of arrays, return an array that contains the value of a particular common field Example: $a = array( array('foo'=>1, 'bar'=>2), array('foo'=>3, 'bar'=>4), ); $b = Utils::array_map_field($a, 'foo'); // $b = array(1, 3);
array_or ( array $input ) : integer Does a bitwise OR of all the numbers in an array
atomtime ( mixed $t ) : string Returns RFC-3339 time from a time string or integer timestamp
check_request_method ( array $expected ) Checks whether the correct HTTP method was used for the request
class_only ( string | object $classname ) : string Strip the namespace off of the fully-qualified class name
crypt ( string $password, string $hash = null ) : string | boolean Crypt a given password, or verify a given password against a given hash.
de_amp ( string $value ) : boolean | string Return & entities in a URL querystring to their previous & glory, for use in redirects
debug ( ) Outputs a call stack with parameters, and a dump of the parameters passed.
debug_reveal ( $show, $hide, $debugid, $close = false ) Helper function used by debug() Not for external use.
end_in_slash ( string $value ) : string Forces a string to end in a single slash
env_is ( string $env, string $key = '_useenv' ) : boolean Are we in a specific environment?
env_test ( string $key = '_useenv' ) : boolean Are we in a testing environment?
firebacktrace ( array $backtrace ) : string Utils::firebacktrace()
firedebug ( ) Outputs debug information like ::debug() but using Firebug's Console.
get_ini_settings ( ) : Array Get a list of the PHP ini settings relevant to Habari
get_ip ( string $default = '0.0.0.0' ) : string Get the remote IP address, but try and take into account users who are behind proxies, whether they know it or not.
get_params ( mixed $params ) : array Returns an associative array of parameters, whether the input value is a querystring or an associative array.
getdate ( integer $timestamp ) : array Return an array of date information Just like getdate() but also returns 0-padded versions of day and month in mday0 and mon0
glob ( string $pattern, integer $flags ) : array Replacement for system glob that returns an empty array if there are no results
glob_to_regex ( string $glob ) : string Returns a regex pattern equivalent to the given glob pattern
html_attr ( array $attrs, integer $quote_flag = ENT_COMPAT, string $encoding = 'UTF-8', boolean $decode = true, boolean $double_encode = true ) : string Create a list of html element attributes from an associative array
html_checkboxes ( string $name, array $options ) : string Creates one or more HTML checkboxes
html_inputs ( array $options ) : string Creates one or more HTML inputs
html_select ( string $name, array $options, string $current = null, array $properties = [] ) : string Create an HTML select tag with options and a current value
htmlspecialchars ( string | array $string, integer $quote_flag = null, string $encoding = 'UTF-8', boolean $decode = true, boolean $double_encode = true ) : string Call htmlspecialchars() with the correct flags and encoding, without double escaping strings.
human_size ( integer $bytesize ) : string Produces a human-readable size string.
implode_quoted ( string $separator, array $values ) : string Behaves like the implode() function, except it quotes values that contain spaces
is_traversable ( mixed $data ) : boolean determines if the given that is travesable in foreach
locale_date ( string $format, integer $timestamp ) : string Return a formatted date/time trying to use strftime() AND date()
mail ( string $to, string $subject, string $message, array $headers = [], string $parameters = '' ) : boolean Send email
map_array ( string $value, string $prefix = '{$', string $postfix = '}' ) : string Used with array_map to create an array of PHP stringvar-style search/replace strings using optional pre/postfixes $mapped_values= array_map(array('\Habari\Utils', 'map_array'), $values);
md5 ( $password, $hash = null ) Crypt or verify a given password using MD5.
mimetype ( string $filename ) : string Return the mimetype of a file
nonce ( ) Returns a random 12-digit hex number
php_check_file_syntax ( $file, &$error = null ) Check the PHP syntax of (and execute) the specified file.
php_check_syntax ( string $code, null | string &$error = null ) : boolean Check the PHP syntax of the specified code.
placeholder_string ( integer $count ) : string Returns a string of question mark parameter placeholders.
quote_spaced ( string $value ) : string Adds quotes around values that have spaces in them
random_password ( integer $length = 10 ) : string Create a random password of a specific length
redirect ( string $url = '', boolean $continue = false ) Redirects the request to a new URL
regexdelim ( string $string, string $choices = null ) : string Convenience function to find a usable PCRE regular expression delimiter for a particular string. (I.e., some character that *isn't* found in the string.)
replace_shortcodes ( string $content, Object $obj_context ) : string Replace shortcodes in content with shortcode output
ror ( $v, $w ) Helper function for array_or
scheme_ports ( string $scheme = null ) : integer Return the port used for a specific URL scheme
setup_wsse ( ) : string Produce a set of inputs that can be used to validate WSSE
sha1 ( $password, $hash = null ) Crypt or verify a given password using SHA.
single_array ( mixed $element ) : array Convert a single non-array variable into an array with that one element
slugify ( string $string, string $separator = '-' ) : string Return a sanitized slug, replacing non-alphanumeric characters to dashes
ssha ( string $password, string $hash = null ) : string Crypt or verify a given password using SSHA.
ssha512 ( string $password, string $hash = null ) : string Crypt or verify a given password using SSHA512.
stripslashes ( $value ) Removes slashes from escaped strings, including strings in arrays
trail ( mixed $value = false ) : string Returns a trailing slash or a string, depending on the value passed in
truncate ( string $str, integer $len = 10, boolean $middle = true ) : string Trims longer phrases to shorter ones with elipsis in the middle
verify_wsse ( array $data, boolean $anyverb = false ) : boolean Verify WSSE values passed in.

Private Methods

Method Description
__construct ( ) Utils constructor This class should not be instantiated.

Method Details

WSSE() public static method

Returns an array of tokens used for WSSE authentication http://www.xml.com/pub/a/2003/12/17/dive.html http://www.sixapart.com/developers/atom/protocol/atom_authentication.html
public static WSSE ( string | array $nonce = '', string $timestamp = '' ) : array
$nonce string | array a string nonce or an existing array to add nonce parameters to
$timestamp string a timestamp
return array an array of WSSE authentication elements

addslashes() public static method

Adds slashes to escape strings, including strings in arrays
public static addslashes ( $value )

amp() public static method

Escape the ampersands in a URL for ouptut
public static amp ( string $value ) : string
$value string A URL for output
return string The URL with escaped ampersands

archive_pages() public static method

Returns the number of pages in an archive using the number of items per page set in options
public static archive_pages ( integer $item_total, integer $items_per_page = null ) : integer
$item_total integer Number of items in the archive
$items_per_page integer Number of items per page
return integer Number of pages based on pagination option.

array_map_field() public static method

Given an array of arrays, return an array that contains the value of a particular common field Example: $a = array( array('foo'=>1, 'bar'=>2), array('foo'=>3, 'bar'=>4), ); $b = Utils::array_map_field($a, 'foo'); // $b = array(1, 3);
public static array_map_field ( Countable $array, string $field, string $key = null ) : array
$array Countable An array of arrays or objects with similar keys or properties
$field string The name of a common field within each array/object
$key string Optional field to use as the key in the result array
return array An array of the values of the specified field within each array/object

array_or() public static method

Does a bitwise OR of all the numbers in an array
public static array_or ( array $input ) : integer
$input array An array of integers
return integer The bitwise OR of the input array

atomtime() public static method

Returns RFC-3339 time from a time string or integer timestamp
public static atomtime ( mixed $t ) : string
$t mixed A string of time or integer timestamp
return string An RFC-3339 formatted time

check_request_method() public static method

Checks whether the correct HTTP method was used for the request
public static check_request_method ( array $expected )
$expected array Expected HTTP methods for the request

class_only() public static method

Strip the namespace off of the fully-qualified class name
public static class_only ( string | object $classname ) : string
$classname string | object The fully-qualified name of the class, or an object instance
return string The class name, with namespace removed

crypt() public static method

Crypt a given password, or verify a given password against a given hash.
public static crypt ( string $password, string $hash = null ) : string | boolean
$password string the password to crypt or verify
$hash string (optional) if given, verify $password against $hash
return string | boolean Encrypted password, or boolean for verification

de_amp() public static method

Return & entities in a URL querystring to their previous & glory, for use in redirects
public static de_amp ( string $value ) : boolean | string
$value string A URL, maybe with a querystring
return boolean | string The valid, de-amped URL

debug() public static method

Outputs a call stack with parameters, and a dump of the parameters passed.
public static debug ( )

debug_reveal() public static method

Helper function used by debug() Not for external use.
public static debug_reveal ( $show, $hide, $debugid, $close = false )

end_in_slash() public static method

Forces a string to end in a single slash
public static end_in_slash ( string $value ) : string
$value string A string, usually a path
return string The string with the slash added or extra slashes removed, but with one slash only

env_is() public static method

Are we in a specific environment?
public static env_is ( string $env, string $key = '_useenv' ) : boolean
$env string The environment to test for
$key string The querystring key that can specify the environment to use
return boolean True if this is a test environment.

env_test() public static method

Are we in a testing environment?
public static env_test ( string $key = '_useenv' ) : boolean
$key string The querystring key that can specify the environment to use
return boolean True if this is a test environment.

firebacktrace() public static method

Utils::firebacktrace()
public static firebacktrace ( array $backtrace ) : string
$backtrace array An array of backtrace details from debug_backtrace()
return string Javascript output that will display the backtrace in the Firebug console.

firedebug() public static method

Outputs debug information like ::debug() but using Firebug's Console.
public static firedebug ( )

get_ini_settings() public static method

Get a list of the PHP ini settings relevant to Habari
public static get_ini_settings ( ) : Array
return Array The relevant PHP ini settings as array of strings

get_ip() public static method

Get the remote IP address, but try and take into account users who are behind proxies, whether they know it or not.
public static get_ip ( string $default = '0.0.0.0' ) : string
$default string a default IP address
return string The client's IP address.

get_params() public static method

Returns an associative array of parameters, whether the input value is a querystring or an associative array.
public static get_params ( mixed $params ) : array
$params mixed An associative array or querystring parameter list
return array An associative array of parameters

getdate() public static method

Return an array of date information Just like getdate() but also returns 0-padded versions of day and month in mday0 and mon0
public static getdate ( integer $timestamp ) : array
$timestamp integer A unix timestamp
return array An array of date data

glob() public static method

Replacement for system glob that returns an empty array if there are no results
public static glob ( string $pattern, integer $flags ) : array
$pattern string The glob() file search pattern
$flags integer Standard glob() flags
return array An array of result files, or an empty array if no results found

glob_to_regex() public static method

Returns a regex pattern equivalent to the given glob pattern
public static glob_to_regex ( string $glob ) : string
$glob string Glob to return
return string regex pattern with '/' delimiter

html_attr() public static method

Create a list of html element attributes from an associative array
public static html_attr ( array $attrs, integer $quote_flag = ENT_COMPAT, string $encoding = 'UTF-8', boolean $decode = true, boolean $double_encode = true ) : string
$attrs array An associative array of parameters
$quote_flag integer Sets what quotes and doublequotes are escaped
$encoding string The encoding of the passed string
$decode boolean Whether or not to unescape any html entities first
$double_encode boolean Whether or not to double escape any html entities
return string The parameters turned into a string of tag attributes

html_checkboxes() public static method

Creates one or more HTML checkboxes
public static html_checkboxes ( string $name, array $options ) : string
$name string The name of the checkbox element. If there are multiple checkboxes for the same name, this method will automatically apply "[]" at the end of the name
$options array An array of checkbox options. Each element should be an array containing "name" and "value". If the checkbox should be checked, it should have a "checked" element.
return string The HTML of the checkboxes

html_inputs() public static method

Creates one or more HTML inputs
public static html_inputs ( array $options ) : string
$options array An array of input elements. Each element should be an array containing "name", "value" and "type".
return string The HTML of the inputs

html_select() public static method

Create an HTML select tag with options and a current value
public static html_select ( string $name, array $options, string $current = null, array $properties = [] ) : string
$name string The name and id of the select control
$options array An associative array of values to use as the select options
$current string The value of the currently selected option
$properties array An associative array of additional properties to assign to the select control
return string The select control markup

htmlspecialchars() public static method

See http://php.net/manual/en/function.htmlspecialchars.php for details on the parameters and purpose of the function.
public static htmlspecialchars ( string | array $string, integer $quote_flag = null, string $encoding = 'UTF-8', boolean $decode = true, boolean $double_encode = true ) : string
$string string | array The string or array of strings to escape
$quote_flag integer Sets what quotes and doublequotes are escaped
$encoding string The encoding of the passed string
$decode boolean Whether or not to unescape any html entities first
$double_encode boolean Whether or not to double escape any html entities
return string The escaped string

human_size() public static method

For example, converts 12345 into 12.34KB
public static human_size ( integer $bytesize ) : string
$bytesize integer Number of bytes
return string Human-readable string

implode_quoted() public static method

Behaves like the implode() function, except it quotes values that contain spaces
public static implode_quoted ( string $separator, array $values ) : string
$separator string A separator between each value
$values array An array of values to separate
return string The concatenated string

is_traversable() public static method

determines if the given that is travesable in foreach
public static is_traversable ( mixed $data ) : boolean
$data mixed
return boolean

locale_date() public static method

Return a formatted date/time trying to use strftime() AND date()
public static locale_date ( string $format, integer $timestamp ) : string
$format string The format for the date. If it contains non-escaped percent signs, it uses strftime(), otherwise date()
$timestamp integer The unix timestamp of the time to format
return string The formatted time

mail() public static method

Send email
public static mail ( string $to, string $subject, string $message, array $headers = [], string $parameters = '' ) : boolean
$to string The destination address
$subject string The subject of the message
$message string The message itself
$headers array An array of key=>value pairs for additional email headers
$parameters string Additional parameters to mail()
return boolean True if sending the message succeeded

map_array() public static method

Used with array_map to create an array of PHP stringvar-style search/replace strings using optional pre/postfixes $mapped_values= array_map(array('\Habari\Utils', 'map_array'), $values);
public static map_array ( string $value, string $prefix = '{$', string $postfix = '}' ) : string
$value string The value to wrap
$prefix string The prefix for the returned value
$postfix string The postfix for the returned value
return string The wrapped value

md5() public static method

Passwords should not be stored using this method, but legacy systems might require it.
public static md5 ( $password, $hash = null )

mimetype() public static method

Return the mimetype of a file
public static mimetype ( string $filename ) : string
$filename string the path of a file
return string The mimetype of the file.

nonce() public static method

Returns a random 12-digit hex number
public static nonce ( )

php_check_file_syntax() public static method

Check the PHP syntax of (and execute) the specified file.
See also: Utils::php_check_syntax()
public static php_check_file_syntax ( $file, &$error = null )

php_check_syntax() public static method

Performs a syntax (lint) check on the specified code testing for scripting errors.
public static php_check_syntax ( string $code, null | string &$error = null ) : boolean
$code string The code string to be evaluated. It does not have to contain PHP opening tags.
$error null | string Intenal Memoizing parameter
return boolean Returns true if the lint check passed, and false if the link check failed.

placeholder_string() public static method

Useful when building, for instance, an IN() list for SQL
public static placeholder_string ( integer $count ) : string
$count integer Count of placeholders to put in the string
return string Placeholder string

quote_spaced() public static method

Adds quotes around values that have spaces in them
public static quote_spaced ( string $value ) : string
$value string A string value that might have spaces
return string The string value, quoted if it has spaces

random_password() public static method

Create a random password of a specific length
public static random_password ( integer $length = 10 ) : string
$length integer Length of the password, if not provded, 10
return string A random password

redirect() public static method

Redirects the request to a new URL
public static redirect ( string $url = '', boolean $continue = false )
$url string The URL to redirect to, or omit to redirect to the current url
$continue boolean Whether to continue processing the script (default false for security reasons, cf. #749)

regexdelim() public static method

Convenience function to find a usable PCRE regular expression delimiter for a particular string. (I.e., some character that *isn't* found in the string.)
public static regexdelim ( string $string, string $choices = null ) : string
$string string The string for which to find a delimiter.
$choices string Delimiters from which to choose one.
return string A valid regex delimiter, or null if none of the choices work.

replace_shortcodes() public static method

Replace shortcodes in content with shortcode output
public static replace_shortcodes ( string $content, Object $obj_context ) : string
$content string The content within which to replace shortcodes
$obj_context Object The object context in which the content was found
return string The content with shortcodes replaced

ror() public static method

Helper function for array_or
public static ror ( $v, $w )

scheme_ports() public static method

Return the port used for a specific URL scheme
public static scheme_ports ( string $scheme = null ) : integer
$scheme string The scheme in question
return integer the port used for the scheme

setup_wsse() public static method

Produce a set of inputs that can be used to validate WSSE
public static setup_wsse ( ) : string
return string HTML inputs for WSSE support

sha1() public static method

Passwords should not be stored using this method, but legacy systems might require it.
public static sha1 ( $password, $hash = null )

single_array() public static method

Convert a single non-array variable into an array with that one element
public static single_array ( mixed $element ) : array
$element mixed Some value, either an array or not
return array Either the original array value, or the passed value as the single element of an array

slugify() public static method

Return a sanitized slug, replacing non-alphanumeric characters to dashes
public static slugify ( string $string, string $separator = '-' ) : string
$string string The string to sanitize. Non-alphanumeric characters will be replaced by dashes
$separator string The slug separator, '-' by default
return string The sanitized slug

ssha() public static method

Implements the {Seeded,Salted}-SHA algorithm as per RfC 2307.
public static ssha ( string $password, string $hash = null ) : string
$password string the password to crypt or verify
$hash string (optional) if given, verify $password against $hash
return string Encrypted password, or boolean for verification

ssha512() public static method

Implements a modified version of the {Seeded,Salted}-SHA algorithm from RfC 2307, using SHA-512 instead of SHA-1. Requires the new hash*() functions.
public static ssha512 ( string $password, string $hash = null ) : string
$password string the password to crypt or verify
$hash string (optional) if given, verify $password against $hash
return string encrypted password, or boolean for verification

stripslashes() public static method

Removes slashes from escaped strings, including strings in arrays
public static stripslashes ( $value )

trail() public static method

Returns a trailing slash or a string, depending on the value passed in
public static trail ( mixed $value = false ) : string
$value mixed A trailing string value
return string A slash if true, the value if value passed, emptystring if false

truncate() public static method

Trims longer phrases to shorter ones with elipsis in the middle
public static truncate ( string $str, integer $len = 10, boolean $middle = true ) : string
$str string The string to truncate
$len integer The length of the returned string
$middle boolean Whether to place the ellipsis in the middle (true) or at the end (false)
return string The truncated string

verify_wsse() public static method

Verify WSSE values passed in.
public static verify_wsse ( array $data, boolean $anyverb = false ) : boolean
$data array payload from a given request, needs to include 'nonce', 'timestamp', and 'digest' as generated by Utils::WSSE()
$anyverb boolean If true, act on any request verb, not just POST.
return boolean True if the WSSE values passed are valid

Property Details

$debug_defined public static property

public static $debug_defined