Location: PHPKode > scripts > phpMailShare > phpms/functions.php
<?php 
$VER = '0.1 beta';

function error($msg){
echo "<div>  
			<strong>Error:</strong> $msg
	  </div>";
die();
}

function ByteSize($bytes) 
    {
    $size = $bytes / 1024;
    if($size < 1024)
        {
        $size = number_format($size, 2);
        $size .= ' KB';
        } 
    else 
        {
        if($size / 1024 < 1024) 
            {
            $size = number_format($size / 1024, 2);
            $size .= ' MB';
            } 
        else if ($size / 1024 / 1024 < 1024)  
            {
            $size = number_format($size / 1024 / 1024, 2);
            $size .= ' GB';
            } 
        }
    return $size;
} 
	
function transformHTML($str) {
   if ((strpos($str,"<HTML") < 0) || (strpos($str,"<html")    < 0)) {
  		$makeHeader = "<html><head><meta http-equiv=\"Content-Type\"    content=\"text/html; charset=iso-8859-1\"></head>\n";
   	if ((strpos($str,"<BODY") < 0) || (strpos($str,"<body")    < 0)) {
   		$makeBody = "\n<body>\n";
   		$str = $makeHeader . $makeBody . $str ."\n</body></html>";
   	} else {
   		$str = $makeHeader . $str ."\n</html>";
   	}
   } else {
   	$str = "<meta http-equiv=\"Content-Type\" content=\"text/html;    charset=iso-8859-1\">\n". $str;
   }
   	return $str;
 }

function get_mime_type(&$structure) {
   $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
   if($structure->subtype) {
   	return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
   }
   	return "TEXT/PLAIN";
   }
   
function get_part($stream, $msg_number, $mime_type, $structure = false,$part_number    = false) {
   
   	if(!$structure) {
   		$structure = imap_fetchstructure($stream, $msg_number);
   	}
   	if($structure) {
   		if($mime_type == get_mime_type($structure)) {
   			if(!$part_number) {
   				$part_number = "1";
   			}
   			$text = imap_fetchbody($stream, $msg_number, $part_number);
   			if($structure->encoding == 3) {
   				return imap_base64($text);
   			} else if($structure->encoding == 4) {
   				return imap_qprint($text);
   			} else {
   			return $text;
   		}
   	}
   
		if($structure->type == 1) /* multipart */ {
   		while(list($index, $sub_structure) = each($structure->parts)) {
   			if($part_number) {
   				$prefix = $part_number . '.';
   			}
   			$data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix .    ($index + 1));
   			if($data) {
   				return $data;
   			}
   		} // END OF WHILE
   		} // END OF MULTIPART
   	} // END OF STRUTURE
   	return false;
   } // END OF FUNCTION

function fetchBox($boxname,$fp,$tp){
global $hostname, $user, $pwd;

set_time_limit(0);

$mbox = imap_open($hostname.$boxname, $user,$pwd) or error(imap_last_error());
if ($hdr = imap_check($mbox)) 
{
	if (($fp > $hdr->Nmsgs) || ($fp < 1)) {$fp=1;}
	if (($tp > $hdr->Nmsgs) || ($tp < 1)) {$tp=$hdr->Nmsgs;} 

	if ($overview=imap_fetch_overview($mbox,"$fp:$tp",0)){
		foreach ($overview as $m){
			//clean from field
			$m->from = ereg_replace("\"","",$m->from);
			
			//format date
			list($dayName,$day,$month,$year,$time) = split(" ",$m->date); 
			$time = substr($time,0,5);
			$m->date = $day ." ". $month ." ". $year . " ". $time;
			
			//decode subject text
			$dec = imap_mime_header_decode($m->subject);
			$m->subject = $dec[0]->text; 
			//reduce subject length
			if (strlen($m->subject) > 150) {$m->subject = substr($m->subject,0,149) ."...";}								   
		}//end of foreach overview
		}//end if have overview
	} 
	else {return imap_last_error();}
imap_close($mbox);
return $overview;
}

function fetchMsg($boxname,$msgno){
global $hostname, $user, $pwd;
set_time_limit(0);

$mbox = imap_open($hostname.$boxname, $user,$pwd) or error(imap_last_error());
if ($hdr = imap_check($mbox)) {
		if ($overview=imap_fetch_overview($mbox,"$msgno:$msgno",0)){
			$m=$overview[0];
			//clean from field
			$m->from = ereg_replace("\"","",$m->from);
			/*
			//format date
			list($dayName,$day,$month,$year,$time) = split(" ",$m->date); 
			$time = substr($time,0,5);
			$m->date = $day ." ". $month ." ". $year . " ". $time;
			*/
			//decode subject text
			$dec = imap_mime_header_decode($m->subject);
			$m->subject = $dec[0]->text; 
			//reduce subject length
			if (strlen($m->subject) > 150) {$m->subject = substr($m->subject,0,149) ."...";}			
			
			//get mail body - NEEDS REVIEW
			$dataTxt = get_part($mbox, $m->msgno, "TEXT/PLAIN");
			$dataHtml = get_part($mbox, $m->msgno, "TEXT/HTML");		
   
			if ($dataHtml != "") {
			   	$msgBody = $dataHtml;
			   	$m->bodyFormat = "html";
			} else {
				$msgBody = ereg_replace("\n","<br />",$dataTxt);
				$m->bodyFormat = "text";
			}
			 if ($dataHtml != "") {
				$msgBody = transformHTML($dataHtml);
			 } else {
			   	$msgBody = ereg_replace("\n","<br />",$dataTxt);
			   	$msgBody = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i","$1http://$2",    $msgBody);
			   	$msgBody = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<A    TARGET=\"_blank\" HREF=\"$1\">$1</A>", $msgBody);
			   	$msgBody = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A    HREF=\"mailto:$1\">$1</A>",$msgBody);
			 }  
			$msgBody = str_replace('<br>','<br />',$msgBody);
//			$m->bodyText = strip_tags($msgBody,'<br><a>');
			$m->bodyText = $msgBody;
			//get attachment info - NEEDS REVIEW
		   	$struct = imap_fetchstructure($mbox,$m->msgno);		   
		   	$contentParts = count($struct->parts);
		   	if ($contentParts >= 2) {
			   for ($i=2;$i<=$contentParts;$i++) {
			$att[$i-2] = imap_bodystruct($mbox,$m->msgno,$i);
			}
			for ($k=0;$k<sizeof($att);$k++) {
				$m->attachments[$k]->id = $k;
				$m->attachments[$k]->size = $att[$k]->bytes;
				
				if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value    == "US-ASCII") {
					if ($att[$k]->parameters[1]->value != "") {
						$m->attachments[$k]->name = $att[$k]->parameters[1]->value;
					}
				} elseif ($att[$k]->parameters[0]->value != "iso-8859-1" &&    $att[$k]->parameters[0]->value != "ISO-8859-1") {
					$m->attachments[$k]->name = $att[$k]->parameters[0]->value;
				}
						
			}//end for $k
		   }//end contentparts
		}//end if have overview
	} 
	else {return imap_last_error();}
imap_close($mbox);
return $m;
}

?>
Return current item: phpMailShare