Beispiel #1
0
function get_funds_graph_data()
{
    $btc = array();
    $fiat = array();
    $query = "\n        SELECT\n            req_type, amount, curr_type, " . sql_format_date('timest') . " AS timest2\n        FROM\n            requests\n        WHERE\n            status != 'CANCEL'\n        ORDER BY\n            timest;\n    ";
    $result = do_query($query);
    $btc_sum = 0;
    $fiat_sum = 0;
    while ($row = mysql_fetch_array($result)) {
        $req_type = $row['req_type'];
        $amount = $row['amount'];
        $curr_type = $row['curr_type'];
        $timest = $row['timest2'];
        if ($req_type == 'WITHDR') {
            $amount = gmp_mul(-1, $amount);
        }
        if ($curr_type == 'BTC') {
            $btc_sum = gmp_add($btc_sum, $amount);
            $btc[$timest] = internal_to_numstr($btc_sum);
        } else {
            $fiat_sum = gmp_add($fiat_sum, $amount);
            $fiat[$timest] = internal_to_numstr($fiat_sum);
        }
    }
    return array($btc, $fiat);
}
Beispiel #2
0
function show_mini_orderbook_table_row($id, $curr, $price, $have, $want, $this_fiat, $this_btc, $sum_fiat, $sum_btc, $mine)
{
    global $minimum_btc_amount, $minimum_fiat_amount;
    $this_btc_str = internal_to_numstr($this_btc, BTC_PRECISION);
    $this_fiat_str = internal_to_numstr($this_fiat, FIAT_PRECISION);
    if (string_is_zero($this_btc_str) || string_is_zero($this_fiat_str) || gmp_cmp($this_btc, $minimum_btc_amount) < 0 || gmp_cmp($this_fiat, $minimum_fiat_amount) < 0) {
        return;
    }
    if ($mine) {
        $sum_btc_str = internal_to_numstr($sum_btc, BTC_PRECISION);
        $sum_fiat_str = internal_to_numstr($sum_fiat, FIAT_PRECISION);
        active_table_row("me", "?page=view_order&orderid={$mine}");
        echo "<td class='right'>{$price}</td>\n";
        echo "<td class='right'>{$this_btc_str}</td>\n";
        echo "<td class='right'>{$this_fiat_str}</td>\n";
        echo "<td class='right'>{$sum_btc_str}</td>\n";
        echo "<td class='right'>{$sum_fiat_str}</td>\n";
        echo "</tr>\n";
    } else {
        echo "<tr>\n";
        echo "<td class='right'>{$price}</td>\n";
        show_mini_orderbook_table_cell($id . 't', $curr, $price, $have, $want, $this_fiat, $this_btc);
        show_mini_orderbook_table_cell($id . 'c', $curr, $price, $have, $want, $sum_fiat, $sum_btc);
    }
    echo "</tr>\n";
}
function redeemVoucher()
{
    $voucher = post('voucher', '-');
    try {
        get_lock("redeem_voucher", 2);
        list($currency, $amount) = redeem_voucher($voucher);
        release_lock("redeem_voucher");
    } catch (Exception $e) {
        release_lock("redeem_voucher");
        throw new Exception($e->getMessage());
    }
    return array("status" => "OK", "currency" => $currency, "amount" => internal_to_numstr($amount));
}
Beispiel #4
0
function show_withdrawals()
{
    echo "<div class='content_box'>\n";
    echo "<h3>" . _("Withdraw requests") . "</h3>\n";
    $result = do_query("\n        SELECT requests.reqid as reqid, uid, amount, " . sql_format_date("timest") . " as timest, name, bank, acc_num, sort_code\n        FROM requests\n        JOIN uk_requests\n        ON uk_requests.reqid = requests.reqid\n        WHERE req_type = 'WITHDR'\n          AND curr_type = '" . CURRENCY . "'\n          AND status = 'VERIFY'");
    $first = true;
    while ($row = mysql_fetch_assoc($result)) {
        if ($first) {
            $first = false;
            echo "<table class='display_data'>\n";
            echo "<tr>";
            // echo "<th>User</th>";
            echo "<th>" . CURRENCY . "</th>";
            echo "<th>Time</th>";
            echo "<th>Name</th>";
            echo "<th>Bank</th>";
            echo "<th>Account#</th>";
            echo "<th>BSB</th>";
            echo "</tr>\n";
        }
        $reqid = $row['reqid'];
        // $uid = $row['uid'];
        $amount = internal_to_numstr($row['amount']);
        $timest = $row['timest'];
        $name = $row['name'];
        $bank = $row['bank'];
        $acc_num = $row['acc_num'];
        $sort_code = $row['sort_code'];
        echo "<tr>";
        echo active_table_row("me", "?page=view_request&reqid={$reqid}&show_finish");
        // echo "<td>$uid</td>";
        echo "<td>{$amount}</td>";
        echo "<td>{$timest}</td>";
        echo "<td>{$name}</td>";
        echo "<td>{$bank}</td>";
        echo "<td>{$acc_num}</td>";
        echo "<td>{$sort_code}</td>";
        echo "</tr>\n";
    }
    if ($first) {
        echo "<p>No pending withdrawals.</p>\n";
    } else {
        echo "</table>\n";
    }
    echo "</div>\n";
}
Beispiel #5
0
function get_orders()
{
    global $is_logged_in;
    $result = do_query("\n        SELECT\n            orderid, initial_amount, amount, type, initial_want_amount, want_amount, want_type\n        FROM\n            orderbook\n        WHERE\n            status = 'OPEN'\n        AND\n            uid = {$is_logged_in}\n    ");
    $orders = array();
    while ($row = mysql_fetch_array($result)) {
        $orderid = $row['orderid'];
        $have_amount = $row['amount'];
        $have_currency = $row['type'];
        $want_amount = $row['want_amount'];
        $want_currency = $row['want_type'];
        if ($have_currency == 'BTC') {
            $text = sprintf("%s %s %s %s %s %s", _("Sell"), internal_to_numstr($have_amount, BTC_PRECISION), $have_currency, _("for"), internal_to_numstr($want_amount, FIAT_PRECISION), $want_currency);
        } else {
            $text = sprintf("%s %s %s %s %s %s", _("Buy"), internal_to_numstr($want_amount, BTC_PRECISION), $want_currency, _("for"), internal_to_numstr($have_amount, FIAT_PRECISION), $have_currency);
        }
        array_push($orders, array('orderid' => $orderid, 'text' => $text, 'have_amount' => internal_to_numstr($have_amount), 'have_currency' => $row['type'], 'want_amount' => internal_to_numstr($want_amount), 'want_currency' => $want_currency));
    }
    return $orders;
}
Beispiel #6
0
function fetch_depth($rate_query, $field, $have, $want)
{
    $ret = array();
    $minimum_btc_amount = numstr_to_internal(MINIMUM_BTC_AMOUNT);
    $minimum_fiat_amount = numstr_to_internal(MINIMUM_FIAT_AMOUNT);
    if ($have == "BTC") {
        $big_enough = "amount >= {$minimum_btc_amount}  AND want_amount >= {$minimum_fiat_amount}";
    } else {
        $big_enough = "amount >= {$minimum_fiat_amount} AND want_amount >= {$minimum_btc_amount} ";
    }
    $query = "\n    SELECT\n        {$rate_query} AS rate,\n        {$field} as amount\n    FROM\n        orderbook\n    WHERE\n        type='{$have}'\n        AND want_type='{$want}'\n        AND status='OPEN'\n        AND {$big_enough}\n    ORDER BY\n        rate DESC\n    ";
    $result = do_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $amount = internal_to_numstr($row['amount']);
        $rate = $row['rate'];
        //bitcoincharts uses NUMERIC(18,8)
        if ($rate < 1000000000) {
            array_push($ret, "[{$rate}, {$amount}]");
        }
    }
    return implode($ret, ", ");
}
function bitcoin_withdraw($uid, $amount, $curr_type, &$voucher_code, &$reqid)
{
    $voucher = isset($_POST['voucher']);
    if ($voucher) {
        syslog(LOG_NOTICE, "address=voucher");
        $query = "\n            INSERT INTO requests (req_type, uid, amount, curr_type)\n            VALUES ('WITHDR', '{$uid}', '{$amount}', '{$curr_type}');\n        ";
    } else {
        $addy = post('address');
        try {
            $validaddy = bitcoin_validate_address($addy);
        } catch (Exception $e) {
            if ($e->getMessage() != 'Unable to connect.') {
                throw $e;
            }
            throw new Problem(_("Sorry..."), _("We are currently experiencing trouble connecting to the Bitcoin network and so cannot verify that you entered a valid Bitcoin address.") . "</p><p>" . _("Your withdrawal request has been cancelled.") . "</p><p>" . _("Please try again in a few minutes."));
        }
        if (!$validaddy['isvalid']) {
            throw new Problem(_('Bitcoin says no'), _('That address you supplied was invalid.'));
        }
        syslog(LOG_NOTICE, "address={$addy}");
        $we_have = bitcoin_get_balance("*", 0);
        if (gmp_cmp($we_have, $amount) <= 0) {
            $message = sprintf(_("User %s is asking to withdraw %s BTC.  We only have %s BTC."), $uid, internal_to_numstr($amount, BTC_PRECISION), internal_to_numstr($we_have, BTC_PRECISION));
            email_tech(_("Exchange Wallet Balance is Too Low"), $message);
        }
        $query = "\n            INSERT INTO requests (req_type, uid, amount, curr_type)\n            VALUES ('WITHDR', '{$uid}', '{$amount}', '{$curr_type}');\n        ";
    }
    endlog();
    do_query($query);
    $reqid = mysql_insert_id();
    if ($voucher) {
        $voucher_code = store_new_bitcoin_voucher_code($reqid);
    } else {
        $query = "\n            INSERT INTO bitcoin_requests (reqid, addy)\n            VALUES ('{$reqid}', '{$addy}');\n        ";
        do_query($query);
    }
}
Beispiel #8
0
function show_users()
{
    $omit_zero_balances = true;
    $omit_very_low_balances = true;
    echo "<div class='content_box'>\n";
    echo "<h3>" . _("Users") . "</h3>\n";
    $query = "\n    SELECT\n        u.uid, oidlogin, is_admin, timest, a.amount as fiat, b.amount as btc\n    FROM\n        users as u\n    JOIN\n        purses as a\n    ON\n        a.uid = u.uid AND a.type = '" . CURRENCY . "'\n    JOIN\n        purses as b\n    ON\n        b.uid = u.uid AND b.type = 'BTC'\n    ORDER BY\n        is_admin DESC, u.uid;\n    ";
    $result = do_query($query);
    $fiat_total = $c_fiat_total = $t_fiat_total = '0';
    $btc_total = $c_btc_total = $t_btc_total = '0';
    $first = true;
    $count_users = $count_funded_users = $count_low_balance_users = $count_shown_users = 0;
    // omit users who don't have more than just least-significant-digit amounts of anything
    $tiny_fiat = pow(10, 8 - FIAT_PRECISION) * 10;
    $tiny_btc = pow(10, 8 - BTC_PRECISION) * 10;
    while ($row = mysql_fetch_assoc($result)) {
        $uid = $row['uid'];
        $oidlogin = $row['oidlogin'];
        $is_admin = $row['is_admin'];
        $timest = $row['timest'];
        $fiat = $row['fiat'];
        $btc = $row['btc'];
        $committed = fetch_committed_balances($uid);
        $c_fiat = $committed[CURRENCY];
        $c_btc = $committed['BTC'];
        $t_fiat = gmp_add($fiat, $c_fiat);
        $t_btc = gmp_add($btc, $c_btc);
        if ($uid == '1') {
            $uid = "fees";
        } else {
            $count_users++;
        }
        if ($omit_zero_balances && $fiat == 0 && $c_fiat == 0 && $btc == 0 && $c_btc == 0) {
            continue;
        }
        if ($first) {
            $first = false;
            echo "<table class='display_data'>\n";
            show_users_header();
        }
        $fiat_total = gmp_add($fiat_total, $fiat);
        $c_fiat_total = gmp_add($c_fiat_total, $c_fiat);
        $t_fiat_total = gmp_add($t_fiat_total, $t_fiat);
        $btc_total = gmp_add($btc_total, $btc);
        $c_btc_total = gmp_add($c_btc_total, $c_btc);
        $t_btc_total = gmp_add($t_btc_total, $t_btc);
        if ($uid != 'fees') {
            $count_funded_users++;
            if ($fiat < $tiny_fiat && $c_fiat < $tiny_fiat && $btc < $tiny_btc && $c_btc < $tiny_btc) {
                $count_low_balance_users++;
                if ($omit_very_low_balances) {
                    continue;
                }
            }
            $count_shown_users++;
        }
        if ($uid == 'fees') {
            $url = "?page=commission";
        } else {
            $url = "?page=statement&user={$uid}";
        }
        if ($is_admin) {
            active_table_row('me', $url);
        } else {
            active_table_row('active', $url);
        }
        echo "<td>{$uid}</td>";
        //      echo "<td>$oidlogin</td>";
        echo "<td class='right'>", internal_to_numstr($fiat, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($c_fiat, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($t_fiat, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($btc, BTC_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($c_btc, BTC_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($t_btc, BTC_PRECISION), "</td>";
        //      echo "<td>$timest</td>";
        echo "</tr>\n";
        if (!($count_shown_users % RESHOW_COLUMN_HEADINGS_AFTER_ROWS)) {
            show_users_header();
        }
    }
    if (!$first) {
        echo "<tr><td></td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td></tr>\n";
        active_table_row('me', "?page=statement&user=all");
        echo "<td></td>";
        echo "<td class='right'>", internal_to_numstr($fiat_total, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($c_fiat_total, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($t_fiat_total, FIAT_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($btc_total, BTC_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($c_btc_total, BTC_PRECISION), "</td>";
        echo "<td class='right'>", internal_to_numstr($t_btc_total, BTC_PRECISION), "</td>";
        echo "</tr>\n";
        echo "</table>\n";
        echo "<p>" . _("Admins are shown in bold type, and at the top of the table.") . "</p>\n";
    }
    echo "<p>" . sprintf(_("There are %s users with funds, and %s in total."), $count_funded_users, $count_users) . "</p>\n";
    if ($omit_very_low_balances && $count_low_balance_users) {
        echo "<p>" . sprintf(_("%d user(s) have very low balances, and aren't shown above."), $count_low_balance_users) . "</p>\n";
    }
    $balance0 = bitcoin_get_balance('*', 0);
    $balance1 = bitcoin_get_balance('*', 1);
    $balance = bitcoin_get_balance('', 0);
    $unconfirmed = gmp_sub($balance0, $balance1);
    echo "<p>" . sprintf(_("The Bitcoin wallet has %s BTC"), internal_to_numstr($balance0, BTC_PRECISION));
    if (gmp_cmp($unconfirmed, 0) != 0) {
        printf(_(", %s BTC of which currently has 0 confirmations"), internal_to_numstr($unconfirmed, BTC_PRECISION));
    }
    echo ".<br/></p>\n";
    if ($balance0 == $balance) {
        $balance = $balance0;
    } else {
        $pending = gmp_sub($balance0, $balance);
        echo "<p>" . sprintf(_("The main wallet account has %s BTC; other accounts have %s BTC awaiting confirmations."), internal_to_numstr($balance, BTC_PRECISION), internal_to_numstr($pending, BTC_PRECISION)) . "</p>";
    }
    // take off the amount that's waiting to be withdrawn.  it's in the wallet, but not in user accounts
    $pending_withdrawal = btc_pending_withdrawal();
    $balance = gmp_sub($balance, $pending_withdrawal);
    if ($pending_withdrawal) {
        echo "<p>" . sprintf(_("There are pending BTC withdrawals worth %s BTC, which will reduce the wallet balance to %s BTC."), internal_to_numstr($pending_withdrawal, BTC_PRECISION), internal_to_numstr($balance, BTC_PRECISION)) . "</p>";
    }
    $diff = gmp_sub($t_btc_total, $balance);
    $cmp = gmp_cmp($diff, 0);
    if ($cmp == 0) {
        echo "<p>" . _("That's the exact right amount.") . "</p>\n";
    } else {
        if ($cmp > 0) {
            echo "<p>" . sprintf(_("That's %s BTC less than is on deposit."), internal_to_numstr($diff, BTC_PRECISION)) . "</p>\n";
        } else {
            echo "<p>" . sprintf(_("That's %s BTC more than is on deposit"), internal_to_numstr(gmp_mul("-1", $diff), BTC_PRECISION)) . "</p>\n";
        }
    }
    echo "</div>\n";
}
Beispiel #9
0
    echo $_SESSION['csrf_token'];
    ?>
" />
            <input type='submit' value='Submit' />
        </form>
    </p>
<?php 
}
if (isset($_POST['code'])) {
    echo "<div class='content_box'>\n";
    echo "<h3>" . _("Deposit Voucher") . "</h3>\n";
    $code = post('code', '-');
    try {
        get_lock("redeem_voucher", 2);
        list($curr_type, $amount) = redeem_voucher($code);
        echo "<p><strong>" . sprintf(_("%s has been credited to your account."), internal_to_numstr($amount) . " {$curr_type}") . "</strong></p>\n";
        echo "<p>" . _("got any more?") . "</p>\n";
        show_deposit_voucher_form($code);
    } catch (Exception $e) {
        $message = $e->getMessage();
        echo "<p>" . _("error") . ": {$message}</p>\n";
        echo "<p>" . _("try again?") . "</p>\n";
        show_deposit_voucher_form($code);
    }
    release_lock("redeem_voucher");
    echo "</div>\n";
} else {
    try {
        $addy = bitcoin_get_account_address((string) $is_logged_in);
    } catch (Exception $e) {
        if ($e->getMessage() != 'Unable to connect.') {
Beispiel #10
0
function show_statement($userid, $interval = 'forever', $from_zero, $deposit_btc, $withdraw_btc, $deposit_fiat, $withdraw_fiat, $buy, $sell)
{
    global $is_logged_in, $is_admin;
    if ($userid) {
        $specified_user = true;
    } else {
        $specified_user = false;
        $userid = $is_logged_in;
    }
    $show_increments = false;
    $show_prices = true;
    echo "<div class='content_box'>\n";
    $all_users = $userid == 'all';
    $deposit_address = $create_timestamp = false;
    if ($all_users) {
        echo "<h3>" . _("Statement for All Users") . "</h3>\n";
        $check_stuff = "";
    } else {
        $openid = get_openid_for_user($userid);
        echo "<h3>" . sprintf(_("Statement for UID %s"), $userid) . "</h3>\n";
        $check_stuff = "uid='{$userid}' AND ";
        if ($is_admin) {
            $create_timestamp = get_account_creation_timest_for_user($userid);
            try {
                $deposit_address = bitcoin_get_account_address($userid);
            } catch (Exception $e) {
            }
        }
    }
    echo "<form method='get'>\n" . "<p>\n" . _("Show entries from ") . "\n" . "<input type='hidden' name='page' value='statement' />\n";
    echo "<select onChange='this.form.submit()' name='interval'>\n";
    foreach (array('4 hour' => _('the last 4 hours'), '12 hour' => _('the last 12 hours'), '1 day' => _('the last 24 hours'), '3 day' => _('the last 3 days'), '1 week' => _('the last 7 days'), '1 month' => _('the last month'), '2 month' => _('the last 2 months'), '3 month' => _('the last 3 months'), '6 month' => _('the last 6 months'), '1 year' => _('the last year'), 'forever' => _('forever'), 'pending' => _('still pending')) as $key => $text) {
        printf("<option %s value='%s'>%s</option>\n", $interval == $key ? "selected='selected'" : "", $key, $text);
    }
    echo "</select>\n";
    if ($is_admin) {
        echo " for <select onChange='this.form.submit()' name='user'>\n";
        if ($all_users) {
            printf("<option value='{$is_logged_in}'>%s</option>\n", _("my account"));
            printf("<option value='all' selected='selected'>all users</option>\n");
        } else {
            if ($userid != $is_logged_in) {
                printf("<option value='{$is_logged_in}'>%s</option>\n", _("my account"));
            }
            printf("<option value='{$userid}' selected='selected'>%s</option>\n", $userid == $is_logged_in ? _("my account") : "UID {$userid}");
            echo "<option value='all'>all users</option>\n";
        }
        echo "</select>\n";
        echo " or UID or OpenID: ";
        echo "<input class='nline' type='text' name='uid'>\n";
    }
    $pending = $interval == 'pending';
    $use_interval = $interval != 'forever' && !$pending;
    $args = $specified_user ? "user={$userid}&" : "";
    $args .= "interval={$interval}";
    if ($from_zero) {
        $args .= "&fromz=1";
    }
    echo "<input type='hidden' name='form' value='1' /><br />\n";
    echo statement_checkbox('dbtc', $deposit_btc, _("Deposit") . " " . "BTC", $args);
    echo statement_checkbox('wbtc', $withdraw_btc, _("Withdraw") . " " . "BTC", $args);
    echo statement_checkbox('dfiat', $deposit_fiat, _("Deposit") . " " . CURRENCY, $args);
    echo statement_checkbox('wfiat', $withdraw_fiat, _("Withdraw") . " " . CURRENCY, $args);
    echo statement_checkbox('bbtc', $buy, _("Buy") . " " . "BTC", $args);
    echo statement_checkbox('sbtc', $sell, _("Sell") . " " . "BTC", $args);
    if ($interval != 'forever') {
        echo statement_checkbox('fromz', $from_zero, _("Start at Zero"));
    } else {
        if ($from_zero) {
            echo "<input type='hidden' name='fromz' value='1' />\n";
        }
    }
    echo "</p>\n";
    echo "</form>\n";
    if (!$all_users) {
        echo "<p>" . _("OpenID") . ": <a href=\"{$openid}\">{$openid}</a></p>\n";
        if ($deposit_address) {
            echo "<p>" . _("Deposit Address") . ": {$deposit_address}</p>\n";
        }
    }
    $query = "\n        SELECT\n            uid,\n            txid, a_orderid AS orderid,\n            a_amount AS gave_amount, '" . CURRENCY . "' AS gave_curr,\n            (b_amount-b_commission) AS got_amount,  'BTC' AS got_curr,\n            NULL as reqid,  NULL as req_type,\n            NULL as amount, NULL as curr_type, NULL as addy, NULL as voucher, NULL as final, NULL as bank, NULL as acc_num,\n            " . sql_format_date('transactions.timest') . " AS date,\n            transactions.timest as timest, " . ($use_interval ? "transactions.timest > NOW() - INTERVAL {$interval}" : ($pending ? "0" : "1")) . " AS new\n        FROM\n            transactions\n        JOIN\n            orderbook\n        ON\n            orderbook.orderid = transactions.a_orderid\n        WHERE\n            {$check_stuff}\n            b_amount != -1\n\n    UNION\n\n        SELECT\n            uid,\n            txid, b_orderid AS orderid,\n            b_amount AS gave_amount, 'BTC' AS gave_curr,\n            (a_amount-a_commission) AS got_amount,  '" . CURRENCY . "' AS got_curr,\n            NULL, NULL,\n            NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n            " . sql_format_date('transactions.timest') . " AS date,\n            transactions.timest as timest, " . ($use_interval ? "transactions.timest > NOW() - INTERVAL {$interval}" : ($pending ? "0" : "1")) . " AS new\n        FROM\n            transactions\n        JOIN\n            orderbook\n        ON\n            orderbook.orderid=transactions.b_orderid\n        WHERE\n            {$check_stuff}\n            b_amount != -1\n\n    UNION\n\n        SELECT\n            uid,\n            NULL, NULL,\n            NULL, NULL,\n            NULL, NULL,\n            requests.reqid,  req_type,\n            amount, curr_type, addy, CONCAT(prefix, '-...') as voucher, status = 'FINAL', bank, acc_num,\n            " . sql_format_date('timest') . " AS date,\n            timest, " . ($use_interval ? "timest > NOW() - INTERVAL {$interval}" : ($pending ? "status != 'FINAL'" : "1")) . " AS new\n        FROM\n            requests\n        LEFT JOIN\n            bitcoin_requests\n        ON\n            requests.reqid = bitcoin_requests.reqid\n        LEFT JOIN\n            voucher_requests\n        ON\n            (requests.reqid = voucher_requests.reqid OR\n             requests.reqid = voucher_requests.redeem_reqid)\n        LEFT JOIN\n            uk_requests\n        ON\n            requests.reqid = uk_requests.reqid\n        WHERE\n            {$check_stuff}\n            status != 'CANCEL'\n\n    ORDER BY\n        timest, txid, got_curr\n    ";
    $first = true;
    $result = do_query($query);
    $fiat = $btc = numstr_to_internal(0);
    $total_fiat_deposit = $total_fiat_withdrawal = $total_btc_deposit = $total_btc_withdrawal = numstr_to_internal(0);
    $total_fiat_got = $total_fiat_given = $total_btc_got = $total_btc_given = numstr_to_internal(0);
    $period_fiat_deposit = $period_fiat_withdrawal = $period_btc_deposit = $period_btc_withdrawal = numstr_to_internal(0);
    $period_fiat_got = $period_fiat_given = $period_btc_got = $period_btc_given = numstr_to_internal(0);
    echo "<table class='display_data'>\n";
    echo "<tr>";
    echo "<th>" . _("Date") . "</th>";
    if ($all_users) {
        echo "<th>" . _("User") . "</th>";
    }
    echo "<th>" . _("Description") . "</th>";
    if (!$pending) {
        if ($show_prices) {
            echo "<th class='right'>" . _("Price") . "</th>";
        }
        if ($show_increments) {
            echo "<th class='right'>+/-</th>";
        }
        echo "<th class='right'>BTC</th>";
        if ($show_increments) {
            echo "<th class='right'>+/-</th>";
        }
        echo "<th class='right'>" . CURRENCY . "</th>";
    }
    echo "</tr>\n";
    if ($create_timestamp && !$pending) {
        printf("<tr><td>%s</td><td>%s</td></tr>\n", $create_timestamp, _("Create Account"));
    }
    $all_final = true;
    while ($row = mysql_fetch_array($result)) {
        $new = $row['new'];
        $uid = $row['uid'];
        $date = $row['date'];
        if ($first && $new) {
            if ($from_zero) {
                $btc = $fiat = numstr_to_internal(0);
            }
            show_balances_in_statement(_("Opening Balances"), $btc, $fiat, $all_users, $show_prices, $show_increments, $pending);
            $first = false;
        }
        if (isset($row['txid'])) {
            /* buying or selling */
            $txid = $row['txid'];
            $orderid = $row['orderid'];
            $gave_amount = $row['gave_amount'];
            $gave_curr = $row['gave_curr'];
            $got_amount = $row['got_amount'];
            $got_curr = $row['got_curr'];
            if ($got_curr == 'BTC') {
                /* buying BTC */
                if ($buy) {
                    $fiat = gmp_sub($fiat, $gave_amount);
                    $btc = gmp_add($btc, $got_amount);
                }
                $total_btc_got = gmp_add($total_btc_got, $got_amount);
                $total_fiat_given = gmp_add($total_fiat_given, $gave_amount);
                $got_str = internal_to_numstr($got_amount, BTC_PRECISION);
                $gave_str = internal_to_numstr($gave_amount, FIAT_PRECISION);
                if ($new && $buy) {
                    $period_btc_got = gmp_add($period_btc_got, $got_amount);
                    $period_fiat_given = gmp_add($period_fiat_given, $gave_amount);
                    if (string_is_zero($got_str) && string_is_zero($gave_str)) {
                        continue;
                    }
                    echo "<tr><td>{$date}</td>";
                    if ($all_users) {
                        echo active_table_cell_link_to_user_statement($uid, $interval);
                    }
                    active_table_cell_for_order(sprintf(_("Buy %s %s for %s %s"), $got_str, $got_curr, $gave_str, $gave_curr), $orderid);
                    if ($show_prices) {
                        printf("<td>%s</td>", trade_price($got_amount, $gave_amount));
                    }
                    if ($show_increments) {
                        printf("<td class='right'>+ %s</td>", $got_str);
                    }
                    printf("<td class='right'> %s</td>", internal_to_numstr($btc, BTC_PRECISION));
                    if ($show_increments) {
                        printf("<td class='right'>- %s</td>", $gave_str);
                    }
                    printf("<td class='right'> %s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                    echo "</tr>\n";
                }
            } else {
                /* selling BTC */
                if ($sell) {
                    $fiat = gmp_add($fiat, $got_amount);
                    $btc = gmp_sub($btc, $gave_amount);
                }
                $total_fiat_got = gmp_add($total_fiat_got, $got_amount);
                $total_btc_given = gmp_add($total_btc_given, $gave_amount);
                $gave_str = internal_to_numstr($gave_amount, BTC_PRECISION);
                $got_str = internal_to_numstr($got_amount, FIAT_PRECISION);
                if ($new && $sell) {
                    $period_fiat_got = gmp_add($period_fiat_got, $got_amount);
                    $period_btc_given = gmp_add($period_btc_given, $gave_amount);
                    if (string_is_zero($got_str) && string_is_zero($gave_str)) {
                        continue;
                    }
                    echo "<tr><td>{$date}</td>";
                    if ($all_users) {
                        echo active_table_cell_link_to_user_statement($uid, $interval);
                    }
                    active_table_cell_for_order(sprintf(_("Sell %s %s for %s %s"), $gave_str, $gave_curr, $got_str, $got_curr), $orderid);
                    if ($show_prices) {
                        printf("<td>%s</td>", trade_price($gave_amount, $got_amount));
                    }
                    if ($show_increments) {
                        printf("<td class='right'>-%s</td>", $gave_str);
                    }
                    // don't show balances between pairs of buy and sell rows if we're showing buy as well as sell
                    printf("<td class='right'>%s</td>", $all_users && $buy ? "" : internal_to_numstr($btc, BTC_PRECISION));
                    if ($show_increments) {
                        printf("<td class='right'>+%s</td>", $got_str);
                    }
                    printf("<td class='right'>%s</td>", $all_users && $buy ? "" : internal_to_numstr($fiat, FIAT_PRECISION));
                    echo "</tr>\n";
                }
            }
        } else {
            /* withdrawal or deposit */
            $reqid = $row['reqid'];
            $req_type = $row['req_type'];
            $amount = $row['amount'];
            $curr_type = $row['curr_type'];
            $voucher = $row['voucher'];
            $final = $row['final'];
            // echo "final is $final<br/>\n";
            $show = $req_type == 'DEPOS' && ($curr_type == 'BTC' && $deposit_btc || $curr_type != 'BTC' && $deposit_fiat) || $req_type != 'DEPOS' && ($curr_type == 'BTC' && $withdraw_btc || $curr_type != 'BTC' && $withdraw_fiat);
            if ($new && $show) {
                echo "<tr><td>{$date}</td>";
                if ($all_users) {
                    echo active_table_cell_link_to_user_statement($uid, $interval);
                }
            }
            if (!$final) {
                $all_final = false;
            }
            if ($req_type == 'DEPOS') {
                /* deposit */
                $title = '';
                if ($voucher) {
                    $title = sprintf(_("from voucher") . " &quot;%s&quot;", $voucher);
                }
                if ($curr_type == 'BTC') {
                    /* deposit BTC */
                    if ($show) {
                        $btc = gmp_add($btc, $amount);
                    }
                    $total_btc_deposit = gmp_add($total_btc_deposit, $amount);
                    if ($new && $show) {
                        $period_btc_deposit = gmp_add($period_btc_deposit, $amount);
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s BTC%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Redeem voucher") . ":" : _("Deposit"), internal_to_numstr($amount, BTC_PRECISION), $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td class='right'>+%s</td>", internal_to_numstr($amount, BTC_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($btc, BTC_PRECISION));
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                        }
                    }
                } else {
                    /* deposit FIAT */
                    if ($show) {
                        $fiat = gmp_add($fiat, $amount);
                    }
                    $total_fiat_deposit = gmp_add($total_fiat_deposit, $amount);
                    if ($new && $show) {
                        $period_fiat_deposit = gmp_add($period_fiat_deposit, $amount);
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s %s%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Redeem voucher") . ":" : _("Deposit"), internal_to_numstr($amount, FIAT_PRECISION), CURRENCY, $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                            if ($show_increments) {
                                printf("<td class='right'>+%s</td>", internal_to_numstr($amount, FIAT_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                        }
                    }
                }
            } else {
                /* withdrawal */
                if ($curr_type == 'BTC') {
                    /* withdraw BTC */
                    if ($show) {
                        $btc = gmp_sub($btc, $amount);
                    }
                    $total_btc_withdrawal = gmp_add($total_btc_withdrawal, $amount);
                    if ($new && $show) {
                        $period_btc_withdrawal = gmp_add($period_btc_withdrawal, $amount);
                        $addy = $row['addy'];
                        if ($addy) {
                            $title = sprintf(_("to Bitcoin address") . " &quot;%s&quot;", $addy);
                        } else {
                            if ($voucher) {
                                $title = sprintf(_("to %svoucher") . " &quot;%s&quot;", $final ? "" : _("unredeemed") . " ", $voucher);
                            }
                        }
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s BTC%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Create voucher") . ":" : _("Withdraw"), internal_to_numstr($amount, BTC_PRECISION), $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td class='right'>-%s</td>", internal_to_numstr($amount, BTC_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($btc, BTC_PRECISION));
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                        }
                    }
                } else {
                    /* withdraw FIAT */
                    if ($show) {
                        $fiat = gmp_sub($fiat, $amount);
                    }
                    $total_fiat_withdrawal = gmp_add($total_fiat_withdrawal, $amount);
                    if ($new && $show) {
                        $period_fiat_withdrawal = gmp_add($period_fiat_withdrawal, $amount);
                        $title = '';
                        if ($voucher) {
                            $title = sprintf(_("to %svoucher") . " &quot;%s&quot;", $final ? "" : _("unredeemed") . " ", $voucher);
                        } else {
                            $title = sprintf(_("to account %s at %s"), $row['acc_num'], $row['bank']);
                        }
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s %s%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Create voucher") . ":" : _("Withdraw"), internal_to_numstr($amount, FIAT_PRECISION), CURRENCY, $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                            if ($show_increments) {
                                printf("<td class='right'>-%s</td>", internal_to_numstr($amount, FIAT_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                        }
                    }
                }
            }
            if ($new) {
                echo "</tr>\n";
            }
        }
    }
    if ($first && $from_zero) {
        $fiat = $btc = numstr_to_internal(0);
    }
    show_balances_in_statement($first ? _("There are no entries for this period") : _("Closing Balances"), $btc, $fiat, $all_users, $show_prices, $show_increments, $pending);
    echo "</table>\n";
    if (!$all_final) {
        echo "<p>" . _("Items marked with '*' are not yet final.") . "</p>\n";
        echo "<p>" . _("Any such withdrawals and vouchers can be cancelled.") . "</p>\n";
        echo "<p>" . _("Any such deposits are pending, and should be finalised within a minute or two.") . "</p>\n";
    }
    echo "</div>";
    if (gmp_cmp($total_fiat_deposit, $period_fiat_deposit) != 0 || gmp_cmp($total_fiat_withdrawal, $period_fiat_withdrawal) != 0 || gmp_cmp($total_btc_deposit, $period_btc_deposit) != 0 || gmp_cmp($total_btc_withdrawal, $period_btc_withdrawal) != 0 || gmp_cmp($total_fiat_got, $period_fiat_got) != 0 || gmp_cmp($total_fiat_given, $period_fiat_given) != 0 || gmp_cmp($total_btc_got, $period_btc_got) != 0 || gmp_cmp($total_btc_given, $period_btc_given) != 0) {
        show_statement_summary(_("Summary of displayed entries"), $period_fiat_deposit, $period_fiat_withdrawal, $period_btc_deposit, $period_btc_withdrawal, $period_fiat_got, $period_fiat_given, $period_btc_got, $period_btc_given);
    }
    show_statement_summary(_("Account Summary"), $total_fiat_deposit, $total_fiat_withdrawal, $total_btc_deposit, $total_btc_withdrawal, $total_fiat_got, $total_fiat_given, $total_btc_got, $total_btc_given);
}
Beispiel #11
0
            echo "    <p>" . sprintf(_("You can withdraw up to %s BTC each day"), internal_to_numstr($limit)) . " (", day_time_range_string(), ").</p>\n";
            if ($withdrawn) {
                echo "    <p>" . sprintf(_("You have withdrawn %s BTC today"), internal_to_numstr($withdrawn)) . "\n";
                if (gmp_cmp($available, '0') > 0) {
                    echo "    " . sprintf(_("and so can withdraw up to %s BTC more."), internal_to_numstr($available));
                } else {
                    echo "    " . _("and so cannot withdraw any more until tomorrow.");
                }
                echo "</p>\n";
            }
        }
        if (gmp_cmp($btc, '0') <= 0) {
            echo "    <p>" . _("You don't have any BTC to withdraw.") . "</p>\n";
        } else {
            if (gmp_cmp($available, '0') > 0) {
                echo "    <p>" . sprintf(_("Enter an amount below to withdraw.  You have %s BTC."), internal_to_numstr($btc)) . "</p>\n";
                ?>
    <p>
        <form action='' class='indent_form' method='post'>
            <label for='input_amount'><?php 
                echo _("Amount");
                ?>
</label>
            <input type='text' id='input_amount' name='amount' value='0.00' />

            <input type='hidden' name='csrf_token' value="<?php 
                echo $_SESSION['csrf_token'];
                ?>
" />
            <input type='hidden' name='curr_type' value='BTC' />
            <input type='hidden' name='voucher' value='1' />
function process()
{
    do_query("SET div_precision_increment = 8");
    // find and cancel any active orders from users with negative BTC or FIAT balances
    // this should never happen unless someone is trying to double-spend their balance
    $query = "\n        SELECT orderid, orderbook.amount as amount, orderbook.type, orderbook.uid as uid\n        FROM orderbook\n        JOIN purses\n        ON orderbook.uid = purses.uid\n        WHERE\n            status != 'CLOSED' AND\n            status != 'CANCEL' AND\n            purses.amount < 0\n        GROUP BY orderid\n        ";
    $result = b_query($query);
    while ($row = mysql_fetch_array($result)) {
        $orderid = $row['orderid'];
        $amount = $row['amount'];
        $type = $row['type'];
        $uid = $row['uid'];
        try {
            echo "cancelling order {$orderid} (spend ", internal_to_numstr($amount), " {$type} for user {$uid}) due to negative balance\n";
            wait_for_lock($uid);
            $query = "\n    UPDATE orderbook\n    SET status = 'CANCEL'\n    WHERE orderid = '{$orderid}'\n            ";
            b_query($query);
            add_funds($uid, $amount, $type);
            // these records indicate returned funds.
            create_record($orderid, $amount, 0, 0, -1, 0);
            release_lock($uid);
        } catch (Error $e) {
            if ($e->getTitle() == 'Lock Error') {
                echo "can't get lock for {$uid}\n";
            } else {
                throw $e;
            }
        }
    }
    $query = "\n        SELECT orderid\n        FROM orderbook\n        WHERE processed=FALSE\n        ORDER BY timest ASC\n    ";
    $result = b_query($query);
    while ($row = mysql_fetch_array($result)) {
        $orderid = $row['orderid'];
        echo "Processing {$orderid}...\n";
        fulfill_order($orderid);
        echo "Completed.\n\n";
        $query = "\n            UPDATE orderbook\n            SET processed=TRUE\n            WHERE orderid='{$orderid}'\n        ";
        b_query($query);
    }
}
Beispiel #13
0
        }
        release_lock($request_uid);
    } else {
        if ($is_admin) {
            $uid_check = "";
        } else {
            $uid_check = "AND uid='{$uid}'";
        }
        $query = "\n        SELECT\n            req_type,\n            amount,\n            curr_type,\n            " . sql_format_date("timest") . " AS timest,\n            status\n        FROM requests\n        WHERE reqid='{$reqid}' {$uid_check}\n    ";
        $result = do_query($query);
        if (!has_results($result)) {
            throw new Problem('No request here', "Don't have viewing permissions.");
        }
        $row = get_row($result);
        $req_type = $row['req_type'];
        $amount = internal_to_numstr($row['amount']);
        $curr_type = $row['curr_type'];
        $timest = $row['timest'];
        $status = $row['status'];
        ?>
 <div class='content_box'>
        <h3><?php 
        echo _("Order info");
        ?>
</h3>
        <p>
        <?php 
        printf(_("Request %s"), $reqid);
        ?>
        </p>
        <?php 
Beispiel #14
0
function info()
{
    global $is_logged_in;
    $balances = fetch_balances($is_logged_in);
    return array("status" => "OK", "uid" => $is_logged_in, "BTC" => internal_to_numstr($balances['BTC']), CURRENCY => internal_to_numstr($balances[CURRENCY]));
}
<?php

require_once '../util.php';
$is_logged_in = 'verify_deposits';
try {
    check_frozen();
    $query = "\n    SELECT\n        reqid,\n        uid,\n        curr_type,\n        amount\n    FROM\n        requests\n    WHERE\n        status='VERIFY'\n        AND req_type='DEPOS'\n    ";
    $result = do_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $reqid = $row['reqid'];
        $query = "\n        UPDATE\n            requests\n        SET\n            status='PROCES'\n        WHERE\n            reqid='{$reqid}'\n        ";
        do_query($query);
        $uid = $row['uid'];
        $type = $row['curr_type'];
        $amount = $row['amount'];
        $query = "\n        UPDATE\n            purses\n        SET\n            amount=amount+'{$amount}'\n        WHERE\n            uid='{$uid}'\n            AND type='{$type}'\n        ";
        do_query($query);
        $query = "\n        UPDATE\n            requests\n        SET\n            status='FINAL'\n        WHERE\n            reqid='{$reqid}'\n        ";
        do_query($query);
        addlog(LOG_RESULT, "Added " . internal_to_numstr($amount) . " {$type} for user {$uid}");
    }
} catch (Error $e) {
    report_exception($e, SEVERITY::ERROR);
    // Same as below, but flag + log this for review,
    echo "\nError: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Problem $e) {
    echo "\nProblem: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Exception $e) {
    echo "\nException: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
}
Beispiel #16
0
function display_double_entry($curr_a, $curr_b, $base_curr, $uid, $is_admin)
{
    if (isset($_GET['show_all']) && get('show_all') == 'true') {
        $show_all = true;
    } else {
        $show_all = false;
    }
    echo "<div class='content_box'>\n";
    if ($curr_a == 'BTC') {
        echo "<h3>" . sprintf(_("People selling %s for %s"), $curr_a, $curr_b) . "</h3>\n";
    } else {
        echo "<h3>" . sprintf(_("People buying %s for %s"), $curr_b, $curr_a) . "</h3>\n";
    }
    $exchange_fields = calc_exchange_rate($curr_a, $curr_b, $base_curr);
    if (!$exchange_fields) {
        if ($curr_a == 'BTC') {
            echo "<p>" . sprintf(_("Nobody is selling %s for %s."), $curr_a, $curr_b) . "</p>";
        } else {
            echo "<p>" . sprintf(_("Nobody is buying %s for %s."), $curr_b, $curr_a) . "</p>";
        }
        echo "</div>";
        return;
    }
    list($total_amount, $total_want_amount, $rate) = $exchange_fields;
    echo "<p>" . _("Best exchange rate is") . " ";
    if ($base_curr == BASE_CURRENCY::A) {
        echo "<b>{$rate} {$curr_b}/{$curr_a}</b>";
    } else {
        echo "<b>{$rate} {$curr_a}/{$curr_b}</b>";
    }
    echo ".</p>";
    if (!$show_all) {
        echo "<p>" . sprintf(_("Showing top %d entries"), DEFAULT_ORDERBOOK_DEPTH) . ":</p>";
    }
    ?>
<table class='display_data'>
        <tr>
            <th><?php 
    echo _("Cost / BTC");
    ?>
</th>
            <th><?php 
    echo _("Giving");
    ?>
</th>
            <th><?php 
    echo _("Wanted");
    ?>
</th>
<?php 
    if ($is_admin) {
        ?>
            <th><?php 
        echo _("User");
        ?>
</th>
<?php 
    }
    if (SHOW_CUMULATIVE_DEPTH) {
        ?>
            <th><?php 
        echo _("Cumulative Give");
        ?>
</th>
            <th><?php 
        echo _("Cumulative Want");
        ?>
</th>
<?php 
    }
    ?>
        </tr><?php 
    $show_query = 'LIMIT ' . DEFAULT_ORDERBOOK_DEPTH;
    if ($show_all) {
        $show_query = '';
    }
    $query = "\n        SELECT\n            orderid,\n            amount,\n            want_amount,\n            uid={$uid} as me,\n            uid,\n            IF(\n                type='BTC',\n                initial_want_amount/initial_amount,\n                initial_amount/initial_want_amount\n            ) AS rate\n        FROM orderbook\n        WHERE type='{$curr_a}' AND want_type='{$curr_b}' AND status='OPEN'\n        ORDER BY\n            IF(type='BTC', rate, -rate) ASC, timest ASC\n        {$show_query}\n    ";
    $result = do_query($query);
    $cumulative_curr_a = 0;
    $cumulative_curr_b = 0;
    if ($curr_a == 'BTC') {
        $precision_a = BTC_PRECISION;
        $precision_b = FIAT_PRECISION;
    } else {
        $precision_a = FIAT_PRECISION;
        $precision_b = BTC_PRECISION;
    }
    while ($row = mysql_fetch_array($result)) {
        $amount_i = $row['amount'];
        $amount = internal_to_numstr($amount_i, $precision_a);
        $cumulative_curr_a = gmp_add($cumulative_curr_a, $amount_i);
        $want_amount_i = $row['want_amount'];
        $want_amount = internal_to_numstr($want_amount_i, $precision_b);
        $cumulative_curr_b = gmp_add($cumulative_curr_b, $want_amount_i);
        // MySQL kindly computes this for us.
        // we trim the excessive 0
        $rate = clean_sql_numstr($row['rate']);
        $me = $row['me'];
        $uid = $row['uid'];
        if ($me) {
            echo "    ", active_table_row("me", "?page=view_order&orderid={$row['orderid']}");
        } else {
            echo "    ", active_table_row("them", "?page=trade&in={$curr_b}&have={$want_amount_i}&want={$amount_i}&rate={$rate}");
        }
        echo "        <td>{$rate}</td>\n";
        echo "        <td>{$amount} {$curr_a}</td>\n";
        echo "        <td>{$want_amount} {$curr_b}</td>\n";
        if ($is_admin) {
            echo "        <td>{$uid}</td>\n";
        }
        if (SHOW_CUMULATIVE_DEPTH) {
            echo "        <td>" . internal_to_numstr($cumulative_curr_a, $precision_a) . " {$curr_a}</td>\n";
            echo "        <td>" . internal_to_numstr($cumulative_curr_b, $precision_b) . " {$curr_b}</td>\n";
        }
        echo "    </tr>\n";
    }
    echo "    <tr>\n";
    echo "        <td>" . _("Total") . ":</td>\n";
    // strstr's 3rd argument only works in PHP 5.3.0 and newer
    //   http://php.net/manual/en/function.strstr.php
    // use explode instead
    $total_amount = explode('.', $total_amount, 2);
    $total_amount = $total_amount[0];
    echo "        <td>{$total_amount} {$curr_a}</td>\n";
    echo "        <td></td>\n";
    echo "    </tr>\n";
    echo "</table>\n";
    if ($show_all) {
        echo "<p><a href='?page=orderbook&show_all=false'>&gt;&gt; " . _("hide") . "</a></p>\n";
    } else {
        echo "<p><a href='?page=orderbook&show_all=true'>&gt;&gt; " . _("show all") . "</a></p>\n";
    }
    echo "</div>\n";
}
Beispiel #17
0
    cancel_order($orderid, $uid);
    ?>
<div class='content_box'>
        <h3>Cancelled!</h3>
        <p>Order <?php 
    echo $orderid;
    ?>
 is no more.</p>
        <p>Back to <a href="?page=orderbook">the orderbook</a>.</p>
    </div><?php 
} else {
    $initial_amount = internal_to_numstr($info->initial_amount);
    $amount = internal_to_numstr($info->amount);
    $type = $info->type;
    $initial_want_amount = internal_to_numstr($info->initial_want_amount);
    $want_amount = internal_to_numstr($info->want_amount);
    $want_type = $info->want_type;
    $timest = $info->timest;
    $status = $info->status;
    ?>
 <div class='content_box'>
        <h3>Order info</h3>
        <p>
        <?php 
    printf(_("Order %s"), $orderid);
    ?>
        </p>
        <p>
        <?php 
    printf(_("When the order was placed: %s for %s"), "{$initial_amount} {$type}", "{$initial_want_amount} {$want_type}");
    ?>
Beispiel #18
0
function show_content_header_balances($uid)
{
    $balances = fetch_balances($uid);
    $fiat = internal_to_numstr($balances[CURRENCY], FIAT_PRECISION, false);
    $btc = internal_to_numstr($balances['BTC'], BTC_PRECISION, false);
    $c_balances = fetch_committed_balances($uid);
    $c_fiat = internal_to_numstr($c_balances[CURRENCY], FIAT_PRECISION);
    $c_btc = internal_to_numstr($c_balances['BTC'], BTC_PRECISION);
    echo "    <div class='content_header_box'>\n";
    echo "        ", SPACE, _("balances"), ":", SPACE, "{$fiat} ";
    if ($c_fiat > 0) {
        echo "(+{$c_fiat}) ";
    }
    echo CURRENCY, SPACE, "{$btc} ";
    if ($c_btc > 0) {
        echo "(+{$c_btc}) ";
    }
    echo "BTC\n";
    echo "    </div>\n";
}
Beispiel #19
0
function active_table_cell_link_for_commission($uid, $txid, $orderid, $sub, $amount, $precision)
{
    $url = "?page=view_order&orderid={$orderid}&uid={$uid}";
    echo "<td class='active right' id='cell_{$txid}_{$orderid}_{$sub}' onmouseover='In(\"{$orderid}\");' onmouseout='Out(\"{$orderid}\");' onclick='document.location=\"{$url}\"'>", internal_to_numstr($amount, $precision), "</td>";
}
Beispiel #20
0
                            $query = "\n                        INSERT INTO users (\n                            is_admin,\n                            oidlogin,\n                            deposref\n                        ) VALUES (\n                            {$admin},\n                            '{$oidlogin}',\n                            CONCAT(FLOOR(RAND() * 900 + 100),\n                                   LPAD(FLOOR(RAND() * 1000),3,'0'),\n                                   LPAD(FLOOR(RAND() * 1000),3,'0'))\n                        );\n                    ";
                            do_query($query);
                            $uid = (string) mysql_insert_id();
                            $free_fiat = numstr_to_internal(FREE_FIAT_ON_SIGNUP);
                            $free_btc = numstr_to_internal(FREE_BTC_ON_SIGNUP);
                            $query = "\n                        INSERT INTO purses\n                            (uid, amount, type)\n                        VALUES\n                            (LAST_INSERT_ID(), {$free_fiat}, '" . CURRENCY . "');\n                    ";
                            do_query($query);
                            $query = "\n                        INSERT INTO purses\n                            (uid, amount, type)\n                        VALUES\n                            (LAST_INSERT_ID(), {$free_btc}, 'BTC');\n                    ";
                            do_query($query);
                            addlog(LOG_LOGIN, sprintf("  new user UID %s (openid %s)", $uid, $oidlogin));
                            show_header('login', $uid);
                            echo "                    <div class='content_box'>\n";
                            echo "                        <h3>" . _("Successful login!") . "</h3>\n";
                            echo "                        <p>" . _("Nice to finally see you here, <i>new</i> user.") . "</p>\n";
                            if (gmp_cmp($free_fiat, 0) > 0 or gmp_cmp($free_btc, 0)) {
                                echo "                        <p>" . sprintf("We've given you %s and %s to test the exchange with.", internal_to_numstr($free_btc) . " BTC", internal_to_numstr($free_fiat) . " " . CURRENCY) . "</p>\n";
                            }
                            echo "                        <p>" . sprintf("Now you may wish to %sdeposit%s funds before continuing.", '<a href="?page=deposit">', '</a>') . "</p>\n";
                        }
                        // store for later
                        $_SESSION['oidlogin'] = $oidlogin;
                        $_SESSION['uid'] = $uid;
                    }
                } else {
                    setcookie('autologin', FALSE, time() - 60 * 60 * 24 * 365);
                    throw new Problem(_("Login Error"), sprintf(_("Unable to login.  Please %stry again%s."), '<a href="?page=login">', '</a>'));
                }
            }
        }
    }
} catch (ErrorException $e) {
        $we_have = bitcoin_get_balance("*", CONFIRMATIONS_FOR_DEPOSIT);
        // add on anything we've recently sent from offline storage but which isn't fully confirmed yet
        $main_unconfirmed = gmp_sub(bitcoin_get_balance("", 1), bitcoin_get_balance("", CONFIRMATIONS_FOR_DEPOSIT));
        $we_have = gmp_add($we_have, $main_unconfirmed);
        addlog(LOG_CRONJOB, "Attempting to withdraw " . internal_to_numstr($amount) . " of " . internal_to_numstr($we_have) . " BTC for user {$uid} (reqid {$reqid})");
        if (gmp_cmp($we_have, $amount) >= 0) {
            update_req($reqid, "PROCES");
            // use 'sendtoaddress' rather than 'sendfrom' because it can 'go overdrawn'
            // so long as there are funds in other accounts (pending deposits) to cover it
            bitcoin_send_to_address($addy, $amount);
            update_req($reqid, "FINAL");
            $we_have = bitcoin_get_balance("*", 0);
            addlog(LOG_CRONJOB, "We have " . internal_to_numstr($we_have) . " BTC in total");
            if (gmp_cmp($we_have, numstr_to_internal(WARN_LOW_WALLET_THRESHOLD)) < 0) {
                email_tech(_("Exchange Wallet Balance is Low"), sprintf(_("The exchange wallet only has %s BTC available."), internal_to_numstr($we_have, BTC_PRECISION)));
            }
        } else {
            $message = sprintf(_("We only have %s BTC so can't withdraw %s BTC"), internal_to_numstr($we_have, BTC_PRECISION), internal_to_numstr($amount, BTC_PRECISION));
            addlog(LOG_CRONJOB, $message);
            // email_tech(_("Exchange Wallet Balance is Too Low"), $message);
        }
    }
} catch (Error $e) {
    report_exception($e, SEVERITY::ERROR);
    // Same as below, but flag + log this for review,
    echo "\nError: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Problem $e) {
    echo "\nProblem: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Exception $e) {
    echo "\nException: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
}
<?php

require_once '../util.php';
$is_logged_in = 'sync_to_bitcoin';
foreach (bitcoin_list_accounts(CONFIRMATIONS_FOR_DEPOSIT) as $account => $balance) {
    if ($balance) {
        try {
            get_openid_for_user($account);
            // check they have an account
        } catch (Exception $e) {
            continue;
        }
        get_user_lock($account);
        addlog(LOG_CRONJOB, sprintf("add %s BTC for user %s", internal_to_numstr($balance), $account));
        sync_to_bitcoin((string) $account);
        release_lock($account);
    }
}
Beispiel #23
0
</th>
            <th><?php 
        echo _("Confirmations Received");
        ?>
</th>
            <th><?php 
        echo _("More Confirmations Needed");
        ?>
</th>
        </tr>
    <?php 
        for ($conf = $needed_conf; $conf >= 0; $conf--) {
            $new_balance = bitcoin_get_balance($uid, $conf);
            if ($balance != $new_balance) {
                $diff = gmp_sub($new_balance, $balance);
                echo "<tr><td>", internal_to_numstr($diff), "</td><td>{$conf}</td><td>", $needed_conf - $conf, "</td></tr>\n";
                $balance = $new_balance;
            }
        }
        echo "</table></div>";
    }
} catch (Exception $e) {
    if ($e->getMessage() != 'Unable to connect.') {
        throw $e;
    }
    echo "<div class='content_box'>\n";
    echo "<h3>" . _("Pending bitcoin deposits") . "</h3>\n";
    echo "<p>" . _("Normally this area would display any Bitcoin deposits you have made that are awaiting confirmations, but we are having trouble connecting to the Bitcoin network at the moment, so it doesn't.") . "</p>\n";
    echo "<p>" . _("Please try again in a few minutes.") . "</p>\n";
    echo "</div>";
}
Beispiel #24
0
if ($first) {
    echo "<p>There are no recent trades.</p>\n";
} else {
    $price = clean_sql_numstr(bcdiv(gmp_strval($amount_fiat_total), gmp_strval($amount_btc_total), 4));
    echo "    <tr>\n";
    if ($is_admin) {
        echo "        <td></td><td></td><td class='right'>--------</td><td></td><td class='right'>--------</td><td class='right'>--------</td>\n";
    } else {
        echo "        <td></td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td>\n";
    }
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "        <td></td>";
    if ($is_admin) {
        echo "        <td></td>";
    }
    echo "        <td>", internal_to_numstr($amount_fiat_total, FIAT_PRECISION), "</td>";
    if ($is_admin) {
        echo "        <td></td>";
    }
    echo "        <td>", internal_to_numstr($amount_btc_total, BTC_PRECISION), "</td>";
    echo "        <td>{$price}</td>";
    echo "    </tr>\n";
    echo "</table>\n";
    if ($mine) {
        echo "<p>" . sprintf(_("The %s amount(s) you %sgave%s are shown in %sbold%s."), $mine, "<span style='font-weight: bold;'>", "</span>", "<span style='font-weight: bold;'>", "</span>") . "</p>\n";
    }
}
?>
</div>
Beispiel #25
0
function test_voucher_comm($v)
{
    echo "commission on voucher for " . internal_to_numstr($v, 2) . " is " . internal_to_numstr(commission_on_deposit_mtgox_fiat_voucher($v), 4) . "<br/>\n";
}
Beispiel #26
0
function check_btc_withdraw_limit($uid, $amount)
{
    $withdrawn = btc_withdrawn_today($uid);
    $limit = numstr_to_internal(MAXIMUM_DAILY_BTC_WITHDRAW);
    $available = gmp_sub($limit, $withdrawn);
    if (gmp_cmp($amount, $available) > 0) {
        throw new Problem(_('Daily limit exceeded'), sprintf(_('You can only withdraw %s per day.'), internal_to_numstr($limit) . ' BTC'));
    }
}
Beispiel #27
0
function display_transactions($uid, $orderid)
{
    global $is_logged_in, $is_admin;
    $ordselq = '';
    if (!$orderid) {
        $sort = "DESC";
    } else {
        $sort = "ASC";
        $ordselq = " AND orderbook.orderid='{$orderid}' ";
    }
    $query = "\n        SELECT\n            orderbook.orderid AS orderid,\n            IF(transactions.a_orderid=orderbook.orderid, 'A', 'B') AS who,\n            transactions.a_amount AS a_amount,\n            transactions.b_amount AS b_amount,\n            transactions.a_commission AS a_commission,\n            transactions.b_commission AS b_commission,\n            orderbook.type AS type,\n            orderbook.want_type AS want_type,\n            " . sql_format_date("transactions.timest") . " AS timest\n        FROM transactions\n        JOIN orderbook\n        ON\n            transactions.a_orderid=orderbook.orderid\n            OR transactions.b_orderid=orderbook.orderid\n        WHERE orderbook.uid='{$uid}' {$ordselq}\n        ORDER BY transactions.txid {$sort};\n    ";
    $result = do_query($query);
    $first = true;
    $a_total = 0;
    $b_total = 0;
    $commission_total = 0;
    $count = 0;
    while ($row = mysql_fetch_assoc($result)) {
        $count++;
        $who = $row['who'];
        $a_amount = $row['a_amount'];
        $b_amount = $row['b_amount'];
        $a_commission = $row['a_commission'];
        $b_commission = $row['b_commission'];
        if ($who == 'B') {
            list($a_amount, $b_amount) = array($b_amount, $a_amount);
            $b_commission = $a_commission;
        }
        // skip cancelled orders since we already show those
        if ((int) $b_amount == -1) {
            continue;
        }
        if ($first) {
            $first = false;
            ?>
 <div class='content_box'>
            <h3>
<?php 
            if ($is_logged_in == $uid) {
                echo _("Your trades") . " ";
            } else {
                echo _("Trades") . " ";
            }
            if ($orderid) {
                echo _('for this order');
            }
            ?>
</h3>
            <table class='display_data'>
                <tr>
<?php 
            if (!$orderid) {
                ?>
                    <th class='right'><?php 
                echo _("Order");
                ?>
</th>
<?php 
            }
            ?>
                    <th class='right'><?php 
            echo _("You gave");
            ?>
</th>
                    <th class='right'><?php 
            echo _("You got");
            ?>
</th>
                    <th class='right'><?php 
            echo _("Commission");
            ?>
</th>
                    <th class='right'><?php 
            echo _("Price");
            ?>
</th>
                    <th class='center'><?php 
            echo _("Time");
            ?>
</th>
                </tr><?php 
        }
        $a_total = gmp_add($a_total, $a_amount);
        $b_total = gmp_add($b_total, $b_amount);
        $commission_total = gmp_add($commission_total, $b_commission);
        if ($b_amount) {
            $commission_percent = bcdiv(bcmul($b_commission, 100), $b_amount, 3);
        } else {
            $commission_percent = 0;
        }
        $b_amount = gmp_sub($b_amount, $b_commission);
        $type = $row['type'];
        $want_type = $row['want_type'];
        $price = 0;
        if ($type == 'BTC') {
            if ($a_amount) {
                $price = fiat_and_btc_to_price($b_amount, $a_amount);
            }
        } else {
            if ($b_amount) {
                $price = fiat_and_btc_to_price($a_amount, $b_amount);
            }
        }
        $this_orderid = $row['orderid'];
        $timest = $row['timest'];
        $give_precision = $type == 'BTC' ? BTC_PRECISION : FIAT_PRECISION;
        $want_precision = $type == 'BTC' ? FIAT_PRECISION : BTC_PRECISION;
        if (!$orderid) {
            echo "    ", active_table_row("active", "?page=view_order&orderid={$this_orderid}"), "\n";
        } else {
            echo "    <tr>\n";
        }
        echo "        ";
        if (!$orderid) {
            echo "<td class='right'>{$this_orderid}</td>";
        }
        echo "<td class='right'>" . internal_to_numstr($a_amount, $give_precision) . " {$type}</td>";
        echo "<td class='right'>" . internal_to_numstr($b_amount, $want_precision) . " {$want_type}</td>";
        echo "<td class='right'>" . internal_to_numstr($b_commission, $want_precision) . " {$want_type} (", sprintf("%.2f", $commission_percent), "%)</td>";
        echo "<td class='right'>{$price}</td>";
        echo "<td class='right'>{$timest}</td>\n";
        echo "    </tr>\n";
    }
    // if we showed any table at all
    if (!$first) {
        // if we need to show a summary line
        if ($orderid && $count > 1) {
            $commission_percent = bcdiv(bcmul(gmp_strval($commission_total), 100), gmp_strval($b_total), 3);
            $b_total = gmp_sub($b_total, $commission_total);
            $price = 0;
            if ($type == 'BTC') {
                if ($a_total) {
                    $price = fiat_and_btc_to_price($b_total, $a_total);
                }
            } else {
                if ($b_total) {
                    $price = fiat_and_btc_to_price($a_total, $b_total);
                }
            }
            $a_total = internal_to_numstr($a_total, $give_precision);
            $b_total = internal_to_numstr($b_total, $want_precision);
            $commission_total = internal_to_numstr($commission_total, $want_precision);
            echo "    <tr>\n";
            echo "        <td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td>\n";
            echo "        <td></td>\n";
            echo "    </tr>\n";
            echo "    <tr>\n";
            echo "        <td class='right'>{$a_total} {$type}</td><td class='right'>{$b_total} {$want_type}</td><td class='right'>{$commission_total} {$want_type} (", sprintf("%.2f", $commission_percent), "%)</td><td class='right'>{$price}</td>\n";
            echo "        <td></td>\n";
            echo "    </tr>\n";
        }
        echo "</table>\n";
        echo "<p>" . _("The 'you got' column is the amount you received after commission was taken off.") . "</p>";
        echo "<p>" . _("The 'price' column shows the effective price of the trade, after commission.") . "</p>";
        echo "</div>\n";
    }
}