/**
 * Returns legend data, querying WMS GetStyles and GetLegendGraphics
 */
function get_legend($mapname, $layername)
{
    // Check cache
    if (defined('GET_LEGEND_CACHE_EXPIRY') && GET_LEGEND_CACHE_EXPIRY) {
        // Check cache folder
        $cache_folder = defined('GET_LEGEND_CACHE_DIRECTORY') && GET_LEGEND_CACHE_DIRECTORY ? GET_LEGEND_CACHE_DIRECTORY : dirname(__FILE__) . '/legend_cache/';
        if (!is_dir($cache_folder) && !@mkdir($cache_folder)) {
            err500('Cannot create cache folder, check permissions and configuration.');
        }
        $cache_file = $cache_folder . '/' . md5($mapname . $layername);
        $filemtime = @filemtime($cache_file);
        // returns FALSE if file does not exist
        if (!$filemtime or time() - $filemtime >= GET_LEGEND_CACHE_EXPIRY) {
            file_put_contents($cache_file, serialize(build_legend($mapname, $layername)));
        }
        return unserialize(file_get_contents($cache_file));
    }
    return build_legend($mapname, $layername);
}
function get_trend_pulses()
{
    session_write_close();
    $params = array('range' => 7);
    $graph = array();
    $otx = new Otx();
    $trend = $otx->get_events_from_all_pulses($params);
    if (is_array($trend) && count($trend) > 0) {
        $legend = build_legend(7);
        foreach ($legend as $l) {
            $graph[] = array('date' => $l, 'value' => intval($trend[$l]['value']));
        }
    }
    return $graph;
}