diff -ruN mailhandler/mailhandler.install /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.install
--- mailhandler/mailhandler.install	2010-02-01 14:27:09.000000000 +0100
+++ /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.install	2011-01-25 15:13:54.000000000 +0100
@@ -1,5 +1,7 @@
 <?php
 
+include_once('user_imap.inc');
+
 /**
  * Implementation of hook_install().
  */
@@ -167,12 +169,13 @@
 function mailhandler_requirements($phase) {
   // Ensure translations don't break at install time
   $t = get_t();$has_imap = function_exists('imap_open');
+  $has_user_imap = user_imap_implementation();
   
   $requirements['mailhandler'] = array(
     'title' => $t('IMAP'),
-    'description' => $t("Mailhandler requires that PHP's !ext is enabled in order to function properly.", array('!ext' => l('IMAP extension', 'http://www.php.net/imap'))),
-    'value' => $has_imap ? $t('Enabled') : $t('Not found'),
-    'severity' => $has_imap ? REQUIREMENT_OK : REQUIREMENT_ERROR,
+    'description' => $t("Mailhandler requires that PHP's !ext is enabled in order to function properly. Otherwise a limited userspace version (POP3 only) will be used.", array('!ext' => l('IMAP extension', 'http://www.php.net/imap'))),
+    'value' => $has_imap ? $has_user_imap ? $t('Userspace') : $t('Enabled') : $t('Not found'),
+    'severity' => $has_imap ? $has_user_imap ? REQUIREMENT_WARNING : REQUIREMENT_OK : REQUIREMENT_ERROR,
   );
   return $requirements;
-}
\ No newline at end of file
+}
diff -ruN mailhandler/mailhandler.module /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.module
--- mailhandler/mailhandler.module	2010-06-04 14:16:11.000000000 +0200
+++ /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.module	2011-01-25 14:46:23.000000000 +0100
@@ -1,6 +1,8 @@
 <?php
 // $Id: mailhandler.module,v 1.96.2.29 2010/06/04 12:16:11 developmentseed Exp $
 
+include_once('user_imap.inc');
+
 
 /**
  * Implementation of hook_cron(). Process msgs from all enabled mailboxes.
@@ -564,7 +566,11 @@
         // Only add term if node type can use that term's vocab
         $term = taxonomy_get_term($data[1][0]);
         if (array_key_exists($term->vid, $vocabs)) {
-          $node->taxonomy = array_merge($node->taxonomy, $data[1]);
+          if (is_array($node->taxonomy[$term->vid])) {
+            $node->taxonomy[$term->vid] = array_merge($node->taxonomy[$term->vid], $data[1]);
+	  } else {
+	    $node->taxonomy[$term->vid] = $data[1];
+	  }
         }
         unset($data[0]);
       }
@@ -579,7 +585,11 @@
         }
         else if (!$vocabulary->tags) {
           array_walk($data[1], 'mailhandler_term_map', $vid);
-          $node->taxonomy = array_merge($node->taxonomy, $data[1]);
+          if (is_array($node->taxonomy[$vid])) {
+            $node->taxonomy[$vid] = array_merge($node->taxonomy[$vid], $data[1]);
+	  } else {
+	    $node->taxonomy[$vid] = $data[1];
+	  }
           unset($data[0]);
         }
         else if ($vocabulary->tags) {
diff -ruN mailhandler/mailhandler.retrieve.inc /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.retrieve.inc
--- mailhandler/mailhandler.retrieve.inc	2010-04-09 04:14:42.000000000 +0200
+++ /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/mailhandler.retrieve.inc	2011-01-27 14:44:28.000000000 +0100
@@ -1,5 +1,6 @@
 <?php
 
+include_once('user_imap.inc');
 
 /**
  * Establish IMAP stream connection to specified mailbox.
diff -ruN mailhandler/user_imap.inc /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/user_imap.inc
--- mailhandler/user_imap.inc	1970-01-01 01:00:00.000000000 +0100
+++ /var/www/htdocs/drupaltest/sites/all/modules/mailhandler/user_imap.inc	2011-01-27 14:07:09.000000000 +0100
@@ -0,0 +1,818 @@
+<?php 
+
+/*
+	'user space' imap.
+	
+	Implements a subset of the php imap functions.
+	Mostly interesting for those on hosting servers with limited php
+	installs.
+	It only supports pop3 and only the subset needed for mailhandler.
+	
+		Jeroen Vreeken <jeroen@vreeken.net>
+ */
+
+function user_imap_pop3_reply($sock)
+{
+	$reply = fgets($sock, 255);
+
+	if ($reply == false)
+		return false;
+
+	if (substr($reply, 0, 1)!='+')
+		return false;
+	
+	return true;
+}
+
+function user_imap_open ($mailbox, $username, $password, $options = NULL, $n_retries = 0, $params = NULL)
+{
+	$connection['mailbox'] = $mailbox;
+	$connection['driver'] = 'pop3';
+	
+	$mailbox_host = preg_replace(
+	    "/({)(.*)(:.*)/",
+	    "$2", 
+	    $mailbox);
+	$mailbox_port = preg_replace(
+	    "/({.*:)([a-zA-Z0-9]*)(\/.*)+(}.*)/",
+	    "$2", 
+	    $mailbox);
+	$mailbox_protocol = preg_replace(
+	    "/([{])(.*)(:)(.*)\/(pop3)(.*)([}])(.*)/",
+	    "$5",
+	    $mailbox);
+
+	if ($mailbox_protocol!='pop3') {
+		return false;
+	}
+
+	$sock = fsockopen($mailbox_host, $mailbox_port);
+
+	if (!$sock) {
+		return false;
+	}
+	$connection['sock'] = $sock;
+	
+	if (!user_imap_pop3_reply($sock)) {
+		return false;
+	}
+	
+	fwrite($sock, "user $username\n");
+	if (!user_imap_pop3_reply($sock)) {
+		return false;
+	}
+
+	fwrite($sock, "pass $password\n");
+	if (!user_imap_pop3_reply($sock)) {
+		return false;
+	}
+
+	return $connection;
+}
+
+function user_imap_num_msg ($connection)
+{
+	$num_msg = 0;
+	
+	fwrite($connection['sock'], "STAT\n");
+	$reply = fgets($connection['sock'], 255);
+
+	if (substr($reply, 0, 1)=='+') {
+		$num_msg_str = preg_replace(
+		    "/(\+OK )([0-9]+)( )([0-9]+)(.*)/",
+		    "$2", 
+		    $reply);
+		$num_msg = (int)$num_msg_str;
+	}
+	
+	return $num_msg;
+}
+
+function user_imap_pop3_size ($connection, $msg)
+{
+	fwrite($connection['sock'], "LIST $msg\n");
+	$reply = fgets($connection['sock'], 255);
+
+	if (substr($reply, 0, 1)=='+') {
+		$size_str = preg_replace(
+		    "/(\+OK [0-9]+ )([0-9]+)(.*)/",
+		    "$2", 
+		    $reply);
+		$size = (int)$size_str;
+	}
+	
+	return $size;
+}
+
+function user_imap_base64($data)
+{
+	return base64_decode($data);
+}
+
+function user_imap_uid($connection, $msg)
+{
+	return $msg;
+}
+
+function user_imap_msgno($connection, $uid)
+{
+	return $uid;
+}
+
+function user_imap_pop3_retr($connection, $msg_number)
+{
+	$found_terminator = false;
+	
+	fwrite($connection['sock'], "RETR $msg_number\n");
+	if (!user_imap_pop3_reply($connection['sock'])) {
+		return false;
+	}
+	while (!$found_terminator) {
+		$message_line = fgets($connection['sock']);
+		if ($message_line == false)
+			return false;
+		if (substr($message_line, 0, 2)==".\r")
+			$found_terminator = true;
+		else
+			$message[] = $message_line;
+	}
+	return $message;
+}
+
+function user_imap_pop3_body($message)
+{
+	$bodyfound = false;
+	$bodyline = 0;
+	foreach($message as $line) {
+		if (substr($line, 0, 1)=="\r")
+			$bodyfound = true;
+		if ($bodyfound) {
+			$bodyline++;
+			if ($bodyline > 1)
+				$body[] = $line;
+		}
+	}
+	return $body;
+}
+
+function user_imap_pop3_header($message)
+{
+	foreach($message as $line) {
+		if (substr($line, 0, 1)=="\r")
+			return $header;
+		else
+			$header[] = $line;
+	}
+	return false;
+}
+
+function user_imap_pop3_header_concatline($header)
+{
+	$lineno = -1;
+	foreach($header as $line) {
+		if (substr($line, 0, 1) == ' ' || substr($line, 0, 1) == "\t") {
+			$newheader[$lineno] = $newheader[$lineno].' '.$line;
+		} else {
+			$lineno++;
+			$newheader[$lineno] = $line;
+		}
+	}
+	$newheader = preg_replace('/\r|\n|\t/',  '', $newheader);
+	return $newheader;
+}
+
+class user_imap_headerinfo_object
+{
+	public $toaddress;	/* full to: line, up to 1024 characters */
+	public $to;	/* an array of objects from the To: line, with the following properties: personal, adl, mailbox, and host */
+	public $fromaddress;	/* full from: line, up to 1024 characters */
+	public $from;	/* an array of objects from the From: line, with the following properties: personal, adl, mailbox, and host */
+	public $ccaddress;	/* full cc: line, up to 1024 characters */
+	public $cc; 	/* an array of objects from the Cc: line, with the following properties: personal, adl, mailbox, and host */
+	public $bccaddress;	/* full bcc: line, up to 1024 characters */
+	public $bcc; /* an array of objects from the Bcc: line, with the following properties: personal, adl, mailbox, and host */
+	public $reply_toaddress;	/* full Reply-To: line, up to 1024 characters */
+	public $reply_to;	/* an array of objects from the Reply-To: line, with the following properties: personal, adl, mailbox, and host */
+	public $senderaddress;	/* full sender: line, up to 1024 characters */
+	public $sender;	/* an array of objects from the Sender: line, with the following properties: personal, adl, mailbox, and host */
+	public $return_pathaddress;	/* full Return-Path: line, up to 1024 characters */
+	public $return_path;	/* an array of objects from the Return-Path: line, with the following properties: personal, adl, mailbox, and host */
+	public $remail;
+	public $date;	/* The message date as found in its headers */
+	public $Date;	/*Same as date */
+	public $subject;	/* The message subject */
+	public $Subject;	/* Same a subject */
+	public $in_reply_to;
+	public $message_id;
+	public $newsgroups;
+	public $followup_to;
+	public $references;
+	public $Recent = ' '; 	/* R if recent and seen, N if recent and not seen, ' ' if not recent. */
+	public $Unseen = 'U'; 	/* U if not seen AND not recent, ' ' if seen OR not seen and recent  */
+	public $Flagged = ' ';	/* F if flagged, ' ' if not flagged */
+	public $Answered = ' ';	/* A if answered, ' ' if unanswered */
+	public $Deleted = ' ';	/* D if deleted, ' ' if not deleted */
+	public $Draft = ' ';	/* X if draft, ' ' if not draft */
+	public $Msgno = 0;	/* The message number */
+	public $MailDate;
+	public $Size = 0;	/* The message size */
+	public $udate = 0;	/* mail message date in Unix time */
+	public $fetchfrom = '';	/* from line formatted to fit fromlength characters */
+	public $fetchsubject = '';	/* subject line formatted to fit subjectlength characters */
+}
+
+class user_imap_headerinfo_address_object {
+	public $personal = '';
+	public $adl;
+	public $mailbox;
+	public $host;
+}
+
+function user_imap_headerinfo_address($line)
+{
+	$lines = preg_split('/,/', $line);
+	$lines = preg_replace('/\t/',  '', $lines);
+	$simple_lines = preg_grep('/<.+@.+>/', $lines, PREG_GREP_INVERT);
+	$personal_lines = preg_grep('/<.+@.+>/', $lines);
+	
+	foreach ($personal_lines as $personal_line) {
+		$object = new user_imap_headerinfo_address_object;
+		$objects[] = $object;
+		$object->personal = preg_replace(
+		    "/(.+)(<.+)/",
+		    "$1",
+		    $personal_line);
+		$object->mailbox = preg_replace(
+		    "/(.*<)(.+)(@.*)/",
+		    "$2", 
+		    $personal_line);
+		$object->host = preg_replace(
+		    "/(.*<)(.+@)(.+)(>.*)/",
+		    "$3", 
+		    $personal_line);
+	}
+	foreach ($simple_lines as $simple_line) {
+		$object = new user_imap_headerinfo_address_object;
+		$objects[] = $object;
+		$object->mailbox = preg_replace(
+		    "/(.+)(@.+)/",
+		    "$1", 
+		    $simple_line);
+		$object->host = preg_replace(
+		    "/(.+@)(.+)/",
+		    "$2", 
+		    $simple_line);
+	}
+	
+	return $objects;
+}
+
+function user_imap_headerinfo ($connection, $msg_number, $fromlength = 0, $subjectlength = 0, $defaulthost = NULL)
+{
+	$headerinfo_object = new user_imap_headerinfo_object;
+	if ($msg_number > user_imap_num_msg($connection))
+		return NULL;
+
+	$headerinfo_object->Size = user_imap_pop3_size ($connection, $msg_number);
+	$headerinfo_object->Msgno = $msg_number;
+
+	$message = user_imap_pop3_retr($connection, $msg_number);
+	$header = user_imap_pop3_header($message);
+	$header = user_imap_pop3_header_concatline($header);
+
+	foreach($header as $line) {
+		if (strtolower(substr($line, 0, 3)) == "to:") {
+			$headerinfo_object->toaddress = substr($line, 4);
+			$headerinfo_object->to = user_imap_headerinfo_address(substr($line,4));
+		}
+		if (strtolower(substr($line, 0, 5)) == "from:") {
+			$headerinfo_object->fromaddress = substr($line, 6);
+			$headerinfo_object->from = user_imap_headerinfo_address(substr($line,6));
+		}
+		if (strtolower(substr($line, 0, 3)) == "cc:") {
+			$headerinfo_object->ccaddress = substr($line, 4);
+			$headerinfo_object->cc = user_imap_headerinfo_address(substr($line,4));
+		}
+		if (strtolower(substr($line, 0, 4)) == "bcc:") {
+			$headerinfo_object->bccaddress = substr($line, 5);
+			$headerinfo_object->bcc = user_imap_headerinfo_address(substr($line,5));
+		}
+		if (strtolower(substr($line, 0, 9)) == "reply-to:") {
+			$headerinfo_object->reply_toaddress = substr($line, 10);
+			$headerinfo_object->reply_to = user_imap_headerinfo_address(substr($line,10));
+		}
+		if (strtolower(substr($line, 0, 7)) == "sender:") {
+			$headerinfo_object->senderaddress = substr($line, 8);
+			$headerinfo_object->sender = user_imap_headerinfo_address(substr($line,8));
+		}
+		if (strtolower(substr($line, 0, 12)) == "return-path:") {
+			$headerinfo_object->return_pathaddress = substr($line, 13);
+			$headerinfo_object->return_path = user_imap_headerinfo_address(substr($line,13));
+		}
+		if (strtolower(substr($line, 0, 5)) == "date:") {
+			$headerinfo_object->date = substr($line, 6);
+			$headerinfo_object->Date = substr($line, 6);
+			$headerinfo_object->udate = strtotime($headerinfo_object->date);
+			$headerinfo_object->MailDate = strftime("%e-%b-%Y %H:%M:%S %z", $headerinfo_object->udate);
+		}
+		if (strtolower(substr($line, 0, 8)) == "subject:") {
+			$headerinfo_object->subject = substr($line, 9);
+			$headerinfo_object->Subject = substr($line, 9);
+		}
+		if (strtolower(substr($line, 0, 12)) == "in-reply-to:") {
+			$headerinfo_object->in_reply_to = substr($line, 13);
+		}
+		if (strtolower(substr($line, 0, 11)) == "message-id:") {
+			$headerinfo_object->message_id = substr($line, 12);
+		}
+		if (strtolower(substr($line, 0, 11)) == "references:") {
+			$headerinfo_object->references = substr($line, 12);
+		}
+	}
+	
+	return $headerinfo_object;
+}
+function user_imap_header ($connection, $msg_number, $fromlength = 0, $subjectlength = 0, $defaulthost = NULL)
+{
+	return user_imap_headerinfo($connection, $msg_number, $fromlength, $subjectlength, $defaulthost);
+}
+
+function user_imap_body_array ($connection , $msg_number)
+{
+	if ($msg_number > user_imap_num_msg($connection))
+		return NULL;
+
+	$message = user_imap_pop3_retr($connection, $msg_number);
+	$body = user_imap_pop3_body($message);
+	$body = preg_replace('/\r/',  '', $body);
+	
+	return $body;
+}
+
+function user_imap_body ($connection , $msg_number, $options = 0)
+{
+	$body_string = '';
+	
+	$body = user_imap_body_array($connection, $msg_number);
+	foreach($body as $line) {
+		$body_string = $body_string.$line;
+	}
+	
+	return $body_string;
+}
+
+class user_imap_mime_header_decode_object {
+	public $charset;
+	public $text;
+}
+
+function user_imap_mime_header_decode($text)
+{
+	$len = strlen($text);
+	$start = true;
+	
+	for ($i = 0; $i < $len; $i++) {
+		if (substr($text, $i, 2)=='=?') {
+			$curobj = new user_imap_mime_header_decode_object;
+			$objects[] = $curobj;
+			$i+=2;
+			while (substr($text, $i, 1)!='?') {
+				$curobj->charset = $curobj->charset.substr($text, $i, 1);
+				$i++;
+			}
+			$i++;
+			$encoding = substr($text, $i, 1);
+			$i+=2;
+			if ($encoding == 'q' || $encoding == 'Q') {
+				$qstring = '';
+				while (substr($text, $i, 2)!='?=') {
+					$qstring = $qstring.substr($text, $i, 1);
+					$i++;
+				}
+				$qstring = preg_replace(
+				    "/_/",
+				    " ", 
+				    $qstring);
+
+				$curobj->text = user_imap_qprint(
+				    $curobj->text.$qstring);
+				$i+=1;
+			}
+			if ($encoding == 'b' || $encoding == 'B') {
+				$b64 = '';
+				while (substr($text, $i, 2)!='?=') {
+					$b64 = $b64.substr($text, $i, 1);
+					$i++;
+				}
+				$curobj->text = user_imap_base64(
+				    $curobj->text.$b64);
+				$i+=1;
+			}
+			$start = false;
+		} else {
+			if ($start || $curobj->charset !='default') {
+				$curobj = new user_imap_mime_header_decode_object;
+				$objects[] = $curobj;
+				$start = false;
+			}
+			$curobj->charset='default';
+			$curobj->text = $curobj->text.substr($text, $i, 1);
+		}
+	}
+	
+	return $objects;
+}
+
+function user_imap_qprint($qtext)
+{
+	return quoted_printable_decode($qtext);
+}
+
+function user_imap_utf8($text)
+{
+	$utf = '';
+	$elements = imap_mime_header_decode($text);
+	for ($i=0; $i<count($elements); $i++) {
+		if (strtolower($elements[$i]->charset) == 'iso-8859-1') {
+			$utf = $utf.utf8_encode($elements[$i]->text);
+		} else {
+			$utf = $utf.$elements[$i]->text;
+		}
+	}
+	return $utf;
+}
+
+class user_imap_fetchstructure_object {
+	public $type;		/* Primary body type */
+	public $encoding = 0;	/* Body transfer encoding */
+	public $ifsubtype = 1;	/* TRUE if there is a subtype string */
+	public $subtype;	/* MIME subtype */
+	public $ifdescription = 0;	/* TRUE if there is a description string */
+	public $description;	/* Content description string
+	public $ifid = 0;	/* TRUE if there is an identification string */
+	public $id;		/* Identification string */
+	public $lines;		/* Number of lines */
+	public $bytes;		/* Number of bytes */
+	public $ifdisposition = 0;	/* TRUE if there is a disposition string */
+	public $disposition;		/* Disposition string */
+	public $ifdparameters = 0;	/* TRUE if the dparameters array exists */
+	public $dparameters;		/* An array of objects where each object has an "attribute" and a "value" property corresponding to the parameters on the Content-disposition MIME header. */
+	public $ifparameters = 0;	/* TRUE if the parameters array exists */
+	public $parameters;		/* An array of objects where each object has an "attribute" and a "value" property. */
+	public $parts;		/* An array of objects identical in structure to the top-level object, each of which corresponds to a MIME body part. */
+
+	public $message;
+	public $header;
+	public $body;
+	public $partnr;
+}
+
+class user_imap_fetchstructure_object_param {
+	public $attribute;
+	public $value;
+}
+
+function user_imap_fetchstructure_type($content_type, &$type, &$subtype)
+{
+	$type_a = 
+	    preg_replace('/(Content-Type: )(.*)(\/)(.*)/i', '$2', $content_type);
+	$subtype_a = 
+	    preg_replace('/(Content-Type: )(.*)(\/)(.*)/i', '$4', $content_type);
+	$subtype_a = preg_replace('/([^;]*)(;.*)/', '$1', $subtype_a);
+	foreach($type_a as $type_ent)
+		$type_s = $type_ent;
+	foreach($subtype_a as $subtype_ent)
+		$subtype = $subtype_ent;
+	$type_s = strtolower($type_s);
+	if ($type_s == 'text')
+		$type = 0;
+	else if ($type_s == 'multipart')
+		$type = 1;
+	else if ($type_s == 'message')
+		$type = 2;
+	else if ($type_s == 'application')
+		$type = 3;
+	else if ($type_s == 'audio')
+		$type = 4;
+	else if ($type_s == 'image')
+		$type = 5;
+	else if ($type_s == 'video')
+		$type = 6;
+	else
+		$type = 7;
+	$subtype = strtoupper($subtype);
+}
+
+function user_imap_fetchstructure_subparse(&$structure_object)
+{
+	$structure_object->bytes = 0;
+	$structure_object->lines = 0;
+	$structure_object->parameters = (array)NULL;
+	$structure_object->dparameters = (array)NULL;
+	$structure_object->parts = (array)NULL;
+	
+	for($i = 0; $i < count($structure_object->body); $i++) {
+		$bodyline = $structure_object->body[$i];
+		$structure_object->lines++;
+		$structure_object->bytes += strlen($bodyline);
+	}
+
+	$content_type = preg_grep('/Content-Type:/i', $structure_object->header);
+	user_imap_fetchstructure_type($content_type, $structure_object->type, $structure_object->subtype);
+	$content_type_multipart = preg_grep('/multipart/i', $content_type);
+	if (count($content_type_multipart)) {
+		$param = new user_imap_fetchstructure_object_param;
+		$param->attribute = 'BOUNDARY';
+		$value = preg_replace('/(.*boundary=\")(.*)(\")/i', '$2', $content_type);
+		foreach($value as $valueent)
+			$param->value = $valueent;
+		$boundary = $param->value;
+		$structure_object->parameters[] = $param;
+		$structure_object->ifparameters = 1;
+		
+		$found = 0;
+		foreach($structure_object->body as $bodyline) {
+			$tmp = substr($bodyline, 2, strlen($boundary));
+			if ($tmp == $boundary) {
+				if ($found) {
+					unset($part_message[count($part_message)-1]);
+					$structure_object_sub = new user_imap_fetchstructure_object;
+					$structure_object_sub->message = $part_message;
+					$structure_object_sub->header = 
+					    user_imap_pop3_header($structure_object_sub->message);
+					$structure_object_sub->header = 
+					    user_imap_pop3_header_concatline($structure_object_sub->header);
+					$structure_object_sub->body =
+					    user_imap_pop3_body($structure_object_sub->message);
+
+					$structure_object->parts[] =
+					    $structure_object_sub;
+				}
+				unset($part_message);
+				$found = 1;
+			} else {
+				$part_message[] = $bodyline;
+			}
+		}
+		foreach($structure_object->parts as $substruct)
+			user_imap_fetchstructure_subparse($substruct);
+	}
+	
+	$content_type_charset = preg_grep('/charset/i', $content_type);
+	if (count($content_type_charset)) {
+		$param = new user_imap_fetchstructure_object_param;
+		$param->attribute = 'CHARSET';
+		$value = preg_replace('/(.*charset=)(.*)/i', '$2', $content_type_charset);
+		$value = preg_replace('/([^;]*)(;.*)/', '$1', $value);
+		foreach($value as $valueent)
+			$param->value = $valueent;
+		$structure_object->parameters[] = $param;
+		$structure_object->ifparameters = 1;
+	}
+	$content_type_name = preg_grep('/name/i', $content_type);
+	if (count($content_type_name)) {
+		$param = new user_imap_fetchstructure_object_param;
+		$param->attribute = 'NAME';
+		$value = preg_replace('/(.*name=\")(.*)(\".*)/i', '$2', $content_type_name);
+		$value = preg_replace('/([^\"]*)(\".*)/', '$1', $value);
+		foreach($value as $valueent)
+			$param->value = $valueent;
+		$structure_object->parameters[] = $param;
+		$structure_object->ifparameters = 1;
+	}
+	$filename = preg_grep('/Content-Disposition.*filename/i', $structure_object->header);
+	if (count($filename)) {
+		$param = new user_imap_fetchstructure_object_param;
+		$param->attribute = 'FILENAME';
+		$value = preg_replace('/(.*filename=\")(.*)(\".*)/i', '$2', $filename);
+		$value = preg_replace('/([^\"]*)(\".*)/', '$1', $value);
+		foreach($value as $valueent)
+			$param->value = $valueent;
+		$structure_object->dparameters[] = $param;
+		$structure_object->ifdparameters = 1;
+	}
+	$content_transfer_encoding = preg_grep('/Content-Transfer-Encoding:/', $structure_object->header);
+	if (count($content_transfer_encoding)) {
+		if (count(preg_grep('/7bit/i', $content_transfer_encoding)))
+			$structure_object->encoding = 0;
+		if (count(preg_grep('/8bit/i', $content_transfer_encoding)))
+			$structure_object->encoding = 1;
+		if (count(preg_grep('/binary/i', $content_transfer_encoding)))
+			$structure_object->encoding = 2;
+		if (count(preg_grep('/base64/i', $content_transfer_encoding)))
+			$structure_object->encoding = 3;
+		if (count(preg_grep('/quoted-printable/i', $content_transfer_encoding)))
+			$structure_object->encoding = 4;
+	}
+	$content_id = preg_grep('/Content-ID:/i', $structure_object->header);
+	if (count($content_id)) {
+		$value = preg_replace('/(Content-ID: )(.*)/i', '$2', $content_id);
+		foreach($value as $valueent)
+			$structure_object->id = $valueent;
+		$structure_object->ifid = 1;
+	}
+}
+
+function user_imap_fetchstructure_setpart(&$p)
+{
+	// SUBPART RECURSION
+	if ($p->parts) {
+		foreach ($p->parts as $partno0=>$p2) {
+			$partnr = $partno0+1;
+			$p2->partnr = "$p->partnr.$partnr";
+			user_imap_fetchstructure_setpart($p2);
+		}
+	}
+}
+
+function user_imap_fetchstructure_complete($connection, $msg_number)
+{
+	$structure_object = new user_imap_fetchstructure_object;
+	
+	if ($msg_number > user_imap_num_msg($connection))
+		return NULL;
+
+	$structure_object->message = user_imap_pop3_retr($connection, $msg_number);
+	$structure_object->header = user_imap_pop3_header($structure_object->message);
+	$structure_object->header = user_imap_pop3_header_concatline($structure_object->header);
+	
+	$structure_object->body = user_imap_pop3_body($structure_object->message);
+
+	user_imap_fetchstructure_subparse($structure_object);
+
+	/* default is '1' for plain mail */
+	$structure_object->partnr = '1';
+	if ($structure_object->parts)
+		foreach ($structure_object->parts as $partno0=>$p) {
+			$structure_object->partnr = '0';
+			$partnr = $partno0+1;
+			$p->partnr = "$partnr";
+			user_imap_fetchstructure_setpart($p);
+		}
+	return $structure_object;
+}
+
+function user_imap_fetchstructure_clean(&$structure_object)
+{
+	unset($structure_object->body);
+	unset($structure_object->header);
+	unset($structure_object->message);
+	unset($structure_object->partnr);
+	
+	if (is_array($structure_object->parts)) {
+		foreach($structure_object->parts as $substruct)
+			user_imap_fetchstructure_clean($substruct);
+		reset($structure_object->parts);
+	}
+}
+
+function user_imap_fetchstructure($connection, $msg_number, $options = 0)
+{
+	$structure_object = user_imap_fetchstructure_complete($connection, $msg_number);
+
+	user_imap_fetchstructure_clean($structure_object);
+	
+	return $structure_object;	
+}
+
+function user_imap_fetchbody_get($structure_object, $section)
+{
+	if ($structure_object->partnr == $section) {
+		return $structure_object->body;
+	}
+
+	if (is_array($structure_object->parts)) {
+		foreach($structure_object->parts as $substruct) {
+			$body = user_imap_fetchbody_get($substruct, $section);
+			if ($body != false) {
+				return $body;
+			}
+		}
+	}
+	return false;
+}
+
+function user_imap_fetchbody($connection, $msg_number, $section, $options = 0)
+{
+	$body = '';
+
+	$structure_object = user_imap_fetchstructure_complete($connection, $msg_number);
+
+	$body_a = user_imap_fetchbody_get($structure_object, $section);
+	
+	if ($body_a != false) {
+		foreach($body_a as $line) {
+			$body = $body.$line;
+		}
+		return $body;
+	}
+	return false;
+}
+
+function user_imap_delete($connection, $msg_number, $options = 0)
+{
+	fwrite($connection['sock'], "DELE $msg_number\n");
+	if (!user_imap_pop3_reply($connection['sock'])) {
+		return false;
+	}
+}
+
+function user_imap_close($connection, $flag = 0)
+{
+	fwrite($connection['sock'], "QUIT\n");
+	if (!user_imap_pop3_reply($connection['sock'])) {
+		return false;
+	}
+	fclose($connection['sock']);
+}
+ 
+if (!function_exists('imap_open')) {
+	function imap_open ($mailbox, $username, $password, $options = NULL, $n_retries = 0, $params = NULL)
+	{
+		return user_imap_open($mailbox, $username, $password, $options, $n_retries, $params);
+	}
+	function imap_num_msg ($connection)
+	{
+		return user_imap_num_msg($connection);
+	}
+	function imap_base64($data)
+	{
+		return user_imap_base64($data);
+	}
+	function imap_uid($connection, $msg)
+	{
+		return user_imap_uid($connection, $msg);
+	}
+	function imap_msgno($connection, $uid)
+	{
+		return user_imap_msgno($connection, $uid);
+	}
+	function imap_header ($connection, $msg_number, $fromlength = 0, $subjectlength = 0, $defaulthost = NULL)
+	{
+		return user_imap_header($connection, $msg_number, $fromlength, $subjectlength, $defaulthost);
+	}
+	function imap_headerinfo ($connection, $msg_number, $fromlength = 0, $subjectlength = 0, $defaulthost = NULL)
+	{
+		return user_imap_headerinfo($connection, $msg_number, $fromlength, $subjectlength, $defaulthost);
+	}
+	function imap_body ($connection , $msg_number, $options = 0)
+	{
+		return user_imap_body($connection, $msg_number, $options);
+	}
+	function imap_qprint($qtext)
+	{
+		return user_imap_qprint($qtext);
+	}
+	function imap_utf8($text)
+	{
+		return user_imap_utf8($text);
+	}
+	function imap_fetchstructure($connection, $msg_number, $options = 0)
+	{
+		return user_imap_fetchstructure($connection, $msg_number, $options);
+	}
+	function imap_fetchbody($connection, $msg_number, $section, $options = 0)
+	{
+		return user_imap_fetchbody($connection, $msg_number, $section, $options);
+	}
+	function imap_mime_header_decode($text)
+	{
+		return user_imap_mime_header_decode($text);
+	}
+	function imap_close($connection, $flag = 0)
+	{
+		return user_imap_close($connection, $flag);
+	}
+	function imap_delete($connection, $msg_number, $options = 0)
+	{
+		return user_imap_delete($connection, $msg_number, $options);
+	}
+	
+	define('TYPETEXT', 0);
+	define('TYPEMULTIPART', 1);
+	define('TYPEMESSAGE', 2);
+	define('TYPEAPPLICATION', 3);
+	define('TYPEAUDIO', 4);
+	define('TYPEIMAGE', 5);
+	define('TYPEVIDEO', 6);
+	define('TYPEOTHER', 7);
+        
+	define('ENC7BIT', 0);
+        define('ENC8BIT', 1);
+        define('ENCBINARY', 2);
+        define('ENCBASE64', 3);
+        define('ENCQUOTEDPRINTABLE', 4);
+        define('ENCOTHER', 5);
+	
+	function user_imap_implementation()
+	{
+		return 1;
+	}
+} else {
+	function user_imap_implementation()
+	{
+		return 0;
+	}
+}
+
+?>