<?php

/**
 * @filename user.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2014-5-16  9:02:27
 * @version 1.0
 * @Description
 */
class User extends CI_Controller {

    var $data = array();

    function __construct() {
        parent::__construct();
        $this->load->add_package_path(APPPATH . '../package_main', false);
        $this->load->helper('url');
        $this->load->model('model_cmf');
        $this->data['频道名称'] = "登陆";
    }

    /*
     * 普通用户前台注册
     */

    public function register() {

//        $saved = $this->session->userdata("captcha_code");
//        if($this->input->post('captcha_code') != $saved){
//            echo "验证码错误!";
//            return;
//        }

        $this->load->helper(array('form', 'url'));
        $data = lz_tag();
        $data['channel'] = "index";
        if (!$_POST) {
            //无上传数据
            $this->parser->parse("register", $data);
            return;
        }
        /*
         * 验证
         */
        if (!$this->validate_reg_form()) {
            $this->parser->parse("register", $data);
            return;
        }
        /*
         * 验证通过
         */
        $password = $this->input->post('password', true);
        $qq_userid = $this->session->userdata("qq_userid");
        $data = array(
            'username' => strip_tags($this->input->post('username')),
            'password' => md5($password),
            'email' => $this->input->post('email', true),
            'regtime' => time(),
            'is_active' => 1,
            'qq_userid' => $qq_userid
        );

        $this->db->insert('users', $data);
        $sql = "select * from users order by id desc limit 0,1";
        $current_user = $this->db->query($sql)->row();
        $this->session->set_userdata('current_user', $current_user);
        redirect("/news/index");
    }

    /*
     * 登录
     */

    public function login() {

        $this->load->helper(array('form', 'url'));
        $data = lz_tag();
        $data['channel'] = "index";
        if (!$_POST) {
            //无上传数据
            $this->parser->parse("login", $data);
            return;
        }
        /*
         * 验证
         */

        if (!$this->validate_login_form()) {
            $this->parser->parse("login", $data);
            return;
        }
        /*
         * 验证通过
         */
        $sql = "select * from users where email = '" . $this->input->post('email', true) . "'";
        $current_user = $this->db->query($sql)->row();
        $this->session->set_userdata("current_user", $current_user);
        redirect("/news/index");
    }

    public function logout() {
        $this->session->unset_userdata('current_user');
        redirect("/news/index");
    }

    /*
     * 注册表单验证
     */

    private function validate_reg_form() {
        $data['channel'] = "index";
        $this->load->library('form_validation');

        $this->form_validation->set_rules('captcha_code', '验证码', 'trim|required|valid_captcha');
        $this->form_validation->set_rules('email', '邮箱', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('username', '昵称', 'trim|required|min_length[3]|max_length[20]|xss_clean');

        if ($this->form_validation->run() == FALSE) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    private function validate_login_form() {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('captcha_code', '验证码', 'trim|required|valid_captcha');
        $this->form_validation->set_rules('email', '邮箱', 'trim|required|valid_email|valid_exist[users.email]');
        $this->form_validation->set_rules('password', '密码', 'trim|required|callback_password_check');

        if ($this->form_validation->run() == FALSE) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function password_check($str) {
        $data['channel'] = "index";
        $sql = "select * from users where email = '" . $this->input->post('email', true) . "'";
        if ($this->db->query($sql)->num_rows() == 0) {
            return false;
        }
        $current_user = $this->db->query($sql)->row();

        if (md5($str) != $current_user->password) {
            $this->form_validation->set_message('password_check', '密码错误');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function test() {
        $data = lz_tag();
        if ($_POST == null) {
            $this->parser->parse(__FUNCTION__, $data);
            return;
        }


        $url = $_POST['url'];
        
        echo stripslashes($url);
    }

}

/* End of file : user.php */
