Project

General

Profile

1
<?php
2

    
3
// $Id: calc_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
if ( !defined( 'WB_PATH' ) ){ require( dirname(dirname(dirname((__DIR__)))).'/config.php' ); }
26
require_once(WB_PATH.'/include/captcha/captcha.php');
27

    
28
if(!isset($_SESSION['captcha_time']))
29
    exit;
30
//unset($_SESSION['captcha_time']);        // otherwise there can't be 2 captchas on one page!
31

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

    
41
// Captcha
42
$sec_id = '';
43
if(isset($_GET['s'])) $sec_id = $_GET['s'];
44
$_SESSION['captcha'.$sec_id] = '';
45
mt_srand((double)microtime()*1000000);
46
$n = mt_rand(1,3);
47
switch ($n) {
48
    case 1:
49
        $x = mt_rand(1,9);
50
        $y = mt_rand(1,9);
51
        $_SESSION['captcha'.$sec_id] = $x + $y;
52
        $cap = "$x+$y"; 
53
        break; 
54
    case 2:
55
        $x = mt_rand(10,20);
56
        $y = mt_rand(1,9);
57
        $_SESSION['captcha'.$sec_id] = $x - $y; 
58
        $cap = "$x-$y"; 
59
        break;
60
    case 3:
61
        $x = mt_rand(2,10);
62
        $y = mt_rand(2,5);
63
        $_SESSION['captcha'.$sec_id] = $x * $y; 
64
        $cap = "$x*$y"; 
65
        break;
66
}
67
$text = $cap;
68

    
69
// choose a font and background
70
$font = $fonts[array_rand($fonts)];
71
$bg = $bgs[array_rand($bgs)];
72
// get image-dimensions
73
list($width, $height, $type, $attr) = getimagesize($bg);
74

    
75
// create reload-image
76
$reload = ImageCreateFromPNG(WB_PATH.'/include/captcha/reload_140_40.png'); // reload-overlay
77

    
78
if(mt_rand(0,2)==0) { // 1 out of 3
79

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

    
136
imagealphablending($reload, TRUE);
137
imagesavealpha($reload, TRUE);
138

    
139
// overlay
140
imagecopy($reload, $image, 0,0,0,0, 140,40);
141
imagedestroy($image);
142
$image = $reload;
143

    
144
captcha_header();
145
ob_start();
146
imagepng($image);
147
header("Content-Length: ".ob_get_length()); 
148
ob_end_flush();
149
imagedestroy($image);
(5-5/13)