captcha.php
105 lines
| 1 | <?php |
| 2 | /* Really Simple Captcha */ |
| 3 | |
| 4 | /* Copyright 2007 Takayuki Miyoshi (email: takayukister at gmail.com) |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | class tam_captcha { |
| 22 | |
| 23 | function tam_captcha() { |
| 24 | $this->chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; |
| 25 | $this->char_length = 4; |
| 26 | $this->fonts = array( |
| 27 | dirname(__FILE__) . '/gentium/GenAI102.TTF', |
| 28 | dirname(__FILE__) . '/gentium/GenAR102.TTF', |
| 29 | dirname(__FILE__) . '/gentium/GenI102.TTF', |
| 30 | dirname(__FILE__) . '/gentium/GenR102.TTF'); |
| 31 | $this->tmp_dir = dirname(__FILE__) . '/tmp/'; |
| 32 | $this->img_size = array(72, 24); |
| 33 | $this->bg = array(255, 255, 255); |
| 34 | $this->fg = array(0, 0, 0); |
| 35 | $this->base = array(6, 18); |
| 36 | $this->font_size = 14; |
| 37 | $this->font_char_width = 15; |
| 38 | $this->img_type = 'png'; |
| 39 | } |
| 40 | |
| 41 | function generate_random_word() { |
| 42 | $word = ''; |
| 43 | for ($i = 0; $i < $this->char_length; $i++) { |
| 44 | $pos = mt_rand(0, strlen($this->chars) - 1); |
| 45 | $char = $this->chars[$pos]; |
| 46 | $word .= $char; |
| 47 | } |
| 48 | return $word; |
| 49 | } |
| 50 | |
| 51 | function generate_image($prefix, $captcha) { |
| 52 | $filename = null; |
| 53 | if ($im = imagecreatetruecolor($this->img_size[0], $this->img_size[1])) { |
| 54 | $bg = imagecolorallocate($im, $this->bg[0], $this->bg[1], $this->bg[2]); |
| 55 | $fg = imagecolorallocate($im, $this->fg[0], $this->fg[1], $this->fg[2]); |
| 56 | imagefill($im, 0, 0, $bg); |
| 57 | $x = $this->base[0] + mt_rand(-2, 2); |
| 58 | for ($i = 0; $i < strlen($captcha); $i++) { |
| 59 | $font = $this->fonts[array_rand($this->fonts)]; |
| 60 | imagettftext($im, $this->font_size, mt_rand(-2, 2), $x, $this->base[1] + mt_rand(-2, 2), $fg, $font, $captcha[$i]); |
| 61 | $x += $this->font_char_width; |
| 62 | } |
| 63 | switch ($this->img_type) { |
| 64 | case 'jpeg': |
| 65 | $filename = $prefix . '.jpeg'; |
| 66 | imagejpeg($im, $this->tmp_dir . $filename); |
| 67 | break; |
| 68 | case 'gif': |
| 69 | $filename = $prefix . '.gif'; |
| 70 | imagegif($im, $this->tmp_dir . $filename); |
| 71 | break; |
| 72 | case 'png': |
| 73 | default: |
| 74 | $filename = $prefix . '.png'; |
| 75 | imagepng($im, $this->tmp_dir . $filename); |
| 76 | } |
| 77 | imagedestroy($im); |
| 78 | } |
| 79 | if ($fh = fopen($this->tmp_dir . $prefix . '.php', 'w')) { |
| 80 | fwrite($fh, '<?php $captcha = "' . $captcha . '"; ?>'); |
| 81 | fclose($fh); |
| 82 | } |
| 83 | return $filename; |
| 84 | } |
| 85 | |
| 86 | function check($prefix, $response) { |
| 87 | if (is_readable($this->tmp_dir . $prefix . '.php')) { |
| 88 | include($this->tmp_dir . $prefix . '.php'); |
| 89 | if (0 == strcasecmp($response, $captcha)) |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | function remove($prefix) { |
| 96 | $suffixes = array('.jpeg', '.gif', '.png', '.php'); |
| 97 | foreach ($suffixes as $suffix) { |
| 98 | $file = $this->tmp_dir . $prefix . $suffix; |
| 99 | if (is_file($file)) |
| 100 | unlink($file); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ?> |