<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/
include_once("../lib/cs_conn.php");
include_once("../lib/cs_common.php");
if(CS_Ucmode==1){
 include_once("../lib/cs_config_uc.php");
 include_once("../lib/client/client.php");
}
@header("Content-type: text/html;charset=utf-8");
//if(!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('qq:848769359');}

$action=CS_Request("action");
$CS_Name=CS_Request("name");
$CS_Pass=CS_Request("pwd");
$loginip=$_SERVER['REMOTE_ADDR'];
$cookietime='3600';
if ($action=="login"){
	global $db;
	if($CS_Name==""){
		exit('Err_100');
	}elseif($CS_Pass==""){
		exit('Err_102');
	}else{
		$sqlj= "select cs_id from ".Getdbname('user')." where cs_name='".$CS_Name."'";
        	$cs_id= $db->Getone($sqlj);
		if(!$cs_id){
	                //--------------------------- Ucenter ---------------------------
			if(CS_Ucmode==1){
			//从UC中获得UC用户ID
			  $ucid = uc_get_user($CS_Name);

			  // 获得后激活帐号
			  if($ucid[0]>0) {

		                  $sql="insert into ".Getdbname('user')." (CS_UID,CS_Name,CS_Pass,CS_Email,cs_addtime) values ('".$ucid[0]."','".$ucid[1]."','".md5($CS_Pass)."','".$ucid[2]."','".date('Y-m-d H:i:s')."')";
		                  if($db->query($sql)){
			                   //发消息通知
			                   $sql="insert into ".Getdbname('xiaoxi')." (cs_userb,cs_usera,cs_name,cs_neir,cs_addtime) values ('系统消息','".$ucid[1]."','系统给您发送了一条消息','欢迎您加入".CS_WebName.",有您的加入我们会更加精彩!','".date('Y-m-d H:i:s')."')";
			                   $db->query($sql);
                                           //登入
                                           $row=$db ->Getrow("select cs_id,cs_name,cs_pass,cs_sid,cs_email from ".Getdbname('user')." where cs_name='".$ucid[1]."'");
				           setcookie("cs_id",$row["cs_id"],time()+$cookietime,'/',Host_User());
				           setcookie("cs_name",$row["cs_name"],time()+$cookietime,'/',Host_User());
				           exit('Err_105');
		                   }
			  }
                        }
	                //--------------------------- Ucenter ---------------------------
                }
		$sql= "select cs_id from ".Getdbname('user')." where cs_name='".$CS_Name."' and cs_pass='".md5($CS_Pass)."'";
        	$cs_id= $db->Getone($sql);
		if($cs_id){
			$db->query("update ".Getdbname('user')." set CS_LogNum=CS_LogNum+1,CS_LogIP ='".$loginip."',CS_LogTime='".date('Y-m-d H:i:s')."' where cs_id='".$cs_id."'");
			$row=$db ->Getrow("select cs_id,cs_name,cs_pass,cs_sid,cs_email from ".Getdbname('user')." where cs_id=".$cs_id."");
			if($row["cs_sid"]==1){
                                exit('Err_104');
			}else{
			  if(CS_LogFun==0){
			    $tishi="b";
			    if(isset($_COOKIE["CS_LoginIP"])){
			        $CS_LoginIP=$_COOKIE["CS_LoginIP"];
			        if($loginip==$CS_LoginIP){
                                         $tishi=="a";
                                }else{
                                       setcookie("CS_LoginIP","",time()-1);
                                }
			    }else{
				setcookie("CS_LoginIP",$loginip,time()+3600);	
			    }
			    if($tishi=="b"){
					setcookie("CS_LoginIP",$loginip,time()+3600);
					$db->query("update ".Getdbname('user')." set CS_Coin=CS_Coin+".CS_LogDs." where Cs_id=".$cs_id."");
			    }
                          }
				setcookie("cs_id",$row["cs_id"],time()+$cookietime,'/',Host_User());
				setcookie("cs_name",$row["cs_name"],time()+$cookietime,'/',Host_User());
				exit('Err_105');
			}
		}else{
                        exit('Err_103');
		}
	}
}

if ($action=="logout"){
	setcookie("cs_id","",time()-1,'/',Host_User());
	setcookie("cs_name","",time()-1,'/',Host_User());
}

if(isset($_COOKIE["cs_name"])){
   $cs_name=$_COOKIE["cs_name"];
}else{
   $cs_name='';
}
if(isset($_COOKIE["cs_id"])){
   $cs_id=$_COOKIE["cs_id"];
}else{
   $cs_id='';
}
$uclogin='';
$row=$db->getrow("Select * from ".Getdbname('user')." where CS_ID=".$cs_id." and cs_name='".$cs_name."'");
if($row){
	//--------------------------- Ucenter ---------------------------
           	if(CS_Ucmode==1){
			//从UC中获得UC用户ID
			$ucid = uc_get_user($cs_name);
			// 获得后同步登陆
			if($ucid[0]>0) {
				$uclogin=uc_user_synlogin($ucid[0]);
			}
                }
	//--------------------------- Ucenter ---------------------------
        $nichen=$row['CS_Nichen'];
        if(empty($row['CS_Nichen'])) $nichen=$row['CS_Name'];
	$str=file_get_contents("../".CS_Templateurl."cs-uinfo.html");
	$str=$str.$uclogin;
	$str=str_replace('{csdjcms:uname}',$_COOKIE["cs_name"],$str);
	$str=str_replace('{csdjcms:uid}',$_COOKIE["cs_id"],$str);
	$str=str_replace('{csdjcms:unichen}',$nichen,$str);
	$str=str_replace('{csdjcms:ucion}',$row["CS_Cion"],$str);
	$str=str_replace('{csdjcms:uvip}',getuser('vip',$row['CS_Name']),$str);
	$str=str_replace('{csdjcms:upic}',piclink('logo',$row['CS_Logo']),$str);
	$str=str_replace('{csdjcms:ulink}',userlink('index',$row['CS_ID']),$str);
	$str=str_replace('{csdjcms:uindex}',CS_WebPath.'user/',$str);
	$str=str_replace('{csdjcms:path}',CS_WebPath,$str);
	echo($str);
}else{
	//--------------------------- Ucenter ---------------------------
	if(CS_Ucmode==1){
           // 获得后同步登出
	   $uclogin=uc_user_synlogout();
        }
	//--------------------------- Ucenter ---------------------------
	$str=file_get_contents("../".CS_Templateurl."cs-ulogin.html");
	$str=$str.$uclogin;
	$str=str_replace('{csdjcms:reglink}',CS_WebPath.'user/reg.php',$str);
	$str=str_replace('{csdjcms:passlink}',CS_WebPath.'user/pass.php',$str);
	$str=str_replace('{csdjcms:qqlink}',CS_WebPath."user/open/qq/qq_login.php",$str);
	echo($str);
}
?>
