<?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
$case_id = phpautotest_last_value($phpautotest_table_cases, 'case_id');
$QUERY = <<<END
SELECT
serialized_variable_dump
FROM
`$phpautotest_table_page`
WHERE
`case_id` = $case_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);
// 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();
include("$_SERVER[DOCUMENT_ROOT]"."$parsed[path]");
$html_output = ob_get_contents();
ob_end_clean();
chdir($phpautotest_wd);
}
else
{
$include = FALSE;
$html_output = HttpClient::quickGet($url);
}
// Obtain primary key of the current page
$id = phpautotest_last_value($phpautotest_table_page);
$html_output = mysql_escape_string($html_output);
// 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_page`
SET
`html_output` = '$html_output'
WHERE
`id` = $id
END;
phpautotest_send_query($QUERY);
// Serialize variable dump and store it in the database IF an include took place
if($include == TRUE)
{
// Unset phpautotest defined variables we dont want in the dump
unset($html_output);
unset($url);
unset($pos);
unset($parsed);
unset($_POST['url']);
unset($getdata);
unset($_POST['getdata']);
unset($QUERY);
unset($html_output);
unset($variable_dump);
// phpautotest_config.php variables, some of these are used in the code that follows
unset($phpautotest_db_location);
unset($phpautotest_db_username);
unset($phpautotest_db_password);
unset($phpautotest_db_name);
unset($phpautotest_table_prefix);
unset($phpautotest_table_cases);
//unset($phpautotest_table_page);
unset($phpautotest_table_test_results);
unset($phpautotest_password);
//unset($success_text);
unset($phpautotest_install_path);
unset($phpautotest_error);
unset($phpautotest_message);
unset($php_test_code_template);
unset($phpautotest_wd);
unset($phpautotest_target_path);
unset($phpautotest_target_wd);
$variable_dump = get_defined_vars();
// The following variables cannot be unset and must be removed this way
unset($variable_dump['phpautotest_table_page']);
unset($variable_dump['success_text']);
$serialized_variable_dump = serialize($variable_dump);
$serialized_variable_dump = mysql_escape_string($serialized_variable_dump);
$QUERY = <<<END
UPDATE
`$phpautotest_table_page`
SET
`serialized_variable_dump` = '$serialized_variable_dump'
WHERE
`id` = $id
END;
phpautotest_send_query($QUERY);
}
echo $success_text;
?>