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

class Admin_model extends CI_Model{

    public $table = 'admin';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [encrypt 加密方法]
     * @param  [type] $data [需要加密的原数据]
     * @return [type]       [加密后的数据]
     */
    
    public function encrypt($data)
    {
        return md5($data);
    }

    /**
     * [check description]
     * @param  [type] $username [用户名]
     * @param  [type] $password [密码]
     * @return [type]           [bool TRUE or FALSE]
     */
    
    public function check($username,$password)
    {

        $query = $this->db->get_where($this->table,array('username'=>$username));

        if($query->num_rows() > 0)
        {
            $row = $query->row_array();
            if($row["password"] == $this->encrypt($password))
            {
                $_SESSION['userid'] = $row['id'];
                $_SESSION['username'] = $row['username'];
                return TRUE;
            }
        }
        return FALSE;
    }


    /**
     * [pwd_change description]
     * @param  [type] $username [用户名]
     * @param  [type] $oldpwd   [原始密码]
     * @param  [type] $password [新密码]
     * @return [type]           [bool TRUE or FALSE]
     */
    public function pwd_change($username,$oldpwd,$password)
    {

        $query = $this->db->get_where($this->table,array('username'=>$username));

        if($query->num_rows()>0)
        {
            $row = $query->row_array();
            if($row["password"] == $this->encrypt($oldpwd))
            {
                $data = array(
                       'password' => $this->encrypt($password)
                );
                $this->db->where('id', $row['id']);
                $this->db->update($this->table, $data);

                return TRUE;
            }
            
        }
        return FALSE;             
    }
}
/* End of file user.php */
/* Location: /application/models/user.php */