htlog.cgi 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = 'htlog.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') : '-none-' );
  29. my $message = sprintf("Time: %s; Origin: %s; Tag: %s; Message: %s\n",
  30. &stamp(time),
  31. $ENV{'REMOTE_ADDR'},
  32. $tag,
  33. $msg
  34. );
  35. print $fh $message;
  36. print $q->header(
  37. -type => 'text/javascript',
  38. -expires => 'now',
  39. );
  40. print "Message logged: $msg\n";
  41. close $fh or die "cannot close log file: $!";