Location: PHPKode > projects > Laikwan WMC > wmc_20070325/hail/main.php
<?

/*
 * Hail is a PHP-Hotmail gateway. It will allow you to
 * check your hotmail account via any webserver with PHP.
 *
 * Created by: Craig Marshall <hide@address.com> and
 * Justin Carlson <hide@address.com>, 30-May-2003
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

// wmc lib
require_once("../wmcLib/clean.php");
require_once("../wmcLib/mime.php");

// hail lib
require_once("MailFolderItem.class.php");
require_once("MailFolder.class.php");
require_once("HTTPMail.class.php");

// pear mimeDecode
require_once 'Mail/mimeDecode.php';



function print_folders($hm){
	echo "<root>\n";
	echo "<msgType>folders</msgType>\n";
	foreach( $hm->get_folders() as $f) {
		echo (($f->name == 'inbox' || $f->name == 'sentitems' || $f->name == 'deleteditems' || $f->name == 'msnpromo' ||  $f->name == 'bulkmail') 
					? "<box>" : "<folder>"); 
		echo "<name>".$f->name."</name>";
		echo "<unread>".$f->unread."</unread>";
		echo "<total>".$f->visible."</total>";
		echo (($f->name == 'inbox' || $f->name == 'sentitems' || $f->name == 'deleteditems' || $f->name == 'msnpromo' ||  $f->name == 'bulkmail') 
					? "</box>" : "</folder>"); 
		echo "\n";
	}
	echo "</root>\n";
}



/*

{ "players" : [
               { "firstName" : "Ryan", "lastName" : "Campbell", "position" : "S" },
               { "firstName" : "Chris", "lastName" : "Campbell", "position" : "QB" },
               { "firstName" : "Kevin", "lastName" : "Hale", "position" : "DT" }
           ]}
		   
*/

function print_folder_items($hm, $folder)
{
	echo "{ 'JSON:ROOT' : [ \n";

	# laikwan 3/5/2006: fix space in folder url
	$folder = ereg_replace(" ", "%20", $folder);	 

/*print $hm->get_folder_items_raw($folder);
return;
*/
	$emails = array();
	foreach(array_reverse( $hm->get_folder_items($folder) ) as $i)
	{
		$this_email = '';
		$this_email .= "'url':'" . ($i->url) . "',";
		
		$sender = ($folder=='sentitems' ? "To: " . $i->to : $i->from) ;
		$subj = $i->subject ;
		$size = $i->size  ;						
		$date = $i->date  ;						
		if($i->new){
//			$sender = '&lt;b&gt;' . $sender . '&lt;/b&gt;';
//			$subj = '&lt;b&gt;' . $subj . '&lt;/b&gt;';
//			$date = '&lt;b&gt;' . $date . '&lt;/b&gt;';						
//			$size = '&lt;b&gt;' . $size . '&lt;/b&gt;';						
			$sender = '<b>' . $sender . '</b>';
			$subj = '<b>' . $subj . '</b>';
			$date = '<b>' . $date . '</b>';						
			$size = '<b>' . $size . '</b>';						

		}

		$this_email .= "'sender':'" . $sender . "',";
		$this_email .= "'subj':'" . $subj . "',";
		if($folder!='msnpromo' && $folder!='sentitems'){
			$this_email .= "'date':'" . $date . "',";
		}
		$this_email .= "'size':'" . $size . "',";
		$this_email .= "'attachment':'" . ($i->hasattachment) . "' "; // no comma at end

		$emails[] = "{" . $this_email . "}";
	}
	echo implode(",\n", $emails);
	echo "]} \n";
}

function print_folder_items2($hm, $folder)
{
	echo "<root>\n";
	echo "<msgType>emails</msgType>\n";

	# laikwan 3/5/2006: fix space in folder url
	$folder = ereg_replace(" ", "%20", $folder);	 

//print $hm->get_folder_items_raw($folder);
//return;

	//Ignored Mark's change using index and vanilla "for" loop,
	//HTTPMail class now returns array in correct order anyway.
	foreach(array_reverse( $hm->get_folder_items($folder) ) as $i)
	{
		echo "<email>\n";
		echo "<url>" . urlencode($i->url) . "</url>";
		
		$sender = ($folder=='sentitems' ? "To: " . $i->to : $i->from) ;
		$subj = $i->subject ;
		$size = $i->size  ;						
		$date = $i->date  ;						
		if($i->new){
			$sender = '&lt;b&gt;' . $sender . '&lt;/b&gt;';
			$subj = '&lt;b&gt;' . $subj . '&lt;/b&gt;';
			$date = '&lt;b&gt;' . $date . '&lt;/b&gt;';						
			$size = '&lt;b&gt;' . $size . '&lt;/b&gt;';						
		}
		echo "<sender>" . $sender . "</sender>";
		echo "<subj>" . $subj . "</subj>";
		if($folder!='msnpromo' && $folder!='sentitems'){
			echo "<date>" . $date . "</date>";
		}
		echo "<size>" . $i->size . "</size>";
		echo "<attachment>" . $i->hasattachment . "</attachment>";

		print "</email>";
	}
}


function print_email($hm, $url){

	$url = urldecode($url);
	$msg = $hm->get_email($url);
//print($url."\n\n");
//print($msg."\n\n");

	// setup mail_mimedecode
	$params['include_bodies'] = true;
	$params['decode_bodies']  = true;
	$params['decode_headers'] = true;
	$decoder = new Mail_mimeDecode($msg);
	$structure = $decoder->decode($params);


if(array_key_exists('print_r',$_GET)){
	print_r($structure);
	return;
}


	// headers
	$headers = $structure->headers;
	$to = $headers['to'];
	$from = $headers['from'];
	$subject = $headers['subject'];
	$date = $headers['date'];

	echo "<root>\n";
	print "<msgType>conv</msgType>\n";
	print "<title>" . wmcClean_noHtmlForXmlElt($subject) . "</title>\n";
	print "<msg>\n";
	print "<sender>" . wmcClean_noHtmlForXmlElt($from) . "</sender>\n";
	print "<recv_email>" . wmcClean_noHtmlForXmlElt($to) . "</recv_email>\n";
	print "<date_easy>" . wmcClean_noHtmlForXmlElt($date) . "</date_easy>\n";

	// body
	$bodies = wmcMime_getMainBodyOfMsg($structure);
	if(!$bodies){
		print_r($structure);
	}

	$attached = wmcMime_getAttachmentListOfMsg($structure);
	if($attached != 0){
		for($i = 0 ; $i<count($attached); $i++){
			$url		= "getAttachment.php?mail_url=" . urlencode($_GET['mail_url']) . "&attach_num=" . $attached[$i]['attach_num'];
			$filename	= $attached[$i]['desc'];
			print "<attachment>" .
					"<url>" . wmcClean_noHtmlForXmlElt($url) . "</url>" .
					"<filename>" .  wmcClean_noHtmlForXmlElt($filename) . "</filename></attachment>\n" ;
		}
	}

	// avoid script and img src
/*	$body = ereg_replace("src='http", "src='###", $body);	 
	$body = ereg_replace("src=\"http", "src=\"###", $body);	 */

	if($bodies['html'] && $bodies['plain']){
		print "<html>" . wmcClean_cleanDataForXmlElt($bodies['html']) . "</html>";
		print "<alt_txt>" . wmcClean_cleanDataForXmlElt($bodies['plain']) . "</alt_txt>";
	}else if($bodies['html']){
		print "<html>" . wmcClean_cleanDataForXmlElt($bodies['html']) . "</html>";
	}else if($bodies['plain']){
		print "<html>" . wmcClean_cleanDataForXmlElt($bodies['plain']) . "</html>";
	}

	print "</msg>";
	echo "</root>\n";
}


function friendly_folder_name($folder) {
  switch ($folder) {
    case 'inbox': return 'Inbox';
    case 'sentitems': return 'Sent Items';
    case 'deleteditems': return 'Deleted Items';
    case 'msnpromo': return 'MSN Announcements';
	case 'bulkmail': return 'Junk Mail';
    default: return $folder;
  }
}




header("Content-type: text/xml; charset=utf-8");  // must use text/xml for ajax/xmlhttp to work :(
header("Expires: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

@ini_set("session.use_cookies",	 0);
@ini_set("session.use_trans_sid", 1);					 
session_start();

echo "<?xml version=\"1.0\" ?>\n\n";
flush();


if (isset($_SESSION['hail']['hm'])) {

	if (array_key_exists('folder_url',$_GET)){
		// print emails
		print_folder_items($_SESSION['hail']['hm'], $_GET['folder_url']);
	}elseif (array_key_exists('mail_url',$_GET)){
		// print conv
		$_SESSION['hail']['hm']->update_read($_GET['mail_url'], 1);
		print_email($_SESSION['hail']['hm'], $_GET['mail_url']);
	}elseif (array_key_exists('print_folders',$_GET)){
		// print folders on demand
		print_folders($_SESSION['hail']['hm']);
	}else{

		$success = 0;
		if (array_key_exists('mark_read',$_GET)){
			$_SESSION['hail']['hm']->update_read($_GET['url'], 1);
			$success = true;
		}elseif (array_key_exists('mark_unread',$_GET)){
			$_SESSION['hail']['hm']->update_read($_GET['url'], 0);
			$success = true;
		}elseif (array_key_exists('delete_mail',$_GET))	{
			$_SESSION['hail']['hm']->delete_mail($_GET['url']);
			$success = true;
		}	

		echo "<root><status>success</status></root>";
	}

}  else {   // not logged in

	$_SESSION['hail']['sess_user'] = "";
	$_SESSION['hail']['sess_pass'] = "";
	
	echo "<root><status>Your login expired</status></root>";

}
?>
Return current item: Laikwan WMC