aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <silvio@socioambiental.org>2012-11-28 17:52:51 -0200
committerSilvio <silvio@socioambiental.org>2012-11-28 17:52:51 -0200
commit035e9195be502b917118ca70651232c38fbee249 (patch)
treecad5bb01fffd64093edca1de86b8f6b121ed1cc4
parent0e95562f4774b2a5a1bb5ad290f5b05cc1a0be2a (diff)
downloadgmap_arcgis-035e9195be502b917118ca70651232c38fbee249.tar.gz
gmap_arcgis-035e9195be502b917118ca70651232c38fbee249.tar.bz2
Adding gmap_arcgis_mapserver() proxy cache
-rw-r--r--gmap_arcgis.module68
1 files changed, 68 insertions, 0 deletions
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;
+}