UtilitiesUtility library. Implements some general purpose functions plus some wrappers that implement functionality available in PHP 5. echo “Including utils.php...\n”; echo “Included Files:\n”; $x=get_included_files(); print_r($x); echo “Include path: “.get_include_path().”\n”; Summary | | | | | Return the canonical path for a given file. | | Return whether or not a path is absolute. | | Write contents to a file. | | Makes directory recursively. | | Does essentially the same thing as scandir on PHP 5. | | An alternate way to find the document root. | | Check that PHP is at least a certain version. | | A wrapper for the $_SESSION superglobal. | | | | | | | | | | | | Determine if there is any POST data. | | Determine if there is any GET data. | | Convenience function to return the path to the current script. | | An alias for current_uri(true, false, $no_escape). | | The absolute URL of the requested script. | | Convert a local path to a URI. | | The reverse of localpath_to_uri, this function takes a URI handled by LnBlog and converts it into a local path to the file or directory in question. | | Quick function to get the user’s IP address. | | Returns a path suitable for use with <link> tags or as an href or src attribute. | | Strips the input of certain characters. | | Like in_array() standard function, except case-insensitive for strings. | | Builds a correct URI with query string. |
canonicalize| function canonicalize ( | $path | ) |
|
Return the canonical path for a given file. The file need no exist. Removes all ‘.’ and ‘..’ components and returns an absolute path. Parameters| path | The path to canoncialize. |
ReturnsThe canonical path to the path parameter.
is_absolute| function is_absolute( | $path | ) |
|
Return whether or not a path is absolute. ParametersReturnsTrue ic the path is absolute, false otherwise.
write_file| function write_file( | $path, | | $contents | ) |
|
Write contents to a file. Basically a wrapper around fopen and fwrite. This function was rewritten to use the FS abstract interface. For one-off writes, this is fine, but for writing multiple files in the same call, the FS interface should be used directly to avoid overhead. Parameters| path | The path to write to. | | contents | A string to write to the file. |
ReturnsPasses on the return value of the fs->write_file() method.
mkdir_rec| function mkdir_rec( | $path, | | | | $mode | = | false | ) |
|
Makes directory recursively. As with write_file, is a wrapper around the FS interface. Same deal. Parameters| path | The path to create. | | mode | The optional octal directory permissions for the path. |
ReturnsPasses on the return value of the underlying class method.
scan_directory| function scan_directory( | $path, | | | | $dirs_only | = | false | ) |
|
Does essentially the same thing as scandir on PHP 5. Gets all the entries in the given directory. Parameters| path | The directory path to scan. | | dirs_only | Option to list only the directories in the path. The default is false. |
ReturnsAn array of directory entry names, removing “.” and “..” entries.
calculate_document_root| function calculate_document_root() |
An alternate way to find the document root. This one works by comparing the current URL on the server to the current directory. The idea is that we can find the location of the current URL in the path and remove it to get the document root. Note that this function IS case-sensitive. ReturnsThe calculated document root path.
php_version_at_least| function php_version_at_least( | $ver | ) |
|
Check that PHP is at least a certain version. Parameters| ver | The target version number in “1.2.3” format. |
ReturnsTrue if that current PHP version is at least ver, false otherwise.
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. |
ReturnsThe 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. |
ReturnsThe 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. |
ReturnsThe 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. |
ReturnsThe 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. |
ReturnsThe value of the superglobal at index key, false if index is not set.
GETPOSTLike $_REQUEST. This is the same as checking POST and then GET and returning the first result. ParametersReturnsThe value of $_POST[$key] if it is set, else $_GET[$key]. Returns false if neither one is set.
has_postDetermine if there is any POST data. ReturnsTrue if $_POST has any members, false otherwise.
has_postDetermine if there is any GET data. ReturnsTrue if $_GET has any members, false otherwise.
current_uri| function current_uri ( | $relative | = | false, | | $query_string | = | false, | | $no_escape | = | false | ) |
|
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. |
ReturnsA string with the URI.
current_file| function current_file( | $no_escape | = | false | ) |
|
An alias for current_uri(true, false, $no_escape).
current_urlReturnsThe absolute URL of the requested script.
localpath_to_uri| function localpath_to_uri( | $path, | | | | $full_uri | = | true | ) |
|
Convert a local path to a URI. Parameters| path | The path to convert. | | full_uri | Option to return the full URI, as opposed to just a root-relative URI. Defaults to true. |
ReturnsA string with the URL oc the given path.
uri_to_localpath| function uri_to_localpath( | $uri | ) |
|
The reverse of localpath_to_uri, this function takes a URI handled by LnBlog and converts it into a local path to the file or directory in question. This function assumes that the URI is fully qualified, e.g. http://somehost.com/somepath/somefile.ext
Note that this may or may not play well with Apache .htaccess files. ParametersReturnsA string containing the local path referenced by the URI. Not that this path may or may not exist.
get_ipQuick function to get the user’s IP address. This should probably be extended to account for proxies and such. ReturnsThe numeric IP address of the client.
getlink| function getlink( | $name, | | | | $type | = | false | ) |
|
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. |
ReturnsThe 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. |
ReturnsThe 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( | $base | = | false, | | $query_string | = | false, | | $no_get | = | true, | | $link_sep | = | '&', | | $add_host | = | false | ) |
|
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 ‘&’ 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. |
ReturnsA string containing the resulting URI.
|