<commit_before><?php
/**
 * Bitcoin Status Page - Peer Stats
 *
 * @category File
 * @package  BitcoinStatus
 * @author   <NAME> <<EMAIL>>
 * @license  https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @link     https://github.com/craigwatson/bitcoind-status
 */

 // Get config
 require_once './php/config.php';

 // Die if we're not in the whitelist
if (!in_array($_SERVER['REMOTE_ADDR'], $config['peercount_whitelist'])) {
    die($_SERVER['REMOTE_ADDR'] . " is not in the whitelist");
}

// Clear data if variable present
if (isset($_GET['clear']) & is_file($config['peercount_file'])) {
    unlink($config['peercount_file']);
}

// Check for existing data
if (is_file($config['peercount_file'])) {
    $data = json_decode(file_get_contents($config['peercount_file']), true);
} else {
    $data = array();
}

// If viewing is enabled, just output and die
if (isset($_GET['view'])) {
    print_r($data);
    die();
}

// Include EasyBitcoin library and set up connection
require_once '/srv/www/bitcoind-status/php/easybitcoin.php';
$bitcoin = new Bitcoin($config['rpc_user'], $config['rpc_pass'], $config['rpc_host'], $config['rpc_port']);

// Setup SSL if configured
if ($config['rpc_ssl'] === true) {
    $bitcoin->setSSL($config['rpc_ssl_ca']);
}

// Get data via RPC
$new_peers = $bitcoin->getpeerinfo();

// Initialise arrays
$to_insert = array(
    'time'    => time(),
    'classic' => 0,
    'core'    => 0,
    'other'   => 0
);

 // Loop through peers
foreach ($new_peers as $peer) {
    if (strpos($peer['subver'], "Satoshi") !== false) {
        $peer_type = 'core';
    } elseif (strpos($peer['subver'], "Classic") !== false) {
        $peer_type = 'classic';
    } else {
        $peer_type = 'other';
    }

    // Increment counters
    $to_insert['counts'][$peer_type] = $to_insert['counts'][$peer_type]+1;
}

// Insert data
$data[] = $to_insert;

// Purge old data
for ($i = 0; $i < count($data); $i++) {
    if ($data[$i]['time'] < (time() - $config['peercount_max_age'])) {
        array_splice($data, $i, 1);
    }
}

// Save array
if (file_put_contents($config['peercount_file'], json_encode($data), LOCK_EX) === false) {
    die("Failure storing data");
}<commit_msg>Fixing storage bug<commit_after><?php
/**
 * Bitcoin Status Page - Peer Stats
 *
 * @category File
 * @package  BitcoinStatus
 * @author   <NAME> <<EMAIL>>
 * @license  https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @link     https://github.com/craigwatson/bitcoind-status
 */

 // Get config
 require_once './php/config.php';

 // Die if we're not in the whitelist
if (!in_array($_SERVER['REMOTE_ADDR'], $config['peercount_whitelist'])) {
    die($_SERVER['REMOTE_ADDR'] . " is not in the whitelist");
}

// Clear data if variable present
if (isset($_GET['clear']) & is_file($config['peercount_file'])) {
    unlink($config['peercount_file']);
}

// Check for existing data
if (is_file($config['peercount_file'])) {
    $data = json_decode(file_get_contents($config['peercount_file']), true);
} else {
    $data = array();
}

// If viewing is enabled, just output and die
if (isset($_GET['view'])) {
    print_r($data);
    die();
}

// Include EasyBitcoin library and set up connection
require_once '/srv/www/bitcoind-status/php/easybitcoin.php';
$bitcoin = new Bitcoin($config['rpc_user'], $config['rpc_pass'], $config['rpc_host'], $config['rpc_port']);

// Setup SSL if configured
if ($config['rpc_ssl'] === true) {
    $bitcoin->setSSL($config['rpc_ssl_ca']);
}

// Get data via RPC
$new_peers = $bitcoin->getpeerinfo();

// Initialise arrays
$to_insert = array(
    'time'    => time(),
    'classic' => 0,
    'core'    => 0,
    'other'   => 0
);

 // Loop through peers
foreach ($new_peers as $peer) {
    if (strpos($peer['subver'], "Satoshi") !== false) {
        $peer_type = 'core';
    } elseif (strpos($peer['subver'], "Classic") !== false) {
        $peer_type = 'classic';
    } else {
        $peer_type = 'other';
    }

    // Increment counters
    $to_insert[$peer_type] = $to_insert[$peer_type]+1;
}

// Insert data
$data[] = $to_insert;

// Purge old data
for ($i = 0; $i < count($data); $i++) {
    if ($data[$i]['time'] < (time() - $config['peercount_max_age'])) {
        array_splice($data, $i, 1);
    }
}

// Save array
if (file_put_contents($config['peercount_file'], json_encode($data), LOCK_EX) === false) {
    die("Failure storing data");
}
{'fraction_non_alphanumeric': 0.1954308979868808, 'fraction_numerical': 0.004523863379325944, 'mean_word_length': 3.7805405405405406, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1299399', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1530, 'n_words': 344}