A new, object-oriented, better vesion of μForum
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.6 KiB

  1. <?php
  2. /**
  3. * @package uForum2
  4. * @file inc/models/UsersModel.php
  5. * @copyright 2007-2015 (c) PioDer
  6. * @link http://www.pioder.pl/
  7. * @license see LICENSE.txt
  8. **/
  9. class UsersModel extends Model
  10. {
  11. private $user_info = null;
  12. public function nickExists($nick)
  13. {
  14. $out = $this->select(USERS_TABLE, 'nick', '`nick`=\''.$nick.'\'', '', 1);
  15. if (count($out) > 0)
  16. return true;
  17. else
  18. return false;
  19. }
  20. public function createNewUser($nick, $passwd, $email)
  21. {
  22. $passwd = $this->generatePasswordHash($nick, $passwd);
  23. $this->db->query('call add_user(\''.$nick.'\', \''.$passwd.'\', \''.$email.'\');');
  24. }
  25. public function getUsersNicks()
  26. {
  27. $out = $this->select (USERS_TABLE, 'user_id, nick', '', 'user_id ASC');
  28. if (count($out) > 0)
  29. return $out;
  30. else
  31. return array();
  32. }
  33. public function getUserInformation($user, $passwd = false)
  34. {
  35. if ($this->user_info == null)
  36. {
  37. $query = '
  38. SELECT
  39. u.nick as nick, u.email as email, u.rank as rank, u.active as active,
  40. ui.regdate as regdate, ui.lastvisit as lastvisit, ui.avatar as avatar, ui.location as location, ui.signature as signature, pc.post_count as post_count, lu.IP as user_IP ';
  41. if ($passwd)
  42. $query .= ',u.password as password ';
  43. $query .= '
  44. FROM '.USERS_TABLE.' u
  45. LEFT JOIN '.USERS_INFO_TABLE.' ui on ui.user_id = u.user_id
  46. LEFT JOIN '.USERS_PC_VIEW.' pc on pc.user_id = u.user_id
  47. LEFT JOIN '.LOGGED_USERS_VIEW.' lu on lu.user_id = u.user_id
  48. WHERE u.user_id = \''.$user.'\' OR u.nick = \''.$user.'\'';
  49. $out = $this->select_query($query);
  50. if (count($out) > 0)
  51. $this->user_info = $out[0];
  52. }
  53. return $this->user_info;
  54. }
  55. public function changeUserPassword($user_id, $nick, $passwd)
  56. {
  57. $passwd = $this->generatePasswordHash($nick, $passwd);
  58. $query =
  59. 'UPDATE .'.USERS_TABLE.'
  60. SET `password`=\''.$passwd.'\'
  61. WHERE `user_id` = \''.$user_id.'\'';
  62. $this->db->query($query);
  63. }
  64. public function changeUserRank($user_id, $rank)
  65. {
  66. $query =
  67. 'UPDATE .'.USERS_TABLE.'
  68. SET `rank`=\''.$rank.'\'
  69. WHERE `user_id` = \''.$user_id.'\'';
  70. $this->db->query($query);
  71. }
  72. public function updateUserProfile($user_id, $nick, $email, $location, $signature, $avatar)
  73. {
  74. $query =
  75. 'UPDATE .'.USERS_TABLE.'
  76. SET ';
  77. if ($nick != null)
  78. $query .= '`nick`=\''.$nick.'\',';
  79. $query .= '`email`=\''.$email.'\'
  80. WHERE `user_id` = \''.$user_id.'\'';
  81. $this->db->query($query);
  82. //profile informations
  83. $query =
  84. 'UPDATE .'.USERS_INFO_TABLE.'
  85. SET
  86. `avatar`=\''.$avatar.'\',
  87. `location`=\''.$location.'\',
  88. `signature`=\''.$signature.'\'
  89. WHERE `user_id` = \''.$user_id.'\'';
  90. $this->db->query($query);
  91. }
  92. public function getUsers($stype, $sorder, $rank)
  93. {
  94. $query = '
  95. SELECT
  96. u.user_id as user_id, u.nick as nick, u.rank as rank, ui.regdate as regdate, ui.lastvisit as lastvisit, pc.post_count as post_count
  97. FROM '.USERS_TABLE.' u
  98. LEFT JOIN '.USERS_INFO_TABLE.' ui on ui.user_id = u.user_id
  99. LEFT JOIN '.USERS_PC_VIEW.' pc on pc.user_id = u.user_id ';
  100. if ($rank !== '')
  101. $query .= 'WHERE rank=\''.$rank.'\'';
  102. $query .= '
  103. ORDER BY '.$stype.' '.$sorder;
  104. $out = $this->select_query($query);
  105. return $out;
  106. }
  107. public function deleteUser($user_id)
  108. {
  109. $query =
  110. 'DELETE FROM .'.USERS_TABLE.'
  111. WHERE `user_id` = \''.$user_id.'\'';
  112. $this->db->query($query);
  113. }
  114. public function generatePasswordHash($nick, $password)
  115. {
  116. $modulo = strlen($nick) % 8;
  117. $salt_begin = substr(RANDOM_STRING, $modulo*SALT_LENGTH, SALT_LENGTH-$modulo);
  118. $salt_end = substr(RANDOM_STRING, $modulo*SALT_LENGTH + SALT_LENGTH-$modulo, $modulo);
  119. return hash('haval256,5', $salt_begin.$password.$salt_end);
  120. }
  121. }
  122. ?>