php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #50563
Patch parse-url-bitfields revision 2010-05-24 13:38 UTC by kalle@php.net
Patch fix-parse_url-warning-against-5_3 revision 2010-05-21 16:10 UTC by ralph at smashlabs dot com
Patch fix-parse_url-warning-against-trunk revision 2010-05-21 16:10 UTC by ralph at smashlabs dot com

Patch parse-url-bitfields for *General Issues Bug #50563

Patch version 2010-05-24 13:38 UTC

Return to Bug #50563 | Download this patch
Patch Revisions:

Developer: kalle@php.net

Index: basic_functions.c
===================================================================
--- basic_functions.c	(revision 299637)
+++ basic_functions.c	(working copy)
@@ -3522,6 +3522,7 @@
 	REGISTER_LONG_CONSTANT("PHP_URL_PATH", PHP_URL_PATH, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("PHP_URL_QUERY", PHP_URL_QUERY, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("PHP_URL_FRAGMENT", PHP_URL_FRAGMENT, CONST_CS | CONST_PERSISTENT);
+	REGISTER_LONG_CONSTANT("PHP_URL_SILENT", PHP_URL_SILENT, CONST_CS | CONST_PERSISTENT);
 
 #define REGISTER_MATH_CONSTANT(x)  REGISTER_DOUBLE_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
 	REGISTER_MATH_CONSTANT(M_E);
Index: url.c
===================================================================
--- url.c	(revision 299637)
+++ url.c	(working copy)
@@ -347,7 +347,7 @@
 	char *str;
 	int str_len;
 	php_url *resource;
-	long key = -1;
+	long key = 0;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &key) == FAILURE) {
 		return;
@@ -355,39 +355,81 @@
 
 	resource = php_url_parse_ex(str, str_len);
 	if (resource == NULL) {
-		php_error_docref1(NULL TSRMLS_CC, str, E_WARNING, "Unable to parse URL");
+		if ((key & PHP_URL_SILENT) == 0) {
+			php_error_docref1(NULL TSRMLS_CC, str, E_WARNING, "Unable to parse URL");
+		}
 		RETURN_FALSE;
 	}
 
-	if (key > -1) {
-		switch (key) {
-			case PHP_URL_SCHEME:
-				if (resource->scheme != NULL) RETVAL_STRING(resource->scheme, 1);
-				break;
-			case PHP_URL_HOST:
-				if (resource->host != NULL) RETVAL_STRING(resource->host, 1);
-				break;
-			case PHP_URL_PORT:
-				if (resource->port != 0) RETVAL_LONG(resource->port);
-				break;
-			case PHP_URL_USER:
-				if (resource->user != NULL) RETVAL_STRING(resource->user, 1);
-				break;
-			case PHP_URL_PASS:
-				if (resource->pass != NULL) RETVAL_STRING(resource->pass, 1);
-				break;
-			case PHP_URL_PATH:
-				if (resource->path != NULL) RETVAL_STRING(resource->path, 1);
-				break;
-			case PHP_URL_QUERY:
-				if (resource->query != NULL) RETVAL_STRING(resource->query, 1);
-				break;
-			case PHP_URL_FRAGMENT:
-				if (resource->fragment != NULL) RETVAL_STRING(resource->fragment, 1);
-				break;
-			default:
-				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid URL component identifier %ld", key);
-				RETVAL_FALSE;
+	key = ~PHP_URL_SILENT;
+
+	if (key > 0) {
+		if (
+			key != PHP_URL_SCHEME && 
+			key != PHP_URL_HOST && 
+			key != PHP_URL_PORT && 
+			key != PHP_URL_USER && 
+			key != PHP_URL_PASS && 
+			key != PHP_URL_PATH && 
+			key != PHP_URL_QUERY && 
+			key != PHP_URL_FRAGMENT
+		) {
+			array_init(return_value);
+
+			if ((key & PHP_URL_SCHEME) && resource->scheme != NULL) {
+				add_assoc_string(return_value, "scheme", resource->scheme, 1);
+			}
+			if ((key & PHP_URL_HOST) && resource->host != NULL) {
+				add_assoc_string(return_value, "host", resource->host, 1);
+			}
+			if ((key & PHP_URL_PORT) && resource->port != 0) {
+				add_assoc_long(return_value, "port", resource->port);
+			}
+			if ((key & PHP_URL_USER) && resource->user != NULL) {
+				add_assoc_string(return_value, "user", resource->user, 1);
+			}
+			if ((key & PHP_URL_PASS) && resource->pass != NULL) {
+				add_assoc_string(return_value, "pass", resource->pass, 1);
+			}
+			if ((key & PHP_URL_PATH) && resource->path != NULL) {
+				add_assoc_string(return_value, "path", resource->path, 1);
+			}
+			if ((key & PHP_URL_QUERY) && resource->query != NULL) {
+				add_assoc_string(return_value, "query", resource->query, 1);
+			}
+			if ((key & PHP_URL_FRAGMENT) && resource->fragment != NULL) {
+				add_assoc_string(return_value, "fragment", resource->fragment, 1);
+			}
+		} else {
+			switch (key) {
+				case PHP_URL_SCHEME:
+					if (resource->scheme != NULL) RETVAL_STRING(resource->scheme, 1);
+					break;
+				case PHP_URL_HOST:
+					if (resource->host != NULL) RETVAL_STRING(resource->host, 1);
+					break;
+				case PHP_URL_PORT:
+					if (resource->port != 0) RETVAL_LONG(resource->port);
+					break;
+				case PHP_URL_USER:
+					if (resource->user != NULL) RETVAL_STRING(resource->user, 1);
+					break;
+				case PHP_URL_PASS:
+					if (resource->pass != NULL) RETVAL_STRING(resource->pass, 1);
+					break;
+				case PHP_URL_PATH:
+					if (resource->path != NULL) RETVAL_STRING(resource->path, 1);
+					break;
+				case PHP_URL_QUERY:
+					if (resource->query != NULL) RETVAL_STRING(resource->query, 1);
+					break;
+				case PHP_URL_FRAGMENT:
+					if (resource->fragment != NULL) RETVAL_STRING(resource->fragment, 1);
+					break;
+				default:
+					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid URL component identifier %ld", key);
+					RETVAL_FALSE;
+			}
 		}
 		goto done;
 	}
Index: url.h
===================================================================
--- url.h	(revision 299637)
+++ url.h	(working copy)
@@ -46,14 +46,15 @@
 PHP_FUNCTION(rawurldecode);
 PHP_FUNCTION(get_headers);
 
-#define PHP_URL_SCHEME 0
-#define PHP_URL_HOST 1
-#define PHP_URL_PORT 2
-#define PHP_URL_USER 3
-#define PHP_URL_PASS 4
-#define PHP_URL_PATH 5
-#define PHP_URL_QUERY 6
-#define PHP_URL_FRAGMENT 7
+#define PHP_URL_SCHEME		1
+#define PHP_URL_HOST		2
+#define PHP_URL_PORT		4
+#define PHP_URL_USER		8
+#define PHP_URL_PASS		16
+#define PHP_URL_PATH		32
+#define PHP_URL_QUERY		64
+#define PHP_URL_FRAGMENT	128
+#define PHP_URL_SILENT		256	/* Supress parse warning */
 
 #endif /* URL_H */
 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 21:01:29 2024 UTC