formatting.php 10 KB

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