<?php
/*
Plugin Name: ckplayer视频播放器
Version: 1.1
Plugin URL: http://yylog.cn/
Description: 文章插入ckplayer视频播放器
Author: 五哥
Author Email: 497399374@qq.com
Author URL: http://yylog.cn/
*/
!defined('EMLOG_ROOT') && exit('access deined!');

$DB = MySql::getInstance();

addAction('adm_writelog_head', 'video_input');
function video_input(){
    global $logid;
    global $DB;
    if(isset($logid)){
        $res = $DB->query("select * from ".DB_PREFIX."yylog_ckplayer where `gid` = $logid");
        if($res->num_rows > 0){
            $j = $res->fetch_array();
        }else{
            $j = array("videoUrl"=>"", "videoWidth"=>"600px", "videoHeight"=>"400px", "autoplay"=>"true", "live"=>"false", "videoType"=>"mp4");
        }
    }else{
            $j = array("videoUrl"=>"", "videoWidth"=>"600px", "videoHeight"=>"400px", "autoplay"=>"true", "live"=>"false", "videoType"=>"mp4");
    }
?>
            <script>
                $('div.small.my-3').append('<a style="margin-left:10px;" href="#setVideo"><i class="fa fa-video-camera"></i> 插入视频</a>')
            </script>

            <div id="setVideo">

		<div class="card mb-3 mt-3 card-sm">
			<div class="card-header" style="background-color: #1cc88a;color: white;">ckplayer视频播放器</div>
			<div class="card-body">
                <div class="form-group"><label style="color:#16baaa">视频地址：<small class="text-muted">（如果为空则不显示播放器，视频格式为m3u8，flv，ts的视频需要跨域权限）</small></label><input name="videoUrl" id="videoUrl" class="form-control" value="<?=$j['videoUrl']?>" /></div>
				<table class="table table-bordered table-striped table-hover dataTable no-footer">
					<colgroup>
						<col width="100">
						<col width="100">
						<col width="100">
						<col width="100">
						<col>
					</colgroup>
					<thead>
						<tr>
							<th>宽度</th>
							<th>高度</th>
							<th>视频类型</th>
							<th>是否直播</th>
							<th>自动播放</th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td><input name="videoWidth" id="videoWidth" class="form-control" value="<?=$j['videoWidth']?:'600px'?>"></td>
							<td><input name="videoHeight" id="videoHeight" class="form-control" value="<?=$j['videoHeight']?:'400px'?>"></td>
							<td><select name="videoType" id="videoType" class="form-control">'mp4','m3u8','flv','ts','mpd'
                                    <option value="mp4" <?=($j['videoType']=='mp4'?"selected":"")?> >mp4</option>
                                    <option value="m3u8" <?=($j['videoType']=='m3u8'?"selected":"")?> >m3u8</option>
                                    <option value="flv" <?=($j['videoType']=='flv'?"selected":"")?> >flv</option>
                                    <option value="ts" <?=($j['videoType']=='ts'?"selected":"")?> >ts</option>
                            </select></td>
							<td><select name="live" id="live" class="form-control">
                                    <option value="true" <?=($j['live']=='true'?"selected":"")?> >是</option>
                                    <option value="false" <?=($j['live']=='false'?"selected":"")?> >否</option>
                            </select></td>
							<td><select name="autoplay" id="autoplay" class="form-control">
                                    <option value="true" <?=($j['autoplay']=='true'?"selected":"")?> >自动播放</option>
                                    <option value="false" <?=($j['autoplay']=='false'?"selected":"")?> >不自动播放</option>
                            </select></td>
						</tr>
					</tbody>
				</table>

            </div>
        </div>
        
            </div>
<?php
}


addAction('save_log', 'set_log_video');
function set_log_video($blogid){
    global $DB;
    
    $videoUrl = Input::postStrVar('videoUrl', '');
    $videoWidth = Input::postStrVar('videoWidth', '600px');
    $videoHeight = Input::postStrVar('videoHeight', 'auto');
    $autoplay = Input::postStrVar('autoplay', 'true');
    $live = Input::postStrVar('live', 'false');
    $videoType = Input::postStrVar('videoType', 'mp4');
    
    $res = $DB->query("select * from ".DB_PREFIX."yylog_ckplayer where `gid` = $blogid");
    $num = $res->num_rows;
    if($num>0){
        $ars = $res->fetch_array();
        $sql = "update ".DB_PREFIX."yylog_ckplayer set `videoHeight` = '{$videoHeight}', `videoWidth` = '{$videoWidth}', `videoUrl` = '{$videoUrl}' , `autoplay` = '{$autoplay}', `live` = '{$live}', `videoType` = '{$videoType}' where id = ".$ars['id'];
    }else{
        $sql = "insert into " . DB_PREFIX . "yylog_ckplayer (`gid`,`videoHeight`, `videoWidth`, `videoUrl`, `autoplay`, `live`, `videoType`) values ('$blogid','$videoHeight','$videoWidth','$videoUrl','$autoplay','$live','$videoType')";
    }
    $DB->query($sql);
}


addAction('log_related', 'insertBlog');
function insertBlog($logData){
    global $DB;
    $res = $DB->query("select * from ".DB_PREFIX."yylog_ckplayer where `gid` = ".$logData['logid']);
    $num = $res->num_rows;
    if($num>0){
        $ars = $res->fetch_array();
        if($ars['videoUrl'] != ''){
            $width = $ars['videoWidth']?: "600px";
            $height = $ars['videoHeight']?: "400px";
            $autoplay = $ars['autoplay']?: "true";
?>
        <meta name="referrer" content="no-referrer">
		<?php if($ars['videoType'] == 'm3u8'){?>
            <script type="text/javascript" src="<?=BLOG_URL?>content/plugins/yylog_ckplayer/ckplayer/hls.js/hls.min.js"></script>
		<?php }elseif($ars['videoType'] == 'flv'){?>
            <script type="text/javascript" src="<?=BLOG_URL?>content/plugins/yylog_ckplayer/ckplayer/flv.js/flv.min.js"></script>
		<?php }elseif($ars['videoType'] == 'ts'){?>
            <script type="text/javascript" src="<?=BLOG_URL?>content/plugins/yylog_ckplayer/ckplayer/mpegts.js/mpegts.js"></script>
		<?php }?>
        
        <link type="text/css" rel="stylesheet" href="<?=BLOG_URL?>content/plugins/yylog_ckplayer/ckplayer/css/ckplayer.red.css" />
        <script type="text/javascript" src="<?=BLOG_URL?>content/plugins/yylog_ckplayer/ckplayer/js/ckplayer.min.js" charset="UTF-8"></script>
        <div class="yylog_ckplayer" style="width: <?=$width?>; height: <?=$height?>;max-width: 100%;min-height:100px;margin:15px auto"></div>
        <script>
			var videoObject = {
				container: '.yylog_ckplayer',
				video:'<?=$ars['videoUrl']?>',
				autoplay: <?=$autoplay?>,
				menu:[{
                    title:'emlog官网',
                    link:'https://www.emlog.net/?ic=6vNridyx'			
                },
                {
                    title:'ckplayer插件',
                    link:'https://www.emlog.net/plugin/detail/647'			
                }],
				playbackrateOpen:false,
				title:'<?=$logData['log_title']?>',
				<?php if($ars['live']=='true'){?>
				    live:true,
				<?php }?>
				
				<?php if($ars['videoType'] == 'm3u8'){?>
				    plug:'hls.js',
				<?php }elseif($ars['videoType'] == 'flv'){?>
				    plug:'flv.js',
				<?php }elseif($ars['videoType'] == 'ts'){?>
				    plug:'mpegts.js',
				<?php }?>
				
				
			};
			var player=new ckplayer(videoObject);
		</script>

<?php
        }
    }

}

