forked from stefangabos/Zebra_Form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
248 lines (173 loc) · 7.68 KB
/
process.php
File metadata and controls
248 lines (173 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
// if we have to generate a CAPTCHA image
if (isset($_GET['captcha']) && ($_GET['captcha'] == 1 || $_GET['captcha'] == 2)) {
// storage method
$storage = ($_GET['captcha'] == 2 ? 'session' : 'cookie');
// if storage method is "session", start a session
if ($storage == 'session') session_start();
// as this file actually generates an image we set the headers accordingly
header('Content-type:image/jpeg');
$font_path = rtrim(implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, __FILE__), 0, -2)), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . basename(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'includes/';
// the number of characters to be used in the generated image
$charactersNumber = 5;
// spacing between characters (this can also be a negative number)
// you should leave this as it is (unless you want to increase it)
// decreasing this value may result in characters overlapping and being hardly distinguishable
$characterSpacing = -2;
// each character's size will be randomly selected from this range
$fontSizeVariation = array(20, 40);
// each character's angle will be randomly selected from this range
// (remember to also change the character spacing if you change these to avoid character overlapping)
$fontAngleVariation = array(-10, 10);
// if you changed anything above, you should probably change this too si that all the characters fit into the image
// and there's not too much of blank space
$imageWidth = 140;
// if you changed anything above, you should probably change this too si that all the characters fit into the image
// and there's not too much of blank space
$imageHeight = 50;
// the quality, in percents, of the generated image
$imageQuality = 70;
// list of characters from which to choose
// (notice that characters that can be (in some circumstances) confused with others, are missing)
// you should not alter this setting
$charList = 'bcdkmpsx345678';
$captcha = array();
$resultString = '';
$totalWidth = 0;
// this is the used font
$font = $font_path . 'babelsans-bold.ttf';
// first we figure out how much space the character would take
for ($i = 0; $i < $charactersNumber; $i++) {
// get a random character
$char = $charList[rand(0, strlen($charList) - 1)];
$resultString .= $char;
// get a random size for the character
$charSize = rand($fontSizeVariation[0], $fontSizeVariation[1]);
// get a random angle for the character
$charAngle = rand($fontAngleVariation[0], $fontAngleVariation[1]);
// get the bounding box of the character
$bbox = imagettfbbox($charSize, $charAngle, $font, $char);
// resolve the returned measurements
$bbox['left'] = abs(min($bbox[0], $bbox[2], $bbox[4], $bbox[6]));
$bbox['top'] = abs(min($bbox[1], $bbox[3], $bbox[5], $bbox[7]));
$bbox['width'] = max($bbox[0], $bbox[2], $bbox[4], $bbox[6]) - min($bbox[0], $bbox[2], $bbox[4], $bbox[6]);
$bbox['height'] = max($bbox[1], $bbox[3], $bbox[5], $bbox[7]) - min($bbox[1], $bbox[3], $bbox[5], $bbox[7]);
// this will be the total width of the random generated word
$totalWidth += $bbox['width'] + $characterSpacing;
// save info about the current character
$captcha[] = array(
'char' => $char,
'size' => $charSize,
'angle' => $charAngle,
'box' => $bbox
);
}
// encode value
$value = md5(md5(md5($resultString)));
// if storage is "session", store the value in session
if ($storage == 'session') $_SESSION['captcha'] = $value;
// otherwise, store the value in a cookie
else setcookie('captcha', $value, time() + 3600, '/');
// either ways, the value will later be read by the form generator
// and used to see if the user entered the correct characters
// create the image
$img = imagecreatetruecolor($imageWidth, $imageHeight);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
// fill the canvas to white
imagefilledrectangle($img, 0, 0, $imageWidth, $imageHeight, $white);
// write some random characters in the background
for ($i = 0; $i <10; $i++) {
// ...having random washed-out colors
$color = imagecolorallocate($img, rand(150, 200), rand(150, 200), rand(150, 200));
imagettftext(
$img,
20,
rand($fontAngleVariation[0],
$fontAngleVariation[1]),
rand(0, $imageWidth),
rand(20, $imageHeight) ,
$color,
$font,
chr(rand(65, 90))
);
}
// draw a bounding rectangle
// imagerectangle($img, 0, 0, $imageWidth - 1, $imageHeight - 1, $black);
// this is to keep the word centered in the box
$left = (($imageWidth - $totalWidth) / 2);
// iterate through the chosen characters
foreach ($captcha as $values) {
// print each character
imagettftext(
$img,
$values['size'],
$values['angle'],
$left ,
($imageHeight + $values['box']['height']) / 2 ,
$black,
$font,
$values['char']
);
// compute the position of the next character
$left += $values['box']['width'] + $characterSpacing;
}
// and finally output the image at the specified quality
imagejpeg($img, null, $imageQuality);
// free memory
imagedestroy($img);
// if we're performing a file upload
} elseif (
isset($_FILES) &&
is_array($_FILES) &&
!empty($_FILES) &&
isset($_GET['form']) &&
isset($_GET['control']) &&
isset($_FILES[$_GET['control']])
) {
function process() {
// the form that initiated the request
$form = $_GET['form'];
// the file upload control on the form that initiated the request
$control = $_GET['control'];
// if file could be uploaded
if (isset($_FILES[$_GET['control']])) {
// save some information about the uploaded file
$file['name'] = $_FILES[$control]['name'];
$file['type'] = $_FILES[$control]['type'];
$file['error'] = $_FILES[$control]['error'];
$file['size'] = $_FILES[$control]['size'];
// if there were problems uploading the file
} elseif (empty($_POST) && empty($_FILES) && isset($_SERVER['CONTENT_LENGTH']) && isset($_SERVER['CONTENT_TYPE']) && (strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false || strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false)) {
// send these values
$file['name'] = '';
$file['type'] = 'unknown';
$file['error'] = 1;
$file['size'] = $_SERVER['CONTENT_LENGTH'];
}
ob_start();
?>
<script type="text/javascript">
var f=parent.window.$('#<?php echo $form?>');
if(undefined!=f){
f.data('Zebra_Form').end_file_upload('<?php echo $control . '\'' . (isset($file) ? ',[\'' . addcslashes($file['name'], '\'') . '\',\'' . $file['type'] . '\',\'' . $file['error'] . '\',\'' . $file['size'] . '\']' : '')?>)
}
</script>
<?php
$contents = ob_get_contents();
ob_end_clean();
$patterns = array(
'/^\s*/m',
'/\r/m',
'/\n/m',
'/\r\n/m',
);
$replacements = array(
'',
);
echo preg_replace($patterns, $replacements, $contents);
}
register_shutdown_function('process');
}
?>