/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.  
  55. /** wpcf7: remove extra <br /> just added before [response], [recaptcha], and [hidden] tags */ 
  56. $pee = preg_replace( '!<br />\n(\[' . $block_hidden_form_tags . '[^]]*\])!',  
  57. "\n$1", $pee ); 
  58.  
  59. $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee ); 
  60. $pee = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee ); 
  61.  
  62. if ( strpos( $pee, '<pre' ) !== false ) { 
  63. $pee = preg_replace_callback( '!(<pre[^>]*>)(.*?)</pre>!is',  
  64. 'clean_pre', $pee ); 
  65.  
  66. $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); 
  67.  
  68. return $pee; 
  69.  
  70. function wpcf7_sanitize_query_var( $text ) { 
  71. $text = wp_unslash( $text ); 
  72. $text = wp_check_invalid_utf8( $text ); 
  73.  
  74. if ( false !== strpos( $text, '<' ) ) { 
  75. $text = wp_pre_kses_less_than( $text ); 
  76. $text = wp_strip_all_tags( $text ); 
  77.  
  78. $text = preg_replace( '/%[a-f0-9]{2}/i', '', $text ); 
  79. $text = preg_replace( '/ +/', ' ', $text ); 
  80. $text = trim( $text, ' ' ); 
  81.  
  82. return $text; 
  83.  
  84. function wpcf7_strip_quote( $text ) { 
  85. $text = trim( $text ); 
  86.  
  87. if ( preg_match( '/^"(.*)"$/s', $text, $matches ) ) { 
  88. $text = $matches[1]; 
  89. } elseif ( preg_match( "/^'(.*)'$/s", $text, $matches ) ) { 
  90. $text = $matches[1]; 
  91.  
  92. return $text; 
  93.  
  94. function wpcf7_strip_quote_deep( $arr ) { 
  95. if ( is_string( $arr ) ) { 
  96. return wpcf7_strip_quote( $arr ); 
  97.  
  98. if ( is_array( $arr ) ) { 
  99. $result = array(); 
  100.  
  101. foreach ( $arr as $key => $text ) { 
  102. $result[$key] = wpcf7_strip_quote_deep( $text ); 
  103.  
  104. return $result; 
  105.  
  106. function wpcf7_normalize_newline( $text, $to = "\n" ) { 
  107. if ( ! is_string( $text ) ) { 
  108. return $text; 
  109.  
  110. $nls = array( "\r\n", "\r", "\n" ); 
  111.  
  112. if ( ! in_array( $to, $nls ) ) { 
  113. return $text; 
  114.  
  115. return str_replace( $nls, $to, $text ); 
  116.  
  117. function wpcf7_normalize_newline_deep( $arr, $to = "\n" ) { 
  118. if ( is_array( $arr ) ) { 
  119. $result = array(); 
  120.  
  121. foreach ( $arr as $key => $text ) { 
  122. $result[$key] = wpcf7_normalize_newline_deep( $text, $to ); 
  123.  
  124. return $result; 
  125.  
  126. return wpcf7_normalize_newline( $arr, $to ); 
  127.  
  128. function wpcf7_strip_newline( $str ) { 
  129. $str = (string) $str; 
  130. $str = str_replace( array( "\r", "\n" ), '', $str ); 
  131. return trim( $str ); 
  132.  
  133. function wpcf7_canonicalize( $text, $strto = 'lower' ) { 
  134. if ( function_exists( 'mb_convert_kana' ) 
  135. && 'UTF-8' == get_option( 'blog_charset' ) ) { 
  136. $text = mb_convert_kana( $text, 'asKV', 'UTF-8' ); 
  137.  
  138. if ( 'lower' == $strto ) { 
  139. $text = strtolower( $text ); 
  140. } elseif ( 'upper' == $strto ) { 
  141. $text = strtoupper( $text ); 
  142.  
  143. $text = trim( $text ); 
  144. return $text; 
  145.  
  146. /** 
  147. * Check whether a string is a valid NAME token. 
  148. * 
  149. * ID and NAME tokens must begin with a letter ([A-Za-z]) 
  150. * and may be followed by any number of letters, digits ([0-9]),  
  151. * hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). 
  152. * 
  153. * @see http://www.w3.org/TR/html401/types.html#h-6.2 
  154. * 
  155. * @return bool True if it is a valid name, false if not. 
  156. */ 
  157. function wpcf7_is_name( $string ) { 
  158. return preg_match( '/^[A-Za-z][-A-Za-z0-9_:.]*$/', $string ); 
  159.  
  160. function wpcf7_sanitize_unit_tag( $tag ) { 
  161. $tag = preg_replace( '/[^A-Za-z0-9_-]/', '', $tag ); 
  162. return $tag; 
  163.  
  164. function wpcf7_is_email( $email ) { 
  165. $result = is_email( $email ); 
  166. return apply_filters( 'wpcf7_is_email', $result, $email ); 
  167.  
  168. function wpcf7_is_url( $url ) { 
  169. $result = ( false !== filter_var( $url, FILTER_VALIDATE_URL ) ); 
  170. return apply_filters( 'wpcf7_is_url', $result, $url ); 
  171.  
  172. function wpcf7_is_tel( $tel ) { 
  173. $result = preg_match( '%^[+]?[0-9()/ -]*$%', $tel ); 
  174. return apply_filters( 'wpcf7_is_tel', $result, $tel ); 
  175.  
  176. function wpcf7_is_number( $number ) { 
  177. $result = is_numeric( $number ); 
  178. return apply_filters( 'wpcf7_is_number', $result, $number ); 
  179.  
  180. function wpcf7_is_date( $date ) { 
  181. $result = preg_match( '/^([0-9]{4, })-([0-9]{2})-([0-9]{2})$/', $date, $matches ); 
  182.  
  183. if ( $result ) { 
  184. $result = checkdate( $matches[2], $matches[3], $matches[1] ); 
  185.  
  186. return apply_filters( 'wpcf7_is_date', $result, $date ); 
  187.  
  188. function wpcf7_is_mailbox_list( $mailbox_list ) { 
  189. if ( ! is_array( $mailbox_list ) ) { 
  190. $mailbox_text = (string) $mailbox_list; 
  191. $mailbox_text = wp_unslash( $mailbox_text ); 
  192.  
  193. $mailbox_text = preg_replace( '/\\\\(?:\"|\')/', 'esc-quote',  
  194. $mailbox_text ); 
  195.  
  196. $mailbox_text = preg_replace( '/(?:\".*?\"|\'.*?\')/', 'quoted-string',  
  197. $mailbox_text ); 
  198.  
  199. $mailbox_list = explode( ', ', $mailbox_text ); 
  200.  
  201. $addresses = array(); 
  202.  
  203. foreach ( $mailbox_list as $mailbox ) { 
  204. if ( ! is_string( $mailbox ) ) { 
  205. return false; 
  206.  
  207. $mailbox = trim( $mailbox ); 
  208.  
  209. if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) { 
  210. $addr_spec = $matches[1]; 
  211. } else { 
  212. $addr_spec = $mailbox; 
  213.  
  214. if ( ! wpcf7_is_email( $addr_spec ) ) { 
  215. return false; 
  216.  
  217. $addresses[] = $addr_spec; 
  218.  
  219. return $addresses; 
  220.  
  221. function wpcf7_is_email_in_domain( $email, $domain ) { 
  222. $email_list = wpcf7_is_mailbox_list( $email ); 
  223. $domain = strtolower( $domain ); 
  224.  
  225. foreach ( $email_list as $email ) { 
  226. $email_domain = substr( $email, strrpos( $email, '@' ) + 1 ); 
  227. $email_domain = strtolower( $email_domain ); 
  228. $domain_parts = explode( '.', $domain ); 
  229.  
  230. do { 
  231. $site_domain = implode( '.', $domain_parts ); 
  232.  
  233. if ( $site_domain == $email_domain ) { 
  234. continue 2; 
  235.  
  236. array_shift( $domain_parts ); 
  237. } while ( $domain_parts ); 
  238.  
  239. return false; 
  240.  
  241. return true; 
  242.  
  243. function wpcf7_is_email_in_site_domain( $email ) { 
  244. if ( wpcf7_is_localhost() ) { 
  245. return true; 
  246.  
  247. $site_domain = strtolower( $_SERVER['SERVER_NAME'] ); 
  248.  
  249. if ( preg_match( '/^[0-9.]+$/', $site_domain ) ) { // 123.456.789.012 
  250. return true; 
  251.  
  252. if ( wpcf7_is_email_in_domain( $email, $site_domain ) ) { 
  253. return true; 
  254.  
  255. $home_url = home_url(); 
  256.  
  257. // for interoperability with WordPress MU Domain Mapping plugin 
  258. if ( is_multisite() && function_exists( 'domain_mapping_siteurl' ) ) { 
  259. $domain_mapping_siteurl = domain_mapping_siteurl( false ); 
  260.  
  261. if ( $domain_mapping_siteurl ) { 
  262. $home_url = $domain_mapping_siteurl; 
  263.  
  264. if ( preg_match( '%^https?://([^/]+)%', $home_url, $matches ) ) { 
  265. $site_domain = strtolower( $matches[1] ); 
  266.  
  267. if ( $site_domain != strtolower( $_SERVER['SERVER_NAME'] ) 
  268. && wpcf7_is_email_in_domain( $email, $site_domain ) ) { 
  269. return true; 
  270.  
  271. return false; 
  272.  
  273. function wpcf7_antiscript_file_name( $filename ) { 
  274. $filename = basename( $filename ); 
  275. $parts = explode( '.', $filename ); 
  276.  
  277. if ( count( $parts ) < 2 ) { 
  278. return $filename; 
  279.  
  280. $script_pattern = '/^(php|phtml|pl|py|rb|cgi|asp|aspx)\d?$/i'; 
  281.  
  282. $filename = array_shift( $parts ); 
  283. $extension = array_pop( $parts ); 
  284.  
  285. foreach ( (array) $parts as $part ) { 
  286. if ( preg_match( $script_pattern, $part ) ) { 
  287. $filename .= '.' . $part . '_'; 
  288. } else { 
  289. $filename .= '.' . $part; 
  290.  
  291. if ( preg_match( $script_pattern, $extension ) ) { 
  292. $filename .= '.' . $extension . '_.txt'; 
  293. } else { 
  294. $filename .= '.' . $extension; 
  295.  
  296. return $filename; 
  297.  
  298. function wpcf7_mask_password( $text, $length_unmasked = 0 ) { 
  299. $length = strlen( $text ); 
  300. $length_unmasked = absint( $length_unmasked ); 
  301.  
  302. if ( 0 == $length_unmasked ) { 
  303. if ( 9 < $length ) { 
  304. $length_unmasked = 4; 
  305. } elseif ( 3 < $length ) { 
  306. $length_unmasked = 2; 
  307. } else { 
  308. $length_unmasked = $length; 
  309.  
  310. $text = substr( $text, 0 - $length_unmasked ); 
  311. $text = str_pad( $text, $length, '*', STR_PAD_LEFT ); 
  312. return $text; 
.