| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class IP_Detection |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Evergreen IP entries table name. |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $table; |
| 13 |
|
| 14 |
/** |
| 15 |
* Cleanup idle IP entries action name. |
| 16 |
*/ |
| 17 |
const CLEANUP_ACTION = "hurrytimer_evergreen_daily_cleanup"; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
global $wpdb; |
| 22 |
$this->table = "{$wpdb->prefix}hurrytimer_evergreen"; |
| 23 |
add_action(self::CLEANUP_ACTION, [__CLASS__, '_cleanup_idle_entries']); |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get IP log. |
| 29 |
* |
| 30 |
* @param int $countdown_id |
| 31 |
* @param string $client_ip_address |
| 32 |
* |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function find($countdown_id, $client_ip_address = null) |
| 36 |
{ |
| 37 |
global $wpdb; |
| 38 |
if (is_null($client_ip_address)) { |
| 39 |
$client_ip_address = Helper::ip_address(); |
| 40 |
} |
| 41 |
$sql = $wpdb->prepare(implode(' ', [ |
| 42 |
"SELECT * FROM {$this->table}", |
| 43 |
"WHERE countdown_id = %d", |
| 44 |
"AND client_ip_address = %s", |
| 45 |
]), |
| 46 |
$countdown_id, $client_ip_address); |
| 47 |
$result = $wpdb->get_row($sql, ARRAY_A); |
| 48 |
if (!$result) { |
| 49 |
$result = $this->_find_compat($countdown_id, $client_ip_address); |
| 50 |
} |
| 51 |
|
| 52 |
return $result; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* This provide compatibility for prior versions. |
| 57 |
* |
| 58 |
* @param int $countdown_id |
| 59 |
* @param string $client_id_address |
| 60 |
* @return void |
| 61 |
* |
| 62 |
*/ |
| 63 |
public function _find_compat($countdown_id, $client_id_address) |
| 64 |
{ |
| 65 |
$client_id_address_key = sanitize_key($client_id_address); |
| 66 |
$transient = sprintf('ht_cdt_%d_%s', $countdown_id, |
| 67 |
$client_id_address_key); |
| 68 |
$expires_at_transient = get_transient($transient); |
| 69 |
if (!$expires_at_transient) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
$this->create($countdown_id, $expires_at_transient, $client_id_address); |
| 73 |
delete_transient($transient); |
| 74 |
delete_transient("{$transient}_status"); |
| 75 |
|
| 76 |
return $this->find($countdown_id, $client_id_address); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Create a new IP log. |
| 81 |
* |
| 82 |
* @param int $countdown_id |
| 83 |
* @param int $client_expires_at |
| 84 |
* @param string $client_ip_address |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function create( |
| 89 |
$countdown_id, |
| 90 |
$client_expires_at, |
| 91 |
$client_ip_address = null |
| 92 |
) { |
| 93 |
global $wpdb; |
| 94 |
|
| 95 |
if (is_null($client_ip_address)) { |
| 96 |
$client_ip_address = Helper::ip_address(); |
| 97 |
} |
| 98 |
|
| 99 |
// Set an auto-destroy datetime. |
| 100 |
$destroy_at = $this->_destroy_at(); |
| 101 |
|
| 102 |
$wpdb->insert($this->table, compact( |
| 103 |
'countdown_id', |
| 104 |
'client_ip_address', |
| 105 |
'client_expires_at', |
| 106 |
'destroy_at' |
| 107 |
)); |
| 108 |
|
| 109 |
$this->_maybe_schedule_destroy(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param $id |
| 114 |
* @param $client_expires_at |
| 115 |
* |
| 116 |
* @return false|int |
| 117 |
*/ |
| 118 |
public function update($id, $client_expires_at) |
| 119 |
{ |
| 120 |
global $wpdb; |
| 121 |
|
| 122 |
$destroy_at = $this->_destroy_at(); |
| 123 |
|
| 124 |
return $wpdb->update($this->table, |
| 125 |
compact('client_expires_at', 'destroy_at'), |
| 126 |
compact('id')); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Delete given countdown and IP entry. |
| 131 |
* |
| 132 |
* @param $countdown_id |
| 133 |
* @param null $client_ip_address |
| 134 |
*/ |
| 135 |
public function forget($countdown_id) |
| 136 |
{ |
| 137 |
global $wpdb; |
| 138 |
|
| 139 |
$delete = $wpdb->delete($this->table, |
| 140 |
compact('countdown_id')); |
| 141 |
|
| 142 |
if (!$this->_has_entries()) { |
| 143 |
$this->_cancel_schedule_destroy(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* IP entry TTL date. |
| 149 |
* @return false|string |
| 150 |
*/ |
| 151 |
private function _destroy_at() |
| 152 |
{ |
| 153 |
$now = current_time('mysql'); |
| 154 |
return date('Y-m-d H:i:s', strtotime($now) + MONTH_IN_SECONDS); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Clean up IP entries with passed `destroy_at`. |
| 159 |
*/ |
| 160 |
private function _cleanup_idle_entries() |
| 161 |
{ |
| 162 |
global $wpdb; |
| 163 |
$now = current_time('mysql'); |
| 164 |
$sql = "DELETE FROM {$this->table} WHERE destroy_at < $now"; |
| 165 |
$wpdb->query($sql); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Check if there is at least one IP entry. |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
private function _has_entries() |
| 173 |
{ |
| 174 |
global $wpdb; |
| 175 |
return $wpdb->get_var("SELECT count(*) FROM {$this->table}") > 0; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Schedule cleanup of IP entries with passed `destroy_at`. |
| 180 |
*/ |
| 181 |
private function _maybe_schedule_destroy() |
| 182 |
{ |
| 183 |
|
| 184 |
if (!wp_next_scheduled(self::CLEANUP_ACTION)) { |
| 185 |
wp_schedule_event(current_time('timestamp'), 'daily', |
| 186 |
self::CLEANUP_ACTION); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
private function _cancel_schedule_destroy() |
| 191 |
{ |
| 192 |
wp_clear_scheduled_hook(self::CLEANUP_ACTION); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|