<?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://api.bitcoincharts.com/v1/trades.csv?symbol=".$_GET["code"]);
    $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);
}
?>
