<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class usersModel extends Controller {
	public function getUsers($param = '') {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."users` ".$param;
		$result = $db->query($sql);

		$items = array();
		
		if($result->num_rows >= 1){
			while($data = $result->fetch_assoc())
			$items[] = $data;
			
			return $items;
		}
		return false;
	}
	public function checkUser($login) {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."users` WHERE `user_login` = '{$login}' LIMIT 1";
		$result = $db->query($sql);

		if($result->num_rows == 1){
			return $result->fetch_assoc();
		}
		return false;
	}
	public function getUser($id) {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."users` WHERE `user_id` = '{$id}' LIMIT 1";
		$result = $db->query($sql);

		if($result->num_rows == 1){
			return $result->fetch_assoc();
		}
		return false;
	}
	public function changeavatarUser($avatar) {
		global $db;
		$sql = "UPDATE `".$this->cfg('prefix')."users` SET `user_avatar` = '{$avatar}' WHERE `user_id` = '{$_SESSION['user_id']}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function editUser($param, $id) {
		global $db;
		$sql = "UPDATE `".$this->cfg('prefix')."users` SET ".$param." WHERE `user_id` = ".$id;
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function createUser($login, $email, $password, $group = '') {
		global $db;

		if(empty($group)){
			$group = $this->setting('user_group');
		} else {
			$group = $group;
		}
		$sql = "INSERT INTO `".$this->cfg('prefix')."users` 
		(	user_login, 
			user_password, 
			user_email, 
			user_group, 
			user_date_reg, 
			user_ip_reg
		) VALUES (
			'{$login}', 
			'{$password}', 
			'{$email}', 
			'{$group}', 
			NOW(), 
			'{$_SERVER['REMOTE_ADDR']}')";
		$db->query($sql);
		
		if(!$db->error) {
			return $db->insert_id;
		}
		return false;;
	}
	public function downUserBalance($userid, $sum){
		global $db;
		$sql = "UPDATE `".$this->cfg('prefix')."users` SET `user_balance` = user_balance-{$sum} WHERE `user_id` = ".$userid;
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function deleteUser($id) {
		global $db;
		$sql = "DELETE FROM `".$this->cfg('prefix')."users` WHERE `user_id` = '{$id}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function getAuth($param = '') {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."auth_logs` ".$param;
		$result = $db->query($sql);

		$items = array();
		
		if($result->num_rows >= 1){
			while($data = $result->fetch_assoc())
			$items[] = $data;
			
			return $items;
		}
		return false;
	}
	public function addAuth($section, $type, $ip, $login = '', $password = '') {
		global $db;
		$sql = "INSERT INTO `".$this->cfg('prefix')."auth_logs` 
		(	al_section, 
			al_type, 
			al_time, 
			al_ip,
			al_user,
			al_password
		) VALUES (
			'{$section}', 
			'{$type}', 
			NOW(),
			'{$ip}', 
			'{$login}', 
			'{$password}')";
		$db->query($sql);
		
		if(!$db->error) {
			return $db->insert_id;
		}
		return false;;
	}
	public function clearAuth(){
		global $db;
		$sql = "TRUNCATE `".$this->cfg('prefix')."auth_logs`";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;;
	}
	public function getInvoices($id = '') {
		global $db;
		if(!empty($id)){
			$sql = "SELECT * FROM `".$this->cfg('prefix')."invoices` WHERE `user_id` = '".$id."'";
		} else {
			$sql = "SELECT * FROM `".$this->cfg('prefix')."invoices`";
		}
		$result = $db->query($sql);

		$items = array();
		
		if($result->num_rows >= 1){
			while($data = $result->fetch_assoc())
			$items[] = $data;
			
			return $items;
		}
		return false;
	}
	public function addInvoice($type, $id, $sum, $service = '') {
		global $db;
		$sql = "INSERT INTO `".$this->cfg('prefix')."invoices` 
		(	invoice_type, 
			user_id, 
			invoice_sum, 
			invoice_service,
			invoice_time
		) VALUES (
			'{$type}', 
			'{$id}', 
			'{$sum}', 
			'{$service}', 
			NOW())";
		$db->query($sql);
		
		if(!$db->error) {
			return $db->insert_id;
		}
		return false;;
	}
}