Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to save log files with a click on the button in the admin page #584

Merged
merged 8 commits into from
Oct 26, 2016
98 changes: 83 additions & 15 deletions Modules/admin/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,27 @@ function admin_controller()
$_SESSION['userid'] = intval(get('id'));
header("Location: ../user/view");
}


else if ($route->action == 'downloadlog')
{
if ($log_enabled) {
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($log_filename) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($log_filename)) {
readfile($log_filename);
}
else
{
echo($log_filename . " does not exist!");
}
exit;
}
}

else if ($route->action == 'getlog')
{
$route->format = "text";
Expand Down Expand Up @@ -105,13 +125,21 @@ function read_file($file, $lines) {
}

else if ($allow_emonpi_admin && $route->action == 'emonpi') {
//put $update_logfile here so it can be referenced in other if statements
//before it was only accesable in the update subaction
//placed some other variables here as well so they are grouped
//together for the emonpi action even though they might not be used
//in the subaction
$update_logfile = "/home/pi/data/emonpiupdate.log";
$backup_logfile = "/home/pi/data/emonpibackup.log";
$update_flag = "/tmp/emoncms-flag-update";
$backup_flag = "/tmp/emonpibackup";
$update_script = "/home/pi/emonpi/service-runner-update.sh";
$backup_file = "/home/pi/data/backup.tar.gz";

if ($route->subaction == 'update' && $session['write'] && $session['admin']) {
$route->format = "text";

$update_flag = "/tmp/emoncms-flag-update";
$update_script = "/home/pi/emonpi/service-runner-update.sh";
$update_logfile = "/home/pi/data/emonpiupdate.log";

$fh = @fopen($update_flag,"w");
if (!$fh) {
$result = "ERROR: Can't write the flag $update_flag.";
Expand All @@ -122,35 +150,75 @@ function read_file($file, $lines) {
@fclose($fh);
}

if ($route->subaction == 'getupdatelog') {
if ($route->subaction == 'getupdatelog' && $session['admin']) {
$route->format = "text";
ob_start();
passthru("cat /home/pi/data/emonpiupdate.log");
passthru("cat " . $update_logfile);
$result = trim(ob_get_clean());
}

if ($route->subaction == 'downloadupdatelog' && $session['admin'])
{
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($update_logfile) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($update_logfile))
{
ob_start();
readfile($update_logfile);
echo(trim(ob_get_clean()));
}
else
{
echo($update_logfile . " does not exist!");
}
exit;
}

if ($route->subaction == 'backup' && $session['write'] && $session['admin']) {
$route->format = "text";
$file = "/tmp/emonpibackup";
$fh = @fopen($file,"w");
if (!$fh) $result = "ERROR: Can't write the flag $file.";
else $result = "Update flag file $file created. Update will start on next cron call in " . (60 - (time() % 60)) . "s...";
$fh = @fopen($backup_flag,"w");
if (!$fh) $result = "ERROR: Can't write the flag $backup_flag.";
else $result = "Update flag file $backup_flag created. Update will start on next cron call in " . (60 - (time() % 60)) . "s...";
@fclose($fh);
}

if ($route->subaction == 'getbackuplog') {
if ($route->subaction == 'getbackuplog' && $session['admin']) {
$route->format = "text";
ob_start();
passthru("cat /home/pi/data/emonpibackup.log");
passthru("cat " . $backup_logfile);
$result = trim(ob_get_clean());
}

if ($route->subaction == 'downloadbackuplog' && $session['admin'])
{
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($backup_logfile) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
flush();
if (file_exists($backup_logfile)) {
ob_start();
readfile($backup_logfile);
echo(trim(ob_get_clean()));
}
else
{
echo($backup_logfile . " does not exist!");
}
exit;
}

if ($route->subaction == "downloadbackup" && $session['write'] && $session['admin']) {
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=backup.tar.gz");
header("Content-Disposition: attachment; filename=\"" . basename($backup_file) . "\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile("/home/pi/data/backup.tar.gz");
readfile($backup_file);
exit;
}
}
Expand Down
4 changes: 3 additions & 1 deletion Modules/admin/admin_main_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function disk_list()
<?php if(is_writable($log_filename)) { ?>
<br>
<button id="getlog" type="button" class="btn btn-info" data-toggle="button" aria-pressed="false" autocomplete="off"><?php echo _('Auto refresh'); ?></button>
<a href="<?php echo $path; ?>admin/downloadlog" class="btn btn-info"><?php echo _('Download log'); ?></a>
<?php } ?>
</td>
</tr>
Expand All @@ -227,7 +228,8 @@ function disk_list()
<p>Note: If using emonBase (Raspberry Pi + RFM69Pi) the updater can still be used to update Emoncms, RFM69Pi firmware will not be changed.</p>
</td>
<td class="buttons" style="border-top: 0px"><br>
<button id="emonpiupdate" class="btn btn-info"><?php echo _('Update Now'); ?></button><br><br>
<button id="emonpiupdate" class="btn btn-info"><?php echo _('Update Now'); ?></button>
<a href="<?php echo $path; ?>admin/emonpi/downloadupdatelog" class="btn btn-info"><?php echo _('Download log'); ?></a><br><br>
</td>
</tr>
<tr>
Expand Down