| 1 |
<?php |
| 2 |
/** |
| 3 |
* Model for the unlock out table. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model; |
| 9 |
|
| 10 |
use WP_Defender\DB; |
| 11 |
|
| 12 |
/** |
| 13 |
* Model for the unlock out table. |
| 14 |
*/ |
| 15 |
class Unlockout extends DB { |
| 16 |
|
| 17 |
// The anonymous type can be used for unauthorized users in the future. |
| 18 |
public const TYPE_REGISTERED = 'registered', TYPE_ANONYMOUS = 'anonymous'; |
| 19 |
public const STATUS_RESOLVED = 'resolved', STATUS_PENDING = 'pending'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Table name. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $table = 'defender_unlockout'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Primary key column. |
| 30 |
* |
| 31 |
* @var int |
| 32 |
* @defender_property |
| 33 |
*/ |
| 34 |
public $id; |
| 35 |
/** |
| 36 |
* Table column for the IP. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @defender_property |
| 40 |
*/ |
| 41 |
public $ip; |
| 42 |
|
| 43 |
/** |
| 44 |
* Table column for the type. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
* @defender_property |
| 48 |
*/ |
| 49 |
public $type; |
| 50 |
|
| 51 |
/** |
| 52 |
* Table column for the email. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
* @defender_property |
| 56 |
*/ |
| 57 |
public $email; |
| 58 |
|
| 59 |
/** |
| 60 |
* Table column for the status. |
| 61 |
* |
| 62 |
* @var string |
| 63 |
* @defender_property |
| 64 |
*/ |
| 65 |
public $status; |
| 66 |
|
| 67 |
/** |
| 68 |
* Table column for the timestamp. |
| 69 |
* |
| 70 |
* @var int |
| 71 |
* @defender_property |
| 72 |
*/ |
| 73 |
public $timestamp; |
| 74 |
|
| 75 |
/** |
| 76 |
* Creates a new record in the database with the given data. |
| 77 |
* |
| 78 |
* @param string $ip The IP address to be stored. |
| 79 |
* @param string $email The email address to be stored. |
| 80 |
* |
| 81 |
* @return bool True if the record was successfully saved, false otherwise. |
| 82 |
*/ |
| 83 |
public function create( $ip, $email ) { |
| 84 |
$this->ip = $ip; |
| 85 |
$this->type = self::TYPE_REGISTERED; |
| 86 |
$this->email = $email; |
| 87 |
$this->status = self::STATUS_PENDING; |
| 88 |
$this->timestamp = time(); |
| 89 |
|
| 90 |
return $this->save(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Remove data by given data. |
| 95 |
* |
| 96 |
* @param int $timestamp The timestamp to compare against. |
| 97 |
* @param int $limit The maximum number of records to delete. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public static function remove_records( $timestamp, $limit ) { |
| 102 |
$orm = self::get_orm(); |
| 103 |
$orm->get_repository( self::class ) |
| 104 |
->where( 'timestamp', '<=', $timestamp ) |
| 105 |
->order_by( 'id' ) |
| 106 |
->limit( $limit ) |
| 107 |
->delete_by_limit(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Remove all records. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public static function truncate() { |
| 116 |
$orm = self::get_orm(); |
| 117 |
$orm->get_repository( self::class )->truncate(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Retrieves the resolved IP address by the given ID, email, and limit time. |
| 122 |
* |
| 123 |
* @param int $id The ID of the record. |
| 124 |
* @param string $email The email associated with the record. |
| 125 |
* @param int $limit_time The limit time for the record. |
| 126 |
* |
| 127 |
* @return string The resolved IP address if it exists and is not expired, otherwise 'expired' or an empty string. |
| 128 |
*/ |
| 129 |
public static function get_resolved_ip_by( $id, $email, $limit_time ) { |
| 130 |
$orm = self::get_orm(); |
| 131 |
|
| 132 |
$model = $orm->get_repository( self::class ) |
| 133 |
->where( 'id', $id ) |
| 134 |
->where( 'email', $email ) |
| 135 |
->first(); |
| 136 |
|
| 137 |
if ( ! is_object( $model ) ) { |
| 138 |
return ''; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $model->timestamp > $limit_time ) { |
| 142 |
$model->status = self::STATUS_RESOLVED; |
| 143 |
$orm->save( $model ); |
| 144 |
|
| 145 |
return $model->ip; |
| 146 |
} else { |
| 147 |
return 'expired'; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|