You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

126 lines
2.6 KiB

  1. <?php
  2. class MUser {
  3. private $_username;
  4. private $_password;
  5. private $_hostIndex;
  6. private $_db;
  7. private $_timeout;
  8. public function __construct() {
  9. }
  10. public function setUsername($username) {
  11. $this->_username = $username;
  12. }
  13. public function username() {
  14. return $this->_username;
  15. }
  16. public function setPassword($password) {
  17. $this->_password = $password;
  18. }
  19. public function password() {
  20. return $this->_password;
  21. }
  22. public function setHostIndex($hostIndex) {
  23. $this->_hostIndex = $hostIndex;
  24. }
  25. public function hostIndex() {
  26. return $this->_hostIndex;
  27. }
  28. public function setDb($db) {
  29. $this->_db = $db;
  30. }
  31. public function defaultDb() {
  32. $dbs = $this->dbs();
  33. return $dbs[0];
  34. }
  35. public function dbs() {
  36. if (empty($this->_db)) {
  37. import("@.MServer");
  38. $server = MServer::serverWithIndex($this->_hostIndex);
  39. $mongoDb = "admin";
  40. if (!$server->mongoAuth()) {
  41. $authDb = MServer::serverWithIndex($this->_hostIndex)->mongoDb();
  42. if ($authDb) {
  43. $mongoDb = $authDb;
  44. }
  45. }
  46. return array($mongoDb);
  47. }
  48. if (is_array($this->_db)) {
  49. return array_values($this->_db);
  50. }
  51. return preg_split("/\\s*,\\s*/", $this->_db);
  52. }
  53. public function setTimeout($timeout) {
  54. $this->_timeout = $timeout;
  55. }
  56. /**
  57. * Validate User
  58. *
  59. * @return boolean
  60. */
  61. public function validate() {
  62. import("@.MServer");
  63. $server = MServer::serverWithIndex($this->_hostIndex);
  64. if (empty($server)) {
  65. return false;
  66. }
  67. return $server->auth($this->_username, $this->_password, $this->_db);
  68. }
  69. public function servers() {
  70. global $MONGO;
  71. return $MONGO["servers"];
  72. }
  73. public function changeHost($hostIndex) {
  74. $_SESSION["login"]["index"] = $hostIndex;
  75. }
  76. public static function login($username, $password, $hostIndex, $db, $timeout) {
  77. $_SESSION["login"] = array(
  78. "username" => $username,
  79. "password" => $password,
  80. "index" => $hostIndex,
  81. "db" => $db
  82. );
  83. setcookie(session_name(), session_id(), time() + $timeout);
  84. }
  85. /**
  86. * Enter description here ...
  87. *
  88. * @return MUser
  89. */
  90. public static function userInSession() {
  91. if (array_key_exists("login", $_SESSION)
  92. && array_key_exists("username", $_SESSION["login"])
  93. && array_key_exists("password", $_SESSION["login"])
  94. && array_key_exists("index", $_SESSION["login"])
  95. && array_key_exists("db", $_SESSION["login"])) {
  96. $user = new MUser();
  97. $user->setUsername($_SESSION["login"]["username"]);
  98. $user->setPassword($_SESSION["login"]["password"]);
  99. $user->setHostIndex($_SESSION["login"]["index"]);
  100. $user->setDb($_SESSION["login"]["db"]);
  101. return $user;
  102. }
  103. return null;
  104. }
  105. }
  106. ?>