Skip to content

Instantly share code, notes, and snippets.

@sn3akrr
Created September 28, 2017 01:34
Show Gist options
  • Save sn3akrr/429a9f8a6063fe3eddf919340c04fd26 to your computer and use it in GitHub Desktop.
Save sn3akrr/429a9f8a6063fe3eddf919340c04fd26 to your computer and use it in GitHub Desktop.
<?php namespace migrate;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use pocketmine\Player;
use pocketmine\event\Listener;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\mcpe\protocol\ModalFormResponsePacket;
class Migrate extends PluginBase implements Listener{
public $pureperms;
public $simpleauth;
public $pudir;
public $sudir;
public function onEnable(){
$this->pureperms = $this->getServer()->getPluginManager()->getPlugin("PurePerms");
$this->simpleauth = $this->getServer()->getPluginManager()->getPlugin("SimpleAuth");
if($this->pureperms == null || $this->simpleauth == null){
$this->getLogger()->error("This plugin requires the following plugins: PurePerms, SimpleAuth. One or more not found. Disabling plugin.");
$this->getServer()->getPluginManager()->disablePlugin($this);
return;
}
@mkdir($this->getDataFolder());
if(!file_exists($this->getDataFolder() . "IMPORTANT")){
file_put_contents($this->getDataFolder() . "IMPORTANT", "Do not remove this file! Removing this will restart the migration process.\nThis migration plugin was made by your bae, Shane Malin (mal0ne_23)\nin return for $50\nDo not distribute, cunt. <3");
$p = $this->pureperms->getDataFolder() . "players/";
$this->pudir = $this->pureperms->getDataFolder() . "old/";
@mkdir($this->pudir);
try{
$file_names = scandir($p);
if(is_array($file_names)) foreach($file_names as $file) if($file != "." && $file != "..") rename($p . $file, $this->pudir . $file);
}catch(\Exception $e){
//fuck this
}
$s = $this->simpleauth->getDataFolder();
$this->sudir = $s . "old/";
@mkdir($this->sudir);
$keys = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","_"];
foreach($keys as $key){
$ldir = $s . $key . "/";
try{
$file_names = scandir($ldir);
if(is_array($file_names)) foreach($file_names as $file) if($file != "." && $file != "..") rename($ldir . $file, $this->sudir . $file);
}catch(\Exception $e){
//bullshit
}
}
}
$this->getServer()->getCommandMap()->register("migrate", new MigrateCommand($this, "migrate", "Migrate your account!"));
}
public function loginCorrect($username, $password){
if(!$this->canMigrate($username)) return false;
$password = $this->hash($username, $password);
$authfile = new Config($this->sudir . $username . ".yml", Config::YAML);
$all = $authfile->getAll();
$hash = $all["hash"];
return hash_equals($hash, $password);
}
public function hash($salt, $password){
return bin2hex(hash("sha512", $password . $salt, true) ^ hash("whirlpool", $salt . $password, true));
}
public function canMigrate($username){
if(!file_exists($this->pudir . $username . ".yml")) return false;
if(!file_exists($this->sudir . $username . ".yml")) return false;
$authfile = new Config($this->sudir . $username . ".yml", Config::YAML);
$all = $authfile->getAll();
if(!isset($all["hash"])) return false;
$file = new Config($this->pudir . $username . ".yml", Config::YAML);
return $file->get("canmigrate", true) ?? true;
}
public function setCanMigrate($username, $can = false){
$file = new Config($this->sudir . $username . ".yml", Config::YAML);
$file->set("canmigrate", $can);
$file->save();
}
public function migrate(Player $player, $username){
$this->setCanMigrate($username);
$pfile = new Config($this->pudir . $username . ".yml", Config::YAML);
$group = $pfile->get("group");
$this->pureperms->getUserDataMgr()->setGroup($player, $this->pureperms->getGroup($group) ?? $this->pureperms->getDefaultGroup(null), null);
}
public function onDpr(DataPacketReceiveEvent $e){
$player = $e->getPlayer();
$packet = $e->getPacket();
if($packet instanceof ModalFormResponsePacket){
if($packet->formId == 420){ //lel
$data = json_decode(json_encode($packet->formData), true);
if($data == null) return;
$username = $data[0];
$password = $data[1];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment