<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Song extends CI_Controller {

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

	public function index()
	{
            $ids=$this->input->get('id',TRUE);
            /*if(empty($ids)){
                $sqlstr="select CS_ID from ".CS_SqlPrefix."dance where CS_YID=0 and CS_HID=0 order by rand() desc LIMIT 30";
		        $result=$this->db->query($sqlstr);
		        $recount=$result->num_rows();
                if($recount>0){
			            foreach ($result->result() as $row) {
                            $ids.=$row->CS_ID.",";
                        }
                }
            }*/
            if(substr($ids,-1)==",") $ids=substr($ids,0,-1);
            $lbid=$ids.$this->session->userdata('clock_dance_id');
            $Arr=explode(',',$lbid);
			for($j=0;$j<count($Arr);$j++){
                  $arr[]=$Arr[$j];
			}
			$lbid=$this->array_hebing($arr);
			$data['lbid']=implode(',', $lbid);
            $this->load->view('song',$data);
    }

	public function getclock()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
            $id=intval($this->input->post('id',TRUE));
			if(strpos($this->session->userdata('clock_dance_id'),','.$id.',') === FALSE){
				 $clock_id.=",".$id;
			}
			$data['clock_dance_id']=$this->session->userdata('clock_dance_id').$clock_id;
            $this->session->set_userdata($data);
    }

	public function data()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            //if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
            $ids=$this->input->get_post('id',TRUE);
            $Arr=explode(',',$ids);
            $lists=array();
            for($j=0;$j<count($Arr);$j++){
                    $id=intval($Arr[$j]);
					if($id>0){
						   $row=$this->db->query("select CS_ID,CS_SingerID,CS_Name,CS_TID,CS_Singer,CS_FID,CS_PlayUrl,CS_Sc,CS_Content from ".CS_SqlPrefix."dance where CS_ID=".$id."")->row();
						   if($row){

							    $lrc=$this->CsdjSkins->str_checkhtml($row->CS_Content);
                                if(empty($lrc)) $lrc='0';

                                $tpic=$this->CsdjSkins->piclink('topic','');
                                $topic='-';
                                $topiclink='###';
								if($row->CS_TID>0){
						            $rowt=$this->db->query("select CS_ID,CS_Pic,CS_Name from ".CS_SqlPrefix."topic where CS_ID=".$row->CS_TID."")->row();
									if($rowt){
                                           $topiclink=$this->CsdjSkins->LinkUrl('show','zj','id',1,$row->CS_TID);
                                           $tpic=$this->CsdjSkins->piclink('topic',$rowt->CS_Pic);
									}
								}
								if(substr($row->CS_PlayUrl,0,7)=='http://'){
                                      $purl=$row->CS_PlayUrl;
								}else{
                                      $purl=$this->CsdjSkins->playqurl($row->CS_PlayUrl,$row->CS_FID);
								}

                                $lists[$j]['name']       = $row->CS_Name;
                                $lists[$j]['singer']     = $row->CS_Singer;
                                $lists[$j]['singerlink'] = $this->CsdjSkins->LinkUrl('show','singer','id',1,$row->CS_SingerID);
                                $lists[$j]['tpic']       = $tpic;
                                $lists[$j]['topic']      = $topic;
                                $lists[$j]['topiclink']  = $topiclink;
                                $lists[$j]['url']        = $purl;
                                $lists[$j]['id']         = $row->CS_ID;
                                $lists[$j]['time']       = $this->get_time($row->CS_Sc);
                                $lists[$j]['lrc']        = $lrc;
						   }
					}
			}
			echo $this->JSON($lists); 
	}

	public function rand()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
            $lists=array();
            $result=$this->db->query("select CS_ID,CS_SingerID,CS_Name,CS_TID,CS_Singer,CS_FID,CS_PlayUrl,CS_Sc,CS_Content from ".CS_SqlPrefix."dance where CS_YID=0 and CS_HID=0 order by rand() desc LIMIT 30");
			$j=0;
			foreach ($result->result() as $row) {

							    $lrc=$this->CsdjSkins->str_checkhtml($row->CS_Content);
                                if(empty($lrc)) $lrc='0';

                                $tpic=$this->CsdjSkins->piclink('topic','');
                                $topic='-';
                                $topiclink='###';
								if($row->CS_TID>0){
						            $rowt=$this->db->query("select CS_ID,CS_Pic,CS_Name from ".CS_SqlPrefix."topic where CS_ID=".$row->CS_TID."")->row();
									if($rowt){
                                           $topiclink=$this->CsdjSkins->LinkUrl('show','zj','id',1,$row->CS_TID);
                                           $tpic=$this->CsdjSkins->piclink('topic',$rowt->CS_Pic);
									}
								}
								if(substr($row->CS_PlayUrl,0,7)=='http://'){
                                      $purl=$row->CS_PlayUrl;
								}else{
                                      $purl=$this->CsdjSkins->playqurl($row->CS_PlayUrl,$row->CS_FID);
								}

                                $lists[$j]['name']       = $row->CS_Name;
                                $lists[$j]['singer']     = $row->CS_Singer;
                                $lists[$j]['singerlink'] = $this->CsdjSkins->LinkUrl('show','singer','id',1,$row->CS_SingerID);
                                $lists[$j]['tpic']       = $tpic;
                                $lists[$j]['topic']      = $topic;
                                $lists[$j]['topiclink']  = $topiclink;
                                $lists[$j]['url']        = $purl;
                                $lists[$j]['id']         = $row->CS_ID;
                                $lists[$j]['time']       = $this->get_time($row->CS_Sc);
                                $lists[$j]['lrc']        = $lrc;
				   $j++;
			}
			echo $this->JSON($lists); 
	}

	public function fav()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
			$lists=array();
		    if(!$this->session->userdata('cs_name')){
                $lists['error']='login';
				echo $this->JSON($lists); 
                exit();
			}
            $result=$this->db->query("select CS_DID from ".CS_SqlPrefix."fav where CS_User='".$this->session->userdata('cs_name')."'");
			$j=0;
			foreach ($result->result() as $row2) {
                   $row=$this->db->query("select CS_ID,CS_SingerID,CS_Name,CS_TID,CS_Singer,CS_FID,CS_PlayUrl,CS_Sc,CS_Content from ".CS_SqlPrefix."dance where CS_ID=".$row2->CS_DID."")->row();

				   if($row){

							    $lrc=$this->CsdjSkins->str_checkhtml($row->CS_Content);
                                if(empty($lrc)) $lrc='0';

                                $tpic=$this->CsdjSkins->piclink('topic','');
                                $topic='-';
                                $topiclink='###';
								if($row->CS_TID>0){
						            $rowt=$this->db->query("select CS_ID,CS_Pic,CS_Name from ".CS_SqlPrefix."topic where CS_ID=".$row->CS_TID."")->row();
									if($rowt){
                                           $topiclink=$this->CsdjSkins->LinkUrl('show','zj','id',1,$row->CS_TID);
                                           $tpic=$this->CsdjSkins->piclink('topic',$rowt->CS_Pic);
									}
								}
								if(substr($row->CS_PlayUrl,0,7)=='http://'){
                                      $purl=$row->CS_PlayUrl;
								}else{
                                      $purl=$this->CsdjSkins->playqurl($row->CS_PlayUrl,$row->CS_FID);
								}

                                $lists[$j]['name']       = $row->CS_Name;
                                $lists[$j]['singer']     = $row->CS_Singer;
                                $lists[$j]['singerlink'] = $this->CsdjSkins->LinkUrl('show','singer','id',1,$row->CS_SingerID);
                                $lists[$j]['tpic']       = $tpic;
                                $lists[$j]['topic']      = $topic;
                                $lists[$j]['topiclink']  = $topiclink;
                                $lists[$j]['url']        = $purl;
                                $lists[$j]['id']         = $row->CS_ID;
                                $lists[$j]['time']       = $this->get_time($row->CS_Sc);
                                $lists[$j]['lrc']        = $lrc;
				   }
				   $j++;
			}
			echo $this->JSON($lists); 
	}

	public function fav_add()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
		    if(!$this->session->userdata('cs_name')){
                exit('login');
			}
            $id=intval($this->input->get_post('id',TRUE));
			$fav['cs_did']=$id;
			if($id>0){
                $sql="SELECT CS_Name,CS_CID FROM ".CS_SqlPrefix."dance where cs_id='".$fav['cs_did']."' and cs_yid=0 and cs_hid=0";
	            $row=$this->CsdjDB->get_all($sql); 
	            if(!$row){
                        exit('抱歉，该歌曲不存在或者没有审核!'); 
                }else{
                        $fav['cs_name']=$row[0]->CS_Name;
                        $fav['cs_cid']=$row[0]->CS_CID;
                }
                //判断是否已经收藏
                $sql="SELECT CS_ID FROM ".CS_SqlPrefix."fav where cs_did='".$fav['cs_did']."' and cs_user='".$this->session->userdata('cs_name')."'";
	            $rowv=$this->CsdjDB->get_all($sql); 
	            if($rowv){
					    //删除收藏
                        $this->db->query("delete from ".CS_SqlPrefix."dt where cs_user='".$this->session->userdata('cs_name')."' and cs_cid=6 and cs_did='$id'");
                        $this->db->query("delete from ".CS_SqlPrefix."fav where cs_user='".$this->session->userdata('cs_name')."' and cs_id='$id'");
						exit('del');
                }

                $fav['CS_User']=$this->session->userdata('cs_name');
                $fav['CS_AddTime']=date('Y-m-d H:i:s');

                $res=$this->CsdjDB->get_insert('fav',$fav);
                if($res>0){
                       //增加收藏人气
		               $this->db->query("update ".CS_SqlPrefix."dance set CS_Shits=CS_Shits+1,CS_Dhits=CS_Dhits+1 where CS_ID='".$fav['cs_did']."'");
                       //写入动态
                       $dt['CS_CID']=6;
                       $dt['CS_DID']=$fav['cs_did'];
                       $dt['CS_YID']=0;
                       $dt['CS_User']=$this->session->userdata('cs_name');
                       $dt['CS_Title']=$fav['cs_name'];
                       $dt['CS_AddTime']=date('Y-m-d H:i:s');
                       $this->CsdjDB->get_insert('dt',$dt);

                       echo 'ok'; 

                }else{
                       exit('抱歉，歌曲收藏失败!'); 
                }
			}
	}

	public function cai()
	{
            @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
            @header("Cache-Control: no-cache, must-revalidate"); 
            @header("Pragma: no-cache");
            if(@!eregi($_SERVER['SERVER_NAME'],$_SERVER['HTTP_REFERER'])){exit('QQ:714568866');}
            $id=intval($this->input->get_post('id',TRUE));
            $lists=array();
            $row2=$this->db->query("select CS_CID from ".CS_SqlPrefix."dance where CS_ID='".$id."'")->row();
            $result2=$this->db->query("select CS_ID,CS_SingerID,CS_Name,CS_TID,CS_Singer,CS_FID,CS_PlayUrl,CS_Sc,CS_Content from ".CS_SqlPrefix."dance where CS_CID=".$row2->CS_CID." and CS_YID=0 and CS_HID=0 order by rand() desc LIMIT 10");
			$j=0;
			foreach ($result2->result() as $row) {
							    $lrc=$this->CsdjSkins->str_checkhtml($row->CS_Content);
                                if(empty($lrc)) $lrc='0';

                                $tpic=$this->CsdjSkins->piclink('topic','');
                                $topic='-';
                                $topiclink='###';
								if($row->CS_TID>0){
						            $rowt=$this->db->query("select CS_ID,CS_Pic,CS_Name from ".CS_SqlPrefix."topic where CS_ID=".$row->CS_TID."")->row();
									if($rowt){
                                           $topiclink=$this->CsdjSkins->LinkUrl('show','zj','id',1,$row->CS_TID);
                                           $tpic=$this->CsdjSkins->piclink('topic',$rowt->CS_Pic);
									}
								}
								if(substr($row->CS_PlayUrl,0,7)=='http://'){
                                      $purl=$row->CS_PlayUrl;
								}else{
                                      $purl=$this->CsdjSkins->playqurl($row->CS_PlayUrl,$row->CS_FID);
								}

                                $lists[$j]['name']       = $row->CS_Name;
                                $lists[$j]['singer']     = $row->CS_Singer;
                                $lists[$j]['singerlink'] = $this->CsdjSkins->LinkUrl('show','singer','id',1,$row->CS_SingerID);
                                $lists[$j]['tpic']       = $tpic;
                                $lists[$j]['topic']      = $topic;
                                $lists[$j]['topiclink']  = $topiclink;
                                $lists[$j]['url']        = $purl;
                                $lists[$j]['id']         = $row->CS_ID;
                                $lists[$j]['time']       = $this->get_time($row->CS_Sc);
                                $lists[$j]['lrc']        = $lrc;
				$j++;
			}

			echo $this->JSON($lists); 
	}

	public function log()
	{
		    if(!$this->CsdjUser->User_Login(1)){
                  $str['error']='no';
		    }else{
                  $str['error']='ok';
                  $str['nichen']=$this->CsdjSkins->getuser('nichen',$this->session->userdata('cs_id'),$this->session->userdata('cs_name'));
                  $str['favnums']=$this->CsdjSkins->getusercount($this->session->userdata('cs_name'),'fav');
			}
			echo $this->JSON($str); 
	}

    function get_time($sc) {
                        if(empty($sc)){
                               return '00:00';
                        }else{
                               $strss=explode(":",$sc);
                               if(count($strss)>2){
                                     if($strss[0]=='00'){
                                            $sc=$strss[1].':'.$strss[2];
                                     }else{
                                           $s=$strss[0];
                                           $f=$strss[1];
                                           if(substr($s,0,1)=='0'){
                                               $s=substr($s,1);
                                           }
                                           if(substr($f,0,1)=='0'){
                                               $f=substr($f,1);
                                           }
                                           $f=$s*60+$f;
                                           if($f<10) $f="0".$f;
                                           $sc=$f.':'.$strss[2];
                                     }
                               }elseif(count($strss)>1){
                                     $f=$strss[0];
                                     if(substr($f,0,1)=='0'){
                                         $f=substr($f,1);
                                     }
                                     if($f<10) $f="0".$f;
                                     $sc=$f.':'.$strss[1];
                               }
                        }
	        return $sc;
    }

    function array_hebing($array) {
		$tempArray = array();
		foreach($array as $val)
			$tempArray[$val] = true;
	    return array_keys($tempArray);
    }

    function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
            static $recursive_counter = 0;
            if (++$recursive_counter > 1000) {
                die('possible deep recursion attack');
            }
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
                } else {
                    $array[$key] = $function($value);
                }
 
                if ($apply_to_keys_also && is_string($key)) {
                    $new_key = $function($key);
                    if ($new_key != $key) {
                        $array[$new_key] = $array[$key];
                        unset($array[$key]);
                    }
                }
            }
            $recursive_counter--;
    }

    function JSON($array) {
	        $this->arrayRecursive($array, 'urlencode', true);
	        $json = json_encode($array);
	        return urldecode($json);
    }
}
