}
}
//#############################################################################
//#############################################################################
//
//                               MAIN PROGRAM
//
//#############################################################################
//#############################################################################
$lastrefresh = gmdate('Y-m-d H:i:s');
$starttime = microtime(true);
xecho("Dash Ninja Control [dmnctl] v" . DMN_VERSION . " (" . date('Y-m-d H:i:s', filemtime(__FILE__)) . ")\n");
if ($argc > 1) {
    xecho("Querying list of nodes for this hub: ");
    $params = array();
    $content = dmn_cmd_get('nodes', $params, $response);
    $nodes = array();
    if (strlen($content) > 0) {
        $content = json_decode($content, true);
        if ($response['http_code'] >= 200 && $response['http_code'] <= 299) {
            $nodes = $content['data'];
            echo "Success (" . count($nodes) . " nodes)\n";
        } elseif ($response['http_code'] >= 400 && $response['http_code'] <= 499) {
            echo "Error (" . $response['http_code'] . ": " . $content['message'] . ")\n";
        }
    } else {
        echo "Error (empty result) [HTTP CODE " . $response['http_code'] . "]\n";
    }
    unset($content, $response, $params);
    $dmnpidstatus = dmn_getpids($nodes, strcasecmp($argv[1], 'status') == 0);
    $dmnpid = $dmnpidstatus;
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
   Dash Ninja is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
    along with Dash Ninja.  If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('DMN_SCRIPT') || !defined('DMN_CONFIG') || DMN_SCRIPT !== true || DMN_CONFIG !== true) {
    die('Not executable');
}
define('DMN_VERSION', '1.0.1');
xecho('dmnblockdegapper v' . DMN_VERSION . "\n");
xecho('Retrieving nodes for this hub: ');
$result = dmn_cmd_get('/nodes', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $nodes = json_decode($result, true);
    if ($nodes === false) {
        echo " Failed to JSON decode!\n";
        die(200);
    } elseif (!is_array($nodes) || !array_key_exists('data', $nodes) || !is_array($nodes['data'])) {
        echo " Incorrect data!\n";
        die(202);
    }
    $nodes = $nodes['data'];
    echo " OK (" . count($nodes) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
    $poolpubkeys = $poolpubkeys['data']['poolpubkeys'];
    echo " OK (" . count($mnpubkeys) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
        $result = json_decode($result, true);
        if ($result !== false) {
            foreach ($result['messages'] as $num => $msg) {
                xecho("Error #{$num}: {$msg}\n");
            }
        }
    }
    die(203);
}
xecho('Retrieving superblocks expected: ');
$result = dmn_cmd_get('/budgetsexpected', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $mnsuperblocks = json_decode($result, true);
    if ($mnsuperblocks === false) {
        echo " Failed to JSON decode!\n";
        die(210);
    } elseif (!is_array($mnsuperblocks) || !array_key_exists('data', $mnsuperblocks) || !is_array($mnsuperblocks['data']) || !array_key_exists('budgetsexpected', $mnsuperblocks['data']) || !is_array($mnsuperblocks['data']['budgetsexpected'])) {
        echo " Incorrect data!\n";
        die(212);
    }
    $mnsuperblocks = $mnsuperblocks['data']['budgetsexpected'];
    echo " OK (" . count($mnsuperblocks) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
        $res = 3;
    } catch (Exception $e) {
        $errmsg = $e->getMessage();
        if (strpos($errmsg, 'timed out') !== false) {
            $res = 2;
        }
    }
    return $res;
}
xecho("dmnportcheckdo v" . DMN_VERSION . "\n");
if ($argc != 4 && $argc != 5) {
    xecho("Usage: " . basename($argv[0]) . " ip port testnet [outputfile]\n");
    die;
}
xecho("Retrieving configuration: ");
$result = dmn_cmd_get('/portcheck/config', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $config = json_decode($result, true);
    if ($config === false) {
        echo " Failed to JSON decode!\n";
        die(100);
    } elseif (!is_array($config) || !array_key_exists('data', $config) || !is_array($config['data'])) {
        echo " Incorrect data!\n";
        die(102);
    }
    echo " OK\n";
    $config = $config['data'];
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
    xecho(" (100% - " . count($commands) . "/" . count($commands) . ") Done in " . round(microtime(true) - $inittime, 3) . " seconds [{$nbok} sucessfully/{$nberr} with errors]\n");
}
xecho("dmnportcheck v" . DMN_VERSION . "\n");
if ($argc < 2 || $argv[1] != 'db' && $argv[1] != 'nodb') {
    xecho("Usage: " . basename($argv[0]) . " [no]db [ip:port:testnet]+\n");
    die;
}
if ($argv[1] == 'db') {
    if (file_exists(DMN_PORTCHECK_SEMAPHORE) && posix_getpgid(intval(file_get_contents(DMN_PORTCHECK_SEMAPHORE))) !== false) {
        xecho("Already running (PID " . sprintf('%d', file_get_contents(DMN_PORTCHECK_SEMAPHORE)) . ")\n");
        die(10);
    }
    file_put_contents(DMN_PORTCHECK_SEMAPHORE, sprintf('%s', getmypid()));
}
xecho('Retrieving MN port check: ');
$result = dmn_cmd_get('/portcheck/list', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $mnportcheck = json_decode($result, true);
    if ($mnportcheck === false) {
        echo " Failed to JSON decode!\n";
        die(200);
    } elseif (!is_array($mnportcheck) || !array_key_exists('data', $mnportcheck) || !is_array($mnportcheck['data'])) {
        echo " Incorrect data!\n";
        die(202);
    }
    $mnportcheck = $mnportcheck['data'];
    echo " OK (" . count($mnportcheck) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
    echo " OK (PubKeys: Testnet=" . count($tnpubkeys) . ")\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
        $result = json_decode($result, true);
        var_dump($result);
        if ($result !== false) {
            foreach ($result['messages'] as $num => $msg) {
                xecho("Error #{$num}: {$msg}\n");
            }
        }
    }
    die(201);
}
xecho('Retrieving MN balance: ');
$result = dmn_cmd_get('/balances', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $mnbal = json_decode($result, true);
    if ($mnbal === false) {
        echo " Failed to JSON decode!\n";
        die(200);
    } elseif (!is_array($mnlist) || !array_key_exists('data', $mnbal) || !is_array($mnbal['data']) || !array_key_exists('balances', $mnbal['data']) || !is_array($mnbal['data']['balances']) || !array_key_exists('mainnet', $mnbal['data']['balances']) || !is_array($mnbal['data']['balances']['mainnet']) || !array_key_exists('testnet', $mnbal['data']['balances']) || !is_array($mnbal['data']['balances']['testnet'])) {
        echo " Incorrect data!\n";
        die(202);
    }
    $mnlastupdate = $mnbal['data']['balances']['mainnet'];
    $tnlastupdate = $mnbal['data']['balances']['testnet'];
    echo " OK (Testnet=" . count($tnlastupdate) . " Mainnet=" . count($mnlastupdate) . ")\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    $mndonations = $mndonations['data']['mndonations'];
    echo " OK (" . count($mndonations) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {
        $result = json_decode($result, true);
        if ($result !== false) {
            foreach ($result['messages'] as $num => $msg) {
                xecho("Error #{$num}: {$msg}\n");
            }
        }
    }
    die(204);
}
xecho('Retrieving pools pubkeys: ');
$result = dmn_cmd_get('/pools', array(), $response);
if ($response['http_code'] == 200) {
    echo "Fetched...";
    $poolpubkeys = json_decode($result, true);
    if ($poolpubkeys === false) {
        echo " Failed to JSON decode!\n";
        die(200);
    } elseif (!is_array($poolpubkeys) || !array_key_exists('data', $poolpubkeys) || !is_array($poolpubkeys['data']) || !array_key_exists('poolpubkeys', $poolpubkeys['data']) || !is_array($poolpubkeys['data']['poolpubkeys'])) {
        echo " Incorrect data!\n";
        die(202);
    }
    $poolpubkeys = $poolpubkeys['data']['poolpubkeys'];
    echo " OK (" . count($mnpubkeys) . " entries)\n";
} else {
    echo "Failed [" . $response['http_code'] . "]\n";
    if ($response['http_code'] != 500) {