PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/includes/functions.php

https://bitbucket.org/wphupp/tmp-passion-sourire
PHP | 362 lines | 263 code | 91 blank | 8 comment | 48 complexity | 6dd8c83801fb910a85007cf70ffb71ce MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, Apache-2.0, MIT
  1. <?php
  2. function wpcf7_plugin_path( $path = '' ) {
  3. return path_join( WPCF7_PLUGIN_DIR, trim( $path, '/' ) );
  4. }
  5. function wpcf7_plugin_url( $path = '' ) {
  6. $url = plugins_url( $path, WPCF7_PLUGIN );
  7. if ( is_ssl() && 'http:' == substr( $url, 0, 5 ) ) {
  8. $url = 'https:' . substr( $url, 5 );
  9. }
  10. return $url;
  11. }
  12. function wpcf7_upload_dir( $type = false ) {
  13. $uploads = wp_get_upload_dir();
  14. $uploads = apply_filters( 'wpcf7_upload_dir', array(
  15. 'dir' => $uploads['basedir'],
  16. 'url' => $uploads['baseurl'] ) );
  17. if ( 'dir' == $type ) {
  18. return $uploads['dir'];
  19. } if ( 'url' == $type ) {
  20. return $uploads['url'];
  21. }
  22. return $uploads;
  23. }
  24. function wpcf7_verify_nonce( $nonce, $action = -1 ) {
  25. if ( substr( wp_hash( $action, 'nonce' ), -12, 10 ) == $nonce ) {
  26. return true;
  27. }
  28. return false;
  29. }
  30. function wpcf7_create_nonce( $action = -1 ) {
  31. return substr( wp_hash( $action, 'nonce' ), -12, 10 );
  32. }
  33. function wpcf7_blacklist_check( $target ) {
  34. $mod_keys = trim( get_option( 'blacklist_keys' ) );
  35. if ( empty( $mod_keys ) ) {
  36. return false;
  37. }
  38. $words = explode( "\n", $mod_keys );
  39. foreach ( (array) $words as $word ) {
  40. $word = trim( $word );
  41. if ( empty( $word ) || 256 < strlen( $word ) ) {
  42. continue;
  43. }
  44. $pattern = sprintf( '#%s#i', preg_quote( $word, '#' ) );
  45. if ( preg_match( $pattern, $target ) ) {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. function wpcf7_array_flatten( $input ) {
  52. if ( ! is_array( $input ) ) {
  53. return array( $input );
  54. }
  55. $output = array();
  56. foreach ( $input as $value ) {
  57. $output = array_merge( $output, wpcf7_array_flatten( $value ) );
  58. }
  59. return $output;
  60. }
  61. function wpcf7_flat_join( $input ) {
  62. $input = wpcf7_array_flatten( $input );
  63. $output = array();
  64. foreach ( (array) $input as $value ) {
  65. $output[] = trim( (string) $value );
  66. }
  67. return implode( ', ', $output );
  68. }
  69. function wpcf7_support_html5() {
  70. return (bool) apply_filters( 'wpcf7_support_html5', true );
  71. }
  72. function wpcf7_support_html5_fallback() {
  73. return (bool) apply_filters( 'wpcf7_support_html5_fallback', false );
  74. }
  75. function wpcf7_use_really_simple_captcha() {
  76. return apply_filters( 'wpcf7_use_really_simple_captcha',
  77. WPCF7_USE_REALLY_SIMPLE_CAPTCHA );
  78. }
  79. function wpcf7_validate_configuration() {
  80. return apply_filters( 'wpcf7_validate_configuration',
  81. WPCF7_VALIDATE_CONFIGURATION );
  82. }
  83. function wpcf7_load_js() {
  84. return apply_filters( 'wpcf7_load_js', WPCF7_LOAD_JS );
  85. }
  86. function wpcf7_load_css() {
  87. return apply_filters( 'wpcf7_load_css', WPCF7_LOAD_CSS );
  88. }
  89. function wpcf7_format_atts( $atts ) {
  90. $html = '';
  91. $prioritized_atts = array( 'type', 'name', 'value' );
  92. foreach ( $prioritized_atts as $att ) {
  93. if ( isset( $atts[$att] ) ) {
  94. $value = trim( $atts[$att] );
  95. $html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
  96. unset( $atts[$att] );
  97. }
  98. }
  99. foreach ( $atts as $key => $value ) {
  100. $key = strtolower( trim( $key ) );
  101. if ( ! preg_match( '/^[a-z_:][a-z_:.0-9-]*$/', $key ) ) {
  102. continue;
  103. }
  104. $value = trim( $value );
  105. if ( '' !== $value ) {
  106. $html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) );
  107. }
  108. }
  109. $html = trim( $html );
  110. return $html;
  111. }
  112. function wpcf7_link( $url, $anchor_text, $args = '' ) {
  113. $defaults = array(
  114. 'id' => '',
  115. 'class' => '',
  116. );
  117. $args = wp_parse_args( $args, $defaults );
  118. $args = array_intersect_key( $args, $defaults );
  119. $atts = wpcf7_format_atts( $args );
  120. $link = sprintf( '<a href="%1$s"%3$s>%2$s</a>',
  121. esc_url( $url ),
  122. esc_html( $anchor_text ),
  123. $atts ? ( ' ' . $atts ) : '' );
  124. return $link;
  125. }
  126. function wpcf7_get_request_uri() {
  127. static $request_uri = '';
  128. if ( empty( $request_uri ) ) {
  129. $request_uri = add_query_arg( array() );
  130. }
  131. return esc_url_raw( $request_uri );
  132. }
  133. function wpcf7_register_post_types() {
  134. if ( class_exists( 'WPCF7_ContactForm' ) ) {
  135. WPCF7_ContactForm::register_post_type();
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141. function wpcf7_version( $args = '' ) {
  142. $defaults = array(
  143. 'limit' => -1,
  144. 'only_major' => false );
  145. $args = wp_parse_args( $args, $defaults );
  146. if ( $args['only_major'] ) {
  147. $args['limit'] = 2;
  148. }
  149. $args['limit'] = (int) $args['limit'];
  150. $ver = WPCF7_VERSION;
  151. $ver = strtr( $ver, '_-+', '...' );
  152. $ver = preg_replace( '/[^0-9.]+/', ".$0.", $ver );
  153. $ver = preg_replace( '/[.]+/', ".", $ver );
  154. $ver = trim( $ver, '.' );
  155. $ver = explode( '.', $ver );
  156. if ( -1 < $args['limit'] ) {
  157. $ver = array_slice( $ver, 0, $args['limit'] );
  158. }
  159. $ver = implode( '.', $ver );
  160. return $ver;
  161. }
  162. function wpcf7_version_grep( $version, array $input ) {
  163. $pattern = '/^' . preg_quote( (string) $version, '/' ) . '(?:\.|$)/';
  164. return preg_grep( $pattern, $input );
  165. }
  166. function wpcf7_enctype_value( $enctype ) {
  167. $enctype = trim( $enctype );
  168. if ( empty( $enctype ) ) {
  169. return '';
  170. }
  171. $valid_enctypes = array(
  172. 'application/x-www-form-urlencoded',
  173. 'multipart/form-data',
  174. 'text/plain',
  175. );
  176. if ( in_array( $enctype, $valid_enctypes ) ) {
  177. return $enctype;
  178. }
  179. $pattern = '%^enctype="(' . implode( '|', $valid_enctypes ) . ')"$%';
  180. if ( preg_match( $pattern, $enctype, $matches ) ) {
  181. return $matches[1]; // for back-compat
  182. }
  183. return '';
  184. }
  185. function wpcf7_rmdir_p( $dir ) {
  186. if ( is_file( $dir ) ) {
  187. if ( ! $result = @unlink( $dir ) ) {
  188. $stat = @stat( $dir );
  189. $perms = $stat['mode'];
  190. @chmod( $dir, $perms | 0200 ); // add write for owner
  191. if ( ! $result = @unlink( $dir ) ) {
  192. @chmod( $dir, $perms );
  193. }
  194. }
  195. return $result;
  196. }
  197. if ( ! is_dir( $dir ) ) {
  198. return false;
  199. }
  200. if ( $handle = @opendir( $dir ) ) {
  201. while ( false !== ( $file = readdir( $handle ) ) ) {
  202. if ( $file == "." || $file == ".." ) {
  203. continue;
  204. }
  205. wpcf7_rmdir_p( path_join( $dir, $file ) );
  206. }
  207. closedir( $handle );
  208. }
  209. return @rmdir( $dir );
  210. }
  211. /* From _http_build_query in wp-includes/functions.php */
  212. function wpcf7_build_query( $args, $key = '' ) {
  213. $sep = '&';
  214. $ret = array();
  215. foreach ( (array) $args as $k => $v ) {
  216. $k = urlencode( $k );
  217. if ( ! empty( $key ) ) {
  218. $k = $key . '%5B' . $k . '%5D';
  219. }
  220. if ( null === $v ) {
  221. continue;
  222. } elseif ( false === $v ) {
  223. $v = '0';
  224. }
  225. if ( is_array( $v ) || is_object( $v ) ) {
  226. array_push( $ret, wpcf7_build_query( $v, $k ) );
  227. } else {
  228. array_push( $ret, $k . '=' . urlencode( $v ) );
  229. }
  230. }
  231. return implode( $sep, $ret );
  232. }
  233. /**
  234. * Returns the number of code units in a string.
  235. *
  236. * @see http://www.w3.org/TR/html5/infrastructure.html#code-unit-length
  237. *
  238. * @return int|bool The number of code units, or false if mb_convert_encoding is not available.
  239. */
  240. function wpcf7_count_code_units( $string ) {
  241. static $use_mb = null;
  242. if ( is_null( $use_mb ) ) {
  243. $use_mb = function_exists( 'mb_convert_encoding' );
  244. }
  245. if ( ! $use_mb ) {
  246. return false;
  247. }
  248. $string = (string) $string;
  249. $encoding = mb_detect_encoding( $string, mb_detect_order(), true );
  250. if ( $encoding ) {
  251. $string = mb_convert_encoding( $string, 'UTF-16', $encoding );
  252. } else {
  253. $string = mb_convert_encoding( $string, 'UTF-16', 'UTF-8' );
  254. }
  255. $byte_count = mb_strlen( $string, '8bit' );
  256. return floor( $byte_count / 2 );
  257. }
  258. function wpcf7_is_localhost() {
  259. $server_name = strtolower( $_SERVER['SERVER_NAME'] );
  260. return in_array( $server_name, array( 'localhost', '127.0.0.1' ) );
  261. }
  262. function wpcf7_deprecated_function( $function, $version, $replacement ) {
  263. $trigger_error = apply_filters( 'deprecated_function_trigger_error', true );
  264. if ( WP_DEBUG && $trigger_error ) {
  265. if ( function_exists( '__' ) ) {
  266. trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', 'contact-form-7' ), $function, $version, $replacement ) );
  267. } else {
  268. trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since Contact Form 7 version %2$s! Use %3$s instead.', $function, $version, $replacement ) );
  269. }
  270. }
  271. }