Passed
Push — master ( ad0e9b...345304 )
by IRFA
03:47
created

Core::test_unlock_account()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 9.8333
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
     * @param string $username
27
     * @return void
28
     */
29
    protected function eventFailedLogin($username=null){
30
        
31
        if($username != null){
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $username of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
32
          $this->setPath($username);
33
        }
34
        if(!File::exists($this->dir)){
35
                File::makeDirectory($this->dir, 0755, true);
36
        }
37
38
        if(!File::exists($this->path))
39
        {
40
            $login_fail = 1;
41
        } else{
42
43
            $get = json_decode(File::get($this->path));
44
            $ip_list = $get->ip;
45
            if(!$this->checkIp($ip_list,$this->ip)){
46
                array_push($ip_list,$this->ip);
47
            }
48
            if($get->attemps == "lock"){
49
                $login_fail = "lock";
50
            } else{
51
                $login_fail = $get->attemps+1;
52
            }
53
        }
54
        
55
            $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())];
56
            File::put($this->path,json_encode($content));
57
            if(File::exists($this->path)){
58
              chmod($this->path,0755);
59
            }
60
          
61
    }
62
63
    /**
64
     * Clean Lockout file if success login
65
     *
66
     * @param  string  $rootNamespace
67
     * @return void
68
     */
69
    protected function eventCleanLockoutAccount(){
70
        $this->unlock_account($this->input);
71
          
72
    }
73
74
    /**
75
     * Logging Failed Login attemps
76
     * stored file in storage/logs/laravel.log
77
     *
78
     * @param  string  $middleware
79
     * @return void
80
     */
81
    protected function logging($middleware="WEB"){
82
        if(config('irfa.lockout.logging')){
83
                    Log::notice($middleware." | Login attemps fail | "."username : ".Request::input(config('irfa.lockout.input_name'))." | ipAddress : ".Request::ip()." | userAgent : ".$_SERVER['HTTP_USER_AGENT'].PHP_EOL);
84
            }
85
    }
86
87
     /**
88
       * Check if user is locked
89
       *
90
       * @param  string  $username
91
       * @return boolean
92
     */
93
    protected function is_locked($username){
94
        $this->setPath($username);
95
        if(File::exists($this->path))
96
        {
97
           $get = json_decode(File::get($this->path));
98
           if($get->attemps > $this->attemps || $get->attemps == "lock"){
99
              return true;
100
           } else{
101
              return false;
102
           }
103
        } else{
104
            return false;
105
        }
106
    }
107
108
    /**
109
       * Show message if failed x attemps
110
       *
111
       * @return string
112
     */
113
    protected function showMessage(){
114
        if(Session::has(config('irfa.lockout.message_name'))){
115
            return Session::get(config('irfa.lockout.message_name'));
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * Locked account  if max attemps reached
123
     *
124
     * @return boolean
125
     */
126
    protected function lockLogin($username = null){
127
        
128
        if(php_sapi_name() == "cli" AND $username != null){
129
          $this->setPath($username);
130
        }
131
132
        if(File::exists($this->path))
133
        {
134
                $get = json_decode(File::get($this->path));
135
                if($get->attemps == "lock"){
136
                return true;
137
                }
138
                if($get->attemps > $this->attemps){
139
                    if($matchip){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $matchip seems to be never defined.
Loading history...
140
                    if($this->checkIp($ip_list,$this->ip)){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip_list seems to be never defined.
Loading history...
141
                        return true;
142
                    } else{
143
                        return false;
144
                    }
145
                    } else{
146
                    return true;
147
                    }
148
                } else{
149
                return false;
150
                }
151
        } else{
152
            return false;
153
            }
154
    }
155
156
     /**
157
     * Check ip locked
158
     *
159
     * @return boolean
160
     */
161
    private function checkIp($ip_list,$ip){
162
        if(collect($ip_list)->contains($ip)){
163
            return true;
164
        } else{
165
            return false;
166
        }
167
168
    }
169
170
     /**
171
     * Clear all locked account
172
     *
173
     * @return boolean
174
     */
175
    public function clear_all(){
176
        $file = new Filesystem();
177
        if($file->cleanDirectory($this->path)){
178
        return true;
179
        } else{
180
        return false;
181
        }
182
    }
183
184
     /**
185
     * Unlocking account manually.
186
     *
187
     * @param string $username
188
     * @return mixed
189
     */
190
    public function unlock_account($username){
191
        $this->setPath($username);
192
         if(File::exists($this->path)){
193
            $readf = File::get($this->path);
194
                File::delete($this->path);
195
            if(php_sapi_name() == "cli"){
196
                echo Lang::get('lockoutMessage.user_unlock_success')."\n";
197
                return $readf;
198
              
199
            } else{
200
                return true;
201
            }
202
        } else{
203
            if(php_sapi_name() == "cli"){
204
                echo Lang::get('lockoutMessage.user_lock_404')."\n";
205
                return false;
206
            } else{
207
                return false;
208
            }
209
        }
210
      }
211
212
    /**
213
     * For Testing
214
     *
215
     * @return boolean or json(if cli)
216
     */
217
    public function test_unlock_account($username){
218
        $this->setPath($username);
219
         if(File::exists($this->path)){
220
            $readf = File::get($this->path);
0 ignored issues
show
Unused Code introduced by
The assignment to $readf is dead and can be removed.
Loading history...
221
                File::delete($this->path);
222
            if(php_sapi_name() == "cli"){
223
                return true;
224
              
225
            } else{
226
                return true;
227
            }
228
        } else{
229
            if(php_sapi_name() == "cli"){
230
               return false;
231
            } else{
232
                return false;
233
            }
234
        }
235
      }
236
237
    /**
238
     * Check account with details
239
     *
240
     * @param string $username
241
     * @return boolean
242
     */
243
    public function check_account($username){
244
      $this->setPath($username);
245
       if(File::exists($this->path)){
246
              $readf = File::get($this->path);
247
              if(php_sapi_name() == "cli"){
248
                
249
                  return $readf;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $readf returns the type string which is incompatible with the documented return type boolean.
Loading history...
250
                
251
              } else{
252
                  return $readf;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $readf returns the type string which is incompatible with the documented return type boolean.
Loading history...
253
              }
254
          } else{
255
              if(php_sapi_name() == "cli"){
256
                  echo Lang::get('lockoutMessage.user_lock_404')."\n";
257
                  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...
258
              } else{
259
                  return false;
260
              }
261
          }
262
        }
263
264
     /**
265
     * Locking account manually
266
     *
267
     * @param string $username
268
     * @return boolean
269
     */
270
    public function lock_account($username){
271
        $sapi = php_sapi_name() == "cli"?"lock-via-cli":"lock-via-web";
272
        $this->setPath($username);
273
        try{
274
          if(!File::exists($this->dir)){
275
              File::makeDirectory($this->dir, 0755, true);
276
          }
277
              $login_fail = "lock";
278
        
279
              $content = ['username' => $this->input,'attemps' => $login_fail,'ip' => isset($ip_list)?$ip_list:[$sapi],'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...
280
              File::put($this->path,json_encode($content));
281
              if(File::exists($this->path)){
282
                chmod($this->path,0755);
283
              }
284
              if(php_sapi_name() == "cli"){
285
                return Lang::get('lockoutMessage.user_lock_success')."\n";
0 ignored issues
show
Bug Best Practice introduced by
The expression return Lang::get('lockou...er_lock_success') . ' ' returns the type string which is incompatible with the documented return type boolean.
Loading history...
286
                
287
              } else{
288
              return true;
289
              }
290
          } 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...
291
              if(php_sapi_name() == "cli"){
292
                return "error";
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'error' returns the type string which is incompatible with the documented return type boolean.
Loading history...
293
                
294
              } else{
295
                return false;
296
              }
297
          }
298
    }
299
}