PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
26use function fclose;
27use function fgets;
28use function fopen;
29use function fwrite;
30use function is_resource;
31use function strtolower;
32use function trim;
33
34class BanList{
36 private array $list = [];
37
38 private bool $enabled = true;
39
40 public function __construct(
41 private string $file
42 ){}
43
44 public function isEnabled() : bool{
45 return $this->enabled;
46 }
47
48 public function setEnabled(bool $flag) : void{
49 $this->enabled = $flag;
50 }
51
52 public function getEntry(string $name) : ?BanEntry{
53 $this->removeExpired();
54
55 return $this->list[strtolower($name)] ?? null;
56 }
57
61 public function getEntries() : array{
62 $this->removeExpired();
63
64 return $this->list;
65 }
66
67 public function isBanned(string $name) : bool{
68 $name = strtolower($name);
69 if(!$this->isEnabled()){
70 return false;
71 }else{
72 $this->removeExpired();
73
74 return isset($this->list[$name]);
75 }
76 }
77
78 public function add(BanEntry $entry) : void{
79 $this->list[$entry->getName()] = $entry;
80 $this->save();
81 }
82
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());
88
89 $this->list[$entry->getName()] = $entry;
90 $this->save();
91
92 return $entry;
93 }
94
95 public function remove(string $name) : void{
96 $name = strtolower($name);
97 if(isset($this->list[$name])){
98 unset($this->list[$name]);
99 $this->save();
100 }
101 }
102
103 public function removeExpired() : void{
104 foreach($this->list as $name => $entry){
105 if($entry->hasExpired()){
106 unset($this->list[$name]);
107 }
108 }
109 }
110
111 public function load() : void{
112 $this->list = [];
113 $fp = @fopen($this->file, "r");
114 if(is_resource($fp)){
115 while(($line = fgets($fp)) !== false){
116 if($line[0] !== "#"){
117 try{
118 $entry = BanEntry::fromString($line);
119 if($entry !== null){
120 $this->list[$entry->getName()] = $entry;
121 }
122 }catch(\RuntimeException $e){
123 $logger = \GlobalLogger::get();
124 $logger->critical("Failed to parse ban entry from string \"" . trim($line) . "\": " . $e->getMessage());
125 }
126
127 }
128 }
129 fclose($fp);
130 }else{
131 \GlobalLogger::get()->error("Could not load ban list");
132 }
133 }
134
135 public function save(bool $writeHeader = true) : void{
136 $this->removeExpired();
137 $fp = @fopen($this->file, "w");
138 if(is_resource($fp)){
139 if($writeHeader){
140 fwrite($fp, "# victim name | ban date | banned by | banned until | reason\n\n");
141 }
142
143 foreach($this->list as $entry){
144 fwrite($fp, $entry->getString() . "\n");
145 }
146 fclose($fp);
147 }else{
148 \GlobalLogger::get()->error("Could not save ban list");
149 }
150 }
151}
Definition: BanEntry.php:35