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