-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralUtility.php
258 lines (223 loc) · 9.34 KB
/
GeneralUtility.php
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
249
250
251
252
253
254
255
256
257
258
<?php
namespace App\Utility;
use App\Container\AppContainer;
use App\Controller\BaseController;
use App\Entity\User;
class GeneralUtility {
/**
* @param string $pass
* @return string|false
*/
static function encryptPassword($pass) {
$options = [
'cost' => 11
];
return password_hash($pass, PASSWORD_BCRYPT, $options);
}
/**
* Generates random codes
*
* @param integer $length
* @return string
*/
static function generateCode($length = 18) {
$chars = 'abcdefghijkmnopqrstuvwxyz023456789';
srand((double)microtime()*1000000);
$i = 0;
$code = '' ;
while ($i <= $length) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$code = $code . $tmp;
$i++;
}
return $code;
}
/**
* Returns current user id or null if user not logged in.
*
* @return integer|null
*/
static function getCurrentUser() {
return isset($_SESSION['currentUser']) ? $_SESSION['currentUser'] : null;
}
/**
* Set the current user
*
* @param integer $currentUser
*/
static function setCurrentUser($currentUser) {
$_SESSION['currentUser'] = $currentUser;
}
/**
* Returns current role or 'guest' if user not logged in.
*
* @return string
*/
static function getCurrentRole() {
return isset($_SESSION['currentRole']) ? $_SESSION['currentRole'] : 'guest';
}
/**
* Set the current role
*
* @param string $currentRole
*/
static function setCurrentRole($currentRole) {
$_SESSION['currentRole'] = $currentRole;
}
/**
* Returns flash message array.
*
* @return array
*/
static function getFlashMessages() {
$flash = AppContainer::getInstance()->getContainer()->get('flash');
$flashMessages = $flash->getMessage('message');
$messages = [];
if (is_array($flashMessages)) {
foreach ($flashMessages as $flashMessage) {
list($text, $style) = explode(';', $flashMessage);
$messages[] = [
'text' => $text,
'style' => $style,
];
}
}
return $messages;
}
/**
* Get real user ip
*
* @return string
*/
static function getUserIP() {
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if (filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
} elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $forward;
} else {
$ip = $remote;
}
return empty(explode(':', explode(',', $forward)[0])[0]) ? $ip : explode(':', explode(',', $forward)[0])[0];
}
/**
* Returns version by composer.json
*
* @return string
*/
static function getVersion() {
if (is_readable(__DIR__ . '/../../composer.json')) {
$package = json_decode(file_get_contents(__DIR__ . '/../../composer.json'), true);
return $package['version'];
}
}
/**
* Returns true if validation is passed
*
* @param \Slim\Http\Request $request
* @return boolean
*/
static function validateUser($request) {
$error = false;
$em = AppContainer::getInstance()->getContainer()->get('em');
$settings = AppContainer::getInstance()->getContainer()->get('settings');
$flash = AppContainer::getInstance()->getContainer()->get('flash');
$userSearch = $em->getRepository('App\Entity\User')->findOneBy(['name' => $request->getParam('user_name')]);
$userName = $request->getParam('user_name');
$userPass = $request->getParam('user_pass');
$userPassRepeat = $request->getParam('user_pass_repeat');
// if user already exists
if ($userSearch instanceof User) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m1') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if $userName smaller than min length
if (strlen($userName) < (int)$settings['validation']['min_user_name_length']) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m2', [$settings['validation']['min_user_name_length']]) . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if $userName taller than max length
if (strlen($userName) > (int)$settings['validation']['max_user_name_length']) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m7', [$settings['validation']['max_user_name_length']]) . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if $userName smaller than min length
if (strlen($userPass) < (int)$settings['validation']['min_password_length'] || strlen($userPassRepeat) < (int)$settings['validation']['min_password_length']) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m3', [$settings['validation']['min_password_length']]) . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if password and password repeat not the same
if ($userPass !== $userPassRepeat) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m4') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if password contains no number
if (!preg_match('/[0-9]+/', $userPass) && $settings['validation']['password_with_digit'] === true) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m10') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if password contains no lowercase letter
if (!preg_match('/[a-z]+/', $userPass) && $settings['validation']['password_with_lcc'] === true) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m11') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if password contains no uppercase letter
if (!preg_match('/[A-Z]+/', $userPass) && $settings['validation']['password_with_ucc'] === true) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m12') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if password contains no non-word character
if (!preg_match('/\W+/', $userPass) && $settings['validation']['password_with_nwc'] === true) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m13') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
// if $userName contains illegal chars
foreach (str_split($userName) as $char) {
// if char not allowed
if (!in_array(strtolower($char), $settings['validation']['allowed_user_name_chars'])) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m8') . ';' . BaseController::STYLE_DANGER);
$error = true;
break;
}
}
$activeRoutes = $settings['locale']['active'];
// if is domain mode
if (LanguageUtility::processHas(LanguageUtility::LOCALE_URL)
&& LanguageUtility::processHas(LanguageUtility::DOMAIN_ENABLED)) {
$activeRoutes[$settings['locale']['generic_code']] = '';
}
// if is session mode
if (LanguageUtility::processHas(LanguageUtility::LOCALE_SESSION)
&& LanguageUtility::processHas(LanguageUtility::DOMAIN_DISABLED)) {
unset($activeRoutes);
$activeRoutes[$settings['locale']['generic_code']] = '';
}
// if $userName is equal to a route
foreach ($activeRoutes as $activeLocale => $domain) {
if (is_readable($settings['config_path'] . 'routes/' . $activeLocale . '.php')) {
$routes = require $settings['config_path'] . 'routes/' . $activeLocale . '.php';
if (isset($routes) && is_array($routes)) {
foreach ($routes as $routeName => $route) {
if (str_replace('/', '', $route['route']) === strtolower($userName)) {
$flash->addMessage('message', LanguageUtility::trans('register-flash-m9') . ';' . BaseController::STYLE_DANGER);
$error = true;
break;
}
}
}
} else {
$flash->addMessage('message', LanguageUtility::trans('register-flash-mXD') . ';' . BaseController::STYLE_DANGER);
$error = true;
}
}
return !$error;
}
}