36 private array $list = [];
38 private bool $enabled =
true;
40 public function __construct(
44 public function isEnabled() :
bool{
45 return $this->enabled;
48 public function setEnabled(
bool $flag) :
void{
49 $this->enabled = $flag;
52 public function getEntry(
string $name) : ?
BanEntry{
53 $this->removeExpired();
55 return $this->list[strtolower($name)] ??
null;
62 $this->removeExpired();
67 public function isBanned(
string $name) : bool{
68 $name = strtolower($name);
69 if(!$this->isEnabled()){
72 $this->removeExpired();
74 return isset($this->list[$name]);
78 public function add(BanEntry $entry) : void{
79 $this->list[$entry->getName()] = $entry;
83 public function addBan(
string $target, ?
string $reason =
null, ?\DateTime $expires =
null, ?
string $source =
null) : BanEntry{
84 $entry = new BanEntry($target);
85 $entry->setSource($source ?? $entry->getSource());
86 $entry->setExpires($expires);
87 $entry->setReason($reason ?? $entry->getReason());
89 $this->list[$entry->getName()] = $entry;
95 public function remove(
string $name) :
void{
96 $name = strtolower($name);
97 if(isset($this->list[$name])){
98 unset($this->list[$name]);
103 public function removeExpired() : void{
104 foreach($this->list as $name => $entry){
105 if($entry->hasExpired()){
106 unset($this->list[$name]);
111 public function load() : void{
113 $fp = @fopen($this->file,
"r");
114 if(is_resource($fp)){
115 while(($line = fgets($fp)) !==
false){
116 if($line[0] !==
"#"){
118 $entry = BanEntry::fromString($line);
120 $this->list[$entry->getName()] = $entry;
122 }
catch(\RuntimeException $e){
123 $logger = \GlobalLogger::get();
124 $logger->critical(
"Failed to parse ban entry from string \"" . trim($line) .
"\": " . $e->getMessage());
131 \GlobalLogger::get()->error(
"Could not load ban list");
135 public function save(
bool $writeHeader =
true) : void{
136 $this->removeExpired();
137 $fp = @fopen($this->file,
"w");
138 if(is_resource($fp)){
140 fwrite($fp,
"# victim name | ban date | banned by | banned until | reason\n\n");
143 foreach($this->list as $entry){
144 fwrite($fp, $entry->getString() .
"\n");
148 \GlobalLogger::get()->error(
"Could not save ban list");