<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
class DomainPatternBlocklist
{
- const DEFAULT_REASON = 'blocked';
-
- /**
- * @var IManageConfigValues
- */
+ /** @var IManageConfigValues */
private $config;
public function __construct(IManageConfigValues $config)
}
/**
- * @param string $pattern
- * @param string|null $reason
+ * @param string $pattern
+ * @param string $reason
+ *
* @return int 0 if the block list couldn't be saved, 1 if the pattern was added, 2 if it was updated in place
*/
- public function addPattern(string $pattern, string $reason = null): int
+ public function addPattern(string $pattern, string $reason): int
{
$update = false;
if ($blocked['domain'] === $pattern) {
$blocklist[] = [
'domain' => $pattern,
- 'reason' => $reason ?? self::DEFAULT_REASON,
+ 'reason' => $reason,
];
$update = true;
if (!$update) {
$blocklist[] = [
'domain' => $pattern,
- 'reason' => $reason ?? self::DEFAULT_REASON,
+ 'reason' => $reason,
];
}
/**
* @param string $pattern
+ *
* @return int 0 if the block list couldn't be saved, 1 if the pattern wasn't found, 2 if it was removed
*/
public function removePattern(string $pattern): int
return $found ? ($this->set($blocklist) ? 2 : 0) : 1;
}
+ /**
+ * @param string $filename
+ *
+ * @return void
+ * @throws Exception
+ */
public function exportToFile(string $filename)
{
$fp = fopen($filename, 'w');
* Appends to the local block list all the patterns from the provided list that weren't already present.
*
* @param array $blocklist
+ *
* @return int The number of patterns actually added to the block list
*/
public function append(array $blocklist): int
* Extracts a server domain pattern block list from the provided CSV file name. Deduplicates the list based on patterns.
*
* @param string $filename
+ *
* @return array
* @throws Exception
*/
$blocklist = [];
while (($data = fgetcsv($fp, 1000)) !== false) {
- $domain = $data[0];
- if (count($data) == 0) {
- $reason = self::DEFAULT_REASON;
- } else {
- $reason = $data[1];
- }
-
- $data = [
- 'domain' => $domain,
- 'reason' => $reason
+ $item = [
+ 'domain' => $data[0],
+ 'reason' => $data[1] ?? '',
];
- if (!in_array($data, $blocklist)) {
- $blocklist[] = $data;
+ if (!in_array($item, $blocklist)) {
+ $blocklist[] = $item;
}
}