1
|
|
|
<?php |
2
|
|
|
namespace Irfa\Lockout\Func; |
3
|
|
|
|
4
|
|
|
use Log; |
5
|
|
|
use Illuminate\Support\Facades\Request, File, Lang, Session; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use Irfa\Lockout\Initializing\Variable; |
9
|
|
|
|
10
|
|
|
class Core extends Variable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Except account. |
14
|
|
|
* |
15
|
|
|
* @return boolean |
16
|
|
|
*/ |
17
|
|
|
protected function exceptAccount(){ |
18
|
|
|
$this->initVar(); |
19
|
|
|
if($this->except_enabled){ |
20
|
|
|
if(in_array($this->input, $this->except_accounts)){ |
21
|
|
|
return true; |
22
|
|
|
} else{ |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
} else{ |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* write login attemps if login attemp is triggered. |
32
|
|
|
* |
33
|
|
|
* @param string $username |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected function eventFailedLogin($username=null){ |
37
|
|
|
$this->initVar(); |
38
|
|
|
if($username !== null){ |
39
|
|
|
$this->setPath($username); |
40
|
|
|
} |
41
|
|
|
if(!File::exists($this->dir)){ |
42
|
|
|
File::makeDirectory($this->dir, 0755, true); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if(!File::exists($this->path)) |
46
|
|
|
{ |
47
|
|
|
$login_fail = 1; |
48
|
|
|
} else{ |
49
|
|
|
|
50
|
|
|
$get = json_decode(File::get($this->path)); |
51
|
|
|
$ip_list = $get->ip; |
52
|
|
|
if(!$this->checkIp($ip_list,$this->ip)){ |
53
|
|
|
array_push($ip_list,$this->ip); |
54
|
|
|
} |
55
|
|
|
if($get->attemps == "lock"){ |
56
|
|
|
$login_fail = "lock"; |
57
|
|
|
} else{ |
58
|
|
|
$login_fail = $get->attemps+1; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$content = ['username' => $this->input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$this->ip],'last_attemps' => date("Y-m-d H:i:s",time())]; |
63
|
|
|
File::put($this->path,json_encode($content)); |
64
|
|
|
if(File::exists($this->path)){ |
65
|
|
|
chmod($this->path,0755); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Clean Lockout file if success login |
72
|
|
|
* |
73
|
|
|
* @param string $rootNamespace |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
protected function eventCleanLockoutAccount() { |
77
|
|
|
$this->initVar(); |
78
|
|
|
$this->unlock_account($this->input); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Logging Failed Login attemps |
84
|
|
|
* stored file in storage/logs/laravel.log |
85
|
|
|
* |
86
|
|
|
* @param string $middleware |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function logging($middleware = "WEB") { |
90
|
|
|
if (config('irfa.lockout.logging')) { |
91
|
|
|
Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if user is locked |
97
|
|
|
* |
98
|
|
|
* @param string $username |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
|
|
protected function is_locked($username){ |
102
|
|
|
$this->initVar(); |
103
|
|
|
$this->setPath($username); |
104
|
|
|
if(File::exists($this->path)) |
105
|
|
|
{ |
106
|
|
|
$get = json_decode(File::get($this->path)); |
107
|
|
|
if($get->attemps > $this->attemps || $get->attemps == "lock"){ |
108
|
|
|
return true; |
109
|
|
|
} else{ |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} else{ |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Show message if failed x attemps |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
protected function showMessage() { |
123
|
|
|
if (Session::has(config('irfa.lockout.message_name'))) { |
124
|
|
|
return Session::get(config('irfa.lockout.message_name')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Locked account if max attemps reached |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
protected function lockLogin($username = null){ |
136
|
|
|
$this->initVar(); |
137
|
|
|
if(php_sapi_name() == "cli" AND $username != null){ |
138
|
|
|
$this->setPath($username); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if(File::exists($this->path)) |
142
|
|
|
{ |
143
|
|
|
$get = json_decode(File::get($this->path)); |
144
|
|
|
if($get->attemps == "lock"){ |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
if($get->attemps > $this->attemps){ |
148
|
|
|
|
149
|
|
|
return true; |
150
|
|
|
} else { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} else { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check ip locked |
160
|
|
|
* |
161
|
|
|
* @return boolean |
162
|
|
|
*/ |
163
|
|
|
private function checkIp($ip_list,$ip){ |
164
|
|
|
$this->initVar(); |
165
|
|
|
if(collect($ip_list)->contains($ip)){ |
166
|
|
|
return true; |
167
|
|
|
} else{ |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Clear all locked account |
175
|
|
|
* |
176
|
|
|
* @return boolean |
177
|
|
|
*/ |
178
|
|
|
public function clear_all(){ |
179
|
|
|
$this->initVar(); |
180
|
|
|
$file = new Filesystem(); |
181
|
|
|
if($file->cleanDirectory($this->path)){ |
182
|
|
|
return true; |
183
|
|
|
} else{ |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Unlocking account manually. |
190
|
|
|
* |
191
|
|
|
* @param string $username |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
public function unlock_account($username){ |
195
|
|
|
$this->initVar(); |
196
|
|
|
$this->setPath($username); |
197
|
|
|
if(File::exists($this->path)){ |
198
|
|
|
$readf = File::get($this->path); |
199
|
|
|
File::delete($this->path); |
200
|
|
|
if(php_sapi_name() == "cli"){ |
201
|
|
|
echo Lang::get('lockoutMessage.user_unlock_success')."\n"; |
202
|
|
|
return $readf; |
203
|
|
|
|
204
|
|
|
} else{ |
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
} else{ |
208
|
|
|
if(php_sapi_name() == "cli"){ |
209
|
|
|
echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
210
|
|
|
return false; |
211
|
|
|
} else{ |
212
|
|
|
return false; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* For Testing |
219
|
|
|
* |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
public function test_unlock_account($username){ |
223
|
|
|
$this->initVar(); |
224
|
|
|
$this->setPath($username); |
225
|
|
|
if(File::exists($this->path)){ |
226
|
|
|
$readf = File::get($this->path); |
|
|
|
|
227
|
|
|
File::delete($this->path); |
228
|
|
|
if(php_sapi_name() == "cli"){ |
229
|
|
|
return true; |
230
|
|
|
|
231
|
|
|
} else{ |
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
} else{ |
235
|
|
|
if(php_sapi_name() == "cli"){ |
236
|
|
|
return false; |
237
|
|
|
} else{ |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check account with details |
245
|
|
|
* |
246
|
|
|
* @param string $username |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function check_account($username){ |
250
|
|
|
$this->initVar(); |
251
|
|
|
$this->setPath($username); |
252
|
|
|
if(File::exists($this->path)){ |
253
|
|
|
$readf = File::get($this->path); |
254
|
|
|
if(php_sapi_name() == "cli"){ |
255
|
|
|
|
256
|
|
|
return $readf; |
257
|
|
|
|
258
|
|
|
} else{ |
259
|
|
|
return $readf; |
260
|
|
|
} |
261
|
|
|
} else{ |
262
|
|
|
if(php_sapi_name() == "cli"){ |
263
|
|
|
echo Lang::get('lockoutMessage.user_lock_404')."\n"; |
264
|
|
|
exit(); |
|
|
|
|
265
|
|
|
} else{ |
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Locking account manually |
273
|
|
|
* |
274
|
|
|
* @param string $username |
275
|
|
|
* @return mixed |
276
|
|
|
*/ |
277
|
|
|
public function lock_account($username){ |
278
|
|
|
$this->initVar(); |
279
|
|
|
$sapi = php_sapi_name() == "cli"?"lock-via-cli":"lock-via-web"; |
280
|
|
|
$this->setPath($username); |
281
|
|
|
try{ |
282
|
|
|
if(!File::exists($this->dir)){ |
283
|
|
|
File::makeDirectory($this->dir, 0755, true); |
284
|
|
|
} |
285
|
|
|
$login_fail = "lock"; |
286
|
|
|
|
287
|
|
|
$content = ['username' => $this->input,'attemps' => $login_fail,'ip' => [$sapi],'last_attemps' => date("Y-m-d H:i:s",time())]; |
288
|
|
|
File::put($this->path,json_encode($content)); |
289
|
|
|
if(File::exists($this->path)){ |
290
|
|
|
chmod($this->path,0755); |
291
|
|
|
} |
292
|
|
|
if(php_sapi_name() == "cli"){ |
293
|
|
|
return Lang::get('lockoutMessage.user_lock_success')."\n"; |
294
|
|
|
|
295
|
|
|
} else{ |
296
|
|
|
return true; |
297
|
|
|
} |
298
|
|
|
} catch(\Exception $e){ |
299
|
|
|
if(php_sapi_name() == "cli"){ |
300
|
|
|
return "error"; |
301
|
|
|
|
302
|
|
|
} else{ |
303
|
|
|
return false; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |