# a php open_basedir patch
# by Pawel Maziarz <p.maziarz@infomex.pl>
#
# this patch enables you to use a special char ~ in open_basedir variable which
# is placed in php.ini. ~ is replaced with script owner's home directory.
# example:
#          open_basedir = "~:/tmp"
#          open_basedir = "~/public_html"
#
# to apply this patch, go to the php source directory and type:
# patch -p1 < php-4.3.2-open_basedir.patch

--- php-4.3.2-orig/main/fopen_wrappers.c	2003-02-23 23:03:54.000000000 +0100
+++ php-4.3.2/main/fopen_wrappers.c	2003-07-23 18:46:09.000000000 +0200
@@ -123,6 +123,26 @@
 				&& (local_open_basedir_pos >= 0)) {
 			local_open_basedir[local_open_basedir_pos--] = 0;
 		}
+	} 
+	/* Special case basedir[0]=='~': Use php script owner home directory 
+	 * example: open_basedir = "/tmp:~", open_basedir = "~/public_html"
+	 */
+	else if (basedir[0] == '~') {
+		struct passwd *pw = NULL;
+		long myuid = 0L;
+		int basedir_len;
+
+		myuid = php_getuid();
+		
+		if ((pw = getpwuid(myuid)) == NULL || pw->pw_dir == NULL)
+			return -1;
+			
+		basedir_len = strlen(basedir);
+
+		if (basedir_len > 1) 
+			snprintf(local_open_basedir, sizeof(local_open_basedir), "%s%s", pw->pw_dir, basedir + 1);
+		else
+			strlcpy(local_open_basedir, pw->pw_dir, sizeof(local_open_basedir));
 	} else {
 		/* Else use the unmodified path */
 		strlcpy(local_open_basedir, basedir, sizeof(local_open_basedir));

