Introduction to Helpers - xenocrat/chyrp-lite GitHub Wiki

The file helpers.php contains a cornucopia of useful helper functions to assist feather and module development. Each function is fully documented in the source file.

Sessions

Name Description
logged_in() Returns whether or not the visitor is logged in.
authenticate() Generates an authentication token for the session.

Routing

Name Description
redirect() Redirects to the supplied URL and exits immediately.
show_403() Shows an error message with a 403 HTTP header.
show_404() Shows an error message with a 404 HTTP header.
url() Generate the canonical URL[note] for an action e.g. url('login').
self_url() Returns an absolute URL[note] for the current request.

Localization

Name Description
lang_code() Converts a language code to a localised display name.
lang_base() Extracts the primary language subtag for the supplied code.
text_direction() Returns the correct text direction for the supplied language code.
__() Translates a string using PHP-gettext.
_f() Translates a string with sprintf() formatting.
_p() Translates a plural (or not) form of a string.
_w() Formats and internationalizes a string that isn't a regular time() value.

See also: Localizing Extensions.

Time/Date

Name Description
when() Formats a string that isn't a regular time() value.
datetime() Formats datetime for SQL queries.
now() Alias to strtotime(), for prettiness like now("+1 day").
convert_datetime() Converts datetime formatting from PHP to ICU format.

Variable Manipulation

Name Description
fallback() Sets the supplied variable if it is not already set.
oneof() Returns a value from the supplied set of arguments.
derezz() Strips tags from the supplied variable and tests it for emptiness.
token() Salt and hash a unique token using the supplied data.
crc24() Performs a 24-bit cyclic redundancy check.
slug() Generates a random slug value for posts and pages.
random() Generates a string of alphanumeric random characters.
shorthand_bytes() Decode shorthand bytes notation from php.ini.
match_any() Try to match a string against an array of regular expressions.
keywords() Parse keyword searches for values in specific database columns.

String Manipulation

Name Description
pluralize() Pluralizes a word.
depluralize() Singularizes a word.
normalize() Attempts to normalize all newlines and whitespace into single spaces.
camelize() Converts a string to camel-case.
decamelize() Undoes camel-case conversion.
truncate() Truncates a string to the requested number of characters or less.
markdown() Implements the Markdown content parsing filter.
emote() Converts emoticons to Unicode emoji HTML entities.
fix() Neutralizes HTML and quotes in strings for display.
unfix() Undoes neutralization of HTML and quotes in strings.
sanitize() Sanitizes a string of troublesome characters, typically for use in URLs.
sanitize_html() Sanitize HTML to disable scripts and obnoxious attributes.

Remote Fetches

Name Description
get_remote() Retrieve the contents of a URL.
grab_urls() Crawls a string and grabs hyperlinks from it.
merge_urls() Combines a base URL and relative path into a target URL.

Modules and Feathers

Name Description
module_enabled() Determines if a module is currently enabled and not cancelled.
feather_enabled() Determines if a feather is currently enabled and not cancelled.
cancel_module() Temporarily declares a module cancelled (disabled).
cancel_feather() Temporarily declares a feather cancelled (disabled).

Upload Management

Name Description
upload() Validates and moves an uploaded file to the uploads directory.
upload_from_url() Copies a file from a remote URL to the uploads directory.
uploaded() Generates an absolute URL[note] or filesystem path to an uploaded file.
uploaded_search() Returns an array of files discovered in the uploads directory.
upload_tester() Tests uploaded file information to determine if the upload was successful.
upload_filename() Generates a sanitized unique name for an uploaded file.
upload_filter_whitelist() Returns an array containing a default list of allowed file extensions.
delete_upload() Deletes an uploaded file.

Input Validation and Processing

Name Description
password_strength() Award a numeric score for the strength of a password. passwordStrength() is the JavaScript equivalent.
is_url() Does the string look like a web URL? isURL() is the JavaScript equivalent.
add_scheme() Prefixes a URL with a scheme if none was detected. addScheme() is the JavaScript equivalent.
is_email() Does the string look like an email address? isEmail() is the JavaScript equivalent.
is_unsafe_ip() Is the string a private or reserved IP address?
is_datetime_zero() Is the string a SQL datetime "zero" variant?
generate_captcha() Generates a captcha form element.
check_captcha() Checks the response to a captcha.

Responding to Requests

Name Description
esce() Escapes a string with backslashes and echoes it to output.
json_set() JSON encodes a value and checks for errors.
json_get() JSON decodes a value and checks for errors.
json_response() Send a structured JSON response.
file_attachment() Send a file attachment to the visitor.
zip_archive() Creates a basic flat Zip archive from an array of items.
email() Sends an email using PHP's mail() function or an alternative.

See also: Adding Ajax Functionality.

⚠️ **GitHub.com Fallback** ⚠️