<?php
$file = basename($_GET["code"]).".csv";
if (file_exists($file)) {
    header('Location: '.$file);      
} else {
  $fn="cache_".$_GET["code"].".csv";
  
  if(!file_exists($fn) || filemtime($fn) < time() - 60*60*24)
  {
    $page = file_get_contents("http://bitcoincharts.com/charts/chart.json?m=".$_GET["code"]."&i=Daily");
    $page = str_replace("[[", "", $page);
    $page = str_replace("], [", "\n", $page);
    $page = str_replace("]", "", $page);
    
    $f=fopen($fn,'wt');
    fwrite($f, "date,price,...");
    fwrite($f, $page);
    fclose($f); 
  }

  echo file_get_contents($fn);
}
?>
