public function saveData(AccountInfo $info) { $name = $info->name; $path = $this->getPath($name); $this->main->getServer()->getScheduler()->scheduleAsyncTask(new JsonSaveDataTask($path, $info->serialize(), $info->lastIp, $info->name, $info->registerTime)); // $stmt = $this->sql->prepare("SELECT time FROM reg WHERE name=:name"); // $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT); // $result = $stmt->execute(); // $row = $result->fetchArray(SQLITE3_ASSOC); // $result->finalize(); // $registered = (is_array($row) and $row["time"] !== -1); // if(!$registered){ // $stmt = $this->sql->prepare(/** @lang SQLite */ // "UPDATE reg SET time = CASE time WHEN -1 THEN :time ELSE time END WHERE name=:name"); // $stmt->bindValue(":ip", $info->lastIp, SQLITE3_TEXT); // $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT); // $stmt->bindValue(":time", $info->registerTime, SQLITE3_INTEGER); // $stmt->execute(); // if($this->sql->changes() === 0){ // $stmt = $this->sql->prepare("INSERT INTO reg (ip, name, time) VALUES (:ip, :name, :time)"); // $stmt->bindValue(":ip", $info->lastIp, SQLITE3_TEXT); // $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT); // $stmt->bindValue(":time", $info->registerTime, SQLITE3_INTEGER); // $stmt->execute(); // } // } }
public function onCompletion(Server $server) { $main = HereAuth::getInstance($server); if ($main !== null) { if ($this->output !== null) { $output = new AccountInfo(); $output->unserialize($this->output); } else { $output = null; } $main->onUserStart($this->identifier, $output); } }
public function saveData($name, AccountInfo $info) { $path = $this->getPath($name); $existed = file_exists($path); $this->main->getServer()->getScheduler()->scheduleAsyncTask(new FileWriteTask($path, $info->serialize())); if (!$existed) { $stmt = $this->sql->prepare("INSERT INTO reg (ip, name, time) VALUES (:ip, :name, :time)"); $stmt->bindValue(":ip", $info->lastIp, SQLITE3_TEXT); $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT); $stmt->bindValue(":time", $info->registerTime, SQLITE3_INTEGER); $stmt->execute(); } }
public function read($params, AccountWriter $writer) { $args = new FormattedArgumentMap($params); $folder = $args->opt("i", "plugins/SimpleAuth"); if (!is_dir($folder)) { throw new \InvalidArgumentException("Input database {$folder} not found or is not a directory"); } $path = rtrim($folder, "/\\") . "/players.db"; if (!is_file($path)) { return; } $this->setStatus("Opening database"); $db = new \SQLite3($path); $result = $db->query("SELECT COUNT(*) AS cnt FROM players"); $total = $result->fetchArray(SQLITE3_ASSOC)["cnt"]; $result->finalize(); $this->setStatus("Preparing data"); $result = $db->query("SELECT name,registerdate,logindate,lastip,hash FROM players"); $i = 0; while (is_array($row = $result->fetchArray(SQLITE3_ASSOC))) { $i++; $info = AccountInfo::defaultInstance($row["name"], $this->defaultOpts); $info->lastIp = $row["lastip"]; $info->registerTime = $row["registerdate"]; $info->lastLogin = $row["logindate"]; $info->passwordHash = hex2bin($row["hash"]); $writer->write($info); $this->setProgress($i / $total); } $db->close(); }
public function write(AccountInfo $info) { $path = $this->getPath($info->name); if ($isOverwrite = is_file($path)) { if (!$this->overwrite) { return; } } file_put_contents($path, zlib_encode($info->serialize(), ZLIB_ENCODING_DEFLATE)); if (!$isOverwrite and $info->registerTime !== -1) { $stmt = $this->sql->prepare("INSERT OR REPLACE INTO reg (ip, name, time) VALUES (:ip, :name, :time)"); $stmt->bindValue(":ip", $info->lastIp, SQLITE3_TEXT); $stmt->bindValue(":name", strtolower($info->name), SQLITE3_TEXT); $stmt->bindValue(":time", $info->registerTime, SQLITE3_INTEGER); $stmt->execute(); } }
public function onCompletion(Server $server) { $main = HereAuth::getInstance($server); if ($main === null) { return; } $result = $this->getResult(); if (is_array($result)) { $info = AccountInfo::fromDatabaseRow($result); } else { $info = null; } $main->onUserStart($this->identifier, $info); }
public function read($params, AccountWriter $writer) { $args = new FormattedArgumentMap($params); $folder = $args->opt("i", "plugins/ServerAuth"); if (!is_dir($folder)) { throw new \InvalidArgumentException("Input database {$folder} not found or is not a directory"); } $folder = rtrim($folder, "/\\") . DIRECTORY_SEPARATOR; $configFile = $folder . "config.yml"; if (($hashMethod = $args->opt("hash", null)) === null) { if (is_file($configFile)) { try { $hashMethod = yaml_parse($configFile)["passwordHash"]; } catch (\Exception $e) { $hashMethod = "md5"; } } else { $hashMethod = "md5"; } } $users = $folder . "users/"; $this->setStatus("Collecting files"); $names = scandir($users); $namesCnt = count($names); $this->setStatus("Transferring data"); foreach ($names as $i => $name) { $this->setStatus($i / $namesCnt); $file = $users . $name; if (!is_file($file) or !preg_match('@^[A-Za-z0-9_]{3,16}\\.yml$@', $name)) { continue; } $name = substr($file, 0, -4); $data = yaml_parse_file($file); $ai = AccountInfo::defaultInstance($name, $this->defaultOpts); $ai->passwordHash = "{IMPORTED}"; $ai->lastIp = $data["ip"]; $ai->registerTime = $data["firstlogin"]; $ai->lastLogin = $data["lastlogin"]; $ai->multiHash = ["saltless;" . $hashMethod => $data["password"], "nonhash:salt" => strtolower($name)]; $writer->write($ai); } }
public function read($params, AccountWriter $writer) { $args = new FormattedArgumentMap($params); $folder = $args->opt("i", "plugins/SimpleAuth"); if (!is_dir($folder)) { throw new \InvalidArgumentException("Input database {$folder} not found or is not a directory"); } $folder = rtrim($folder, DIRECTORY_SEPARATOR) . "/"; $dir = $folder . "players/"; $this->setStatus("Indexing accounts"); $alphas = array_filter(scandir($dir), function ($alpha) use($dir) { return $alpha !== "." and strlen(rtrim($alpha, DIRECTORY_SEPARATOR)) === 1 and is_dir($dir . $alpha); }); $alphaCnt = count($alphas); $this->setStatus("Transferring data"); foreach ($alphas as $i => $alpha) { $base = $i / $alphaCnt; $subdir = $dir . rtrim($alpha, DIRECTORY_SEPARATOR) . "/"; $names = scandir($subdir); $namesCnt = count($names); foreach ($names as $cnt => $name) { $this->setProgress($base + $cnt / $namesCnt / $alphaCnt); if (!preg_match('@^[A-za-z0-9_]{3,16}\\.yml$@', $name)) { continue; } $data = yaml_parse_file($subdir . $name); $name = substr($name, 0, -4); if (!is_array($data)) { continue; } $info = AccountInfo::defaultInstance($name, $this->defaultOpts); $info->passwordHash = hex2bin($data["hash"]); $info->lastIp = $data["lastip"]; $info->registerTime = $data["registerdate"]; $info->lastLogin = $data["logindate"]; $writer->write($info); } } $this->setProgress(1.0); }
public function read($params, AccountWriter $writer) { $args = new FormattedArgumentMap($params); $this->cred->host = $args->opt("h", $this->cred->host); $this->cred->username = $args->opt("u", $this->cred->username); $this->cred->password = $args->opt("p", $this->cred->password); $this->cred->schema = $args->opt("d", $this->cred->schema); $this->cred->schema = $args->opt("s", $this->cred->schema); $this->cred->port = (int) $args->opt("port", $this->cred->port); $this->cred->socket = $args->opt("socket", $this->cred->socket); $this->cred->socket = $args->opt("sk", $this->cred->socket); $this->setStatus("Connecting"); $conn = MySQLDatabase::createMysqliInstance($this->cred); if (isset($conn->connect_error)) { throw new \InvalidArgumentException("Could not connect to {$this->cred}"); } $this->setStatus("Preparing data"); $result = $conn->query("SELECT name, registerdate, logindate, lastip, hash FROM simpleauth_players"); $this->setStatus("Transferring data"); if ($result instanceof \mysqli_result) { $this->setProgress(0.0); $rows = 0; while (is_array($row = $result->fetch_assoc())) { $info = AccountInfo::defaultInstance($row["name"], $this->defaultOpts); $info->registerTime = (int) $row["registerdate"]; $info->lastLogin = (int) $row["logindate"]; $info->lastIp = $result["lastip"]; $info->passwordHash = hex2bin($result["hash"]); $writer->write($info); $this->setProgress(++$rows / $result->num_rows); } $result->close(); } else { throw new \InvalidArgumentException("Not a SimpleAuth user database"); } }
public function read($params, AccountWriter $writer) { $args = new FormattedArgumentMap($params); $this->cred->host = $args->opt("h", $this->cred->host); $this->cred->username = $args->opt("u", $this->cred->username); $this->cred->password = $args->opt("p", $this->cred->password); $this->cred->schema = $args->opt("d", $this->cred->schema); $this->cred->schema = $args->opt("s", $this->cred->schema); $this->cred->port = (int) $args->opt("port", $this->cred->port); $this->cred->socket = $args->opt("socket", $this->cred->socket); $this->cred->socket = $args->opt("sk", $this->cred->socket); $this->setStatus("Connecting"); $conn = MySQLDatabase::createMysqliInstance($this->cred); if (isset($conn->connect_error)) { throw new \InvalidArgumentException("Could not connect to {$this->cred}"); } $prefix = $args->opt("prefix", null); $prefix = $args->opt("pfx", $prefix); if ($prefix === null) { $this->setStatus("Searching for tables"); $result = $conn->query("SHOW TABLES LIKE '%serverauth%'"); if ($result === false) { throw new \InvalidArgumentException("Could not search tables"); } $prefixes = []; while (is_array($row = $result->fetch_array(MYSQLI_NUM))) { if (StringUtils::endsWith($row[0], "serverauth")) { $prefix = substr($row[0], 0, -10); if (isset($prefixes[$prefix])) { $prefixes[$prefix]++; } else { $prefix[$prefix] = 1; } } elseif (StringUtils::endsWith($row[0], "serverauthdata")) { $prefix = substr($row[0], 0, -14); if (isset($prefixes[$prefix])) { $prefixes[$prefix]++; } else { $prefix[$prefix] = 1; } } } $result->close(); foreach ($prefixes as $prefix => $cnt) { if ($cnt === 2) { $ok = true; break; } } if (!isset($ok)) { throw new \RuntimeException("ServerAuth tables not found in {$this->cred}"); } } $serverAuthTable = $prefix . "serverauth"; $serverAuthDataTable = $prefix . "serverauthdata"; $hashMethod = $args->opt("hash", null); if ($hashMethod === null) { $this->setStatus("Detecting hash algorithm"); $result = $conn->query("SELECT password_hash FROM `{$serverAuthTable}`"); $row = $result->fetch_assoc(); if (!is_array($row)) { throw new \RuntimeException("Corrupted ServerAuth database: serverauth table empty"); } $hashMethod = $row["password_hash"]; if (!in_array($hashMethod, hash_algos())) { throw new \RuntimeException("ServerAuth database uses a hash algorithm not supported by PHP " . PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION . "." . PHP_RELEASE_VERSION . ". This may imply a corrupted database."); } } $this->setStatus("Preparing data for transfer"); $result = $conn->query("SELECT user,password,ip,firstlogin,lastlogin FROM `{$serverAuthDataTable}`"); $this->setStatus("Transferring"); if ($result instanceof \mysqli_result) { $rows = 0; while (is_array($row = $result->fetch_assoc())) { $info = AccountInfo::defaultInstance($result["user"], $this->defaultOpts); $info->registerTime = (int) $result["firstlogin"]; $info->lastLogin = (int) $result["lastlogin"]; $info->lastIp = $result["ip"]; $info->passwordHash = "{IMPORTED}"; $info->multiHash = ["saltless;" . $hashMethod => $result["password"], "nonhash:salt" => strtolower($result["user"])]; $writer->write($info); $this->setProgress(++$rows / $result->num_rows); } $result->close(); } else { throw new \InvalidArgumentException("Not a SimpleAuth user database"); } }
/** * @param int $identifier * @param AccountInfo|null $info */ public function onUserStart($identifier, $info) { $player = $this->getPlayerById($identifier); if ($player === null) { return; } if (!isset($info->name)) { $info = AccountInfo::defaultInstance($this, $player); } $this->users[$player->getId()] = new User($this, $player, $info); }
public function resetAccount($callback = null) { if (!is_callable($callback)) { $callback = function () { }; } $this->accountInfo = AccountInfo::defaultInstance($this->getPlayer(), $this->getMain()); $this->logout("This account has been reset."); $name = $this->getPlayer()->getName(); $this->getMain()->getDataBase()->unregisterAccount($name, $callback); }
public function write(AccountInfo $info) { $this->mysqli->query($info->getDatabaseQuery($this->tableName, new MySQLEscapeInvokable($this->mysqli), $this->overwrite)); }