<?php
// Used a replacement for microtime(1)
function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
// Reads contents of a file 1024 bytes at a time
function phpautotest_readf($filename)
{
$fh = fopen("$filename", 'rb');
while (!feof($fh))
{
$content .= fgets ($fh, 1024);;
}
fclose($fh);
return $content;
}
// Display an error to the user
function phpautotest_show_error($error)
{
// Make sure header and menu are always present before displaying the error
require_once 'includes/header.php';
require_once 'includes/top_menu.php';
echo
"
<center>
<table cellspacing='1' cellpadding='3' bgcolor='white' width = '35%' class = 'box'>
<tr bgcolor='#005896'>
<td align = 'left'><font color='#FFFFFF'><b>error!</b></font></td>
</tr>
<tr bgcolor='#dddddd'>
<td align = 'left'><font color='#000000'>$error</font></td>
</tr>
</table>
</center>
";
require 'includes/footer.php';
exit;
}
// Display a message to the user. It differs from phpautotest_show_error() as it does not stop execution of code
function phpautotest_show_message($message)
{
// Make sure header and menu are always present before displaying the error
require_once 'includes/header.php';
require_once 'includes/top_menu.php';
echo
"
<center>
<table cellspacing='1' cellpadding='3' bgcolor='white' width = '35%' class = 'box'>
<tr bgcolor='#005896'>
<td align = 'left'><font color='#FFFFFF'><b>message!</b></font></td>
</tr>
<tr bgcolor='#dddddd'>
<td align = 'left'><font color='#000000'>$message</font></td>
</tr>
</table>
</center>
";
}
// Stops execution if user is not logged in. Preferably called at the top of a page.
function restrict_access()
{
require('phpautotest_config.php');
if($_COOKIE['logged_in'] != 1)//if(!isset($_SESSION['logged_in']))
{
phpautotest_show_error($phpautotest_error['restricted_area']);
}
}
// Escapes ^.[]$()|*+?{\ from $string so it can be used in pattern matching
function escape_for_regex($string)
{
$escape_this = "^.[]$()|*+?{";
// Replacement of \ with \\ causes problems in the following loop as each replacement results in a \
$string = str_replace('\\', '\\\\', $string);
$escape_this_num = strlen($escape_this);
for($i = 0; $i < $escape_this_num; $i++)
{
$search[$i] = $escape_this[$i];
$replace[$i] = "\\".$escape_this[$i];
}
$string = str_replace($search, $replace, $string);
return $string;
}
// Obtains last value of primary key (usually `id`)
function phpautotest_last_value($table, $key = 'id')
{
$QUERY = <<<END
SELECT
$key
FROM
`$table`
ORDER BY $key DESC LIMIT 1
END;
$result = phpautotest_send_query($QUERY);
$row = mysql_fetch_assoc($result);
if($row[$key])
{
return $row[$key];
}
else
{
return 0;
}
}
// Extracts path from an absolute URL, this path may be used to calculate absolute URL from a relative URL
function phpautotest_return_path($url)
{
// Split the location and remove the last element (filename) from the resulting array. Implode the array to obtain the path sans filename
$sa = explode('/', $url);
array_pop($sa);
$path = implode('/', $sa);
return $path;
}
function phpautotest_writef($filename, $data)
{
if(!($file = fopen($filename, 'w')) || !fwrite($file, "$data"))
{
return "fail";
}
}
// Connect to the database and return a MySQL link identifier on success, or print and error and die on failure
function phpautotest_db_connect()
{
require('phpautotest_config.php');
$db = mysql_connect($phpautotest_db_location, $phpautotest_db_username, $phpautotest_db_password) or die('Error connecting to the database');
mysql_select_db($phpautotest_db_name);
return $db;
}
// Create a database connection and sends the query which is passed as an argument. Returns either a resource identifier, TRUE or FALSE
function phpautotest_send_query($query)
{
$db = phpautotest_db_connect();
$result = mysql_query($query, $db) or die(mysql_error());
return $result;
}
?>