PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/solar27/include/game/classes/session.php

https://bitbucket.org/sebs/mosolar
PHP | 107 lines | 60 code | 29 blank | 18 comment | 16 complexity | 32d8c5bfca0ff104cecf840c00a289d2 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-2.0, MIT, GPL-2.0
  1. <?php
  2. // Solar Imperium is licensed under GPL2, Check LICENSE.TXT for mode details //
  3. class Session {
  4. var $DB;
  5. var $game_id;
  6. //////////////////////////////////////////////////////////////////////
  7. // Constructor
  8. //////////////////////////////////////////////////////////////////////
  9. function Session($DB) {
  10. $this->DB = $DB;
  11. $this->game_id = round($_SESSION["game"]);
  12. if (isset ($_SESSION["empire_id"])) {
  13. $rs = $this->DB->Execute("SELECT * FROM game".$this->game_id."_tb_session WHERE empire='" . intval($_SESSION["empire_id"])."'");
  14. if (!$rs) trigger_error($this->DB->ErrorMsg());
  15. if ($rs->EOF) {
  16. if (!$this->DB->Execute("INSERT INTO game".$this->game_id."_tb_session (empire,lastdate) " .
  17. "VALUES('" . $_SESSION["empire_id"] . "','" .
  18. time(NULL) . "')")) trigger_error($this->DB->ErrorMsg());
  19. }
  20. // update session date
  21. if (!$this->DB->Execute("UPDATE game".$this->game_id."_tb_session " .
  22. "SET lastdate = '" . time(NULL) . "' WHERE empire='" . $_SESSION["empire_id"]."'")) trigger_error($this->DB->ErrorMsg());
  23. }
  24. // delete old sessions
  25. $date_timeout = time(NULL) - CONF_SESSION_TIMEOUT;
  26. if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_session WHERE lastdate < $date_timeout")) trigger_error($this->DB->ErrorMsg());
  27. }
  28. //////////////////////////////////////////////////////////////////////
  29. // Logout of the system
  30. //////////////////////////////////////////////////////////////////////
  31. function logout() {
  32. if (isset ($_SESSION["empire_id"]))
  33. if (!$this->DB->Execute("DELETE FROM game".$this->game_id."_tb_session WHERE empire='".$_SESSION["empire_id"]."'")) trigger_error($this->DB->ErrorMsg());
  34. session_destroy();
  35. $_SESSION = null;
  36. }
  37. //////////////////////////////////////////////////////////////////////
  38. // Login of the system
  39. //////////////////////////////////////////////////////////////////////
  40. function login($username, $password) {
  41. global $CONF_PREMIUM_MEMBERS;
  42. if ($this->getOnlinePLayers() >= CONF_MAX_SESSIONS) {
  43. if (!in_array($username,$CONF_PREMIUM_MEMBERS))
  44. die(T_("Too much players connected, cannot login!"));
  45. }
  46. $empire = $this->DB->Execute("SELECT id FROM game".$this->game_id."_tb_empire WHERE email='" . addslashes($username) . "' AND " .
  47. "password='" . md5($password) . "' AND active > 0");
  48. if (!$empire) trigger_error($this->DB->ErrorMsg());
  49. if ($empire->EOF)
  50. return false;
  51. $_SESSION["empire_id"] = $empire->fields["id"];
  52. $_SESSION["email"] = $username;
  53. if (!$this->DB->Execute("INSERT INTO game".$this->game_id."_tb_session (empire,lastdate) " .
  54. "VALUES('" . $_SESSION["empire_id"] . "'," .
  55. time(NULL) . ")")) trigger_error($this->DB->ErrorMsg());
  56. return true;
  57. }
  58. //////////////////////////////////////////////////////////////////////
  59. // Empire is active?
  60. //////////////////////////////////////////////////////////////////////
  61. function isActive() {
  62. if (!isset ($_SESSION["empire_id"]))
  63. return 0;
  64. $query = "SELECT * FROM game".$this->game_id."_tb_empire WHERE id='" . $_SESSION["empire_id"]."'";
  65. $empire = $this->DB->Execute($query);
  66. if (!$empire) trigger_error($this->DB->ErrorMsg());
  67. return $empire->fields["active"];
  68. }
  69. //////////////////////////////////////////////////////////////////////
  70. // how much online players?
  71. //////////////////////////////////////////////////////////////////////
  72. function getOnlinePLayers() {
  73. $count = $this->DB->Execute("SELECT COUNT(*) FROM game".$this->game_id."_tb_session");
  74. if (!$count) trigger_error($this->DB->ErrorMsg());
  75. return $count->fields[0];
  76. }
  77. }
  78. ?>