Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
mortzu committed May 3, 2022
1 parent 442b3d2 commit e7dec08
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 115 deletions.
2 changes: 1 addition & 1 deletion config.defaults.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$config['sync_with_nodes_json'] = 'https://example.org/nodes.json';
$config['meshviewer_json_url'] = '';
$config['url_base'] = 'https://example.org/';
$config['view_additional_css'] = array();
$config['view_footer'] = '';
Expand Down
121 changes: 62 additions & 59 deletions cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php

/*
2018, mortzu <mortzu@gmx.de>.
2018 - 2022, mortzu <mortzu@uni-bremen.de>.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
Expand Down Expand Up @@ -39,68 +39,71 @@
require_once __DIR__ . '/config.defaults.php';
require_once __DIR__ . '/config.php';

if (!empty($config['sync_with_nodes_json'])) {
$nodes_list = array();

if (FALSE === $node_json = file_get_contents($config['sync_with_nodes_json']))
error_log('Could not fetch nodes.json');
elseif (empty($node_json))
error_log('Fetched nodes.json is empty');
else {
$node_json_decoded = json_decode($node_json, true);

if (!is_array($node_json_decoded))
error_log('Fetched nodes.json is not valid JSON');
else
foreach ($node_json_decoded['nodes'] as $nodes)
array_push($nodes_list, strtolower(preg_replace('/[^A-Za-z0-9-]/', '-', $nodes['nodeinfo']['hostname'])));
}
$nodes_list = array();

if (FALSE === $meshviewer_json = file_get_contents($config['meshviewer_json_url'])) {
error_log('Could not fetch meshviewer.json');
exit(1);
} elseif (empty($meshviewer_json)) {
error_log('Fetched meshviewer.json is empty');
exit(1);
} else {
$meshviewer_data = json_decode($meshviewer_json, true);

if (!is_array($meshviewer_data)) {
error_log('Could not fetch json');
exit(1);
}
}

foreach ($meshviewer_data['nodes'] as $nodes)
array_push($nodes_list, strtolower(preg_replace('/[^A-Za-z0-9-]/', '-', $nodes['hostname'])));

foreach (glob(__DIR__ . '/data/verified/*') as $node_file) {
$node_status_current = 0;
$node_name = basename($node_file);
$node_status_current = 0;
$node_name = basename($node_file);

if (!empty($config['sync_with_nodes_json'])) {
if (!in_array($node_name, $nodes_list)) {
error_log('Moved ' . $node_name . ' to deleted');
rename($node_file, __DIR__ . '/data/deleted/' . basename($node_name));
continue;
error_log('Moved ' . $node_name . ' to deleted');
rename($node_file, __DIR__ . '/data/deleted/' . basename($node_name));
continue;
}
}

$json_decoded = json_decode(file_get_contents($node_file), true);

if (!is_array($json_decoded))
continue;

$email_to = $json_decoded['mail'];
$node_status_last = (isset($json_decoded['status']) ? $json_decoded['status'] : 0);

system('ping6 -c5 -W5 ' . $node_name . '.' . $config['domain_suffix'] . ' >/dev/null 2>&1', $return_val);

if ($return_val == 0)
$node_status_current = 1;
else {
if ($node_status_last == 0) {
$mail = new PHPMailer;
$mail->isSendmail();
$mail->CharSet = 'utf-8';
$mail->setFrom($config['email_from']);
$mail->addAddress($email_to);
$mail->isHTML(false);
$mail->Subject = str_replace('___NODENAME___', $node_name, $config['email_subject_offline']);
$mail->Body = str_replace(array('___NODENAME___', '___EMAIL___', '___LINK_DELETE___'), array($node_name, $email_to, $config['url_base'] . '?delete&token=' . $json_decoded['token']), $config['email_message_offline']);
$mail->send();

$node_status_current = 2;
} elseif ($node_status_last == 2)
$node_status_current = 2;
else
$node_status_current = 0;
}

$json_decoded['status'] = $node_status_current;

file_put_contents($node_file, json_encode($json_decoded));

$json_decoded = json_decode(file_get_contents($node_file), true);

if (!is_array($json_decoded))
continue;

$email_to = $json_decoded['mail'];
$node_status_last = (isset($json_decoded['status']) ? $json_decoded['status'] : 0);

foreach ($meshviewer_data['nodes'] as $nodes) {
if (($nodes['hostname'] == $node_name) && ($nodes['is_online'] == true)) {
$node_status_current = 1;
break;
}
}

if ($node_status_current != 1) {
if ($node_status_last == 0) {
$mail = new PHPMailer;
$mail->isSendmail();
$mail->CharSet = 'utf-8';
$mail->setFrom($config['email_from']);
$mail->addAddress($email_to);
$mail->isHTML(false);
$mail->Subject = str_replace('___NODENAME___', $node_name, $config['email_subject_offline']);
$mail->Body = str_replace(array('___NODENAME___', '___EMAIL___', '___LINK_DELETE___'), array($node_name, $email_to, $config['url_base'] . '?delete&token=' . $json_decoded['token']), $config['email_message_offline']);
$mail->send();

$node_status_current = 2;
} elseif ($node_status_last == 2)
$node_status_current = 2;
else
$node_status_current = 0;
}

$json_decoded['status'] = $node_status_current;

file_put_contents($node_file, json_encode($json_decoded));
}
108 changes: 53 additions & 55 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
2018, mortzu <mortzu@gmx.de>.
2018 - 2022, mortzu <mortzu@uni-bremen.de>.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
Expand Down Expand Up @@ -46,11 +46,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="vendor/twbs/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="vendor/simonwhitaker/github-fork-ribbon-css/gh-fork-ribbon.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Arvo" type="text/css">
<?php
foreach ($config['view_additional_css'] as $additional_css)
echo " <link rel=\"stylesheet\" href=\"{$additional_css}\" type=\"text/css\">\n";
echo " <link rel=\"stylesheet\" href=\"{$additional_css}\" type=\"text/css\">\n";
?>
<title><?php echo $config['view_title']; ?> - <?php echo $config['view_subtitle']; ?></title>
</head>
Expand All @@ -60,13 +58,13 @@

<div class="container">
<?php
echo $config['view_header_pre'];
echo $config['view_header_pre'];
?>
<hgroup>
<h1><?php echo $config['view_title']; ?></h1>
<h2><?php echo $config['view_subtitle']; ?></h2>
<?php
echo $config['view_header'];
echo $config['view_header'];
?>
</hgroup>

Expand All @@ -75,63 +73,63 @@
<?php

if (isset($_GET['delete']) && isset($_GET['token']) && !empty($_GET['token'])) {
foreach (glob(__DIR__ . '/data/verified/*') as $filename) {
$json_decoded = json_decode(file_get_contents($filename), true);
foreach (glob(__DIR__ . '/data/verified/*') as $filename) {
$json_decoded = json_decode(file_get_contents($filename), true);

if ($_GET['token'] == $json_decoded['token']) {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_deleted'] . '</div>';
unlink($filename);
if ($_GET['token'] == $json_decoded['token']) {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_deleted'] . '</div>';
unlink($filename);
}
}
}
} elseif (isset($_GET['token']) && !empty($_GET['token'])) {
foreach (glob(__DIR__ . '/data/pending/*') as $filename) {
$json_decoded = json_decode(file_get_contents($filename), true);
foreach (glob(__DIR__ . '/data/pending/*') as $filename) {
$json_decoded = json_decode(file_get_contents($filename), true);

if ($_GET['token'] == $json_decoded['token']) {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_confirmed'] . '</div>';
rename($filename, __DIR__ . '/data/verified/' . basename($filename));
if ($_GET['token'] == $json_decoded['token']) {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_confirmed'] . '</div>';
rename($filename, __DIR__ . '/data/verified/' . basename($filename));
}
}
}
} elseif (isset($_POST['nodename']) && !empty($_POST['nodename'])) {
$nodename = strtolower(preg_replace('/[^A-Za-z0-9-]/', '-', $_POST['nodename']));

if (false === $nodeinfo = file_get_contents('http://' . $nodename . '.' . $config['domain_suffix'] . '/cgi-bin/status')) {
if (false === $nodeinfo = file_get_contents('http://' . $nodename . '.' . $config['domain_suffix'] . '/cgi-bin/nodeinfo'))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_noconnect'] . '</div>';
elseif (NULL === $nodeinfo_json = json_decode($nodeinfo, true))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_parse'] . '</div>';
elseif (!isset($nodeinfo_json['owner']['contact']) || empty($nodeinfo_json['owner']['contact']))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_nomail'] . '</div>';
$nodename = strtolower(preg_replace('/[^A-Za-z0-9-]/', '-', $_POST['nodename']));

if (false === $nodeinfo = file_get_contents('http://' . $nodename . '.' . $config['domain_suffix'] . '/cgi-bin/status')) {
if (false === $nodeinfo = file_get_contents('http://' . $nodename . '.' . $config['domain_suffix'] . '/cgi-bin/nodeinfo'))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_noconnect'] . '</div>';
elseif (NULL === $nodeinfo_json = json_decode($nodeinfo, true))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_parse'] . '</div>';
elseif (!isset($nodeinfo_json['owner']['contact']) || empty($nodeinfo_json['owner']['contact']))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_nomail'] . '</div>';
else
$nodecontact = strtolower($nodeinfo_json['owner']['contact']);
} elseif (false === preg_match('/<dt>Contact<\/dt><dd>(.*)<\/dd>/', $nodeinfo, $nodecontact_array))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_nomail'] . '</div>';
else
$nodecontact = strtolower($nodeinfo_json['owner']['contact']);
} elseif (false === preg_match('/<dt>Contact<\/dt><dd>(.*)<\/dd>/', $nodeinfo, $nodecontact_array))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_nomail'] . '</div>';
else
$nodecontact = strtolower($nodecontact_array[1]);

if (!empty($nodecontact)) {
if (!filter_var($nodecontact, FILTER_VALIDATE_EMAIL))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_novalidmail'] . '</div>';
elseif (file_exists(__DIR__ . '/data/pending/' . $nodename))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_already'] . '</div>';
else {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_confirmation'] . '</div>';

$token = md5(time());

file_put_contents(__DIR__ . '/data/pending/' . $nodename, json_encode(array('token' => $token, 'mail' => $nodecontact)));

$mail = new PHPMailer;
$mail->isSendmail();
$mail->CharSet = 'utf-8';
$mail->setFrom($config['email_from']);
$mail->addAddress($nodecontact);
$mail->isHTML(false);
$mail->Subject = $config['email_subject_confirmation'];
$mail->Body = str_replace(array('___LINK_CONFIRM___', '___LINK_DELETE___', '___EMAIL___'), array($_SERVER['SCRIPT_URI'] . '?token=' . $token, $_SERVER['SCRIPT_URI'] . '?delete&token=' . $token, $nodecontact), $config['email_message_confirmation']);
$mail->send();
$nodecontact = strtolower($nodecontact_array[1]);

if (!empty($nodecontact)) {
if (!filter_var($nodecontact, FILTER_VALIDATE_EMAIL))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_novalidmail'] . '</div>';
elseif (file_exists(__DIR__ . '/data/pending/' . $nodename))
echo '<div class="alert alert-danger" role="alert">' . $config['view_text_node_already'] . '</div>';
else {
echo '<div class="alert alert-success" role="alert">' . $config['view_text_confirmation'] . '</div>';

$token = md5(time());

file_put_contents(__DIR__ . '/data/pending/' . $nodename, json_encode(array('token' => $token, 'mail' => $nodecontact)));

$mail = new PHPMailer;
$mail->isSendmail();
$mail->CharSet = 'utf-8';
$mail->setFrom($config['email_from']);
$mail->addAddress($nodecontact);
$mail->isHTML(false);
$mail->Subject = $config['email_subject_confirmation'];
$mail->Body = str_replace(array('___LINK_CONFIRM___', '___LINK_DELETE___', '___EMAIL___'), array($_SERVER['SCRIPT_URI'] . '?token=' . $token, $_SERVER['SCRIPT_URI'] . '?delete&token=' . $token, $nodecontact), $config['email_message_confirmation']);
$mail->send();
}
}
}
} else {

?>
Expand Down

0 comments on commit e7dec08

Please sign in to comment.