-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathGuard.php
More file actions
229 lines (195 loc) · 5.62 KB
/
Copy pathMathGuard.php
File metadata and controls
229 lines (195 loc) · 5.62 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
<?php
namespace ZexBre\MathGuard;
/**
* @author Matej Koval
* http://www.codegravity.com/projects/mathguard
*
* MathGuard is a PHP class that inserts a small piece of HTML code into your HTML form which requires the user to evaluate an expression consisting of two random numbers. When user submits the form with the answer, the answer is hashed and compared to the security code that has been submitted as well. This way you can easily protect your forms from spambots.
*/
/** SETTINGS - you can easily modify these constants **/
DEFINE('MATHGUARD_COLOR', "red");
DEFINE('MATHGUARD_SIZE', 5);
DEFINE('MATHGUARD_LINEHEIGHT', 6);
DEFINE('MATHGUARD_QUESTION', "security question, please solve:");
DEFINE('MATHGUARD_FIELD_ANSWER',md5("codegravity_answer".(date("d")*date("W")*date("W")))); // change this immediatelly to something else. eg. myanswer123
DEFINE('MATHGUARD_FIELD_CODE',md5("codegravity_code".(date("d")*date("W")))); // change this immediatelly to something else. eg. mysecretcodefield
/** SETTINGS - you can easily modify these constants **/
class MathGuard {
/** you can also modify $output to your needs **/
private static function produceOutput($prime) {
$a = rand() % 10; // generates the random number
$b = rand() % 10; // generates the random number
$code = MathGuard :: generateCode($a, $b, $prime);
// please don't remove the backlink, thank you
$output = MATHGUARD_QUESTION .
"<table border='0'><tr><td></td></tr><tr><td style='font-family: monospace; font-size:" . MATHGUARD_SIZE . "px; font-weight: bold; color: " . MATHGUARD_COLOR . "; padding:0px; margin: 0px; line-height:" . MATHGUARD_LINEHEIGHT . "px;'>\n\n" . MathGuard :: renderExpression($a, $b) . "</td>
<td><input type='input' name='".MATHGUARD_FIELD_ANSWER."' size='2' maxlength='2'/><input type='hidden' name='".MATHGUARD_FIELD_CODE."' value='$code' /></td>
</tr>
</table>
";
return $output;
}
/** function that converts the decimal number to line of 3 random characters
* @param $dec decimal number that is being converted to line of 3 random characters
* */
private static function decToBin($dec) {
$pattern = "123456789ABCDEFGHIJKLMNOPQRTSTUWXYZ"; //without zero, it was interpreted as an empty space
$output = " ";
$i = 0;
do {
if ($dec % 2) {
$rand = rand() % 34;
$output {
2 - $i }
= $pattern {
$rand };
} else {
$output {
2 - $i }
= " ";
}
$dec = (int) ($dec / 2);
$i++;
} while ($dec > 0);
$output = str_replace(" ", " ", $output);
return $output;
}
/** function that renders a final 3x5 matrix consisting of random characters
* @param $a random number a that renders to the 3x5 matrix consisting of random characters
* @param $b random number b that renders to the 3x5 matrix consisting of random characters
* */
private static function renderExpression($a, $b) {
$number = array (
array (
7,
5,
5,
5,
7
),
array (
2,
6,
2,
2,
7
),
array (
7,
1,
7,
4,
7
),
array (
7,
1,
7,
1,
7
),
array (
4,
5,
7,
1,
1
),
array (
7,
4,
7,
1,
7
),
array (
7,
4,
7,
5,
7
),
array (
7,
1,
1,
1,
1
),
array (
7,
5,
7,
5,
7
),
array (
7,
5,
7,
1,
7
)
);
$plus = array (
0,
2,
7,
2,
0
);
$eq = array (
0,
7,
0,
7,
0
);
$output = "\n\n";
for ($line = 0; $line < 5; $line++) {
$output .= MathGuard :: decToBin($number[$a][$line]) . " " . MathGuard :: decToBin($plus[$line]) . " " . MathGuard :: decToBin($number[$b][$line]) . " " . MathGuard :: decToBin($eq[$line]) . "\n";
$output = str_replace("0", " ", $output);
}
return nl2br($output);
}
/** A main hashing function: concat of user's answer, hour and the additional prime number (default 37) */
private static function encode($input, $prime) {
return md5($input . date("H-d") . $prime);
}
/** This function generates the hash code from the two numbers
* @param $a first number
* @param $b second sumber
* @param $prime additional number to encode with
* */
private static function generateCode($a, $b, $prime) {
$code = MathGuard :: encode($a + $b, $prime);
return $code;
}
/** This function checks whether the answer and generated security code match
* @param $mathguard_answer answer the user has entered
* @param $mathguard_code hashcode the mathguard has generated
*/
public static function checkResult($mathguard_answer, $mathguard_code, $prime = 37) {
$mathguard_code = @$_POST[MATHGUARD_FIELD_CODE];
$mathguard_answer = @$_POST[MATHGUARD_FIELD_ANSWER];
// echo("prime; $prime, $mathguard_answer");
$result_encoded = MathGuard :: encode($mathguard_answer, $prime);
if ($result_encoded == $mathguard_code)
return true;
else
return false;
}
/** this function inserts the two math term into your form, the parameter is optional */
public static function insertQuestion($prime = 37) { //default prime is 37, you can change it when specifying the different parameter
$output = MathGuard :: produceOutput($prime);
echo $output;
}
/** this function returns math expression into your form, the parameter is optional
* quite simmilar to insertQuestion, but returns the output as a text instead of echoing
*/
public static function returnQuestion($prime = 37) { //default prime is 37, you can change it when specifying the different parameter
$output = MathGuard :: produceOutput($prime);
return $output;
}
}
?>