PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / lockout-ip.php

lockout-ip.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/model/lockout-ip.php

242 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Model;
4
5 use Calotes\Helper\Array_Cache;
6 use WP_Defender\DB;
7 use WP_Defender\Model\Lockout_Log;
8 use WP_Defender\Model\Setting\Blacklist_Lockout;
9 use WP_Defender\Model\Setting\User_Agent_Lockout;
10
11 class Lockout_Ip extends DB {
12 public const STATUS_BLOCKED = 'blocked', STATUS_NORMAL = 'normal';
13
14 protected $table = 'defender_lockout';
15
16 /**
17 * @var int
18 * @defender_property
19 */
20 public $id;
21 /**
22 * @var string
23 * @defender_property
24 */
25 public $ip;
26 /**
27 * @var string
28 * @defender_property
29 */
30 public $status;
31 /**
32 * @var string
33 * @defender_property
34 */
35 public $lockout_message;
36 /**
37 * @var int
38 * @defender_property
39 */
40 public $release_time;
41 /**
42 * @var int
43 * @defender_property
44 */
45 public $lock_time;
46 /**
47 * @var int
48 * @defender_property
49 */
50 public $lock_time_404;
51 /**
52 * @var int
53 * @defender_property
54 */
55 public $attempt;
56 /**
57 * @var int
58 * @defender_property
59 */
60 public $attempt_404;
61 /**
62 * @var array
63 * @defender_property
64 */
65 public $meta = array();
66
67 /**
68 * Get the record by IP, if it not appears, then create one.
69 *
70 * @param string $ip
71 * @param null|string $status
72 * @param boolean $all
73 *
74 * @return $this
75 */
76 public static function get( $ip, $status = null, $all = false ) {
77 $model = Array_Cache::get( $ip, 'lockout' );
78 if ( is_object( $model ) ) {
79 return $model;
80 }
81 $orm = self::get_orm();
82 $builder = $orm->get_repository( Lockout_Ip::class )
83 ->where( 'ip', $ip );
84 if ( null !== $status ) {
85 $status = 'unban' === $status ? self::STATUS_BLOCKED : self::STATUS_NORMAL;
86 $builder->where( 'status', $status );
87 }
88
89 if ( true === $all ) {
90 $model = $builder->get();
91 return $model;
92 }
93
94 $model = $builder->first();
95
96 if ( ! is_object( $model ) ) {
97 $model = new Lockout_Ip();
98 $model->ip = $ip;
99 $model->attempt = 0;
100 $model->status = self::STATUS_NORMAL;
101 $model->lockout_message = '';
102 $model->release_time = '';
103 $model->lock_time = '';
104 $model->lock_time_404 = '';
105 $model->attempt_404 = '';
106 $orm->save( $model );
107 }
108
109 Array_Cache::set( $ip, $model, 'lockout' );
110
111 return $model;
112 }
113
114 /**
115 * Get bulk IPs.
116 *
117 * @param string $status
118 * @param string|null $ips
119 * @param int|string|null $limit
120 *
121 * @return $this
122 */
123 public static function get_bulk( $status, $ips = null, $limit = null ) {
124 $orm = self::get_orm();
125 $builder = $orm->get_repository( Lockout_Ip::class );
126 if ( null === $ips ) {
127 $builder->where( 'status', $status );
128 }
129 if ( null !== $ips ) {
130 $builder->where( 'ip', 'in', $ips );
131 }
132 if ( null !== $limit ) {
133 $builder->limit( $limit );
134 }
135 $models = $builder->get();
136
137 return $models;
138 }
139
140 /**
141 * Get the access status of this IP.
142 *
143 * @return array
144 */
145 public function get_access_status() {
146 $settings = wd_di()->get( Blacklist_Lockout::class );
147 if (
148 ! in_array( $this->ip, $settings->get_list( 'blocklist' ), true )
149 && ! in_array( $this->ip, $settings->get_list( 'allowlist' ), true )
150 ) {
151
152 return array( 'na' );
153 }
154
155 $result = array();
156 if ( in_array( $this->ip, $settings->get_list( 'blocklist' ), true ) ) {
157 $result[] = 'banned';
158 }
159 if ( in_array( $this->ip, $settings->get_list( 'allowlist' ), true ) ) {
160 $result[] = 'allowlist';
161 }
162
163 return $result;
164 }
165
166 /**
167 * Return the access status of this IP, as readable text.
168 * Todo: check values from ban_status(), class Table_Lockout:
169 * 'ban' (STATUS_BAN) --> 'banned'
170 * 'not_ban' (STATUS_NOT_BAN) --> 'na'
171 * 'allowlist' (STATUS_ALLOWLIST) --> 'allowlist'
172 *
173 * @param string $status
174 *
175 * @return string
176 */
177 public function get_access_status_text( $status ) {
178 switch ( $status ) {
179 case 'banned':
180 return __( 'Banned', 'wpdef' );
181 case 'allowlist':
182 return __( 'In Allowlist', 'wpdef' );
183 case 'na':
184 return __( 'Not banned or in allowlist' );
185 default:
186 return '';
187 }
188 }
189
190 /**
191 * Get locked IPs.
192 * Todo: only columns 'id', 'ip', 'status' from $models.
193 *
194 * @return array
195 */
196 public static function query_locked_ip() {
197 $orm = self::get_orm();
198 $models = $orm->get_repository( self::class )
199 ->where( 'status', self::STATUS_BLOCKED )
200 ->group_by( 'ip' )
201 ->order_by( 'lock_time', 'desc' )
202 ->get();
203
204 return $models;
205 }
206
207 /**
208 * @return bool
209 */
210 public function is_locked() {
211 if ( self::STATUS_BLOCKED === $this->status ) {
212 $time = new \DateTime( 'now', wp_timezone() );
213 if ( $this->release_time < $time->getTimestamp() ) {
214 // Unlock it.
215 $this->attempt = 0;
216 $this->meta = array(
217 'nf' => array(),
218 'login' => array(),
219 );
220 $this->status = self::STATUS_NORMAL;
221 $this->save();
222
223 return false;
224 } else {
225 return true;
226 }
227 }
228
229 return false;
230 }
231
232 /**
233 * Return remaining release time.
234 *
235 * @return int Remaining release time.
236 */
237 public function remaining_release_time() {
238 $time = new \DateTime( 'now', wp_timezone() );
239 return $this->release_time - $time->getTimestamp();
240 }
241 }
242