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