<?php
$login = query_string('1');
$aspectratio = query_string('4');
$capa = code_decode(query_string('5'),"D");

$verifica_stm = mysqli_num_rows(mysqli_query($conexao,"SELECT * FROM streamings where login = '".$login."'"));

if($verifica_stm == 0) {
die ("Error! Missing data.");
}

$dados_config = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM configuracoes"));
$dados_stm = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM streamings where login = '".$login."'"));
$dados_servidor = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM servidores where codigo = '".$dados_stm["codigo_servidor"]."'"));

if($dados_servidor["nome_principal"]) {
$servidor = $dados_servidor["nome_principal"].".".$dados_config["dominio_padrao"];
} else {
$servidor = $dados_servidor["nome"].".".$dados_config["dominio_padrao"];
}

$url_source = "https://".$servidor."/".$login."/".$login."/playlist.m3u8";

$dados_anuncio = mysqli_fetch_array(mysqli_query($conexao,"SELECT * FROM anuncios_videos WHERE codigo_stm = '".$dados_stm["codigo"]."' ORDER BY RAND() LIMIT 1"));

$anuncio_tempo = $dados_anuncio["tempo"];
$anuncio_video = "https://".$servidor.":1443/play.php?login=".$login."&video=".$dados_anuncio["video"]."";

mysqli_query($conexao,"UPDATE anuncios_videos SET exibicoes = exibicoes+1 WHERE codigo = '".$dados_anuncio["codigo"]."'");

if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == "pt") {
$lang_aguarde = "Aguarde";
$lang_botao = "Pular >";
$lang_segundos = "segundos";
} elseif(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == "en") {
$lang_aguarde = "Wait";
$lang_botao = "Skip >";
$lang_segundos = "seconds";
} elseif(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == "es") {
$lang_aguarde = "Espere";
$lang_botao = "Saltar >";
$lang_segundos = "segundos";
} else {
$lang_aguarde = "Aguarde";
$lang_botao = "Pular >";
$lang_segundos = "segundos";
}
?>
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script type="text/javascript" charset="utf-8" src="//cdn.jsdelivr.net/npm/clappr@latest/dist/clappr.min.js"></script>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <title>Player</title>
<style>*{margin:0;}html,body{height:100%;}.seek-disabled{display:none !important;}</style>
</head>
<body>
<div  class="container-fluid">
    <div class="row">
    	<?php if($aspectratio == "16:9") { ?>
        <div class="embed-responsive embed-responsive-16by9">
        <?php } ?>
    	<?php if($aspectratio == "4:3") { ?>
        <div class="embed-responsive embed-responsive-4by3">
        <?php } ?>
        <div id="player_webtv" class="embed-responsive-item"></div>
        </div>
</div>
</div>
    <script type="text/javascript" charset="utf-8">
  window.onload = function() {
  var player = new Clappr.Player({
    source: '<?php echo $url_source; ?>',
    parentId: '#player_webtv',
	width: '100%',
    height: '100%',
    mute: <?php echo query_string('3'); ?>,
    hideMediaControl: true,
    poster: '<?php echo $capa; ?>',
	autoPlay: <?php echo query_string('2'); ?>,
	plugins: [ClapprAds],
	ads: {
		midRoll: {
            at: <?php echo $anuncio_tempo; ?>,
            src: '<?php echo $anuncio_video; ?>',
            skip: true,
            timeout: 5
        }
	}
  });
}
!function(t){var i=function(t,i,o){this.text={wait:"<?php echo $lang_aguarde; ?> % <?php echo $lang_segundos; ?>...",skip:"<?php echo $lang_botao; ?>"},this.onEnd=!1,this.wrapper=this._initWrapper(),this.video=this._initVideo(t),this.wrapper.appendChild(this.video),this.muteButton=this._initMuteButton(),i&&(this.skipButton=this._initSkipButton(o),this.wrapper.appendChild(this.skipButton))};i.prototype._initWrapper=function(){var t=document.createElement("div");return t.style.display="block",t.style.position="absolute",t.style.width="100%",t.style.height="100%",t.style.top="0px",t.style.left="0px",t.style.zIndex=1e4,t},i.prototype._initVideo=function(t){var i=document.createElement("video");return i.style.display="block",i.style.position="absolute",i.style.width="100%",i.style.height="100%",i.controls=!1,i.src=t,i.addEventListener("ended",this._end.bind(this)),i},i.prototype._initSkipButton=function(t){var i=document.createElement("button");return i.style.display="none",i.style.position="absolute",i.style.bottom="45px",i.style.right="0px",i.style.padding="15px",i.style.backgroundColor="#000",i.style.border="solid thin #000",i.style.fontSize="12px",i.style.color="#FFF",i.style.right="-1px",i.disabled=!0,i.addEventListener("click",this._end.bind(this)),this._skipButtonCountdown(i,t),i},i.prototype._initMuteButton=function(){var t=document.createElement("div");return t.style.position="absolute",t.style.bottom="145px",t.style.right="100px",t.style.padding="15px",t.style.backgroundColor="#000",t.style.border="solid thin #000",t.style.fontSize="12px",t.style.color="#FFF",t.innerText="Volume",t.addEventListener("click",function(){this.video.muted=!this.video.muted}.bind(this)),t},i.prototype._skipButtonCountdown=function(t,i){var o=setInterval(function(){t.style.display="block",i>0?(t.innerHTML=this.text.wait.replace("%",i),i--):(t.innerHTML=this.text.skip,t.disabled=!1,clearInterval(o))}.bind(this),1e3)},i.prototype._end=function(t){t&&t.preventDefault(),this.wrapper.parentNode.removeChild(this.wrapper),"function"==typeof this.onEnd&&this.onEnd()},i.prototype.play=function(){this.video.play()},i.prototype.pause=function(){this.video.pause()},i.prototype.attachMuteButton=function(){this.wrapper.appendChild(this.muteButton)};var o=Clappr.UICorePlugin.extend({_isAdPlaying:!1,_hasPreRollPlayed:!1,_hasPostRollPlayed:!1,_preRoll:!1,_midRoll:!1,_postRoll:!1,_videoText:{},_rand:function(t,i){return Math.floor(Math.random()*(i-t+1))+t},name:"clappr_ads",initialize:function(){if("ads"in this._options){if("preRoll"in this._options.ads){if(!("src"in this._options.ads.preRoll))throw"No source";this._preRoll=this._options.ads.preRoll}if("midRoll"in this._options.ads){if(!("src"in this._options.ads.midRoll))throw"No source";this._midRoll=this._options.ads.midRoll,"string"==typeof this._midRoll.src&&(this._midRoll.src=[this._midRoll.src]),"at"in this._midRoll&&"object"!=typeof this._midRoll.at&&(this._midRoll.at=[this._midRoll.at])}if("postRoll"in this._options.ads){if(!("src"in this._options.ads.postRoll))throw"No source";this._postRoll=this._options.ads.postRoll}if("text"in this._options.ads){var t=this._options.ads.text;"wait"in t&&(this._videoText.wait=t.wait),"skip"in t&&(this._videoText.skip=t.skip)}}this.bindEvents()},bindEvents:function(){this.listenTo(this.core,Clappr.Events.CORE_READY,function(){var t=this.core.getCurrentContainer();t.listenTo(t.playback,Clappr.Events.PLAYBACK_PLAY,this._onPlaybackPlay.bind(this,t)),t.listenTo(t.playback,Clappr.Events.PLAYBACK_TIMEUPDATE,this._onPlaybackTimeUpdate.bind(this,t)),t.listenTo(t.playback,Clappr.Events.PLAYBACK_ENDED,this._onPlaybackEnd.bind(this))}.bind(this))},_onPlaybackPlay:function(t){if(this._isAdPlaying)t.playback.pause();else{if(!this._preRoll||this._hasPreRollPlayed)return;this.playPreRoll(t)}},_onPlaybackTimeUpdate:function(t){var i=t.currentTime,o=t.getDuration();if(this._midRoll){var s;s="at"in this._midRoll?this._midRoll.at:[Math.floor(o/2)];for(var l,e,n=!1,a=0;a<s.length;a++)l=s[a],Math.floor(i)==l&&(e=a,n=!0);n&&(this._midRoll.at.length===this._midRoll.src.length?this.playMidRoll(t,e):this.playMidRoll(t))}this._postRoll&&!this._hasPostRollPlayed&&i&&Math.round(1e3*i)==Math.round(1e3*o)&&this.playPostRoll(t)},_onPlaybackEnd:function(){this._isAdPlaying=!1,this._hasPreRollPlayed=!1},playPreRoll:function(t){var o;this._isAdPlaying||(o="object"==typeof this._preRoll.src?this._preRoll.src[this._rand(0,this._preRoll.src.length-1)]:this._preRoll.src,t.playback.pause(),video=new i(o,this._preRoll.skip,this._preRoll.timeout),video.onEnd=this._onPreRollEnd.bind(this,video,t.playback),"wait"in this._videoText&&(video.text.wait=this._videoText.wait),"skip"in this._videoText&&(video.text.skip=this._videoText.skip),this._preRoll.muteButton&&video.attachMuteButton(),t.$el.append(video.wrapper),video.play(),"onPlay"in this._preRoll&&this._preRoll.onPlay(this._preRoll,{position:"preroll"}),this._hasPreRollPlayed=!0)},playMidRoll:function(t,o){var s;this._isAdPlaying||(this._isAdPlaying=!0,t.playback.pause(),t.playback.seek(parseInt(Math.floor(t.currentTime+1))),s=void 0===o?this._midRoll.src[this._rand(0,this._midRoll.src.length-1)]:this._midRoll.src[o],video=new i(s,this._midRoll.skip,this._midRoll.timeout),this._midRoll.muteButton&&video.attachMuteButton(),t.$el.append(video.wrapper),video.play(),"onPlay"in this._midRoll&&this._midRoll.onPlay(this._midRoll,{position:"preroll"}),video.onEnd=function(){this._isAdPlaying=!1,t.playback.play()}.bind(this))},playPostRoll:function(t){var o;this._isAdPlaying||(this._isAdPlaying=!0,this._hasPostRollPlayed=!0,t.playback.pause(),o="object"==typeof this._postRoll.src?this._postRoll.src[this._rand(0,this._postRoll.src.length-1)]:this._postRoll.src,video=new i(o),this._postRoll.muteButton&&video.attachMuteButton(),t.$el.append(video.wrapper),video.play(),"onPlay"in this._postRoll&&this._postRoll.onPlay(this._postRoll,{position:"preroll"}),video.onEnd=function(){this._isAdPlaying=!1,t.playback.play()}.bind(this))},_onPreRollEnd:function(t,i){this._isAdPlaying=!1,setTimeout(function(){i.play()},100)}});t.ClapprAds=o}(window);
  </script>
</body>
</html>