<?php
require 'includes/phpautotest_config.php';
require 'includes/phpautotest_functions.php';
require 'includes/HttpClient.class.php';
// Maintain session variables, load them from previous pages serialized data. We already created a record for this page and therefore earlier page is two records back.
session_start(); // This line causes php.exe to crash when testing drupal
$test_id = $_POST['test_id'];
$page_id = $_POST['page_id'];
$test_page_id = $_POST['test_page_id'];
$QUERY = <<<END
SELECT
serialized_variable_dump
FROM
`$phpautotest_table_test_results`
WHERE
`test_id` = '$test_id'
ORDER BY id DESC LIMIT 2
END;
$result = phpautotest_send_query($QUERY);
$row = mysql_fetch_assoc($result);
$row = mysql_fetch_assoc($result);
$variable_dump = unserialize($row['serialized_variable_dump']);
if($variable_dump['_SESSION'])
{
$_SESSION += $variable_dump['_SESSION'];
}
// Values passed via HTTP are encoded and cannot be directly unserialized
// NOTE: Try to use PHP's inbuilt urldecode()
$getdata = str_replace('\"', '"', $_POST['getdata']);
// Extract GET variables from POST array and add it to GET
$_GET += unserialize($getdata);
// Extract data from URL of $url
$url = $_POST['url'];
$parsed = parse_url($url);
ob_start();
$pos = strpos($parsed['path'], '.php');
// Not a PHP file, so do not include
if($pos === FALSE)
{
// No file was included, required later so that we dont unneccessarily record the variable dump
$include = FALSE;
$phpautotest_time_start = getmicrotime(1);
$html_output = HttpClient::quickGet($url);
$phpautotest_time_end = getmicrotime(1);
}
else
{
// If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory. To avoid errors we set include path to that of the target file.
if(file_exists("$_SERVER[DOCUMENT_ROOT]"."$parsed[path]"))
{
$include = TRUE;
$phpautotest_wd = getcwd();
$phpautotest_target_path = phpautotest_return_path($parsed['path']);
$phpautotest_target_wd = "$_SERVER[DOCUMENT_ROOT]".$phpautotest_target_path;
chdir($phpautotest_target_wd);
ob_start();
$phpautotest_time_start = getmicrotime(1);
include("$_SERVER[DOCUMENT_ROOT]"."$parsed[path]");
$phpautotest_time_end = getmicrotime(1);
$html_output = ob_get_contents();
ob_end_clean();
chdir($phpautotest_wd);
}
else
{
$include = FALSE;
$phpautotest_time_start = getmicrotime(1);
$html_output = HttpClient::quickGet($url);
$phpautotest_time_end = getmicrotime(1);
}
}
$html_output = mysql_escape_string($html_output);
// Calculate execution time/time taken to retrieve page
$phpautotest_time_taken = $phpautotest_time_end - $phpautotest_time_start;
// No HTML entities should be stored in the database
$html_output = htmlentities($html_output);
// Store HTML output in the database
$QUERY = <<<END
UPDATE
`$phpautotest_table_test_results`
SET
`html_output` = '$html_output',
`time_taken` = $phpautotest_time_taken
WHERE
`id` = $test_page_id
END;
phpautotest_send_query($QUERY);
// Serialize variable dump and store it in the database IF an include took place
if($pos != FALSE)
{
$serialized_variable_dump = serialize(get_defined_vars());
$serialized_variable_dump = mysql_escape_string($serialized_variable_dump);
$QUERY = <<<END
UPDATE
`$phpautotest_table_test_results`
SET
`serialized_variable_dump` = '$serialized_variable_dump'
WHERE
`id` = $test_page_id
END;
phpautotest_send_query($QUERY);
}
echo $success_text;
?>