<?php

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

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Infoauthors_model');
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            $http_origin = $_SERVER['HTTP_ORIGIN'];
            $allowed_domains = array(
              'https://cht.mycht.cn',
              'https://origin-cht.mycht.cn'
            );

            if (in_array($http_origin, $allowed_domains))
            {
                header("Access-Control-Allow-Origin: $http_origin");
            }
        }
    }

    public function index() {
        if ($this->permission->is_admin(false) === false) {
            $data = array();
            $this->load->view('header', $data);
            $this->load->view('login');
            $this->load->view('footer');
        } else {
            redirect(site_url());
        }
    }

    //登录检测
    public function login_submit() {
        //如果已经登陆则跳到管理首页
        if ($this->session->userdata('session_admin')) {
            $data[] = array('name' => 'go', 'value' => site_url());
            echo json_encode($data);
            return TRUE;
        }

        $this->form_validation->set_rules('email', 'lang:login_email', 'required');
        $this->form_validation->set_rules('password', 'lang:login_password', 'required');

        if ($this->form_validation->run() == FALSE) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
            return FALSE;
        } else {
            if ($this->Infoauthors_model->check_login($this->input->post('email'), $this->input->post('password'))) {
                $this->session->set_userdata('session_admin', $this->Infoauthors_model->detail($this->input->post('email')));
                //记住账号密码
                $this->input->set_cookie("user_email", $this->input->post('email'), 7776000);
                $this->input->set_cookie("user_pwd", $this->input->post('password'), 7776000);
                $data[] = array('name' => 'go', 'value' => site_url());
                echo json_encode($data);
                return TRUE;
            }

            //用户名和密码不正确，或者没有管理权限
            if ($this->session->userdata('session_admin') === false) {
                $data[] = array('name' => 'login_warning', 'value' => $this->lang->line('login_warning'));
                echo json_encode($data);
            }
        }
    }

    public function signup() {
        $data = array();
        $data['signup_step'] = 1;
        $data['author_field'] = $this->load->view('author_field', $data, true);
        $this->load->view('header', $data);
        $this->load->view('signup');
        $this->load->view('footer');
    }

    public function signup_vpn() {
        $data = array();
        $data['signup_step'] = 1;
        $data['terms'] = 'vpn';
        $data['navbar_active'] = false;
        $data['author_field'] = $this->load->view('author_field', $data, true);
        $this->load->view('header', $data);
        $this->load->view('signup');
        $this->load->view('footer');
    }

    public function signup_submit() {
        $this->form_validation->set_rules('a_email', 'lang:signup_a_email', 'required');
        $this->form_validation->set_rules('a_password', 'lang:signup_a_password', 'required');
        $this->form_validation->set_rules('a_name', 'lang:signup_a_name', 'required');
        $this->form_validation->set_rules('a_bank', 'lang:a_bank', 'required');
        $this->form_validation->set_rules('a_bank_card', 'lang:a_bank_card', 'required');
        $this->form_validation->set_rules('terms', 'You must choose this option!', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
            return FALSE;
        } else {
            if ($this->Infoauthors_model->check_signup($this->input->post('a_email'))) {
                $data[] = array('name' => 'a_email', 'value' => 'Someone already has that email. Try another?');
                echo json_encode($data);
                return FALSE;
            }
            //头像处理
            $a_photo = '';
            if ($this->input->post('profile_url') != '') {
                $profile_file_path = APPPATH . 'document/profile_photo';
                $profile_url = $this->input->post('profile_url');
                $profile_array = explode('.', $profile_url);
                $profile_ext = 'jpg'; //end($profile_array);
                rename($profile_file_path . '/' . $profile_url, $profile_file_path . '/profile_' . $this->input->post('a_email') . '.' . $profile_ext);
                rename($profile_file_path . '/' . str_replace('_thumb', '', $profile_url), $profile_file_path . '/profile_' . $this->input->post('a_email') . '_org.' . $profile_ext);
                $a_photo = 'profile_' . $this->input->post('a_email') . '.' . $profile_ext;
            }

            //处理上传的身份证复印件
            if ($this->input->post('ID_file_name') != '') {
                $ID_file_path = APPPATH . 'document/ID_CARD';
                $a_id_file = $this->input->post('ID_file_name');
                $file_array = explode('.', $a_id_file);
                $ext = 'jpg'; //end($file_array);
                rename($ID_file_path . '/' . $a_id_file, $ID_file_path . '/id_card_' . $this->input->post('a_email') . '.' . $ext);
            }
            $a_name = trim($this->input->post('a_name'));
            $a_name_cn = trim($this->input->post('a_name_cn'));
            $this->Infoauthors_model->add(
                    $this->input->post('a_email'), $this->input->post('a_password'), $a_name, $a_name_cn, $a_photo, $this->input->post('a_mobile_phone'), $this->input->post('a_phone'), $this->input->post('a_id_card'), $this->input->post('a_gender'), $this->input->post('a_address'), $this->input->post('a_school'), $this->input->post('a_bank'), $this->input->post('a_bank_card'), $this->input->post('a_resume')
            );

            $data[] = array('name' => 'go', 'value' => site_url('login/signup_success'));
            echo json_encode($data);
            return TRUE;
        }
    }

    public function signup_success() {
        $data['title'] = $this->lang->line('signup_success_title');
        $data['content'] = $this->lang->line('signup_success');
        $this->load->view('header', $data);
        $this->load->view('message');
        $this->load->view('footer');
    }

    public function upload_ID_file($profile = false) {
        $save_path = 'ID_CARD';
        $jsfunction = 'get_id_card_file';
        $upload_id = 'ID_file';
        if ($profile) {
            $save_path = 'profile_photo';
            $jsfunction = 'get_photo_url';
            $upload_id = 'Profile_file';
        }
        $config['upload_path'] = APPPATH . 'document/' . $save_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = true;

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload($upload_id)) {
            $error = array('error' => $this->upload->display_errors());
            var_dump($error);
        } else {
            $data = array('upload_data' => $this->upload->data());
            $file_name = $data['upload_data']['file_name'];
            if ($profile) {
                $baseurls = str_replace('controllers', '', dirname(__FILE__));
                $config['image_library'] = 'gd2';
                $config['source_image'] = $baseurls . 'document/profile_photo/' . $file_name;
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = false;
                $config['width'] = 175;
                $config['height'] = 175;
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                $profile_array = explode('.', $file_name);
                $file_name = $profile_array[0] . '_thumb.' . $profile_array[1];
            }
            echo "<script>parent.$jsfunction('$file_name');</script>";
        }
    }

    public function findpwd() {
        $data = array();
        $this->load->view('header', $data);
        $this->load->view('findpwd');
        $this->load->view('footer');
    }

    //找回密码发送邮件
    public function do_findpwd() {
        $this->form_validation->set_rules('email', 'lang:login_email', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data = array();
            $data[] = array('name' => 'email', 'value' => 'Please fill in your email address!');
            echo json_encode($data);
            return FALSE;
        }

        $userdata = $this->Infoauthors_model->detail($this->input->post('email'));
        if ($userdata) {
            $fromName = 'author site';
            $fromEmail = 'noreply02@chinahighlights.net';
            $toName = $userdata->a_name;
            $toEmail = $userdata->a_email;
            $subject = 'Your password for "http://author.mycht.cn"';
            $body = 'Your password for "http://author.mycht.cn" : ' . $userdata->a_password . '<br><br><br><br>For security please change your password once logged in.';
            $is_sendmail = $this->Infoauthors_model->SendMail($fromName, $fromEmail, $toName, $toEmail, $subject, $body);
            if ($is_sendmail) {
                $data[] = array('name' => 'ok', 'value' => 'Action successful. Your password has been emailed to you.');
                echo json_encode($data);
            } else {
                $data[] = array('name' => 'no', 'value' => 'Action unsuccessful. Try again later.!');
                echo json_encode($data);
                return FALSE;
            }
        } else {
            $data[] = array('name' => 'no', 'value' => 'Error: Your email is incorrect!');
            echo json_encode($data);
            return FALSE;
        }
    }

    //退出登录，清空session
    public function out() {
        $this->session->unset_userdata('session_admin');
        $this->load->helper('cookie');
        //delete_cookie("user_email");
        delete_cookie("user_pwd");
        redirect(site_url('login'));
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
