<?php

require_once ('vn/web/auth.php');
require_once ('vn/hedera/init.php');

use Vn\Web\Auth;

// Update the visit info using browscap

Auth::getCredentials ();
$conn = new mysqli
(
	 $conf['db']['host']
	,Auth::getUser ()
	,Auth::getPassword ()
	,$conf['db']['schema']
);

if ($conn->connect_error)
	exit ($conn->connect_error);

$result = $conn->query ("SELECT id, agent FROM visit_agent "
	."WHERE version = '0.0' OR platform = 'unknown' OR cookies IS NULL ORDER BY id DESC");

$stmt = $conn->prepare (
	'UPDATE visit_agent SET platform = ?, browser = ?, version = ?, javascript = ?, cookies = ? WHERE id = ?');

if ($result && $stmt)
{
	set_time_limit (0);

	$stmt->bind_param('sssiii'
		,$platform
		,$browser
		,$version
		,$javascript
		,$cookies
		,$id
	);

	while ($row = $result->fetch_assoc ())
	{
		echo sprintf ("Processing row: %d\n", $row['id']);

		$info = get_browser ($row['agent']);
		$platform   = $info->platform;
		$browser    = $info->browser;
		$version    = $info->version;
		$javascript = $info->javascript;
		$cookies    = $info->cookies;
		$id         = $row['id'];
		$stmt->execute ();
	}
}

if ($stmt)
	$stmt->close ();
if ($result)
	$result->free ();
	
if ($conn->errno)
	exit ($conn->error);

$conn->close ();

?>
