86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace pedodev\linkprotection\core;
|
|
|
|
use phpbb\config\config;
|
|
|
|
class automatic_link_helper
|
|
{
|
|
private string $filepath;
|
|
private array $automatic_links = array();
|
|
|
|
public function __construct(config $config)
|
|
{
|
|
$this->filepath = __DIR__ . "/../automatic_links_{$config['pedodev_linkprotection_fileprefix']}.json";
|
|
|
|
if ($config['pedodev_linkprotection_automaticenabled'])
|
|
{
|
|
$this->load_automatic_links();
|
|
}
|
|
}
|
|
|
|
public function load_automatic_links(): void
|
|
{
|
|
if (!file_exists($this->filepath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
$automatic_links_json = file_get_contents($this->filepath);
|
|
var_dump($automatic_links_json);
|
|
|
|
if ($automatic_links_json && ctype_print($automatic_links_json))
|
|
{
|
|
$this->automatic_links = json_decode($automatic_links_json, $associative = true);
|
|
}
|
|
}
|
|
|
|
public function save_automatic_links(array $automatic_links): void
|
|
{
|
|
if (!touch($this->filepath) || !chmod($this->filepath, 0600))
|
|
{
|
|
trigger_error("Unable to touch automatic link JSON file. Please check your file and ownership permissions", E_USER_ERROR);
|
|
}
|
|
|
|
$automatic_links_json = json_encode($automatic_links);
|
|
|
|
if (!$automatic_links_json || !ctype_print($automatic_links_json))
|
|
{
|
|
trigger_error("Unable to encode automatic link list to JSON format", E_USER_ERROR);
|
|
}
|
|
|
|
if (file_put_contents($this->filepath, $automatic_links_json) === false)
|
|
{
|
|
trigger_error("Unable to write automatic link list to file", E_USER_ERROR);
|
|
}
|
|
|
|
if (!chmod($this->filepath, 0400))
|
|
{
|
|
trigger_error("Unable to set automatic link JSON file permissions", E_USER_WARNING);
|
|
}
|
|
|
|
$this->automatic_links = $automatic_links;
|
|
}
|
|
|
|
public function get_automatic_links(): array
|
|
{
|
|
return $this->automatic_links;
|
|
}
|
|
|
|
/*
|
|
* Checks whether the given link contains a substring match in any of the automatic links configured in the admin CP
|
|
* Returns the title of the automatic link if a match is found, otherwise returns null
|
|
*/
|
|
public function find_substring_match(string $link): ?string
|
|
{
|
|
foreach ($this->automatic_links as $url => $title)
|
|
{
|
|
if (str_contains($link, $url))
|
|
{
|
|
return $title;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|