<?php

include ("conf.php");
include ("nfsenutil.php");
session_start();
unset($_SESSION['nfsend']);

function OpenLogFile () {
	global $log_handle;
	global $DEBUG;

	if ( $DEBUG ) {
		$log_handle = fopen("/var/tmp/nfsen-log", "a");
		$_d = date("Y-m-d-H:i:s");
		ReportLog("\n=========================\nPic run at $_d\n");
	} else 
		$log_handle = null;

} // End of OpenLogFile

function CloseLogFile () {
	global $log_handle;

	if ( $log_handle )
		fclose($log_handle);

} // End of CloseLogFile

function ReportLog($message) {
	global $log_handle;

	if ( $log_handle )
		fwrite($log_handle, "$message\n");

} // End of ReportLog

OpenLogFile();

$profileswitch = $_GET['profileswitch'];
$file = $_GET['file'];

header("Content-type: image/gif");

if ( preg_match("/^(.+)\/(.+)/", $profileswitch, $matches) ) {
	$_profilegroup = $matches[1];
	$_profilename  = $matches[2];
} else {
	$fp = fopen("ErrorGraph.gif", 'rb');
	fpassthru($fp);
	return 1;
}

if ( !preg_match("/^[A-Za-z0-9][A-Za-z0-9\-+_]+$/", $_profilename) ) {
	$fp = fopen("ErrorGraph.gif", 'rb');
	fpassthru($fp);
	return 1;
}
if ( $_profilegroup != '.' && !preg_match("/^[A-Za-z0-9][A-Za-z0-9\-+_]+$/", $_profilegroup) ) {
	$fp = fopen("ErrorGraph.gif", 'rb');
	fpassthru($fp);
	return 1;
}

if ( !preg_match("/^[A-Za-z0-9][A-Za-z0-9\-+_]+$/", $file) ) {
	$fp = fopen("ErrorGraph.gif", 'rb');
	fpassthru($fp);
	return 1;
}

$opts = array();
$opts['.silent'] = 1;
$opts['profile'] = $profileswitch;
$opts['picture'] = $file . '.gif';
nfsend_query("@get-picture", $opts, 1);
nfsend_disconnect();
unset($_SESSION['nfsend']);
CloseLogFile();

?>
