05-functions.php
Table of Contents
- url_origin() : string
- Get the actual absolute origin of the request sent by the user.
- full_url() : string
- Get the full url, as requested by the client.
- url_stem() : string
- Get the stem URL at which this Pepperminty Wiki instance is located You can just append ?get_params_here to this and it will be a valid URL.
- human_filesize() : string
- Converts a filesize into a human-readable string.
- human_time_since() : string
- Calculates the time since a particular timestamp and returns a human-readable result.
- human_time() : string
- Renders a given number of seconds as something that humans can understand more easily.
- glob_recursive() : array<string|int, mixed>
- A recursive glob() function.
- path_resolve() : string
- Resolves a relative path against a given base directory.
- is_directory_empty() : bool
- Determines if a directory is empty or not.
- filepath_to_pagename() : string
- Converts a filepath to a page name.
- get_page_parent() : string|bool
- Gets the name of the parent page to the specified page.
- get_subpages() : object
- Gets a list of all the sub pages of the current page.
- check_subpage_parents() : mixed
- Makes sure that a subpage's parents exist.
- makepathsafe() : string
- Makes a path (or page name) safe.
- slugify() : string
- Slugifies a given string such that it can only contain a-z0-9-_.
- hide_email() : string
- Hides an email address from bots. Returns a fragment of HTML that contains the mangled email address.
- starts_with() : bool
- Checks to see if $haystack starts with $needle.
- ends_with() : bool
- Checks to see if $hackstack ends with $needle.
- mb_stripos_all() : array<string|int, mixed>|false
- Case-insensitively finds all occurrences of $needle in $haystack. Handles UTF-8 characters correctly.
- startsWith() : bool
- Tests whether a string starts with a specified substring.
- endsWith() : bool
- Tests whether a string ends with a given substring.
- str_replace_once() : string
- Replaces the first occurrence of $find with $replace.
- system_mime_type_extensions() : array<string|int, mixed>
- Returns the system's mime type mappings, considering the first extension listed to be cacnonical.
- system_mime_type_extension() : string
- Converts a given mime type to it's associated file extension.
- system_extension_mime_types() : array<string|int, mixed>
- Returns the system MIME type mapping of extensions to MIME types.
- system_extension_mime_type() : string
- Converts a given file extension to it's associated mime type.
- errorimage() : resource
- Creates an images containing the specified text.
- stack_trace() : string
- Generates a stack trace.
- var_dump_ret() : string
- Calls var_dump() and returns the output.
- var_dump_short() : string
- Calls var_dump(), shortening the output for various types.
- getallheaders() : mixed
- Polyfill for PHP's native getallheaders() function on platforms that don't have it.
- render_timestamp() : string
- Renders a timestamp in HTML.
- render_pagename() : string
- Renders a page name in HTML.
- render_editor() : string
- Renders an editor's or a group of editors name(s) in HTML.
- minify_css() : string
- Minifies CSS. Uses simple computationally-cheap optimisations to reduce size.
- save_settings() : bool
- Saves the settings file back to peppermint.json.
- save_pageindex() : bool
- Save the page index back to disk, respecting $settings->minify_pageindex
- save_userdata() : bool
- Saves the currently logged in user's data back to peppermint.json.
- get_user_pagename() : string
- Figures out the path to the user page for a given username.
- extract_user_from_userpage() : string
- Extracts a username from a user page path.
- email_user() : bool
- Sends a plain text email to a user, replacing {username} with the specified username.
- email_users() : int
- Sends a plain text email to a list of users, replacing {username} with each user's name.
- delete_recursive() : mixed
- Recursively deletes a directory and it's contents.
- crypto_id() : string
- Generates a crytographically-safe random id of the given length.
- is_cli() : bool
- Returns whether we are both on the cli AND the cli is enabled.
- metrics2servertiming() : string
- send_cookie() : void
- Sets a cookie on the client via the set-cookie header.
Functions
url_origin()
Get the actual absolute origin of the request sent by the user.
url_origin([array<string|int, mixed> $s = false ][, bool $use_forwarded_host = false ]) : string
Parameters
- $s : array<string|int, mixed> = false
-
The $_SERVER variable contents. Defaults to $_SERVER.
- $use_forwarded_host : bool = false
-
Whether to utilise the X-Forwarded-Host header when calculating the actual origin.
Return values
string —The actual origin of the user's request.
full_url()
Get the full url, as requested by the client.
full_url([array<string|int, mixed> $s = false ][, bool $use_forwarded_host = false ]) : string
Parameters
- $s : array<string|int, mixed> = false
-
The $_SERVER variable. Defaults to $_SERVER.
- $use_forwarded_host : bool = false
-
Whether to take the X-Forwarded-Host header into account.
Tags
Return values
string —The full url, as requested by the client.
url_stem()
Get the stem URL at which this Pepperminty Wiki instance is located You can just append ?get_params_here to this and it will be a valid URL.
url_stem([array<string|int, mixed> $s = false ][, bool $use_forwarded_host = false ]) : string
Uses full_url() under the hood. Note that this is based on the URL of the current request.
Parameters
- $s : array<string|int, mixed> = false
-
The $_SERVER variable (defaults to $_SERVER)
- $use_forwarded_host : bool = false
-
Whether to use the x-forwarded-host header or ignore it.
Return values
string —The stem url, as described above
human_filesize()
Converts a filesize into a human-readable string.
human_filesize(int $bytes[, int $decimals = 2 ]) : string
Parameters
- $bytes : int
-
The number of bytes to convert.
- $decimals : int = 2
-
The number of decimal places to preserve.
Tags
Return values
string —A human-readable filesize.
human_time_since()
Calculates the time since a particular timestamp and returns a human-readable result.
human_time_since(int $time) : string
Parameters
- $time : int
-
The timestamp to convert.
Tags
Return values
string —The time since the given timestamp as a human-readable string.
human_time()
Renders a given number of seconds as something that humans can understand more easily.
human_time(int $seconds) : string
Parameters
- $seconds : int
-
The number of seconds to render.
Return values
string —The rendered time.
glob_recursive()
A recursive glob() function.
glob_recursive(string $pattern, int $flags) : array<string|int, mixed>
Parameters
- $pattern : string
-
The glob pattern to use to find filenames.
- $flags : int
-
The glob flags to use when finding filenames.
Tags
Return values
array<string|int, mixed> —An array of the filepaths that match the given glob.
path_resolve()
Resolves a relative path against a given base directory.
path_resolve(string $path[, string|null $basePath = null ]) : string
Parameters
- $path : string
-
The relative path to resolve.
- $basePath : string|null = null
-
The base directory to resolve against.
Tags
Return values
string —An absolute path.
is_directory_empty()
Determines if a directory is empty or not.
is_directory_empty(string $dir) : bool
Parameters
- $dir : string
-
The path to the directory to check.
Tags
Return values
bool —True if the directory is empty, or false if it is not empty.
filepath_to_pagename()
Converts a filepath to a page name.
filepath_to_pagename(string $filepath) : string
Parameters
- $filepath : string
-
The filepath to convert.
Return values
string —The extracted pagename.
get_page_parent()
Gets the name of the parent page to the specified page.
get_page_parent(string $pagename) : string|bool
Parameters
- $pagename : string
-
The child page to get the parent page name for.
Tags
Return values
string|bool —get_subpages()
Gets a list of all the sub pages of the current page.
get_subpages(object $pageindex, string $pagename) : object
Parameters
- $pageindex : object
-
The pageindex to use to search.
- $pagename : string
-
The name of the page to list the sub pages of.
Return values
object —An object containing all the subpages and their respective distances from the given page name in the pageindex tree.
check_subpage_parents()
Makes sure that a subpage's parents exist.
check_subpage_parents(string $pagename[, bool $create_dir = true ]) : mixed
Note this doesn't check the pagename itself.
Parameters
- $pagename : string
-
The pagename to check.
- $create_dir : bool = true
-
Whether to create an associated directory for subpages or not.
Return values
mixed —makepathsafe()
Makes a path (or page name) safe.
makepathsafe(string $string) : string
A safe path / page name may not contain: Forward-slashes at the beginning Multiple dots in a row Odd characters (e.g. ?%*:|"<>() etc.) A safe path may, however, contain unicode characters such as éôà etc.
Parameters
- $string : string
-
The string to make safe.
Return values
string —A safe version of the given string.
slugify()
Slugifies a given string such that it can only contain a-z0-9-_.
slugify(string $text) : string
Also automatically makes it lowercase.
Parameters
- $text : string
-
The text to operate on.
Return values
string —The slugified string.
hide_email()
Hides an email address from bots. Returns a fragment of HTML that contains the mangled email address.
hide_email(string $email[, string $display_text = null ]) : string
Parameters
- $email : string
- $display_text : string = null
-
The display text for the resulting HTML - if null then the original email address is used. Note that because it's base64 encoded and then textContent is used, one does not need to run either htmlentities() or rawurlencode() over this value as it's completely safe.
Return values
string —The mangled email address as a fragment of HTML.
starts_with()
Checks to see if $haystack starts with $needle.
starts_with(string $haystack, string $needle) : bool
Parameters
- $haystack : string
-
The string to search.
- $needle : string
-
The string to search for at the beginning of $haystack.
Return values
bool —Whether $needle can be found at the beginning of $haystack.
ends_with()
Checks to see if $hackstack ends with $needle.
ends_with(string $haystack, string $needle) : bool
The matching bookend to starts_with.
Parameters
- $haystack : string
-
The haystack to search..
- $needle : string
-
The needle to look for.
Return values
bool —mb_stripos_all()
Case-insensitively finds all occurrences of $needle in $haystack. Handles UTF-8 characters correctly.
mb_stripos_all(string $haystack, string $needle) : array<string|int, mixed>|false
Parameters
- $haystack : string
-
The string to search.
- $needle : string
-
The string to find.
Tags
Return values
array<string|int, mixed>|false —An array of match indices, or false if nothing was found.
startsWith()
Tests whether a string starts with a specified substring.
startsWith(string $haystack, string $needle) : bool
Parameters
- $haystack : string
-
The string to check against.
- $needle : string
-
The substring to look for.
Return values
bool —Whether the string starts with the specified substring.
endsWith()
Tests whether a string ends with a given substring.
endsWith(string $whole, string $end) : bool
Parameters
- $whole : string
-
The string to test against.
- $end : string
-
The substring test for.
Return values
bool —Whether $whole ends in $end.
str_replace_once()
Replaces the first occurrence of $find with $replace.
str_replace_once(string $find, string $replace, string $subject) : string
Parameters
- $find : string
-
The string to search for.
- $replace : string
-
The string to replace the search string with.
- $subject : string
-
The string ot perform the search and replace on.
Return values
string —The source string after the find and replace has been performed.
system_mime_type_extensions()
Returns the system's mime type mappings, considering the first extension listed to be cacnonical.
system_mime_type_extensions() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —An array of mime type mappings.
system_mime_type_extension()
Converts a given mime type to it's associated file extension.
system_mime_type_extension(string $type) : string
Parameters
- $type : string
-
The mime type to convert.
Tags
Return values
string —The extension for the given mime type.
system_extension_mime_types()
Returns the system MIME type mapping of extensions to MIME types.
system_extension_mime_types() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —An array mapping file extensions to their associated mime types.
system_extension_mime_type()
Converts a given file extension to it's associated mime type.
system_extension_mime_type(string $ext) : string
Parameters
- $ext : string
-
The extension to convert.
Tags
Return values
string —The mime type associated with the given extension.
errorimage()
Creates an images containing the specified text.
errorimage(string $text[, int $target_size = null ]) : resource
Useful for sending errors back to the client.
Parameters
- $text : string
-
The text to include in the image.
- $target_size : int = null
-
The target width to aim for when creating the image. Not not specified, a value is determined automatically.
Return values
resource —The handle to the generated GD image.
stack_trace()
Generates a stack trace.
stack_trace([bool $log_trace = true ][, bool $full = false ]) : string
Parameters
- $log_trace : bool = true
-
Whether to send the stack trace to the error log.
- $full : bool = false
-
Whether to output a full description of all the variables involved.
Return values
string —A string prepresentation of a stack trace.
var_dump_ret()
Calls var_dump() and returns the output.
var_dump_ret(mixed $var) : string
Parameters
- $var : mixed
-
The thing to pass to var_dump().
Return values
string —The output captured from var_dump().
var_dump_short()
Calls var_dump(), shortening the output for various types.
var_dump_short(mixed $var) : string
Parameters
- $var : mixed
-
The thing to pass to var_dump().
Return values
string —A shortened version of the var_dump() output.
getallheaders()
Polyfill for PHP's native getallheaders() function on platforms that don't have it.
getallheaders() : mixed
Tags
Return values
mixed —render_timestamp()
Renders a timestamp in HTML.
render_timestamp(int $timestamp[, bool $absolute = false ][, bool $html = true ]) : string
Parameters
- $timestamp : int
-
The timestamp to render.
- $absolute : bool = false
-
Whether the time should be displayed absolutely, or relative to the current time.
- $html : bool = true
-
Whether the result should formatted as HTML (true) or plain text (false).
Return values
string —HTML representing the given timestamp.
render_pagename()
Renders a page name in HTML.
render_pagename(object $rchange) : string
Parameters
- $rchange : object
-
The recent change to render as a page name
Return values
string —HTML representing the name of the given page.
render_editor()
Renders an editor's or a group of editors name(s) in HTML.
render_editor(string $editorName) : string
Parameters
- $editorName : string
-
The name of the editor to render. Note that this may contain ARBITRARY HTML! In other words, make sure that the editor name(s) are sanitized (e.g. htmlentities()'d) before padding to this function.
Return values
string —HTML representing the given editor's name.
minify_css()
Minifies CSS. Uses simple computationally-cheap optimisations to reduce size.
minify_css(string $css_str) : string
CSS Minification ideas by Jean from catswhocode.com
Parameters
- $css_str : string
-
The string of CSS to minify.
Tags
Return values
string —The minified CSS string.
save_settings()
Saves the settings file back to peppermint.json.
save_settings() : bool
Return values
bool —Whether the settings were saved successfully.
save_pageindex()
Save the page index back to disk, respecting $settings->minify_pageindex
save_pageindex() : bool
Return values
bool —Whether the page index was saved successfully or not.
save_userdata()
Saves the currently logged in user's data back to peppermint.json.
save_userdata() : bool
Return values
bool —Whether the user's data was saved successfully. Returns false if the user isn't logged in.
get_user_pagename()
Figures out the path to the user page for a given username.
get_user_pagename(string $username) : string
Does not check to make sure the user acutally exists.
Parameters
- $username : string
-
The username to get the path to their user page for.
Return values
string —The path to the given user's page.
extract_user_from_userpage()
Extracts a username from a user page path.
extract_user_from_userpage(string $userPagename) : string
Parameters
- $userPagename : string
-
The suer page path to extract from.
Return values
string —The name of the user that the user page belongs to.
email_user()
Sends a plain text email to a user, replacing {username} with the specified username.
email_user(string $username, string $subject, string $body[, bool $ignore_verification = false ]) : bool
Parameters
- $username : string
-
The username to send the email to.
- $subject : string
-
The subject of the email.
- $body : string
-
The body of the email.
- $ignore_verification : bool = false
-
Whether to ignore user email verification status and send the email anyway. Defaults to false.
Return values
bool —Whether the email was sent successfully or not. Currently, this may fail if the user doesn't have a registered email address.
email_users()
Sends a plain text email to a list of users, replacing {username} with each user's name.
email_users(array<string|int, string> $usernames, string $subject, string $body) : int
Parameters
- $usernames : array<string|int, string>
-
A list of usernames to email.
- $subject : string
-
The subject of the email.
- $body : string
-
The body of the email.
Return values
int —The number of emails sent successfully.
delete_recursive()
Recursively deletes a directory and it's contents.
delete_recursive(string $path[, bool $delete_self = true ]) : mixed
Adapted by Starbeamrainbowlabs
Parameters
- $path : string
-
The path to the directory to delete.
- $delete_self : bool = true
-
Whether to delete the top-level directory. Set this to false to delete only a directory's contents
Tags
Return values
mixed —crypto_id()
Generates a crytographically-safe random id of the given length.
crypto_id(int $length) : string
Parameters
- $length : int
-
The length of id to generate.
Return values
string —The random id.
is_cli()
Returns whether we are both on the cli AND the cli is enabled.
is_cli() : bool
Return values
bool —metrics2servertiming()
metrics2servertiming(stdClass $perfdata) : string
Parameters
- $perfdata : stdClass
Return values
string —send_cookie()
Sets a cookie on the client via the set-cookie header.
send_cookie(string $key, string $value, int $expires) : void
Uses setcookie() under-the-hood.
Parameters
- $key : string
-
The cookie name to set.
- $value : string
-
The cookie value to set.
- $expires : int
-
The expiry time to set on the cookie.