PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/public/wp-content/themes/modernize/include/plugin/really-simple-captcha/really-simple-captcha.php

https://gitlab.com/kath.de/cibedo_cibedo.de
PHP | 200 lines | 110 code | 45 blank | 45 comment | 16 complexity | 6df8693ae48ec41052d681bff379b5d8 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Really Simple CAPTCHA
  4. Plugin URI: http://ideasilo.wordpress.com/2009/03/14/really-simple-captcha/
  5. Description: Really Simple CAPTCHA is a CAPTCHA module intended to be called from other plugins. It is originally created for my Contact Form 7 plugin.
  6. Author: Takayuki Miyoshi
  7. Version: 1.2
  8. Author URI: http://ideasilo.wordpress.com/
  9. */
  10. /* Copyright 2007-2010 Takayuki Miyoshi (email: takayukister at gmail.com)
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. class ReallySimpleCaptcha {
  24. function ReallySimpleCaptcha() {
  25. /* Characters available in images */
  26. $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  27. /* Length of a word in an image */
  28. $this->char_length = 4;
  29. /* Array of fonts. Randomly picked up per character */
  30. $this->fonts = array(
  31. dirname( __FILE__ ) . '/gentium/GenAI102.TTF',
  32. dirname( __FILE__ ) . '/gentium/GenAR102.TTF',
  33. dirname( __FILE__ ) . '/gentium/GenI102.TTF',
  34. dirname( __FILE__ ) . '/gentium/GenR102.TTF' );
  35. /* Directory temporary keeping CAPTCHA images and corresponding codes */
  36. $this->tmp_dir = dirname( __FILE__ ) . '/tmp/';
  37. /* Array of CAPTCHA image size. Width and height */
  38. $this->img_size = array( 72, 24 );
  39. /* Background color of CAPTCHA image. RGB color 0-255 */
  40. $this->bg = array( 255, 255, 255 );
  41. /* Foreground (character) color of CAPTCHA image. RGB color 0-255 */
  42. $this->fg = array( 0, 0, 0 );
  43. /* Coordinates for a text in an image. I don't know the meaning. Just adjust. */
  44. $this->base = array( 6, 18 );
  45. /* Font size */
  46. $this->font_size = 14;
  47. /* Width of a character */
  48. $this->font_char_width = 15;
  49. /* Image type. 'png', 'gif' or 'jpeg' */
  50. $this->img_type = 'png';
  51. /* Mode of temporary files */
  52. $this->file_mode = 0755;
  53. }
  54. /* Generate and return random word with $chars characters x $char_length length */
  55. function generate_random_word() {
  56. $word = '';
  57. for ( $i = 0; $i < $this->char_length; $i++ ) {
  58. $pos = mt_rand( 0, strlen( $this->chars ) - 1 );
  59. $char = $this->chars[$pos];
  60. $word .= $char;
  61. }
  62. return $word;
  63. }
  64. /* Generate CAPTCHA code and corresponding code and save them into $tmp_dir directory.
  65. $prefix is file prefix for both files.
  66. $captcha is a random word usually generated by generate_random_word()
  67. This function returns the filename of the CAPTCHA image temporary file */
  68. function generate_image( $prefix, $word ) {
  69. $dir = trailingslashit( $this->tmp_dir );
  70. $filename = null;
  71. if ( $im = imagecreatetruecolor( $this->img_size[0], $this->img_size[1] ) ) {
  72. $bg = imagecolorallocate( $im, $this->bg[0], $this->bg[1], $this->bg[2] );
  73. $fg = imagecolorallocate( $im, $this->fg[0], $this->fg[1], $this->fg[2] );
  74. imagefill( $im, 0, 0, $bg );
  75. $x = $this->base[0] + mt_rand( -2, 2 );
  76. for ( $i = 0; $i < strlen( $word ); $i++ ) {
  77. $font = $this->fonts[array_rand( $this->fonts )];
  78. imagettftext( $im, $this->font_size, mt_rand( -2, 2 ), $x,
  79. $this->base[1] + mt_rand( -2, 2 ), $fg, $font, $word[$i] );
  80. $x += $this->font_char_width;
  81. }
  82. switch ( $this->img_type ) {
  83. case 'jpeg':
  84. $filename = sanitize_file_name( $prefix . '.jpeg' );
  85. imagejpeg( $im, $dir . $filename );
  86. break;
  87. case 'gif':
  88. $filename = sanitize_file_name( $prefix . '.gif' );
  89. imagegif( $im, $dir . $filename );
  90. break;
  91. case 'png':
  92. default:
  93. $filename = sanitize_file_name( $prefix . '.png' );
  94. imagepng( $im, $dir . $filename );
  95. }
  96. imagedestroy( $im );
  97. @chmod( $dir . $filename, $this->file_mode );
  98. }
  99. $answer_file = $dir . sanitize_file_name( $prefix . '.php' );
  100. if ( $fh = fopen( $answer_file, 'w' ) ) {
  101. @chmod( $answer_file, $this->file_mode );
  102. fwrite( $fh, '<?php $captcha = "' . $word . '"; ?>' );
  103. fclose( $fh );
  104. }
  105. return $filename;
  106. }
  107. /* Check a $response against the code kept in the temporary file with $prefix
  108. Return true if the two match, otherwise return false. */
  109. function check( $prefix, $response ) {
  110. $dir = trailingslashit( $this->tmp_dir );
  111. $filename = sanitize_file_name( $prefix . '.php' );
  112. $file = $dir . $filename;
  113. if ( is_readable( $file ) ) {
  114. include( $file );
  115. if ( 0 == strcasecmp( $response, $captcha ) )
  116. return true;
  117. }
  118. return false;
  119. }
  120. /* Remove temporary files with $prefix */
  121. function remove( $prefix ) {
  122. $suffixes = array( '.jpeg', '.gif', '.png', '.php' );
  123. foreach ( $suffixes as $suffix ) {
  124. $filename = sanitize_file_name( $prefix . $suffix );
  125. $file = trailingslashit( $this->tmp_dir ) . $filename;
  126. if ( is_file( $file ) )
  127. unlink( $file );
  128. }
  129. }
  130. /* Clean up dead files older than $minutes in the tmp folder */
  131. function cleanup( $minutes = 60 ) {
  132. $dir = trailingslashit( $this->tmp_dir );
  133. if ( ! is_dir( $dir ) || ! is_readable( $dir ) || ! is_writable( $dir ) )
  134. return false;
  135. $count = 0;
  136. if ( $handle = @opendir( $dir ) ) {
  137. while ( false !== ( $filename = readdir( $handle ) ) ) {
  138. if ( ! preg_match( '/^[0-9]+\.(php|png|gif|jpeg)$/', $filename ) )
  139. continue;
  140. $file = $dir . $filename;
  141. $stat = @stat( $file );
  142. if ( ( $stat['mtime'] + $minutes * 60 ) < time() ) {
  143. @unlink( $file );
  144. $count += 1;
  145. }
  146. }
  147. closedir( $handle );
  148. }
  149. return $count;
  150. }
  151. }
  152. ?>