Cached Utils::getIP() method

This commit is contained in:
Shoghi Cervantes 2013-05-26 16:15:30 +02:00
parent 838e7ad010
commit 6d54495402
2 changed files with 10 additions and 9 deletions

View File

@ -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");
}
}
}
}

View File

@ -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\:\.]*)</span>#', $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;
}