Source of trunk/imageverify.php at revision 395 (03/26/2009 4:03:03, 4495 bytes, 155 lines, language: php) [download]:
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | ** Image Verification |
| 5 | ** for CodewiseBlog |
| 6 | ** |
| 7 | |
| 8 | ** Copyright (c) 2006-2008 Codewise.org |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | ** This file is part of CodewiseBlog |
| 13 | ** |
| 14 | ** CodewiseBlog is free software; you can redistribute it and/or modify |
| 15 | ** it under the terms of the GNU General Public License as published by |
| 16 | ** the Free Software Foundation; either version 2 of the License, or |
| 17 | ** (at your option) any later version. |
| 18 | ** |
| 19 | ** CodewiseBlog is distributed in the hope that it will be useful, |
| 20 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | ** GNU General Public License for more details. |
| 23 | ** |
| 24 | ** You should have received a copy of the GNU General Public License |
| 25 | ** along with CodewiseBlog; if not, write to the Free Software |
| 26 | ** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | ** This file is require()d by cwbmulti.php and requested directly. |
| 31 | ** Only generate the image when requested directly. |
| 32 | */ |
| 33 | if(basename($_SERVER['SCRIPT_NAME']) == "imageverify.php") |
| 34 | { |
| 35 | |
| 36 | header("Content-Type: image/jpeg"); |
| 37 | |
| 38 | require("settings.php"); |
| 39 | require("l1_mysql.php"); |
| 40 | |
| 41 | if(isset($_GET['id'])) |
| 42 | { |
| 43 | $db = new L1_MySQL(SQL_HOST, SQL_USER, SQL_PASS, SQL_DB); |
| 44 | |
| 45 | $q = $db->issue_query("SELECT text FROM imageverify WHERE id = ".($prepared_id = $db->prepare_value($_GET['id']))); |
| 46 | |
| 47 | if($db->num_rows[$q] == 0) |
| 48 | error_image("Bogus IVID: {$_GET['id']}"); |
| 49 | |
| 50 | $text = $db->fetch_var($q); |
| 51 | } else { |
| 52 | /* if no IVID was given, generate some random letters and display them |
| 53 | * anyways. It's good for debugging. */ |
| 54 | $text = genivtext(); |
| 55 | } |
| 56 | |
| 57 | // 150 x 50 white image |
| 58 | $image = imagecreatetruecolor(150, 50); |
| 59 | $white = imagecolorallocate($image, 0xff,0xff,0xff); |
| 60 | imagefilledrectangle($image, 0, 0, 150, 50, $white); |
| 61 | |
| 62 | $fonts = glob(FSPATH . "/fonts/*.pfa"); |
| 63 | |
| 64 | // this is the distance from the left of the image to draw the first letter |
| 65 | $x = 5; |
| 66 | |
| 67 | for($i = 0; $i < strlen($text); $i++) |
| 68 | { |
| 69 | $letter = $text[$i]; |
| 70 | |
| 71 | $fontname = $fonts[ mt_rand(0, count($fonts) - 1) ]; |
| 72 | |
| 73 | $font = imagepsloadfont( $fontname ); |
| 74 | if($font === FALSE) |
| 75 | error_image("Failed to load font: $fontname"); |
| 76 | |
| 77 | // random colors, hopefully visible ;) |
| 78 | $color = imagecolorallocate($image, mt_rand(0, 192), mt_rand(0, 192), mt_rand(0, 192)); |
| 79 | |
| 80 | // slant letter between +/- .50 |
| 81 | imagepsslantfont($font, (mt_rand(-50, 50) * .01)); |
| 82 | |
| 83 | $pos = imagepstext($image, $letter, $font, 40, $color, $white, $x, 40); |
| 84 | |
| 85 | // draw the next letter 3px right of where the last letter ended |
| 86 | $x += $pos[2] + 3; |
| 87 | |
| 88 | imagepsfreefont($font); |
| 89 | } |
| 90 | |
| 91 | imagejpeg($image); |
| 92 | } |
| 93 | |
| 94 | // display some useful error message |
| 95 | function error_image($text) |
| 96 | { |
| 97 | $font = 5; |
| 98 | |
| 99 | // length and width based on text and font size |
| 100 | $x = (strlen($text) + 7) * imagefontwidth($font); |
| 101 | $y = (imagefontheight($font) + 2); |
| 102 | |
| 103 | $image = imagecreatetruecolor($x, $y); |
| 104 | $white = imagecolorallocate($image, 0xff,0xff,0xff); |
| 105 | $black = imagecolorallocate($image, 0,0,0); |
| 106 | imagefilledrectangle($image, 0,0, $x,$y, $white); |
| 107 | |
| 108 | imagestring($image, $font, 0,0, "Error: $text", $black); |
| 109 | |
| 110 | imagejpeg($image); |
| 111 | exit; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | ** generate 4 random letters suitable for display |
| 116 | ** this should not be used except by this script. |
| 117 | */ |
| 118 | function genivtext() |
| 119 | { |
| 120 | // exclude i,I,l,L and O,D; they're too similar |
| 121 | $letters = array("A","a","B","b","C","c","d","E","e","F","f","G","g","H", |
| 122 | "h","J","j","K","k","M","m","N","n","P","p","Q","q","R","r","S","s","T", |
| 123 | "t","U","u","V","v","W","w","X","x","Y","y","Z","z"); |
| 124 | |
| 125 | $ivtext = ""; |
| 126 | for($i = 0; $i < 4; $i++) |
| 127 | $ivtext .= $letters[ mt_rand(0, count($letters) - 1) ]; |
| 128 | |
| 129 | return $ivtext; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | ** Use this to generate letters and hash to feed to the image generator. |
| 134 | ** The return value should be the ?id= argument to imageverify.php |
| 135 | */ |
| 136 | function genivid() |
| 137 | { |
| 138 | global $db; |
| 139 | |
| 140 | $ivtext = genivtext(); |
| 141 | $timestamp = time(); |
| 142 | |
| 143 | $db->insert("imageverify", array("text" => $ivtext, "timestamp" => $timestamp)); |
| 144 | |
| 145 | $q = $db->issue_query("SELECT id FROM imageverify WHERE text = '$ivtext' AND timestamp = $timestamp"); |
| 146 | $id = $db->fetch_var($q); |
| 147 | |
| 148 | // delete records more than 1 day old. |
| 149 | $db->issue_query("DELETE FROM imageverify WHERE timestamp < " . (time() - 60*60*24*1)); |
| 150 | |
| 151 | return $id; |
| 152 | } |
| 153 | |
| 154 | ?> |
| 155 |