GravatarPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009,2011 StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * @package GravatarPlugin
  21. * @maintainer Eric Helgeson <erichelgeson@gmail.com>
  22. */
  23. if (!defined('STATUSNET') && !defined('LACONICA')) {
  24. // This check helps protect against security problems;
  25. // your code file can't be executed directly from the web.
  26. exit(1);
  27. }
  28. class GravatarPlugin extends Plugin
  29. {
  30. function onEndProfileGetAvatar($profile, $size, &$avatar)
  31. {
  32. if (empty($avatar)) {
  33. try {
  34. $user = $profile->getUser();
  35. if (!empty($user->email)) {
  36. // Fake one!
  37. $avatar = new Avatar();
  38. $avatar->width = $avatar->height = $size;
  39. $avatar->url = $this->gravatar_url($user->email, $size);
  40. return false;
  41. }
  42. } catch (NoSuchUserException $e) {
  43. return true;
  44. }
  45. }
  46. return true;
  47. }
  48. function gravatar_url($email, $size)
  49. {
  50. $url = "https://secure.gravatar.com/avatar.php?gravatar_id=".
  51. md5(strtolower($email)).
  52. "&default=".urlencode(Avatar::defaultImage($size)).
  53. "&size=".$size;
  54. return $url;
  55. }
  56. function onPluginVersion(&$versions)
  57. {
  58. $versions[] = array('name' => 'Gravatar',
  59. 'version' => GNUSOCIAL_VERSION,
  60. 'author' => 'Eric Helgeson, Evan Prodromou',
  61. 'homepage' => 'http://status.net/wiki/Plugin:Gravatar',
  62. 'rawdescription' =>
  63. // TRANS: Plugin decsription.
  64. _m('The Gravatar plugin allows users to use their <a href="http://www.gravatar.com/">Gravatar</a> with StatusNet.'));
  65. return true;
  66. }
  67. }