htlog.cgi 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/perl -w
  2. ## Author: Alois Mahdal at zxcvb cz
  3. # Back-end for very primitive remote logging. Front-end is htlogr.pm
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. use CGI;
  15. my $LOG_FILE = '/var/log/htlogr.log';
  16. open my $fh, ">>", $LOG_FILE or die "cannot open log file for appending: $!";
  17. sub stamp {
  18. my $unixtime = ($_[0] ? $_[0] : time );
  19. my ($sec, $min, $hour, $mday, $mon, $year) = localtime $unixtime;
  20. return sprintf (
  21. "%04i-%02i-%02i %02i:%02i:%02i",
  22. $year + 1900, $mon + 1, $mday,
  23. $hour, $min, $sec
  24. );
  25. }
  26. my $q = CGI->new;
  27. my $msg = ( defined $q->param('msg') ? $q->param('msg') : '-' );
  28. my $tag = ( defined $q->param('tag') ? $q->param('tag') : '-' );
  29. my $i = ( defined $q->param('i') ? $q->param('i') : '-' );
  30. my $message = sprintf("Time: %s; Origin: %s; Tag: %s; I: %s; Message: %s\n",
  31. &stamp(time),
  32. $ENV{'REMOTE_ADDR'},
  33. $tag,
  34. $i,
  35. $msg
  36. );
  37. print $fh $message;
  38. my $out = "Message logged: $msg\n";
  39. print $q->header(
  40. -type => 'text/plain',
  41. -expires => 'now',
  42. -content_lentgth => length $out,
  43. -connection => 'close'
  44. );
  45. print $out;
  46. close $fh or die "cannot close log file: $!";