PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
TimingsCommand.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\command\defaults;
25
39use Symfony\Component\Filesystem\Path;
40use function count;
41use function fclose;
42use function file_exists;
43use function fopen;
44use function fseek;
45use function fwrite;
46use function http_build_query;
47use function is_array;
48use function json_decode;
49use function mkdir;
50use function stream_get_contents;
51use function strtolower;
52use const CURLOPT_AUTOREFERER;
53use const CURLOPT_FOLLOWLOCATION;
54use const CURLOPT_HTTPHEADER;
55use const CURLOPT_POST;
56use const CURLOPT_POSTFIELDS;
57use const PHP_EOL;
58
60
61 public function __construct(){
62 parent::__construct(
63 "timings",
64 KnownTranslationFactory::pocketmine_command_timings_description(),
65 KnownTranslationFactory::pocketmine_command_timings_usage()
66 );
67 $this->setPermission(DefaultPermissionNames::COMMAND_TIMINGS);
68 }
69
70 public function execute(CommandSender $sender, string $commandLabel, array $args){
71 if(count($args) !== 1){
73 }
74
75 $mode = strtolower($args[0]);
76
77 if($mode === "on"){
78 if(TimingsHandler::isEnabled()){
79 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_alreadyEnabled());
80 return true;
81 }
82 TimingsHandler::setEnabled();
83 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_enable());
84
85 return true;
86 }elseif($mode === "off"){
87 TimingsHandler::setEnabled(false);
88 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_disable());
89 return true;
90 }
91
92 if(!TimingsHandler::isEnabled()){
93 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_timings_timingsDisabled());
94
95 return true;
96 }
97
98 $paste = $mode === "paste";
99
100 if($mode === "reset"){
101 TimingsHandler::reload();
102 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_reset());
103 }elseif($mode === "merged" || $mode === "report" || $paste){
104 $timings = "";
105 if($paste){
106 $fileTimings = Utils::assumeNotFalse(fopen("php://temp", "r+b"), "Opening php://temp should never fail");
107 }else{
108 $index = 0;
109 $timingFolder = Path::join($sender->getServer()->getDataPath(), "timings");
110
111 if(!file_exists($timingFolder)){
112 mkdir($timingFolder, 0777);
113 }
114 $timings = Path::join($timingFolder, "timings.txt");
115 while(file_exists($timings)){
116 $timings = Path::join($timingFolder, "timings" . (++$index) . ".txt");
117 }
118
119 $fileTimings = fopen($timings, "a+b");
120 }
122 foreach($lines as $line){
123 fwrite($fileTimings, $line . PHP_EOL);
124 }
125
126 if($paste){
127 fseek($fileTimings, 0);
128 $data = [
129 "browser" => $agent = $sender->getServer()->getName() . " " . $sender->getServer()->getPocketMineVersion(),
130 "data" => $content = stream_get_contents($fileTimings)
131 ];
132 fclose($fileTimings);
133
134 $host = $sender->getServer()->getConfigGroup()->getPropertyString(YmlServerProperties::TIMINGS_HOST, "timings.pmmp.io");
135
136 $sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask(
138 "https://$host?upload=true",
139 10,
140 [],
141 [
142 CURLOPT_HTTPHEADER => [
143 "User-Agent: $agent",
144 "Content-Type: application/x-www-form-urlencoded"
145 ],
146 CURLOPT_POST => true,
147 CURLOPT_POSTFIELDS => http_build_query($data),
148 CURLOPT_AUTOREFERER => false,
149 CURLOPT_FOLLOWLOCATION => false
150 ]
151 )],
152 function(array $results) use ($sender, $host) : void{
154 if($sender instanceof Player && !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
155 return;
156 }
157 $result = $results[0];
158 if($result instanceof InternetException){
159 $sender->getServer()->getLogger()->logException($result);
160 return;
161 }
162 $response = json_decode($result->getBody(), true);
163 if(is_array($response) && isset($response["id"])){
164 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsRead(
165 "https://" . $host . "/?id=" . $response["id"]));
166 }else{
167 $sender->getServer()->getLogger()->debug("Invalid response from timings server (" . $result->getCode() . "): " . $result->getBody());
168 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_pasteError());
169 }
170 }
171 ));
172 }else{
173 fclose($fileTimings);
174 Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_command_timings_timingsWrite($timings));
175 }
176 }else{
178 }
179
180 return true;
181 }
182}
execute(CommandSender $sender, string $commandLabel, array $args)
static assumeNotFalse(mixed $value, \Closure|string $context="This should never be false")
Definition: Utils.php:623