php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch php_cli_server_add_docroot_to_include_path for Built-in web server Bug #60075

Patch version 2012-03-04 18:01 UTC

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

Developer: stuart@3ft9.com

Index: sapi/cli/php_cli_server.c
===================================================================
--- sapi/cli/php_cli_server.c	(revision 323877)
+++ sapi/cli/php_cli_server.c	(working copy)
@@ -187,6 +187,8 @@
 	int address_family;
 	char *document_root;
 	size_t document_root_len;
+	char *include_path;
+	size_t include_path_len;
 	char *router;
 	size_t router_len;
 	socklen_t socklen;
@@ -1858,6 +1860,12 @@
 		/* can't handle paths that contain nul bytes */
 		return php_cli_server_send_error_page(server, client, 400 TSRMLS_CC);
 	}
+
+	if (FAILURE == zend_alter_ini_entry_ex("include_path", strlen("include_path") + 1, server->include_path, server->include_path_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC)) {
+		php_cli_server_logf("%s Failed to set include_path (%s)" TSRMLS_CC, client->addr_str, server->include_path);
+		return FAILURE;
+	}
+
 	{
 		zend_file_handle zfd;
 		zfd.type = ZEND_HANDLE_FILENAME;
@@ -1978,17 +1986,21 @@
 		zfd.free_filename = 0;
 		zfd.opened_path = NULL;
 
-		zend_try {
-			zval *retval = NULL;
-			if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, &retval, 1, &zfd)) {
-				if (retval) {
-					decline = Z_TYPE_P(retval) == IS_BOOL && !Z_LVAL_P(retval);
-					zval_ptr_dtor(&retval);
+		if (FAILURE == zend_alter_ini_entry_ex("include_path", strlen("include_path") + 1, server->include_path, server->include_path_len, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC)) {
+			php_cli_server_logf("%s Failed to set include_path (%s)" TSRMLS_CC, client->addr_str, server->include_path);
+		} else {
+			zend_try {
+				zval *retval = NULL;
+				if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, &retval, 1, &zfd)) {
+					if (retval) {
+						decline = Z_TYPE_P(retval) == IS_BOOL && !Z_LVAL_P(retval);
+						zval_ptr_dtor(&retval);
+					}
+				} else {
+					decline = 1;
 				}
-			} else {
-				decline = 1;
-			}
-		} zend_end_try();
+			} zend_end_try();
+		}
 
 		if (old_cwd[0] != '\0') {
 			php_ignore_value(VCWD_CHDIR(old_cwd));
@@ -2017,7 +2029,7 @@
 			destroy_request_info(&SG(request_info));
 			return SUCCESS;
 		}
-	} 
+	}
 
 	if (server->router) {
 		if (!php_cli_server_dispatch_router(server, client TSRMLS_CC)) {
@@ -2080,12 +2092,13 @@
 	pefree(*p, 1);
 } /* }}} */
 
-static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router TSRMLS_DC) /* {{{ */
+static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *include_path, const char *router TSRMLS_DC) /* {{{ */
 {
 	int retval = SUCCESS;
 	char *host = NULL;
 	char *errstr = NULL;
 	char *_document_root = NULL;
+	char *_include_path = NULL;
 	char *_router = NULL;
 	int err = 0;
 	int port = 3000;
@@ -2161,6 +2174,17 @@
 		server->document_root_len = document_root_len;
 	}
 
+	{
+		size_t include_path_len = strlen(include_path);
+		_include_path = pestrndup(include_path, include_path_len, 1);
+		if (!_include_path) {
+			retval = FAILURE;
+			goto out;
+		}
+		server->include_path = _include_path;
+		server->include_path_len = include_path_len;
+	}
+
 	if (router) {
 		size_t router_len = strlen(router);
 		_router = pestrndup(router, router_len, 1);
@@ -2402,11 +2426,15 @@
 		document_root = ret ? document_root_buf: ".";
 	}
 
+	char *include_path = PG(include_path);
+	char new_include_path[strlen(document_root) + strlen(include_path) + 2];
+	sprintf(new_include_path, "%s%c%s", document_root, ZEND_PATHS_SEPARATOR, include_path);
+
 	if (argc > php_optind) {
 		router = argv[php_optind];
 	}
 
-	if (FAILURE == php_cli_server_ctor(&server, server_bind_address, document_root, router TSRMLS_CC)) {
+	if (FAILURE == php_cli_server_ctor(&server, server_bind_address, document_root, new_include_path, router TSRMLS_CC)) {
 		return 1;
 	}
 	sapi_module.phpinfo_as_text = 0;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 01:01:28 2024 UTC