Online/src/Ad5001/Online/Main.php

73 lines
3.2 KiB
PHP
Raw Normal View History

2016-05-29 17:08:23 +00:00
<?php
2016-07-30 16:12:56 +00:00
namespace Ad5001\Online;
2016-05-29 17:08:23 +00:00
use pocketmine\command\CommandSender;
use pocketmine\command\Command;
use pocketmine\event\Listener;
use pocketmine\plugin\PluginBase;
use pocketmine\network\upnp\UPnP;
2016-05-29 17:08:23 +00:00
use pocketmine\Server;
use pocketmine\Player;
2016-05-29 17:08:23 +00:00
class Main extends PluginBase{
public function onEnable(){
$this->saveDefaultConfig();
if(!file_exists($this->getDataFolder() . "index.html")) {
file_put_contents($this->getDataFolder() . "index.html", $this->getResource("index.html"));
}
if(!stream_resolve_include_path("router.php")) {
file_put_contents($this->getDataFolder() . "router.php", $this->getResource("handler.php"));
}
2016-05-29 17:08:23 +00:00
if(!file_exists($this->getDataFolder() . "404.html")) {
file_put_contents($this->getDataFolder() . "404.html", $this->getResource("404.html"));
}
2016-05-30 06:10:51 +00:00
if(!file_exists($this->getDataFolder() . "403.html")) {
file_put_contents($this->getDataFolder() . "403.html", $this->getResource("403.html"));
}
2016-05-29 17:08:23 +00:00
set_time_limit(0);
2016-07-30 16:12:56 +00:00
$this->port = $this->getConfig()->get("port");
2016-05-29 17:08:23 +00:00
$this->getServer()->getScheduler()->scheduleAsyncTask(new execTask($this->getServer()->getFilePath()));
// UPnP::PortForward($port); \\\\ Beta for Windows
2016-05-29 17:08:23 +00:00
}
2016-07-30 16:12:56 +00:00
public function onDisable() {
if($this->getConfig()->get("KillOnShutdown") !== "false") {
switch(true) {
case stristr(PHP_OS, "WIN"):
exec('FOR /F "tokens=4 delims= " %P IN (\'netstat -a -n -o ^| findstr :'. $this->port .'\') DO @ECHO TaskKill.exe /PID %P');
break;
case stristr(PHP_OS, "DAR") or stristr(PHP_OS, "LINUX"):
shell_exec("kill -kill `lsof -t -i tcp:$this->port`");
break;
}
}
}
2016-05-29 17:08:23 +00:00
}
class execTask extends \pocketmine\scheduler\AsyncTask {
public function __construct(string $path) {
$this->path = $path;
}
public function onRun() {
$address = '0.0.0.0';
$port = yaml_parse(file_get_contents("plugins\\Online\\config.yml"))["port"];
// shell_exec("cd plugins/Online");
switch(true) {
case stristr(PHP_OS, "WIN"):
2016-07-30 16:12:56 +00:00
// echo '"%CD%\\bin\\php\\php.exe -t %CD%\\plugins\\Online -n -d include_path=\'%CD%\\plugins\\Online\\\' -S ' . $address . ":" . $port . ' -f %CD%\\plugins\\Online\\router.php"';
shell_exec('start "Online Listener" cmd /c "%CD%\\bin\\php\\php.exe -t %CD%\\plugins\\Online -n -d include_path=\'%CD%\\plugins\\Online\\\' -d extension=\'%CD%\\bin\\php\\ext\\php_yaml.dll\' -S ' . $address . ":" . $port . ' router.php"');
break;
case stristr(PHP_OS, "DAR"):
2016-07-30 16:12:56 +00:00
shell_exec('open -a Terminal "' . $this->path . "bin\\php\\php.exe -t " . $this->path . "plugins\\Online -n -d include_path=\'" . $this->path . "plugins\\Online\\\' -d extension=\'" . $this->path . "bin\\php\\ext\\php_yaml.dll\' -S " . $address . ":" . $port . ' router.php"');
break;
case stristr(PHP_OS, "LINUX"):
2016-07-30 16:12:56 +00:00
shell_exec('gnome-terminal -e "' . $this->path . "bin\\php\\php.exe -t " . $this->path . "plugins\\Online -n -d include_path=\'" . $this->path . "plugins\\Online\\\' -d extension=\'" . $this->path . "bin\\php\\ext\\php_yaml.dll\' -S " . $address . ":" . $port . ' router.php"');
break;
}
}
2016-05-29 17:08:23 +00:00
}