From 6d54495402ba53026df00176ff95387f9e06a064 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 26 May 2013 16:15:30 +0200 Subject: [PATCH] Cached Utils::getIP() method --- src/API/ServerAPI.php | 5 +---- src/utils/Utils.php | 14 +++++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 88732d553..0a13d20a3 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -102,9 +102,7 @@ class ServerAPI{ console("[INFO] [UPnP] Trying to port forward..."); UPnP_PortForward($this->getProperty("server-port")); } - if(($ip = Utils::getIP()) !== false){ - console("[INFO] External IP: ".$ip); - } + if($this->getProperty("last-update") === false or ($this->getProperty("last-update") + 3600) < time()){ console("[INFO] Checking for new server version"); console("[INFO] Last check: \x1b[36m".date("Y-m-d H:i:s", $this->getProperty("last-update"))."\x1b[0m"); @@ -144,7 +142,6 @@ class ServerAPI{ console("[INFO] \x1b[36mThis is the latest STABLE version"); } } - } } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 19f0c6665..22d884043 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -31,6 +31,7 @@ define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE class Utils{ public static $online = true; + public static $ip = false; public function run(){ } @@ -39,31 +40,34 @@ class Utils{ return ((@fsockopen("google.com", 80) !== false or @fsockopen("www.linux.org", 80) !== false or @fsockopen("www.php.net", 80) !== false) ? true:false); } - public static function getIP(){ + public static function getIP($force = false){ if(Utils::$online === false){ return false; + }elseif(Utils::$ip !== false and $force !== true){ + return Utils::$ip; } $ip = trim(strip_tags(Utils::curl_get("http://checkip.dyndns.org/"))); if(preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - return $matches[1]; + Utils::$ip = $matches[1]; }else{ $ip = Utils::curl_get("http://www.checkip.org/"); if(preg_match('#">([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - return $matches[1]; + Utils::$ip = $matches[1]; }else{ $ip = Utils::curl_get("http://checkmyip.org/"); if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - return $matches[1]; + Utils::$ip = $matches[1]; }else{ $ip = trim(Utils::curl_get("http://ifconfig.me/ip")); if($ip != ""){ - return $ip; + Utils::$ip = $ip; }else{ return false; } } } } + return Utils::$ip; }