php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #68277
Patch patch-geoip-open-type revision 2014-10-23 15:22 UTC by phpbugs at joern dot heissler dot de
Patch geoip-1.1.0-city.patch revision 2014-10-23 13:08 UTC by slim at inbox dot lv
Patch patch-geoip_record_by_name revision 2014-10-23 02:47 UTC by phpbugs at joern dot heissler dot de

Patch patch-geoip-open-type for geoip Bug #68277

Patch version 2014-10-23 15:22 UTC

Return to Bug #68277 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: phpbugs@joern.heissler.de

Index: geoip.c
===================================================================
--- geoip.c	(revision 335123)
+++ geoip.c	(working copy)
@@ -156,6 +156,46 @@
 }
 /* }}} */
 
+/* {{{ geoip_open_type
+ * Checks availability of GeoIP database `type', opens it and returns the database.
+ * Returns NULL on failure.
+ * The warnings can be suppressed by setting `suppress_warning' to true.
+ */
+static GeoIP * geoip_open_type(int type, int suppress_warning)
+{
+	GeoIP * gi;
+	const char *dbname;
+
+	if (type < 0 || type >= NUM_DB_TYPES) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Database type given (%d) is out of bound [0, %d].",
+		                 type, NUM_DB_TYPES - 1);
+		return NULL;
+	}
+	
+	dbname = GeoIPDBFileName[type];
+	if (! dbname) {
+		dbname = "<unknown>";
+	}
+
+	if (! GeoIP_db_avail(type)) {
+		if (! suppress_warning) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", dbname);
+		}
+		return NULL;
+	}
+
+	gi = GeoIP_open_type(type, GEOIP_STANDARD);
+	if (! gi) {
+		if (! suppress_warning) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open database file at %s.", dbname);
+		}
+		return NULL;
+	}
+
+	return gi;
+}
+/* }}} */
+
 /* {{{ PHP_MINIT_FUNCTION
  */
 PHP_MINIT_FUNCTION(geoip)
@@ -328,22 +368,11 @@
 		return;
 	}
 
-	if (edition < 0 || edition >= NUM_DB_TYPES)
-	{
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Database type given is out of bound.");
+	gi = geoip_open_type(edition, 0);
+	if (! gi) {
 		return;
 	}
 	
-	if (GeoIP_db_avail(edition)) {
-		gi = GeoIP_open_type(edition, GEOIP_STANDARD);
-	} else {
-		if (NULL != GeoIPDBFileName[edition])
-			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_COUNTRY_EDITION]);
-		else
-			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available.");
-		return;
-	}
-	
 	db_info = GeoIP_database_info(gi);
 	GeoIP_delete(gi);
 
@@ -365,10 +394,8 @@
 			return; \
 		} \
 		\
-		if (GeoIP_db_avail(db_type)) { \
-			gi = GeoIP_open_type(db_type, GEOIP_STANDARD); \
-		} else { \
-			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[db_type]); \
+		gi = geoip_open_type(db_type, 0); \
+		if (! gi) { \
 			return; \
 		} \
 		\
@@ -397,10 +424,8 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_COUNTRY_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_COUNTRY_EDITION]);
+	gi = geoip_open_type(GEOIP_COUNTRY_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -426,10 +451,8 @@
 		return;
 	}
 	
-	if (GeoIP_db_avail(GEOIP_ORG_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_ORG_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_ORG_EDITION]);
+	gi = geoip_open_type(GEOIP_ORG_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -456,10 +479,8 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_ASNUM_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_ASNUM_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_ASNUM_EDITION]);
+	gi = geoip_open_type(GEOIP_ASNUM_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -486,10 +507,8 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_DOMAIN_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_DOMAIN_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_DOMAIN_EDITION]);
+	gi = geoip_open_type(GEOIP_DOMAIN_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -517,10 +536,8 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_NETSPEED_EDITION_REV1)) {
-		gi = GeoIP_open_type(GEOIP_NETSPEED_EDITION_REV1, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_NETSPEED_EDITION_REV1]);
+	gi = geoip_open_type(GEOIP_NETSPEED_EDITION_REV1, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -548,16 +565,14 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_CITY_EDITION_REV1) || GeoIP_db_avail(GEOIP_CITY_EDITION_REV0)) {
-		if (GeoIP_db_avail(GEOIP_CITY_EDITION_REV1)) {
-			gi = GeoIP_open_type(GEOIP_CITY_EDITION_REV1, GEOIP_STANDARD);
-		} else {
-			gi = GeoIP_open_type(GEOIP_CITY_EDITION_REV0, GEOIP_STANDARD);
-		}
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_CITY_EDITION_REV0]);
+	gi = geoip_open_type(GEOIP_CITY_EDITION_REV1, 1);
+	if (! gi) {
+		gi = geoip_open_type(GEOIP_CITY_EDITION_REV0, 0);
+	}
+	if (! gi) {
 		return;
 	}
+
 	gir = GeoIP_record_by_name(gi, hostname);
 
 	GeoIP_delete(gi);
@@ -602,10 +617,8 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_NETSPEED_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_NETSPEED_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_NETSPEED_EDITION]);
+	gi = geoip_open_type(GEOIP_NETSPEED_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
@@ -628,14 +641,11 @@
 		return;
 	}
 
-	if (GeoIP_db_avail(GEOIP_REGION_EDITION_REV0) || GeoIP_db_avail(GEOIP_REGION_EDITION_REV1)) {
-		if (GeoIP_db_avail(GEOIP_REGION_EDITION_REV1)) {
-			gi = GeoIP_open_type(GEOIP_REGION_EDITION_REV1, GEOIP_STANDARD);
-		} else {
-			gi = GeoIP_open_type(GEOIP_REGION_EDITION_REV0, GEOIP_STANDARD);
-		}
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_REGION_EDITION_REV0]);
+	gi = geoip_open_type(GEOIP_REGION_EDITION_REV1, 1);
+	if (! gi) {
+		gi = geoip_open_type(GEOIP_REGION_EDITION_REV0, 0);
+	}
+	if (! gi) {
 		return;
 	}
 
@@ -667,10 +677,8 @@
 		return;
 	}
 	
-	if (GeoIP_db_avail(GEOIP_ISP_EDITION)) {
-		gi = GeoIP_open_type(GEOIP_ISP_EDITION, GEOIP_STANDARD);
-	}   else {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Required database not available at %s.", GeoIPDBFileName[GEOIP_ISP_EDITION]);
+	gi = geoip_open_type(GEOIP_ISP_EDITION, 0);
+	if (! gi) {
 		return;
 	}
 
Index: tests/010.phpt
===================================================================
--- tests/010.phpt	(revision 335123)
+++ tests/010.phpt	(working copy)
@@ -10,7 +10,7 @@
 
 ?>
 --EXPECTF--
-Warning: geoip_database_info(): Database type given is out of bound. in %s on line %d
+Warning: geoip_database_info(): Database type given (-%d) is out of bound [0, %d]. in %s on line %d
 
-Warning: geoip_database_info(): Database type given is out of bound. in %s on line %d
+Warning: geoip_database_info(): Database type given (%d) is out of bound [0, %d]. in %s on line %d
 
Index: tests/011.phpt
===================================================================
--- tests/011.phpt	(revision 335123)
+++ tests/011.phpt	(working copy)
@@ -9,4 +9,4 @@
 
 ?>
 --EXPECTF--
-Warning: geoip_database_info(): Required database not available. in %s on line %d
+Warning: geoip_database_info(): Required database not available at %s. in %s on line %d
Index: tests/013.phpt
===================================================================
--- tests/013.phpt	(revision 335123)
+++ tests/013.phpt	(working copy)
@@ -11,5 +11,5 @@
 var_dump(geoip_time_zone_by_country_and_region('JP','01'));
 ?>
 --EXPECT--
-string(16) "America/Montreal"
+string(15) "America/Toronto"
 string(10) "Asia/Tokyo"
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 09 11:05:50 2022 UTC