gravatar.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2010–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('gravatar.pl', 'Gravatar');
  17. use Digest::MD5 qw(md5_hex);
  18. our ($q, $bol, %CookieParameters, $FullUrlPattern, @MyRules, @MyInitVariables, @MyFormChanges);
  19. # Same as in mail.pl
  20. $CookieParameters{mail} = '';
  21. my $gravatar_regexp = "\\[\\[gravatar:(?:$FullUrlPattern )?([^\n:]+):([0-9a-f]+)\\]\\]";
  22. push(@MyRules, \&GravatarRule);
  23. sub GravatarRule {
  24. if ($bol && m!\G$gravatar_regexp!cg) {
  25. my $url = $1;
  26. my $gravatar = "https://secure.gravatar.com/avatar/$3";
  27. my $name = FreeToNormal($2);
  28. $url = ScriptUrl($name) unless $url;
  29. return $q->span({-class=>"portrait gravatar"},
  30. $q->a({-href=>$url,
  31. -class=>'newauthor'},
  32. $q->img({-src=>$gravatar,
  33. -class=>'portrait',
  34. -alt=>''})),
  35. $q->br(),
  36. GetPageLink($name));
  37. }
  38. return;
  39. }
  40. sub GravatarFormAddition {
  41. my ($html, $type) = @_;
  42. # gravatars only make sense for comments
  43. return $html unless $type eq 'comment';
  44. my $addition = $q->span({-class=>'mail'},
  45. $q->label({-for=>'mail'}, T('Email:') . ' ')
  46. . ' ' . $q->textfield(-name=>'mail', -id=>'mail',
  47. -default=>GetParam('mail', '')));
  48. $html =~ s!(name="homepage".*?)</p>!$1 $addition</p>!i;
  49. return $html;
  50. }
  51. push(@MyInitVariables, \&AddGravatar);
  52. sub AddGravatar {
  53. # the implementation in mail.pl takes precedence!
  54. if (not grep { $_ == \&MailFormAddition } @MyFormChanges) {
  55. push(@MyFormChanges, \&GravatarFormAddition);
  56. }
  57. my $aftertext = UnquoteHtml(GetParam('aftertext'));
  58. my $mail = GetParam('mail');
  59. $mail =~ s/^[ \t]+//;
  60. $mail =~ s/[ \t]+$//;
  61. my $gravatar = md5_hex(lc($mail));
  62. my $username = GetParam('username');
  63. my $homepage = GetParam('homepage');
  64. $homepage = 'http://' . $homepage
  65. if $homepage and $homepage !~ m!^https?://!i;
  66. $homepage .= ' ' if $homepage;
  67. if ($aftertext && $mail && $aftertext !~ /^\[\[gravatar:/) {
  68. SetParam('aftertext',
  69. "[[gravatar:$homepage $username:$gravatar]]\n$aftertext");
  70. }
  71. }
  72. *GravatarOldGetSummary = \&GetSummary;
  73. *GetSummary = \&GravatarNewGetSummary;
  74. sub GravatarNewGetSummary {
  75. my $summary = GravatarOldGetSummary(@_);
  76. $summary =~ s/^$gravatar_regexp *//;
  77. return $summary;
  78. }