<?php
date_default_timezone_set('Australia/Sydney');

// D1 daily-images — use D1 for all cameras, fallback to files.txt on failure
@include_once('/home/subdomains/livetimelapse/public_html/php/api/daily-images-helper.php');

if (!function_exists('_getFileListForDay')) {
    function _getFileListForDay($cam, $year, $month, $day, $fallbackPath) {
        if (function_exists('dailyImagesGetFileList')) {
            $d1Name = str_replace('_', '-', $cam);
            $d1Date = "$year-$month-$day";
            $d1Result = dailyImagesGetFileList($d1Name, $d1Date, 'web');
            if ($d1Result !== false) {
                $ftResult = @file("$fallbackPath", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                if ($ftResult !== false && count($ftResult) > count($d1Result) + 5) {
                    return $ftResult;
                }
                return $d1Result;
            }
        }
        return @file("$fallbackPath");
    }
}
/////////////////////////////////////////////////////////////////////////
//////////////   QUERY STRING VARIABLES   ///////////////////////////////
/////////////////////////////////////////////////////////////////////////

ini_set('display_errors','0');

$per_page = $_GET['pp'];

// Which page is being displayed
$page_number = $_GET['pn'];

// Has the latest image been requestedautom
$latest_image = $_GET['latest_image'];

// Get daily and project variables
$last2hours = $_GET['last2hours'];
$today = $_GET['today'];
$yesterday = $_GET['yesterday'];
$project = $_GET['project'];
$last30days = $_GET['last30days'];
$last7days = $_GET['last7days'];

//Get custom time period variables if they exist
$start_year = $_GET['s1y'];
$start_month = $_GET['s1m'];
$start_day = check_month_days($start_month, $_GET['s1d']);
$start_time = $_GET['s1t'];

$stop_year = $_GET['s2y'];
$stop_month = $_GET['s2m'];
$stop_day = check_month_days($stop_month, $_GET['s2d']);
$stop_time = $_GET['s2t'];

// Are we saving a timelapse sequence?
$save_timelapse = $_GET['save_timelapse'];

// Get users IP address
$ip_address = $_SERVER['REMOTE_ADDR'];
$currentCaboolture = file_get_contents("/home/subdomains/livetimelapse/public_html/cabooltureIP.txt");
$currentWarwick = file_get_contents("/home/subdomains/livetimelapse/public_html/warwickIP.txt");
$currentGayndah = file_get_contents("/home/subdomains/livetimelapse/public_html/gayndahIP.txt");
$currentRoaming = file_get_contents("/home/subdomains/livetimelapse/public_html/roamingIP.txt");

$remote_ip = file_get_contents("/home/subdomains/livetimelapse/public_html/remote_ip.txt");

// Defaults if not set above
if (!$per_page) { $per_page = '20'; }
if (!$page_number) { $page_number = '1'; }

/////////////////////////////////////////////////////////////////////////
//////////////   ASSORED VARIABLES   ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

$cache = rand(0,10000);
$timeLapseImageSize = '980x655';
$displayImageSize = '980x655';
$newS3 = TRUE;

require('tlc.include.a-script.php');

if ($cam == 'tlc_mettle_robina_jn1040_zoom') { $cam = 'tlc_mettle_robina_jn1040'; $timeLapseImageSize = '980x551'; $displayImageSize = '980x551'; echo 'here'; echo $displayImageSize; date_default_timezone_set("Australia/Sydney"); $newS3 = TRUE; }

////////////////////////////////////////////////////////////////////////
//////////////   FUNCTION LIST            ///////////////////////////////
/////////////////////////////////////////////////////////////////////////

function get_images($cam, $date, $start_time_unix, $stop_time_unix)
{
    global $basedir;
    global $basedir_archive;
    global $archived_images_thumbs;
    global $archived_images_full;

    global $project_start_date;
    global $project_start_hour;
    global $project_start_min;
    global $project_start_ampm;

    global $project_stop_hour;
    global $project_stop_min;
    global $project_stop_ampm;

    global $last2hours;
    global $today;
    global $yesterday;
    global $project;

    global $timeLapseImageSize;

    if ($cam == 'tlc_wilson_park_zoom') { $cam = 'tlc_wilson_park'; }

    $start_date = date('Ymd', $start_time_unix);
    $stop_date = date('Ymd', $stop_time_unix);

    $directories = Array();
    $a = 0;
    $working_date = $start_date;

    while ($working_date <= $stop_date)
    {
        $directories[$a] = "{$basedir_archive}/{$cam}/".substr($working_date,0,4)."/".substr($working_date,4,2)."/".substr($working_date,6,2)."";
        $dates[$a] = $working_date; // Add to master dates array
        $working_date = date('Ymd', (strtotime($working_date) + 86400));
        $a++;
        if ($a == 1000) { break; }
    }

    $c = 0;


    for ($b = 0; $b < sizeof($directories); $b++)
    {
        if ($last2hours == 'on')
        {
            // If no start and stop details exist in the query string, use a default as below
            $current_time = time();
            $current_date = date('Ymd');
            $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");

            // If outside of the cameras operating hours, display the last hour of images
            if (($current_time > $relative_project_stop || $current_time < $relative_project_start))
            {
                if ($current_time > $relative_project_stop)
                {
                    $start_hour = $relative_project_stop - 7200;
                    $stop_hour = $relative_project_stop;
                }
                else
                {
                    $start_hour =  $relative_project_stop - 93600;
                    $stop_hour =  $relative_project_stop - 86400;
                }
            }
            else
            {
                $start_hour = $start_time_unix;;
                $stop_hour = $stop_time_unix;
            }
        }
        elseif ($today == 'on' || $yesterday == 'on' || $project == 'on')
        {
            $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
        }
        else
        {
            $days = sizeof($directories);
            $current_time = time();
            $current_date = date('Ymd');
            $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");

            // For single day sequences
            if ($days == 1)
            {
                // Use the start and stop time selected
                $start_hour = $start_time_unix;
                $stop_hour = $stop_time_unix;
            }
            // For sequences spanning multiple days
            else
            {
                // If day 1 of the squence
                if ($b == 0)
                {
                    // Honour the start time selected.  Use project relative stop time
                    $start_hour = $start_time_unix;
                    $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                }
                // If it's the last day in the sequence
                elseif ($b == ($days-1))
                {
                    // Use project relative start but honour the selected stop hour
                    $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                    $stop_hour = $stop_time_unix;
                }
                // For all other days
                else
                {
                    // Use project relative start and stop hours
                    $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                    $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                }
            }
        }

        //print_r($dates);

        $current_year = substr($dates[$b], 0, 4);
        $current_month = substr($dates[$b], 4, 2);
        $current_day = substr($dates[$b], 6, 2);

        $current_file_list = _getFileListForDay($cam, $current_year, $current_month, $current_day, "{$basedir_archive}/$cam/$current_year/$current_month/$current_day/files.txt");
        if(is_bool($current_file_list) === true) {
            $current_file_list = array();
        }
        for ($c = 0; $c < sizeof($current_file_list); $c++)
        {
            $current_file_list[$c] = trim($current_file_list[$c]);
            if (strstr($current_file_list[$c], $timeLapseImageSize))
            {
                $check = str_replace("{$timeLapseImageSize}_", '', $current_file_list[$c]);
                $check = str_replace('.jpg', '', $check);
                $current_image_time = strtotime("{$dates[$b]}, $check");

                if (@$_GET['ben'] == 'yes') { echo $current_file_list[$c]; }

                if ($current_image_time > $start_hour && $current_image_time < $stop_hour)
                {
                    $archived_images[] = "{$cam}/$current_year/$current_month/$current_day/".trim($current_file_list[$c]);
                }
            }
        }
    }
    if(is_array($archived_images)) {
        @sort($archived_images);
    }

    return $archived_images;

}

function get_project_images($cam)
{
    global $archivedir;
    global $basedir_archive;
    global $basedir;

    global $project_start_time_unix;
    global $project_stop_time_unix;

    global $project_start_hour;
    global $project_start_min;
    global $project_start_ampm;

    global $project_stop_hour;
    global $project_stop_min;
    global $project_stop_ampm;

    global $project_images_per_day;
    global $project_limit_start_hour;
    global $project_limit_start_min;
    global $project_limit_start_ampm;
    global $project_limit_stop_hour;
    global $project_limit_stop_min;
    global $project_limit_stop_ampm;

    global $timeLapseImageSize;

    if ($cam == 'tlc_wilson_park_zoom') { $cam = 'tlc_wilson_park'; }

    $start_date = date('Ymd', $project_start_time_unix);
    $stop_date = date('Ymd', $project_stop_time_unix);
    $current_date = date('Ymd', time()+86400);
    $current_date_unix = time()+86400;

    $directories = Array();
    $a = 0;
    $working_date = $project_start_time_unix;

    while (($working_date <= $project_stop_time_unix) && ($working_date < $current_date_unix))
    {
        $directories[$a] = "{$basedir_archive}/{$cam}/".date("Y", $working_date)."/".date("m", $working_date)."/".date("d", $working_date);
        $dates[$a] = date("Ymd", $working_date); // Add to master dates array
        $working_date = $working_date + 86400;
        $a++;
        if ($a == 1000) { break; }
    }

    for ($b = 0; $b < sizeof($directories); $b++)
    {
        if ($project_limit_start_hour) {
            $start_hour = strtotime("{$dates[$b]}, {$project_limit_start_hour}:{$project_limit_start_min}$project_limit_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_limit_stop_hour}:{$project_limit_stop_min}$project_limit_stop_ampm");
        } else {
            $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
        }

        $current_year = substr($dates[$b], 0, 4);
        $current_month = substr($dates[$b], 4, 2);
        $current_day = substr($dates[$b], 6, 2);

        $current_list = _getFileListForDay($cam, $current_year, $current_month, $current_day, "{$directories[$b]}/files.txt");
        if(is_bool($current_list) === false) {
            // Filter the file list.  We only want 640x480 images between the project start and stop times
            for ($a = 0; $a < sizeof($current_list); $a++)
            {
                if (strstr($current_list[$a], $timeLapseImageSize))
                {
                    $check = str_replace("{$timeLapseImageSize}_", '', $current_list[$a]);
                    $check = str_replace('.jpg', '', $check);
                    $current_image_time = strtotime("{$dates[$b]}, $check");

                    if (($current_image_time > $start_hour) && ($current_image_time < $stop_hour))
                    {
                        $filtered_list[$b][] = "{$cam}/$current_year/$current_month/$current_day/".trim($current_list[$a]);
                    }
                }
            }
            if(is_null($filtered_list[$b]) === false) {
                $forward_skip = round(sizeof($filtered_list[$b]) / $project_images_per_day);
                if ($forward_skip <= 0) { $forward_skip = 1; }

                if ($forward_skip > 0)
                {
                    $a = 0;
                    while ($filtered_list[$b][$a])
                    {
                        $final_list[] = $filtered_list[$b][$a];
                        $a = $a + $forward_skip;
                    }
                }
            }
        }
    }
    if(is_null($final_list) === true) {
        $final_list = array();
    }
    sort($final_list);

    return $final_list;

}


function get_last30days_images($cam)
{
    global $archivedir;
    global $basedir_archive;
    global $basedir;

    global $project_start_time_unix;
    global $project_stop_time_unix;

    global $project_start_hour;
    global $project_start_min;
    global $project_start_ampm;

    global $project_stop_hour;
    global $project_stop_min;
    global $project_stop_ampm;

    global $project_images_per_day;
    global $project_limit_start_hour;
    global $project_limit_start_min;
    global $project_limit_start_ampm;
    global $project_limit_stop_hour;
    global $project_limit_stop_min;
    global $project_limit_stop_ampm;

    global $timeLapseImageSize;
    global $last30days;

    if ($cam == 'tlc_wilson_park_zoom') { $cam = 'tlc_wilson_park'; }

    $start_date = date("Ymd", strtotime("-30 days"));
    $stop_date = date('Ymd', $project_stop_time_unix);
    $current_date = date('Ymd', time()+86400);
    $current_date_unix = time()+86400;

    $directories = Array();
    $a = 0;
    $working_date = strtotime("-30 days");

    $project_images_per_day = 12;
    $project_limit_start_hour = '06';
    $project_stop_hour = '18';

    echo date("Ymd", $working_date);

    while (($working_date <= $project_stop_time_unix) && ($working_date < $current_date_unix))
    {
        $directories[$a] = "{$basedir_archive}/{$cam}/".date("Y", $working_date)."/".date("m", $working_date)."/".date("d", $working_date);
        $dates[$a] = date("Ymd", $working_date); // Add to master dates array
        $working_date = $working_date + 86400;
        $a++;
        if ($a == 1000) { break; }
    }

    for ($b = 0; $b < sizeof($directories); $b++)
    {
        if ($project_limit_start_hour) {
            $start_hour = strtotime("{$dates[$b]}, {$project_limit_start_hour}:{$project_limit_start_min}$project_limit_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_limit_stop_hour}:{$project_limit_stop_min}$project_limit_stop_ampm");
        } else {
            $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
        }

        $current_year = substr($dates[$b], 0, 4);
        $current_month = substr($dates[$b], 4, 2);
        $current_day = substr($dates[$b], 6, 2);

        $current_list = _getFileListForDay($cam, $current_year, $current_month, $current_day, "{$directories[$b]}/files.txt");
        if(is_bool($current_list) === false) {
            // Filter the file list.  We only want 640x480 images between the project start and stop times
            for ($a = 0; $a < sizeof($current_list); $a++)
            {
                if (strstr($current_list[$a], $timeLapseImageSize))
                {
                    $check = str_replace("{$timeLapseImageSize}_", '', $current_list[$a]);
                    $check = str_replace('.jpg', '', $check);
                    $current_image_time = strtotime("{$dates[$b]}, $check");

                    if (($current_image_time > $start_hour) && ($current_image_time < $stop_hour))
                    {
                        $filtered_list[$b][] = "{$cam}/$current_year/$current_month/$current_day/".trim($current_list[$a]);
                    }
                }
            }
            if(is_null($filtered_list[$b]) === false) {
                $forward_skip = round(sizeof($filtered_list[$b]) / $project_images_per_day);
                if ($forward_skip <= 0) { $forward_skip = 1; }

                if ($forward_skip > 0)
                {
                    $a = 0;
                    while ($filtered_list[$b][$a])
                    {
                        $final_list[] = $filtered_list[$b][$a];
                        $a = $a + $forward_skip;
                    }
                }
            }
        }
    }
    if(is_null($final_list) === true) {
        $final_list = array();
    }
    sort($final_list);

    return $final_list;

}

function get_last7days_images($cam)
{
    global $archivedir;
    global $basedir_archive;
    global $basedir;

    global $project_start_time_unix;
    global $project_stop_time_unix;

    global $project_start_hour;
    global $project_start_min;
    global $project_start_ampm;

    global $project_stop_hour;
    global $project_stop_min;
    global $project_stop_ampm;

    global $project_images_per_day;
    global $project_limit_start_hour;
    global $project_limit_start_min;
    global $project_limit_start_ampm;
    global $project_limit_stop_hour;
    global $project_limit_stop_min;
    global $project_limit_stop_ampm;

    global $timeLapseImageSize;
    global $last30days;
    global $last7days;

    if ($cam == 'tlc_wilson_park_zoom') { $cam = 'tlc_wilson_park'; }

    $start_date = date("Ymd", strtotime("-7 days"));
    $stop_date = date('Ymd', $project_stop_time_unix);
    $current_date = date('Ymd', time()+86400);
    $current_date_unix = time()+86400;

    $directories = Array();
    $a = 0;
    $working_date = strtotime("-7 days");

    $project_images_per_day = 30;
    $project_limit_start_hour = '06';
    $project_stop_hour = '18';

    echo date("Ymd", $working_date);

    while (($working_date <= $project_stop_time_unix) && ($working_date < $current_date_unix))
    {
        $directories[$a] = "{$basedir_archive}/{$cam}/".date("Y", $working_date)."/".date("m", $working_date)."/".date("d", $working_date);
        $dates[$a] = date("Ymd", $working_date); // Add to master dates array
        $working_date = $working_date + 86400;
        $a++;
        if ($a == 1000) { break; }
    }

    for ($b = 0; $b < sizeof($directories); $b++)
    {
        if ($project_limit_start_hour) {
            $start_hour = strtotime("{$dates[$b]}, {$project_limit_start_hour}:{$project_limit_start_min}$project_limit_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_limit_stop_hour}:{$project_limit_stop_min}$project_limit_stop_ampm");
        } else {
            $start_hour = strtotime("{$dates[$b]}, {$project_start_hour}:{$project_start_min}$project_start_ampm");
            $stop_hour = strtotime("{$dates[$b]}, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
        }

        $current_year = substr($dates[$b], 0, 4);
        $current_month = substr($dates[$b], 4, 2);
        $current_day = substr($dates[$b], 6, 2);

        $current_list = _getFileListForDay($cam, $current_year, $current_month, $current_day, "{$directories[$b]}/files.txt");
        if(is_bool($current_list) === false) {
            // Filter the file list.  We only want 640x480 images between the project start and stop times
            for ($a = 0; $a < sizeof($current_list); $a++)
            {
                if (strstr($current_list[$a], $timeLapseImageSize))
                {
                    $check = str_replace("{$timeLapseImageSize}_", '', $current_list[$a]);
                    $check = str_replace('.jpg', '', $check);
                    $current_image_time = strtotime("{$dates[$b]}, $check");

                    if (($current_image_time > $start_hour) && ($current_image_time < $stop_hour))
                    {
                        $filtered_list[$b][] = "{$cam}/$current_year/$current_month/$current_day/".trim($current_list[$a]);
                    }
                }
            }
            if(is_null($filtered_list[$b]) === false) {
                $forward_skip = round(sizeof($filtered_list[$b]) / $project_images_per_day);
                if ($forward_skip <= 0) { $forward_skip = 1; }

                if ($forward_skip > 0)
                {
                    $a = 0;
                    while ($filtered_list[$b][$a])
                    {
                        $final_list[] = $filtered_list[$b][$a];
                        $a = $a + $forward_skip;
                    }
                }
            }
        }
    }
    if(is_null($final_list) === true) {
        $final_list = array();
    }
    sort($final_list);

    return $final_list;

}

function isLeapYear($year) {
    return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
}

function check_month_days($month, $day)
{
    if ($month == 'jan' && $day > 31) { return 31; }
    if ($month == 'feb' && $day > 28) {
        if(isLeapYear(date('Y'))) {
            return 29;
        }
        else {
            return 28;
        }
    }
    if ($month == 'mar' && $day > 31) { return 31; }
    if ($month == 'apr' && $day > 30) { return 30; }
    if ($month == 'may' && $day > 31) { return 31; }
    if ($month == 'jun' && $day > 30) { return 30; }
    if ($month == 'jul' && $day > 31) { return 31; }
    if ($month == 'aug' && $day > 31) { return 31; }
    if ($month == 'sep' && $day > 30) { return 30; }
    if ($month == 'oct' && $day > 31) { return 31; }
    if ($month == 'nov' && $day > 30) { return 30; }
    if ($month == 'dec' && $day > 31) { return 31; }
    else { return $day; }
}

function round_time($time, $mode, $nearest)
{
    if ($mode == 'up')
    {
        if ($nearest == 10)
        {
            $time_values = '|00|,|10|,|20|,|30|,|40|,|50|';

            $a = 0;
            $current_minutes = date("i", $time);
            while (strstr($time_values, "|$current_minutes|") == FALSE)
            {
                $current_minutes = date("i", $time);
                $time = $time+30;
                $a++;
                if ($a == 20) { break; }
            }
        }
    }
    if ($mode == 'down')
    {
        if ($nearest == 10)
        {
            $time_values = '|00|,|10|,|20|,|30|,|40|,|50|';

            $a = 0;
            $current_minutes = date("i", $time);
            while (strstr($time_values, "|$current_minutes|") == FALSE)
            {
                $current_minutes = date("i", $time);
                $time = $time-30;
                $a++;
                if ($a == 20) { break; }
            }
        }
    }
    return $time;
}

/////////////////////////////////////////////////////////////////////////
//////////////   BEGIN MAIN SCRIPT        ///////////////////////////////
/////////////////////////////////////////////////////////////////////////

if ($last2hours == 'on' || $today == 'on' || $yesterday == 'on' || $project == 'on' || $last30days == 'on' || $last7days == 'on')
{

    ///////////////////////////////
    // PRE-DEFINED TIME PERIODS ///
    ///////////////////////////////

    if ($last2hours == 'on')
    {
        $loop_speed = '120';
        $start_time_unix = time()-7200;
        $stop_time_unix = time();

        // If no start and stop details exist in the query string, use a default as below
        $current_time = time();
        $current_date = date("Ymd");
        $yesterdays_date = date("Ymd",time()-86400);
        $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");

        // If outside of the cameras operating hours, display the last hour of images
        if (($current_time > $relative_project_stop || $current_time < $relative_project_start))
        {
            if ($current_time > $relative_project_stop)
            {
                $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                $start_time_unix = $relative_project_stop - 7200;
                $stop_time_unix = $relative_project_stop;
            }
            else
            {
                $relative_project_start = strtotime("$yesterdays_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                $relative_project_stop = strtotime("$yesterdays_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                $start_time_unix =  $relative_project_stop - 3600;
                $stop_time_unix =  $relative_project_stop;
            }
        }

        // If the start time is before the actual start time of the project for the current day
        if ($start_time_unix < $relative_project_start) { $start_time_unix = $relative_project_start; }
    }
    elseif ($today == 'on')
    {
        $loop_speed = '120';
        $today_date = date("Ymd");
        $start_time_unix = strtotime("$today_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $stop_time_unix = strtotime("$today_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
    }
    elseif ($yesterday == 'on')
    {
        $loop_speed = '120';
        $yesterday_date = date("Ymd", time()-86400);
        $start_time_unix = strtotime("$yesterday_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $stop_time_unix = strtotime("$yesterday_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
    }
    elseif ($project == 'on')
    {
        $loop_speed = '120';
        $today_date = date("Ymd");
        $start_time_unix = strtotime("$project_start_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $stop_time_unix = strtotime("$today_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
    }
    elseif ($last30days == 'on')
    {
        $loop_speed = '120';
        $start_date = date("Ymd", strtotime("-30 days"));
        $start_time_unix = strtotime("$start_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $stop_time_unix = strtotime("$today_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
    }
    elseif ($last7days == 'on')
    {
        $loop_speed = '120';
        $start_date = date("Ymd", strtotime("-7 days"));
        $start_time_unix = strtotime("$start_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $stop_time_unix = strtotime("$today_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
    }

    $animation = 'yes';

    // We still need these variables to have the time options selected
    $start_time_unix = round_time($start_time_unix, 'down', '10');
    $start_year = date("Y", $start_time_unix);
    $start_month = strtolower(date("M", $start_time_unix));
    $start_day = date("d", $start_time_unix);
    $start_time = date("Hi", $start_time_unix);

    $stop_time_unix = round_time($stop_time_unix, 'up', '10');
    $stop_year = date("Y", $stop_time_unix);
    $stop_month = strtolower(date("M", $stop_time_unix));
    $stop_day = date("d", $stop_time_unix);
    $stop_time = date("Hi", $stop_time_unix);
}
elseif ($_GET['s1y'] && $_GET['s2y'])
{

    ///////////////////////////////
    // CUSTOM TIME PERIOD       ///
    ///////////////////////////////

    // Assorted Variables
    $loop_speed = '120';
    $animation = 'yes';

    //If the start and stop details exist from the query string, use them
    if ($start_year && $stop_year)
    {
        $start_time_unix = strtotime("$start_month $start_day,$start_year, $start_time");
        $stop_time_unix = strtotime("$stop_month $stop_day, $stop_year, $stop_time");
    }
    else
    {
        // If no start and stop details exist in the query string, use a default as below
        $current_time = time();
        $current_date = date("Ymd");
        $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
        $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");

        // If outside of the cameras operating hours, display the last hour of images
        if (($current_time > $relative_project_stop || $current_time < $relative_project_start))
        {
            if ($current_time > $relative_project_stop)
            {
                $start_time_unix = $relative_project_stop - 3600;
                $stop_time_unix = $relative_project_stop;
            }
            else
            {
                $start_time_unix =  $relative_project_stop - 90000;
                $stop_time_unix =  $relative_project_stop - 86400;
            }
        }
        // Otherwise, default to the previous hours worth of images
        else
        {
            $start_time_unix = time()-3600;
            $stop_time_unix = time();
        }
    }

    // We still need these variables to have the time options selected
    $start_time_unix = round_time($start_time_unix, 'down', '10');
    $start_year = date("Y", $start_time_unix);
    $start_month = strtolower(date("M", $start_time_unix));
    $start_day = date("d", $start_time_unix);
    $start_time = date("Hi", $start_time_unix);

    $stop_time_unix = round_time($stop_time_unix, 'up', '10');
    $stop_year = date("Y", $stop_time_unix);
    $stop_month = strtolower(date("M", $stop_time_unix));
    $stop_day = date("d", $stop_time_unix);
    $stop_time = date("Hi", $stop_time_unix);

}
else
{
    // This is to show a default time selection for the custom time period
    $start_time_unix = round_time($start_time_unix, 'down', '10');
    $start_time_unix = time()-3600;
    $start_year = date("Y", $start_time_unix);
    $start_month = strtolower(date("M", $start_time_unix));
    $start_day = date("d", $start_time_unix);
    $start_time = date("Hi", $start_time_unix);

    $stop_time_unix = round_time($stop_time_unix, 'up', '10');
    $stop_time_unix = time();
    $stop_year = date("Y", $stop_time_unix);
    $stop_month = strtolower(date("M", $stop_time_unix));
    $stop_day = date("d", $stop_time_unix);
    $stop_time = date("Hi", $stop_time_unix);

}

// Creat the archived image list
if ($start_time_unix > $stop_time_unix)
{
    $error = 'yes';
}
elseif ($project == 'on')
{
    $image_list = get_project_images($cam);
    echo date("Ymd", $project_start_time_unix);
}
elseif ($last30days == 'on')
{

    $image_list = get_last30days_images($cam);
    //echo "<br><br><br>";

//    print_r($image_list);
}
elseif ($last7days == 'on')
{

    $image_list = get_last7days_images($cam);
    //echo "<br><br><br>";

  //  print_r($image_list);
}
else
{
    $image_list = get_images($cam, $date, $start_time_unix, $stop_time_unix);
}

if(is_array($image_list)) {
    $total_images = sizeof($image_list);
}
else {
    $image_list = array();
}
$animation_start_screen = date("g:ia, F j", $start_time_unix);
$animation_stop_screen = date("g:ia, F j", $stop_time_unix);


/////////////////////////////////////////////////////////////////////////
//////////////   START BROWSER OUTPUT       /////////////////////////////
/////////////////////////////////////////////////////////////////////////



//if ($cam == 'tlc_osg_wynnum_jn1018') { echo "Work in progress"; exit(); }

?>

<script src="https://www.livetimelapse.com.au/js/exif.js"></script>

<script type="text/javascript">

    <?php

    // All image URLs routed through CDN Worker — no region lookup needed
    $basedir_archive_www ='https://legacy.livetimelapse.com.au/db/archive_timelapse';

    if ($animation == 'yes')
    {
        echo '
  webcam_images = new Array();
  webcam_images_display = new Array();
  image_status = new Array();
  ';
        if ($newS3) {
            $bucketName = str_replace("_", "-", $cam);
        } else {
            $bucketName = $cam;
        }

        $fileListName = $bucketName.".".rand(0, 100000);
        $imageListSize = sizeof($image_list);
        for ($a = 0; $a < $imageListSize; $a++)
        {
            $getYear = explode('/', $image_list[$a]);
            if ($archivedCamera) { $bucketName = 'tlc-www-archive'; }
            $imagePath = str_replace("{$bucketName}","{$cam}",$image_list[$a]);
            echo "webcam_images[$a] = \"https://images.livetimelapse.com.au/{$bucketName}/{$imagePath}\";\r\n";
            $zipOutput[] = "https://images.livetimelapse.com.au/{$bucketName}/{$imagePath}";
        }

        file_put_contents("/home/subdomains/livetimelapse/public_html/www-download/{$fileListName}.txt", serialize ($zipOutput));

        echo "\r\n";

        $nimages = sizeof($image_list);
        $scrollbarimages = sizeof($image_list);

        echo "nImages = $nimages;\r\n";
    }

    ?>

    function loop_mode_decide()
    {
        if (play_status == 'play')
        {
            stop();
        }
        else if (play_status == 'stop')
        {
            fwd();
        }
    }

    function nav()
    {
        var w = document.webcams.cams.selectedIndex;
        var url_add = document.webcams.cams.options[w].value;
        window.location.href = url_add;
    }

    function select_event()
    {
        var w = document.custom_time_period.events.selectedIndex;
        var url_add = document.custom_time_period.events.options[w].value;
        window.location.href = url_add;
    }

</script>
<?php

if ($animation == 'yes')
{
    echo "<script type=\"text/javascript\" src=\"https://www.livetimelapse.com.au/js/timelapse.console.preload.js\"></script>";
}

?>
<link rel="shortcut icon" href="https://php.livetimelapse.com.au/tlc.include.ico">
<style tyle="text/css">

    body
    {
        margin-top:0px;
        background-color:white;
        color: black;
        /* background: #EEEEEE url('http://www.weatherzone.com.au/images/widgets/bg_body_new.gif') repeat-y 50% 0; */
    }

    .gradient
    {
        /* fallback (Opera) */
        background: #c3cde8;
        /* Mozilla: */
        background: -moz-linear-gradient(left, #c3cde8, #FFFFFF);
        /* Chrome, Safari:*/
        background: -webkit-gradient(linear, left top, right top, from(#c3cde8), to(#FFFFFF));
        /* MSIE */
        filter: progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#c3cde8', EndColorStr='#FFFFFF', GradientType=1);
    }

    table,td,tr
    {
        font-family:"Helvetica", "Arial", sans-serif;
        font-size: 11pt;
    }

    .heading
    {
        color: #002352;
        font-weight:bold;
        letter-spacing:1px;
    }

    .text
    {
        color: #012c5a;
    }

    .time
    {
        width:70px;
    }

    .link
    {
        color: #002352;
        font-weight:bold;
        padding-top:10px;
        padding-bottom:10px;
    }

    .camera_menu
    {
        width:220px;
    }

    A:LINK, A:VISITED, A:ACTIVE
    {
        text-decoration: none;
        letter-spacing:1px;
        color: #002352;
    }

    A:HOVER
    {
        text-decoration: none;
        color: red;
    }

    .buttona
    {
        font: Verdana, Geneva, Arial, Helvetica, sans-serif;
        background-color: #D7E5F2;
        color: #102132;
        padding:10px;
        padding-top:5px;
        padding-bottom:5px;

    }

    .button {
        background-color: #b9caf7;
        -moz-border-radius: 5px;
        -webkit-border-radius: 5px;
        -moz-box-shadow: 2px 2px 1px #999;
        -webkit-box-shadow: 2px 2px 1px #999;
        border: solid 1px #CCC;
        font-size:14px;
        color: #000000;
        text-shadow: 2px 2px 5px #000000;
    }

    .button:hover {
        background-color: #05b3ff;
    }



    #progress_bar_container
    {
        width:100%;
        padding-left:0px;
        padding-right:0px;
        padding-top:0px;
    }

    #progress_wrapper
    {
        position:relative;
        width:100%;
        height:30px;
        padding-top:0px;
        padding-bottom:0px;
        align:center;
        border:solid 1px #595858;
    }

    #exifData
    {
        position: absolute;
        bottom: 15px;
        left: 0px;
        padding: 8px;
        font-size: 120%;
        color: yellow;
        text-shadow: 1px 1px black;
    }

    #cameraInfo
    {
        position: absolute;
        top: 0px;
        left: 0px;
        width: 980px;
        height: 655px;
        padding: 8px;
        font-size: 100%;
        color: yellow;
        text-shadow: 1px 1px black;
        text-align: left;
    }


    .progress_bar
    {
        z-index:0;
        position:absolute;
        top:0px;
        left:0px;
        width:1px;
        height:28px;
        padding:0px;
        border-right:solid 1px #595858;
        background-color:#064273;
        color: white;
        text-align:left;
    }

    .progress_text
    {
        z-index:1;
        position:absolute;
        top:5px;
        left:0px;
        height: 100%;
        width: 100%;
        padding:0px;
        padding-top:0px;
        text-align:middle;
        color: #a2a0a0;
    }

    .infoHeader
    {
        display: inline-block;
        color: red;
        padding: 2px 5px 2px 5px;
    }

    .infoText
    {
        display: inline-block;
        color: green;
    }

    .thumbWrapper {
        display: inline-block;
    }

    .thumbLabel {
        position: absolute;
        width: 980px;
        margin-top: 305px;
        font-size: 120%;
        color: yellow;
        font-weight: bold;
        text-align: center;
        z-index: 100;
        text-shadow: 2px 2px 2px black;
        padding: 3px 8px 3px 8px;
    }

</style>
<title>Time Lapse Pty Ltd - <?php echo $html_title; ?></title>
</head>
<?php

$basedir_www = "https://www.livetimelapse.com.au/clients".$_SERVER['SCRIPT_NAME'];

if ($animation == 'yes') { echo "<body onLoad=\"launch()\">"; }

?>

<table align="center" style="background-color:white;width:1045px;height:100%;" cellpadding="0" cellspacing="2">
    <tr>
        <td>

            <table align="center" style="background-color:white;width:100%;height:100%;border:2px solid grey;" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="height:100px;background-color:white;color:black;border-bottom: 1px solid black;">

                        <table style="width:100%;height:100%;" cellpadding="0" cellspacing="0">
                            <tr>
                                <td>

                                    <div style="padding-left:10px;">
                                        <img src="https://www.livetimelapse.com.au/images/logo.png" width="430" height="80">
                                    </div>

                                </td>
                                <td>

                                    <table style="width:100%;height:100%;" cellpadding="0" cellspacing="0">
                                        <tr valign="top">
                                            <td align="right">

                                                <div style="padding:10px;">
                                                    <font class="heading">Welcome <?php echo $html_client; ?></font>
                                                    <br>
                                                    Time Lapse Management Console V1.1
                                                </div>


                                            </td>
                                        </tr>
                                        <tr valign="bottom">
                                            <td align="right">


                                            </td>
                                        </tr>
                                    </table>

                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>
                <tr>
                    <td style="background-color:#064273;color:white;">

                        <table width="100%">
                            <tr>
                                <td>

                                    <div style="padding-top:5px;padding-bottom:5px;">

                                </td>
                                <td>

                                    <font style="color:white;font-weight:bold;">Current Location:</font><font style="color:white;"> <?php echo $html_location; ?></font>

                                </td>
                                <form name="webcams" action="">
                                    <td align="right">

                                        <a href="logout.php" style="color: white;">Logout</a>

                                        <select name="cams" onChange="nav();" class="camera_menu" style="color:#3a62c9;">
                                            <option value="">Jump to Camera</option>
                                            <option value="">&nbsp;</option>
                                            <?php

                                            for ($a = 0; $a < sizeof($camera_quicklink_list)+3; $a++)
                                            {
                                                if ($camera_quicklink_list[$a]['title'] == 'SPACER')
                                                {
                                                    echo "<option value=\"#\" >&nbsp;</option>";
                                                }
                                                else
                                                {
                                                    echo "<option value=\"{$camera_quicklink_list[$a]['link']}\" >{$camera_quicklink_list[$a]['title']}</option>";
                                                }
                                            }

                                            ?>
                                            <option>&nbsp;</option>
                                        </select>

                                    </td>
                                </form>
                            </tr>
                        </table>

                    </td>
                </tr>
                <tr valign="top">
                    <td style="height:100%;">

                        <table style="width:100%;height:100%;background-color:#d9e0f0;" cellpadding="0" cellspacing="0">
                            <tr valign="top">
                                <td style="width:25%;border-right: 1px solid #9399a9;">

                                    <table style="width:100%;" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="heading" style="font-size:150%;padding-top:10px;padding-bottom:10px;" align="center">Time Lapse Control</div>

                                                <?php

                                                if ($animation == 'yes')
                                                {
                                                    echo "
                  <div style=\"text-align:justify;padding:7px;\">
                  Use the left and right arrows on your keyboard to step through the images
                  </div>
  
                  <div align=\"center\" style=\"padding-left:5px;padding-right:5px;\">
                  <!— Start Animation Controls Table —>
                  <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"color:white;\">
                  <tr>
                  <td align=\"center\">
                  <div style=\"padding:2px;\">
                  <a href=\"JavaScript: func()\" onClick=\"decrementImage();\" id=\"normal\" title=\"Step backward one frame. You can also use the back arrow on your keyboard.\">
                  <img src=\"https://www.livetimelapse.com.au/images/left.png\" width=\"46\" height=\"45\" border=\"0\">
                  </a>
                  </div>
                  </td>
                  <td align=\"center\">
                  <div style=\"padding:2px;\">
                  <a href=\"JavaScript: func()\" onClick=\"loop_mode_decide();\" id=\"normal\" title=\"Pause or start the time lapse sequence\">
                  <img src=\"https://www.livetimelapse.com.au/images/playpause.png\" width=\"60\" height=\"45\" border=\"0\">
                  </a>
                  </div>
                  </td>
                  <td align=\"center\">
                  <div style=\"padding:2px;\">
                  <a href=\"javascript:void(0);\" onClick=\"incrementImage();\" title=\"Step foward one frame.  You can also use the forward arrow on your keyboard.\">
                  <img src=\"https://www.livetimelapse.com.au/images/right.png\" width=\"46\" height=\"45\" border=\"0\">
                  </a>
                  </div>
                  </td>
                  <td align=\"center\">
                  <div style=\"padding:2px;\">
                  <a href=\"javascript:void(0);\" onClick=\"change_speed(dSt);\" title=\"Decrease the speed of the time lapse.  You can also use the '<' key.\">
                  <img src=\"https://www.livetimelapse.com.au/images/slower.png\" width=\"46\" height=\"45\" border=\"0\">
                  </a>
                  </div>
                  </td>
                  <td align=\"center\">
                  <div style=\"padding:2px;\">
                  <A HREF=\"JavaScript: func()\" onClick=\"change_speed(-dSt)\" id=\"Down\" title=\"Increase the speed of the time lapse. You can also use the '>' key.\">
                  <img src=\"https://www.livetimelapse.com.au/images/faster.png\" width=\"46\" height=\"45\" border=\"0\">
                  </a>
                  </div>
                  </td>    
                  </tr>   
                  </table>
                  <!— Stop Animation Controls Table —>
                  <br>
                  ";
                                                }
                                                else
                                                {
                                                    echo "<div style=\"padding:5px;text-align:justify;\">You are currently viewing the realtime image.  Use the custom time selection or pre-defined time period links
                  below to view a time lapse sequence</div><br>";
                                                }

                                                ?>

                                            </td>
                                        </tr>
                                        <tr valign="top">
                                            <form name="custom_time_period" action="<?php echo $basedir_www; ?>" method="GET">
                                                <td class="gradient" style="border-bottom: 1px solid #9399a9;">

                                                    <div class="heading" style="font-size:150%;padding-top:10px;padding-bottom:10px;" align="center">Custom Time Period</div>

                                                    <?php

                                                    if ($timelapse_links)
                                                    {
                                                        echo "<div class=\"link\" align=\"center\">
                  <select id=\"events\" style=\"width:220px;\" onChange=\"select_event();\">
                  <option>Time Lapse of Significant Events</option>
                  <option>&nbsp;</option>";

                                                        for ($a = 0; $a < sizeof($timelapse_links)+1; $a++)
                                                        {
                                                            if ($timelapse_links[$a]['title'] == 'SPACER')
                                                            {
                                                                echo "<option value=\"#\">&nbsp;</option>";
                                                            }
                                                            else
                                                            {
                                                                echo "<option value=\"{$timelapse_links[$a]['link']}\">{$timelapse_links[$a]['title']}</option>";
                                                            }
                                                        }

                                                        echo "<option>&nbsp;</option>
                  </select>                
                  </div>";
                                                    }

                                                    ?>

                                                    <table align="center" cellpadding=2 cellspacing=2 border=0>
                                                        <tr>
                                                            <td>

                                                                <div class="heading" align="center">Start</div>

                                                            </td>
                                                            <td>

                                                            </td>
                                                            <td>

                                                                <div class="heading" align="center">Finish</div>

                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="center">

                                                                <select class="time" name="s1y">
                                                                    <?php

                                                                    $year = '2011';

                                                                    while ($year < 2028)
                                                                    {
                                                                        echo "\t\t\t<option value=\"$year\"";
                                                                        if ($start_year == $year) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $year;
                                                                        echo "</option>\r\n";
                                                                        $year++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                            <td>

                                                            </td>
                                                            <td align="center">

                                                                <select class="time" name="s2y">
                                                                    <?php

                                                                    $year = '2011';

                                                                    while ($year < 2028)
                                                                    {
                                                                        echo "\t\t\t<option value=\"$year\"";
                                                                        if ($stop_year == $year) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $year;
                                                                        echo "</option>\r\n";
                                                                        $year++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="center">

                                                                <select class="time" name="s1m">
                                                                    <?php

                                                                    $increment = '0';
                                                                    $a = 0;

                                                                    while ($a <= 11)
                                                                    {
                                                                        $new_month = date("M", $increment);
                                                                        $new_month_lc = strtolower($new_month);
                                                                        echo "\t\t\t<option value=\"$new_month_lc\"";
                                                                        if ($start_month == $new_month_lc) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $new_month;
                                                                        echo "</option>\r\n";
                                                                        $increment = $increment + 2678410;
                                                                        $a++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                            <td>

                                                                <div style="padding:7px;">
                                                                    <img src="https://www.livetimelapse.com.au/images/arrow.png" width="45" height="24">
                                                                </div>

                                                            </td>
                                                            <td align="center">

                                                                <select class="time" name="s2m">
                                                                    <?php

                                                                    $increment = '0';
                                                                    $a = 0;

                                                                    while ($a <= 11)
                                                                    {
                                                                        $new_month = date("M", $increment);
                                                                        $new_month_lc = strtolower($new_month);
                                                                        echo "\t\t\t<option value=\"$new_month_lc\"";
                                                                        if ($stop_month == $new_month_lc) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $new_month;
                                                                        echo "</option>\r\n";
                                                                        $increment = $increment + 2678410;
                                                                        $a++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="center">

                                                                <select class="time" name="s1d">
                                                                    <?php

                                                                    $increment = '0';
                                                                    $a = 0;

                                                                    while ($a <= 30)
                                                                    {
                                                                        $new_day = date("d", $increment);
                                                                        echo "\t\t\t<option value=\"$new_day\"";
                                                                        if ($start_day == $new_day) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $new_day;
                                                                        echo "</option>\r\n";
                                                                        $increment = $increment + 86400;
                                                                        $a++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                            <td>

                                                            </td>
                                                            <td align="center">

                                                                <select class="time" name="s2d">
                                                                    <?php

                                                                    $increment = '0';
                                                                    $a = 0;

                                                                    while ($a <= 30)
                                                                    {
                                                                        $new_day = date("d", $increment);
                                                                        echo "\t\t\t<option value=\"$new_day\"";
                                                                        if ($stop_day == $new_day) { echo " selected>"; }
                                                                        else { echo ">"; }
                                                                        echo $new_day;
                                                                        echo "</option>\r\n";
                                                                        $increment = $increment + 86400;
                                                                        $a++;
                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="center">

                                                                <select class="time" name="s1t">
                                                                    <?php

                                                                    $a = 0;

                                                                    $current_date = date("Ymd");
                                                                    $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                                                                    $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                                                                    $increment = strtotime("$current_date, 12:00am");

                                                                    while ($increment <= $relative_project_stop)
                                                                    {
                                                                        $new_time_screen = date("H:i", $increment);
                                                                        $new_time_pass = date("Hi", $increment);

                                                                        if ($increment >= $relative_project_start && $increment <= $relative_project_stop)
                                                                        {
                                                                            echo "\t\t\t<option value=\"$new_time_pass\"";
                                                                            if ($start_time == $new_time_pass) { echo " selected>"; }
                                                                            else { echo ">"; }
                                                                            echo $new_time_screen;
                                                                            echo "</option>\r\n";
                                                                        }

                                                                        $increment = $increment + 600;

                                                                    }

                                                                    ?>
                                                                </select>

                                                            </td>
                                                            <td align="center">

                                                            </td>
                                                            <td align="center">

                                                                <select class="time" name="s2t">
                                                                    <?php

                                                                    $a = 0;

                                                                    $current_date = date("Ymd");
                                                                    $relative_project_start = strtotime("$current_date, {$project_start_hour}:{$project_start_min}$project_start_ampm");
                                                                    $relative_project_stop = strtotime("$current_date, {$project_stop_hour}:{$project_stop_min}$project_stop_ampm");
                                                                    $increment = strtotime("$current_date, 12:00am");

                                                                    while ($increment <= $relative_project_stop)
                                                                    {
                                                                        $new_time_screen = date("H:i", $increment);
                                                                        $new_time_pass = date("Hi", $increment);

                                                                        if ($increment >= $relative_project_start && $increment <= $relative_project_stop)
                                                                        {
                                                                            echo "\t\t\t<option value=\"$new_time_pass\"";
                                                                            if ($stop_time == $new_time_pass) { echo " selected>"; }
                                                                            else { echo ">"; }
                                                                            echo $new_time_screen;
                                                                            echo "</option>\r\n";
                                                                        }

                                                                        $increment = $increment + 600;


                                                                    }


                                                                    ?>
                                                                </select>

                                                            </td>
                                                        </tr>
                                                    </table>

                                                    <?php

                                                    if ($_GET['global_admin'] && $verified == 'yes')
                                                    {
                                                        echo "<input type=\"hidden\" name=\"global_admin\" value=\"{$_GET['global_admin']}\">";
                                                    }

                                                    if ($_GET['client'])
                                                    {
                                                        echo "<input type=\"hidden\" name=\"client\" value=\"{$_GET['client']}\">";
                                                    }

                                                    ?>

                                                    <div align="center" style="padding-top:10px;">
                                                        <input type="submit" value="View Time Lapse" class="button">
                                                    </div>
                                                    <br>
                                                </td>
                                            </form>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?c=$cache$global_admin_additional{$client_additional}"; ?>" class="link">Realtime Image</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?last2hours=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Last 2 Hrs</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?today=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Today</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?yesterday=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Yesterday</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?last7days=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Last 7 Days</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?last30days=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Last 30 Days</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="border-bottom: 1px solid #9399a9;height:30%;background-color:#d9e0f0;">

                                                <div class="link" align="center">
                                                    <a href="<?php echo "{$basedir_www}?project=on&c={$cache}$global_admin_additional{$client_additional}"; ?>" class="link">Entire Project</a>
                                                </div>

                                            </td>
                                        </tr>
                                        <tr>
                                            <td class="gradient" style="height:30%;background-color:#d9e0f0;">
                                                <div align="center" style="padding:15px;font-size:80%;">
                                                    <?php

                                                    $stop_exec = microtime();

                                                    echo "Page created in ".round(($stop_exec - $start_exec), 2)." seconds";


                                                    if ($ip_address == $currentCaboolture || $ip_address == $currentWarwick || $ip_address == $currentGayndah || $ip_address == $currentRoaming) {
                                                        echo 'here';
                                                        echo "<br><br><a href=\"http://54.145.221.201/video-creation/create.php?bucketName={$cameraName}&ImageList=";

                                                        for ($a = 0; $a < sizeof($image_list); $a++)
                                                        {
                                                            if ($archivedCamera) { $cameraName = 'tlc-www-archive'; } else { $cameraName = $getYear[0]; }

                                                            echo "{$image_list[$a]}";
                                                            if ($a !== sizeof($image_list)-1) {
                                                                echo ",";
                                                            }
                                                        }

                                                        echo "\">Create Video</a><br><br>";

                                                        $cameraIP = @file_get_contents("/home/subdomains/livetimelapse/public_html/www/{$cam}/ip.txt");
                                                        echo '
  <input type="text" id="website" value="'.$cameraIP.'" style="display: inline-block; width: 100px; margin-top: 10px;">
<button data-copytarget="#website" style="display: inline-block;margin-top: 10px;">copy</button>
<div style="margin-top: 10px;">'.date ("F d Y h:iA.", filemtime("/home/subdomains/livetimelapse/public_html/www/{$cam}/ip.txt")).'</div>
<br>
<a href="http://'.$cameraIP.'" style="font-size: 150%" target="blank">Router</a> | <a href="ssh://root@'.$cameraIP.'" style="font-size: 150%">SSH</a>
  
  <SCRIPT LANGUAGE="JavaScript">

(function() {

  // click events 
  document.body.addEventListener("click", copy, true);

  // event handler
  function copy(e) {

    // find target element
    var
      t = e.target,
      c = t.dataset.copytarget,
      inp = (c ? document.querySelector(c) : null);

    // is element selectable?
    if (inp && inp.select) {

      // select text
      inp.select();

      try {
        // copy text
        document.execCommand("copy");
        inp.blur();
      }
      catch (err) {
        alert("please press Ctrl/Cmd+C to copy");
      }

    }

  }

})();

</SCRIPT>
  ';
                                                    }
                                                    ?>
                                                </div>
                                            </td>
                                        </tr>
                                    </table>

                                </td>

                                <?php

                                $cache = rand(0,20000);

                                if ($animation == 'yes' && $total_images == 0)
                                {
                                    echo "<td style=\"\" align=\"center\" valign=\"middle\"><font style=\"font-size:150%;\">Sorry, there are no images for the time period you've selected.</font></td>";
                                }
                                elseif ($animation == 'yes' && $error != 'yes')
                                {
                                    echo "

            <td style=\"background-color:#dbdfe5;\" align=\"center\">
            
            <div style=\"padding:3px 0px 3px 0px;\">
            <div id=\"progress_bar_container\" align=\"center\">
              <div id=\"progress_wrapper\">
                <div id=\"progress_text\" class=\"progress_text\">&nbsp;</div>
                <div id=\"progress_bar\" class=\"progress_bar\">&nbsp;</div>
              </div>
            </div>  
            
            <input type=\"hidden\" id=\"finished_text\" value=\"Displaying <b>$total_images</b> images between $animation_start_screen and $animation_stop_screen\">
            <ilayer name=\"animationlayer\" left=\"1\" top=\"1\" id=\"animationlayer\">
            

                                        <div style='position: relative; display: inline-block;'>
  <img
        src='https://www.livetimelapse.com.au/images/bigtransparent.gif'
        name=animation
        id='timelapseimage'
       style='display: block; width: 980px; height: auto;'>
  <img src='../om-civil-logo.png' 
       alt='Logo' 
       style='position: absolute; bottom: 20px; right: 0px; width: 174px; opacity: 0.8;'>
</div>


            <img style=\"border: solid 1px #C1C1C1;\" src=\"https://www.livetimelapse.com.au/images/bigtransparent.gif\" name=animation id=\"timelapseimage\" border=\"0\">
            </ilayer>
            </div>
           
            ";

                                    // End internal IP matched display
                                    if ($ip_address == $currentCaboolture || $ip_address == $currentWarwick || $ip_address == $currentGayndah || $ip_address == $currentRoaming) {


                                        echo '
                    <br>
                    <div>
                      <a style="float: left;" id="deleteImage" target="_new" href="#">Delete Image</a>
                      <a style="float: right;" href="http://s3download.livetimelapse.com.au/create.zip.file.php?images='.$fileListName.'">Download ZIP</a>
                    </div>
                    ';

                                    }

                                    echo "           
            </td>
              ";
                                }
                                else
                                {
                                    if ($error != 'yes')
                                    {
                                        if ($cam == 'tlc_wilson_park_zoom') {
                                            echo "
                  <td style=\"background-color:#dbdfe5;\" align=\"center\" valign=\"top\">
                  <div style=\"padding-top:5px;padding-bottom:5px;padding-left:5px;padding-right:5px;\"><img src=\"https://legacy.livetimelapse.com.au/www/tlc_wilson_park/{$displayImageSize}.jpg?c={$cache}\"></div>
                  </td>
                  ";
                                        } else {
                                            echo "
                  <td style=\"background-color:#dbdfe5;\" align=\"center\" valign=\"top\">
                  <div style=\"padding-top:5px;padding-bottom:5px;padding-left:5px;padding-right:5px;position: relative;\">
                    ";




                                            ?>



                                            <?php

                                            //echo "<div class=\"thumbLabel\">There is currently a  network wide server outage that is effecting all clients.<br><br>We are working as quickly as possible to rectify the situtation</div>";

                                            echo "
<div style='position: relative; display: inline-block;'>
  <img id='cameraImage' 
       src='https://webcams.livetimelapse.com.au/{$cam}/{$displayImageSize}.jpg?c={$cache}' 
       style='display: block; width: 980px; height: auto;'>
  <img src='../om-civil-logo.png' 
       alt='Logo' 
       style='position: absolute; bottom: 20px; right: 0px; width: 174px; opacity: 0.8;'>
</div>";

                                            // End internal IP matched display
                                            if ($ip_address == $currentCaboolture || $ip_address == $currentWarwick || $ip_address == $currentGayndah || $ip_address == $currentRoaming) {

                                                ?>

                                                <style>

                                                    .carrier {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 10px;
                                                        left: 10px;
                                                        text-align: left;
                                                        font-weight: bold;
                                                        font-size: 160%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .mobileNumber {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 40px;
                                                        left: 14px;
                                                        text-align: left;
                                                        font-weight: bold;
                                                        font-size: 160%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .cameraType {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 40px;
                                                        right: 14px;
                                                        text-align: right;
                                                        font-weight: bold;
                                                        font-size: 160%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .cameraModes {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 10px;
                                                        right: 14px;
                                                        text-align: right;
                                                        font-weight: bold;
                                                        font-size: 160%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }


                                                    .powerOff {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 70px;
                                                        right: 14px;
                                                        text-align: right;
                                                        font-weight: bold;
                                                        font-size: 100%;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .Memcard {
                                                        position: absolute;
                                                        width: auto;
                                                        top: 90px;
                                                        right: 14px;
                                                        text-align: right;
                                                        font-weight: bold;
                                                        font-size: 100%;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .statusText {
                                                        position: absolute;
                                                        right: 14px;
                                                        bottom: 40px;
                                                        text-align: center;
                                                        font-weight: bold;
                                                        font-size: 100%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .USBStatusText {
                                                        position: absolute;
                                                        right: 14px;
                                                        bottom: 60px;
                                                        text-align: center;
                                                        font-weight: bold;
                                                        font-size: 100%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .cameraShutter {
                                                        position: absolute;
                                                        width: 100%;
                                                        bottom: 38px;
                                                        left: 10px;
                                                        text-align: left;
                                                        font-weight: bold;
                                                        font-size: 120%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }

                                                    .systemName {
                                                        position: absolute;
                                                        width: 100%;
                                                        bottom: 38px;
                                                        left: 10px;
                                                        text-align: center;
                                                        font-weight: bold;
                                                        font-size: 120%;
                                                        color: #f9ff00;
                                                        text-shadow: 1px 1px black;
                                                    }


                                                    .previousLink {
                                                        display: inline-block;
                                                        width: 100%;
                                                        bottom: 38px;
                                                        float: left;
                                                        font-weight: bold;
                                                        font-size: 120%;
                                                        color: #f9ff00;
                                                    }

                                                    .nextLink {
                                                        display: inline-block;
                                                        width: 100%;
                                                        bottom: 38px;
                                                        float: right;
                                                        font-weight: bold;
                                                        font-size: 120%;
                                                        color: #f9ff00;
                                                    }

                                                </style>

                                                <?php

                                                $masterList = unserialize(file_get_contents('/home/subdomains/livetimelapse/public_html/db/masterList.txt'));
                                                $currentURL = $_SERVER['REQUEST_URI'];

                                                for ($a = 0; $a < sizeof($masterList); $a++) { if (strstr($masterList[$a]['url'], $currentURL)) { $currentPosition = $a; } }

                                                $cameraStatsRAW[$camera] = @file_get_contents('/home/subdomains/livetimelapse/public_html/monitoring/logs/' . $cam . '/DSLRStats.txt');
                                                $cameraStats[$camera] = explode("|", $cameraStatsRAW[$camera]);

                                                for ($a = 0; $a < sizeof($cameraStats[$camera]); $a++) {
                                                    $explode = explode("-", $cameraStats[$camera][$a]);
                                                    $DSLRStats[$camera][$explode[0]] = $explode[1];
                                                }


                                                $cameraClean = str_replace("Tlc", "", ucwords(str_replace("_", " ", $camera)));
                                                $cameraShutter = round(((int) $cameraStats[$camera][3]/1000),0);

                                                $IPAddress = @file_get_contents("/home/subdomains/livetimelapse/public_html/www/{$camera}/ip.txt");

                                                if (substr($IPAddress, 0, 3) == '113.'
                                                    || substr($IPAddress, 0, 3) == '182.') {
                                                    $carrier = 'Beagle';
                                                } elseif (substr($IPAddress, 0, 3) == '161.') {
                                                    $carrier = 'Optus Static';
                                                } elseif (substr($IPAddress, 0, 3) == '124.') {
                                                    $carrier = 'Optus Static DS';
                                                } elseif (substr($IPAddress, 0, 3) == '58.') {
                                                    $carrier = 'Optus Static DS';
                                                } elseif (substr($IPAddress, 0, 3) == '59.') {
                                                    $carrier = 'Optus Static DS';
                                                } elseif (substr($IPAddress, 0, 4) == '1.44' || substr($IPAddress, 0, 3) == '119' || substr($IPAddress, 0, 3) == '49.' || substr($IPAddress, 0, 3) == '211.') {
                                                    $carrier = 'Optus Dynamic';
                                                } elseif (substr($IPAddress, 0, 3) == '120.'
                                                    || substr($IPAddress, 0, 3) == '123.') {
                                                    $carrier = 'Telstra';
                                                } elseif (substr($IPAddress, 0, 3) == '121.' || substr($IPAddress, 0, 3) == '118.') {
                                                    $carrier = 'Internode';
                                                } else {
                                                    $carrier = $IPAddress;
                                                }

                                                $mobileNumber = file_get_contents("/home/subdomains/livetimelapse/public_html/monitoring/logs/". $cam ."/mobileNumber.txt");
                                                $mobileNumberDate = filemtime("/home/subdomains/livetimelapse/public_html/monitoring/logs/". $cam ."/mobileNumber.txt");

                                                if ($DSLRStats[$camera]['Auto Power Off'] == "0") { $DSLRStats[$camera]['Auto Power Off'] = "APO Disabled"; $APOTextColour = '#73e873'; } else { $DSLRStats[$camera]['Auto Power Off'] = "APO NOT SET"; $APOTextColour = 'red'; }
                                                if ($DSLRStats[$camera]['Available Shots'] > "1") { $DSLRStats[$camera]['Available Shots'] = "Mem Card"; $MemcardTextColour = '#73e873'; } else { $DSLRStats[$camera]['Available Shots'] = "NO CARD"; $MemcardTextColour = 'red'; }

                                                $statusAge = round((time() - (@filemtime("webcams{$cam}/status.txt"))) / 60);
                                                if ($statusAge > 5) { $statusText = "Offline"; $statusTextColour = '#ff8787'; $offlineCount++; } else { $statusText = "Online"; $statusTextColour = '#73e873'; $onlineCount++; }

                                                $USBStatus = @file_get_contents("/home/subdomains/livetimelapse/public_html/monitoring/logs/{$cam}/DSLRStatus.txt");
                                                if ($USBStatus == "CONNECTED") { $USBStatusText = "Cam USB"; $USBStatusTextColour = '#73e873'; } else { $USBStatusText = "NO CAM"; $USBStatusTextColour = 'red'; }

                                                $cameraShutter = round(($DSLRStats[$camera]['Shutter Counter']/1000),0);
                                                if ($cameraShutter < 50) { $cameraShutterStyle = 'cameraShutterGreen'; }
                                                else if ($cameraShutter > 50 && $cameraShutter < 99) { $cameraShutterStyle = 'cameraShutterOrange'; }
                                                else if ($cameraShutter > 99) { $cameraShutterStyle = 'cameraShutterRed'; }



                                                echo "
                    <div id=\"exifData\">
                    
                    </div>
                    <div id=\"cameraInfo\">
                    <div class=\"carrier\">$carrier</div>
                    <div class=\"mobileNumber\" style=\"color: {$mobileNumberDateCSS}\">{$mobileNumber}</div>
                    <div class=\"cameraModes\">{$DSLRStats[$camera]['Canon Auto Exposure Mode']} Mode, {$DSLRStats[$camera]['Focus Mode']} Focus</div>
                    <div class=\"cameraType\">{$DSLRStats[$camera]['Camera Model']}</div>
                    <div class=\"powerOff\" style=\"color: $APOTextColour\">{$DSLRStats[$camera]['Auto Power Off']}</div>
                    <div class=\"memCard\" style=\"color: $MemcardTextColour\">{$DSLRStats[$camera]['Available Shots']}</div>
                    <div class=\"USBStatusText\" style=\"color: {$USBStatusTextColour}\">".$USBStatusText."</div>
                    <div class=\"statusText\" style=\"color: {$statusTextColour}\">".$statusText."</div>
                    <div class=\"cameraShutter {$cameraShutterStyle}\">{$cameraShutter}k</div>
                    <div class=\"systemName\">{$cam}</div>
                      
                    </div>
                    <script>
                    document.getElementById(\"cameraImage\").onload = function() {
                    EXIF.getData(this, function() {
                      var Model = EXIF.getTag(this, \"Model\");
                      var ISO = EXIF.getTag(this, \"ISOSpeedRatings\");
                      var FNumber = EXIF.getTag(this, \"FNumber\");
                      var ExposureProgram = EXIF.getTag(this, \"ExposureProgram\");
                      var FocalLength = EXIF.getTag(this, \"FocalLength\");
                      
                      if (ExposureProgram == 'Aperture priority') { ExposureProgram = 'AV'; }
                      
                      //console.log(EXIF.getAllTags(this));
                      
                      //document.getElementById(\"exifData\").innerHTML = Model + ' : F' + FNumber.numerator + ' @ ' + FocalLength + 'mm ' + ExposureProgram + ' (ISO ' + ISO + ')';
                      //document.getElementById(\"cameraInfo\").innerHTML = '<span class=\"infoHeader\">System Name</span> <span class=\"infoText\">{$cameraStats[$camera][0]}</span>' + 
                      //'<span class=\"infoHeader\">Camera Model</span> <span class=\"infoText\">".trim($cameraStats[$camera][1])."</span>' +
                      //'<span class=\"infoHeader\">Camera Serial</span> <span class=\"infoText\">".trim($cameraStats[$camera][2])."</span>' +
                      //'<span class=\"infoHeader\">Camera Time</span> <span class=\"infoText\">".trim($cameraStats[$camera][4])."</span>' +
                      //'<span class=\"infoHeader\">Camera Shutter</span> <span class=\"infoText\">".trim($cameraStats[$camera][3])."</span>' +
                      //'<span class=\"infoHeader\">Camera Lens</span> <span class=\"infoText\">".trim($cameraStats[$camera][5])."</span>' +
                      //'<span class=\"infoHeader\">Capturing To</span> <span class=\"infoText\">".trim($cameraStats[$camera][6])."</span>';
                                   
                      });
                    }
                    </script>
                    ";

                                                echo "
                    </div>
                    <br>
                    <!--<span class=\"previousLink\"><a href=\"{$masterList[$currentPosition-1]['url']}\"><< Previous Camera ({$masterList[$currentPosition-1]['camera']})</a></span>-->
                    <br><br>
                    <!--<span class=\"nextLink\"><a href=\"{$masterList[$currentPosition+1]['url']}\">Next Camera ({$masterList[$currentPosition+1]['camera']})>></a></span>-->
                    ";

                                            } // End internal IP matched display

                                            echo "
                  </td>
                  ";

                                        }
                                    }
                                    else
                                    {
                                        echo "<td style=\"background-color:#dbdfe5;\" align=\"center\" valign=\"middle\"><font style=\"font-size:150%;\">Sorry, you have chosen an invalid start and stop time.</font></td>";
                                    }
                                }

                                ?>

                    </td>
                </tr>
            </table>

        </td>
    </tr>
</table>

</td>
</tr>
</table>

<br>
<br>
<br>

<form method="post" name="control_form">
    <input type="hidden" name="frame_nr" size="2" readonly>
</form>

<div align="center" id="fullsize" style="visibility:hidden;">
    <a href="" style="color:black;"></a>
</div>

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-50430379-1', 'livetimelapse.com.au');
    ga('send', 'pageview');

</script>

</body>
</html>
