/wp-content/plugins/contact-form-7/modules/captcha.php

https://github.com/konscript/schroderpartners · PHP · 490 lines · 357 code · 121 blank · 12 comment · 73 complexity · 5ed8bf47d5c7b9d9f7281081256ac716 MD5 · raw file

  1. <?php
  2. /**
  3. ** A base module for [captchac] and [captchar]
  4. **/
  5. /* Shortcode handler */
  6. wpcf7_add_shortcode( 'captchac', 'wpcf7_captcha_shortcode_handler', true );
  7. wpcf7_add_shortcode( 'captchar', 'wpcf7_captcha_shortcode_handler', true );
  8. function wpcf7_captcha_shortcode_handler( $tag ) {
  9. if ( ! is_array( $tag ) )
  10. return '';
  11. $type = $tag['type'];
  12. $name = $tag['name'];
  13. $options = (array) $tag['options'];
  14. $values = (array) $tag['values'];
  15. if ( empty( $name ) )
  16. return '';
  17. $validation_error = wpcf7_get_validation_error( $name );
  18. $atts = '';
  19. $id_att = '';
  20. $class_att = '';
  21. $size_att = '';
  22. $maxlength_att = '';
  23. $tabindex_att = '';
  24. if ( 'captchac' == $type )
  25. $class_att .= ' wpcf7-captcha-' . $name;
  26. foreach ( $options as $option ) {
  27. if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
  28. $id_att = $matches[1];
  29. } elseif ( preg_match( '%^class:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
  30. $class_att .= ' ' . $matches[1];
  31. } elseif ( preg_match( '%^([0-9]*)[/x]([0-9]*)$%', $option, $matches ) ) {
  32. $size_att = (int) $matches[1];
  33. $maxlength_att = (int) $matches[2];
  34. } elseif ( preg_match( '%^tabindex:(\d+)$%', $option, $matches ) ) {
  35. $tabindex_att = (int) $matches[1];
  36. }
  37. }
  38. if ( $id_att )
  39. $atts .= ' id="' . trim( $id_att ) . '"';
  40. if ( $class_att )
  41. $atts .= ' class="' . trim( $class_att ) . '"';
  42. // Value.
  43. if ( ! wpcf7_is_posted() && isset( $values[0] ) )
  44. $value = $values[0];
  45. else
  46. $value = '';
  47. if ( 'captchac' == $type ) {
  48. if ( ! class_exists( 'ReallySimpleCaptcha' ) ) {
  49. return '<em>' . __( 'To use CAPTCHA, you need <a href="http://wordpress.org/extend/plugins/really-simple-captcha/">Really Simple CAPTCHA</a> plugin installed.', 'wpcf7' ) . '</em>';
  50. }
  51. $op = array();
  52. // Default
  53. $op['img_size'] = array( 72, 24 );
  54. $op['base'] = array( 6, 18 );
  55. $op['font_size'] = 14;
  56. $op['font_char_width'] = 15;
  57. $op = array_merge( $op, wpcf7_captchac_options( $options ) );
  58. if ( ! $filename = wpcf7_generate_captcha( $op ) )
  59. return '';
  60. if ( is_array( $op['img_size'] ) )
  61. $atts .= ' width="' . $op['img_size'][0] . '" height="' . $op['img_size'][1] . '"';
  62. $captcha_url = trailingslashit( wpcf7_captcha_tmp_url() ) . $filename;
  63. $html = '<img alt="captcha" src="' . $captcha_url . '"' . $atts . ' />';
  64. $ref = substr( $filename, 0, strrpos( $filename, '.' ) );
  65. $html = '<input type="hidden" name="_wpcf7_captcha_challenge_' . $name . '" value="' . $ref . '" />' . $html;
  66. return $html;
  67. } elseif ( 'captchar' == $type ) {
  68. if ( $size_att )
  69. $atts .= ' size="' . $size_att . '"';
  70. else
  71. $atts .= ' size="40"'; // default size
  72. if ( $maxlength_att )
  73. $atts .= ' maxlength="' . $maxlength_att . '"';
  74. if ( '' !== $tabindex_att )
  75. $atts .= sprintf( ' tabindex="%d"', $tabindex_att );
  76. $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />';
  77. $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
  78. return $html;
  79. }
  80. }
  81. /* Validation filter */
  82. add_filter( 'wpcf7_validate_captchar', 'wpcf7_captcha_validation_filter', 10, 2 );
  83. function wpcf7_captcha_validation_filter( $result, $tag ) {
  84. $type = $tag['type'];
  85. $name = $tag['name'];
  86. $_POST[$name] = (string) $_POST[$name];
  87. $captchac = '_wpcf7_captcha_challenge_' . $name;
  88. if ( ! wpcf7_check_captcha( $_POST[$captchac], $_POST[$name] ) ) {
  89. $result['valid'] = false;
  90. $result['reason'][$name] = wpcf7_get_message( 'captcha_not_match' );
  91. }
  92. wpcf7_remove_captcha( $_POST[$captchac] );
  93. return $result;
  94. }
  95. /* Ajax echo filter */
  96. add_filter( 'wpcf7_ajax_onload', 'wpcf7_captcha_ajax_refill' );
  97. add_filter( 'wpcf7_ajax_json_echo', 'wpcf7_captcha_ajax_refill' );
  98. function wpcf7_captcha_ajax_refill( $items ) {
  99. if ( ! is_array( $items ) )
  100. return $items;
  101. $fes = wpcf7_scan_shortcode( array( 'type' => 'captchac' ) );
  102. if ( empty( $fes ) )
  103. return $items;
  104. $refill = array();
  105. foreach ( $fes as $fe ) {
  106. $name = $fe['name'];
  107. $options = $fe['options'];
  108. if ( empty( $name ) )
  109. continue;
  110. $op = wpcf7_captchac_options( $options );
  111. if ( $filename = wpcf7_generate_captcha( $op ) ) {
  112. $captcha_url = trailingslashit( wpcf7_captcha_tmp_url() ) . $filename;
  113. $refill[$name] = $captcha_url;
  114. }
  115. }
  116. if ( ! empty( $refill ) )
  117. $items['captcha'] = $refill;
  118. return $items;
  119. }
  120. /* Messages */
  121. add_filter( 'wpcf7_messages', 'wpcf7_captcha_messages' );
  122. function wpcf7_captcha_messages( $messages ) {
  123. return array_merge( $messages, array( 'captcha_not_match' => array(
  124. 'description' => __( "The code that sender entered does not match the CAPTCHA", 'wpcf7' ),
  125. 'default' => __( 'Your entered code is incorrect.', 'wpcf7' )
  126. ) ) );
  127. }
  128. /* Tag generator */
  129. add_action( 'admin_init', 'wpcf7_add_tag_generator_captcha', 45 );
  130. function wpcf7_add_tag_generator_captcha() {
  131. wpcf7_add_tag_generator( 'captcha', __( 'CAPTCHA', 'wpcf7' ),
  132. 'wpcf7-tg-pane-captcha', 'wpcf7_tg_pane_captcha' );
  133. }
  134. function wpcf7_tg_pane_captcha( &$contact_form ) {
  135. ?>
  136. <div id="wpcf7-tg-pane-captcha" class="hidden">
  137. <form action="">
  138. <table>
  139. <?php if ( ! class_exists( 'ReallySimpleCaptcha' ) ) : ?>
  140. <tr><td colspan="2"><strong style="color: #e6255b"><?php echo esc_html( __( "Note: To use CAPTCHA, you need Really Simple CAPTCHA plugin installed.", 'wpcf7' ) ); ?></strong><br /><a href="http://wordpress.org/extend/plugins/really-simple-captcha/">http://wordpress.org/extend/plugins/really-simple-captcha/</a></td></tr>
  141. <?php endif; ?>
  142. <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
  143. </table>
  144. <table class="scope captchac">
  145. <caption><?php echo esc_html( __( "Image settings", 'wpcf7' ) ); ?></caption>
  146. <tr>
  147. <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  148. <input type="text" name="id" class="idvalue oneline option" /></td>
  149. <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  150. <input type="text" name="class" class="classvalue oneline option" /></td>
  151. </tr>
  152. <tr>
  153. <td><?php echo esc_html( __( "Foreground color", 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  154. <input type="text" name="fg" class="color oneline option" /></td>
  155. <td><?php echo esc_html( __( "Background color", 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  156. <input type="text" name="bg" class="color oneline option" /></td>
  157. </tr>
  158. <tr><td colspan="2"><?php echo esc_html( __( "Image size", 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  159. <input type="checkbox" name="size:s" class="exclusive option" />&nbsp;<?php echo esc_html( __( "Small", 'wpcf7' ) ); ?>&emsp;
  160. <input type="checkbox" name="size:m" class="exclusive option" />&nbsp;<?php echo esc_html( __( "Medium", 'wpcf7' ) ); ?>&emsp;
  161. <input type="checkbox" name="size:l" class="exclusive option" />&nbsp;<?php echo esc_html( __( "Large", 'wpcf7' ) ); ?>
  162. </td></tr>
  163. </table>
  164. <table class="scope captchar">
  165. <caption><?php echo esc_html( __( "Input field settings", 'wpcf7' ) ); ?></caption>
  166. <tr>
  167. <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  168. <input type="text" name="id" class="idvalue oneline option" /></td>
  169. <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  170. <input type="text" name="class" class="classvalue oneline option" /></td>
  171. </tr>
  172. <tr>
  173. <td><code>size</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  174. <input type="text" name="size" class="numeric oneline option" /></td>
  175. <td><code>maxlength</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
  176. <input type="text" name="maxlength" class="numeric oneline option" /></td>
  177. </tr>
  178. </table>
  179. <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?>
  180. <br />1) <?php echo esc_html( __( "For image", 'wpcf7' ) ); ?>
  181. <input type="text" name="captchac" class="tag" readonly="readonly" onfocus="this.select()" />
  182. <br />2) <?php echo esc_html( __( "For input field", 'wpcf7' ) ); ?>
  183. <input type="text" name="captchar" class="tag" readonly="readonly" onfocus="this.select()" />
  184. </div>
  185. </form>
  186. </div>
  187. <?php
  188. }
  189. /* Warning message */
  190. add_action( 'wpcf7_admin_before_subsubsub', 'wpcf7_captcha_display_warning_message' );
  191. function wpcf7_captcha_display_warning_message( &$contact_form ) {
  192. if ( ! $contact_form )
  193. return;
  194. $has_tags = (bool) $contact_form->form_scan_shortcode(
  195. array( 'type' => array( 'captchac' ) ) );
  196. if ( ! $has_tags )
  197. return;
  198. if ( ! class_exists( 'ReallySimpleCaptcha' ) )
  199. return;
  200. $uploads_dir = wpcf7_captcha_tmp_dir();
  201. wpcf7_init_captcha();
  202. if ( ! is_dir( $uploads_dir ) || ! is_writable( $uploads_dir ) ) {
  203. $message = sprintf( __( 'This contact form contains CAPTCHA fields, but the temporary folder for the files (%s) does not exist or is not writable. You can create the folder or change its permission manually.', 'wpcf7' ), $uploads_dir );
  204. echo '<div class="error"><p><strong>' . esc_html( $message ) . '</strong></p></div>';
  205. }
  206. if ( ! function_exists( 'imagecreatetruecolor' ) || ! function_exists( 'imagettftext' ) ) {
  207. $message = __( 'This contact form contains CAPTCHA fields, but the necessary libraries (GD and FreeType) are not available on your server.', 'wpcf7' );
  208. echo '<div class="error"><p><strong>' . esc_html( $message ) . '</strong></p></div>';
  209. }
  210. }
  211. /* CAPTCHA functions */
  212. function wpcf7_init_captcha() {
  213. global $wpcf7_captcha;
  214. if ( ! class_exists( 'ReallySimpleCaptcha' ) )
  215. return false;
  216. if ( ! is_object( $wpcf7_captcha ) )
  217. $wpcf7_captcha = new ReallySimpleCaptcha();
  218. $captcha =& $wpcf7_captcha;
  219. $captcha->tmp_dir = trailingslashit( wpcf7_captcha_tmp_dir() );
  220. wp_mkdir_p( $captcha->tmp_dir );
  221. return true;
  222. }
  223. function wpcf7_captcha_tmp_dir() {
  224. if ( defined( 'WPCF7_CAPTCHA_TMP_DIR' ) )
  225. return WPCF7_CAPTCHA_TMP_DIR;
  226. else
  227. return wpcf7_upload_dir( 'dir' ) . '/wpcf7_captcha';
  228. }
  229. function wpcf7_captcha_tmp_url() {
  230. if ( defined( 'WPCF7_CAPTCHA_TMP_URL' ) )
  231. return WPCF7_CAPTCHA_TMP_URL;
  232. else
  233. return wpcf7_upload_dir( 'url' ) . '/wpcf7_captcha';
  234. }
  235. function wpcf7_generate_captcha( $options = null ) {
  236. global $wpcf7_captcha;
  237. if ( ! wpcf7_init_captcha() )
  238. return false;
  239. $captcha =& $wpcf7_captcha;
  240. if ( ! is_dir( $captcha->tmp_dir ) || ! is_writable( $captcha->tmp_dir ) )
  241. return false;
  242. $img_type = imagetypes();
  243. if ( $img_type & IMG_PNG )
  244. $captcha->img_type = 'png';
  245. elseif ( $img_type & IMG_GIF )
  246. $captcha->img_type = 'gif';
  247. elseif ( $img_type & IMG_JPG )
  248. $captcha->img_type = 'jpeg';
  249. else
  250. return false;
  251. if ( is_array( $options ) ) {
  252. if ( isset( $options['img_size'] ) )
  253. $captcha->img_size = $options['img_size'];
  254. if ( isset( $options['base'] ) )
  255. $captcha->base = $options['base'];
  256. if ( isset( $options['font_size'] ) )
  257. $captcha->font_size = $options['font_size'];
  258. if ( isset( $options['font_char_width'] ) )
  259. $captcha->font_char_width = $options['font_char_width'];
  260. if ( isset( $options['fg'] ) )
  261. $captcha->fg = $options['fg'];
  262. if ( isset( $options['bg'] ) )
  263. $captcha->bg = $options['bg'];
  264. }
  265. $prefix = mt_rand();
  266. $captcha_word = $captcha->generate_random_word();
  267. return $captcha->generate_image( $prefix, $captcha_word );
  268. }
  269. function wpcf7_check_captcha( $prefix, $response ) {
  270. global $wpcf7_captcha;
  271. if ( ! wpcf7_init_captcha() )
  272. return false;
  273. $captcha =& $wpcf7_captcha;
  274. return $captcha->check( $prefix, $response );
  275. }
  276. function wpcf7_remove_captcha( $prefix ) {
  277. global $wpcf7_captcha;
  278. if ( ! wpcf7_init_captcha() )
  279. return false;
  280. $captcha =& $wpcf7_captcha;
  281. if ( preg_match( '/[^0-9]/', $prefix ) ) // Contact Form 7 generates $prefix with mt_rand()
  282. return false;
  283. $captcha->remove( $prefix );
  284. }
  285. function wpcf7_cleanup_captcha_files() {
  286. global $wpcf7_captcha;
  287. if ( ! wpcf7_init_captcha() )
  288. return false;
  289. $captcha =& $wpcf7_captcha;
  290. if ( is_callable( array( $captcha, 'cleanup' ) ) )
  291. return $captcha->cleanup();
  292. $dir = trailingslashit( wpcf7_captcha_tmp_dir() );
  293. if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! is_writable( $dir ) )
  294. return false;
  295. if ( $handle = @opendir( $dir ) ) {
  296. while ( false !== ( $file = readdir( $handle ) ) ) {
  297. if ( ! preg_match( '/^[0-9]+\.(php|png|gif|jpeg)$/', $file ) )
  298. continue;
  299. $stat = @stat( $dir . $file );
  300. if ( $stat['mtime'] + 3600 < time() ) // 3600 secs == 1 hour
  301. @unlink( $dir . $file );
  302. }
  303. closedir( $handle );
  304. }
  305. }
  306. if ( ! is_admin() && 'GET' == $_SERVER['REQUEST_METHOD'] )
  307. wpcf7_cleanup_captcha_files();
  308. function wpcf7_captchac_options( $options ) {
  309. if ( ! is_array( $options ) )
  310. return array();
  311. $op = array();
  312. $image_size_array = preg_grep( '%^size:[smlSML]$%', $options );
  313. if ( $image_size = array_shift( $image_size_array ) ) {
  314. preg_match( '%^size:([smlSML])$%', $image_size, $is_matches );
  315. switch ( strtolower( $is_matches[1] ) ) {
  316. case 's':
  317. $op['img_size'] = array( 60, 20 );
  318. $op['base'] = array( 6, 15 );
  319. $op['font_size'] = 11;
  320. $op['font_char_width'] = 13;
  321. break;
  322. case 'l':
  323. $op['img_size'] = array( 84, 28 );
  324. $op['base'] = array( 6, 20 );
  325. $op['font_size'] = 17;
  326. $op['font_char_width'] = 19;
  327. break;
  328. case 'm':
  329. default:
  330. $op['img_size'] = array( 72, 24 );
  331. $op['base'] = array( 6, 18 );
  332. $op['font_size'] = 14;
  333. $op['font_char_width'] = 15;
  334. }
  335. }
  336. $fg_color_array = preg_grep( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
  337. if ( $fg_color = array_shift( $fg_color_array ) ) {
  338. preg_match( '%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $fg_color, $fc_matches );
  339. if ( 3 == strlen( $fc_matches[1] ) ) {
  340. $r = substr( $fc_matches[1], 0, 1 );
  341. $g = substr( $fc_matches[1], 1, 1 );
  342. $b = substr( $fc_matches[1], 2, 1 );
  343. $op['fg'] = array( hexdec( $r . $r ), hexdec( $g . $g ), hexdec( $b . $b ) );
  344. } elseif ( 6 == strlen( $fc_matches[1] ) ) {
  345. $r = substr( $fc_matches[1], 0, 2 );
  346. $g = substr( $fc_matches[1], 2, 2 );
  347. $b = substr( $fc_matches[1], 4, 2 );
  348. $op['fg'] = array( hexdec( $r ), hexdec( $g ), hexdec( $b ) );
  349. }
  350. }
  351. $bg_color_array = preg_grep( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options );
  352. if ( $bg_color = array_shift( $bg_color_array ) ) {
  353. preg_match( '%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $bg_color, $bc_matches );
  354. if ( 3 == strlen( $bc_matches[1] ) ) {
  355. $r = substr( $bc_matches[1], 0, 1 );
  356. $g = substr( $bc_matches[1], 1, 1 );
  357. $b = substr( $bc_matches[1], 2, 1 );
  358. $op['bg'] = array( hexdec( $r . $r ), hexdec( $g . $g ), hexdec( $b . $b ) );
  359. } elseif ( 6 == strlen( $bc_matches[1] ) ) {
  360. $r = substr( $bc_matches[1], 0, 2 );
  361. $g = substr( $bc_matches[1], 2, 2 );
  362. $b = substr( $bc_matches[1], 4, 2 );
  363. $op['bg'] = array( hexdec( $r ), hexdec( $g ), hexdec( $b ) );
  364. }
  365. }
  366. return $op;
  367. }
  368. $wpcf7_captcha = null;
  369. ?>