Guest User

clientservicestibiaweblog1

a guest
Sep 5th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: jlcvp - leu
  5.  * Date: 07/06/17
  6.  * Time: 21:22
  7.  */
  8.  
  9. require 'config/config.php';
  10.  
  11. // comment to show E_NOTICE [undefinied variable etc.], comment if you want make script and see all errors
  12. error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE);
  13.  
  14. // true = show sent queries and SQL queries status/status code/error message
  15. define('DEBUG_DATABASE', false);
  16.  
  17. define('INITIALIZED', true);
  18.  
  19. if (!defined('ONLY_PAGE'))
  20.     define('ONLY_PAGE', true);
  21.  
  22. // check if site is disabled/requires installation
  23. include_once('./system/load.loadCheck.php');
  24.  
  25. // fix user data, load config, enable class auto loader
  26. include_once('./system/load.init.php');
  27.  
  28. // DATABASE
  29. include_once('./system/load.database.php');
  30. if (DEBUG_DATABASE)
  31.     Website::getDBHandle()->setPrintQueries(true);
  32. // DATABASE END
  33.  
  34. /*error example:
  35. {
  36.     "errorCode":3,
  37.     "errorMessage":"Account name or password is not correct."
  38. }*/
  39.  
  40. //error function
  41. function sendError($error_msg,$code=3){
  42.     $retError = array();
  43.     $retError["errorCode"] = $code;
  44.     $retError["errorMessage"] = $error_msg;
  45.     die(json_encode($retError));
  46. }
  47.  
  48.  
  49. $request_body = file_get_contents('php://input');
  50. $result = json_decode($request_body, true);
  51.  
  52. $acc = $result["accountname"];
  53. $password = $result["password"];
  54.  
  55. $query = $SQL->prepare("SELECT `id`,`premdays` FROM `accounts` WHERE `name` = :acc AND `password` = SHA1(:pass) LIMIT 1");
  56.  
  57. $query->bindValue(":acc", $acc);
  58. $query->bindValue(":pass", $password);
  59.  
  60. $dbResource = $query->execute();
  61.  
  62. if (!$dbResource) {
  63.     sendError("failed to get account.");
  64. }
  65.  
  66. $dbRet = $query->fetch();
  67. if (!dbRet) {
  68.     sendError("failed to fetch account data");
  69. }
  70.  
  71. $accId = $dbRet["id"];
  72. $premdays = $dbRet["premdays"];
  73.  
  74. if (!$accId) {
  75.     sendError("Account name or password is not correct.");
  76. }
  77.  
  78. $dbResource = $SQL->query("SELECT `name`,`sex`,`lastlogin` FROM `players` WHERE `account_id` = $accId");
  79.  
  80. if (!$dbResource) {
  81.     sendError("failed to get characters.");
  82. }
  83.  
  84. $accArray = array();
  85.  
  86. $lastlogin=0;
  87.  
  88. while ($dbRet = $dbResource->fetch()) {
  89.     $dict = array(
  90.         "worldid" => 0,
  91.         "name" => $dbRet["name"],
  92.         "ismale" => (($dbRet["sex"]==1)?true:false),
  93.         "tutorial" => (($dbRet["lastlogin"]>0) ? false:true)
  94.     );
  95.     $accArray[] = $dict;
  96.     if($lastlogin<$dbRet["lastlogin"]){
  97.         $lastlogin = $dbRet["lastlogin"];
  98.     }
  99. }
  100.  
  101. $data = array();
  102.  
  103. //TODO: Melhorar estrutura de dado aqui e preencher com os dados reais da account
  104. $session = array(
  105.     "sessionkey" => $acc . "\n" . $password,
  106.     "lastlogintime" => $lastlogin,
  107.     "ispremium" => ($premdays > 0 || $config["server"]["freePremium"]) ? true : false,
  108.     "premiumuntil" => ($freePremium) ? (time() + 365 * 86400) : (time() + $premdays * 86400),
  109.     "status" => "active"
  110. );
  111.  
  112. $data["session"] = $session;
  113.  
  114. $playerData = array();
  115.  
  116. //TODO: melhorar estrutura de dado aqui para permitir multiple worlds
  117. $world = array(
  118.     "id" => 0,
  119.     "name" => $config["server"]["serverName"],
  120.     "externaladdress" => $config["server"]["ip"],
  121.     "externalport" => $config["server"]["gameProtocolPort"],
  122.     "previewstate" => 0,
  123.     "location" => "BRA",
  124.     "externaladdressunprotected" => $config["server"]["ip"],
  125.     "externaladdressprotected" => $config["server"]["ip"]
  126. );
  127.  
  128. $worlds = array($world);
  129. $playerData["worlds"] = $worlds;
  130. $playerData["characters"] = $accArray;
  131.  
  132.  
  133. $data["playdata"] = $playerData;
  134.  
  135. echo json_encode($data);
Add Comment
Please, Sign In to add comment