form_timeout.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # form_timeout.pl - a form timeout based anti-spam module for Oddmuse
  2. #
  3. # Copyright (C) 2014 Aki Goto <tyatsumi@gmail.com>
  4. #
  5. # Original code is in PHP from http://textcaptcha.com/really
  6. # by Rob Tuley <hello@rob.cx>. Used with permission.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. use strict;
  21. use v5.10;
  22. AddModuleDescription('form_timeout.pl', 'Form Timeout Extension');
  23. =head1 DESCRIPTION
  24. This is an anti-spam module for Oddmuse using form timeout method.
  25. Edit permission is timed out in specified duration (default is 30 minutes)
  26. after viewing the edit form. When edit content is posted directly by a spam bot
  27. without viewing the edit form, edit will be denied.
  28. =head1 CONFIGURATION
  29. $FormTimeoutSalt
  30. Mandatory. Token hash salt. Specify arbitrary string.
  31. Default = undef.
  32. $FormTimeoutTimeout
  33. The form timeout in seconds.
  34. Default = 60 * 30 (30 minutes).
  35. =cut
  36. our ($q, $Now, @MyInitVariables);
  37. our ($FormTimeoutSalt, $FormTimeoutTimeout);
  38. use Digest::MD5 qw(md5_hex);
  39. $FormTimeoutSalt = undef;
  40. $FormTimeoutTimeout = 60 * 30; # 30 minutes
  41. push(@MyInitVariables, \&FormTimeoutInitVariables);
  42. sub FormTimeoutInitVariables {
  43. if (!defined($FormTimeoutSalt)) {
  44. ReportError(T('Set $FormTimeoutSalt.'), '500 INTERNAL SERVER ERROR');
  45. }
  46. }
  47. sub FormTimeoutGetHash {
  48. my ($when) = @_;
  49. return md5_hex($FormTimeoutSalt . $when);
  50. }
  51. sub FormTimeoutGetToken {
  52. return $Now . '#' . FormTimeoutGetHash($Now);
  53. }
  54. sub FormTimeoutGetTime {
  55. my ($token) = @_;
  56. my ($when, $hash) = split /#/, $token;
  57. my $valid_hash = FormTimeoutGetHash($when);
  58. if ($hash ne $valid_hash) {
  59. return '';
  60. }
  61. return $when;
  62. }
  63. sub FormTimeoutCheck {
  64. my $token = GetParam('form_timeout_token', '');
  65. my $when = FormTimeoutGetTime($token);
  66. if ($when eq '' || $when < $Now - $FormTimeoutTimeout) {
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. *OldFormTimeoutGetFormStart = \&GetFormStart;
  72. *GetFormStart = \&NewFormTimeoutGetFormStart;
  73. sub NewFormTimeoutGetFormStart {
  74. my ($ignore, $method, $class) = @_;
  75. my $form = OldFormTimeoutGetFormStart($ignore, $method, $class);
  76. my $token = FormTimeoutGetToken();
  77. $form .= $q->input({-type=>'hidden', -name=>'form_timeout_token',
  78. -value=>$token});
  79. return $form;
  80. }
  81. *OldFormTimeoutDoEdit = \&DoEdit;
  82. *DoEdit = \&NewFormTimeoutDoEdit;
  83. sub NewFormTimeoutDoEdit {
  84. my ($id, $newText, $preview) = @_;
  85. if (!FormTimeoutCheck()) {
  86. ReportError(T('Form Timeout'), '403 FORBIDDEN', undef,
  87. $q->p(Ts('Editing not allowed: %s is read-only.', NormalToFree($id))));
  88. }
  89. OldFormTimeoutDoEdit($id, $newText, $preview);
  90. }