Chyrp

Helpers

Various functions used throughout Chyrp’s code.

Summary
Helpers Various functions used throughout Chyrp’s code.
Variables
$time_start Times Chyrp.
$l10n Stores loaded gettext domains.
Functions
session Begins Chyrp’s custom session storage whatnots.
error Shows an error message.
show_403 Shows an error message with a 403 status.
logged_in Returns whether or not they are logged in by returning the Visitor.$id (which defaults to 0).
load_translator Loads a .mo file for gettext translation.
__ Returns a translated string.
_p Returns a plural (or not) form of a translated string.
_f Returns a formatted translated string.
redirect Redirects to the given URL and exits immediately.
url Mask for Route->url().
pluralize Returns a pluralized string.  This is a port of Rails’s pluralizer.
depluralize Returns a depluralized string.  This is the inverse of pluralize.
truncate Truncates a string to the passed length, appending an ellipsis to the end.
when Returns date formatting for a string that isn’t a regular time() value
datetime Returns a standard datetime string based on either the passed timestamp or their time offset, usually for MySQL inserts.
fix Returns a HTML-sanitized version of a string.
unfix Returns the reverse of fix().
lang_code Returns the passed language code (e.g. en_US) to the human-readable text (e.g.  English (US))
sanitize Returns a sanitized string, typically for URLs.
trackback_respond Responds to a trackback request.
trackback_send Sends a trackback request.
send_pingbacks Sends pingback requests to the URLs in a string.
grab_urls Crawls a string for links.
pingback_url Checks if a URL is pingback-capable.
camelize Converts a given string to camel-case.
decamelize Decamelizes a string.
selected If $val1 == $val2, outputs ‘ selected=”selected”’
checked If $val == 1 (true), outputs ‘ checked=”checked”’
module_enabled Returns whether the given module is enabled or not.
feather_enabled Returns whether the given feather is enabled or not.
fallback Gracefully falls back a given variable if it’s empty or not set.
random Returns a random string.
unique_filename Makes a given filename unique for the uploads directory.
upload Moves an uploaded file to the uploads directory.
upload_from_url Copy a file from a specified URL to their upload directory.
timer_start Starts the timer.
timer_stop Stops the timer and returns the total time.
normalize Attempts to normalize all newlines and whitespace into single spaces.
get_remote Grabs the contents of a website/location.
self_url Returns the current URL.
show_404 Shows a 404 error message, extracting the passed array into the scope.
set_locale Set locale in a platform-independent way
sanitize_input Makes sure no inherently broken ideas such as magic_quotes break our application
match Try and match a string against an array of regular expressions.
cancel_module Temporarily removes a module from $config->enabled_modules.
timezones Returns an array of timezones that have unique offsets.  Doesn’t count deprecated timezones.
set_timezone Sets the timezone.
get_timezone() Returns the current timezone.
error_panicker Exits and states where the error occurred.
keywords Handle keyword-searching.
init_extensions Initialize all Modules and Feathers and return them as two arrays.

Variables

$time_start

$time_start

Times Chyrp.

$l10n

$l10n

Stores loaded gettext domains.

Functions

session

function session()

Begins Chyrp’s custom session storage whatnots.

error

function error( $title,   
$body,   
$backtrace  =  array() )

Shows an error message.

Parameters

$title The title for the error dialog.
$body The message for the error dialog.

show_403

function show_403( $title,
$body )

Shows an error message with a 403 status.

Parameters

$title The title for the error dialog.
$body The message for the error dialog.

logged_in

function logged_in()

Returns whether or not they are logged in by returning the Visitor.$id (which defaults to 0).

load_translator

function load_translator( $domain,
$mofile )

Loads a .mo file for gettext translation.

Parameters

$domain The name for this translation domain.
$mofile The .mo file to read from.

__

function __( $text,   
$domain  =  "chyrp" )

Returns a translated string.

Parameters

$text The string to translate.
$domain The translation domain to read from.

_p

function _p( $single,   
$plural,   
$number,   
$domain  =  "chyrp" )

Returns a plural (or not) form of a translated string.

Parameters

$single Singular string.
$plural Pluralized string.
$number The number to judge by.
$domain The translation domain to read from.

_f

function _f( $string,   
$args  =  array(),
$domain  =  "chyrp" )

Returns a formatted translated string.

redirect

function redirect( $url,   
$use_chyrp_url  =  false )

Redirects to the given URL and exits immediately.

url

function url( $url )

Mask for Route->url().

pluralize

function pluralize( $string )

Returns a pluralized string.  This is a port of Rails’s pluralizer.

Parameters

$string The string to pluralize.

depluralize

function depluralize( $string )

Returns a depluralized string.  This is the inverse of pluralize.

Parameters

$string The string to depluralize.

truncate

function truncate( $text,   
$numb  =  50,
$keep_words  =  true,
$minimum  =  10 )

Truncates a string to the passed length, appending an ellipsis to the end.

Parameters

$text String to shorten.
$numb Length of the shortened string.
$keep_words Whether or not to keep words in-tact.
$minimum If the truncated string is less than this and $keep_words is true, it will act as if $keep_words is false.

when

function when( $formatting,   
$when,   
$strftime  =  false )

Returns date formatting for a string that isn’t a regular time() value

Parameters

$formatting The formatting for date().
$time The string to convert to time (typically a datetime).
$strftime Use `strftime` instead of `date`?

datetime

function datetime( $when  =  null )

Returns a standard datetime string based on either the passed timestamp or their time offset, usually for MySQL inserts.

Parameters

$when An optional timestamp.

fix

function fix( $string,   
$quotes  =  false,
$decode_first  =  true )

Returns a HTML-sanitized version of a string.

unfix

function unfix( $string )

Returns the reverse of fix().

lang_code

function lang_code( $code )

Returns the passed language code (e.g. en_US) to the human-readable text (e.g.  English (US))

Parameters

$code The language code to convert

Credits

This is from TextPattern, modified to match Chyrp’s language code formatting.

sanitize

function sanitize( $string,   
$force_lowercase  =  true,
$anal  =  false )

Returns a sanitized string, typically for URLs.

Parameters

$string The string to sanitize.
$anal If set to true, will remove all non-alphanumeric characters.

trackback_respond

function trackback_respond( $error  =  false,
$message  =  "" )

Responds to a trackback request.

Parameters

$error Is this an error?
$message Message to return.

trackback_send

function trackback_send( $post,
$target )

Sends a trackback request.

Parameters

$post The post we’re sending from.
$target The URL we’re sending to.

send_pingbacks

function send_pingbacks( $string,
$post )

Sends pingback requests to the URLs in a string.

Parameters

$string The string to crawl for pingback URLs.
$post The post we’re sending from.

grab_urls

function grab_urls( $string )

Crawls a string for links.

Parameters

$string The string to crawl.

Returns

$matches[] An array of all URLs found in the string.

pingback_url

function pingback_url( $url )

Checks if a URL is pingback-capable.

Parameters

$url The URL to check.

Returns

$url The pingback target, if the URL is pingback-capable.

camelize

function camelize( $string,   
$keep_spaces  =  false )

Converts a given string to camel-case.

Parameters

$string The string to camelize.
$keep_spaces Whether or not to convert underscores to spaces or remove them.

Returns

A CamelCased string.

See Also

decamelize

decamelize

function decamelize( $string )

Decamelizes a string.

Parameters

$string The string to decamelize.

Returns

A de_camel_cased string.

See Also

camelize

selected

function selected( $val1,   
$val2,   
$return  =  false )

If $val1 == $val2, outputs ‘ selected=”selected”’

checked

function checked( $val )

If $val == 1 (true), outputs ‘ checked=”checked”’

module_enabled

function module_enabled( $name )

Returns whether the given module is enabled or not.

Parameters

$name The folder name of the module.

feather_enabled

function feather_enabled( $name )

Returns whether the given feather is enabled or not.

Parameters

$name The folder name of the feather.

fallback

function fallback( & $variable,   
$fallback  =  null,
$return  =  false )

Gracefully falls back a given variable if it’s empty or not set.

Parameters

&$variable The variable to check for.
$fallback What to set if the variable is empty or not set.
$return Whether to set it or to return.

Returns

$variable = $fallback If $return is false and $variable is empty or not set.
$fallback If $return is true and $variable is empty or not set.

random

function random( $length,   
$specialchars  =  false )

Returns a random string.

Parameters

$length How long the string should be.

unique_filename

function unique_filename( $name,   
$path  =  "",
$num  =  2 )

Makes a given filename unique for the uploads directory.

Parameters

$name The name to check.

Returns

$name A unique version of the given $name.

upload

function upload( $file,   
$extension  =  null,
$path  =  "",
$put  =  false )

Moves an uploaded file to the uploads directory.

Parameters

$file The $_FILES value.
$extension An array of valid extensions (case-insensitive).
$path A sub-folder in the uploads directory (optional).
$put Use copy() instead of move_uploaded_file()?

Returns

$filename The resulting filename from the upload.

upload_from_url

function upload_from_url( $url,   
$extension  =  null,
$path  =  "" )

Copy a file from a specified URL to their upload directory.

Parameters

$url The URL to copy.
$extension An array of valid extensions (case-insensitive).
$path A sub-folder in the uploads directory (optional).

See Also

upload

timer_start

function timer_start()

Starts the timer.

timer_stop

function timer_stop( $precision  =  3 )

Stops the timer and returns the total time.

Parameters

$precision Number of decimals places to round to.

Returns

A formatted number with the given $precision.

normalize

function normalize( $string )

Attempts to normalize all newlines and whitespace into single spaces.

get_remote

function get_remote( $url )

Grabs the contents of a website/location.

self_url

function self_url()

Returns the current URL.

show_404

function show_404()

Shows a 404 error message, extracting the passed array into the scope.

Parameters

$scope An array of values to extract into the scope.

set_locale

function set_locale( $locale )

Set locale in a platform-independent way

Parameters

$locale the locale name (‘en_US’, ‘uk_UA’, ‘fr_FR’ etc.)

Returns

The encoding name used by locale-aware functions.

sanitize_input

function sanitize_input( & $data )

Makes sure no inherently broken ideas such as magic_quotes break our application

Parameters

$data The array to be sanitized, usually one of ($_GET, $_POST, $_COOKIE, $_REQUEST)

match

function match( $try,
$haystack )

Try and match a string against an array of regular expressions.

Parameters

$try An array of regular expressions, or a single regular expression.
$haystack The string to test.

cancel_module

function cancel_module( $target )

Temporarily removes a module from $config->enabled_modules.

timezones

function timezones()

Returns an array of timezones that have unique offsets.  Doesn’t count deprecated timezones.

set_timezone

function set_timezone( $timezone )

Sets the timezone.

Parameters

$timezone The timezone to set.

get_timezone()

function get_timezone()

Returns the current timezone.

error_panicker

function error_panicker( $number,
$message,
$file,
$line )

Exits and states where the error occurred.

keywords

function keywords( $query,
$plain )

Handle keyword-searching.

Parameters

$query The query to parse.
$plain WHERE syntax to search for non-keyword queries.

Returns

An array containing the “WHERE” queries and the corresponding parameters.

init_extensions

function init_extensions()

Initialize all Modules and Feathers and return them as two arrays.