Esempio n. 1
0
/**
 * Display an XML error.
 */
function display_xml_error($e)
{
    xml_header();
    ?>
<error time="<?php 
    echo iso_date();
    ?>
">
<?php 
    echo xmlescape($e->getMessage());
    ?>
</error>
  <?php 
    die;
}
Esempio n. 2
0
function api_v1_graphs($graph)
{
    $start_time = microtime(true);
    $result = array();
    /**
     * Graph rendering goes like this:
     * 0. check graph rendering permissions
     * 1. get raw graph data (from a {@link GraphRenderer} through {@link construct_graph_renderer()})
     * 2. apply deltas as necessary
     * 3. add technicals as necessary
     * 4. strip dates outside of the requested ?days parameter (e.g. from extra_days)
     * 5. construct heading and links
     * 6. construct subheading and revise last_updated
     * 7. return data
     * that is, deltas and technicals are done on the server-side; not the client-side.
     */
    $renderer = construct_graph_renderer($graph['graph_type'], $graph['arg0'], $graph['arg0_resolved']);
    // 0. check graph rendering permissions
    if ($renderer->requiresUser()) {
        if (!isset($graph['user_id']) || !$graph['user_id']) {
            throw new GraphException("No user specified for authenticated graph");
        }
        if (!isset($graph['user_hash']) || !$graph['user_hash']) {
            throw new GraphException("No user hash specified for authenticated graph");
        }
        $user = get_user($graph['user_id']);
        if (!$user) {
            throw new GraphException("No such user found");
        }
        if (!has_expected_user_graph_hash($graph['user_hash'], $user)) {
            throw new GraphException("Mismatched user hash for user " . $graph['user_id'] . " with graph type " . $graph['graph_type']);
        }
        if ($renderer->requiresAdmin()) {
            if (!$user['is_admin']) {
                throw new GraphException("Graph requires administrator privileges");
            }
        }
        $renderer->setUser($user['id']);
    }
    if ($renderer->usesDays()) {
        // 0.5 limit 'days' parameter as necessary
        $get_permitted_days = get_permitted_days();
        $has_valid_days = false;
        foreach ($get_permitted_days as $key => $days) {
            if ($days['days'] == $graph['days']) {
                $has_valid_days = true;
            }
        }
        if (!$has_valid_days) {
            throw new GraphException("Invalid days '" . $graph['days'] . "' for graph that requires days");
        }
    }
    // 1. get raw graph data
    try {
        $data = $renderer->getData($graph['days']);
        $original_count = count($data['data']);
        $result['type'] = $renderer->getChartType();
        // 2. apply deltas as necessary
        $data['data'] = calculate_graph_deltas($graph, $data['data'], false);
        // if there is no data, bail out early
        if (count($data['data']) == 0) {
            $result['type'] = 'nodata';
        } else {
            if ($renderer->canHaveTechnicals()) {
                // 3. add technicals as necessary
                // (only if there is at least one point of data, otherwise calculate_technicals() will throw an error)
                $technicals = calculate_technicals($graph, $data['data'], $data['columns'], false);
                $data['columns'] = $technicals['headings'];
                $data['data'] = $technicals['data'];
            }
        }
        // 4. discard early data
        if ($renderer->usesDays()) {
            $data['data'] = discard_early_data($data['data'], $graph['days']);
            $after_discard_count = count($data['data']);
        }
        $result['columns'] = $data['columns'];
        $result['key'] = $data['key'];
        $result['data'] = $data['data'];
        // clean up columns
        foreach ($result['columns'] as $key => $value) {
            $result['columns'][$key]['technical'] = isset($result['columns'][$key]['technical']) && $result['columns'][$key]['technical'] ? true : false;
            if ($result['columns'][$key]['technical']) {
                if (!isset($result['columns'][$key]['type'])) {
                    $result['columns'][$key]['type'] = 'number';
                }
            }
        }
    } catch (NoDataGraphException_AddAccountsAddresses $e) {
        $result['type'] = 'nodata';
        $result['text'] = ct("Either you have not specified any accounts or addresses, or these addresses and accounts have not yet been updated by :site_name.");
        $result['args'] = array(':site_name' => get_site_config('site_name'));
        $result['data'] = array();
        $data['last_updated'] = false;
        $data['add_accounts_addresses'] = true;
    } catch (NoDataGraphException_AddCurrencies $e) {
        $result['type'] = 'nodata';
        $result['text'] = ct("Either you have not enabled this currency, or your summaries for this currency have not yet been updated by :site_name.");
        $result['args'] = array(':site_name' => get_site_config('site_name'));
        $result['data'] = array();
        $data['last_updated'] = false;
        $data['add_more_currencies'] = true;
    }
    // 5. construct heading and links
    $result['heading'] = array('label' => $renderer->getTitle(), 'args' => $renderer->getTitleArgs(), 'url' => $renderer->getURL(), 'title' => $renderer->getLabel());
    if (isset($data['h1'])) {
        $result['h1'] = $data['h1'];
    }
    if (isset($data['h2'])) {
        $result['h2'] = $data['h2'];
    }
    if (isset($data['no_header'])) {
        $result['noHeader'] = $data['no_header'];
    }
    // 6. construct subheading and revise last_updated\
    if ($result['type'] != 'nodata' && $renderer->hasSubheading()) {
        $suffix = "";
        if ($graph['delta'] == 'percent') {
            $suffix .= '%';
        }
        if ($renderer->getCustomSubheading() !== false) {
            $result['subheading'] = number_format_html($renderer->getCustomSubheading(), 4, $suffix);
        } else {
            if ($result['type'] == 'piechart') {
                // sum up the first row and use that as a total
                if (count($data['data']) != 1) {
                    throw new GraphException("Expected one row of data for a piechart, got " . count($data['data']));
                }
                $sum = 0;
                foreach ($data['data'] as $ignored => $row) {
                    foreach ($row as $value) {
                        $sum += $value;
                    }
                }
                $result['subheading'] = number_format_html($sum, 4, $suffix);
            } else {
                $result['subheading'] = format_subheading_values_objects($graph, $data['data'], $data['columns']);
            }
        }
    }
    $result['lastUpdated'] = recent_format_html($data['last_updated']);
    $result['timestamp'] = iso_date();
    $result['classes'] = $renderer->getClasses();
    $result['graph_type'] = $graph['graph_type'];
    if (is_localhost()) {
        $result['_debug'] = $graph;
        if (isset($after_discard_count)) {
            $result['_debug']['data_discarded'] = $original_count - $after_discard_count;
        } else {
            $result['_debug']['data_not_discarded'] = true;
        }
    }
    // make sure that all 'number'-typed data is numeric
    foreach ($result['data'] as $i => $row) {
        foreach ($row as $key => $value) {
            $column = $result['columns'][$key];
            if ($column['type'] == 'number' || $column['type'] == 'percent') {
                $result['data'][$i][$key] = (double) $value;
                if (is_localhost()) {
                    $result['_debug']['number_formatted'] = true;
                }
            }
        }
    }
    // make sure that all data rows are numeric arrays and not objects
    // i.e. reindex everything to be numeric arrays, so they aren't output as JSON objects
    foreach ($result['data'] as $i => $row) {
        $new_row = array_values($row);
        foreach ($row as $key => $value) {
            $new_row[$key] = $value;
        }
        $result['data'][$i] = $new_row;
    }
    // format any extra text from the result
    if (isset($data['add_more_currencies'])) {
        $result['extra'] = array('classes' => 'add_accounts', 'href' => url_for('wizard_currencies'), 'label' => ct("Add more currencies"), 'args' => array());
    }
    if (isset($data['add_accounts_addresses'])) {
        $result['extra'] = array('classes' => 'add_accounts', 'href' => url_for('wizard_accounts'), 'label' => ct("Add accounts and addresses"), 'args' => array());
    }
    // 7. calculate if the graph data may be out of date
    if ($renderer->requiresUser() && $renderer->getUser()) {
        $user = get_user($renderer->getUser());
        if ($user && $renderer->usesSummaries() && (!$user['has_added_account'] || !$user['is_first_report_sent'] || strtotime($user['last_account_change']) > strtotime($user['last_sum_job']))) {
            $result['outofdate'] = true;
        }
    }
    $end_time = microtime(true);
    $time_diff = ($end_time - $start_time) * 1000;
    $result['time'] = (double) number_format_autoprecision($time_diff, 1, '.', '');
    $result['hash'] = $graph['hash'];
    // 7. return data
    return $result;
}
Esempio n. 3
0
<?php

/**
 * An existing free user has not logged in within X days and we
 * now need to disable their account.
 */
// get the relevant user info
$user = get_user($job['arg_id']);
if (!$user) {
    throw new JobException("Cannot find user ID " . $job['arg_id']);
}
// check that they're not a premium user etc - this should never happen
if ($user['is_premium']) {
    throw new JobException("Premium user was requested to be disabled - this should not happen");
}
// update user (before sending email)
$q = db()->prepare("UPDATE user_properties SET is_disabled=1,disabled_at=NOW() WHERE id=? LIMIT 1");
$q->execute(array($user['id']));
// construct email
if ($user['email']) {
    $disables_at = strtotime(($user['last_login'] ? $user['last_login'] : $user['created_at']) . " +" . get_site_config('user_expiry_days') . " day");
    send_user_email($user, "disable", array("name" => $user['name'] ? $user['name'] : $user['email'], "days" => number_format(get_site_config('user_expiry_days')), "disables" => iso_date($disables_at), "disables_text" => recent_format($disables_at, false, ""), "url" => absolute_url(url_for("user#user_premium")), "login" => absolute_url(url_for("login")), "profile" => absolute_url(url_for("profile"))));
    crypto_log("Sent disabled account e-mail to " . htmlspecialchars($user['email']) . ".");
} else {
    crypto_log("User had no valid e-mail address.");
}
Esempio n. 4
0
 * is disabled.
 */
// get the relevant user info
$user = get_user($job['arg_id']);
if (!$user) {
    throw new JobException("Cannot find user ID " . $job['arg_id']);
}
// check that they're not a premium user etc - this should never happen
if ($user['is_premium']) {
    throw new JobException("Premium user was requested to be warned of disabled - this should not happen");
} else {
    if ($user['is_disabled']) {
        throw new JobException("Disabled user was requested to be warned of disabled - this should not happen");
    }
}
$disables_at = strtotime(($user['last_login'] ? $user['last_login'] : $user['created_at']) . " +" . get_site_config('user_expiry_days') . " day");
// update user (before sending email)
$q = db()->prepare("UPDATE user_properties SET is_disable_warned=1,disable_warned_at=NOW() WHERE id=? LIMIT 1");
$q->execute(array($user['id']));
if ($disables_at > time()) {
    // there's no point in sending an email if it's going to be disabled in the past; it will be disabled on our very next run anyway
    // construct email
    if ($user['email']) {
        send_user_email($user, "disable_warning", array("name" => $user['name'] ? $user['name'] : $user['email'], "days" => number_format(get_site_config('user_expiry_days')), "disables" => iso_date($disables_at), "disables_text" => recent_format($disables_at, false, ""), "url" => absolute_url(url_for("user#user_premium")), "login" => absolute_url(url_for("login"))));
        crypto_log("Sent disable warning soon e-mail to " . htmlspecialchars($user['email']) . ".");
    } else {
        crypto_log("User had no valid e-mail address.");
    }
} else {
    crypto_log("Did not send any disable warning: disable time is set into the past (" . iso_date($disables_at) . ")");
}
Esempio n. 5
0
<?php

/**
 * An existing premium user's account needs to expire.
 * May send out an e-mail.
 */
// get the relevant user info
$user = get_user($job['arg_id']);
if (!$user) {
    throw new JobException("Cannot find user ID " . $job['arg_id']);
}
$was_premium = $user['is_premium'];
// update user (before sending email)
$q = db()->prepare("UPDATE user_properties SET updated_at=NOW(),is_premium=0 WHERE id=? LIMIT 1");
$q->execute(array($user['id']));
crypto_log("Disabled premium status on user " . $user['id'] . ".");
// construct email, but only if we haven't already sent an email out
if ($user['email'] && $was_premium) {
    send_user_email($user, "expire", array("name" => $user['name'] ? $user['name'] : $user['email'], "expires" => iso_date($user['premium_expires']), "expires_text" => recent_format($user['premium_expires'], false, ""), "prices" => get_text_premium_prices(), "prices_html" => get_html_premium_prices(), "url" => absolute_url(url_for("user#user_premium"))));
    crypto_log("Sent premium expired e-mail to " . htmlspecialchars($user['email']) . ".");
} else {
    crypto_log("User had no valid e-mail address.");
}
Esempio n. 6
0
foreach ($cleaned_blocks as $line) {
    $m = date('Y-m', $line['start']);
    if (!isset($months[$m])) {
        $months[$m] = array('blocks' => 0, 'seconds' => 0, 'revisions' => 0, 'start' => $line['start'], 'end' => $line['end']);
    }
    $months[$m]['blocks']++;
    $months[$m]['seconds'] += $line['end'] - $line['start'];
    $months[$m]['revisions'] += $line['revisions'];
    $months[$m]['start'] = min($months[$m]['start'], $line['start']);
    $months[$m]['end'] = max($months[$m]['end'], $line['end']);
}
// print months as CSV
$fp = fopen("months.csv", "w");
fwrite($fp, csv_array(array("Month", "Blocks", "Seconds", "Hours", "Revisions", "Block Start", "Block End")));
foreach ($months as $m => $line) {
    fwrite($fp, csv_array(array($m, $line['blocks'], $line['seconds'], $line['seconds'] / 3600, $line['revisions'], iso_date($line['start']), iso_date($line['end']))));
}
echo "Wrote months.csv with " . number_format(count($months)) . " months...\n";
fclose($fp);
function csv($s)
{
    return "\"" . str_replace("\"", "\"\"", $s) . "\"";
}
function csv_array($array)
{
    $r = array();
    foreach ($array as $value) {
        $r[] = csv($value);
    }
    return implode(",", $r) . "\n";
}