<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class licensesModel extends Controller {
	public function getLicenses($param = '') {
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."licenses` ".$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 getLicenseInfo($id){
		global $db;
		$sql = "SELECT * FROM `".$this->cfg('prefix')."licenses` WHERE `license_id` = '{$id}' LIMIT 1";
		$result = $db->query($sql);

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

		if($result->num_rows == 1){
			return true;
		}
		return false;
	}
	public function updateLicenseStatus($licenseid, $status){
		global $db;
		$id = '-1';
		$sql .= "UPDATE `".$this->cfg('prefix')."licenses` SET";
		$sql .= "`license_status` = '{$status}',";
		$sql .= "`license_time_update` = NOW()";
		$sql .= "WHERE `license_id` = '{$licenseid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function buyLicense($licenseid, $month){
		global $db;
		$sql .= "UPDATE `".$this->cfg('prefix')."licenses` SET";
		$sql .= "`license_status` = '1', ";
		$sql .= "`license_buy_time` = NOW() + INTERVAL {$month} MONTH ";
		$sql .= "WHERE `license_id` = '{$licenseid}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function editLicense($licenseid, $domain) {
		global $db;
		$sql .= "UPDATE `".$this->cfg('prefix')."licenses` SET";
		$sql .= "`license_domain` = '{$domain}',";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
	public function addLicense($domain) {
		global $db;
		$sql = "INSERT INTO `".$this->cfg('prefix')."licenses` 
		(	user_id, 
			license_domain,
			license_add_time
		) VALUES (
			'{$_SESSION['user_id']}', 
			'{$domain}',
			NOW())";
		$db->query($sql);
		
		if(!$db->error) {
			return $db->insert_id;
		}
		return false;
	}
	public function deleteLicense($id) {
		global $db;
		$sql = "DELETE FROM `".$this->cfg('prefix')."licenses` WHERE `license_id` = '{$id}'";
		$db->query($sql);
		
		if(!$db->error) {
			return true;
		}
		return false;
	}
}