phpDocumentor Testlink
[ class tree: Testlink ] [ index: Testlink ] [ all elements ]

Source for file string_api.php

Documentation is available at string_api.php

  1. <?php
  2. /** 
  3.  * TestLink Open Source Project - http://testlink.sourceforge.net/
  4.  * This script is distributed under the GNU General Public License 2 or later.
  5.  * 
  6.  * @filesource $RCSfile: fsource_Testlink__string_api.php.html,v $
  7.  * @version $Revision: 1.1.2.1 $
  8.  * @modified $Date: 2009/04/04 23:31:08 $  $Author: havlat $
  9.  * @author franciscom
  10.  * 
  11.  *     ### String Processing functions ###
  12.  * 
  13.  *  rev:
  14.  *   20080822 - franciscom - restored missed string_email_links()
  15.  *      20080606 - havlatm - remove useles mantis related code
  16.  *      20071104 - franciscom - changes to string_email_links()
  17.  *     
  18.  *  ----------------------------------------------------------------------------------- */
  19.  // The original code was developed by:
  20.  // Mantis - a php based bugtracking system
  21.  // Copyright (C) 2002 - 2004  Mantis Team   - mantisbt-dev@lists.sourceforge.net
  22.  // -------------------------------------------------------------------------------------
  23.  
  24.  
  25. /** Preserve spaces at beginning of lines. Lines must be separated by \n rather than <br /> */
  26. function string_preserve_spaces_at_bol$p_string 
  27. {
  28.     $lines explode"\n"$p_string );
  29.     $line_count count$lines );
  30.     for $i 0$i $line_count$i++ {
  31.             $count    0;
  32.             $prefix    '';
  33.  
  34.             $t_char substr$lines[$i]$count);
  35.             $spaces 0;
  36.             while ( ( $t_char  == ' ' || $t_char == "\t" ) ) {
  37.                 if $t_char == ' ' )
  38.                     $spaces++;
  39.                 else
  40.                     $spaces += 4// 1 tab = 4 spaces, can be configurable.
  41.  
  42.                 $count++;
  43.                 $t_char substr$lines[$i]$count);
  44.             }
  45.  
  46.             for $j 0$j $spaces$j++ {
  47.                 $prefix .= '&nbsp;';
  48.             }
  49.  
  50.             $lines[$i$prefix substr$lines[$i]$count );
  51.     }
  52.     return implode"\n"$lines );
  53. }
  54.  
  55. /** Prepare a string to be printed without being broken into multiple lines */
  56. function string_no_break$p_string {
  57.     if strpos$p_string' ' !== false {
  58.         return '<span class="nowrap">' $p_string "</span>";
  59.     else {
  60.         return $p_string;
  61.     }
  62. }
  63.  
  64. /** 
  65.  * Similar to nl2br, but fixes up a problem where new lines are doubled between <pre> tags.
  66.  * additionally, wrap the text an $p_wrap character intervals if the config is set
  67.  */
  68. function string_nl2br$p_string$p_wrap 100 
  69. {
  70.         $p_string nl2br$p_string );
  71.  
  72.         # fix up eols within <pre> tags (#1146)
  73.         $pre2 array();
  74.         preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/"$p_string$pre1);
  75.         for $x 0$x count($pre1[0])$x++ 
  76.         {
  77.             $pre2[$xpreg_replace("/<br[^>]*?>/"""$pre1[0][$x]);
  78.             # @@@ thraxisp - this may want to be replaced by html_entity_decode (or equivalent)
  79.             #     if other encoded characters are a problem
  80.             $pre2[$xpreg_replace("/&nbsp;/"" "$pre2[$x]);
  81.             if ON == config_get'wrap_in_preformatted_text' ) ) 
  82.             {
  83.                 $pre2[$xpreg_replace("/([^\n]{".$p_wrap."})(?!<\/pre>)/""$1\n"$pre2[$x]);
  84.             }
  85.             $pre1[0][$x"/" preg_quote($pre1[0][$x]"/""/";
  86.         }
  87.  
  88.         return preg_replace$pre1[0]$pre2$p_string );
  89. }
  90.  
  91. /** Prepare a multiple line string for display to HTML */
  92. function string_display$p_string 
  93. {    
  94.     $p_string string_strip_hrefs$p_string );
  95.     $p_string string_html_specialchars$p_string );
  96.     $p_string string_restore_valid_html_tags$p_string/* multiline = */ true );
  97.     $p_string string_preserve_spaces_at_bol$p_string );
  98.     $p_string string_nl2br$p_string );
  99.  
  100.     return $p_string;
  101. }
  102.  
  103. /** Prepare a single line string for display to HTML */
  104. function string_display_line$p_string 
  105. {
  106.     $p_string string_strip_hrefs$p_string );
  107.     $p_string string_html_specialchars$p_string );
  108.     $p_string string_restore_valid_html_tags$p_string/* multiline = */ false );
  109.     return $p_string;
  110. }
  111.  
  112. /** 
  113.  * Prepare a string for display to HTML and add href anchors for URLs, emails,
  114.  * bug references, and cvs references
  115.  */
  116. function string_display_links$p_string 
  117. {
  118.     $p_string string_display$p_string );
  119.     $p_string string_insert_hrefs$p_string );
  120.     return $p_string;
  121. }
  122.  
  123. /** 
  124.  * Prepare a single line string for display to HTML and add href anchors for
  125.  * URLs, emails, bug references, and cvs references
  126.  */ 
  127. function string_display_line_links$p_string 
  128. {
  129.     $p_string string_display_line$p_string );
  130.     $p_string string_insert_hrefs$p_string );
  131.  
  132.     return $p_string;
  133. }
  134.  
  135. /** Prepare a string for display in rss */
  136. function string_rss_links$p_string 
  137. {
  138.     # rss can not start with &nbsp; which spaces will be replaced into by string_display().
  139.     $t_string trim$p_string );
  140.  
  141.     # same steps as string_display_links() without the preservation of spaces since &nbsp; is undefined in XML.
  142.     $t_string string_strip_hrefs$t_string );
  143.     $t_string string_html_specialchars$t_string );
  144.     $t_string string_restore_valid_html_tags$t_string );
  145.     $t_string string_nl2br$t_string );
  146.     $t_string string_insert_hrefs$t_string );
  147.     $t_string string_process_bug_link$t_string/* anchor */ true/* detailInfo */ false/* fqdn */ true );
  148.     $t_string string_process_bugnote_link$t_string/* anchor */ true/* detailInfo */ false/* fqdn */ true );
  149.     $t_string string_process_cvs_link$t_string );
  150.     # another escaping to escape the special characters created by the generated links
  151.     $t_string string_html_specialchars$t_string );
  152.  
  153.     return $t_string;
  154. }
  155.  
  156.    
  157. /** Prepare a string for plain text display in email */
  158. function string_email$p_string 
  159. {
  160.     $p_string string_strip_hrefs$p_string );
  161.     return $p_string;
  162. }
  163.   
  164. /**  Prepare a string for plain text display in email and add URLs for bug
  165.      links and cvs links
  166. */     
  167. function string_email_links$p_string {
  168.     $p_string string_email$p_string );
  169.   return $p_string;
  170. }
  171.  
  172. /** Process a string for display in a textarea box */
  173. function string_textarea$p_string 
  174. {
  175.     $p_string string_html_specialchars$p_string );
  176.     return $p_string;
  177. }
  178.  
  179.     # --------------------
  180.     # Process a string for display in a text box
  181.     function string_attribute$p_string {
  182.         $p_string string_html_specialchars$p_string );
  183.  
  184.         return $p_string;
  185.     }
  186.  
  187.     # --------------------
  188.     # Process a string for inclusion in a URL as a GET parameter
  189.     function string_url$p_string {
  190.         $p_string rawurlencode$p_string );
  191.  
  192.         return $p_string;
  193.     }
  194.  
  195.     # --------------------
  196.     # validate the url as part of this site before continuing
  197.     function string_sanitize_url$p_url {
  198.  
  199.         $t_url strip_tagsurldecode$p_url ) );
  200.         if preg_match'?http(s)*://?'$t_url 
  201.             // no embedded addresses
  202.             if preg_match'?^' config_get'path' '?'$t_url == 
  203.                 // url is ok if it begins with our path, if not, replace it
  204.                 $t_url 'index.php';
  205.             }
  206.         }
  207.         if $t_url == '' {
  208.             $t_url 'index.php';
  209.         }
  210.         
  211.         // split and encode parameters
  212.         if strpos$t_url'?' !== FALSE {
  213.             list$t_path$t_param split'\?'$t_url);
  214.             if $t_param !== "" {
  215.                 $t_vals array();
  216.                 parse_str$t_param$t_vals );
  217.                 $t_param '';
  218.                 foreach($t_vals as $k => $v{
  219.                     if ($t_param != ''{
  220.                         $t_param .= '&'
  221.                     }
  222.                     $t_param .= "$k=urlencodestrip_tagsurldecode$v ) ) );
  223.                 }
  224.                 return $t_path '?' $t_param;
  225.             else {
  226.                 return $t_path;
  227.             }
  228.         else {
  229.             return $t_url;
  230.         }
  231.     }
  232.     
  233.  
  234.  
  235.     #===================================
  236.     # Tag Processing
  237.     #===================================
  238.  
  239. /** Detect URLs and email addresses in the string and replace them with href anchors */
  240. function string_insert_hrefs$p_string 
  241. {
  242.     if !config_get('html_make_links') ) {
  243.         return $p_string;
  244.     }
  245.  
  246.     $t_change_quotes false;
  247.     ifini_get_bool'magic_quotes_sybase' ) ) {
  248.         $t_change_quotes true;
  249.         ini_set'magic_quotes_sybase'false );
  250.     }
  251.  
  252.     # Find any URL in a string and replace it by a clickable link
  253.     $p_string preg_replace'/(([[:alpha:]][-+.[:alnum:]]*):\/\/(%[[:digit:]A-Fa-f]{2}|[-_.!~*\';\/?%^\\\\:@&={\|}+$#\(\),\[\][:alnum:]])+)/se',
  254.                               "'<a href=\"'.rtrim('\\1','.').'\">\\1</a> [<a href=\"'.rtrim('\\1','.').'\" target=\"_blank\">^</a>]'"$p_string);
  255.                               
  256.     if$t_change_quotes {
  257.         ini_set'magic_quotes_sybase'true );
  258.     }
  259.  
  260.     # Set up a simple subset of RFC 822 email address parsing
  261.     #  We don't allow domain literals or quoted strings
  262.     #  We also don't allow the & character in domains even though the RFC
  263.     #  appears to do so.  This was to prevent &gt; etc from being included.
  264.     #  Note: we could use email_get_rfc822_regex() but it doesn't work well
  265.     #  when applied to data that has already had entities inserted.
  266.     #
  267.     # bpfennig: '@' doesn't accepted anymore
  268.     # achumakov: characters 0x80-0xFF aren't acceptable, too
  269.     $t_atom '[^\'@\'](?:[^()<>@,;:\\\".\[\]\000-\037\177-\377 &]+)';
  270.  
  271.     # In order to avoid selecting URLs containing @ characters as email
  272.     #  addresses we limit our selection to addresses that are preceded by:
  273.     #  * the beginning of the string
  274.     #  * a &lt; entity (allowing '<foo@bar.baz>')
  275.     #  * whitespace
  276.     #  * a : (allowing 'send email to:foo@bar.baz')
  277.     #  * a \n, \r, or > (because newlines have been replaced with <br />
  278.     #    and > isn't valid in URLs anyway
  279.     #
  280.     # At the end of the string we allow the opposite:
  281.     #  * the end of the string
  282.     #  * a &gt; entity
  283.     #  * whitespace
  284.     #  * a , character (allowing 'email foo@bar.baz, or ...')
  285.     #  * a \n, \r, or <
  286.  
  287.     $p_string preg_replace'/(?<=^|&quot;|&lt;|[\s\:\>\n\r])('.$t_atom.'(?:\.'.$t_atom.')*\@'.$t_atom.'(?:\.'.$t_atom.')*)(?=$|&quot;|&gt;|[\s\,\<\n\r])/s',
  288.                             '<a href="mailto:\1">\1</a>'$p_string);
  289.  
  290.     return $p_string;
  291. }
  292.  
  293. /** Detect href anchors in the string and replace them with URLs and email addresses */
  294. function string_strip_hrefs$p_string 
  295. {
  296.     # First grab mailto: hrefs.  We don't care whether the URL is actually
  297.     # correct - just that it's inside an href attribute.
  298.     $p_string preg_replace'/<a\s[^\>]*href="mailto:([^\"]+)"[^\>]*>[^\<]*<\/a>/si',
  299.                                 '\1'$p_string);
  300.  
  301.     # Then grab any other href
  302.     $p_string preg_replace'/<a\s[^\>]*href="([^\"]+)"[^\>]*>[^\<]*<\/a>/si',
  303.                                 '\1'$p_string);
  304.     return $p_string;
  305. }
  306.  
  307. # This function looks for text with htmlentities
  308. # like &lt;b&gt; and converts is into corresponding
  309. # html <b> based on the configuration presets
  310. function string_restore_valid_html_tags$p_string$p_multiline true 
  311. {
  312.     $t_html_valid_tags config_get$p_multiline 'html_valid_tags' 'html_valid_tags_single_line' );
  313.  
  314.     if OFF === $t_html_valid_tags || is_blank$t_html_valid_tags ) ) {
  315.         return $p_string;
  316.     }
  317.  
  318.     $tags explode','$t_html_valid_tags );
  319.     foreach ($tags as $key => $value
  320.     
  321.         if !is_blank$value ) ) {
  322.             $tags[$keytrim($value)
  323.         }
  324.     }
  325.     $tags implode'|'$tags);
  326.  
  327.     $p_string eregi_replace'&lt;(' $tags ')[[:space:]]*&gt;''<\\1>'$p_string );
  328.     $p_string eregi_replace'&lt;\/(' .$tags ')[[:space:]]*&gt;''</\\1>'$p_string );
  329.     $p_string eregi_replace'&lt;(' $tags ')[[:space:]]*\/&gt;''<\\1 />'$p_string );
  330.  
  331.     return $p_string;
  332. }
  333.  
  334.  
  335.     # --------------------
  336.     # Return a string with the $p_character pattern repeated N times.
  337.     # $p_character - pattern to repeat
  338.     # $p_repeats - number of times to repeat.
  339.     function string_repeat_char$p_character$p_repeats {
  340.         return str_pad''$p_repeats$p_character );
  341.     }
  342.  
  343.     # --------------------
  344.     # Format date for display
  345.     // martin: @todo update and integrate
  346.     function string_format_complete_date$p_date {
  347.         $t_timestamp db_unixtimestamp$p_date );
  348.         return dateconfig_get'complete_date_format' )$t_timestamp );
  349.     }
  350.  
  351.     # --------------------
  352.     # Shorten a string for display on a dropdown to prevent the page rendering too wide
  353.     #  ref issues #4630, #5072, #5131
  354.  
  355.     function string_shorten$p_string {
  356.         $t_max config_get'max_dropdown_length' );
  357.         if ( ( strlen$p_string $t_max && $t_max ) ){
  358.             $t_pattern '/([\s|.|,|\-|_|\/|\?]+)/';
  359.             $t_bits preg_split$t_pattern$p_string-1PREG_SPLIT_DELIM_CAPTURE );
  360.  
  361.             $t_string '';
  362.             $t_last $t_bitscount$t_bits ];
  363.             $t_last_len strlen$t_last );
  364.  
  365.             foreach $t_bits as $t_bit {
  366.                 if ( ( strlen$t_string strlen$t_bit $t_last_len <= $t_max )
  367.                     || strpos$t_bit'.,-/?' ) ) {
  368.                     $t_string .= $t_bit;
  369.                 else {
  370.                     break;
  371.                 }
  372.             }
  373.             $t_string .= '...' $t_last;
  374.             return $t_string;
  375.         else {
  376.             return $p_string;
  377.         }
  378.     }
  379.  
  380.     # --------------------
  381.     # remap a field name to a string name (for sort filter)
  382.  
  383.     function string_get_field_name$p_string {
  384.  
  385.         $t_map array(
  386.                 'last_updated' => 'last_update',
  387.                 'id' => 'email_bug'
  388.                 );
  389.  
  390.         $t_string $p_string;
  391.         if isset$t_map$p_string ) ) {
  392.             $t_string $t_map$p_string ];
  393.         }
  394.         return lang_get_defaulted$t_string );
  395.     }
  396.  
  397.     # --------------------
  398.     # Calls htmlentities on the specified string, passing along
  399.     # the current charset.
  400.     function string_html_entities$p_string {
  401.         return htmlentities$p_stringENT_COMPATconfig_get('charset') );
  402.     }
  403.  
  404.     # --------------------
  405.     # Calls htmlspecialchars on the specified string, passing along
  406.     # the current charset, if the current PHP version supports it.
  407.     function string_html_specialchars$p_string {
  408.         # achumakov: @ added to avoid warning output in unsupported codepages
  409.         # e.g. 8859-2, windows-1257, Korean, which are treated as 8859-1.
  410.         # This is VERY important for Eastern European, Baltic and Korean languages
  411.         return preg_replace("/&amp;(#[0-9]+|[a-z]+);/i""&$1;"@htmlspecialchars$p_stringENT_COMPATconfig_get('charset') ) );
  412.     }
  413.     
  414.     # --------------------
  415.     # Prepares a string to be used as part of header().
  416.     function string_prepare_header$p_string {
  417.         $t_string $p_string;
  418.  
  419.         $t_truncate_pos strpos($p_string"\n");
  420.         if ($t_truncate_pos !== false {
  421.             $t_string substr($t_string0$t_truncate_pos);
  422.         }
  423.  
  424.         $t_truncate_pos strpos($p_string"\r");
  425.         if ($t_truncate_pos !== false {
  426.             $t_string substr($t_string0$t_truncate_pos);
  427.         }
  428.  
  429.         return $t_string;
  430.     }
  431.  
  432.     # --------------------
  433.     # Checks the supplied string for scripting characters, if it contains any, then return true, otherwise return false.
  434.     function string_contains_scripting_chars$p_string {
  435.         if ( ( strstr$p_string'<' !== false || strstr$p_string'>' !== false ) ) {
  436.             return true;
  437.         }
  438.  
  439.         return false;
  440.     }
  441. ?>

Documentation generated on Fri, 03 Apr 2009 14:13:50 +0200 by phpDocumentor 1.4.1