ます’s Blog - どうでもいい記事100選

どうでもいい記事100選

Added "max_file_uploads" INI directive in 4.4.9

大垣さんのつぶやき知ったのですが、正式にサポート終了しているとはいえ完全に根絶されているとは思えないのでパッチを作ってみました。
#include "zend_ini.h" を追加したら reference error undefined symbol が解消されるのは意味不明だったけど。。。生成された Makefile が腐ってる?
display_errorsをOnにしている環境だと同時にdisplay_startup_errorsをOnにしないと警告が出ないので注意してください。。。モロにハマった。_| ̄|○

diff -urN php-4.4.9,orig/main/main.c php-4.4.9/main/main.c
--- php-4.4.9,orig/main/main.c	2007-12-31 16:22:54.000000000 +0900
+++ php-4.4.9/main/main.c	2009-11-25 14:45:09.000000000 +0900
@@ -357,6 +357,7 @@
 	PHP_INI_ENTRY("sendmail_path",	DEFAULT_SENDMAIL_PATH,	PHP_INI_SYSTEM,		NULL)
 	PHP_INI_ENTRY("disable_functions",			"",			PHP_INI_SYSTEM,		NULL)
 	PHP_INI_ENTRY("disable_classes",			"",			PHP_INI_SYSTEM,		NULL)
+	PHP_INI_ENTRY("max_file_uploads",			"20",			PHP_INI_SYSTEM,		NULL)
 
 	STD_PHP_INI_BOOLEAN("allow_url_fopen",		"1",		PHP_INI_SYSTEM,		OnUpdateBool,			allow_url_fopen,			php_core_globals,	core_globals)
 	STD_PHP_INI_BOOLEAN("always_populate_raw_post_data",		"0",		PHP_INI_SYSTEM|PHP_INI_PERDIR,		OnUpdateBool,			always_populate_raw_post_data,			php_core_globals,	core_globals)
diff -urN php-4.4.9,orig/main/rfc1867.c php-4.4.9/main/rfc1867.c
--- php-4.4.9,orig/main/rfc1867.c	2007-12-31 16:22:55.000000000 +0900
+++ php-4.4.9/main/rfc1867.c	2009-11-25 15:43:30.000000000 +0900
@@ -28,6 +28,7 @@
 #include "php.h"
 #include "php_open_temporary_file.h"
 #include "zend_globals.h"
+#include "zend_ini.h"
 #include "php_globals.h"
 #include "php_variables.h"
 #include "rfc1867.h"
@@ -787,6 +788,12 @@
 	zval *array_ptr = (zval *) arg;
 	int fd=-1;
 	zend_llist header;
+	char *max_uploads = INI_STR("max_file_uploads");
+	int upload_cnt = 0;
+
+	if (max_uploads && *max_uploads) {
+		upload_cnt = atoi(max_uploads);
+	}
 
 	if (SG(request_info).content_length > SG(post_max_size)) {
 		sapi_module.sapi_error(E_WARNING, "POST Content-Length of %ld bytes exceeds the limit of %ld bytes", SG(request_info).content_length, SG(post_max_size));
@@ -933,6 +940,14 @@
 				continue;
 			}
 
+			/* If file_uploads=off, skip the file part */
+			if (!PG(file_uploads)) {
+				skip_upload = 1;
+			} else if (upload_cnt <= 0) {
+				skip_upload = 1;
+				sapi_module.sapi_error(E_WARNING, "Maximum number of allowable file uploads has been exceeded");
+			}
+
 			/* Return with an error if the posted data is garbled */
 			if (!param) {
 				sapi_module.sapi_error(E_WARNING, "File Upload Mime headers garbled");
@@ -970,6 +985,7 @@
 			if (!skip_upload) {
 				/* Handle file */
 				fd = php_open_temporary_fd(PG(upload_tmp_dir), "php", &temp_filename TSRMLS_CC);
+				upload_cnt--;
 				if (fd==-1) {
 					sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
 					cancel_upload = UPLOAD_ERROR_E;