Utilities

Utility library.  Implements some general purpose functions plus some wrappers that implement functionality available in PHP 5.

Functions
SESSION
function SESSION(
$key,
$val""
)

A wrapper for the $_SESSION superglobal.  Provides a handy way to avoid undefined variable warnings without explicitly calling isset() or empty() every time.

Parameters
key

The key for the superglobal index.

val

The optional value to set to the superglobal.

Returns

The value of the superglobal at index key, false if index is not set.

SERVER
function SERVER(
$key,
$val""
)
Parameters
key

The key for the superglobal index.

val

The optional value to set to the superglobal.

Returns

The value of the superglobal at index key, false if index is not set.

COOKIE
function COOKIE(
$key,
$val""
)
Parameters
key

The key for the superglobal index.

val

The optional value to set to the superglobal.

Returns

The value of the superglobal at index key, false if index is not set.

POST
function POST(
$key,
$val""
)
Parameters
key

The key for the superglobal index.

val

The optional value to set to the superglobal.

Returns

The value of the superglobal at index key, false if index is not set.

GET
function GET(
$key,
$val""
)
Parameters
key

The key for the superglobal index.

val

The optional value to set to the superglobal.

Returns

The value of the superglobal at index key, false if index is not set.

has_post
function has_post()

Determine if there is any POST data.

Returns

True if $_POST has any members, false otherwise.

current_uri
function current_uri(
$relativefalse,
$query_stringfalse,
$no_escapefalse
)

Convenience function to return the path to the current script.  Useful for the action attribute in form tags.

Parameters
relative

Optionally get the URI relative to the current directory, i.e.  just the file name.  Default is false.

query_string

Query string to append to URI.  Default is  false, which mean that the current query string will be used.

no_escape

Indicates whether or not ampersands in the query string should be escaped as &, which is needed for XHTML compliance.

Returns

A string with the URI.

current_file
function current_file(
$no_escapefalse
)

An alias for current_uri(true, false, $no_escape).

current_url
function current_url(
$force_path ''
)
Returns

The absolute URL of the requested script.

get_ip
function get_ip()

Quick function to get the user's IP address.  This should probably be extended to account for proxies and such.

Returns

The numeric IP address of the client.

get_user_agent
function get_user_agent()

Get the user's user-agent string, or a default if it is not set.

Returns

A string representing the user agent.

getlink
function getlink(
$name,
$typefalse
)

Returns a path suitable for use with <link> tags or as an href or src attribute.  Takes a file name and optional type (script, image, or style sheet) and returns a URI.  If the type is not given, the function guesses it from the file extension.

Parameters
name

The filename of the item to link.

type

The optional type of the item.  May be one of the defined constants LINK_IMAGE, LINK_SCRIPT, of LINK_STYLESHEET.

Returns

The root-relative path to the item.  If no item is found in any path, returns the name parameter as-is.

sanitize
function sanitize(
$str,
$pattern"/\W/",
$sub""
)

Strips the input of certain characters.  This is a thin wrapper around the preg_replace function.

Parameters
str

The string to strip.

pattern

The pattern to match.  Default is /\W/, i.e. non-word chars.

sub

The substitution string for pattern, default is null string.

Returns

The string with all characters matching sub replaced by the sub character.

in_arrayi
function in_arrayi(
$needle,
$haystack
)

Like in_array() standard function, except case-insensitive for strings.

make_uri
function make_uri(
$basefalse,
$query_stringfalse,
$no_gettrue,
$link_sep'&amp;',
$add_hostfalse
)

Builds a correct URI with query string.  This is an all-purpose kind of function designed to handle most contingencies.

Parameters
base

The base URI to use.  If not given, the current URI is used.

query_string

An associative array representing the query string.

no_get

A boolean set to true to suppress automatic importing of the $_GET superglobal into the query_string parameter.

link_sep

Character to separate query string parameters.  Default value is '&amp;' for link hrefs.  Use '&' for redirects.

add_host

If set to true, adds a host and protocol to the returned URI if they are not already present.

Returns

A string containing the resulting URI.

get_entry_from_uri
function get_entry_from_uri(
$uri
)

Takes a URI and converts it into an entry object.

HTML String Convenience Functions
Functions
ahref
function ahref(
$href,
$text,
$attribsfalse
)

Creates markup for a hyperlink.

Parameters
href

The URL to link to.

text

The main link text.

attribs

An associative array of additional attributes, with the key as the attribute name.