Project

General

Profile

1
<?php
2

    
3
// $Id: ttf_image.php 2 2017-07-02 15:14:29Z Manuela $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker 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

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
require_once("../../../config.php");
27
require_once(WB_PATH.'/include/captcha/captcha.php');
28

    
29
if(!isset($_SESSION['captcha_time']))
30
    exit;
31
//unset($_SESSION['captcha_time']);
32

    
33
// get lists of fonts and backgrounds
34
require_once(WB_PATH.'/framework/functions.php');
35
$t_fonts = file_list(WB_PATH.'/include/captcha/fonts');
36
$t_bgs = file_list(WB_PATH.'/include/captcha/backgrounds');
37
$fonts = array();
38
$bgs = array();
39
foreach($t_fonts as $file) { if(preg_match('/\.ttf/',$file)) { $fonts[]=$file; } }
40
foreach($t_bgs as $file) { if(preg_match('/\.png/',$file)) { $bgs[]=$file; } }
41

    
42
// make random string
43
if(!function_exists('randomString')) {
44
    function randomString($len) {
45
        list($usec, $sec) = explode(' ', microtime());
46
        mt_srand((float)$sec + ((float)$usec * 100000));
47
        //$possible="ABCDEFGHJKLMNPRSTUVWXYZabcdefghkmnpqrstuvwxyz23456789";
48
        $possible="abdfhkrsvwxz23456789";
49
        $str="";
50
        while(strlen($str)<$len) {
51
            $str.=substr($possible,(mt_rand()%(strlen($possible))),1);
52
        }
53
        return($str);
54
    }
55
}
56
$text = randomString(5); // number of characters
57

    
58
$sec_id = '';
59
if(isset($_GET['s'])) $sec_id = $_GET['s'];
60
$_SESSION['captcha'.$sec_id] = $text;
61

    
62
// choose a font and background
63
$font = $fonts[array_rand($fonts)];
64
$bg = $bgs[array_rand($bgs)];
65
// get image-dimensions
66
list($width, $height, $type, $attr) = getimagesize($bg);
67

    
68
// create reload-image
69
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay
70

    
71
if(mt_rand(0,2)==0) { // 1 out of 3
72

    
73
    // draw each character individualy
74
    $image = ImageCreateFromPNG($bg); // background image
75
    $grey = mt_rand(0,50);
76
    $color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
77
    $ttf = $font;
78
    $ttfsize = 25; // fontsize
79
    $count = 0;
80
    $image_failed = true;
81
    $angle = mt_rand(-15,15);
82
    $x = mt_rand(10,25);
83
    $y = mt_rand($height-10,$height-2);
84
    do {
85
        for($i=0;$i<strlen($text);$i++) {
86
            $res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text{$i});
87
            $angle = mt_rand(-15,15);
88
            $x = mt_rand($res[4],$res[4]+10);
89
            $y = mt_rand($height-15,$height-5);
90
        }
91
        if($res[4] > $width) {
92
            $image_failed = true;
93
        } else {
94
            $image_failed = false;
95
        }
96
        if(++$count > 4) // too many tries! Use the image
97
            break;
98
    } while($image_failed);
99
    
100
} else {
101
    
102
    // draw whole string at once
103
    $image_failed = true;
104
    $count=0;
105
    do {
106
        $image = ImageCreateFromPNG($bg); // background image
107
        $grey = mt_rand(0,50);
108
        $color = ImageColorAllocate($image, $grey, $grey, $grey); // font-color
109
        $ttf = $font;
110
        $ttfsize = 25; // fontsize
111
        $angle = mt_rand(0,5);
112
        $x = mt_rand(5,30);
113
        $y = mt_rand($height-10,$height-2);
114
        $res = imagettftext($image, $ttfsize, $angle, $x, $y, $color, $ttf, $text);
115
        // check if text fits into the image
116
        if(($res[0]>0 && $res[0]<$width) && ($res[1]>0 && $res[1]<$height) && 
117
             ($res[2]>0 && $res[2]<$width) && ($res[3]>0 && $res[3]<$height) && 
118
             ($res[4]>0 && $res[4]<$width) && ($res[5]>0 && $res[5]<$height) && 
119
             ($res[6]>0 && $res[6]<$width) && ($res[7]>0 && $res[7]<$height)
120
        ) {
121
            $image_failed = false;
122
        }
123
        if(++$count > 4) // too many tries! Use the image
124
            break;
125
    } while($image_failed);
126
    
127
}
128

    
129
imagealphablending($reload, TRUE);
130
imagesavealpha($reload, TRUE);
131

    
132
// overlay
133
imagecopy($reload, $image, 0,0,0,0, 140,40);
134
imagedestroy($image);
135
$image = $reload;
136

    
137
captcha_header();
138
ob_start();
139
imagepng($image);
140
header("Content-Length: ".ob_get_length()); 
141
ob_end_flush();
142
imagedestroy($image);
(12-12/13)