";
$apikey = $_COOKIE['apikey'];
if ($apikey=="") {
$apikey = $_GET['apikey'];
}
$show_loc_ID = $_GET['show_loc_ID'];
$show_show_ID = $_GET['show_show_ID'];
$show_game_date = $_GET['show_game_date'];
$qsi = $_GET['qsi'];
$m = $_GET['m'];
if ($m=="") {
$m = $_POST['m'];
}
$url = "https://triviocity.com/api.php?m=".$m."&apikey=".$apikey."&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi;
//echo $url." ";
$content = file_get_contents($url);
//echo $content." ";
$x1 = explode("*--*--*", $content);
$show_schedule = $x1[0];
$adspace = $x1[1];
$pregame = $x1[2];
$questions = $x1[3];
$freqplayed = $x1[4];
$local_files = $x1[5];
$apiuser = $x1[6];
$user_ID = $apiuser;
$apiregion = $x1[7];
$region_ID = $apiregion;
$user_info = $x1[8];
$feud = $x1[9];
$expire = (time() + (60*60*24*30));
$show_type = $_GET['show_type'];
$v = $_GET['v'];
//echo "V: ".$v." ";
//echo "M: ".$m." ";
if ($v=="load_begins") {
//echo $user_info;
//echo " ".$apiuser." ";
setcookie("user_ID", $apiuser, $expire, "/", "localhost");
//echo $region_ID." ";
//$region_ID = $_COOKIE['region_ID'];
setcookie("region_ID", $region_ID, $expire, "/", "localhost");
}
chdir('trivia');
if ($v == "load_begins") {
//get schedule
//get settings
//get file changes
$xa = explode("^*^", $show_schedule);
$xb = explode("|", $xa[1]);
#begin show schedule import
//echo "Getting Show Schedule ";
$num_shows = $xa[0];
$a = 0;
$delete = "delete from show_schedule_local";
mysqli_query($local, $delete);
$ss = $num_shows;
//echo $num_shows." ";
chdir('images');
mkdir('loc');
chdir('loc');
mkdir($region_ID);
chdir($region_ID);
//echo getcwd();
while ($a < ($num_shows + 1)) {
$show_info = $xb[$a];
//echo $a.": ".$show_info."
";
$xc = explode("^^", $show_info);
$sched_ID = $xc[0];
$question_set_id = $xc[1];
$game_date = $xc[2];
$logo_loc = $xc[3];
$corp_event = $xc[4];
$loc_ID = $xc[5];
$show_ID = $xc[6];
$loc_name = $xc[7];
$show_type = $xc[8];
//echo $loc_name." ";
if ($sched_ID != "") {
//download logo...
$remote_l = "https://www.triviocity.com/" . $logo_loc;
//echo $remote_l." ";
if ($remote_l != "https://www.triviocity.com/") {
$replace_string = "images/loc/" . $region_ID . "/";
$logo_loc_temp = str_replace($replace_string, "", $logo_loc);
copy($remote_l, $logo_loc_temp);
}
$insert = "insert into show_schedule_local (sched_ID, user_ID, region_ID, game_date, loc_ID, show_ID, loc_name, show_type, question_set_id, logo_loc, corp_event)
values ('" . $sched_ID . "', '" . $user_ID . "', '" . $region_ID . "', '" . $game_date . "', '" . $loc_ID . "', '" . $show_ID . "', '" . $loc_name . "', '" . $show_type . "', '" . $question_set_id . "', '" . $logo_loc . "', '" . $corp_event . "')";
//echo " ".$insert." ";
mysqli_query($local, $insert);
//$ss = $ss."|".$loc_name."^^".$game_date."^^".$show_type;
}
if ($loc_ID != "") {
$delete = "delete from adspace where loc_ID = " . $loc_ID . " and show_ID = " . $show_ID;
//echo $delete . " ";
mysqli_query($local, $delete);
}
//echo $game_date;
//$pd = date('l, m/d/Y', strtotime($game_date));
$a = ($a + 1);
}
chdir('../');
chdir('../');
chdir('../');
//echo getcwd();
//echo json_encode($ss);
$xa = "";
$xb = "";
$xc = "";
#end show schedule import
$xa = explode("^*^", $local_files);
$num_l = ($xa[0] + 1);
$xb = explode("|", $xa[1]);
$a = 0;
//echo $num_l." ";
while ($a < $num_l) {
//echo $a." ";
$file_info = $xb[$a];
//echo $file_info." ";
$xc = explode("^^", $file_info);
$local_file_ID = $xc[0];
$dir = $xc[1];
$subdir = $xc[2];
$filename = $xc[3];
$game_system = $xc[4];
$info = $xc[5];
//does this file exist in local_files_history
//if not, grab file
if ($local_file_ID != "") {
$query = "select local_file_history_ID from local_files_history where local_file_ID = " . $local_file_ID;
//echo $query." ";
$result = mysqli_query($local, $query);
$num = mysqli_num_rows($result);
if ($num == 0 || $num == "") {
//insert into local_file_history
//get file
if ($dir != "" && $dir != "root") {
chdir($dir);
if ($subdir != "") {
chdir($subdir);
}
}
//get the file...
if ($dir != "" && $dir != "root") {
chdir('../');
if ($subdir != "") {
chdir('../');
}
}
$remote_path = $filename;
$remote_path = str_replace("php", "txt", $filename);
$file_path = $filename;
if ($dir == "root") {
$remote_path = "https://www.triviocity.com/local_files/" . $remote_path;
} else {
if ($subdir == "") {
$remote_path = "https://www.triviocity.com/" . $dir . "/" . $remote_path;
} else {
$remote_path = "https://www.triviocity.com/" . $dir . "/" . $subdir . "/" . $remote_path;
}
}
$insert = "insert into local_files_history (local_file_ID, user_ID, date_downloaded, downloaded, uu_ID) values (" . $local_file_ID . ", " . $user_ID . ", '" . date('Y-m-d H:i:s') . "', 'Y', '" . $uu_ID . "')";
mysqli_query($local, $insert);
}
}
$a = ($a + 1);
}
$x = explode("|", $user_info);
$auto_num_teams = $x[0];
$final_score_update = $x[1];
$halftime_score_option = $x[2];
$final_score_option = $x[3];
$split_after = $x[4];
$timer_default = $x[5];
$timer_default_bonus = $x[6];
if ($final_score_update!="") {
setcookie("final_score_update", $final_score_update, $expire, "/", "localhost");
}
if ($final_score_option!="") {
setcookie("final_score_option", $final_score_option, $expire, "/", "localhost");
}
if ($halftime_score_option!="") {
setcookie("halftime_score_option", $halftime_score_option, $expire, "/", "localhost");
}
if ($auto_num_teams!="") {
setcookie("auto_num_teams", $auto_num_teams, $expire, "/", "localhost");
}
if ($split_after=="") {
$split_after = $_COOKIE['split_after'];
if ($split_after=="") {
$split_after = 12;
}
}
setcookie("split_after", $split_after, $expire, "/", "localhost");
$delete = "delete from user_settings where user_ID = " . $user_ID;
mysqli_query($local, $delete);
$insert = "insert into user_settings (auto_num_teams, final_score_update, halftime_score_option, final_score_option, split_after, timer_default, timer_default_bonus, user_ID)
values ('" . $auto_num_teams . "', '" . $final_score_update . "', '" . $halftime_score_option . "', '" . $final_score_option . "', '" . $split_after . "', '".$timer_default."', '".$timer_default_bonus."', '" . $user_ID . "')";
//echo $insert." ";
mysqli_query($local, $insert);
$url = "Location: index.php";
header($url);
}
elseif ($m=="") {
?>
Team Trivia | Local Loader
">
";
if ($show_type=="trivia") {
$xquery = "select * from questions where game_date = '".$show_game_date."' and question_set_id = ".$qsi;
//echo $xquery."
";
$xresult = mysqli_query($local, $xquery);
$xnum = mysqli_num_rows($xresult);
//echo "NUM: ".$xnum."
";
if ($xnum==0) {
?>
DOWNLOAD GAME
";
if ($xnum > 0) {
if ($show_game_date==$todayis) {
?>
GO TO GAME NAVIGATION
GO TO ARRANGER
RESET
";
$xa = explode("^*^", $questions);
$num_q = ($xa[0] + 1);
//echo $num_pre;
$xb = explode("|", $xa[1]);
$a = 0;
//echo " ";
mkdir('inserts');
chdir('inserts');
//echo "INSERTS?: " . getcwd() . " ";
while ($a < $num_q) {
$questions = $xb[$a];
//echo $questions . " ";
$xc = explode("^^", $questions);
$id = $xc[0];
$q_game_date = $xc[1];
$game_round = $xc[2];
$qno = $xc[3];
$question = $xc[4];
$question = str_replace("'", "'", $question);
$answer = $xc[5];
$answer = str_replace("'", "'", $answer);
$host_info = $xc[6];
$host_info = str_replace("'", "'", $host_info);
$display_category = $xc[7];
$display_category = str_replace("'", "'", $display_category);
$category = $xc[8];
$category = str_replace("'", "'", $category);
$question_set_id = $xc[9];
$answerofnight = $xc[10];
$answerofnight = str_replace("'", "'", $answerofnight);
$hintofweek = $xc[11];
$q_region_ID = $xc[12];
$orig_ID = $xc[13];
$sources = $xc[14];
$show_qsi = $question_set_id;
$media_answer = $xc[15];
$media_answer_url = $xc[16];
$media_question = $xc[17];
$media_question_url = $xc[18];
if ($id != "") {
//echo $a.": ".$questions." ";
//does this question already exist? if so, don't insert...
$query = "select id from questions where orig_ID = " . $id;
$result = mysqli_query($local, $query);
$num_questions = mysqli_num_rows($result);
if (!$result || ($num_questions == "" || $num_questions == 0)) {
$load_questions = "insert into questions (game_round, game_date, qno, question, answer, host_info,
display_category,
question_set_id,
media_question, media_question_url, media_answer, media_answer_url,
answerofnight, hintofweek, region_ID, orig_ID)
values
('" . $game_round . "', '" . $q_game_date . "', " . $qno . ", '" . $question . "', '" . $answer . "', '" . $host_info . "', '" . $display_category . "', '" . $show_qsi . "',
'" . $media_question . "', '" . $media_question_url . "', '" . $media_answer . "', '" . $media_answer_url . "', '" . $answerofnight . "', '" . $hintofweek . "', '" . $q_region_ID . "', '" . $id . "')";
//echo $load_questions . " ";
mysqli_query($local, $load_questions);
}
//mkdir('Y');
//echo getcwd() . " ";
}
//chdir('..');
//include get images...
$a = ($a + 1);
}
chdir('..');
//echo getcwd() . " ";
#end questions
$url = "Location: loader.php?m=freqplayed&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
header($url);
}
elseif ($m == "pregame") {
#begin preggame
//echo " ".$pregame;
//echo "Getting PreGame Slides ";
$xa = explode("^*^", $pregame);
$num_pre = ($xa[0] + 1);
//echo $num_pre;
$xb = explode("|", $xa[1]);
//echo $xb." ";
$a = 0;
chdir('images');
mkdir('pre');
chdir('pre');
//echo getcwd()." ";
$delete = "delete from pregame_screens where loc_ID = ".$show_loc_ID." and show_ID = ".$show_show_ID;
mysqli_query($local, $delete);
while ($a < $num_pre) {
//echo $a." ";
$pre_info = $xb[$a];
$xc = explode("^^", $pre_info);
//echo $pre_info." ";
$pre_title = $xc[0];
$start_date = $xc[1];
$end_date = $xc[2];
$screen_pos = $xc[3];
$screen_url = $xc[4];
//echo $screen_url." ";
$date_added = $xc[5];
$added_by = $xc[6];
$remote_ad = $xc[7];
//echo $remote_ad." ";
$local_ad = $xc[8];
//echo $local_ad;
$ps_loc_ID = $xc[9];
$ps_show_ID = $xc[10];
$pregame_active = $xc[11];
if ($pre_title != "" && $pre_title != $last_title) {
$cquery = "select * from pregame_screens where screen_url = '" . $screen_url . "' and loc_ID = " . $show_loc_ID . " and show_ID = " . $show_show_ID;
$cresult = mysqli_query($local, $cquery);
$cnum = mysqli_num_rows($cresult);
if ($cnum == 0 || $cnum == "" || !$cresult) {
//echo $cquery." ";
if (file_exists($local_ad)) {
$exists = 'Y';
//don't do anything
} else {
$exists = 'N';
//echo $pre_title." does not exist ";
if (!copy($remote_ad, $local_ad)) {
echo "COULD NOT COPY " . $remote_ad;
}
}
$insert = "insert into pregame_screens (loc_ID, show_ID, pre_title, start_date, end_date, screen_pos, screen_url, date_added, added_by)
values (" . $show_loc_ID . ", " . $show_show_ID . ", '" . $pre_title . "', '" . $start_date . "', '" . $end_date . "', '" . $screen_pos . "', '" . $screen_url . "', '" . $date_added . "', '" . $added_by . "')";
//echo $insert . " ";
mysqli_query($local, $insert);
}
}
$last_title = $pre_title;
$a = ($a + 1);
}
chdir('..');
chdir('..');
//echo getcwd();
$xa = "";
$xb = "";
$xc = "";
//are there pregame screens?
#end pregame
$url = "Location: loader.php?m=&v=DONE&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
header($url);
}
elseif ($m == "adspace") {
#begin ad space
//echo " ".$adspace;
//echo "Getting AdSpace ";
$xa = explode("^*^", $adspace);
$xb = explode("|", $xa[1]);
$num_ads = ($xa[0] + 1);
chdir('images');
mkdir('ads');
chdir('ads');
//echo getcwd()." ";
$a = 0;
$delete = "delete from adspace where loc_ID = ".$show_loc_ID." and show_ID = ".$show_show_ID;
mysqli_query($local, $delete);
while ($a < $num_ads) {
$ad_info = $xb[$a];
//echo $ad_info." ";
$xc = explode("^^", $ad_info);
$sortID = $xc[0];
$title = $xc[1];
$remote_ad = $xc[2];
//echo $remote_ad." ";
$imgurl = $xc[3];
//echo $imgurl." ";
$adtext = $xc[4];
$date_added = $xc[5];
$start_date = $xc[6];
$end_date = $xc[7];
$adType = $xc[8];
$refresh = $xc[9];
$a_loc_ID = $xc[10];
$a_show_ID = $xc[11];
if ($adType == "image") {
//$remote_ad = "https://www.triviocity.com/game/" . $imgurl;
$imgurl = str_replace("images/ads/" . $region_ID . "/", "", $imgurl);
$local_ad = $imgurl;
//echo $remote_ad." | ".$local_ad." ";
if (file_exists($local_ad)) {
$exists = 'Y';
//don't do anything
} else {
$exists = 'N';
copy($remote_ad, $local_ad);
}
$imgurl = "images/ads/" . $imgurl;
}
// if (($last_title!=$title) && $title!="") {
$cquery = "select * from adspace where imgurl = '" . $imgurl . "' and loc_ID = " . $show_loc_ID . " and show_ID = " . $show_show_ID;
$cresult = mysqli_query($local, $cquery);
$cnum = mysqli_num_rows($cresult);
if ($cnum == 0 || $cnum == "" || !$cresult) {
$insert = "insert into adspace (sortID, show_ID, loc_ID, title, imgurl, adtext, date_added, start_date, end_date, adType, refresh)
values (" . $sortID . ", " . $a_show_ID . ", " . $a_loc_ID . ", '" . $title . "', '" . $imgurl . "', '" . $adtext . "', '" . $date_added . "', '" . $start_date . "', '" . $end_date . "', '" . $adType . "', " . $refresh . ")";
mysqli_query($local, $insert);
}
//echo $insert." ";
//}
// $last_title = $title;
$a = ($a + 1);
}
chdir('..');
chdir('..');
$xa = "";
$xb = "";
$xc = "";
//$last_title = "";
#end ad space
if ($show_type=="trivia") {
$url = "Location: loader.php?m=load_images&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
}
else {
$url = "Location: loader.php?m=feud&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
}
header($url);
}
elseif ($m=="load_images") {
chdir('inserts');
$query = "select media_answer, media_answer_url, media_question, media_question_url, question_set_id
from questions where game_date = '".$show_game_date."' and question_set_id = ".$qsi;
//echo $query." ";
$result = mysqli_query($local, $query);
while ($row = mysqli_fetch_assoc($result)) {
$media_answer = $row['media_answer'];
$media_answer_url = $row['media_answer_url'];
$media_question = $row['media_question'];
$media_question_url = $row['media_question_url'];
$x = explode("-", $show_game_date);
$year = $x[0];
$month = $x[1];
$day = $x[2];
$question_set_id = $row['question_set_id'];
if ($media_answer == 1) {
mkdir($year, 0777);
chdir($year);
mkdir($month, 0777);
chdir($month);
mkdir($show_game_date, 0777);
chdir($show_game_date);
mkdir($question_set_id, 0777);
chdir($question_set_id);
//echo getcwd()." ";
//echo "WITH QUESTION SET ID: " . getcwd();
$webimage = 'https://www.triviocity.com/game/inserts/' . $year . '/' . $month . '/' . $show_game_date . '/' . $question_set_id . '/' . $media_answer_url;
//echo $webimage." ";
if (!copy($webimage, $media_answer_url)) {
//echo "COULD NOT DOWNLOAD " . $webimage . " ";
}
chdir('..');
chdir('..');
chdir('..');
chdir('..');
}
}
chdir('..');
$url = "Location: loader.php?m=pregame&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
header($url);
}
elseif ($m == "freqplayed") {
#begin freqplayed
$drop = "DROP TABLE scores_".$region_ID;
mysqli_query($local, $drop);
$create = "CREATE TABLE `scores_".$region_ID."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`teamname` varchar(100) NOT NULL,
`loc_ID` int(11) NOT NULL,
`show_ID` int(11) NOT NULL,
`region_ID` int(11) NOT NULL,
`team_ID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1";
mysqli_query($local, $create);
$remote_sql = "https://www.triviocity.com/local_files/league_teams.sql";
$local_sql = "league_teams.sql";
copy($remote_sql, $local_sql);
//import league_teams.sql
$query = '';
$sqlScript = file($local_sql);
foreach ($sqlScript as $line) {
$startWith = substr(trim($line), 0, 2);
$endWith = substr(trim($line), -1, 1);
if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
continue;
}
$query = $query . $line;
if ($endWith == ';') {
mysqli_query($local, $query);
$query = '';
}
}
//echo $freqplayed." ";
$xa = explode("^*^|", $freqplayed);
$num_t = ($xa[0] + 1);
//echo $num_t." ";
$xb = explode("|", $xa[1]);
$a = 0;
//echo " ";
while ($a < $num_t) {
$team_info = $xb[$a];
//echo $team_info." ";
$xc = explode("^^", $team_info);
$team_ID = $xc[0];
$teamname = addslashes($xc[1]);
$team_loc = $xc[2];
$team_show = $xc[3];
if ($team_ID != "") {
$query = "select team_ID from scores_" . $region_ID . " where team_ID = " . $team_ID . " and loc_ID = " . $team_loc . " and show_ID = " . $team_show;
//echo $query . " ";
$result = mysqli_query($local, $query);
$num = mysqli_num_rows($result);
if ($num == 0 || $num == "") {
$insert = "insert into scores_" . $region_ID . " (team_ID, teamname, loc_ID, show_ID, region_ID) values (" . $team_ID . ", '" . $teamname . "', " . $team_loc . ", " . $team_show . ", " . $region_ID . ")";
//echo $insert." ";
mysqli_query($local, $insert);
}
}
$a = ($a + 1);
}
$url = "Location: loader.php?m=adspace&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
header($url);
#end freq played
}
elseif ($m=="feud") {
$dow_D = date('D');
//show existing sets
if ($dow_D=="Mon") {
$day_math = "0 days";
}
elseif ($dow_D=="Tue") {
$day_math = "1 days";
}
elseif ($dow_D=="Wed") {
$day_math = "2 days";
}
elseif ($dow_D=="Thu") {
$day_math = "3 days";
}
elseif ($dow_D=="Fri") {
$day_math = "4 days";
}
elseif ($dow_D=="Sat") {
$day_math = "5 days";
}
elseif ($dow_D=="Sun") {
$day_math = "6 days";
}
$query_date = date('Y-m-d');
$date = date_create($query_date);
date_sub($date, date_interval_create_from_date_string($day_math));
$start_date = date_format($date, 'Y-m-d');
$query = "select * from feud_sets where weekstart = '".$start_date."'";
$result = mysqli_query($local, $query);
$num = mysqli_num_rows($result);
?>
Team Trivia | Local Loader
";
$x = explode("^*^", $feud);
$feud_surveys = $x[0];
$feud_responses = $x[1];
$feud_set = $x[2];
// echo $feud_set." ";
$xf = explode("^^", $feud_set);
$insert = "insert into feud_sets (set_name, added_by, date_created, weekstart, question_set_id, set_ID)
values ('".$xf[0]."', '".$xf[1]."','".$xf[2]."','".$xf[3]."','".$xf[4]."', '".$xf[5]."')";
mysqli_query($local, $insert);
while ($a < 20) {
$xb = explode("|", $feud_surveys);
$xc = $xb[$a];
$xd = explode("^^", $xc);
$sRound = $xd[0];
$sNo = $xd[1];
$survey = addslashes($xd[2]);
$response1 = addslashes($xd[3]);
$response2 = addslashes($xd[4]);
$response3 = addslashes($xd[5]);
$response4 = addslashes($xd[6]);
$response5 = addslashes($xd[7]);
$sID = $xd[8];
$set_ID = $xd[9];
if ($sRound!="") {
$insert = "insert into feud_surveys_host (sRound, sNo, survey, response1, response2, response3, response4, response5, sID, user_ID, set_ID)
values ('" . $sRound . "', " . $sNo . ", '" . $survey . "', '" . $response1 . "', '" . $response2 . "', '" . $response3 . "', '" . $response4 . "', '" . $response5 . "', " . $sID . ", ".$user_ID.", ".$set_ID.")";
//echo $insert . " ";
mysqli_query($local, $insert);
}
$a = ($a + 1);
}
$a = 0;
while ($a < 83) {
$xb = explode("|", $feud_responses);
$xc = $xb[$a];
// echo $a.": ".$xc." ";
$xd = explode("^^", $xc);
$response_rank = $xd[0];
$response = addslashes($xd[1]);
$hint = $xd[2];
$sID = $xd[3];
$set_ID = $xd[4];
if ($response_rank!="") {
$insert = "insert into feud_responses_host (response_rank, response, hint, sID, set_ID, user_ID) values
(" . $response_rank . ", '" . $response . "', '" . $hint . "', " . $sID . ", " . $set_ID . ", " . $user_ID . ")";
//echo $insert." ";
mysqli_query($local, $insert);
}
$a = ($a + 1);
}
$url = "Location: loader.php?m=adspace&show_loc_ID=".$show_loc_ID."&show_show_ID=".$show_show_ID."&show_game_date=".$show_game_date."&qsi=".$qsi."&show_type=".$show_type;
header($url);
}