From aebff1e38c23bf2fe217c5c9bc24c65d779efcbc Mon Sep 17 00:00:00 2001
From: Benaka Moorthi <benaka.moorthi@gmail.com>
Date: Sat, 21 Sep 2013 20:06:24 -0400
Subject: [PATCH] Remove &date= query parameter from maxmind URLs if present.

---
 plugins/UserCountry/GeoIPAutoUpdater.php | 39 +++++++++++++++++++-----
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/plugins/UserCountry/GeoIPAutoUpdater.php b/plugins/UserCountry/GeoIPAutoUpdater.php
index 5ad460d5b2..cb7deb50a7 100755
--- a/plugins/UserCountry/GeoIPAutoUpdater.php
+++ b/plugins/UserCountry/GeoIPAutoUpdater.php
@@ -105,6 +105,8 @@ class GeoIPAutoUpdater
 
         $zippedOutputPath = GeoIp::getPathForGeoIpDatabase($zippedFilename);
 
+        $url = self::removeDateFromUrl($url);
+
         // download zipped file to misc dir
         try {
             $success = Http::sendHttpRequest($url, $timeout = 3600, $userAgent = null, $zippedOutputPath);
@@ -285,12 +287,18 @@ class GeoIPAutoUpdater
      */
     public static function setUpdaterOptionsFromUrl()
     {
-        self::setUpdaterOptions(array(
-                                     'loc'    => Common::getRequestVar('loc_db', false, 'string'),
-                                     'isp'    => Common::getRequestVar('isp_db', false, 'string'),
-                                     'org'    => Common::getRequestVar('org_db', false, 'string'),
-                                     'period' => Common::getRequestVar('period', false, 'string'),
-                                ));
+        $options = array(
+            'loc'    => Common::getRequestVar('loc_db', false, 'string'),
+            'isp'    => Common::getRequestVar('isp_db', false, 'string'),
+            'org'    => Common::getRequestVar('org_db', false, 'string'),
+            'period' => Common::getRequestVar('period', false, 'string'),
+        );
+
+        foreach (self::$urlOptions as $optionKey => $optionName) {
+            $options[$optionKey] = Common::unsanitizeInputValue($options[$optionKey]); // URLs should not be sanitized
+        }
+
+        self::setUpdaterOptions($options);
     }
 
     /**
@@ -313,7 +321,10 @@ class GeoIPAutoUpdater
                 continue;
             }
 
-            Piwik_SetOption($optionName, $url = $options[$optionKey]);
+            $url = $options[$optionKey];
+            $url = self::removeDateFromUrl($url);
+
+            Piwik_SetOption($optionName, $url);
         }
 
         // set period option
@@ -590,4 +601,16 @@ class GeoIPAutoUpdater
         $timestamp = Piwik_GetOption(self::LAST_RUN_TIME_OPTION_NAME);
         return $timestamp === false ? false : Date::factory((int)$timestamp);
     }
-}
+
+    /**
+     * Removes the &date=... query parameter if present in the URL. This query parameter
+     * is in MaxMind URLs by default and will force the download of an old database.
+     * 
+     * @param string $url
+     * @return string
+     */
+    private static function removeDateFromUrl($url)
+    {
+        return preg_replace("/&date=[^&#]*/", '', $url);
+    }
+}
\ No newline at end of file
-- 
GitLab