<?php

/**
 * @filename member.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-11-6  9:44:16
 * @version 1.0
 * @Description
 * 会员
 * 网站可能需要，可能不需要，
 * 如果有，整个网站的会员是唯一的
 */
class Member extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('parser');
        $this->load->helper('url');
    }

    /*
     * 登录
     */

    public function login($type_id = 1) {
        
    }

    /*
     * 注册
     */

    public function to_register() {
        $this->load->add_package_path(APPPATH . '../package_ssioo', false);
        $data = lz_tag();
        $this->parser->parse('register', $data);
    }

    /*
     * 注册
     */

    public function register() {

        $email = $_POST['email'];

        $sql = "select * from cms_user where email = '{$email}'";

        if ($this->db->query($sql)->num_rows() > 0) {
            echo "email重复";
            return;
        }

        $user['email'] = $email;
        $user['name'] = $_POST['name'];
        $user['remark'] = $_POST['remark'];
        $user['role_id'] = 5;
        $password = $this->getRandom(6);
        $user['password'] = md5($password);

        $this->db->insert('cms_user', $user);

        $this->load->library('email');              //加载email类
        $this->email->to($email);               //发送邮件到指定邮箱      
        $this->email->from('1657112878@qq.com', '我是发件人 管理员');
        $this->email->subject('ssioo导航插件 客户密码');
        $this->email->message('你好！' . $user['name']. ' 这是你的新密码' . $password . '，请及时修改密码！');
        $this->email->send();
        echo "注册成功，登录密码已经发到您的邮箱，请接收";
    }

    /*
     * 客户修改个人信息
     */

    public function manage() {
        $data = lz_tag();
        $current_admin_user = $this->session->userdata("current_admin_user");
        $data['user'] = array($current_admin_user);
        $this->parser->parse('member_manage', $data);
    }

    /*
     * 客户修改个人信息
     * ajax
     */

    public function info_update() {
        $current_user = $this->session->userdata("current_user");

        $email = $_POST['email'];

        $sql = "select * from cms_user where id != {$current_user->id} and email = '{$email}'";
        lz_log($sql);
        if ($this->db->query($sql)->num_rows() > 0) {

            echo "email重复";
            return;
        }
        echo "OK";
    }

    /**
     * 重置密码，发送新密码给用户
     */
    public function get_password() {
        $email = $this->input->post('email');

        $password = $this->getRandom(6); //调用函数，随机生成6位数密码     
        $this->db->where('email', $email);
        $query = $this->db->get('cms_user');
        if ($query->num_rows()) {
            $users = $query->result();
            $user = $users[0];
            $this->db->update('cms_user', array('password' => md5($password)), array('id' => $user->id));
            $this->load->library('email');              //加载email类
            $this->email->to($email);               //发送邮件到指定邮箱      
            $this->email->from('1657112878@qq.com', '我是发件人 管理员');
            $this->email->subject('ssioo导航插件 客户密码');
            $this->email->message('你好！' . $user->name . ' 这是你的新密码' . $password . '，请及时修改密码！');
            $this->email->send();
            echo "OK";
        } else {
            echo "该邮箱没有被注册！";
        }
    }

    /* 产生随机密码 */

    function getRandom($leng) {                     //$leng传你所有的长度
        $a = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //加小写字母都可以
        $str = null;
        for ($i = 0; $i < $leng; $i++) {
            $str .= $a[rand(0, strlen($a) - 1)];
        }
        return $str;
    }

}

/* End of file : member.php */
