|
1
|
|
|
<?php |
|
2
|
|
|
namespace raphiz\passwordcards; |
|
3
|
|
|
|
|
4
|
|
|
class RequestUtils |
|
5
|
|
|
{ |
|
6
|
|
|
public static function isPost() |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
return $_SERVER['REQUEST_METHOD'] == "POST"; |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
public static function preventSpam() |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
|
14
|
|
|
$blacklistfile = __DIR__ . '/../blacklist/' . $ip; |
|
15
|
|
|
$count = 0; |
|
16
|
|
|
$creationDate = 0; |
|
17
|
|
|
if (file_exists($blacklistfile)) { |
|
18
|
|
|
$contents = (int)file_get_contents($blacklistfile); |
|
19
|
|
|
// If the stored value is big, it's the unix timestamp. |
|
20
|
|
|
// Otherwise it's the amount of created cards. |
|
21
|
|
|
if ($contents > 5) { |
|
22
|
|
|
$creationDate = $contents; |
|
23
|
|
|
} else { |
|
24
|
|
|
$count = $contents; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
if ($creationDate > 0) { |
|
30
|
|
|
// If blocked time is over, release lock |
|
31
|
|
|
if ($creationDate - time() < 0) { |
|
32
|
|
|
file_put_contents($blacklistfile, 0); |
|
33
|
|
|
} else { |
|
34
|
|
|
return $creationDate - time(); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($count === 5) { |
|
39
|
|
|
// Write unix timestamp into the blacklist file. The |
|
40
|
|
|
// ip is blocked till then. |
|
41
|
|
|
file_put_contents($blacklistfile, time() + 5*60); |
|
42
|
|
|
} else { |
|
43
|
|
|
// increment count... |
|
44
|
|
|
file_put_contents($blacklistfile, ($count+1)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function parseSeed() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
if ( |
|
53
|
|
|
isset($_POST['seed']) && |
|
54
|
|
|
is_numeric($_POST['seed']) |
|
55
|
|
|
) { |
|
56
|
|
|
return $_POST['seed']; |
|
57
|
|
|
} |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function parseSpacebarSize() |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
if ( |
|
64
|
|
|
isset($_POST['space-length']) && |
|
65
|
|
|
is_numeric($_POST['space-length']) && |
|
66
|
|
|
$_POST['space-length'] < 8 && |
|
67
|
|
|
$_POST['space-length'] > 0 |
|
68
|
|
|
) { |
|
69
|
|
|
return $_POST['space-length']; |
|
70
|
|
|
} |
|
71
|
|
|
return 8; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function parseText() |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
if (isset($_POST['msg'])) { |
|
77
|
|
|
return substr($_POST['msg'], 0, 20); |
|
78
|
|
|
} |
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function parsePrimaryColor() |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
if ( |
|
85
|
|
|
isset($_POST['primaryColor']) && |
|
86
|
|
|
preg_match("/#[0-9a-zA-Z]{6}/", $_POST['primaryColor']) |
|
87
|
|
|
) { |
|
88
|
|
|
return $_POST['primaryColor']; |
|
89
|
|
|
} |
|
90
|
|
|
return '#1ABC9C'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function parseSecondaryColor() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
if ( |
|
96
|
|
|
isset($_POST['secondaryColor']) && |
|
97
|
|
|
preg_match("/#[0-9a-zA-Z]{6}/", $_POST['secondaryColor']) |
|
98
|
|
|
) { |
|
99
|
|
|
return $_POST['secondaryColor']; |
|
100
|
|
|
} |
|
101
|
|
|
return '#ffffff'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function parseKeyboardLayout() |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
if ( |
|
107
|
|
|
isset($_POST['keyboardlayout']) && |
|
108
|
|
|
preg_match("/qwerty|qwertz/", $_POST['keyboardlayout']) |
|
109
|
|
|
) { |
|
110
|
|
|
return strtolower($_POST['keyboardlayout']); |
|
111
|
|
|
} |
|
112
|
|
|
return 'qwerty'; |
|
113
|
|
|
} |
|
114
|
|
|
public static function parsePattern() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$pattern = ""; |
|
117
|
|
|
|
|
118
|
|
|
// With numbers? |
|
119
|
|
|
if (self::isChecked('with-numbers')) { |
|
120
|
|
|
$pattern .= '0-9'; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// With lower? |
|
124
|
|
|
if (self::isChecked('with-lower')) { |
|
125
|
|
|
$pattern .= 'a-z'; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// With upper? |
|
129
|
|
|
if (self::isChecked('with-upper')) { |
|
130
|
|
|
$pattern .= 'A-Z'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// With symbols? |
|
134
|
|
|
if (self::isChecked('with-symbols')) { |
|
135
|
|
|
$pattern .= '*-*'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// With space? |
|
139
|
|
|
if (self::isChecked('with-space')) { |
|
140
|
|
|
$pattern .= ' '; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// With others? |
|
144
|
|
|
if (self::isChecked('with-other')) { |
|
145
|
|
|
if (isset($_POST['other-chars'])) { |
|
146
|
|
|
$pattern .= substr($_POST['other-chars'], 0, 20); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
return $pattern; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private static function isChecked($parameter) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
if ( |
|
155
|
|
|
isset($_POST[$parameter]) && |
|
156
|
|
|
$_POST[$parameter] === "on" |
|
157
|
|
|
) { |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public static function preparePdfHeader($length) |
|
164
|
|
|
{ |
|
165
|
|
|
header('Content-Description: File Transfer'); |
|
166
|
|
|
header('Content-Type: application/pdf'); |
|
167
|
|
|
header('Content-Disposition: attachment; filename=passwordcard.pdf'); |
|
168
|
|
|
header('Content-Transfer-Encoding: binary'); |
|
169
|
|
|
header('Content-Length: ' . $length); |
|
170
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
171
|
|
|
header('Expires: 0'); |
|
172
|
|
|
header('Pragma: public'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: