[PHP-DEV] cvs: /php3/functions imap.c imap.h From: Andrew Skalski (askalski <email protected>)
Date: 04/19/00

askalski Wed Apr 19 13:01:44 2000 EDT

  Modified files:
    /php3/functions imap.c imap.h
  Log:
  added imap_mime_header_decode functionality, as seen in php4
  
  
Index: php3/functions/imap.c
diff -u php3/functions/imap.c:1.88 php3/functions/imap.c:1.89
--- php3/functions/imap.c:1.88 Thu Apr 13 21:08:04 2000
+++ php3/functions/imap.c Wed Apr 19 13:01:14 2000
@@ -33,7 +33,7 @@
    | Andrew Skalski <askalski <email protected>> |
    +----------------------------------------------------------------------+
  */
-/* $Id: imap.c,v 1.88 2000/04/14 04:08:04 chagenbu Exp $ */
+/* $Id: imap.c,v 1.89 2000/04/19 20:01:14 askalski Exp $ */
 
 #define IMAP41
 
@@ -61,6 +61,7 @@
 #include "imap.h"
 #include "mail.h"
 #include "rfc822.h"
+#include "utf8.h"
 #include "modules.h"
 #if (WIN32|WINNT)
 #include "winsock.h"
@@ -201,6 +202,7 @@
         {"imap_utf8", php3_imap_utf8, NULL},
         {"imap_utf7_decode", php3_imap_utf7_decode, NULL},
         {"imap_utf7_encode", php3_imap_utf7_encode, NULL},
+ {"imap_mime_header_decode", php3_imap_mime_header_decode, NULL},
         {NULL, NULL, NULL}
 };
 
@@ -2822,6 +2824,115 @@
 #undef B64CHAR
 #undef B64
 #undef UNB64
+
+/* {{{ proto object imap_mime_header_decode(string str)
+ Decode mime header element in accordance with RFC 2047 and return array of objects containing 'charset' encoding and decoded 'text' */
+void php3_imap_mime_header_decode(INTERNAL_FUNCTION_PARAMETERS)
+{
+ /* string, string-endp, charset, charset-endp,
+ * encoding, encoding-endp, text, text-endp,
+ * language separator, marked start of unencoded text */
+ unsigned char *s, *se, *cs, *ce, *e, *ee, *t, *te, *ls, *mark;
+ /* decoded text */
+ SIZEDTEXT decoded;
+ int argc;
+ pval *arg, obj;
+
+ /* collect arguments */
+ argc = ARG_COUNT(ht);
+ if (argc != 1 || getParameters(ht, argc, &arg) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(arg);
+ s = arg->value.str.val;
+ se = s + arg->value.str.len;
+
+ /* init return value */
+ if (array_init(return_value) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ /* conversion routine derived from Mark Crispin's c-client library
+ * function utf_mime2text()
+ */
+#define MINENCWORD 9
+ for (mark = s; s < se; s++) {
+ /* try to tokenize as a mime2 encoded word */
+ if ((se - s) > MINENCWORD && *s == '=' && s[1] == '?' &&
+ (cs = mime2_token(s + 2, se, &ce)) &&
+ (e = mime2_token(ce + 1, se, &ee)) &&
+ (t = mime2_text(e + 2, se, &te)))
+ {
+ /* try to decode the text */
+ if (!mime2_decode(e, t, te, &decoded)) {
+ /* skip over the block */
+ s = te + 1;
+ continue;
+ }
+
+ /* first, flush out any non-encoded text */
+ if (mark < s) {
+ object_init(&obj);
+ add_property_string(&obj,
+ "charset", "default", 1);
+ add_property_stringl(&obj,
+ "text", mark, s - mark, 1);
+ add_next_index_object(return_value, obj);
+ }
+
+ /* advance the string pointer and the mark */
+ s = te + 1;
+ mark = s + 1;
+
+ /* tie off charset and remove language specifier */
+ *ce = '\0';
+ if (ls = strchr(cs, '*')) *ls = '\0';
+ /* flush decoded text */
+ object_init(&obj);
+ add_property_string(&obj, "charset", cs, 1);
+ add_property_stringl(&obj, "text",
+ decoded.data, decoded.size, 1);
+ add_next_index_object(return_value, obj);
+ /* restore charset and free decoded data */
+ if (ls) *ls = '*';
+ *ce = '?';
+ fs_give((void**) &decoded.data);
+
+ /* skip trailing whitespace; handle continuations */
+ for (t = s + 1; t < se && (*t == ' ' || *t == '\t');
+ t++);
+ if (t < se - MINENCWORD) switch (*t) {
+ case '=': /* encoded block? */
+ if (t[1] == '?') s = t - 1;
+ break;
+ case '\r': /* CR */
+ if (t[1] == '\n') t++;
+ case '\n': /* LF */
+ if (t[1] == ' ' || t[1] == '\t') {
+ /* eat up leading spaces/tabs */
+ do t++;
+ while (t < se - MINENCWORD &&
+ (t[1] == ' ' || t[1] == '\t'));
+ /* found something interesting */
+ if (t < se - MINENCWORD &&
+ t[1] == '=' && t[2] == '?')
+ {
+ s = t;
+ }
+ }
+ }
+ }
+ }
+#undef MINENCWORD
+ /* flush out remaining non-encoded text */
+ if (mark < se) {
+ object_init(&obj);
+ add_property_string(&obj, "charset", "default", 1);
+ add_property_stringl(&obj, "text", mark, se - mark, 1);
+ add_next_index_object(return_value, obj);
+ }
+}
+/* }}} */
 
 /* {{{ proto int imap_setflag_full(int stream_id, string sequence, string flag [, int options])
    Sets flags on messages */
Index: php3/functions/imap.h
diff -u php3/functions/imap.h:1.16 php3/functions/imap.h:1.17
--- php3/functions/imap.h:1.16 Mon Jan 17 11:19:03 2000
+++ php3/functions/imap.h Wed Apr 19 13:01:14 2000
@@ -1,4 +1,4 @@
-/* $Id: imap.h,v 1.16 2000/01/17 19:19:03 askalski Exp $ */
+/* $Id: imap.h,v 1.17 2000/04/19 20:01:14 askalski Exp $ */
 
 #ifndef _INCLUDED_IMAP_H
 #define _INCLUDED_IMAP_H
@@ -78,6 +78,7 @@
 void php3_imap_utf8(INTERNAL_FUNCTION_PARAMETERS);
 void php3_imap_utf7_decode(INTERNAL_FUNCTION_PARAMETERS);
 void php3_imap_utf7_encode(INTERNAL_FUNCTION_PARAMETERS);
+void php3_imap_mime_header_decode(INTERNAL_FUNCTION_PARAMETERS);
 #else
 #define php3_imap_module_ptr NULL
 #endif /* HAVE_IMAP */

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>