Просмотр файла pofhq-wapmail/functions.php

Размер файла: 5.81Kb
<?
/*
$Header: /home/cvsroot/pofhq-wapmail/functions.php,v 1.7 2002/12/13 19:20:13 pau Exp $
Copyright (c) 2002 Pau Oliva Fora <[email protected]>               
Licensed under the GNU GPL. For full terms see the file COPYING.
*/

header("content-type:text/vnd.wap.wml");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");                          // HTTP/1.0

$VERSION="1.3beta2";

function wmlheader() {
	echo("<?xml version=\"1.0\"?>\n");
	echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n\n");
	echo("<wml>\n");
	}

function wmlmeta() {

	echo("<head>\n");
	echo("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\"/>\n");
	echo("<meta http-equiv=\"Cache-Control\" content=\"must-revalidate\" forua=\"true\"/>\n");
	echo("<meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/>\n");
	echo("</head>\n");
	}

function wmlfooter() {
	echo("</card>\n</wml>\n");
	}

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) { 

	/* yields the part of a message with a particular MIME type. Thanks to          */
	/* <[email protected]> for this function in php manual user contributed notes. */
	
	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 decode2(imap_base64($text)); 
			} else if($structure->encoding == 4) { 
				$text = imap_qprint($text);
				if (ereg("=\n", $text)) $text = ereg_replace ("=\n", "", $text);
				return decode2($text); 
			} else { 
				return decode2($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; 
				} 
			} 
		} 
	} 
	return false; 
} 


function decode($string) {

		/* Decodes base64 and quoted printable to a 'human readable' format */

		  if(ereg("=\?.{0,}\?[Bb]\?",$string)){
			  $arrHead=split("=\?.{0,}\?[Bb]\?",$string);
			  while(list($key,$value)=each($arrHead)){
				  if(ereg("\?=",$value)){
					  $arrTemp=split("\?=",$value);
					  $arrTemp[0]=base64_decode($arrTemp[0]);
					  $arrHead[$key]=join("",$arrTemp);
				  }
			  }
			  $string=join("",$arrHead);
		  }

		  if(ereg("=\?.{0,}\?[Qq]\?",$string)){
			  $string=quoted_printable_decode($string);
			  $string=ereg_replace("=\?.{0,}\?[Qq]\?","",$string);
			  $string=ereg_replace("\?=","",$string);
		  }
		  return decode2($string);
}


function decode2($string) {

	/* Displays message correctly when there is a '$' sign on it */
	/* and reduces redundant signs usually used in signatures.   */

	if (ereg("\\$",$string)) $string=ereg_replace("\\$","\$$","$string");
	if (ereg(" />",$string)) $string=ereg_replace(" />","/>","$string");
	if (ereg("\ \ ",$string)) $string=ereg_replace("\ \ "," ","$string");
	if (ereg("----",$string)) $string=ereg_replace("----","--","$string");
	if (ereg("____",$string)) $string=ereg_replace("____","__","$string");
	if (ereg("====",$string)) $string=ereg_replace("====","==","$string");
	if (ereg("####",$string)) $string=ereg_replace("####","##","$string");
	if (ereg("&amp;nbsp;",$string)) $string=ereg_replace("&amp;nbsp;","","$string");
	return $string;
}


function waplink($title,$name,$href,$fields) {
	
	/* generates the needed link in wml from an array */

	echo "<anchor title=\"$title\">$name\n";
	echo "<go href=\"$href\" method=\"post\">\n";
	foreach ($fields as $key => $value) echo "<postfield name=\"$key\" value=\"$value\"/>\n";
	echo "</go></anchor><br/>\n";	
}


function wapbutton($form,$label,$href,$fields) {

	/* generates a wml button in the browser (similar to a link) */

   echo "<do type=\"accept\" label=\"$label\">\n";
	echo "<go href=\"$href\" method=\"post\">\n";
	if ($form) foreach ($fields as $key => $value) echo "<postfield name=\"$key\" value=\"$".$value."\"/>\n";
	else foreach ($fields as $key => $value) echo "<postfield name=\"$key\" value=\"$value\"/>\n";
	echo "</go></do>\n";
}


function isutf8($string) {

	/* try to see if $msgbody is encoded in UTF-8, otherwise encode it. */

	if (ereg("б|й|н|у|ъ|а|и|м|т|щ|д|л|п|ц|ь|Б|Й|Н|У|Ъ|А|И|М|Т|Щ|Д|Л|П|Ц|Ь|с|С|з|З",$string)) $string = utf8_encode($string);
	return $string;
}


function getsubject($string) {

	/* returns the subject of a message ready to display */
	
	if (empty($string)) $string = "(No subject)";
	else $string = isutf8(trim(htmlspecialchars(decode($string))));
	return $string;
}


function getmsgbody($mbox,$msgid) {

	/* returns the body of a message ready to display */

	$msgbody = isutf8(decode2(trim(nl2br(htmlspecialchars(get_part($mbox, $msgid, "TEXT/PLAIN"))))));
	$msgbody = "$msgbody ";
	$bodysize = strlen($msgbody);
	if ($bodysize == 1) {
		$msgbody = isutf8(decode2(trim(nl2br(htmlspecialchars(strip_tags(get_part($mbox, $msgid, "TEXT/HTML")))))));
		$msgbody = "$msgbody ";
	}
	return $msgbody;
}


function  wmldie($string) {

	/* wml output error */

	echo "<card><p>ERROR: $string <br/><br/></p>\n";
	wmlfooter();
	die();
}

?>