PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/contact-form-7/includes/formatting.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 387 lines | 280 code | 88 blank | 19 comment | 42 complexity | d3a9b50a1e2a6b6e719debcca44e762f MD5 | raw file
  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. $pattern = '%^[+]?' // + sign
  168. . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234
  169. . '(?:[/ -]*' // delimiter
  170. . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234
  171. . ')*$%';
  172. $result = preg_match( $pattern, trim( $tel ) );
  173. return apply_filters( 'wpcf7_is_tel', $result, $tel );
  174. }
  175. function wpcf7_is_number( $number ) {
  176. $result = is_numeric( $number );
  177. return apply_filters( 'wpcf7_is_number', $result, $number );
  178. }
  179. function wpcf7_is_date( $date ) {
  180. $result = preg_match( '/^([0-9]{4,})-([0-9]{2})-([0-9]{2})$/', $date, $matches );
  181. if ( $result ) {
  182. $result = checkdate( $matches[2], $matches[3], $matches[1] );
  183. }
  184. return apply_filters( 'wpcf7_is_date', $result, $date );
  185. }
  186. function wpcf7_is_mailbox_list( $mailbox_list ) {
  187. if ( ! is_array( $mailbox_list ) ) {
  188. $mailbox_text = (string) $mailbox_list;
  189. $mailbox_text = wp_unslash( $mailbox_text );
  190. $mailbox_text = preg_replace( '/\\\\(?:\"|\')/', 'esc-quote',
  191. $mailbox_text );
  192. $mailbox_text = preg_replace( '/(?:\".*?\"|\'.*?\')/', 'quoted-string',
  193. $mailbox_text );
  194. $mailbox_list = explode( ',', $mailbox_text );
  195. }
  196. $addresses = array();
  197. foreach ( $mailbox_list as $mailbox ) {
  198. if ( ! is_string( $mailbox ) ) {
  199. return false;
  200. }
  201. $mailbox = trim( $mailbox );
  202. if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) {
  203. $addr_spec = $matches[1];
  204. } else {
  205. $addr_spec = $mailbox;
  206. }
  207. if ( ! wpcf7_is_email( $addr_spec ) ) {
  208. return false;
  209. }
  210. $addresses[] = $addr_spec;
  211. }
  212. return $addresses;
  213. }
  214. function wpcf7_is_email_in_domain( $email, $domain ) {
  215. $email_list = wpcf7_is_mailbox_list( $email );
  216. $domain = strtolower( $domain );
  217. foreach ( $email_list as $email ) {
  218. $email_domain = substr( $email, strrpos( $email, '@' ) + 1 );
  219. $email_domain = strtolower( $email_domain );
  220. $domain_parts = explode( '.', $domain );
  221. do {
  222. $site_domain = implode( '.', $domain_parts );
  223. if ( $site_domain == $email_domain ) {
  224. continue 2;
  225. }
  226. array_shift( $domain_parts );
  227. } while ( $domain_parts );
  228. return false;
  229. }
  230. return true;
  231. }
  232. function wpcf7_is_email_in_site_domain( $email ) {
  233. if ( wpcf7_is_localhost() ) {
  234. return true;
  235. }
  236. $site_domain = strtolower( $_SERVER['SERVER_NAME'] );
  237. if ( preg_match( '/^[0-9.]+$/', $site_domain ) ) { // 123.456.789.012
  238. return true;
  239. }
  240. if ( wpcf7_is_email_in_domain( $email, $site_domain ) ) {
  241. return true;
  242. }
  243. $home_url = home_url();
  244. // for interoperability with WordPress MU Domain Mapping plugin
  245. if ( is_multisite()
  246. and function_exists( 'domain_mapping_siteurl' ) ) {
  247. $domain_mapping_siteurl = domain_mapping_siteurl( false );
  248. if ( $domain_mapping_siteurl ) {
  249. $home_url = $domain_mapping_siteurl;
  250. }
  251. }
  252. if ( preg_match( '%^https?://([^/]+)%', $home_url, $matches ) ) {
  253. $site_domain = strtolower( $matches[1] );
  254. if ( $site_domain != strtolower( $_SERVER['SERVER_NAME'] )
  255. and wpcf7_is_email_in_domain( $email, $site_domain ) ) {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. function wpcf7_antiscript_file_name( $filename ) {
  262. $filename = basename( $filename );
  263. $parts = explode( '.', $filename );
  264. if ( count( $parts ) < 2 ) {
  265. return $filename;
  266. }
  267. $script_pattern = '/^(php|phtml|pl|py|rb|cgi|asp|aspx)\d?$/i';
  268. $filename = array_shift( $parts );
  269. $extension = array_pop( $parts );
  270. foreach ( (array) $parts as $part ) {
  271. if ( preg_match( $script_pattern, $part ) ) {
  272. $filename .= '.' . $part . '_';
  273. } else {
  274. $filename .= '.' . $part;
  275. }
  276. }
  277. if ( preg_match( $script_pattern, $extension ) ) {
  278. $filename .= '.' . $extension . '_.txt';
  279. } else {
  280. $filename .= '.' . $extension;
  281. }
  282. return $filename;
  283. }
  284. function wpcf7_mask_password( $text, $length_unmasked = 0 ) {
  285. $length = strlen( $text );
  286. $length_unmasked = absint( $length_unmasked );
  287. if ( 0 == $length_unmasked ) {
  288. if ( 9 < $length ) {
  289. $length_unmasked = 4;
  290. } elseif ( 3 < $length ) {
  291. $length_unmasked = 2;
  292. } else {
  293. $length_unmasked = $length;
  294. }
  295. }
  296. $text = substr( $text, 0 - $length_unmasked );
  297. $text = str_pad( $text, $length, '*', STR_PAD_LEFT );
  298. return $text;
  299. }