PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
AutoUpdater.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\updater;
23 
29 
31 
33  protected $server;
34  protected $endpoint;
35  protected $hasUpdate = false;
36  protected $updateInfo = null;
37 
38  public function __construct(Server $server, $endpoint){
39  $this->server = $server;
40  $this->endpoint = "http://$endpoint/api/";
41 
42  if($server->getProperty("auto-updater.enabled", true)){
43  $this->check();
44  if($this->hasUpdate()){
45  if($this->server->getProperty("auto-updater.on-update.warn-console", true)){
46  $this->showConsoleUpdate();
47  }
48  }elseif($this->server->getProperty("auto-updater.preferred-channel", true)){
49  $version = new VersionString();
50  if(!$version->isDev() and $this->getChannel() !== "stable"){
51  $this->showChannelSuggestionStable();
52  }elseif($this->getChannel() === "stable"){
53  $this->showChannelSuggestionBeta();
54  }
55  }
56  }
57  }
58 
59  protected function check(){
60  $response = Utils::getURL($this->endpoint . "?channel=" . $this->getChannel(), 4);
61  $response = json_decode($response, true);
62  if(!is_array($response)){
63  return;
64  }
65 
66  $this->updateInfo = [
67  "version" => $response["version"],
68  "api_version" => $response["api_version"],
69  "build" => $response["build"],
70  "date" => $response["date"],
71  "details_url" => isset($response["details_url"]) ? $response["details_url"] : null,
72  "download_url" => $response["download_url"]
73  ];
74 
75  $this->checkUpdate();
76  }
77 
81  public function hasUpdate(){
82  return $this->hasUpdate;
83  }
84 
85  public function showConsoleUpdate(){
86  $logger = $this->server->getLogger();
87  $newVersion = new VersionString($this->updateInfo["version"]);
88  $logger->warning("----- PocketMine-MP Auto Updater -----");
89  $logger->warning("Your version of PocketMine-MP is out of date. Version " . $newVersion->get(false) . " (build #" . $newVersion->getBuild() . ") was released on " . date("D M j h:i:s Y", $this->updateInfo["date"]));
90  if($this->updateInfo["details_url"] !== null){
91  $logger->warning("Details: " . $this->updateInfo["details_url"]);
92  }
93  $logger->warning("Download: " . $this->updateInfo["download_url"]);
94  $logger->warning("----- -------------------------- -----");
95  }
96 
97  public function showPlayerUpdate(Player $player){
98  $player->sendMessage(TextFormat::DARK_PURPLE . "The version of PocketMine-MP that this server is running is out of date. Please consider updating to the latest version.");
99  $player->sendMessage(TextFormat::DARK_PURPLE . "Check the console for more details.");
100  }
101 
102  protected function showChannelSuggestionStable(){
103  $logger = $this->server->getLogger();
104  $logger->info("----- PocketMine-MP Auto Updater -----");
105  $logger->info("It appears you're running a Stable build, when you've specified that you prefer to run " . ucfirst($this->getChannel()) . " builds.");
106  $logger->info("If you would like to be kept informed about new Stable builds only, it is recommended that you change 'preferred-channel' in your pocketmine.yml to 'stable'.");
107  $logger->info("----- -------------------------- -----");
108  }
109 
110  protected function showChannelSuggestionBeta(){
111  $logger = $this->server->getLogger();
112  $logger->info("----- PocketMine-MP Auto Updater -----");
113  $logger->info("It appears you're running a Beta build, when you've specified that you prefer to run Stable builds.");
114  $logger->info("If you would like to be kept informed about new Beta or Development builds, it is recommended that you change 'preferred-channel' in your pocketmine.yml to 'beta' or 'development'.");
115  $logger->info("----- -------------------------- -----");
116  }
117 
118  public function getUpdateInfo(){
119  return $this->updateInfo;
120  }
121 
122  public function doCheck(){
123  $this->check();
124  }
125 
126  protected function checkUpdate(){
127  if($this->updateInfo === null){
128  return;
129  }
130  $currentVersion = new VersionString($this->server->getPocketMineVersion());
131  $newVersion = new VersionString($this->updateInfo["version"]);
132 
133  if($currentVersion->compare($newVersion) > 0 and ($currentVersion->get() !== $newVersion->get() or $currentVersion->getBuild() > 0)){
134  $this->hasUpdate = true;
135  }else{
136  $this->hasUpdate = false;
137  }
138 
139  }
140 
141  public function getChannel(){
142  $channel = strtolower($this->server->getProperty("auto-updater.preferred-channel", "stable"));
143  if($channel !== "stable" and $channel !== "beta" and $channel !== "development"){
144  $channel = "stable";
145  }
146 
147  return $channel;
148  }
149 }
static getURL($page, $timeout=10)
Definition: Utils.php:338
getProperty($variable, $defaultValue=null)
Definition: Server.php:1243