Fwd: [PHP-CVS] cvs: php4 /ext/mailparse mailparse.c php_mailparse.h

Chuck Hagenbuch chuck@horde.org
Wed, 29 Aug 2001 11:22:37 -0400


There are a number of things in this extension that we could make use of, if we 
wanted to require it. I'm not sure if that's a good idea, but it's worth 
discussing.

----- Forwarded message from Wez Furlong <wez.php@thebrainroom.com> -----
    Date: Tue, 28 Aug 2001 21:52:10 -0000
    From: Wez Furlong <wez.php@thebrainroom.com>
Reply-To: Wez Furlong <wez.php@thebrainroom.com>
 Subject: [PHP-CVS] cvs: php4 /ext/mailparse mailparse.c php_mailparse.h  
      To: php-cvs@lists.php.net

wez		Tue Aug 28 17:52:10 2001 EDT

  Modified files:              
    /php4/ext/mailparse	mailparse.c php_mailparse.h 
  Log:
  @- Added mailparse_uudecode_all() which extracts all uuencoded attachments
     (Wez)
  
  
Index: php4/ext/mailparse/mailparse.c
diff -u php4/ext/mailparse/mailparse.c:1.8 php4/ext/mailparse/mailparse.c:1.9
--- php4/ext/mailparse/mailparse.c:1.8	Mon Jul 30 04:24:29 2001
+++ php4/ext/mailparse/mailparse.c	Tue Aug 28 17:52:10 2001
@@ -18,7 +18,7 @@
    | the support routines for this extension were based upon.             |
    +----------------------------------------------------------------------+
  */
-/* $Id: mailparse.c,v 1.8 2001/07/30 08:24:29 zeev Exp $ */
+/* $Id: mailparse.c,v 1.9 2001/08/28 21:52:10 wez Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -31,6 +31,7 @@
 #include "mailparse_rfc822.h"
 #include "ext/standard/info.h"
 #include "main/php_output.h"
+#include "php_open_temporary_file.h"
 
 /* just in case the config check doesn't enable mbstring automatically */
 #if !HAVE_MBSTRING
@@ -59,6 +60,7 @@
 	PHP_FE(mailparse_rfc822_parse_addresses,		NULL)
 	PHP_FE(mailparse_determine_best_xfer_encoding, NULL)
 	PHP_FE(mailparse_stream_encode,					
	NULL)
+	PHP_FE(mailparse_uudecode_all,					NULL)
 
 	{NULL, NULL, NULL}
 };
@@ -137,6 +139,123 @@
 
 	php_error(E_WARNING, "%s(): %s %d", get_active_function_name(TSRMLS_C), 
msg, num);
 }
+
+#define UUDEC(c)	(char)(((c)-' ')&077)
+#define UU_NEXT(v)	v = fgetc(infp); if (v == EOF) break; v = UUDEC(v)
+static void mailparse_do_uudecode(FILE * infp, FILE * outfp)
+{
+	int A, B, C, D, n;
+
+	while(!feof(infp))	{
+		UU_NEXT(n);
+
+		while(n != 0)	{
+			UU_NEXT(A);
+			UU_NEXT(B);
+			UU_NEXT(C);
+			UU_NEXT(D);
+
+			if (n-- > 0)
+				fputc( (A << 2) | (B >> 4), outfp);
+			if (n-- > 0)
+				fputc( (B << 4) | (C >> 2), outfp);
+			if (n-- > 0)
+				fputc( (C << 6) | D, outfp);
+		}
+		/* skip newline */
+		fgetc(infp);
+	}
+}
+
+
+/* {{{ proto array mailparse_uudecode_all(resource fp)
+	Scan the data from fp and extract each embedded uuencoded file. Returns 
an array listing filename information
+*/
+PHP_FUNCTION(mailparse_uudecode_all)
+{
+	zval * file, * item;
+	FILE *infp, *outfp=NULL, *partfp=NULL;
+	int type;
+	char * buffer = NULL;
+	char * outpath = NULL;
+	int nparts = 0;
+
+	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &file))
+		return;
+
+	infp = (FILE*)zend_fetch_resource(&file TSRMLS_CC, -1, "File-Handle", 
&type, 1, php_file_le_fopen());
+	ZEND_VERIFY_RESOURCE(infp);
+
+	outfp = php_open_temporary_file(NULL, "mailparse", &outpath TSRMLS_CC);
+	if (outfp == NULL)	{
+		zend_error(E_WARNING, "%s(): unable to open temp file", 
get_active_function_name(TSRMLS_CC));
+		RETURN_FALSE;
+	}
+
+	rewind(infp);
+	
+	buffer = emalloc(4096);
+	while(fgets(buffer, 4096, infp))	{
+		/* Look for the "begin " sequence that identifies a uuencoded 
file */
+		if (strncmp(buffer, "begin ", 6) == 0)	{
+			char * origfilename;
+			int len;
+			/* parse out the file name.
+			 * The next 4 bytes are an octal number for perms; 
ignore it */
+			origfilename = &buffer[10];
+			/* NUL terminate the filename */
+			len = strlen(origfilename);
+			while(isspace(origfilename[len-1]))
+				origfilename[--len] = '\0';
+		
+			/* make the return an array */
+			if (nparts == 0)	{
+				array_init(return_value);
+				/* create an initial item representing the file 
with all uuencoded parts
+				 * removed */
+				MAKE_STD_ZVAL(item);
+				array_init(item);
+				add_assoc_string(item, "filename", outpath, 0);
+				add_next_index_zval(return_value, item);
+			}
+			
+			/* add an item */
+			MAKE_STD_ZVAL(item);
+			array_init(item);
+			add_assoc_string(item, "origfilename", origfilename, 1);
+
+			/* create a temp file for the data */
+			partfp = php_open_temporary_file(NULL, "mailparse", 
&outpath TSRMLS_CC);
+			if (partfp)	{
+				nparts++;
+				add_assoc_string(item, "filename", outpath, 0);
+				add_next_index_zval(return_value, item);
+
+				/* decode it */
+				mailparse_do_uudecode(infp, partfp);
+				fclose(partfp);
+			}
+		}
+		else	{
+			/* write to the output file */
+			fputs(buffer, outfp);
+		}
+	}
+	fclose(outfp);
+	rewind(infp);
+	efree(buffer);
+
+	if (nparts == 0)	{
+		/* delete temporary file */
+		unlink(outpath);
+		efree(outpath);
+		RETURN_FALSE;
+	}
+}
+/* }}} */
+
+
+
 
 /* {{{ proto array mailparse_rfc822_parse_addresses(string addresses)
 	parse addresses and return a hash containing that data
Index: php4/ext/mailparse/php_mailparse.h
diff -u php4/ext/mailparse/php_mailparse.h:1.2 
php4/ext/mailparse/php_mailparse.h:1.3
--- php4/ext/mailparse/php_mailparse.h:1.2	Sat Jul 28 07:35:58 2001
+++ php4/ext/mailparse/php_mailparse.h	Tue Aug 28 17:52:10 2001
@@ -18,7 +18,7 @@
    | the support routines for this extension were based upon.             |
    +----------------------------------------------------------------------+
  */
-/* $Id: php_mailparse.h,v 1.2 2001/07/28 11:35:58 zeev Exp $ */
+/* $Id: php_mailparse.h,v 1.3 2001/08/28 21:52:10 wez Exp $ */
 
 #ifndef PHP_MAILPARSE_H
 #define PHP_MAILPARSE_H
@@ -58,6 +58,7 @@
 PHP_FUNCTION(mailparse_rfc822_parse_addresses);
 PHP_FUNCTION(mailparse_determine_best_xfer_encoding);
 PHP_FUNCTION(mailparse_stream_encode);
+PHP_FUNCTION(mailparse_uudecode_all);
 
 #include "rfc2045.h"
 #include "mailparse_rfc822.h"



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-cvs-unsubscribe@lists.php.net
For additional commands, e-mail: php-cvs-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net


----- End forwarded message -----


-chuck

--
Charles Hagenbuch, <chuck@horde.org>
Some fallen angels have their good reasons.