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 HTTP header.
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 given length, optionally taking into account HTML tags, and/or keeping words in tact.
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 or returns 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 Sets a given variable if it is not set.
oneof Returns the first argument that is set and non-empty.
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.
uploaded Returns a URL to an uploaded file.
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 and immediately exits.
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 to match a string against an array of regular expressions.
cancel_module Temporarily removes a module from $config->enabled_modules.
time_in_timezone Returns the appropriate time() for representing a timezone.
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.
xml2arr Recursively converts a SimpleXML object (and children) to an array.
arr2xml Recursively adds an array (or object I guess) to a SimpleXML object.
relative_time Returns the difference between the given timestamps or now.
list_notate Notates an array as a list of things.
email Send an email.  Function arguments are exactly the same as the PHP mail() function.
now Alias to strtotime, for prettiness like now(“+1 day”).
comma_sep Convert a comma-seperated string into an array of the listed values.

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.
$backtrace The trace of the error.

show_403

function show_403( $title,
$body )

Shows an error message with a 403 HTTP header.

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.

Parameters

$string String to translate and format.
$args One arg or an array of arguments to format with.
$domain The translation domain to read from.

redirect

function redirect( $url,   
$use_chyrp_url  =  false )

Redirects to the given URL and exits immediately.

Parameters

$url The URL to redirect to.  If it begins with / it will be relative to the Config.chyrp_url.
$use_chyrp_url Use the Config.chyrp_url instead of Config.url for $urls beginning with /?

url

function url( $url,   
$controller  =  null )

Mask for Route->url().

pluralize

function pluralize( $string,   
$number  =  null )

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

Parameters

$string The string to pluralize.
$number If passed, and this number is 1, it will not pluralize.

depluralize

function depluralize( $string,   
$number  =  null )

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

Parameters

$string The string to depluralize.
$number If passed, and this number is not 1, it will not depluralize.

truncate

function truncate( $text,   
$length  =  100,
$ending  =  "...",
$exact  =  false,
$html  =  false )

Truncates a string to the given length, optionally taking into account HTML tags, and/or keeping words in tact.

Parameters

$text String to shorten.
$length Length to truncate to.
$ending What to place at the end, e.g.  “...”.
$exact Break words?
$html Auto-close cut-off HTML tags?

Author

CakePHP team, code style modified.

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().
$when Time to base on.  If it is not numeric it will be run through strtotime.
$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 )

Returns a HTML-sanitized version of a string.

Parameters

$string String to fix.
$quotes Encode quotes?

unfix

function unfix( $string )

Returns the reverse of fix().

Parameters

$string String to unfix.

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

Author

TextPattern devs, modified to fit with Chyrp.

sanitize

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

Returns a sanitized string, typically for URLs.

Parameters

$string The string to sanitize.
$force_lowercase Force the string to lowercase?
$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

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

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 or returns selected=”selected”

Parameters

$val1 First value.
$val2 Second value.
$return Return selected=”selected” instead of outputting it

checked

function checked( $val )

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

Parameters

$val Value to check.

module_enabled

function module_enabled( $name )

Returns whether the given module is enabled or not.

Parameters

$name The folder name of the module.

Returns

Whether or not the requested module is enabled.

feather_enabled

function feather_enabled( $name )

Returns whether the given feather is enabled or not.

Parameters

$name The folder name of the feather.

Returns

Whether or not the requested feather is enabled.

fallback

function fallback( & $variable )

Sets a given variable if it is not set.

The last of the arguments or the first non-empty value will be used.

Parameters

&$variable The variable to return or set.

Returns

The value of whatever was chosen.

oneof

function oneof()

Returns the first argument that is set and non-empty.

It will guess where to stop based on the types of the arguments, e.g.  “” has priority over array() but not 1.

random

function random( $length,   
$specialchars  =  false )

Returns a random string.

Parameters

$length How long the string should be.
$specialchars Use special characters in the resulting string?

unique_filename

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

Makes a given filename unique for the uploads directory.

Parameters

$name The name to check.
$path Path to check in.
$num Number suffix from which to start increasing if the filename exists.

Returns

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

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

uploaded

function uploaded( $file,   
$url  =  true )

Returns a URL to an uploaded file.

Parameters

$file Filename relative to the uploads directory.

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.

Returns

The normalized string.

get_remote

function get_remote( $url )

Grabs the contents of a website/location.

Parameters

$url The URL of the location to grab.

Returns

The response from the remote URL.

self_url

function self_url()

Returns the current URL.

show_404

function show_404()

Shows a 404 error message and immediately exits.

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, or $_REQUEST

match

function match( $try,
$haystack )

Try to 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.

Returns

Whether or not the match succeeded.

cancel_module

function cancel_module( $target )

Temporarily removes a module from $config->enabled_modules.

Parameters

$target Module name to disable.

time_in_timezone

function time_in_timezone( $timezone )

Returns the appropriate time() for representing a timezone.

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( $errno,
$message,
$file,
$line )

Exits and states where the error occurred.

keywords

function keywords( $query,   
$plain,   
$table  =  null )

Handle keyword-searching.

Parameters

$query The query to parse.
$plain WHERE syntax to search for non-keyword queries.
$table If specified, the keywords will be checked against this table’s columns for validity.

Returns

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

init_extensions

function init_extensions()

Initialize all Modules and Feathers.

xml2arr

function xml2arr( $parse )

Recursively converts a SimpleXML object (and children) to an array.

Parameters

$parse The SimpleXML object to convert into an array.

arr2xml

function arr2xml( & $object,
$data )

Recursively adds an array (or object I guess) to a SimpleXML object.

Parameters

&$object The SimpleXML object to modify.
$data The data to add to the SimpleXML object.

relative_time

function relative_time( $when,   
$from  =  null )

Returns the difference between the given timestamps or now.

Parameters

$time Timestamp to compare to.
$from Timestamp to compare from.  If not specified, defaults to now.

Returns

A string formatted like “3 days ago” or “3 days from now”.

list_notate

function list_notate( $array,   
$quotes  =  false )

Notates an array as a list of things.

Parameters

$array An array of things to notate.
$quotes Wrap quotes around strings?

Returns

A string like “foo, bar, and baz”.

email

function email()

Send an email.  Function arguments are exactly the same as the PHP mail() function.

This is intended so that modules can provide an email method if the server cannot use mail().

now

function now( $when )

Alias to strtotime, for prettiness like now(“+1 day”).

comma_sep

function comma_sep( $string )

Convert a comma-seperated string into an array of the listed values.