Passed
Push — master ( 5250f7...abb268 )
by IRFA
03:17
created

Core::showMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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
          
42
    }
43
        protected function eventCleanLockoutAccount(){
44
        $input = Request::input(config('irfa.lockout.input_name'));
45
        $this->unlock_account($input);
46
          
47
    }
48
    protected function logging($middleware="WEB"){
49
        if(config('irfa.lockout.logging')){
50
                    Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL);
51
            }
52
    }
53
    protected function showMessage(){
54
      if(Session::has(config('irfa.lockout.message_name'))){
55
         return Session::get(config('irfa.lockout.message_name'));
56
      }
57
58
      return null;
59
    }
60
    protected function lockLogin(){
61
        $ip = Request::ip();
62
        $matchip= empty(config('irfa.lockout.match_ip'))?false:config('irfa.lockout.match_ip');
63
        $dir = config('irfa.lockout.lockout_file_path');
64
        $attemps = config('irfa.lockout.login_attemps');
65
        $path = $dir.md5(Request::input('email'));
66
        if(File::exists($path))
67
        {
68
                $get = json_decode(File::get($path));
69
            // dd($get->attemps.">".$attemps);
70
                if($get->attemps == "lock"){
71
                return true;
72
                }
73
                if($get->attemps > $attemps){
74
                    if($matchip){
75
                    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...
76
                        return true;
77
                    } else{
78
                        return false;
79
                    }
80
                    } else{
81
                    return true;
82
                    }
83
                } else{
84
                return false;
85
                }
86
        } else{
87
            return false;
88
            }
89
    }
90
    private function checkIp($ip_list,$ip){
91
        if(collect($ip_list)->contains($ip)){
92
            return true;
93
        } else{
94
            return false;
95
        }
96
97
    }
98
    public function clear_all(){
99
        $file = new Filesystem();
100
        if($file->cleanDirectory(config('irfa.lockout.lockout_file_path'))){
101
        return true;
102
        } else{
103
        return false;
104
        }
105
    }
106
    public function unlock_account($username){
107
        $ip = Request::ip();
0 ignored issues
show
Unused Code introduced by
The assignment to $ip is dead and can be removed.
Loading history...
108
        $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...
109
        $dir = config('irfa.lockout.lockout_file_path');
110
        $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...
111
        $path = $dir.md5($username);
112
113
        if(File::exists($path)){
114
            $readf = File::get($path);
115
                File::delete($path);
116
            if(php_sapi_name() == "cli"){
117
                echo Lang::get('lockoutMessage.user_unlock_success')."\n";
118
                return $readf;
119
              
120
            } else{
121
                return true;
122
            }
123
        } else{
124
            if(php_sapi_name() == "cli"){
125
                echo Lang::get('lockoutMessage.user_lock_404')."\n";
126
                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...
127
            } else{
128
                return false;
129
            }
130
        }
131
        }
132
        public function check_account($username){
133
        $dir = config('irfa.lockout.lockout_file_path');
134
        $path = $dir.md5($username);
135
136
        if(File::exists($path)){
137
            $readf = File::get($path);
138
            if(php_sapi_name() == "cli"){
139
              
140
                return $readf;
141
              
142
            } else{
143
                return $readf;
144
            }
145
        } else{
146
            if(php_sapi_name() == "cli"){
147
                echo Lang::get('lockoutMessage.user_lock_404')."\n";
148
                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...
149
            } else{
150
                return false;
151
            }
152
        }
153
        }
154
155
        public function lock_account($username){
156
        $ip = php_sapi_name() == "cli"?"lock-via-cli":"lock-via-web";
157
        $input = $username;
158
        $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...
159
        $dir = config('irfa.lockout.lockout_file_path');
160
        $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...
161
        $path = $dir.md5($username);
162
        try{
163
            if(!File::exists($dir)){
164
                File::makeDirectory($dir, 0750, true);
165
            }
166
                $login_fail = "lock";
167
          
168
                $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...
169
                File::put($path,json_encode($content));
170
                if(php_sapi_name() == "cli"){
171
                return Lang::get('lockoutMessage.user_lock_success')."\n";
172
                  
173
                } else{
174
                return true;
175
                }
176
            } 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...
177
                if(php_sapi_name() == "cli"){
178
                return "error";
179
                  
180
                } else{
181
                return false;
182
                }
183
            }
184
        }
185
}