<?php
require_once('tiki-setup.php');
require_once('lib/graph-engine/graph.multiline.php');
require_once('lib/graph-engine/gd.php');
require_once('lib/graph-engine/pdflib.php');

// List of valid functions
$valid = array(
	'abs',
	'acos',
	'acosh',
	'asin',
	'asinh',
	'atan2',
	'atan',
	'atanh',
	'ceil',
	'cos',
	'cosh',
	'deg2rad',
	'exp',
	'expm1',
	'floor',
	'fmod',
	'hypot',
	'log10',
	'log1p',
	'log',
	'max',
	'min',
	'pi',
	'pow',
	'rad2deg',
	'round',
	'sin',
	'sinh',
	'sqrt',
	'tan',
	'tanh'
);

function convert_formula( $formula )
{
	global $valid;

	// Stripping all quotes
	$formula = str_replace( '`', '', $formula );
	$formula = str_replace( '"', '', $formula );
	$formula = str_replace( "'", '', $formula );

	// Make sure only valid functions are used
	preg_match_all( '/([a-z0-9_]+)/i', $formula, $out, PREG_PATTERN_ORDER );
	foreach( $out as $match )
		if( !in_array( strtolower( $match[1] ), $valid ) && $match[1] !== 'x' )
			die( "Invalid function call {$match[1]}" );
	
	// Replace spaces for commas
	$formula = preg_replace( "/\s+/", ', ', $formula );

	$formula = str_replace( 'x', '$x', $formula );

	return create_function('$x', "return $formula;");
}

if( !( is_numeric( $_GET['w'] )
	&& is_numeric( $_GET['h'] )
	&& is_numeric( $_GET['s'] )
	&& $_GET['s'] <= 500 && $_GET['s'] > 0
	&& is_numeric( $_GET['min'] )
	&& is_numeric( $_GET['max'] )
	&& is_array( $_GET['f'] )
	&& $_GET['min'] < $_GET['max'] ) )
	die;

switch( $_GET['t'] )
{
case 'png':
	$renderer = &new GD_GRenderer( $_GET['w'], $_GET['h'] );
	break;
case 'pdf':
	$renderer = &new PDFLib_GRenderer( $_GET['p'], $_GET['o'] );
	break;
default:
	die;
}

$graph = &new MultilineGraphic;
$graph->setTitle( $_GET['title'] );

$size = ($_GET['max'] - $_GET['min']) / $_GET['s'];

$data = array();
foreach( array_values( $_GET['f'] ) as $key=>$formula )
{
	$formula = convert_formula( $formula );
	
	$data['x'] = array();
	$data['y'.$key] = array();
	
	for( $x = $_GET['min']; $_GET['max'] > $x; $x += $size )
	{
		$data['x'][] = $x;
		$data['y'.$key][] = $formula($x);
	}
}

$graph->setData( $data );
$graph->draw( $renderer );

$renderer->httpOutput( "graph.{$_GET['t']}" );
?>
