<?php
/****************优客365网址导航系统 商业版********************/
/*                                                            */
/*  Youke365.com (C)2017 Youke365 Inc.                        */
/*  This is NOT a freeware, use is subject to license terms   */
/*  优客365网址导航系统是商业软件,使用于商业用途请购买授权    */
/*  V1.0 2017                                                 */
/*  技术支持：1513773524@qq.com                               */
/*  官方网址：http://www.youke365.com                         */
/*  官方论坛：http://bbs.youke365.com                         */                           
/**************************************************************/

if (!defined('IN_YOUKE365')) exit('Access Denied');

$pagename = '登录账户';
$pageurl = '?mod=login';
$tplfile = 'login.html';
$table = $DB->table('users');

if (!$smarty->isCached($tplfile)) {
	$smarty->assign('site_title', $pagename.' - '.$options['site_name']);
	$smarty->assign('site_keywords', $options['site_keywords']);
	$smarty->assign('site_description', $options['site_description']);
	$smarty->assign('site_path', get_sitepath().' &raquo; '.$pagename);
    
    if (!empty($_POST['action']) && $_POST['action'] == 'login') {
		$nick_name = I($_POST['nick_name']);
		$user_pass = I($_POST['pass']);
		$open_id = I($_POST['open_id']);
		
		if (empty($nick_name)) {
			msgbox('用户名为空或者错误！');
		}
        
		if (empty($user_pass)) {
			msgbox('请输入登陆密码！');
		}
		
		$newpass = md5($user_pass);
		$user = $DB->fetch_one("SELECT user_id, user_pass, login_time, login_count FROM $table WHERE nick_name='$nick_name'");
		// print_r($user);
		if (!$user) {
			msgbox('用户名或密码错误，请重试！');
		} else {
            if ($newpass != $user['user_pass']) {
            	msgbox('用户名或密码错误，请重试！');
            } else {
				//积分
				if (datediff('h', $user['login_time']) == 24) {
					$DB->query("UPDATE $table SET user_score=user_score+1 WHERE user_id=".$user['user_id']." LIMIT 1");
				}
				
				$ip_address = sprintf("%u", ip2long(get_client_ip()));
            	$login_count = $user['login_count'] + 1;
				
				$data = array(
					'login_time' => time(),
					'login_ip' => $ip_address,
					'login_count' => $login_count,
					'open_id'=>$open_id,
				);
				$where = array('user_id' => $user['user_id']);
				$DB->update($table, $data, $where);
				
				$auth_cookie = authcode("$user[user_id]|$newpass|$login_count");
				$expire = time() + 3600 * 24;
				setcookie('auth_cookie', $auth_cookie, $expire, $options['site_root']);
			
				redirect('?mod=home');
			}
		}
	}
}

smarty_output($tplfile);
?>