View source
<?php
namespace Drupal\ip;
class IpTracker {
private $connection;
private $account;
private $request;
function __construct(\Drupal\Core\Database\Connection $connection, \Symfony\Component\HttpFoundation\Request $request, \Drupal\Core\Session\AccountInterface $account) {
$this->connection = $connection;
$this->request = $request;
$this->account = $account;
}
function save() {
$ip = $this->request
->getClientIp();
$uid = $this->account
->id();
$iplong = ip2long($ip);
if (!empty($iplong)) {
$sql = "SELECT visits FROM {ip_tracker} WHERE uid = :uid AND ip = :ip";
$args = array(
':uid' => $uid,
':ip' => $iplong,
);
$visits = $this->connection
->query($sql, $args)
->fetchField();
if ($visits) {
return $this->connection
->update('ip_tracker')
->fields(array(
'visits' => $visits + 1,
'last_visit' => REQUEST_TIME,
))
->condition('uid', $uid)
->condition('ip', $iplong)
->execute();
}
else {
return $this->connection
->insert('ip_tracker')
->fields(array(
'uid' => $uid,
'ip' => $iplong,
'visits' => 1,
'first_visit' => REQUEST_TIME,
'last_visit' => REQUEST_TIME,
))
->execute();
}
}
}
function remove() {
return $this->connection
->delete('ip_tracker')
->condition('uid', $this->account
->id())
->execute();
}
}