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

class Edit extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
		$this->CsdjUser->User_Login();
	}

        //资料
	public function index()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-index.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editsave]",site_url('user/edit/save'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

        //头像
	public function logo()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-logo.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editsave]",site_url('user/edit/logo_save'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

        //密码
	public function pass()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-pass.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editsave]",site_url('user/edit/pass_save'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

        //主页相关
	public function web()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-web.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editsave]",site_url('user/edit/web_save'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }
		 //认证相关
	public function renz()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-renz.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editrenz]",site_url('m/sfpt'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }
		//签到
	public function qian()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-qian.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editrenz]",site_url('m/sfpt'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }
		//注册推广
	public function tui()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-tui.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editrenz]",site_url('m/sfpt'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }
		//主页播放器
	public function bfq()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-bfq.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","播放器-设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editbfq]",site_url('user/edit/web_bfq'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

	//卡密充值
	public function km()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-km.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","卡密充值-设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editkm]",site_url('user/edit/web_km'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }
//金币提现
	public function tixian()
	{
                $data='';
                $this->load->get_user_templates();
		$template=$this->load->view('edit-tixian.html',$data,true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

		$Mark_Text=str_replace("{cscms:title}","金币提现-设置 - 会员管理中心",$template);
		$Mark_Text=str_replace("{cscms:keywords}",stripslashes(html_entity_decode(Web_Keywords)),$Mark_Text);
		$Mark_Text=str_replace("{cscms:description}",stripslashes(html_entity_decode(Web_Description)),$Mark_Text);
		$Mark_Text=str_replace("[user:editkm]",site_url('user/edit/web_tixian'),$Mark_Text); //修改连接

	        $row=$this->CsdjDB->get_select ('user','CS_ID','*',''.$this->session->userdata('cs_id').'');
		$Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
		$Mark_Text=$this->CsdjSkins->Common_Mark($Mark_Text,$this->session->userdata('cs_name'));
                $this->CsdjSkins->skins($Mark_Text,$data);
        }

        //资料修改
	public function save()
	{

                $edit['cs_nichen']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_nichen', TRUE));
                $edit['cs_email']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_email', TRUE));
                $edit['cs_qq']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_qq', TRUE));
    $edit['cs_zy']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_zy', TRUE));
	    $edit['cs_gq']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_gq', TRUE));
		 $edit['cs_gqbfq']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_gqbfq', TRUE));
                $edit['cs_sex']=intval($this->input->post('cs_sex', TRUE));
				$edit['cs_qg']=intval($this->input->post('cs_qg', TRUE));
                $edit['cs_city']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_city', TRUE));
                $edit['cs_qianm']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_qianm', TRUE));

                if(empty($edit['cs_city'])){
                     $sheng=$this->CsdjSkins->str_checkhtml($this->input->post('sheng', TRUE));
                     $shi=$this->CsdjSkins->str_checkhtml($this->input->post('shi', TRUE));
                     $qu=$this->CsdjSkins->str_checkhtml($this->input->post('qu', TRUE));
                     $edit['cs_city']=$sheng.'-'.$shi.'-'.$qu;
                     $edit['cs_city']=str_replace("---", "",$edit['cs_city']);
                     $edit['cs_city']=str_replace("--", "",$edit['cs_city']);
                }

                if(empty($edit['cs_nichen'])) $this->CsdjSkins->Msg_url('抱歉，呢称不能为空!','javascript:history.back();');  
                if(empty($edit['cs_email'])) $this->CsdjSkins->Msg_url('抱歉，油箱不能为空!','javascript:history.back();');  

                //修改
                $this->CsdjDB->get_update('user',$this->session->userdata('cs_id'),$edit);
          
                $this->CsdjSkins->Msg_url('恭喜您，资料修改成功!',@$_SERVER['HTTP_REFERER']);  
        }

        //头像修改
	public function logo_save()
	{
                $edit['cs_logo']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_logo', TRUE));
                if(empty($edit['cs_logo'])){ //FLASH上传

                            $tempFile = file_get_contents("php://input");

		            $picname=$this->session->userdata('cs_id').".jpg"; 
		            $filename = "./attachment/logo/".$this->session->userdata('cs_id').".jpg"; 

		            if (!empty($tempFile)) {

	    	    		if($handle=fopen($filename,"w+")) {   
		    	    		if(!fwrite($handle,$tempFile)==FALSE)
		    	    		{   
		    	    			fclose($handle);
		    	    		}
	    	    		} 
 
                                //生成小图
		                $this->load->model('CsdjPic');
                                $this->CsdjPic->resize($filename,70,70);

                                    if(FTP_Fun==1){  //开启远程附件
                                          $this->load->library('ftp');
                                          $FTPUP['hostname'] = FTP_Server;
                                          $FTPUP['port']     = FTP_Port;
                                          $FTPUP['username'] = FTP_Name;
                                          $FTPUP['password'] = FTP_Pass;
                                          $FTPUP['passive']  = FTP_Ive;
                                          $this->ftp->connect($FTPUP);

                                          $targetPath = './attachment/logo/';
                                          //----------创建目录------------------------
                                          $Dirs=FTP_Dir;
                                          if(substr($Dirs,0,1)=='/') $Dirs=substr($Dirs,1);
                                          if(substr($Dirs,-1)=='/') $Dirs=substr($Dirs,0,-1);
                                          if(!empty($Dirs)) $Dirs.='/';
                                          $All=explode("/",$targetPath);
                                          $Dir1='./'.$Dirs.$All[1].'/';
                                          $this->ftp->mkdir($Dir1, DIR_WRITE_MODE); 
                                          $Dir2=$Dir1.$All[2].'/';
                                          $this->ftp->mkdir($Dir2, DIR_WRITE_MODE); 

                                          //----------上传文件------------------------
                                          $this->ftp->mkdir($targetPath, DIR_WRITE_MODE); 
                                          $filenames=$Dir2.$this->session->userdata('cs_id').".jpg";
                                          $this->ftp->upload($filename, $filenames);
                                          $this->ftp->upload($filename.'.small.jpg', $filenames.'.small.jpg');
                                          $this->ftp->close();

	                                  @unlink($filename);
	                                  @unlink($filename.'.small.jpg');

                                     }

                                     //写入数据库
                                     $this->db->query("update ".CS_SqlPrefix."user set cs_logo='".$this->session->userdata('cs_id').".jpg' where cs_id='".$this->session->userdata('cs_id')."'");
                                     exit('UploadPicSucceed');
                               
		            } else {
			            exit('UploadPicError');
		            }

                }
        }

        //资料密码
	public function pass_save()
	{
                $cs_pass=$this->input->post('cs_pass', TRUE);
                $cs_pass1=$this->input->post('cs_pass1', TRUE);
                $cs_pass2=$this->input->post('cs_pass2', TRUE);

                if(empty($cs_pass) || empty($cs_pass1)) $this->CsdjSkins->Msg_url('抱歉，原密码和新密码不能为空!','javascript:history.back();');  
                if($cs_pass1!=$cs_pass2) $this->CsdjSkins->Msg_url('抱歉，两次密码不一直!','javascript:history.back();'); 
                if(md5(md5($cs_pass))!=$this->session->userdata('cs_pass')) $this->CsdjSkins->Msg_url('抱歉，原密码不正确!','javascript:history.back();'); 

                $edit['cs_pass']=md5($cs_pass2);
                //修改
                $this->CsdjDB->get_update('user',$this->session->userdata('cs_id'),$edit);
                //更新session
                $ulogin['cs_pass']=md5($edit['cs_pass']);
                $this->session->set_userdata($ulogin);

                $this->CsdjSkins->Msg_url('恭喜您，密码修改成功!',@$_SERVER['HTTP_REFERER']);  
        }

        //主页相关修改
	public function web_save()
	{
                $edit['cs_bgpic']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_bgpic', TRUE));
                $edit['cs_toppic']=$this->CsdjSkins->str_checkhtml($this->input->post('cs_toppic', TRUE));
		if(!empty($edit['cs_bgpic']) && !preg_match("/^[a-z | 0-9 | A-Z| \.\_\/]+$/",$edit['cs_bgpic']))  $this->CsdjSkins->Msg_url('背景图片格式错误!','javascript:history.back();'); 
		if(!empty($edit['cs_toppic']) && !preg_match("/^[a-z | 0-9 | A-Z| \.\_\/]+$/",$edit['cs_toppic']))  $this->CsdjSkins->Msg_url('顶部图片格式错误!','javascript:history.back();'); 

                $this->CsdjDB->get_update('user',$this->session->userdata('cs_id'),$edit);
                $this->CsdjSkins->Msg_url('恭喜您，修改成功!',@$_SERVER['HTTP_REFERER']);  
        }
}