<?php
# Configuration. Please change to fit your website

# Description. Google and social networks may print this in previews
$description = "Laverne – more than a research assistant- she pulls from the most up to date scientific literature to give you an unbiased review of proteins, pathways, diseases and modifications and how they interact. Use Laverne to then dig further into these relationships by looking at the literature that are the basis for her suggestions. Type in your gene, disease or pathway of interest and press enter to put Laverne to work. Laverne- surprisingly current!";

# Twitter handle
$twitter_handle = "@novusbio";

# Vizit backend server. Usualy it is in the form https://${CLIENT}-ws.biovista.com and 
# is different (CORS) from the hosting page server.
# Comment out to auto guess for test or development setups.
$srvws = 'https://novus-ws.biovista.com';

# Image path URL. If you have setup a $image_path/$uuid.png redirection 
# for images (associates graphs with your site in search engines)
# If not comment it out and suffer the SEO pernalty. Don't put trailing slash.
$image_path = '/vizit-image';

# Vizit URL. The url must contain a vizit graphpanel.
# Comment out to guess based on the current url and the backend server.
# Note: Clients and test pages should comment it out.
#$vizit_url = 'https://www.biovista.com/vizit';

######################################
### That's enough. Edit no more!   ###
######################################

# derive stuff from config
$root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];

if (!isset($srvws)) {
	$srvws = $root;
}

$backend_image_path = "$srvws/ws/rest/v1/vizit/graph-image";

# initialize variables
$remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$http_x_forwarded_for = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
$http_referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

function get_url($url) {
	global $remote_addr;
	global $http_x_forwarded_for;
	global $http_referrer;
	global $http_user_agent;
	$context = stream_context_create(array(
		'http' => array(
			'method' => 'GET',
			'header' => "BV_ORIG_CLIENT: $remote_addr-$http_x_forwarded_for\r\n" .
				"BV_ORIG_USER_AGENT: $http_user_agent\r\n" .
				"BV_ORIG_REFERER: $http_referrer\r\n"
			)
		)
	);
	return file_get_contents($url, false, $context);
}


# first let's check if it is an image
if (isset($image_path) && substr($_SERVER['REQUEST_URI'], 0, strlen($image_path)) === $image_path && isset($_GET['img'])) {
	header('Content-Type: image/png');
	$url = "$backend_image_path/" . $_GET['img'] . '.png';
	echo get_url($url);
}
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<!-- Meta Tag for responsive -->
<meta name="fragment" content="!" />

<?php
# it's not an image. Let's go for vizit graph
$fragment = isset($_GET['_escaped_fragment_']) ? $_GET['_escaped_fragment_'] : '';
if (!$fragment) {
	echo '</head><body></body></html>';
	return;
}
$uuid = preg_replace('/&.*$/', '', preg_replace('/^.*bv_gid=/', '', $fragment));

if (isset($image_path)) {
	$image_path = "$root$image_path";
} else {
	$image_path = "$backend_image_path";
}
$image_url = "$image_path/$uuid.png";
$url = "$srvws/ws/rest/v1/vizit/graph-entities/?" . $fragment;

$entities = get_url($url);
$title = htmlentities("Vizit - Live Graph with $entities", ENT_QUOTES);

echo "<title>$title</title>\n";
# required for pinterest
echo "<meta property='og:type' content='article'/>\n";
# required for linkedin
echo "<meta property='og:title' content='$title'/>\n";
echo "<meta property='og:description' content='$description'/>\n";
echo "<meta property='og:image' content='$image_url'/>\n";
# required for twitter
echo "<meta name='twitter:card' content='summary_large_image'/>\n";
echo "<meta name='twitter:site' content='$twitter_handle'/>\n";
echo "<meta name='twitter:title' content='$title'/>\n";
echo "<meta name='twitter:description' content='$description'/>\n";
echo "<meta property='twitter:image' content='$image_url'/>\n";
?>
</head>

<body>
<?php
if (!isset($vizit_url)) {
	if ($root == $srvws) {
		$vizit_url = "$srvws/vizit/fixedsize.html";
	} else {
		$vizit_url = substr(preg_replace('/(.*)(\?|&)_escaped_fragment_=[^&]+?(&)(.*)/i', '$1$2$4', "$root{$_SERVER['REQUEST_URI']}" . '&'), 0, -1);
	}
}
$url = "$srvws/ws/rest/v1/vizit/html?" . $fragment . "&url=" . urlencode($vizit_url) . "&image_path=" . urlencode($image_path);
echo get_url($url);
?>
</body>
</html>
