From 6788efa5cef51f7e922e11e3509ad6681e4558ac Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 2 Sep 2008 01:22:57 +0000 Subject: [PATCH] Use GeoIP-based mirror selection. --- www/phplib/mirrors.php | 64 ++++++++++++++++++++++++++++++++++++++++++ www/software.php | 44 ++++++++--------------------- 2 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 www/phplib/mirrors.php diff --git a/www/phplib/mirrors.php b/www/phplib/mirrors.php new file mode 100644 index 0000000..25817aa --- /dev/null +++ b/www/phplib/mirrors.php @@ -0,0 +1,64 @@ + array("California, USA", 37.7898, -122.3942), + "http://ftp2.easysw.com/pub" => array("New Jersey, USA", 40.4619, -74.3561), + "http://ftp.funet.fi/pub/mirrors/ftp.easysw.com/pub" => array("Espoo, Finland", 60.2167, 24.6667), + "http://ftp.rz.tu-bs.de/pub/mirror/ftp.easysw.com/ftp/pub" => array("Braunschweig, Germany", 52.2667, 10.5333), +); + + +// +// 'mirror_closest()' - Return the closest mirror to the current IP. +// + +function // O - Closest mirror +mirror_closest() +{ + global $_SERVER, $MIRRORS; + + + // Get the current longitude for the client... + $current = geoip_record_by_name($_SERVER["REMOTE_ADDR"]); + $lon = $current["longitude"]; + + // Loop through the mirrors to find the closest one, currently just using + // the longitude... + $closest_mirror = ""; + $closest_distance = 999; + + reset($MIRRORS); + foreach ($MIRRORS as $mirror => $data) + { + $distance = abs($lon - $data[2]); + if ($distance > 180) + $distance = 360 - $distance; + + if ($distance < $closest_distance) + { + $closest_mirror = $mirror; + $closest_distance = $distance; + } + } + + return ($closest_mirror); +} + + +// +// End of "$Id$". +// +?> diff --git a/www/software.php b/www/software.php index f5fab38..577242b 100644 --- a/www/software.php +++ b/www/software.php @@ -10,16 +10,7 @@ // include_once "phplib/html.php"; - - -// List of download servers... -$sitelist = array( - "ftp://ftp.easysw.com/pub" => "California, USA via FTP", - "http://ftp.easysw.com/pub" => "California, USA via HTTP", - "http://ftp.funet.fi/pub/mirrors/ftp.easysw.com/pub" => "Espoo, Finland via HTTP", - "ftp://ftp.funet.fi/pub/mirrors/ftp.easysw.com/pub" => "Espoo, Finland via FTP", -); - +include_once "phplib/mirrors.php"; // Get the list of software files... $fp = fopen("data/software.md5", "r"); @@ -42,10 +33,10 @@ if (array_key_exists("SITE", $_GET)) setcookie("SITE", $site, time() + 90 * 86400, "/"); } else if (array_key_exists("SITE", $_COOKIE) && - array_key_exists($_COOKIE["SITE"], $sitelist)) + array_key_exists($_COOKIE["SITE"], $MIRRORS)) $site = $_COOKIE["SITE"]; else - $site = ""; + $site = mirror_closest(); if (array_key_exists("VERSION", $_GET)) $version = $_GET["VERSION"]; @@ -86,38 +77,27 @@ if ($file != "") { print("

Download

\n"); - if ($site != "") - print("

Your download should begin shortly. If not, please " - ."click here to download the file " - ."from the current mirror.

\n"); - else - print("

Please select a mirror site below to begin the download.

\n"); - - print("
\n" + print("

Your download should begin shortly. If not, please " + ."click here to download the file " + ."from the current mirror.

\n" + ."\n" ."\n" ."\n"); - if ($site == "") - print("None
\n"); - - reset($sitelist); - while (list($key, $val) = each($sitelist)) + reset($MIRRORS); + while (list($key, $val) = each($MIRRORS)) { print("$val
\n"); + print(">$val[0]
\n"); } - if ($site != "") - print("

\n"); - else - print("

\n"); - - print("

\n"); + print("

\n" + ."\n"); } else {