<?php

/** This file is from Weathermap version 0.97d */
$guest_account = true;
chdir('../../');
require_once "./include/auth.php";
// include the weathermap class so that we can get the version
require_once dirname(__FILE__) . "/lib/globals.php";
require_once dirname(__FILE__) . "/lib/cacti-plugin-user.php";
require_once dirname(__FILE__) . "/lib/cacti-plugin-common.php";
require_once dirname(__FILE__) . "/lib/WMCactiAPI.class.php";
$action = "";
if (isset($_POST['action'])) {
    $action = $_POST['action'];
} elseif (isset($_GET['action'])) {
    $action = $_GET['action'];
}
$plugin = new WeatherMapCactiUserPlugin($config, $colors, WMCactiAPI::getConfigOption("weathermap_image_format", "png"));
$plugin->dispatchRequest($action, $_REQUEST, null);
// vim:ts=4:sw=4:
function weathermap_run_maps($mydir, $map_id = -1)
{
    global $weathermap_debugging;
    global $WEATHERMAP_VERSION;
    global $weathermap_warncount;
    global $weathermap_poller_start_time;
    global $weathermap_error_suppress;
    global $weathermap_mem_highwater;
    $weathermap_mem_highwater = 0;
    // This one makes Cacti's database.php puke...
    // WMMemoryNote('weathermap_initial_memory');
    require_once "all.php";
    // and this..
    // WMMemoryNote('weathermap_loaded_memory');
    $total_warnings = 0;
    $warning_notes = "";
    $start_time = microtime(true);
    if ($weathermap_poller_start_time == 0) {
        $weathermap_poller_start_time = $start_time;
    }
    $outputDirectory = realpath($mydir . DIRECTORY_SEPARATOR . 'output');
    $configDirectory = realpath($mydir . DIRECTORY_SEPARATOR . 'configs');
    $mapCount = 0;
    // take our debugging cue from the poller - turn on Poller debugging to get weathermap debugging
    if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_DEBUG) {
        $weathermap_debugging = true;
        $mode_message = "DEBUG mode is on";
        $global_debug = true;
    } else {
        $mode_message = "Normal logging mode. Turn on DEBUG in Cacti for more information";
        $global_debug = false;
    }
    $quietLogging = intval(WMCactiAPI::getConfigOption("weathermap_quiet_logging"), 0);
    // moved this outside the module_checks, so there should always be something in the logs!
    if ($quietLogging == 0) {
        cacti_log("Weathermap {$WEATHERMAP_VERSION} starting - {$mode_message}\n", true, "WEATHERMAP");
    }
    if (!wm_module_checks()) {
        wm_warn("Required modules for PHP Weathermap {$WEATHERMAP_VERSION} were not present. Not running. [WMPOLL08]\n");
        return;
    }
    weathermap_memory_check("MEM Initial");
    // move to the weathermap folder so all those relatives paths don't *have* to be absolute
    $orig_cwd = getcwd();
    chdir($mydir);
    WMCactiAPI::setConfigOption("weathermap_last_start_time", $start_time);
    // first, see if the output directory exists and is writable
    if (weathermap_directory_writeable($outputDirectory)) {
        $mapList = weathermap_get_runlist($map_id, $quietLogging);
        wm_debug("Iterating all maps.");
        $imageFormat = strtolower(WMCactiAPI::getConfigOption("weathermap_output_format", "png"));
        $rrdtool_path = read_config_option("path_rrdtool");
        foreach ($mapList as $mapParameters) {
            $weathermap_warncount = 0;
            weathermap_run_map($mapParameters, $configDirectory, $outputDirectory, $imageFormat, $quietLogging, $global_debug, $mapCount, $rrdtool_path, $weathermap_error_suppress, $weathermap_poller_start_time, $total_warnings);
            $total_warnings += $weathermap_warncount;
            $mapCount++;
        }
        wm_debug("Iterated all {$mapCount} maps.\n");
    } else {
        wm_warn("Output directory ({$outputDirectory}) doesn't exist!. No maps created. You probably need to create that directory, and make it writable by the poller process (like you did with the RRA directory) [WMPOLL07]\n");
        $total_warnings++;
        $warning_notes .= " (Output directory problem prevents any maps running WMPOLL07)";
    }
    weathermap_memory_check("MEM Final");
    chdir($orig_cwd);
    $end_time = microtime(true);
    $duration = $end_time - $start_time;
    $stats_string = sprintf('%s: %d maps were run in %.2f seconds with %d warnings', date(DATE_RFC822), $mapCount, $duration, $total_warnings);
    if (true === function_exists("memory_get_peak_usage")) {
        $peak_memory = memory_get_peak_usage();
        WMCactiAPI::setConfigOption("weathermap_peak_memory", WMUtility::formatNumberWithMetricPrefix($peak_memory));
        $stats_string .= sprintf(" using %sbytes peak memory", $peak_memory);
    }
    if ($quietLogging == 0) {
        wm_warn("STATS: Weathermap {$WEATHERMAP_VERSION} run complete - {$stats_string}\n", true);
    }
    WMCactiAPI::setConfigOption("weathermap_last_stats", $stats_string);
    WMCactiAPI::setConfigOption("weathermap_last_finish_time", $end_time);
    WMCactiAPI::setConfigOption("weathermap_last_map_count", $mapCount);
    if (true === function_exists('memory_get_usage')) {
        WMCactiAPI::setConfigOption("weathermap_final_memory", memory_get_usage());
        WMCactiAPI::setConfigOption("weathermap_highwater_memory", $weathermap_mem_highwater);
    }
    if (true === function_exists("memory_get_peak_usage")) {
        WMCactiAPI::setConfigOption("weathermap_peak_memory", memory_get_peak_usage());
    }
}
    private function outputCycleInitialisation($showFullscreen)
    {
        $refreshTime = WMCactiAPI::getConfigOption("weathermap_cycle_refresh", 20);
        $pollerInterval = WMCactiAPI::getConfigOption("poller_interval", 300);
        ?>

        <script src='vendor/jquery/dist/jquery.min.js'></script>
        <script src='vendor/jquery-idletimer/dist/idle-timer.min.js'></script>
        <script src="cacti-resources/map-cycle.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                WMcycler.start({
                    fullscreen: <?php 
        echo $showFullscreen ? "1" : "0";
        ?>
,
                    poller_cycle: <?php 
        echo $pollerInterval * 1000;
        ?>
,
                    isFiltered: true,
                    fullScreenURL: '',
                    period: <?php 
        echo $refreshTime * 1000;
        ?>
                });
            });
        </script>
        <?php 
    }