From 035e9195be502b917118ca70651232c38fbee249 Mon Sep 17 00:00:00 2001 From: Silvio Date: Wed, 28 Nov 2012 17:52:51 -0200 Subject: Adding gmap_arcgis_mapserver() proxy cache --- gmap_arcgis.module | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/gmap_arcgis.module b/gmap_arcgis.module index 76b667e..3387dd7 100644 --- a/gmap_arcgis.module +++ b/gmap_arcgis.module @@ -242,3 +242,71 @@ function gmap_arcgis_link_nota_tecnica() { return l(t('Sources'), 'quem-somos', array('fragment' => 'tabset-tab-3', 'external' => TRUE)); } + +/** + * Implementation of hook_menu(). + */ +function gmap_arcgis_menu() { + $items['gmap_arcgis/mapserver'] = array( + 'title' => 'MapServer Proxy', + 'description' => 'Caches requests to a MapServer', + 'page callback' => 'gmap_arcgis_mapserver', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + + return $items; +} + +/** + * Menu callback. + * + * Caches dynamic ArcGIS requests by ignoring the timestamp pattern. + */ +function gmap_arcgis_mapserver() { + header('Content-Type: text/plain;charset=utf-8'); + + // The ArcGIS server URL. + $base = 'http://gis2.socioambiental.org:8399/arcgis/rest/services/'; + + // The dynamic pattern we want to exclude. + $pattern = '/_[0-9]*._callback/'; + + $dynamic = FALSE; + $request = $_SERVER['argv'][0]; + $query = str_replace('q=gmap_arcgis/mapserver/', '', $request); + $query = preg_replace('/&/', '?', $query, 1); + $dest = $base . $query; + $process = curl_init($dest); + + if (preg_match($pattern, $dest, $timestamp) == 1) { + $dynamic = TRUE; + $key = sha1(preg_replace($pattern, '', $dest)); + } + else { + $key = sha1($dest); + } + + $data = cache_get($key, 'cache_page'); + + if ($data == 0) { + curl_setopt($process, CURLOPT_TIMEOUT, 30); + curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE); + + $return = curl_exec($process); + curl_close($process); + + cache_set($key, $return, 'cache_page', CACHE_TEMPORARY); + } + else { + $return = $dynamic == TRUE ? preg_replace($pattern, $timestamp[0], $data->data) : $data->data; + + // Cache expiration. + if ($data->created < time() - 1800) { + cache_clear_all($key, 'cache_page'); + } + } + + echo $return; + exit; +} -- cgit v1.2.3