/includes/formatting.php

  1. <?php 
  2.  
  3. function wpcf7_autop( $pee, $br = 1 ) { 
  4. if ( trim( $pee ) === '' ) { 
  5. return ''; 
  6.  
  7. $pee = $pee . "\n"; // just to make things a little easier, pad the end 
  8. $pee = preg_replace( '|<br />\s*<br />|', "\n\n", $pee ); 
  9. // Space things out a little 
  10. /** wpcf7: remove select and input */ 
  11. $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; 
  12. $pee = preg_replace( '!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee ); 
  13. $pee = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $pee ); 
  14.  
  15. /** wpcf7: take care of [response], [recaptcha], and [hidden] tags */ 
  16. $block_hidden_form_tags = '(?:response|recaptcha|hidden)'; 
  17. $pee = preg_replace( '!(\[' . $block_hidden_form_tags . '[^]]*\])!',  
  18. "\n$1\n\n", $pee ); 
  19.  
  20. $pee = str_replace( array( "\r\n", "\r" ), "\n", $pee ); // cross-platform newlines 
  21.  
  22. if ( strpos( $pee, '<object' ) !== false ) { 
  23. $pee = preg_replace( '|\s*<param([^>]*)>\s*|', "<param$1>", $pee ); // no pee inside object/embed 
  24. $pee = preg_replace( '|\s*</embed>\s*|', '</embed>', $pee ); 
  25.  
  26. $pee = preg_replace( "/\n\n+/", "\n\n", $pee ); // take care of duplicates 
  27. // make paragraphs, including one at the end 
  28. $pees = preg_split( '/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY ); 
  29. $pee = ''; 
  30.  
  31. foreach ( $pees as $tinkle ) { 
  32. $pee .= '<p>' . trim( $tinkle, "\n" ) . "</p>\n"; 
  33.  
  34. $pee = preg_replace( '|<p>\s*</p>|', '', $pee ); // under certain strange conditions it could create a P of entirely whitespace 
  35. $pee = preg_replace( '!<p>([^<]+)</(div|address|form|fieldset)>!', "<p>$1</p></$2>", $pee ); 
  36. $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee ); // don't pee all over a tag 
  37. $pee = preg_replace( "|<p>(<li.+?)</p>|", "$1", $pee ); // problem with nested lists 
  38. $pee = preg_replace( '|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee ); 
  39. $pee = str_replace( '</blockquote></p>', '</p></blockquote>', $pee ); 
  40. $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee ); 
  41. $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee ); 
  42.  
  43. /** wpcf7: take care of [response], [recaptcha], and [hidden] tag */ 
  44. $pee = preg_replace( '!<p>\s*(\[' . $block_hidden_form_tags . '[^]]*\])!',  
  45. "$1", $pee ); 
  46. $pee = preg_replace( '!(\[' . $block_hidden_form_tags . '[^]]*\])\s*</p>!',  
  47. "$1", $pee ); 
  48.  
  49. if ( $br ) { 
  50. /** wpcf7: add textarea */ 
  51. $pee = preg_replace_callback( '/<(script|style|textarea).*?<\/\\1>/s', create_function( '$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);' ), $pee ); 
  52. $pee = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $pee ); // optionally make line breaks 
  53. $pee = str_replace( '<WPPreserveNewline />', "\n", $pee ); 
  54. $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee ); 
  55. $pee = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee ); 
  56. if ( strpos( $pee, '<pre' ) !== false ) 
  57. $pee = preg_replace_callback( '!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee ); 
  58. $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); 
  59.  
  60. return $pee; 
  61.  
  62. function wpcf7_sanitize_query_var( $text ) { 
  63. $text = wp_unslash( $text ); 
  64. $text = wp_check_invalid_utf8( $text ); 
  65.  
  66. if ( false !== strpos( $text, '<' ) ) { 
  67. $text = wp_pre_kses_less_than( $text ); 
  68. $text = wp_strip_all_tags( $text ); 
  69.  
  70. $text = preg_replace( '/%[a-f0-9]{2}/i', '', $text ); 
  71. $text = preg_replace( '/ +/', ' ', $text ); 
  72. $text = trim( $text, ' ' ); 
  73.  
  74. return $text; 
  75.  
  76. function wpcf7_strip_quote( $text ) { 
  77. $text = trim( $text ); 
  78.  
  79. if ( preg_match( '/^"(.*)"$/s', $text, $matches ) ) { 
  80. $text = $matches[1]; 
  81. } elseif ( preg_match( "/^'(.*)'$/s", $text, $matches ) ) { 
  82. $text = $matches[1]; 
  83.  
  84. return $text; 
  85.  
  86. function wpcf7_strip_quote_deep( $arr ) { 
  87. if ( is_string( $arr ) ) { 
  88. return wpcf7_strip_quote( $arr ); 
  89.  
  90. if ( is_array( $arr ) ) { 
  91. $result = array(); 
  92.  
  93. foreach ( $arr as $key => $text ) { 
  94. $result[$key] = wpcf7_strip_quote_deep( $text ); 
  95.  
  96. return $result; 
  97.  
  98. function wpcf7_normalize_newline( $text, $to = "\n" ) { 
  99. if ( ! is_string( $text ) ) { 
  100. return $text; 
  101.  
  102. $nls = array( "\r\n", "\r", "\n" ); 
  103.  
  104. if ( ! in_array( $to, $nls ) ) { 
  105. return $text; 
  106.  
  107. return str_replace( $nls, $to, $text ); 
  108.  
  109. function wpcf7_normalize_newline_deep( $arr, $to = "\n" ) { 
  110. if ( is_array( $arr ) ) { 
  111. $result = array(); 
  112.  
  113. foreach ( $arr as $key => $text ) { 
  114. $result[$key] = wpcf7_normalize_newline_deep( $text, $to ); 
  115.  
  116. return $result; 
  117.  
  118. return wpcf7_normalize_newline( $arr, $to ); 
  119.  
  120. function wpcf7_strip_newline( $str ) { 
  121. $str = (string) $str; 
  122. $str = str_replace( array( "\r", "\n" ), '', $str ); 
  123. return trim( $str ); 
  124.  
  125. function wpcf7_canonicalize( $text ) { 
  126. if ( function_exists( 'mb_convert_kana' ) 
  127. && 'UTF-8' == get_option( 'blog_charset' ) ) { 
  128. $text = mb_convert_kana( $text, 'asKV', 'UTF-8' ); 
  129.  
  130. $text = strtolower( $text ); 
  131. $text = trim( $text ); 
  132. return $text; 
  133.  
  134. /** 
  135. * Check whether a string is a valid NAME token. 
  136. * 
  137. * ID and NAME tokens must begin with a letter ([A-Za-z]) 
  138. * and may be followed by any number of letters, digits ([0-9]),  
  139. * hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). 
  140. * 
  141. * @see http://www.w3.org/TR/html401/types.html#h-6.2 
  142. * 
  143. * @return bool True if it is a valid name, false if not. 
  144. */ 
  145. function wpcf7_is_name( $string ) { 
  146. return preg_match( '/^[A-Za-z][-A-Za-z0-9_:.]*$/', $string ); 
  147.  
  148. function wpcf7_sanitize_unit_tag( $tag ) { 
  149. $tag = preg_replace( '/[^A-Za-z0-9_-]/', '', $tag ); 
  150. return $tag; 
  151.  
  152. function wpcf7_is_email( $email ) { 
  153. $result = is_email( $email ); 
  154. return apply_filters( 'wpcf7_is_email', $result, $email ); 
  155.  
  156. function wpcf7_is_url( $url ) { 
  157. $result = ( false !== filter_var( $url, FILTER_VALIDATE_URL ) ); 
  158. return apply_filters( 'wpcf7_is_url', $result, $url ); 
  159.  
  160. function wpcf7_is_tel( $tel ) { 
  161. $result = preg_match( '%^[+]?[0-9()/ -]*$%', $tel ); 
  162. return apply_filters( 'wpcf7_is_tel', $result, $tel ); 
  163.  
  164. function wpcf7_is_number( $number ) { 
  165. $result = is_numeric( $number ); 
  166. return apply_filters( 'wpcf7_is_number', $result, $number ); 
  167.  
  168. function wpcf7_is_date( $date ) { 
  169. $result = preg_match( '/^([0-9]{4, })-([0-9]{2})-([0-9]{2})$/', $date, $matches ); 
  170.  
  171. if ( $result ) { 
  172. $result = checkdate( $matches[2], $matches[3], $matches[1] ); 
  173.  
  174. return apply_filters( 'wpcf7_is_date', $result, $date ); 
  175.  
  176. function wpcf7_is_mailbox_list( $mailbox_list ) { 
  177. if ( ! is_array( $mailbox_list ) ) { 
  178. $mailbox_list = explode( ', ', (string) $mailbox_list ); 
  179.  
  180. $addresses = array(); 
  181.  
  182. foreach ( $mailbox_list as $mailbox ) { 
  183. if ( ! is_string( $mailbox ) ) { 
  184. return false; 
  185.  
  186. $mailbox = trim( $mailbox ); 
  187.  
  188. if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) { 
  189. $addr_spec = $matches[1]; 
  190. } else { 
  191. $addr_spec = $mailbox; 
  192.  
  193. if ( ! wpcf7_is_email( $addr_spec ) ) { 
  194. return false; 
  195.  
  196. $addresses[] = $addr_spec; 
  197.  
  198. return $addresses; 
  199.  
  200. function wpcf7_is_email_in_domain( $email, $domain ) { 
  201. $email_list = wpcf7_is_mailbox_list( $email ); 
  202. $domain = strtolower( $domain ); 
  203.  
  204. foreach ( $email_list as $email ) { 
  205. $email_domain = substr( $email, strrpos( $email, '@' ) + 1 ); 
  206. $email_domain = strtolower( $email_domain ); 
  207. $domain_parts = explode( '.', $domain ); 
  208.  
  209. do { 
  210. $site_domain = implode( '.', $domain_parts ); 
  211.  
  212. if ( $site_domain == $email_domain ) { 
  213. continue 2; 
  214.  
  215. array_shift( $domain_parts ); 
  216. } while ( $domain_parts ); 
  217.  
  218. return false; 
  219.  
  220. return true; 
  221.  
  222. function wpcf7_is_email_in_site_domain( $email ) { 
  223. if ( wpcf7_is_localhost() ) { 
  224. return true; 
  225.  
  226. $site_domain = strtolower( $_SERVER['SERVER_NAME'] ); 
  227.  
  228. if ( preg_match( '/^[0-9.]+$/', $site_domain ) ) { // 123.456.789.012 
  229. return true; 
  230.  
  231. if ( wpcf7_is_email_in_domain( $email, $site_domain ) ) { 
  232. return true; 
  233.  
  234. $home_url = home_url(); 
  235.  
  236. // for interoperability with WordPress MU Domain Mapping plugin 
  237. if ( is_multisite() && function_exists( 'domain_mapping_siteurl' ) ) { 
  238. $domain_mapping_siteurl = domain_mapping_siteurl( false ); 
  239.  
  240. if ( $domain_mapping_siteurl ) { 
  241. $home_url = $domain_mapping_siteurl; 
  242.  
  243. if ( preg_match( '%^https?://([^/]+)%', $home_url, $matches ) ) { 
  244. $site_domain = strtolower( $matches[1] ); 
  245.  
  246. if ( $site_domain != strtolower( $_SERVER['SERVER_NAME'] ) 
  247. && wpcf7_is_email_in_domain( $email, $site_domain ) ) { 
  248. return true; 
  249.  
  250. return false; 
  251.  
  252. function wpcf7_antiscript_file_name( $filename ) { 
  253. $filename = basename( $filename ); 
  254. $parts = explode( '.', $filename ); 
  255.  
  256. if ( count( $parts ) < 2 ) 
  257. return $filename; 
  258.  
  259. $script_pattern = '/^(php|phtml|pl|py|rb|cgi|asp|aspx)\d?$/i'; 
  260.  
  261. $filename = array_shift( $parts ); 
  262. $extension = array_pop( $parts ); 
  263.  
  264. foreach ( (array) $parts as $part ) { 
  265. if ( preg_match( $script_pattern, $part ) ) 
  266. $filename .= '.' . $part . '_'; 
  267. else 
  268. $filename .= '.' . $part; 
  269.  
  270. if ( preg_match( $script_pattern, $extension ) ) 
  271. $filename .= '.' . $extension . '_.txt'; 
  272. else 
  273. $filename .= '.' . $extension; 
  274.  
  275. return $filename; 
  276.  
  277. function wpcf7_mask_password( $text, $length_unmasked = 0 ) { 
  278. $length = strlen( $text ); 
  279. $length_unmasked = absint( $length_unmasked ); 
  280.  
  281. if ( 0 == $length_unmasked ) { 
  282. if ( 9 < $length ) { 
  283. $length_unmasked = 4; 
  284. } elseif ( 3 < $length ) { 
  285. $length_unmasked = 2; 
  286. } else { 
  287. $length_unmasked = $length; 
  288.  
  289. $text = substr( $text, 0 - $length_unmasked ); 
  290. $text = str_pad( $text, $length, '*', STR_PAD_LEFT ); 
  291. return $text; 
.