'), $text); // Clean up the HTML and replace some tags with line endings if (isset($break)) { $text = _filter_htmlcorrector($text); $text = str_replace(array('
', ' -
-
- '
* - filter_url_length_messaging, defaults to 72
*/
function messaging_text_format_html($text, $format = 'messaging') {
// Turn URLs into links
$text = _filter_url($text, $format);
// Filter out dangerous HTML.
$text = _filter_html($text, $format);
// Convert line breaks
$text = _filter_autop($text);
// Fix faulty HTML
$text = _filter_htmlcorrector($text);
return $text;
}
/**
* HTML quick filter
*
* This one only fixes line endings when everything else is already formatted and filtered.
*/
function messaging_text_format_htmlfast($text) {
// Convert line breaks
return _filter_autop($text);
}
/**
* Composes message from different parts, recursively and applies filter
*
* Filter is applied now only once
*
* @param $text
* Simple string or array of message parts
* It may have named elements like #prefix and #text
* or it may be single strings to render straight forward
* @param $glue
* Text to glue all lines together
* @param $filter
* Input format to apply to the results
*/
function messaging_text_render($text, $glue = '', $format = NULL, $filter = NULL) {
$output = '';
if (is_array($text)) {
if (isset($text['#prefix'])) {
$output .= $text['#prefix'].$glue;
unset($text['#prefix']);
}
if (isset($text['#text'])) {
$output .= $text['#text'];
return $output;
}
foreach (element_children($text) as $key) {
// The filter is not passed along
$text[$key] = messaging_text_render($text[$key], $glue);
}
$output .= implode($glue, $text);
} else {
$output .= $text;
}
// The format and the final filter are applied now only once
if (isset($format)) {
$output = messaging_text_check_markup($output, $format);
}
if (isset($filter)) {
$output = messaging_text_filter($output, $filter);
}
return $output;
}
/**
* Truncate messages to given length. Adapted from node_teaser() in node.module
*/
function messaging_text_truncate($text, $length) {
// If we have a short message, return the message
if (drupal_strlen($text) < $length) {
return $text;
}
// Initial slice.
$teaser = truncate_utf8($text, $length);
$position = 0;
// Cache the reverse of the message.
$reversed = strrev($teaser);
// split at paragraph boundaries.
$breakpoints = array('' => 0, '
' => 6, '
' => 4, "\n" => 1);
// We use strpos on the reversed needle and haystack for speed.
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$position = - $length - $offset;
return ($position == 0) ? $teaser : substr($teaser, 0, $position);
}
}
// When even the first paragraph is too long, we try to split at the end of
// the last full sentence.
$breakpoints = array('. ' => 1, '! ' => 1, '? ' => 1, ' ' => 0);
$min_length = strlen($reversed);
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$min_length = min($length, $min_length);
$position = 0 - $length - $offset;
}
}
return ($position == 0) ? $teaser : substr($teaser, 0, $position);
}
/**
* This is a fast version of check_markup
*
* The differences with regular check_markup are:
* - It uses a static cache instead of querying the database
* - It doesn't check for permissions
*
* @see check_markup()
*
* @param $text
* @param $format
*/
function messaging_text_check_markup($text, $format = FILTER_FORMAT_DEFAULT) {
$cache = &messaging_static(__FUNCTION__);
// When $check = TRUE, do an access check on $format.
if (isset($text)) {
$format = filter_resolve_format($format);
// Check for a cached version of this piece of text.
$cache_id = $format .':'. md5($text);
if (!isset($cache[$cache_id])) {
// Convert all Windows and Mac newlines to a single newline,
// so filters only need to deal with one possibility.
$text = str_replace(array("\r\n", "\r"), "\n", $text);
// Get a complete list of filters, ordered properly.
$filters = filter_list_format($format);
// Give filters the chance to escape HTML-like data such as code or formulas.
foreach ($filters as $filter) {
$text = module_invoke($filter->module, 'filter', 'prepare', $filter->delta, $format, $text, $cache_id);
}
// Perform filtering.
foreach ($filters as $filter) {
$text = module_invoke($filter->module, 'filter', 'process', $filter->delta, $format, $text, $cache_id);
}
$cache[$cache_id] = $text;
}
return $cache[$cache_id];
}
else {
$text = t('n/a');
}
return $text;
}
// Some other function wrappers, for backwards compatibility with old sending methods
/**
* HTML to text simple filtering.
*/
function messaging_check_plain($text, $break = NULL) {
return messaging_text_check_plain($text, $break);
}
/**
* Converts strings to plain utf-8 single line
*/
function messaging_check_subject($text) {
return Messaging_Method::check_subject($text);
}
/**
* Build a simple text with message subject and body
*/
function messaging_text_build($message, $glue = ' ') {
return $message->get_text($glue);
}