<?php
// +----------------------------------------------------------------------
// | 天方夜谭 [ 世界上最好用的QQ秒赞系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//页面标题
define('Title', '播放器管理');

//创建播放器
if (POST('action')=='player'){
    header('Content-Type:application/json; charset=utf-8');
    $name = POST('name');
    $auto_play = POST('auto_play');
    $play_mode = POST('play_mode');
    $is_clone = POST('is_clone');
    $style = POST('style');
    if (get_count('player',"only='{$GLOBALS['users']['only']}'",'id')>=$GLOBALS['users']['p_quota']){
        $arr = array('state'=>'no','msg'=>"对不起，您最大只能创建{$GLOBALS['users']['p_quota']}个播放器！");
    }else if ($name=='' || $auto_play=='' || $play_mode=='' || $is_clone=='' || $style==''){
        $arr = array('state'=>'no','msg'=>'所有项不能为空！');
    }else{
        $insert = '`font_color`,`external_box_shadow`,`background`,`transparent`,`switch_box_shadow`,`switch_background`,`hover_btn_coler`,`cover_frame_color`,`progress_color`,`progress_back_color`,`progress_btn_color`,`roll_color`,`scroll_bar_color`,`lyric_container_color`,`lyric_art_font_file`,`lyric_text_shadow`,`lyric_font_color`,`lyric_hover_color`';
        $value = "'".TFYT('player_font_color')."','".TFYT('player_external_box_shadow')."','".TFYT('player_background')."','".TFYT('player_transparent')."','".TFYT('player_switch_box_shadow')."','".TFYT('player_switch_background')."','".TFYT('player_hover_btn_coler')."','".TFYT('player_cover_frame_color')."','".TFYT('player_progress_color')."','".TFYT('player_progress_back_color')."','".TFYT('player_progress_btn_color')."','".TFYT('player_roll_color')."','".TFYT('player_scroll_bar_color')."','".TFYT('player_lyric_container_color')."','".TFYT('player_lyric_art_font_file')."','".TFYT('player_lyric_text_shadow')."','".TFYT('player_lyric_font_color')."','".TFYT('player_lyric_hover_color')."'";
        if ($TFYT_DB->query("insert into ".PREFIX."player (`only`,`name`,`auto_play`,`play_mode`,`song_quota`,`is_clone`,`style`,`addtime`,{$insert})values('{$GLOBALS['users']['only']}','{$name}','{$auto_play}','{$play_mode}','".TFYT('reg_s_quota')."','{$is_clone}','{$style}',NOW(),{$value})")){
            $arr = array('state'=>'ok','msg'=>'创建成功！系统送您'.TFYT('reg_s_quota').'个歌单配额');
        }else{
            $arr = array('state'=>'no','msg'=>'创建失败！请稍后再试');
        }
    }
    exit(json_encode($arr));
}elseif (GET('id')!=''){
    $id = GET('id');
    if (!$player_info = $TFYT_DB->get_row("select * from ".PREFIX."player where id='{$id}' and `only`='{$GLOBALS['users']['only']}'")){
        echo "<script language='javascript'>alert('系统找不到要编辑的播放器');window.location.href='/Application/Mains.php?Mould=Player';</script>";
    }
}elseif (POST('action')=='playerlist'){
    exit(json_encode(object_array($TFYT_DB->get_results("select * from ".PREFIX."player where `only`='{$GLOBALS['users']['only']}' order by id asc"))));
}elseif (POST('action')=='addsong'){
    header('Content-Type:application/json; charset=utf-8');
    $name = POST('name');
    $img = POST('img');
    if (empty($name) || empty($img)){
        $arr = array('state'=>'no','msg'=>'所有项不能为空！');
    }else{
        if ($TFYT_DB->query("insert into ".PREFIX."player_song (`name`,`img`,`addtime`,`only`)values('{$name}','{$img}',NOW(),'{$GLOBALS['users']['only']}')")){
            $arr = array('state'=>'ok','msg'=>'创建成功！系统送您'.$GLOBALS['users']['u_quota'].'个歌曲配额');
        }else{
            $arr = array('state'=>'no','msg'=>'创建失败！请稍后再试');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='songlist'){
    exit(json_encode(object_array($TFYT_DB->get_results("select * from ".PREFIX."player_song where `only`='{$GLOBALS['users']['only']}' order by id asc"))));
}elseif (POST('action')=='musiclist'){
    exit(json_encode(object_array($TFYT_DB->get_results("select music_id,music_name,music_artist from ".PREFIX."player_music where `only`='{$GLOBALS['users']['only']}' and song_id is null order by music_id asc"))));
}elseif (POST('action')=='addmusicgosong'){
    $song_id = POST('song_id');
    $music_id = POST('music_id');
    if (empty($song_id) || empty($music_id)){
        $arr = array('state'=>'no','msg'=>'请选择歌单和歌曲！');
    }elseif (get_count('player_music',"only='{$GLOBALS['users']['only']}'",'music_id')>$GLOBALS['users']['u_quota']){
        $arr = array('state'=>'no','msg'=>"对不起，该歌单最多只能储存".$GLOBALS['users']['u_quota']."首歌！");
    }else{
        if ($TFYT_DB->query("update ".PREFIX."player_music set song_id='{$song_id}' where music_id='{$music_id}' and only='{$GLOBALS['users']['only']}'")){
            $arr = array('state'=>'ok','msg'=>'移动到目标歌单成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'移动到目标歌单失败，系统错误，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='delmusicgosong'){
    $music_id = POST('music_id');
    if (empty($music_id)){
        $arr = array('state'=>'no','msg'=>'系统找不到要删除的歌曲！');
    }else{
        if ($TFYT_DB->query("delete from ".PREFIX."player_music where music_id='{$music_id}' and only='{$GLOBALS['users']['only']}'")){
            $arr = array('state'=>'ok','msg'=>'删除歌曲成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'删除歌曲失败，系统错误，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='getsonggomusic'){
    exit(json_encode(object_array($TFYT_DB->get_results("select * from ".PREFIX."player_music where song_id='".POST('song_id')."' and `only`='{$GLOBALS['users']['only']}' order by song_id asc"))));
}elseif (POST('action')=='getsonginfo'){
    $id = POST('id');
    if ($info = $TFYT_DB->get_row("select * from ".PREFIX."player_song where id='{$id}' and only='{$GLOBALS['users']['only']}'")){
        $arr = array('state'=>'ok','msg'=>'获取歌曲信息成功！','num'=>$info->quota,'num2'=>get_count('player_music',"song_id='{$id}'",'music_id'));
    }else{
        $arr = array('state'=>'no','msg'=>'删除歌单信息失败，请稍后再试！');
    }
    exit(json_encode($arr));
}elseif (POST('action')=='getsonginfo2'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'请选择要编辑的歌单！');
    }else{
        if ($info = $TFYT_DB->get_row("select * from ".PREFIX."player_song where id='{$id}' and only='{$GLOBALS['users']['only']}'")){
            $arr = array('state'=>'ok','msg'=>'获取歌单信息成功！','name'=>$info->name,'img'=>$info->img,'player_id'=>$info->player_id,'num'=>$info->quota,'num2'=>get_count('player_music',"song_id='{$id}'",'music_id'),'player'=>get_data('player','name',"id='{$info->player_id}'"));
        }else{
            $arr = array('state'=>'no','msg'=>'编辑歌单信息失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='editsong'){
    $id = POST('id');
    $name = POST('name');
    $img = POST('img');
    $player = POST('player');
   if (empty($id) || empty($name) || empty($img) || empty($player)){
       $arr = array('state'=>'no','msg'=>'所有项不能为空！');
   }else{
        $set = "name='{$name}',img='{$img}',player_id='{$player}'";
        if ($TFYT_DB->query("update ".PREFIX."player_song set {$set} where id='{$id}' and only='{$GLOBALS['users']['only']}'")){
            $arr = array('state'=>'ok','msg'=>'保存成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'保存失败，请稍后再试！');
        }
   }
   exit(json_encode($arr));
}elseif (POST('action')=='delsong'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'请选择你要删除的歌单！');
    }else{
        if ($TFYT_DB->query("delete from ".PREFIX."player_song where id='{$id}' and `only`='{$GLOBALS['users']['only']}'")){
            $TFYT_DB->query("update ".PREFIX."player_music set song_id=null where song_id='{$id}' and only='{$GLOBALS['users']['only']}'");
            $arr = array('state'=>'ok','msg'=>'删除成功，歌单的音乐已经恢复到默认的音乐列表！');
        }else{
            $arr = array('state'=>'no','msg'=>'删除失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='delplayer'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'系统找不到要删除的播放器！');
    }else{
        if ($TFYT_DB->query("delete from ".PREFIX."player where id='{$id}' and `only`='{$GLOBALS['users']['only']}'")){
            $TFYT_DB->query("update ".PREFIX."player_song set player_id=null where player_id='{$id}' and only='{$GLOBALS['users']['only']}'");
            $arr = array('state'=>'ok','msg'=>'删除成功，播放器里的歌单已恢复到默认歌单列表！');
        }else{
            $arr = array('state'=>'no','msg'=>'删除失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='generate'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'系统找不到要生成的播放器！');
    }else{
        if ($info=$TFYT_DB->get_row("select * from ".PREFIX."player where id='{$id}' and `only`='{$GLOBALS['users']['only']}'")){
            @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/",0777);
            @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}",0777);
            @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css",0777);
            @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/js",0777);
            $css = @file_get_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/player/css/player.style");
            $js = @file_get_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/player/js/player.script");
            $a = str_replace('{font_color}', $info->font_color,$css);
            $b = str_replace('{external_box_shadow}', $info->external_box_shadow,$a);
            $c = str_replace('{background}', $info->background,$b);
            $d = str_replace('{transparent}', $info->transparent,$c);
            $e = str_replace('{switch_box_shadow}', $info->switch_box_shadow,$d);
            $f = str_replace('{switch_background}', $info->switch_background,$e);
            $g = str_replace('{hover_btn_coler}', $info->hover_btn_coler,$f);
            $h = str_replace('{cover_frame_color}', $info->cover_frame_color,$g);
            $i = str_replace('{progress_color}', $info->progress_color,$h);
            $j = str_replace('{progress_back_color}', $info->progress_back_color,$i);
            $k = str_replace('{progress_btn_color}', $info->progress_btn_color,$j);
            $l = str_replace('{roll_color}', $info->roll_color,$k);
            $m = str_replace('{scroll_bar_color}', $info->scroll_bar_color,$l);
            $n = str_replace('{lyric_container_color}', $info->lyric_container_color,$m);
            $o = str_replace('{lyric_art_font_file}', $info->lyric_art_font_file,$n);
            $p = str_replace('{lyric_text_shadow}', $info->lyric_text_shadow,$o);
            $q = str_replace('{lyric_font_color}', $info->lyric_font_color,$js);
            $r = str_replace('{lyric_hover_color}', $info->lyric_hover_color,$q);
            $s = str_replace('{api.music}', TFYT('music_api'),$r);
            $t = str_replace('{player.css}', $_SERVER['HTTP_HOST']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css/player.css",$s);
            if (
                @file_put_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css/player.css",$p)
                &&
                @file_put_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/js/player.js",$t)
            ){
                $arr = array('state'=>'ok','msg'=>'生成成功，将代码放入您的网站中即可！','code'=>'<SCRIPT SRC="//'.$_SERVER['HTTP_HOST'].'/ThinkPHP/Cache/'.$GLOBALS['users']['only'].'/player'.$id.'/js/player.js" ID="TFYT" TFYT_KEY="'.$GLOBALS['users']['only'].'" TFYT_ID="'.$id.'" TFYT_URL="//'.$_SERVER['HTTP_HOST'].'/Monitor/"></SCRIPT>');
            }else{
                $arr = array('state'=>'no','msg'=>'生成失败，写入文件失败，请检查网站是否有写入权限，请联系网站管理员修复！');
            }
        }else{
            $arr = array('state'=>'no','msg'=>'生成播放器失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('id')!=''){
    $id = POST('id');
    $name = POST('name');
    $auto_play = POST('auto_play');
    $play_mode = POST('play_mode');
    $is_clone = POST('is_clone');
    $style = POST('style');
    $font_color = POST('font_color');
    $external_box_shadow = POST('external_box_shadow');
    $background = POST('background');
    $transparent = POST('transparent');
    $switch_box_shadow = POST('switch_box_shadow');
    $switch_background = POST('switch_background');
    $hover_btn_coler = POST('hover_btn_coler');
    $cover_frame_color = POST('cover_frame_color');
    $progress_color = POST('progress_color');
    $progress_back_color = POST('progress_back_color');
    $progress_btn_color = POST('progress_btn_color');
    $roll_color = POST('roll_color');
    $scroll_bar_color = POST('scroll_bar_color');
    $lyric_container_color = POST('lyric_container_color');
    $lyric_art_font_file = POST('lyric_art_font_file');
    $lyric_text_shadow = POST('lyric_text_shadow');
    $lyric_font_color = POST('lyric_font_color');
    $lyric_hover_color = POST('lyric_hover_color');
    if (empty($id) || empty($name) || empty($style)){
        exit('<script language=\'javascript\'>alert(\'所有项不能为空！\');history.go(-1);</script>');
    }else{
        if (get_isvip($GLOBALS['users']['vip'],$GLOBALS['users']['vipend'])){
            $set = "name='{$name}',auto_play='{$auto_play}',play_mode='{$play_mode}',is_clone='{$is_clone}',style='{$style}',font_color='{$font_color}',external_box_shadow='{$external_box_shadow}',background='{$background}',transparent='{$transparent}',switch_box_shadow='{$switch_box_shadow}',switch_background='{$switch_background}',hover_btn_coler='{$hover_btn_coler}',cover_frame_color='{$cover_frame_color}',progress_color='{$progress_color}',progress_back_color='{$progress_back_color}',progress_btn_color='{$progress_btn_color}',roll_color='{$roll_color}',scroll_bar_color='{$scroll_bar_color}',lyric_container_color='{$lyric_container_color}',lyric_art_font_file='{$lyric_art_font_file}',lyric_text_shadow='{$lyric_text_shadow}',lyric_font_color='{$lyric_font_color}',lyric_hover_color='{$lyric_hover_color}'";
        }else{
            $set = "name='{$name}',auto_play='{$auto_play}',play_mode='{$play_mode}',is_clone='{$is_clone}',style='{$style}'";
        }
        @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/",0777);
        @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}",0777);
        @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css",0777);
        @mkdir($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/js",0777);
        $css = @file_get_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/player/css/player.style");
        $js = @file_get_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/player/js/player.script");
        $a = str_replace('{font_color}', $font_color,$css);
        $b = str_replace('{external_box_shadow}', $external_box_shadow,$a);
        $c = str_replace('{background}', $background,$b);
        $d = str_replace('{transparent}', $transparent,$c);
        $e = str_replace('{switch_box_shadow}', $switch_box_shadow,$d);
        $f = str_replace('{switch_background}', $switch_background,$e);
        $g = str_replace('{hover_btn_coler}', $hover_btn_coler,$f);
        $h = str_replace('{cover_frame_color}', $cover_frame_color,$g);
        $i = str_replace('{progress_color}', $progress_color,$h);
        $j = str_replace('{progress_back_color}', $progress_back_color,$i);
        $k = str_replace('{progress_btn_color}', $progress_btn_color,$j);
        $l = str_replace('{roll_color}', $roll_color,$k);
        $m = str_replace('{scroll_bar_color}', $scroll_bar_color,$l);
        $n = str_replace('{lyric_container_color}', $lyric_container_color,$m);
        $o = str_replace('{lyric_art_font_file}', $lyric_art_font_file,$n);
        $p = str_replace('{lyric_text_shadow}', $lyric_text_shadow,$o);
        $q = str_replace('{lyric_font_color}', $lyric_font_color,$js);
        $r = str_replace('{lyric_hover_color}', $lyric_hover_color,$q);
        $s = str_replace('{api.music}', TFYT('music_api'),$r);
        $t = str_replace('{player.css}', $_SERVER['HTTP_HOST']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css/player.css",$s);
        if (
            @file_put_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/css/player.css",$p)
            &&
            @file_put_contents($_SERVER['DOCUMENT_ROOT']."/ThinkPHP/Cache/{$GLOBALS['users']['only']}/player{$id}/js/player.js",$t)
        ){
            $TFYT_DB->query("update ".PREFIX."player set {$set} where id='{$id}' and only='{$GLOBALS['users']['only']}'");
            exit("<script language='javascript'>alert('修改成功！');window.location.href='?Mould=Player';</script>");
        }else{
            exit("<script language='javascript'>alert('修改成功，但写入文件失败，请检查网站是否有写入权限2！');window.location.href='?Mould=Player';</script>");
        }
    }
}

//获取播放器样式
$player_style = $TFYT_DB->get_results("select * from ".PREFIX."website_player order by id asc");

//加载头部
Load_Template(Mains.'head.php');

//加载视图
Load_Template(Mains.'player.php');

//加载底部
Load_Template(Mains.'foot.php');