Source for file string_api.php
Documentation is available at string_api.php
* TestLink Open Source Project - http://testlink.sourceforge.net/
* This script is distributed under the GNU General Public License 2 or later.
* @filesource $RCSfile: fsource_Testlink__string_api.php.html,v $
* @version $Revision: 1.1.2.1 $
* @modified $Date: 2009/04/04 23:31:08 $ $Author: havlat $
* ### String Processing functions ###
* 20080822 - franciscom - restored missed string_email_links()
* 20080606 - havlatm - remove useles mantis related code
* 20071104 - franciscom - changes to string_email_links()
* ----------------------------------------------------------------------------------- */
// The original code was developed by:
// Mantis - a php based bugtracking system
// Copyright (C) 2002 - 2004 Mantis Team - mantisbt-dev@lists.sourceforge.net
// -------------------------------------------------------------------------------------
/** Preserve spaces at beginning of lines. Lines must be separated by \n rather than <br /> */
$lines = explode( "\n", $p_string );
$line_count = count( $lines );
for ( $i = 0; $i < $line_count; $i++ ) {
$t_char = substr( $lines[$i], $count, 1 );
while ( ( $t_char == ' ' ) || ( $t_char == "\t" ) ) {
$spaces += 4; // 1 tab = 4 spaces, can be configurable.
$t_char = substr( $lines[$i], $count, 1 );
for ( $j = 0; $j < $spaces; $j++ ) {
$lines[$i] = $prefix . substr( $lines[$i], $count );
/** Prepare a string to be printed without being broken into multiple lines */
if ( strpos( $p_string, ' ' ) !== false ) {
return '<span class="nowrap">' . $p_string . "</span>";
* Similar to nl2br, but fixes up a problem where new lines are doubled between <pre> tags.
* additionally, wrap the text an $p_wrap character intervals if the config is set
$p_string = nl2br( $p_string );
# fix up eols within <pre> tags (#1146)
for ( $x = 0; $x < count($pre1[0]); $x++ )
# @@@ thraxisp - this may want to be replaced by html_entity_decode (or equivalent)
# if other encoded characters are a problem
if ( ON == config_get( 'wrap_in_preformatted_text' ) )
$pre2[$x] = preg_replace("/([^\n]{". $p_wrap. "})(?!<\/pre>)/", "$1\n", $pre2[$x]);
$pre1[0][$x] = "/" . preg_quote($pre1[0][$x], "/") . "/";
/** Prepare a multiple line string for display to HTML */
/** Prepare a single line string for display to HTML */
* Prepare a string for display to HTML and add href anchors for URLs, emails,
* bug references, and cvs references
* Prepare a single line string for display to HTML and add href anchors for
* URLs, emails, bug references, and cvs references
/** Prepare a string for display in rss */
# rss can not start with which spaces will be replaced into by string_display().
$t_string = trim( $p_string );
# same steps as string_display_links() without the preservation of spaces since is undefined in XML.
$t_string = string_process_bug_link( $t_string, /* anchor */ true, /* detailInfo */ false, /* fqdn */ true );
$t_string = string_process_bugnote_link( $t_string, /* anchor */ true, /* detailInfo */ false, /* fqdn */ true );
$t_string = string_process_cvs_link( $t_string );
# another escaping to escape the special characters created by the generated links
/** Prepare a string for plain text display in email */
/** Prepare a string for plain text display in email and add URLs for bug
/** Process a string for display in a textarea box */
# Process a string for display in a text box
# Process a string for inclusion in a URL as a GET parameter
# validate the url as part of this site before continuing
if ( preg_match( '?http(s)*://?', $t_url ) > 0 ) {
// url is ok if it begins with our path, if not, replace it
// split and encode parameters
if ( strpos( $t_url, '?' ) !== FALSE ) {
list ( $t_path, $t_param ) = split( '\?', $t_url, 2 );
foreach($t_vals as $k => $v) {
return $t_path . '?' . $t_param;
#===================================
#===================================
/** Detect URLs and email addresses in the string and replace them with href anchors */
$t_change_quotes = false;
ini_set( 'magic_quotes_sybase', false );
# Find any URL in a string and replace it by a clickable link
$p_string = preg_replace( '/(([[:alpha:]][-+.[:alnum:]]*):\/\/(%[[:digit:]A-Fa-f]{2}|[-_.!~*\';\/?%^\\\\:@&={\|}+$#\(\),\[\][:alnum:]])+)/se',
"'<a href=\"'.rtrim('\\1','.').'\">\\1</a> [<a href=\"'.rtrim('\\1','.').'\" target=\"_blank\">^</a>]'", $p_string);
ini_set( 'magic_quotes_sybase', true );
# Set up a simple subset of RFC 822 email address parsing
# We don't allow domain literals or quoted strings
# We also don't allow the & character in domains even though the RFC
# appears to do so. This was to prevent > etc from being included.
# Note: we could use email_get_rfc822_regex() but it doesn't work well
# when applied to data that has already had entities inserted.
# bpfennig: '@' doesn't accepted anymore
# achumakov: characters 0x80-0xFF aren't acceptable, too
$t_atom = '[^\'@\'](?:[^()<>@,;:\\\".\[\]\000-\037\177-\377 &]+)';
# In order to avoid selecting URLs containing @ characters as email
# addresses we limit our selection to addresses that are preceded by:
# * the beginning of the string
# * a < entity (allowing '<foo@bar.baz>')
# * a : (allowing 'send email to:foo@bar.baz')
# * a \n, \r, or > (because newlines have been replaced with <br />
# and > isn't valid in URLs anyway
# At the end of the string we allow the opposite:
# * the end of the string
# * a , character (allowing 'email foo@bar.baz, or ...')
$p_string = preg_replace( '/(?<=^|"|<|[\s\:\>\n\r])('. $t_atom. '(?:\.'. $t_atom. ')*\@'. $t_atom. '(?:\.'. $t_atom. ')*)(?=$|"|>|[\s\,\<\n\r])/s',
'<a href="mailto:\1">\1</a>', $p_string);
/** Detect href anchors in the string and replace them with URLs and email addresses */
# First grab mailto: hrefs. We don't care whether the URL is actually
# correct - just that it's inside an href attribute.
$p_string = preg_replace( '/<a\s[^\>]*href="mailto:([^\"]+)"[^\>]*>[^\<]*<\/a>/si',
# Then grab any other href
$p_string = preg_replace( '/<a\s[^\>]*href="([^\"]+)"[^\>]*>[^\<]*<\/a>/si',
# This function looks for text with htmlentities
# like <b> and converts is into corresponding
# html <b> based on the configuration presets
$t_html_valid_tags = config_get( $p_multiline ? 'html_valid_tags' : 'html_valid_tags_single_line' );
if ( OFF === $t_html_valid_tags || is_blank( $t_html_valid_tags ) ) {
$tags = explode( ',', $t_html_valid_tags );
foreach ($tags as $key => $value)
$tags[$key] = trim($value);
$p_string = eregi_replace( '<(' . $tags . ')[[:space:]]*>', '<\\1>', $p_string );
$p_string = eregi_replace( '<\/(' . $tags . ')[[:space:]]*>', '</\\1>', $p_string );
$p_string = eregi_replace( '<(' . $tags . ')[[:space:]]*\/>', '<\\1 />', $p_string );
# Return a string with the $p_character pattern repeated N times.
# $p_character - pattern to repeat
# $p_repeats - number of times to repeat.
return str_pad( '', $p_repeats, $p_character );
# Format date for display
// martin: @todo update and integrate
$t_timestamp = db_unixtimestamp( $p_date );
# Shorten a string for display on a dropdown to prevent the page rendering too wide
# ref issues #4630, #5072, #5131
if ( ( strlen( $p_string ) > $t_max ) && ( $t_max > 0 ) ){
$t_pattern = '/([\s|.|,|\-|_|\/|\?]+)/';
$t_bits = preg_split( $t_pattern, $p_string, - 1, PREG_SPLIT_DELIM_CAPTURE );
$t_last = $t_bits[ count( $t_bits ) - 1 ];
$t_last_len = strlen( $t_last );
foreach ( $t_bits as $t_bit ) {
if ( ( strlen( $t_string ) + strlen( $t_bit ) + $t_last_len + 3 <= $t_max )
|| ( strpos( $t_bit, '.,-/?' ) > 0 ) ) {
$t_string .= '...' . $t_last;
# remap a field name to a string name (for sort filter)
'last_updated' => 'last_update',
if ( isset ( $t_map[ $p_string ] ) ) {
$t_string = $t_map[ $p_string ];
return lang_get_defaulted( $t_string );
# Calls htmlentities on the specified string, passing along
# Calls htmlspecialchars on the specified string, passing along
# the current charset, if the current PHP version supports it.
# achumakov: @ added to avoid warning output in unsupported codepages
# e.g. 8859-2, windows-1257, Korean, which are treated as 8859-1.
# This is VERY important for Eastern European, Baltic and Korean languages
# Prepares a string to be used as part of header().
$t_truncate_pos = strpos($p_string, "\n");
if ($t_truncate_pos !== false ) {
$t_string = substr($t_string, 0, $t_truncate_pos);
$t_truncate_pos = strpos($p_string, "\r");
if ($t_truncate_pos !== false ) {
$t_string = substr($t_string, 0, $t_truncate_pos);
# Checks the supplied string for scripting characters, if it contains any, then return true, otherwise return false.
if ( ( strstr( $p_string, '<' ) !== false ) || ( strstr( $p_string, '>' ) !== false ) ) {
|