|
|
- <?php
- /**
- * @package uForum2
- * @file inc/models/UsersModel.php
- * @copyright 2007-2015 (c) PioDer
- * @link http://www.pioder.pl/
- * @license see LICENSE.txt
- **/
-
- class UsersModel extends Model
- {
- private $user_info = null;
-
- public function nickExists($nick)
- {
- $out = $this->select(USERS_TABLE, 'nick', '`nick`=\''.$nick.'\'', '', 1);
- if (count($out) > 0)
- return true;
- else
- return false;
- }
-
- public function createNewUser($nick, $passwd, $email)
- {
- $passwd = $this->generatePasswordHash($nick, $passwd);
- $this->db->query('call add_user(\''.$nick.'\', \''.$passwd.'\', \''.$email.'\');');
- }
-
- public function getUsersNicks()
- {
- $out = $this->select (USERS_TABLE, 'user_id, nick', '', 'user_id ASC');
- if (count($out) > 0)
- return $out;
- else
- return array();
- }
-
- public function getUserInformation($user, $passwd = false)
- {
- if ($this->user_info == null)
- {
- $query = '
- SELECT
- u.nick as nick, u.email as email, u.rank as rank, u.active as active,
- 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 ';
- if ($passwd)
- $query .= ',u.password as password ';
- $query .= '
- FROM '.USERS_TABLE.' u
- LEFT JOIN '.USERS_INFO_TABLE.' ui on ui.user_id = u.user_id
- LEFT JOIN '.USERS_PC_VIEW.' pc on pc.user_id = u.user_id
- LEFT JOIN '.LOGGED_USERS_VIEW.' lu on lu.user_id = u.user_id
- WHERE u.user_id = \''.$user.'\' OR u.nick = \''.$user.'\'';
-
- $out = $this->select_query($query);
- if (count($out) > 0)
- $this->user_info = $out[0];
- }
-
- return $this->user_info;
- }
-
- public function changeUserPassword($user_id, $nick, $passwd)
- {
- $passwd = $this->generatePasswordHash($nick, $passwd);
- $query =
- 'UPDATE .'.USERS_TABLE.'
- SET `password`=\''.$passwd.'\'
- WHERE `user_id` = \''.$user_id.'\'';
-
- $this->db->query($query);
- }
-
- public function changeUserRank($user_id, $rank)
- {
- $query =
- 'UPDATE .'.USERS_TABLE.'
- SET `rank`=\''.$rank.'\'
- WHERE `user_id` = \''.$user_id.'\'';
-
- $this->db->query($query);
- }
-
- public function updateUserProfile($user_id, $nick, $email, $location, $signature, $avatar)
- {
- $query =
- 'UPDATE .'.USERS_TABLE.'
- SET ';
- if ($nick != null)
- $query .= '`nick`=\''.$nick.'\',';
- $query .= '`email`=\''.$email.'\'
- WHERE `user_id` = \''.$user_id.'\'';
-
- $this->db->query($query);
-
- //profile informations
- $query =
- 'UPDATE .'.USERS_INFO_TABLE.'
- SET
- `avatar`=\''.$avatar.'\',
- `location`=\''.$location.'\',
- `signature`=\''.$signature.'\'
- WHERE `user_id` = \''.$user_id.'\'';
-
- $this->db->query($query);
- }
-
- public function getUsers($stype, $sorder, $rank)
- {
- $query = '
- SELECT
- 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
- FROM '.USERS_TABLE.' u
- LEFT JOIN '.USERS_INFO_TABLE.' ui on ui.user_id = u.user_id
- LEFT JOIN '.USERS_PC_VIEW.' pc on pc.user_id = u.user_id ';
- if ($rank !== '')
- $query .= 'WHERE rank=\''.$rank.'\'';
- $query .= '
- ORDER BY '.$stype.' '.$sorder;
-
- $out = $this->select_query($query);
-
- return $out;
- }
-
- public function deleteUser($user_id)
- {
- $query =
- 'DELETE FROM .'.USERS_TABLE.'
- WHERE `user_id` = \''.$user_id.'\'';
- $this->db->query($query);
- }
-
- public function generatePasswordHash($nick, $password)
- {
- $modulo = strlen($nick) % 8;
-
- $salt_begin = substr(RANDOM_STRING, $modulo*SALT_LENGTH, SALT_LENGTH-$modulo);
-
- $salt_end = substr(RANDOM_STRING, $modulo*SALT_LENGTH + SALT_LENGTH-$modulo, $modulo);
-
- return hash('haval256,5', $salt_begin.$password.$salt_end);
- }
- }
- ?>
|