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