jani                                     Sat, 05 Sep 2009 17:07:14 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=288081

Log:
- Fixed bug #49182 (PHP CGI always outputs the shebang line)

Bug: http://bugs.php.net/49182 (Assigned) PHP CGI always outputs the shebang 
line
      
Changed paths:
    U   php/php-src/branches/PHP_5_2/Zend/zend_object_handlers.c
    U   php/php-src/branches/PHP_5_3/NEWS
    U   php/php-src/branches/PHP_5_3/main/fopen_wrappers.c
    U   php/php-src/branches/PHP_5_3/sapi/cgi/cgi_main.c
    U   php/php-src/trunk/main/fopen_wrappers.c
    U   php/php-src/trunk/sapi/cgi/cgi_main.c

Modified: php/php-src/branches/PHP_5_2/Zend/zend_object_handlers.c
===================================================================
--- php/php-src/branches/PHP_5_2/Zend/zend_object_handlers.c	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/branches/PHP_5_2/Zend/zend_object_handlers.c	2009-09-05 17:07:14 UTC (rev 288081)
@@ -569,7 +569,7 @@
 			/* we don't have access controls - will just add it */
 			new_zval = &EG(uninitialized_zval);

-/* 			zend_error(E_NOTICE, "Undefined property: %s", Z_STRVAL_P(member)); */
+			zend_error(E_NOTICE, "Undefined property: %s", Z_STRVAL_P(member));
 			new_zval->refcount++;
 			zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void **) &retval);
 		} else {

Modified: php/php-src/branches/PHP_5_3/NEWS
===================================================================
--- php/php-src/branches/PHP_5_3/NEWS	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/branches/PHP_5_3/NEWS	2009-09-05 17:07:14 UTC (rev 288081)
@@ -2,6 +2,10 @@
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 2009, PHP 5.3.2

+?? ??? 2009, PHP 5.3.1RC?
+- Restored shebang line check to CGI sapi (not checked by scanner anymore).
+  (Jani)
+
 ?? ??? 2009, PHP 5.3.1
 - Upgraded bundled sqlite to version 3.6.17. (Scott)


Modified: php/php-src/branches/PHP_5_3/main/fopen_wrappers.c
===================================================================
--- php/php-src/branches/PHP_5_3/main/fopen_wrappers.c	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/branches/PHP_5_3/main/fopen_wrappers.c	2009-09-05 17:07:14 UTC (rev 288081)
@@ -382,9 +382,12 @@
  */
 PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC)
 {
+	FILE *fp;
+#ifndef PHP_WIN32
+	struct stat st;
+#endif
 	char *path_info, *filename;
 	int length;
-	zend_bool orig_display_errors;

 	filename = SG(request_info).path_translated;
 	path_info = SG(request_info).request_uri;
@@ -451,7 +454,7 @@
 		}
 	} /* if doc_root && path_info */

-	if(filename) {
+	if (filename) {
 		filename = zend_resolve_path(filename, strlen(filename) TSRMLS_CC);
 	}

@@ -463,21 +466,32 @@
 		STR_FREE(SG(request_info).path_translated);
 		SG(request_info).path_translated = NULL;
 		return FAILURE;
-	} else {
-		STR_FREE(SG(request_info).path_translated);
-		SG(request_info).path_translated = filename;
 	}
+	fp = VCWD_FOPEN(filename, "rb");

-	orig_display_errors = PG(display_errors);
-	PG(display_errors) = 0;
-	if (zend_stream_open(filename, file_handle TSRMLS_CC) == FAILURE) {
-		PG(display_errors) = orig_display_errors;
+#ifndef PHP_WIN32
+	/* refuse to open anything that is not a regular file */
+	if (fp && (0 > fstat(fileno(fp), &st) || !S_ISREG(st.st_mode))) {
+		fclose(fp);
+		fp = NULL;
+	}
+#endif
+
+	if (!fp) {
 		STR_FREE(SG(request_info).path_translated);	/* for same reason as above */
 		SG(request_info).path_translated = NULL;
 		return FAILURE;
 	}
-	PG(display_errors) = orig_display_errors;

+	file_handle->opened_path = expand_filepath(filename, NULL TSRMLS_CC);
+
+	SG(request_info).path_translated = filename;
+
+	file_handle->filename = SG(request_info).path_translated;
+	file_handle->free_filename = 0;
+	file_handle->handle.fp = fp;
+	file_handle->type = ZEND_HANDLE_FP;
+
 	return SUCCESS;
 }
 /* }}} */

Modified: php/php-src/branches/PHP_5_3/sapi/cgi/cgi_main.c
===================================================================
--- php/php-src/branches/PHP_5_3/sapi/cgi/cgi_main.c	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/branches/PHP_5_3/sapi/cgi/cgi_main.c	2009-09-05 17:07:14 UTC (rev 288081)
@@ -159,6 +159,7 @@
 typedef struct _php_cgi_globals_struct {
 	zend_bool rfc2616_headers;
 	zend_bool nph;
+	zend_bool check_shebang_line;
 	zend_bool fix_pathinfo;
 	zend_bool force_redirect;
 	zend_bool discard_path;
@@ -1369,6 +1370,7 @@
 PHP_INI_BEGIN()
 	STD_PHP_INI_ENTRY("cgi.rfc2616_headers",     "0",  PHP_INI_ALL,    OnUpdateBool,   rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.nph",                 "0",  PHP_INI_ALL,    OnUpdateBool,   nph, php_cgi_globals_struct, php_cgi_globals)
+	STD_PHP_INI_ENTRY("cgi.check_shebang_line",  "1",  PHP_INI_SYSTEM, OnUpdateBool,   check_shebang_line, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.force_redirect",      "1",  PHP_INI_SYSTEM, OnUpdateBool,   force_redirect, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.fix_pathinfo",        "1",  PHP_INI_SYSTEM, OnUpdateBool,   fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
@@ -1385,6 +1387,7 @@
 {
 	php_cgi_globals->rfc2616_headers = 0;
 	php_cgi_globals->nph = 0;
+	php_cgi_globals->check_shebang_line = 1;
 	php_cgi_globals->force_redirect = 1;
 	php_cgi_globals->redirect_status_env = NULL;
 	php_cgi_globals->fix_pathinfo = 1;
@@ -1451,6 +1454,7 @@
 	int exit_status = SUCCESS;
 	int cgi = 0, c, i, len;
 	zend_file_handle file_handle;
+	int retval = FAILURE;
 	char *s;

 	/* temporary locals */
@@ -2058,6 +2062,26 @@
 				}
 			}

+			if (CGIG(check_shebang_line) && file_handle.handle.fp && (file_handle.handle.fp != stdin)) {
+				/* #!php support */
+				c = fgetc(file_handle.handle.fp);
+				if (c == '#') {
+					while (c != '\n' && c != '\r' && c != EOF) {
+						c = fgetc(file_handle.handle.fp);	/* skip to end of line */
+					}
+					/* handle situations where line is terminated by \r\n */
+					if (c == '\r') {
+						if (fgetc(file_handle.handle.fp) != '\n') {
+							long pos = ftell(file_handle.handle.fp);
+							fseek(file_handle.handle.fp, pos - 1, SEEK_SET);
+						}
+					}
+					CG(start_lineno) = 2;
+				} else {
+					rewind(file_handle.handle.fp);
+				}
+			}
+
 			switch (behavior) {
 				case PHP_MODE_STANDARD:
 					php_execute_script(&file_handle TSRMLS_CC);

Modified: php/php-src/trunk/main/fopen_wrappers.c
===================================================================
--- php/php-src/trunk/main/fopen_wrappers.c	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/trunk/main/fopen_wrappers.c	2009-09-05 17:07:14 UTC (rev 288081)
@@ -334,9 +334,12 @@
  */
 PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC)
 {
+	FILE *fp;
+#ifndef PHP_WIN32
+	struct stat st;
+#endif
 	char *path_info, *filename;
 	int length;
-	zend_bool orig_display_errors;

 	filename = SG(request_info).path_translated;
 	path_info = SG(request_info).request_uri;
@@ -403,7 +406,7 @@
 		}
 	} /* if doc_root && path_info */

-	if(filename) {
+	if (filename) {
 		filename = zend_resolve_path(filename, strlen(filename) TSRMLS_CC);
 	}

@@ -415,21 +418,32 @@
 		STR_FREE(SG(request_info).path_translated);
 		SG(request_info).path_translated = NULL;
 		return FAILURE;
-	} else {
-		STR_FREE(SG(request_info).path_translated);
-		SG(request_info).path_translated = filename;
 	}
+	fp = VCWD_FOPEN(filename, "rb");

-	orig_display_errors = PG(display_errors);
-	PG(display_errors) = 0;
-	if (zend_stream_open(filename, file_handle TSRMLS_CC) == FAILURE) {
-		PG(display_errors) = orig_display_errors;
+#ifndef PHP_WIN32
+	/* refuse to open anything that is not a regular file */
+	if (fp && (0 > fstat(fileno(fp), &st) || !S_ISREG(st.st_mode))) {
+		fclose(fp);
+		fp = NULL;
+	}
+#endif
+
+	if (!fp) {
 		STR_FREE(SG(request_info).path_translated);	/* for same reason as above */
 		SG(request_info).path_translated = NULL;
 		return FAILURE;
 	}
-	PG(display_errors) = orig_display_errors;

+	file_handle->opened_path = expand_filepath(filename, NULL TSRMLS_CC);
+
+	SG(request_info).path_translated = filename;
+
+	file_handle->filename = SG(request_info).path_translated;
+	file_handle->free_filename = 0;
+	file_handle->handle.fp = fp;
+	file_handle->type = ZEND_HANDLE_FP;
+
 	return SUCCESS;
 }
 /* }}} */

Modified: php/php-src/trunk/sapi/cgi/cgi_main.c
===================================================================
--- php/php-src/trunk/sapi/cgi/cgi_main.c	2009-09-05 16:50:38 UTC (rev 288080)
+++ php/php-src/trunk/sapi/cgi/cgi_main.c	2009-09-05 17:07:14 UTC (rev 288081)
@@ -159,6 +159,7 @@
 typedef struct _php_cgi_globals_struct {
 	zend_bool rfc2616_headers;
 	zend_bool nph;
+	zend_bool check_shebang_line;
 	zend_bool fix_pathinfo;
 	zend_bool force_redirect;
 	zend_bool discard_path;
@@ -1366,6 +1367,7 @@
 PHP_INI_BEGIN()
 	STD_PHP_INI_ENTRY("cgi.rfc2616_headers",     "0",  PHP_INI_ALL,    OnUpdateBool,   rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.nph",                 "0",  PHP_INI_ALL,    OnUpdateBool,   nph, php_cgi_globals_struct, php_cgi_globals)
+	STD_PHP_INI_ENTRY("cgi.check_shebang_line",  "1",  PHP_INI_SYSTEM, OnUpdateBool,   check_shebang_line, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.force_redirect",      "1",  PHP_INI_SYSTEM, OnUpdateBool,   force_redirect, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.redirect_status_env", NULL, PHP_INI_SYSTEM, OnUpdateString, redirect_status_env, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_ENTRY("cgi.fix_pathinfo",        "1",  PHP_INI_SYSTEM, OnUpdateBool,   fix_pathinfo, php_cgi_globals_struct, php_cgi_globals)
@@ -1382,6 +1384,7 @@
 {
 	php_cgi_globals->rfc2616_headers = 0;
 	php_cgi_globals->nph = 0;
+	php_cgi_globals->check_shebang_line = 1;
 	php_cgi_globals->force_redirect = 1;
 	php_cgi_globals->redirect_status_env = NULL;
 	php_cgi_globals->fix_pathinfo = 1;
@@ -1448,6 +1451,7 @@
 	int exit_status = SUCCESS;
 	int cgi = 0, c, i, len;
 	zend_file_handle file_handle;
+	int retval = FAILURE;
 	char *s;

 	/* temporary locals */
@@ -2051,6 +2055,26 @@
 				}
 			}

+			if (CGIG(check_shebang_line) && file_handle.handle.fp && (file_handle.handle.fp != stdin)) {
+				/* #!php support */
+				c = fgetc(file_handle.handle.fp);
+				if (c == '#') {
+					while (c != '\n' && c != '\r' && c != EOF) {
+						c = fgetc(file_handle.handle.fp);	/* skip to end of line */
+					}
+					/* handle situations where line is terminated by \r\n */
+					if (c == '\r') {
+						if (fgetc(file_handle.handle.fp) != '\n') {
+							long pos = ftell(file_handle.handle.fp);
+							fseek(file_handle.handle.fp, pos - 1, SEEK_SET);
+						}
+					}
+					CG(start_lineno) = 2;
+				} else {
+					rewind(file_handle.handle.fp);
+				}
+			}
+
 			switch (behavior) {
 				case PHP_MODE_STANDARD:
 					php_execute_script(&file_handle TSRMLS_CC);
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to