<?php if( !array_key_exists('method', $_REQUEST) || $_REQUEST['method'] != 'GetAttachmentData') : ?>
<pre>

This script allows you to directly test the backend libraries via a browser
Usage:
method: = method to test
Common tests:
	
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=GetFolderList">method=GetFolderList</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=GetMessageList&arg1=INBOX&arg2=0">GetMessageList&arg1=INBOX&arg2=0</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=GetMessageList&arg1=INBOX.Dropbox&arg2=0">GetMessageList&arg1=INBOX.Dropbox&arg2=0</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=StatMessage&arg1=INBOX&arg2=166">StatMessage&arg1=INBOX&arg2=166</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=GetMessage&arg1=INBOX&arg2=168&arg3=64&arg4=0">GetMessage&arg1=INBOX&arg2=168&arg3=64&arg4=0</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=AllGetMessage&arg1=INBOX&arg2=0&arg3=128&arg4=0">AllGetMessage&arg1=INBOX&arg2=0&arg3=128&arg4=0</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=AllGetMessage&arg1=INBOX.Dropbox&arg2=0&arg3=32&arg4=0">AllGetMessage&arg1=INBOX.Dropbox&arg2=70806&arg3=32&arg4=0</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=DeleteMessage&arg1=INBOX.Dropbox&arg2=70985">DeleteMessage&arg1=INBOX.Dropbox&arg2=70985</a> 
<a href="testbackend.php?backend=imap&classname=BackendIMAP&method=GetAttachmentData&arg1=INBOX.Dropbox:70888:1">GetAttachmentData&arg1=INBOX.Dropbox:70888:1</a> 

Abook
<a href="testbackend.php?backend=atmailabook&classname=BackendAtmailAbook&method=GetFolderList">method=GetFolderList</a> 
<a href="testbackend.php?backend=atmailabook&classname=BackendAtmailAbook&method=GetMessageList&arg1=root&arg2=0">GetMessageList&arg1=root&arg2=0</a> 
<a href="testbackend.php?backend=atmailabook&classname=BackendAtmailAbook&method=GetMessage&arg1=root&arg2=2518">GetMessage&arg1=root&arg2=2518</a> 

Combined
<a href="testbackend.php?classname=BackendCombined&method=GetHierarchy">classname=BackendCombined&method=method=GetHierarchy</a> 
<a href="testbackend.php?classname=BackendCombined&method=GetMessageList&arg1=s/&arg2=0">classname=BackendCombined&method=GetMessageList&arg1=INBOX&arg2=0</a> 

<?php
$requiredArgs = array('classname', 'method');
foreach( $requiredArgs as $requiredArg )
	if( !array_key_exists($requiredArg, $_REQUEST) )
		exit('Required argument missing: ' . $requiredArg );
echo time() . "\n";
print_r($_REQUEST);
endif;

include_once("atmaildefs.php");
include_once("config.php");
include_once("proto.php");
include_once("request.php");
include_once("debug.php");
include_once("compat.php");
//include_once("version.php");
//require_once( 'backend/' . $_REQUEST['backend'].'.php');

// Load our backend driver
$backend_dir = opendir(BASE_PATH . "/backend");
while($entry = readdir($backend_dir)) {
    if(substr($entry,0,1) == "." || substr($entry,-3) != "php")
        continue;

	if( $entry == "maildir.php" )
        continue;

	if (!function_exists("mapi_logon") && ($entry == "ics.php"))
        continue;

    include_once(BASE_PATH . "/backend/" . $entry);
}

//$BACKEND_PROVIDER = "BackendICS";
//$BACKEND_PROVIDER = "BackendIMAP";
//$BACKEND_PROVIDER = "BackendAtmailAbook";
//$BACKEND_PROVIDER = "BackendCombined";
$BACKEND_PROVIDER = $_REQUEST['classname'];

if( !array_key_exists('method', $_REQUEST) )
	return;

$testclassinstance = new $BACKEND_PROVIDER(${$BACKEND_PROVIDER."_config"});
//print_r($testclassinstance);
$logonSuccess = $testclassinstance->Logon('1@1.com', 'BrowserTestClient', '11111');
if( !$logonSuccess )
	die('logon failed');
	
$testclassinstance->Setup('1@1.com', 'BrowserTestClient', '');

if( $_REQUEST['method'] == 'GetAttachmentData')
{
	
	header("Content-Type: application/octet-stream");
	$result = $testclassinstance->$_REQUEST['method']( isset($_REQUEST['arg1'])?$_REQUEST['arg1']:null, isset($_REQUEST['arg2'])?$_REQUEST['arg2']:null, isset($_REQUEST['arg3'])?$_REQUEST['arg3']:null, isset($_REQUEST['arg4'])?$_REQUEST['arg4']:null);
	
	
}
elseif( $_REQUEST['method'] == 'AllGetMessage' )
{
	
	$result = $testclassinstance->GetMessageList($_REQUEST['arg1'], $_REQUEST['arg2']);
	
	//echo print_r( $result, true );
	//echo widePrint( $result );
	$statArray = array();
	$messageArray = array();
	foreach( $result as $messageInfo )
	{
		
		$statArray[] = $testclassinstance->StatMessage($_REQUEST['arg1'], $messageInfo['id']);
		
		//echo print_r( $stat, true );
		//echo widePrint($stat);
		$row = (Array)$testclassinstance->GetMessage($_REQUEST['arg1'], $messageInfo['id'], $_REQUEST['arg3'], $_REQUEST['arg4']); 
		$row['UID'] = $messageInfo['id'];
		$messageArray[] = $row;
		
		//echo print_r( $message, true );
	   // echo widePrint($message);
		
	}
	//echo print_r($statArray, true);
	//echo widePrint($statArray);
	//echo print_r($messageArray[0],true);
	echo htmlentities(widePrint($messageArray, array('UID' => -5, 'from' => -20, 'subject' => -40, 'body' => -80)),ENT_QUOTES,'UTF-8');
}
else
{

	$result = $testclassinstance->$_REQUEST['method']( isset($_REQUEST['arg1'])?$_REQUEST['arg1']:null, isset($_REQUEST['arg2'])?$_REQUEST['arg2']:null, isset($_REQUEST['arg3'])?$_REQUEST['arg3']:null, isset($_REQUEST['arg4'])?$_REQUEST['arg4']:null);
	//echo htmlentities( print_r($result, true) );
	//echo print_r($result, true);
	echo 'about to output result' . "<br />";
	print_r($result);
	//echo widePrint($result);
	
}

if( $_REQUEST['method'] != 'GetAttachmentData') :
?>

done
</pre>
<?                                               
endif;

function widePrint( $data, $keyWidthArray = null )
{
	debugLog( __METHOD__ . ' #' . __LINE__ . ': \$data' . print_r($data, true) );
	
	if( !is_array($data) || count($data) == 0 )
		return;
	$defaultOutputWidth = 160;
	if( $keyWidthArray == null )
	{
		
		$keyWidthArray = array();
		$dataKeys = array_keys($data);
		$keys = array_keys($data[$dataKeys[0]]);
		$colWidth = floor($defaultOutputWidth/count($keys));
		foreach( $keys as $k )
			$keyWidthArray[$k] = '-' . $colWidth;
		
	}
	$keys = array_keys($keyWidthArray);
	$lineFormat = '';
	foreach( $keyWidthArray as $k => &$v )
		$lineFormat .= " %" . $v . "." . (strpos($v,'-')!==false?substr($v,1):$v) . "s |";
	$result = call_user_func_array('sprintf', array_merge((array)$lineFormat, $keys)) . "\n";
	foreach( $data as &$row )
	{

	    $rowSpecificFields = array();
		foreach( $keys as &$k)
		{
			if( is_string($row[$k]) || is_numeric($row[$k]) )
			{
				//nothing to do
			}	
			elseif( is_array($row[$k]) && isset($row[$k][0]) && is_string($row[$k][0]) )
				$row[$k] = implode(', ', (Array)$row[$k]);
			elseif( is_object($row[$k]) && is_array((Array)$row[$k]) && is_string($row[$k][0]) )
				$row[$k] = implode(', ', (Array)$row[$k]);
			else
				$row[$k] = print_r($row[$k],true);
			$rowSpecificFields[] = str_replace(array("\n","\r"),'',$row[$k]);
		}
		$result .= call_user_func_array('sprintf', array_merge((array)$lineFormat, $rowSpecificFields)) . "\n";
	
	}
	return $result;

}

