$mail = array (
'from' => '',
'to' => '',
'subject' => '',
'parameters' => '',
);
$mtgox_key = '';
$mtgox_secret = base64_decode ('');
$logfile = 'mtgox.log';
$mtgox_error = false;
function mtgox_error ($m, $curl = null) {
global $mtgox_error;
if ($curl) $m .= ': ' . curl_error ($curl);
$mtgox_error = "$m\n";
}
function mtgox_log ($result = null, $amount = null) {
global $code, $msg, $mtgox_error, $logfile;
$m = array (
'time' => time (),
'code' => $code,
'msg' => $msg,
'addr' => $_SERVER ['REMOTE_ADDR'],
);
if (isset ($result)) $m ['result'] = $result;
if (is_numeric ($amount)) $m ['amount'] = $amount;
if (is_string ($mtgox_error)) $m ['error'] = $mtgox_error;
$f = fopen ($logfile, 'at');
if ($f) {
fwrite ($f, serialize ($m) . "\n");
fclose ($f);
}
}
function mtgox0 ($method, $data = array ()) {
global $mtgox_key, $mtgox_secret;
$curl = curl_init ("https://mtgox.com/api/0/$method.php");
if (! $curl) return mtgox_error ('curl_init () failed');
if (! curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true)) return mtgox_error ('curl_setopt (CURLOPT_RETURNTRANSFER) failed', $curl);
if (! curl_setopt ($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; ' . php_uname ('s') . '; PHP/' . phpversion () . ')')) mtgox_error ('curl_setopt (CURLOPT_USERAGENT) failed', $curl);
$r = explode (' ', microtime ());
$data ['nonce'] = $r [1] . substr ((int) (($r [0] + 1) * 1000000), 1);
$r = http_build_query ($data);
if (! curl_setopt ($curl, CURLOPT_POSTFIELDS, $r)) return mtgox_error ('curl_setopt (CURLOPT_POSTFIELDS) failed', $curl);
if (! curl_setopt ($curl, CURLOPT_HTTPHEADER, array (
"Rest-Key: $mtgox_key",
'Rest-Sign: ' . base64_encode (hash_hmac ('sha512', $r, $mtgox_secret, true)),
))) return mtgox_error ('curl_setopt (CURLOPT_HTTPHEADER) failed', $curl);
$r = curl_exec ($curl);
if (curl_errno ($curl)) return mtgox_error ('curl_exec () failed', $curl);
if (! is_string ($r)) return mtgox_error ('CURL did not return data');
$d = json_decode ($r, true);
$e = json_last_error ();
if ($e) return mtgox_error ("json_decode () failed: $e; raw data: $r");
return $d;
}
$msg = array_key_exists ('msg', $_POST) ? trim ($_POST ['msg']) : '';
$code = array_key_exists ('code', $_POST) ? trim ($_POST ['code']) : '';
if (array_key_exists ('more', $_POST)) $code = '';
echo '';
?>