diff options
Diffstat (limited to 'Digitigrade/Policy')
| -rw-r--r-- | Digitigrade/Policy/BasicCensor.php | 47 | ||||
| -rw-r--r-- | Digitigrade/Policy/BlockInstance.php | 37 |
2 files changed, 84 insertions, 0 deletions
diff --git a/Digitigrade/Policy/BasicCensor.php b/Digitigrade/Policy/BasicCensor.php new file mode 100644 index 0000000..5866488 --- /dev/null +++ b/Digitigrade/Policy/BasicCensor.php @@ -0,0 +1,47 @@ +<?php +namespace Digitigrade\Policy; + +use Digitigrade\Model\Actor; +use Digitigrade\Model\Note; +use Digitigrade\Policy; + +/** + * Simple word censorship policy that replaces all instances of some text with another string. + * + * Suffers from the Scunthorpe problem. + * + * Can also be (ab)used to replace some word with another word for humorous effect :3 + */ +class BasicCensor extends Policy { + /** @var string[] */ + private array $words; + private string $replacement; + + public function __construct(array $words, string $replacement) { + $this->words = $words; + $this->replacement = $replacement; + } + + public function checkNote(Note $note): bool { + foreach ($this->words as $w) { + if (isset($note->summary)) + $note->summary = str_replace($w, $this->replacement, $note->summary); + $note->plainContent = str_replace($w, $this->replacement, $note->plainContent); + foreach ($note->formattedContent as $type => $content) { + $note->formattedContent[$type] = str_replace($w, $this->replacement, $content); + } + } + return true; + } + + public function checkActor(Actor $actor): bool { + foreach ($this->words as $w) { + $actor->displayName = str_replace($w, $this->replacement, $actor->displayName); + if (isset($actor->pronouns)) + $actor->pronouns = str_replace($w, $this->replacement, $actor->pronouns); + if (isset($actor->bio)) + $actor->bio = str_replace($w, $this->replacement, $actor->bio); + } + return true; + } +}
\ No newline at end of file diff --git a/Digitigrade/Policy/BlockInstance.php b/Digitigrade/Policy/BlockInstance.php new file mode 100644 index 0000000..5f902f2 --- /dev/null +++ b/Digitigrade/Policy/BlockInstance.php @@ -0,0 +1,37 @@ +<?php +namespace Digitigrade\Policy; + +use Digitigrade\Model\Actor; +use Digitigrade\Model\Interaction; +use Digitigrade\Model\Note; +use Digitigrade\Model\PushableModel; +use Digitigrade\Policy; + +class BlockInstance extends Policy { + /** @var string[] */ + private array $blockedHosts; + + public function __construct(string ...$hosts) { + $this->blockedHosts = $hosts; + } + + private function isBlocked(PushableModel $obj): bool { + $objHost = hostname_from_uri($obj->uri); + foreach ($this->blockedHosts as $host) { + if (str_ends_with($objHost, $host)) { + return true; + } + } + return false; + } + + public function checkActor(Actor $actor): bool { + return !$this->isBlocked($actor); + } + public function checkInteraction(Interaction $interaction): bool { + return !$this->isBlocked($interaction); + } + public function checkNote(Note $note): bool { + return !$this->isBlocked($note); + } +}
\ No newline at end of file |
