Passed
Push — master ( 5bb679...8d229b )
by IRFA
04:04 queued 11s
created

Core::is_locked()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 9.9
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
9
class Core
10
{
11
    protected function eventFailedLogin(){
12
        $ip = Request::ip();
13
        $input = Request::input(config('irfa.lockout.input_name'));
14
        $matchip= config('irfa.lockout.match_ip') == true ? $ip :null;
0 ignored issues
show
Unused Code introduced by
The assignment to $matchip is dead and can be removed.
Loading history...
15
        $dir = config('irfa.lockout.lockout_file_path');
16
        $path = $dir.md5($input);
17
18
        if(!File::exists($dir)){
19
                File::makeDirectory($dir, 0750, true);
20
        }
21
22
        if(!File::exists($path))
23
        {
24
            $login_fail = 1;
25
        } else{
26
27
            $get = json_decode(File::get($path));
28
            $ip_list = $get->ip;
29
            if(!$this->checkIp($ip_list,$ip)){
30
                array_push($ip_list,$ip);
31
            }
32
            if($get->attemps == "lock"){
33
                $login_fail = "lock";
34
            } else{
35
                $login_fail = $get->attemps+1;
36
            }
37
        }
38
        
39
            $content = ['username' => $input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$ip],'last_attemps' => date("Y-m-d H:i:s",time())];
40
            File::put($path,json_encode($content));
41
            if(File::exists($path)){
42
              chmod($path,0750);
43
            }
44
          
45
    }
46
    protected function eventCleanLockoutAccount(){
47
        $input = Request::input(config('irfa.lockout.input_name'));
48
        $this->unlock_account($input);
49
          
50
    }
51
    protected function logging($middleware="WEB"){
52
        if(config('irfa.lockout.logging')){
53
                    Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL);
54
            }
55
    }
56
    protected function is_locked($username){
57
        $dir = config('irfa.lockout.lockout_file_path');
58
        $attemps = config('irfa.lockout.login_attemps');
59
        $path = $dir.md5($username);
60
        if(File::exists($path))
61
        {
62
           $get = json_decode(File::get($path));
63
           if($get->attemps > $attemps || $get->attemps == "lock"){
64
              return true;
65
           } else{
66
              return false;
67
           }
68
        } else{
69
            return false;
70
        }
71
    }
72
73
    protected function showMessage(){
74
        if(Session::has(config('irfa.lockout.message_name'))){
75
            return Session::get(config('irfa.lockout.message_name'));
76
        }
77
78
        return null;
79
    }
80
    protected function lockLogin(){
81
        $ip = Request::ip();
82
        $input = Request::input(config('irfa.lockout.input_name'));
83
        $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip');
84
        $dir = config('irfa.lockout.lockout_file_path');
85
        $attemps = config('irfa.lockout.login_attemps');
86
        $path = $dir.md5($input);
87
        if(File::exists($path))
88
        {
89
                $get = json_decode(File::get($path));
90
            // dd($get->attemps.">".$attemps);
91
                if($get->attemps == "lock"){
92
                return true;
93
                }
94
                if($get->attemps > $attemps){
95
                    if($matchip){
96
                    if($this->checkIp($ip_list,$ip)){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip_list seems to be never defined.
Loading history...
97
                        return true;
98
                    } else{
99
                        return false;
100
                    }
101
                    } else{
102
                    return true;
103
                    }
104
                } else{
105
                return false;
106
                }
107
        } else{
108
            return false;
109
            }
110
    }
111
    private function checkIp($ip_list,$ip){
112
        if(collect($ip_list)->contains($ip)){
113
            return true;
114
        } else{
115
            return false;
116
        }
117
118
    }
119
    public function clear_all(){
120
        $file = new Filesystem();
121
        if($file->cleanDirectory(config('irfa.lockout.lockout_file_path'))){
122
        return true;
123
        } else{
124
        return false;
125
        }
126
    }
127
    public function unlock_account($username){
128
        $ip = Request::ip();
0 ignored issues
show
Unused Code introduced by
The assignment to $ip is dead and can be removed.
Loading history...
129
        $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip');
0 ignored issues
show
Unused Code introduced by
The assignment to $matchip is dead and can be removed.
Loading history...
130
        $dir = config('irfa.lockout.lockout_file_path');
131
        $attemps = config('irfa.lockout.attemps');
0 ignored issues
show
Unused Code introduced by
The assignment to $attemps is dead and can be removed.
Loading history...
132
        $path = $dir.md5($username);
133
134
        if(File::exists($path)){
135
            $readf = File::get($path);
136
                File::delete($path);
137
            if(php_sapi_name() == "cli"){
138
                echo Lang::get('lockoutMessage.user_unlock_success')."\n";
139
                return $readf;
140
              
141
            } else{
142
                return true;
143
            }
144
        } else{
145
            if(php_sapi_name() == "cli"){
146
                echo Lang::get('lockoutMessage.user_lock_404')."\n";
147
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
148
            } else{
149
                return false;
150
            }
151
        }
152
        }
153
        public function check_account($username){
154
        $dir = config('irfa.lockout.lockout_file_path');
155
        $path = $dir.md5($username);
156
157
        if(File::exists($path)){
158
            $readf = File::get($path);
159
            if(php_sapi_name() == "cli"){
160
              
161
                return $readf;
162
              
163
            } else{
164
                return $readf;
165
            }
166
        } else{
167
            if(php_sapi_name() == "cli"){
168
                echo Lang::get('lockoutMessage.user_lock_404')."\n";
169
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
170
            } else{
171
                return false;
172
            }
173
        }
174
        }
175
176
        public function lock_account($username){
177
        $ip = php_sapi_name() == "cli"?"lock-via-cli":"lock-via-web";
178
        $input = $username;
179
        $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip');
0 ignored issues
show
Unused Code introduced by
The assignment to $matchip is dead and can be removed.
Loading history...
180
        $dir = config('irfa.lockout.lockout_file_path');
181
        $attemps = config('irfa.lockout.login_attemps');
0 ignored issues
show
Unused Code introduced by
The assignment to $attemps is dead and can be removed.
Loading history...
182
        $path = $dir.md5($username);
183
        try{
184
            if(!File::exists($dir)){
185
                File::makeDirectory($dir, 0750, true);
186
            }
187
                $login_fail = "lock";
188
          
189
                $content = ['username' => $input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$ip],'last_attemps' => date("Y-m-d H:i:s",time())];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip_list seems to never exist and therefore isset should always be false.
Loading history...
190
                File::put($path,json_encode($content));
191
                if(File::exists($path)){
192
                  chmod($path,0750);
193
                }
194
                if(php_sapi_name() == "cli"){
195
                return Lang::get('lockoutMessage.user_lock_success')."\n";
196
                  
197
                } else{
198
                return true;
199
                }
200
            } catch(Exception $e){
0 ignored issues
show
Bug introduced by
The type Irfa\Lockout\Func\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
201
                if(php_sapi_name() == "cli"){
202
                return "error";
203
                  
204
                } else{
205
                return false;
206
                }
207
            }
208
        }
209
}