Location: PHPKode > projects > phpautotest > phpautotest_backend.php
<?php
require 'includes/phpautotest_config.php';
require 'includes/phpautotest_functions.php';
require 'includes/HttpClient.class.php';

// Since serialized form data (postdata) has been passed we are dealing with POST variables
if($_POST['postdata'])
{
	// Values passed via HTTP are encoded and cannot be directly unserialized
	// NOTE: Try to use PHP's inbuilt urldecode()
	$postdata = str_replace('\"', '"', $_POST['postdata']);
	
	// Unserialize and obtain the array
	$pd = unserialize($postdata);
	
	// Store elements in the post array
	$_POST += $pd['elements'];
	
	// Base64 decode action and plocation URL's
	$plocation = base64_decode($pd['plocation']);
	$action = base64_decode($pd['action']);
	
	// Obtain the correct action, we need to prefix 'http://.....' if it does not already exist
	$pos = strpos($action, 'http://');
	
	// === is used as strpos() can return 0 which may be mistaken for FALSE
	if($pos === FALSE)
	{
		// action is relative path, calculate absolute
		
		$path = phpautotest_return_path($plocation);
		
		// Prefix this to the action url to obtain absolute path
		$url = $path.'/'.$action;
	}
	else
	{
		// action is absolute, use directly
		$url = $action;
	}
}

// Page change is either due to simple hyperlink or GET form
if($_POST['getdata'])
{
	// Values passed via HTTP are encoded and cannot be directly unserialized
	$getdata = str_replace('\"', '"', $_POST['getdata']);
	
	// Unserialize and obtain the array
	$gd = unserialize($getdata);

	$plocation = $gd['plocation'];	
	
	// Base64 decode the URL
	$url = base64_decode($plocation);
}

// Extract data from URL of $url and store it in $_GET
$parsed = parse_url($url);
parse_str($parsed['query'], $vals);
$_GET += $vals;	

// Delete HTTP data belonging to phpautotest
unset($_GET['getdata']);
unset($_GET['postdata']);

// Serialize all GET data and store it in POST array (since we are making a post request)
$_POST['getdata'] = serialize($_GET);

// Send url info too
$_POST['url'] = $url;

## Create `page` record to store data
$serialized_post_vars = serialize($_POST);
$serialized_post_vars = mysql_escape_string($serialized_post_vars);

$case_id = phpautotest_last_value($phpautotest_table_cases, 'case_id');
$QUERY = <<<END

	INSERT INTO
		`$phpautotest_table_page`
		(`id`, `case_id`, `page_location`, `serialized_post_vars`)
	VALUES
		('?', $case_id, '$url', '$serialized_post_vars')
END;

$result = phpautotest_send_query($QUERY);

## Call record_page.php which will in turn call target page
$parsed = parse_url($url);

$pos = strpos($parsed['path'], '.php');

// Not a PHP file, so do retrieve
if($pos === FALSE)
{
	// Storage code is not directly placed here because if execution record_page.php is stopped we have to record output again
	$retrieve = TRUE;
	$rco = HttpClient::quickGet($url);
}
else
{
	$retrieve = FALSE;
	$rco = HttpClient::quickPost($phpautotest_install_path.'record_page.php', $_POST);	

	// Recording failed due to exit/die in target page, buffered HTML is output directly into $rco
	if($rco != $success_text)
	{
		$retrieve = TRUE;
	}
}

if($retrieve)
{
	// Obtain primary key of the current page
	$id = phpautotest_last_value($phpautotest_table_page);

	$rco = mysql_escape_string($rco);
	
	// No HTML entities should be stored in the database	
	$rco = htmlentities($rco);
	
	// Store HTML output in the database
	$QUERY = <<<END

		UPDATE
			`$phpautotest_table_page`
		SET
			`html_output` = '$rco',
			`method` = 'R'
		WHERE
			`id` = $id
END;

	phpautotest_send_query($QUERY);
}
?>
Return current item: phpautotest