コード例 #1
0
 /**
  * Authenticate a Player
  * 
  * @param Player $player
  * @param string $password
  * @param boolean $hash
  * 
  * @return int|boolean true on SUCCESS, otherwise the current error
  */
 public function authenticatePlayer(Player $player, $password, $hash = true)
 {
     if ($hash) {
         $password = hash($this->getPasswordHash(), $password);
     }
     if ($this->isPlayerRegistered($player->getName())) {
         if (!$this->isPlayerAuthenticated($player)) {
             $this->getServer()->getPluginManager()->callEvent($event = new Events\ServerAuthAuthenticateEvent($player));
             if ($event->isCancelled()) {
                 return ServerAuth::CANCELLED;
             }
             $cfg = $this->getConfig()->getAll();
             if ($this->getDataProvider()) {
                 //Check MySQL connection
                 if ($this->getDatabase() && $this->getDatabase()->ping()) {
                     $query = "SELECT user, password, ip, firstlogin, lastlogin FROM " . $this->getDatabaseConfig()["table_prefix"] . "serverauthdata WHERE user='******'";
                     $db_password = $this->getDatabase()->query($query)->fetch_assoc()["password"];
                     if ($db_password) {
                         if ($password == $db_password) {
                             $query = "UPDATE " . $this->getDatabaseConfig()["table_prefix"] . "serverauthdata SET ip='" . $player->getAddress() . "', lastlogin='******' WHERE user='******'";
                             if ($this->getDatabase()->query($query)) {
                                 $this->auth_users[strtolower($player->getName())] = "";
                                 if ($cfg['login']['enable-failed-logins-kick'] && isset($this->auth_attempts[strtolower($player->getName())])) {
                                     unset($this->auth_attempts[strtolower($player->getName())]);
                                 }
                                 return ServerAuth::SUCCESS;
                             } else {
                                 return ServerAuth::ERR_GENERIC;
                             }
                         } else {
                             if ($cfg['login']['enable-failed-logins-kick']) {
                                 if (isset($this->auth_attempts[strtolower($player->getName())])) {
                                     $this->auth_attempts[strtolower($player->getName())]++;
                                 } else {
                                     $this->auth_attempts[strtolower($player->getName())] = 1;
                                 }
                                 if ($this->auth_attempts[strtolower($player->getName())] >= $cfg['login']['max-login-attempts']) {
                                     $player->close("", $this->translateColors("&", ServerAuth::getAPI()->getConfigLanguage()->getAll()["login"]["too-many-attempts"]));
                                     unset($this->auth_attempts[strtolower($player->getName())]);
                                     return ServerAuth::TOO_MANY_ATTEMPTS;
                                 }
                             }
                             return ServerAuth::ERR_WRONG_PASSWORD;
                         }
                     } else {
                         return ServerAuth::ERR_GENERIC;
                     }
                 } else {
                     return ServerAuth::ERR_GENERIC;
                 }
             } else {
                 $data = new Config($this->getDataFolder() . "users/" . strtolower($player->getName() . ".yml"), Config::YAML);
                 if ($password == $data->get("password")) {
                     $data->set("ip", $player->getAddress());
                     $data->set("lastlogin", $player->getLastPlayed());
                     $data->save();
                     $this->auth_users[strtolower($player->getName())] = "";
                     if ($cfg['login']['enable-failed-logins-kick'] && isset($this->auth_attempts[strtolower($player->getName())])) {
                         unset($this->auth_attempts[strtolower($player->getName())]);
                     }
                     return ServerAuth::SUCCESS;
                 } else {
                     if ($cfg['login']['enable-failed-logins-kick']) {
                         if (isset($this->auth_attempts[strtolower($player->getName())])) {
                             $this->auth_attempts[strtolower($player->getName())]++;
                         } else {
                             $this->auth_attempts[strtolower($player->getName())] = 1;
                         }
                         if ($this->auth_attempts[strtolower($player->getName())] >= $cfg['login']['max-login-attempts']) {
                             $player->close("", $this->translateColors("&", ServerAuth::getAPI()->getConfigLanguage()->getAll()["login"]["too-many-attempts"]));
                             unset($this->auth_attempts[strtolower($player->getName())]);
                             return ServerAuth::TOO_MANY_ATTEMPTS;
                         }
                     }
                     return ServerAuth::ERR_WRONG_PASSWORD;
                 }
             }
         } else {
             return ServerAuth::ERR_USER_ALREADY_AUTHENTICATED;
         }
     } else {
         return $this->isPlayerRegistered($player->getName());
     }
 }
コード例 #2
0
 public function onAuthenticate(ServerAuthAuthenticateEvent $event)
 {
     $event->getPlayer()->setNameTag($this->temp_nametags[$event->getPlayer()->getName()]);
 }
コード例 #3
0
 public function onAuth(ServerAuthAuthenticateEvent $event)
 {
     $this->hub->onPlayerAuth($event->getPlayer());
 }
コード例 #4
0
ファイル: ServerAuth.php プロジェクト: majovec/ServerAuth
 /**
  * Authenticate a Player
  * 
  * @param Player $player
  * @param string $password
  * @param boolean $hash
  * 
  * @return int|boolean true on SUCCESS, otherwise the current error
  */
 public function authenticatePlayer(Player $player, $password, $hash = true)
 {
     if ($hash) {
         $password = hash($this->getPasswordHash(), $password);
     }
     if ($this->isPlayerRegistered($player->getName())) {
         if (!$this->isPlayerAuthenticated($player)) {
             //Reset cancelled message
             $this->canc_message = $this->chlang["operation-cancelled"];
             $this->getServer()->getPluginManager()->callEvent($event = new Events\ServerAuthAuthenticateEvent($player));
             if ($event->isCancelled()) {
                 return ServerAuth::CANCELLED;
             }
             $cfg = $this->getConfig()->getAll();
             if ($this->getDataProvider()) {
                 //Check MySQL connection
                 if ($this->getDatabase() && $this->getDatabase()->ping()) {
                     $stmt = $this->getDatabase()->prepare("SELECT user, password, ip, firstlogin, lastlogin FROM " . $this->getDatabaseConfig()["table_prefix"] . "serverauthdata WHERE user=?");
                     $stmt_player = strtolower($player->getName());
                     $stmt->bind_param("s", $stmt_player);
                     $stmt->execute();
                     $stmt->bind_result($user, $db_password, $ip, $firstlogin, $lastlogin);
                     $stmt->fetch();
                     $stmt->close();
                     if ($db_password) {
                         if ($password == $db_password) {
                             $stmt = $this->getDatabase()->prepare("UPDATE " . $this->getDatabaseConfig()["table_prefix"] . "serverauthdata SET ip=?, lastlogin=? WHERE user=?");
                             $stmt_ip = $player->getAddress();
                             $stmt_lastplayed = $player->getLastPlayed();
                             $stmt_player = strtolower($player->getName());
                             $stmt->bind_param("sss", $stmt_ip, $stmt_lastplayed, $stmt_player);
                             if ($stmt->execute()) {
                                 $this->auth_users[strtolower($player->getName())] = "";
                                 if ($cfg['login']['enable-failed-logins-kick'] && isset($this->auth_attempts[strtolower($player->getName())])) {
                                     unset($this->auth_attempts[strtolower($player->getName())]);
                                 }
                                 $stmt->close();
                                 return ServerAuth::SUCCESS;
                             } else {
                                 $stmt->close();
                                 return ServerAuth::ERR_GENERIC;
                             }
                         } else {
                             if ($cfg['login']['enable-failed-logins-kick']) {
                                 if (isset($this->auth_attempts[strtolower($player->getName())])) {
                                     $this->auth_attempts[strtolower($player->getName())]++;
                                 } else {
                                     $this->auth_attempts[strtolower($player->getName())] = 1;
                                 }
                                 if ($this->auth_attempts[strtolower($player->getName())] >= $cfg['login']['max-login-attempts']) {
                                     $player->close("", $this->translateColors("&", $this->chlang["login"]["too-many-attempts"]));
                                     unset($this->auth_attempts[strtolower($player->getName())]);
                                     return ServerAuth::TOO_MANY_ATTEMPTS;
                                 }
                             }
                             return ServerAuth::ERR_WRONG_PASSWORD;
                         }
                     } else {
                         return ServerAuth::ERR_GENERIC;
                     }
                 } else {
                     return ServerAuth::ERR_GENERIC;
                 }
             } else {
                 $data = new Config($this->getDataFolder() . "users/" . strtolower($player->getName() . ".yml"), Config::YAML);
                 if ($password == $data->get("password")) {
                     $data->set("ip", $player->getAddress());
                     $data->set("lastlogin", $player->getLastPlayed());
                     $data->save();
                     $this->auth_users[strtolower($player->getName())] = "";
                     if ($cfg['login']['enable-failed-logins-kick'] && isset($this->auth_attempts[strtolower($player->getName())])) {
                         unset($this->auth_attempts[strtolower($player->getName())]);
                     }
                     return ServerAuth::SUCCESS;
                 } else {
                     if ($cfg['login']['enable-failed-logins-kick']) {
                         if (isset($this->auth_attempts[strtolower($player->getName())])) {
                             $this->auth_attempts[strtolower($player->getName())]++;
                         } else {
                             $this->auth_attempts[strtolower($player->getName())] = 1;
                         }
                         if ($this->auth_attempts[strtolower($player->getName())] >= $cfg['login']['max-login-attempts']) {
                             $player->close("", $this->translateColors("&", $this->chlang["login"]["too-many-attempts"]));
                             unset($this->auth_attempts[strtolower($player->getName())]);
                             return ServerAuth::TOO_MANY_ATTEMPTS;
                         }
                     }
                     return ServerAuth::ERR_WRONG_PASSWORD;
                 }
             }
         } else {
             return ServerAuth::ERR_USER_ALREADY_AUTHENTICATED;
         }
     } else {
         return $this->isPlayerRegistered($player->getName());
     }
 }