.
/**
* @package CoreAPI
* @subpackage StringProcessingAPI
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2009 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
$t_core_dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
/**
* requires bug_api
*/
require_once( $t_core_dir . 'bug_api.php' );
/**
* requires user_pref_api
*/
require_once( $t_core_dir . 'user_pref_api.php' );
$g_cache_html_valid_tags = '';
$g_cache_html_valid_tags_single_line = '';
/**
* Return a string with all alphabetical characters converted to lowercase,
* using an appropriate multibyte implementation as available to system.
* @param string Input string
* @return string Lower cased string
*/
if ( function_exists( 'mb_strtolower' ) ) {
function string_lower( $p_string ) {
return mb_strtolower( $p_string );
}
} else {
function string_lower( $p_string ) {
return strtolower( $p_string );
}
}
# ## --------------------
# Preserve spaces at beginning of lines.
# Lines must be separated by \n rather than
function string_preserve_spaces_at_bol( $p_string ) {
$lines = explode( "\n", $p_string );
$line_count = count( $lines );
for( $i = 0;$i < $line_count;$i++ ) {
$count = 0;
$prefix = '';
$t_char = substr( $lines[$i], $count, 1 );
$spaces = 0;
while(( $t_char == ' ' ) || ( $t_char == "\t" ) ) {
if( $t_char == ' ' ) {
$spaces++;
} else {
$spaces += 4;
}
// 1 tab = 4 spaces, can be configurable.
$count++;
$t_char = substr( $lines[$i], $count, 1 );
}
for( $j = 0;$j < $spaces;$j++ ) {
$prefix .= ' ';
}
$lines[$i] = $prefix . substr( $lines[$i], $count );
}
return implode( "\n", $lines );
}
# --------------------
# Prepare a string to be printed without being broken into multiple lines
function string_no_break( $p_string ) {
if( strpos( $p_string, ' ' ) !== false ) {
return '' . $p_string . "";
} else {
return $p_string;
}
}
# --------------------
# Similar to nl2br, but fixes up a problem where new lines are doubled between
#
tags.
# additionally, wrap the text an $p_wrap character intervals if the config is set
function string_nl2br( $p_string, $p_wrap = 100 ) {
$output = '';
$pieces = preg_split( '/(]*>.*?<\/pre>)/is', $p_string, -1, PREG_SPLIT_DELIM_CAPTURE );
if( isset( $pieces[1] ) ) {
foreach( $pieces as $piece ) {
if( preg_match( '/(]*>.*?<\/pre>)/is', $piece ) ) {
$piece = preg_replace( "/
]*?>/", "", $piece );
# @@@ thraxisp - this may want to be replaced by html_entity_decode (or equivalent)
# if other encoded characters are a problem
$piece = preg_replace( "/ /", " ", $piece );
if( ON == config_get( 'wrap_in_preformatted_text' ) ) {
$output .= preg_replace( '/([^\n]{' . $p_wrap . '})(?!<\/pre>)/', "$1\n", $piece );
} else {
$output .= $piece;
}
} else {
$output .= nl2br( $piece );
}
}
return $output;
} else {
return nl2br( $p_string );
}
}
# --------------------
# Prepare a multiple line string for display to HTML
function string_display( $p_string ) {
$t_data = event_signal( 'EVENT_DISPLAY_TEXT', $p_string, true );
return $t_data;
}
# --------------------
# Prepare a single line string for display to HTML
function string_display_line( $p_string ) {
$t_data = event_signal( 'EVENT_DISPLAY_TEXT', $p_string, false );
return $t_data;
}
# --------------------
# Prepare a string for display to HTML and add href anchors for URLs, emails,
# bug references, and cvs references
function string_display_links( $p_string ) {
$t_data = event_signal( 'EVENT_DISPLAY_FORMATTED', $p_string, true );
return $t_data;
}
# --------------------
# Prepare a single line string for display to HTML and add href anchors for
# URLs, emails, bug references, and cvs references
function string_display_line_links( $p_string ) {
$t_data = event_signal( 'EVENT_DISPLAY_FORMATTED', $p_string, false );
return $t_data;
}
# --------------------
# Prepare a string for display in rss
function string_rss_links( $p_string ) {
# rss can not start with which spaces will be replaced into by string_display().
$t_string = trim( $p_string );
$t_string = event_signal( 'EVENT_DISPLAY_RSS', $t_string );
# another escaping to escape the special characters created by the generated links
return string_html_specialchars( $t_string );
}
# --------------------
# Prepare a string for plain text display in email
function string_email( $p_string ) {
$p_string = string_strip_hrefs( $p_string );
return $p_string;
}
# --------------------
# Prepare a string for plain text display in email and add URLs for bug
# links and cvs links
function string_email_links( $p_string ) {
return event_signal( 'EVENT_DISPLAY_EMAIL', $p_string );
}
# --------------------
# Process a string for display in a textarea box
function string_textarea( $p_string ) {
$p_string = string_html_specialchars( $p_string );
return $p_string;
}
# --------------------
# Process a string for display in a text box
function string_attribute( $p_string ) {
$p_string = string_html_specialchars( $p_string );
return $p_string;
}
# --------------------
# Process a string for inclusion in a URL as a GET parameter
function string_url( $p_string ) {
$p_string = rawurlencode( $p_string );
return $p_string;
}
# --------------------
# validate the url as part of this site before continuing
# see tests/test_string_sanitize_url.php for test cases
function string_sanitize_url( $p_url, $p_return_absolute = false ) {
$t_url = strip_tags( urldecode( $p_url ) );
$t_path = rtrim( config_get( 'path' ), '/' );
$t_short_path = rtrim( config_get( 'short_path' ), '/' );
$t_pattern = '(?:/*(?P