98 lines
2.4 KiB
PHP
Executable file
98 lines
2.4 KiB
PHP
Executable file
<?php
|
|
|
|
/*
|
|
* The cache helper is used by the protected link page when the 'multiple links' option is enabled.
|
|
* It handles reading and writing to the cache the data object which stores the number of remaining links and the remaining time a user has after solving a captcha
|
|
* data['links'] is the number of remaining links, while data['time'] is a UNIX timestamp containing the time at which the duration of the object expires
|
|
*/
|
|
|
|
namespace pedodev\linkprotection\core;
|
|
|
|
use phpbb\cache\driver\driver_interface;
|
|
use phpbb\user;
|
|
use phpbb\config\config;
|
|
|
|
class cache_helper
|
|
{
|
|
private string $name;
|
|
private array $data;
|
|
|
|
public function __construct(
|
|
private driver_interface $cache,
|
|
private user $user,
|
|
private config $config,
|
|
) {}
|
|
|
|
private function write_object(int $time): void
|
|
{
|
|
$this->cache->put(
|
|
$this->name,
|
|
$this->data,
|
|
$time,
|
|
);
|
|
}
|
|
|
|
/*
|
|
* Read from the cache and store it in $this->data
|
|
*/
|
|
public function read_object(): void
|
|
{
|
|
if (!$this->config['pedodev_linkprotection_multiplelinks'])
|
|
{
|
|
return;
|
|
}
|
|
|
|
$this->name = 'linkprotection_' . $this->user->data['user_id'];
|
|
$this->data = array();
|
|
|
|
if ($this->cache->_exists($this->name))
|
|
{
|
|
$this->data = $this->cache->get($this->name);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Called when a user solves a captcha. Stores the number of links (depends on their group) and the duration in the cache
|
|
*/
|
|
public function save_object(int $num_links): void
|
|
{
|
|
if (!$this->config['pedodev_linkprotection_multiplelinks'] || $num_links <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
$save_duration = (int)$this->config['pedodev_linkprotection_solveduration'];
|
|
$this->name = 'linkprotection_' . $this->user->data['user_id'];
|
|
|
|
$this->data = array(
|
|
'links' => $num_links,
|
|
'time' => time() + $save_duration,
|
|
);
|
|
|
|
$this->write_object($save_duration);
|
|
}
|
|
|
|
/*
|
|
* Called when a user visits a link without solving a captcha. Decreases their link 'allowance' by 1.
|
|
*/
|
|
public function decrement_remaining_links(): void
|
|
{
|
|
if (empty($this->data) || !isset($this->data['links']))
|
|
{
|
|
return;
|
|
}
|
|
|
|
$this->data['links'] -= 1;
|
|
$this->write_object($this->get_remaining_time());
|
|
}
|
|
|
|
public function get_remaining_links(): int
|
|
{
|
|
return ((isset($this->data) && isset($this->data['links'])) ? $this->data['links'] : -1);
|
|
}
|
|
|
|
public function get_remaining_time(): int
|
|
{
|
|
return ((isset($this->data) && isset($this->data['time'])) ? ($this->data['time'] - time()) : -1);
|
|
}
|
|
}
|