| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Hurrytimer\Utils\Helpers; |
| 7 |
|
| 8 |
class IP_Detection |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Evergreen IP entries table name. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $table; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var int $campaignId |
| 19 |
*/ |
| 20 |
protected $campaignId; |
| 21 |
|
| 22 |
public function __construct( $campaignId ) |
| 23 |
{ |
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
$this->table = "{$wpdb->prefix}hurrytimer_evergreen"; |
| 27 |
$this->campaignId = $campaignId; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Find IP log. |
| 33 |
* |
| 34 |
* @return array|null |
| 35 |
*/ |
| 36 |
public function getCurrentUserEndDate() |
| 37 |
{ |
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
$ipAddress = $this->fetchCurrentUserIpAddress(); |
| 41 |
|
| 42 |
try { |
| 43 |
$sql = $wpdb->prepare( |
| 44 |
"SELECT id, client_expires_at FROM {$this->table} |
| 45 |
WHERE countdown_id = %d |
| 46 |
AND client_ip_address = %s", |
| 47 |
$this->campaignId, |
| 48 |
$ipAddress |
| 49 |
); |
| 50 |
|
| 51 |
/** |
| 52 |
* @var object{id, client_expires_at} $result |
| 53 |
*/ |
| 54 |
$found = $wpdb->get_row( $sql ); |
| 55 |
|
| 56 |
if ( !$found ) { |
| 57 |
$found = $this->getCurrentUserEndDateLegacy( $this->campaignId, $ipAddress ); |
| 58 |
} |
| 59 |
if ( !$found ) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
return [ $found->id, $found->client_expires_at ]; |
| 64 |
} catch ( Exception $e ) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public function fetchCurrentUserIpAddress() |
| 70 |
{ |
| 71 |
return Helpers::ip_address(); |
| 72 |
} |
| 73 |
|
| 74 |
public function getCurrentUserResetToken() |
| 75 |
{ |
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
$ipAddress = $this->fetchCurrentUserIpAddress(); |
| 79 |
|
| 80 |
try { |
| 81 |
$sql = $wpdb->prepare( |
| 82 |
"SELECT id, reset_token FROM {$this->table} |
| 83 |
WHERE countdown_id = %d |
| 84 |
AND client_ip_address = %s", |
| 85 |
$this->campaignId, |
| 86 |
$ipAddress |
| 87 |
); |
| 88 |
|
| 89 |
$found = $wpdb->get_row( $sql ); |
| 90 |
if ( !$found ) { |
| 91 |
$found = $this->getCurrentUserEndDateLegacy( $this->campaignId, $ipAddress ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( empty( $found ) || !is_object( $found ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
return [ $found->id, $found->reset_token ]; |
| 99 |
|
| 100 |
} catch ( Exception $e ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* This provide compatibility for prior versions. |
| 108 |
* |
| 109 |
* @param int $campaignId |
| 110 |
* @param string $ipAddress |
| 111 |
* |
| 112 |
* @return array|null |
| 113 |
* |
| 114 |
*/ |
| 115 |
private function getCurrentUserEndDateLegacy( $campaignId, $ipAddress ) |
| 116 |
{ |
| 117 |
$_ipAddress = sanitize_key( $ipAddress ); |
| 118 |
|
| 119 |
$transient = sprintf( 'ht_cdt_%d_%s', $campaignId, $_ipAddress ); |
| 120 |
|
| 121 |
$endDateTS = get_transient( $transient ); |
| 122 |
|
| 123 |
if ( !$endDateTS ) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
$created = $this->create( $campaignId, $endDateTS ); |
| 127 |
|
| 128 |
if ( $created ) { |
| 129 |
// Clear transients. |
| 130 |
delete_transient( $transient ); |
| 131 |
delete_transient( "{$transient}_status" ); |
| 132 |
|
| 133 |
return $this->getCurrentUserEndDate(); |
| 134 |
} |
| 135 |
|
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Create a new IP log. |
| 141 |
* |
| 142 |
* @param int $campaignId |
| 143 |
* @param int $endDateTS |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public function create( $campaignId, $endDateTS ) |
| 147 |
{ |
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
// Auto-destroy after one month. |
| 151 |
$destroy_at = Helpers::date_later( MONTH_IN_SECONDS ); |
| 152 |
|
| 153 |
$result = $wpdb->insert( |
| 154 |
$this->table, |
| 155 |
[ |
| 156 |
'countdown_id' => $campaignId, |
| 157 |
'client_ip_address' => $this->fetchCurrentUserIpAddress(), |
| 158 |
'client_expires_at' => $endDateTS, |
| 159 |
'destroy_at' => $destroy_at |
| 160 |
] |
| 161 |
); |
| 162 |
|
| 163 |
return $result !== false; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* |
| 168 |
* Update client expiration time. |
| 169 |
* |
| 170 |
* @param $id |
| 171 |
* @param $client_expires_at |
| 172 |
* |
| 173 |
* @return false|int |
| 174 |
*/ |
| 175 |
public function update( $id, $client_expires_at ) |
| 176 |
{ |
| 177 |
global $wpdb; |
| 178 |
|
| 179 |
$destroy_at = Helpers::date_later( MONTH_IN_SECONDS ); |
| 180 |
|
| 181 |
return $wpdb->update( |
| 182 |
$this->table, |
| 183 |
compact( 'client_expires_at', 'destroy_at' ), |
| 184 |
compact( 'id' ) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
public function updateOrCreate( $id, $endDateTS ) |
| 189 |
{ |
| 190 |
if ( empty( $id ) ) { |
| 191 |
return $this->create( $this->campaignId, $endDateTS ); |
| 192 |
} else { |
| 193 |
$this->update( $id, $endDateTS ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public function updateCurrentUserResetToken( $id, $reset_token ) |
| 198 |
{ |
| 199 |
global $wpdb; |
| 200 |
|
| 201 |
if ( empty( $id ) ) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
return $wpdb->update( |
| 205 |
$this->table, |
| 206 |
compact( 'reset_token' ), |
| 207 |
compact( 'id' ) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Delete given countdown and IP entry. |
| 213 |
* |
| 214 |
* @param int $campaignId |
| 215 |
* @param bool $currentUser |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
public function forget( $campaignId, $currentUser = false ) |
| 219 |
{ |
| 220 |
global $wpdb; |
| 221 |
$where = [ 'countdown_id' => $campaignId ]; |
| 222 |
if ( $currentUser ) { |
| 223 |
$where[ 'client_ip_address' ] = $this->fetchCurrentUserIpAddress(); |
| 224 |
} |
| 225 |
$wpdb->delete( $this->table, $where ); |
| 226 |
} |
| 227 |
|
| 228 |
function forgetAll( $currentUser = false ) |
| 229 |
{ |
| 230 |
global $wpdb; |
| 231 |
if ( $currentUser ) { |
| 232 |
$where[ 'client_ip_address' ] = $this->fetchCurrentUserIpAddress(); |
| 233 |
$wpdb->delete( $this->table, $where ); |
| 234 |
} else { |
| 235 |
$wpdb->query( "delete from {$this->table}" ); |
| 236 |
} |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|