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.

144 lines
3.5 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. <?Php
  2. /**
  3. * \class User
  4. * \brief Handles the user's login and user.
  5. *
  6. */
  7. class User {
  8. private $xmppSession;
  9. private $username = '';
  10. private $password = '';
  11. /**
  12. * Class constructor. Reloads the user's session or attempts to authenticate
  13. * the user.
  14. * Note that the constructor is private. This class is a singleton.
  15. */
  16. function __construct()
  17. {
  18. if($this->isLogged()) {
  19. $sess = Session::start(APP_NAME);
  20. $this->username = $sess->get('login');
  21. $this->password = $sess->get('pass');
  22. $this->xmppSession = Jabber::getInstance($this->username);
  23. }
  24. else if(isset($_POST['login'])
  25. && isset($_POST['pass'])
  26. && $_POST['login'] != ''
  27. && $_POST['pass'] != '') {
  28. $this->authenticate($_POST['login'], $_POST['pass']);
  29. }
  30. }
  31. /**
  32. * Checks if the user has an open session.
  33. */
  34. function isLogged()
  35. {
  36. // User is not logged in if both the session vars and the members are unset.
  37. $sess = Session::start(APP_NAME);
  38. return (($this->username != '' && $this->password != '') || $sess->get('login'));
  39. }
  40. function authenticate($login,$pass)
  41. {
  42. try{
  43. $data = false;
  44. if( !($data = $this->getConf($login)) ) {
  45. // We check if we wants to create an account
  46. header('Location:'.BASE_URI.'index.php?q=disconnect&err=noaccount');
  47. }
  48. $this->xmppSession = Jabber::getInstance($login);
  49. $this->xmppSession->login($login, $pass);
  50. // Careful guys, md5 is _not_ secure. SHA1 recommended here.
  51. if(sha1($pass) == $data['pass']) {
  52. $sess = Session::start(APP_NAME);
  53. $sess->set('login', $login);
  54. $sess->set('pass', $pass);
  55. $this->username = $login;
  56. $this->password = $pass;
  57. } else {
  58. throw new MovimException(t("Wrong password"));
  59. }
  60. }
  61. catch(MovimException $e){
  62. echo $e->getMessage();
  63. return $e->getMessage();
  64. }
  65. }
  66. function desauth()
  67. {
  68. PresenceHandler::clearPresence();
  69. $sess = Session::start('jaxl');
  70. Session::dispose('jaxl');
  71. $sess = Session::start(APP_NAME);
  72. Session::dispose(APP_NAME);
  73. }
  74. function setLang($language)
  75. {
  76. global $sdb;
  77. $conf = $sdb->select('ConfVar', array('login' => $this->username));
  78. $conf[0]->language = $language;
  79. $sdb->save($conf[0]);
  80. }
  81. function setConf($data)
  82. {
  83. global $sdb;
  84. $conf = $sdb->select('ConfVar', array('login' => $this->username));
  85. $conf[0]->setConf(
  86. $data['login'],
  87. $data['pass'],
  88. $data['host'],
  89. $data['domain'],
  90. $data['port'],
  91. $data['boshhost'],
  92. $data['boshsuffix'],
  93. $data['boshport'],
  94. $data['language'],
  95. $data['first']
  96. );
  97. $sdb->save($conf[0]);
  98. }
  99. function getConf($user = false, $element = false) {
  100. $login = ($user != false) ? $user : $this->username;
  101. $query = ConfVar::query()
  102. ->where(array('login' => $login));
  103. $conf = ConfVar::run_query($query);
  104. if($conf != false) {
  105. $array = $conf[0]->getConf();
  106. if($element != false)
  107. return $array[$element];
  108. else
  109. return $array;
  110. } else {
  111. return false;
  112. }
  113. }
  114. function getLogin()
  115. {
  116. return $this->username;
  117. }
  118. function getPass()
  119. {
  120. return $this->password;
  121. }
  122. }