<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
     *
     */
    class Login_model extends CI_Model {
        
        public function __construct() {
            parent::__construct();
        }
        
        public function login_user($username,$password)
        {
            $this->db->where('correo',$username);
            $this->db->where('contrasena',$password);
            $query = $this->db->get('usuario');
            if($query->num_rows() == 1)
            {
                return $query->row();
            }else
            {
                return null;
            }
        }
        
        public function login_correcto($username,$password)
        {
            $this->db->where('correo',$username);
            $this->db->where('contrasena',$password);
            $query = $this->db->get('usuario');
            if($query->num_rows() == 1)
            {
                return true;
            }else{
                return false;
            }
        }

        public function existeLogin($username)
        {
            $this->db->where('correo',$username);
            $query = $this->db->get('usuario');
            if($query->num_rows() == 1)
            {
                return true;
            }else{
                return false;
            }
        }
        
        public function actualiza_login($username,$password)
        {
            $this->db->where('correo',$username);
            $query = $this->db->update('usuario', array('contrasena'=>$password, 'restablecer'=>''));
        }
        
        public function permiteCambiarContrasena($username,$guid)
        {
            $this->db->where('correo',$username);
            $this->db->where('restablecer',$guid);
            $query = $this->db->get('usuario');
            if($query->num_rows() == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        
        public function estableceGUID($username,$guid)
        {
            $this->db->where('correo',$username);
            $query = $this->db->update('usuario', array('restablecer'=>$guid));
        }
        
        function guid(){
            if (function_exists('com_create_guid'))
            {
                return com_create_guid();
            }
            else
            {
                mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
                $charid = strtoupper(md5(uniqid(rand(), true)));
                $hyphen = chr(45);// "-"
                $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
                return $uuid;
            }
        }
    }
