1
|
|
|
<?php namespace EvolutionCMS\Support; |
2
|
|
|
|
3
|
|
|
use EvolutionCMS\Interfaces\CaptchaInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* ###### |
7
|
|
|
* ## Verification Word |
8
|
|
|
* ###### |
9
|
|
|
* ## This class generate an image with random text |
10
|
|
|
* ## to be used in form verification. It has visual |
11
|
|
|
* ## elements design to confuse OCR software preventing |
12
|
|
|
* ## the use of BOTS. |
13
|
|
|
* ## |
14
|
|
|
* ####### |
15
|
|
|
* ## Author: Huda M Elmatsani |
16
|
|
|
* ## Email: justhuda at netrada.co.id |
17
|
|
|
* ## |
18
|
|
|
* ## 25/07/2004 |
19
|
|
|
* ####### |
20
|
|
|
* ## Copyright (c) 2004 Huda M Elmatsani All rights reserved. |
21
|
|
|
* ## This program is free for any purpose use. |
22
|
|
|
* ######## |
23
|
|
|
* ## |
24
|
|
|
* ## USAGE |
25
|
|
|
* ## create some image with noise texture, put in image directory, |
26
|
|
|
* ## rename to noise_#, see examples |
27
|
|
|
* ## put some true type font into font directory, |
28
|
|
|
* ## rename to font_#, see exmplae |
29
|
|
|
* ## you can search and put free font you like |
30
|
|
|
* ## |
31
|
|
|
* ## see sample.php for test and usage |
32
|
|
|
* ## sample URL: http://www.program-ruti.org/veriword/ |
33
|
|
|
* #### |
34
|
|
|
*/ |
35
|
|
|
class Captcha implements CaptchaInterface |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/* path to font directory*/ |
38
|
|
|
public $dir_font = "ttf/"; |
|
|
|
|
39
|
|
|
/* path to background image directory*/ |
40
|
|
|
public $dir_noise = "captcha/"; |
41
|
|
|
public $word = ""; |
42
|
|
|
public $im_width = 0; |
43
|
|
|
public $im_height = 0; |
44
|
|
|
public $im; |
|
|
|
|
45
|
|
|
|
46
|
|
|
public function __construct($w=200, $h=80) { |
|
|
|
|
47
|
|
|
/* create session to set word for verification */ |
48
|
|
|
$this->setVeriword(); |
49
|
|
|
$this->dir_font = MODX_BASE_PATH . 'assets/' . $this->dir_font; |
50
|
|
|
$this->im_width = $w; |
51
|
|
|
$this->im_height = $h; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setVeriword() { |
|
|
|
|
55
|
|
|
/* create session variable for verification, |
56
|
|
|
you may change the session variable name */ |
57
|
|
|
$this->word = $this->makeText(); |
58
|
|
|
$_SESSION['veriword'] = $this->word; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function output() { |
62
|
|
|
/* output the image as jpeg */ |
63
|
|
|
$this->drawImage(); |
64
|
|
|
header("Content-type: image/jpeg"); |
65
|
|
|
imagejpeg($this->im); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function makeText() { |
69
|
|
|
$modx = evolutionCMS(); |
70
|
|
|
// set default words |
71
|
|
|
$words="MODX,Access,Better,BitCode,Chunk,Cache,Desc,Design,Excell,Enjoy,URLs,TechView,Gerald,Griff,Humphrey,Holiday,Intel,Integration,Joystick,Join(),Oscope,Genetic,Light,Likeness,Marit,Maaike,Niche,Netherlands,Ordinance,Oscillo,Parser,Phusion,Query,Question,Regalia,Righteous,Snippet,Sentinel,Template,Thespian,Unity,Enterprise,Verily,Veri,Website,WideWeb,Yap,Yellow,Zebra,Zygote"; |
72
|
|
|
$words = $modx->config['captcha_words'] ? $modx->config['captcha_words'] : $words; |
73
|
|
|
$arr_words = array_filter(array_map('trim', explode(',', $words))); |
74
|
|
|
|
75
|
|
|
/* pick one randomly for text verification */ |
76
|
|
|
return (string) $arr_words[array_rand($arr_words)].rand(10,999); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function drawText() { |
80
|
|
|
$dir = dir($this->dir_font); |
81
|
|
|
$fontstmp = array(); |
82
|
|
|
while (false !== ($file = $dir->read())) { |
83
|
|
|
if(substr($file, -4) == '.ttf') { |
84
|
|
|
$fontstmp[] = $this->dir_font.$file; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
$dir->close(); |
88
|
|
|
$text_font = (string) $fontstmp[array_rand($fontstmp)]; |
89
|
|
|
|
90
|
|
|
/* angle for text inclination */ |
91
|
|
|
$text_angle = rand(-9,9); |
92
|
|
|
/* initial text size */ |
93
|
|
|
$text_size = 30; |
|
|
|
|
94
|
|
|
/* calculate text width and height */ |
95
|
|
|
$box = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word); |
96
|
|
|
$text_width = $box[2]-$box[0]; //text width |
97
|
|
|
$text_height= $box[5]-$box[3]; //text height |
|
|
|
|
98
|
|
|
|
99
|
|
|
/* adjust text size */ |
100
|
|
|
$text_size = round((20 * $this->im_width)/$text_width); |
|
|
|
|
101
|
|
|
|
102
|
|
|
/* recalculate text width and height */ |
103
|
|
|
$box = imagettfbbox ( $text_size, $text_angle, $text_font, $this->word); |
104
|
|
|
$text_width = $box[2]-$box[0]; //text width |
105
|
|
|
$text_height= $box[5]-$box[3]; //text height |
106
|
|
|
|
107
|
|
|
/* calculate center position of text */ |
108
|
|
|
$text_x = ($this->im_width - $text_width)/2; |
109
|
|
|
$text_y = ($this->im_height - $text_height)/2; |
110
|
|
|
|
111
|
|
|
/* create canvas for text drawing */ |
112
|
|
|
$im_text = imagecreate ($this->im_width, $this->im_height); |
113
|
|
|
$bg_color = imagecolorallocate ($im_text, 255, 255, 255); |
114
|
|
|
|
115
|
|
|
/* pick color for text */ |
116
|
|
|
$text_color = imagecolorallocate ($im_text, 0, 51, 153); |
|
|
|
|
117
|
|
|
|
118
|
|
|
/* draw text into canvas */ |
119
|
|
|
imagettftext ( $im_text, |
120
|
|
|
$text_size, |
121
|
|
|
$text_angle, |
122
|
|
|
$text_x, |
123
|
|
|
$text_y, |
124
|
|
|
$text_color, |
125
|
|
|
$text_font, |
126
|
|
|
$this->word); |
127
|
|
|
|
128
|
|
|
/* remove background color */ |
129
|
|
|
imagecolortransparent($im_text, $bg_color); |
130
|
|
|
return $im_text; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
public function drawImage() { |
135
|
|
|
|
136
|
|
|
/* pick one background image randomly from image directory */ |
137
|
|
|
$img_file = MODX_BASE_PATH . 'assets/' . $this->dir_noise."noise".rand(1,4).".jpg"; |
|
|
|
|
138
|
|
|
|
139
|
|
|
/* create "noise" background image from your image stock*/ |
140
|
|
|
$noise_img = @imagecreatefromjpeg ($img_file); |
141
|
|
|
$noise_width = imagesx($noise_img); |
142
|
|
|
$noise_height = imagesy($noise_img); |
143
|
|
|
|
144
|
|
|
/* resize the background image to fit the size of image output */ |
145
|
|
|
$this->im = imagecreatetruecolor($this->im_width,$this->im_height); |
|
|
|
|
146
|
|
|
imagecopyresampled ($this->im, |
147
|
|
|
$noise_img, |
148
|
|
|
0, 0, 0, 0, |
149
|
|
|
$this->im_width, |
150
|
|
|
$this->im_height, |
151
|
|
|
$noise_width, |
152
|
|
|
$noise_height); |
153
|
|
|
|
154
|
|
|
/* put text image into background image */ |
155
|
|
|
imagecopymerge ( $this->im, |
156
|
|
|
$this->drawText(), |
157
|
|
|
0, 0, 0, 0, |
158
|
|
|
$this->im_width, |
159
|
|
|
$this->im_height, |
160
|
|
|
70 ); |
161
|
|
|
|
162
|
|
|
return $this->im; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function destroy() { |
166
|
|
|
imagedestroy($this->im); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.