1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HayriCan\IpChecker; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Laravel IP Checker |
7
|
|
|
* |
8
|
|
|
* @author Hayri Can BARÇIN <hayricanbarcin (#) gmail (.) com> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link https://github.com/HayriCan/laravel-ip-checker |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Carbon\Carbon; |
|
|
|
|
14
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
15
|
|
|
use HayriCan\IpChecker\Contracts\IpCheckerInterface; |
16
|
|
|
|
17
|
|
|
class FileDriver implements IpCheckerInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* file path to save the logs |
22
|
|
|
*/ |
23
|
|
|
protected $path; |
24
|
|
|
protected $ipList = []; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->path = storage_path(config('ipchecker.filepath')); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getIpArray() |
35
|
|
|
{ |
36
|
|
|
if (is_dir($this->path)) { |
37
|
|
|
$files = scandir($this->path); |
38
|
|
|
|
39
|
|
|
foreach ($files as $file) { |
40
|
|
|
if (!is_dir($file)) { |
41
|
|
|
$lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
42
|
|
|
foreach ($lines as $line) { |
43
|
|
|
$contentarr = explode(";", $line); |
44
|
|
|
array_push($this->ipList, $contentarr[2]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
$ipList = $this->ipList ?? []; |
49
|
|
|
|
50
|
|
|
return $ipList; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array|\Illuminate\Support\Collection|mixed |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function getIpList() |
60
|
|
|
{ |
61
|
|
|
if (is_dir($this->path)) { |
62
|
|
|
$files = scandir($this->path); |
63
|
|
|
|
64
|
|
|
foreach ($files as $file) { |
65
|
|
|
if (!is_dir($file)) { |
66
|
|
|
$lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
67
|
|
|
foreach ($lines as $line) { |
68
|
|
|
$contentarr = explode(";", $line); |
69
|
|
|
array_push($this->ipList, $this->mapArrayToModel($contentarr)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
return collect($this->ipList); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $array |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function saveIp($array) |
84
|
|
|
{ |
85
|
|
|
$array['created_at']=Carbon::now()->toDateTimeString(); |
86
|
|
|
$filename = $this->getFilename(); |
87
|
|
|
|
88
|
|
|
$contents = implode(";", $array); |
89
|
|
|
|
90
|
|
|
File::makeDirectory($this->path, 0777, true, true); |
91
|
|
|
|
92
|
|
|
File::append(($this->path.DIRECTORY_SEPARATOR.$filename), $contents.PHP_EOL); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $ipAddress |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function deleteIp($ipAddress) |
102
|
|
|
{ |
103
|
|
|
if (is_dir($this->path)) { |
104
|
|
|
$files = scandir($this->path); |
105
|
|
|
|
106
|
|
|
foreach ($files as $file) { |
107
|
|
|
if (!is_dir($file)) { |
108
|
|
|
$lines = file($this->path.DIRECTORY_SEPARATOR.$file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
109
|
|
|
foreach ($lines as $line) { |
110
|
|
|
if (strpos($line, $ipAddress)){ |
111
|
|
|
$contents = file_get_contents($this->path.DIRECTORY_SEPARATOR.$file); |
112
|
|
|
$contents = str_replace($line,'',$contents); |
113
|
|
|
file_put_contents($this->path.DIRECTORY_SEPARATOR.$file,$contents); |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Helper method for mapping array into models |
127
|
|
|
* |
128
|
|
|
* @param array $data |
129
|
|
|
* @return IpList |
130
|
|
|
*/ |
131
|
|
|
public function mapArrayToModel(array $data){ |
132
|
|
|
$model = new IpList(); |
133
|
|
|
$model->group = $data[0]; |
134
|
|
|
$model->definition = $data[1]; |
135
|
|
|
$model->ip = $data[2]; |
136
|
|
|
$model->created_at = Carbon::make($data[3]); |
137
|
|
|
return $model; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* get log file if defined in constants |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getFilename() |
146
|
|
|
{ |
147
|
|
|
$filename = 'iplist.php'; |
148
|
|
|
if (config('ipchecker.filename')){ |
|
|
|
|
149
|
|
|
$filename = config('ipchecker.filename'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $filename; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths