PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
BanList.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\permission;
25
27use function fclose;
28use function fgets;
29use function fopen;
30use function fwrite;
31use function is_resource;
32use function strtolower;
33use function trim;
34
35class BanList{
40 private array $list = [];
41
42 private bool $enabled = true;
43
44 public function __construct(
45 private string $file
46 ){}
47
48 public function isEnabled() : bool{
49 return $this->enabled;
50 }
51
52 public function setEnabled(bool $flag) : void{
53 $this->enabled = $flag;
54 }
55
56 public function getEntry(string $name) : ?BanEntry{
57 $this->removeExpired();
58
59 return $this->list[strtolower($name)] ?? null;
60 }
61
65 public function getEntries() : array{
66 $this->removeExpired();
67
68 return $this->list;
69 }
70
71 public function isBanned(string $name) : bool{
72 $name = strtolower($name);
73 if(!$this->isEnabled()){
74 return false;
75 }else{
76 $this->removeExpired();
77
78 return isset($this->list[$name]);
79 }
80 }
81
82 public function add(BanEntry $entry) : void{
83 $this->list[$entry->getName()] = $entry;
84 $this->save();
85 }
86
87 public function addBan(string $target, ?string $reason = null, ?\DateTime $expires = null, ?string $source = null) : BanEntry{
88 $entry = new BanEntry($target);
89 $entry->setSource($source ?? $entry->getSource());
90 $entry->setExpires($expires);
91 $entry->setReason($reason ?? $entry->getReason());
92
93 $this->list[$entry->getName()] = $entry;
94 $this->save();
95
96 return $entry;
97 }
98
99 public function remove(string $name) : void{
100 $name = strtolower($name);
101 if(isset($this->list[$name])){
102 unset($this->list[$name]);
103 $this->save();
104 }
105 }
106
107 public function removeExpired() : void{
108 foreach(Utils::promoteKeys($this->list) as $name => $entry){
109 if($entry->hasExpired()){
110 unset($this->list[$name]);
111 }
112 }
113 }
114
115 public function load() : void{
116 $this->list = [];
117 $fp = @fopen($this->file, "r");
118 if(is_resource($fp)){
119 while(($line = fgets($fp)) !== false){
120 if($line[0] !== "#"){
121 try{
122 $entry = BanEntry::fromString($line);
123 if($entry !== null){
124 $this->list[$entry->getName()] = $entry;
125 }
126 }catch(\RuntimeException $e){
127 $logger = \GlobalLogger::get();
128 $logger->critical("Failed to parse ban entry from string \"" . trim($line) . "\": " . $e->getMessage());
129 }
130
131 }
132 }
133 fclose($fp);
134 }else{
135 \GlobalLogger::get()->error("Could not load ban list");
136 }
137 }
138
139 public function save(bool $writeHeader = true) : void{
140 $this->removeExpired();
141 $fp = @fopen($this->file, "w");
142 if(is_resource($fp)){
143 if($writeHeader){
144 fwrite($fp, "# victim name | ban date | banned by | banned until | reason\n\n");
145 }
146
147 foreach($this->list as $entry){
148 fwrite($fp, $entry->getString() . "\n");
149 }
150 fclose($fp);
151 }else{
152 \GlobalLogger::get()->error("Could not save ban list");
153 }
154 }
155}
Definition BanEntry.php:35