PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
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 
22 namespace pocketmine\permission;
23 
26 
27 class BanList{
28 
30  private $list = [];
31 
33  private $file;
34 
36  private $enabled = true;
37 
41  public function __construct($file){
42  $this->file = $file;
43  }
44 
48  public function isEnabled(){
49  return $this->enabled === true;
50  }
51 
55  public function setEnabled($flag){
56  $this->enabled = (bool) $flag;
57  }
58 
62  public function getEntries(){
63  $this->removeExpired();
64 
65  return $this->list;
66  }
67 
73  public function isBanned($name){
74  $name = strtolower($name);
75  if(!$this->isEnabled()){
76  return false;
77  }else{
78  $this->removeExpired();
79 
80  return isset($this->list[$name]);
81  }
82  }
83 
87  public function add(BanEntry $entry){
88  $this->list[$entry->getName()] = $entry;
89  $this->save();
90  }
91 
100  public function addBan($target, $reason = null, $expires = null, $source = null){
101  $entry = new BanEntry($target);
102  $entry->setSource($source != null ? $source : $entry->getSource());
103  $entry->setExpires($expires);
104  $entry->setReason($reason != null ? $reason : $entry->getReason());
105 
106  $this->list[$entry->getName()] = $entry;
107  $this->save();
108 
109  return $entry;
110  }
111 
115  public function remove($name){
116  $name = strtolower($name);
117  if(isset($this->list[$name])){
118  unset($this->list[$name]);
119  $this->save();
120  }
121  }
122 
123  public function removeExpired(){
124  foreach($this->list as $name => $entry){
125  if($entry->hasExpired()){
126  unset($this->list[$name]);
127  }
128  }
129  }
130 
131  public function load(){
132  $this->list = [];
133  $fp = @fopen($this->file, "r");
134  if(is_resource($fp)){
135  while(($line = fgets($fp)) !== false){
136  if($line{0} !== "#"){
137  $entry = BanEntry::fromString($line);
138  if($entry instanceof BanEntry){
139  $this->list[$entry->getName()] = $entry;
140  }
141  }
142  }
143  fclose($fp);
144  }else{
145  MainLogger::getLogger()->error("Could not load ban list");
146  }
147  }
148 
149  public function save($flag = true){
150  $this->removeExpired();
151  $fp = @fopen($this->file, "w");
152  if(is_resource($fp)){
153  if($flag === true){
154  fwrite($fp, "# Updated " . strftime("%x %H:%M", time()) . " by " . Server::getInstance()->getName() . " " . Server::getInstance()->getPocketMineVersion() . "\n");
155  fwrite($fp, "# victim name | ban date | banned by | banned until | reason\n\n");
156  }
157 
158  foreach($this->list as $entry){
159  fwrite($fp, $entry->getString() . "\n");
160  }
161  fclose($fp);
162  }else{
163  MainLogger::getLogger()->error("Could not save ban list");
164  }
165  }
166 
167 }
add(BanEntry $entry)
Definition: BanList.php:87
Definition: BanEntry.php:24
static getInstance()
Definition: Server.php:1444
addBan($target, $reason=null, $expires=null, $source=null)
Definition: BanList.php:100
static fromString($str)
Definition: BanEntry.php:105