<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model
{
	
	function __construct(){
        parent::__construct();
		$this->vservices->setApiUrl($this->config->item('api_url'));
		$this->vservices->setConnection($this->curl);
    } 
	
	function checkLogin ($src, $token, $username, $password) 
	{
		$data = $this->vservices->actionExecute('login', array('src' => $src, 'token' => $token, 'username' => $username, 'password' => $password), 'user');		
		$arr_users = explode("&", $data);
		$str_status = "";
		$str_fullname = "";
		$str_usid = "";
		$usid = "";
		$fullname = "";
		for ($i=0; $i<count($arr_users); $i++)
		{
			if (strpos($arr_users[$i], 'status=') !== false)
			{
				$str_status = $arr_users[$i];
			}
			
			if (strpos($arr_users[$i], 'fullname=') !== false)
			{
				$str_fullname = $arr_users[$i];
			}
			
			if (strpos($arr_users[$i], 'id=') !== false)
			{
				$str_usid = $arr_users[$i];
			}
		}
		$arr_status = explode("=", $str_status);
		if ($str_fullname != "")
		{
			$arr_fullname = explode("=", $str_fullname);
			$fullname = $arr_fullname[1];
		}
		if ($str_usid !== "")
		{
			$arr_usid = explode("=", $str_usid);
			$usid = $arr_usid[1];
		}
		$status = (int)$arr_status[1];
		if ($status == 0){
			$us_id = (int)$usid;
			$query = "SELECT * FROM tbluser WHERE us_id = ?";
			$result = $this->db->query($query, array($us_id));
			if($result->num_rows() == 1){
				return $data;
			}else
			{
				return null;
			}
		}else
		{
			return $data;
		}
		
	}
}