<commit_before><?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * User_model class.
 * 
 * @extends CI_Model
 */
class User_model extends CI_Model {

	/**
	 * __construct function.
	 * 
	 * @access public
	 * @return void
	 */
	public function __construct() 
	{
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * create_user function.
	 * 
	 * @access public
	 * @param mixed $username
	 * @param mixed $email
	 * @param mixed $password
	 * @return bool true on success, false on failure
	 */
	public function create_user($data) 
	{
		if ($data['ManufactureName'] !== '--') 
		{
			$avto = $this->get_avto_id($data);
		}
		else
		{
			$avto = NULL;
		}

		$data1 = array(
			'login'   	=> $data['login'],
			'name'   	=> $data['name'],
			'surname'   => $data['surname'],
			'sex'   	=> $data['sex'],
			'birthsday' => $data['birthsday'],
			'tel'   	=> $data['tel'],
			'email'     => $data['email'],
			'avto_id'	=> $avto,
		);

		$this->db->insert('users', $data1);
		$insert_id = $this->db->insert_id();

		$data2 = array(
			'user_id' 		=> $insert_id,
			'user_enabled'  => $this->config->item('STATUS_ON'),
			'user_rights'   => $this->config->item('user_rights'),
			'password'   	=> $this-><PASSWORD>($data['password']),
		);
		
		$this->db->insert('authorization', $data2);
		
		return TRUE;
	}	
	
	private function get_avto_id($data) 
	{
		$this->db->select('id');
		$this->db->from('avto');
		$this->db->where('manufacture', $data['ManufactureName']);
		$this->db->where('model', $data['ModelName']);
		$this->db->where('year', $data['year']);
		$query = $this->db->get();

		$avto_id = $query->result_array();
		$avto = $avto_id['0']['id'];

		return $avto;
	}

	/**
	 * resolve_user_login function.
	 * 
	 * @access public
	 * @param mixed $username
	 * @param mixed $password
	 * @return bool true on success, false on failure
	 */
	public function resolve_user_login($login, $password) 
	{
		$this->db->select('authorization.password as password');
		$this->db->from('authorization');
		$this->db->join('users', 'authorization.user_id = users.id', 'inner');
		$this->db->where('users.login', $login);
		$hash = $this->db->get()->row('password');
		
		return $this->verify_password_hash($password, $hash);
	}
	
	/**
	 * get_user_id_from_username function.
	 * 
	 * @access public
	 * @param mixed $username
	 * @return int the user id
	 */
	public function get_user_id_from_username($login) 
	{
		$this->db->select('id');
		$this->db->from('users');
		$this->db->where('login', $login);

		return $this->db->get()->row('id');
	}
	
	/**
	 * get_user function.
	 * 
	 * @access public
	 * @param mixed $user_id
	 * @return object the user object
	 */
	public function get_user($user_id) 
	{
		$this->db->from('users');
		$this->db->where('id', $user_id);
		return $this->db->get()->row();
	}

	public function get_user_data($user_id)
	{
		$this->db->from('authorization');
		$this->db->where('user_id', $user_id);
		return $this->db->get()->row();
	}
	
	/**
	 * hash_password function.
	 * 
	 * @access private
	 * @param mixed $password
	 * @return string|bool could be a string on success, or bool false on failure
	 */
	private function hash_password($password) 
	{
		return md5($password);
	}
	
	/**
	 * verify_password_hash function.
	 * 
	 * @access private
	 * @param mixed $password
	 * @param mixed $hash
	 * @return bool
	 */
	private function verify_password_hash($password, $hash) 
	{
		return $this->hash_password($password) === $hash;
	}	

	public function set_user_session()
	{
		$data = array(
			'session_id' => session_id(),
			'ip_address'  => $this->input->ip_address(),
			'last_activity'   => '1',
			'user_agent'   	=> '1',
			'user_id' => $this->session->userdata('user_id'),
		);

		$this->db->insert('ci_session', $data);
	}

	public function get_user_session($session_id)
	{
		$this->db->from('ci_session');
		$this->db->where('session_id', $session_id);
		return $this->db->get()->row();
	}

	public function get_users()
	{
		$this->db->select('users.login, users.name, users.surname, users.sex, <commit_msg>Some comments and suggwstions added to user model<commit_after><?php
defined('BASEPATH') OR exit('No direct script access allowed');

//auto, not aVto ;)

/**
 * User_model class.
 * 
 * @category Database
 * @package	Pdr
 * @subpackage Pdr
 * @author <NAME> <<EMAIL>>
 * @license private 	
 * @link https://github.com/Bandydan/pdr
 */
class User_model extends CI_Model {

	/**
	 * Almost empty constructor
	 * 
	 * @return void
	 */
	public function __construct() 
	{
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * Method creates user by given data.
	 * 
	 * @param array $data - array of incoming user data
	 * 
	 * @return bool true on success, false on failure
	 */
	public function create_user($data) 
	{
		$avto = NULL;
		if ($data['ManufactureName'] !== '--') 
		{
			$avto = $this->_get_avto_id($data);
		}

		//TODO: Why don't you use $data itself to insert into database?
		$data1 = array(
			'login'   	=> $data['login'],
			'name'   	=> $data['name'],
			'surname'   => $data['surname'],
			'sex'   	=> $data['sex'],
			'birthsday' => $data['birthsday'],
			'tel'   	=> $data['tel'],
			'email'     => $data['email'],
			'avto_id'	=> $avto,
		);

		$this->db->insert('users', $data1);
		$insert_id = $this->db->insert_id();

		$data2 = array(
			'user_id' 		=> $insert_id,
			'user_enabled'  => $this->config->item('STATUS_ON'),
			'user_rights'   => $this->config->item('user_rights'),
			'password'   	=> $<PASSWORD>($data['password']),
		);
		
		$this->db->insert('authorization', $data2);
		
		return TRUE;
	}	
	
	/**
	 * @access private
	 * Method gets id of given auto by its data
	 * TODO:
	 * @deprecated METHOD SHOULD BE REMOVED, ID SHOULD COME FROM THE FORM!!!
	 * @param array $data 
	 * @return integer
	 * 
	 */
	private function _get_avto_id($data) 
	{
		$this->db->select('id');
		$this->db->from('avto');
		$this->db->where('manufacture', $data['ManufactureName']);
		$this->db->where('model', $data['ModelName']);
		$this->db->where('year', $data['year']);
		$query = $this->db->get();

		$avto_id = $query->result_array();
		$avto = $avto_id['0']['id'];

		return $avto;
	}


//I would suggest to make one check_password function from
// resolve_user_login and _verify_password_hash functions.
	/**
	 * Gets user password from DB and compares it with the given one.
	 * 
	 * @access public
	 * @param mixed $login    user login
	 * @param mixed $password user password
	 * @return bool true on success, false on failure
	 */
	public function resolve_user_login($login, $password) 
	{
		$this->db->select('authorization.password as password');
		$this->db->from('authorization');
		$this->db->join('users', 'authorization.user_id = users.id', 'inner');
		$this->db->where('users.login', $login);
		$hash = $this->db->get()->row('password');
		
		return $this->verify_password_hash($password, $hash);
	}
	
	/**
	 * get_user_id_from_username function.
	 * 
	 * @access public
	 * @param mixed $login - Login
	 * @return integer - the user id
	 */
	public function get_user_id_from_username($login) 
	{
		$this->db->select('id');
		$this->db->from('users');
		$this->db->where('login', $login);

		return $this->db->get()->row('id');
	}
	
	/**
	 * get_user function.
	 * 
	 * @param integer $user_id - Id of user
	 * @return object the user object
	 */
	public function get_user($user_id) 
	{
		$this->db->from('users');
		$this->db->where('id', $user_id);
		return $this->db->get()->row();
	}

	/**
	 * Method gets user data by its ID
	 * @param integer $user_id - ID of user
	 * @return array of user data
	 */
	public function get_user_data($user_id)
	{
		$this->db->from('authorization');
		$this->db->where('user_id', $user_id);
		return $this->db->get()->row();
	}



//I suggest changing md5 to http://php.net/manual/ru/function.hash.php with sha256 and use its part (substr) to compare
	/**
	 * hash_password function.
	 * 
	 * @access private
	 * @param mixed $password - password
	 * @return string|bool could be a string on success, or bool false on failure
	 */
	private function _hash_password($password) 
	{
		return md5($password);
	}
	
	/**
	 * verify_password_hash function.
	 * 
	 * @access private
	 * @param mixed $password - password
	 * @param mixed $hash     - hash
	 * @return bool
	 */
	private function _verify_password_hash($password, $hash) 
	{
		return $this->_hash_password($password) === $hash;
	}	

	/**
	 * Method saves user session additional data to DB
	 * @return void
	 */
	public function set_user_session()
	{
		$data = array(
			'session_id' => session_id(),
			'ip_address'  => $this->input->ip_address(),
			'last_activity'   => '1',
			'user_agent'   	=> '1',
			'user_id' => $this->session->userdata('user_id'),
		);

		$this->db->insert('ci_session', $data);
	}

	/**
	 * Method retrieves user session additional data from DB
	 * @param string $session_id 
	 * @return array of data
	 */
	public function get_user_session($session_id)
	{
		$this->db->from('ci_session');
		$this->db->where('session_id', $session_id);
		return $this->db->get()->row();
	}

	/**
	 * Method gets array of users from DB
	 * 
	 * @return query result array
	 */
	public function get_users()
	{

		//I suggest moving user fields to array and then to join it using implode. Instead of user table mentioning, it'd be better to use u. and then user as u
		$this->db->select('users.login, users.name, users.surname, users.sex, 
{'fraction_non_alphanumeric': 0.18837675350701402, 'fraction_numerical': 0.002109482122139015, 'mean_word_length': 3.5045130641330164, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 197, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1535779', 'n_tokens_mistral': 3662, 'n_tokens_neox': 3230, 'n_words': 740}