/plugins/Gravatar/default.php

https://github.com/ghoppe/Garden · PHP · 83 lines · 67 code · 8 blank · 8 comment · 7 complexity · d8f72727d0c75613afb730be30286575 MD5 · raw file

  1. <?php if (!defined('APPLICATION')) exit();
  2. // Define the plugin:
  3. $PluginInfo['Gravatar'] = array(
  4. 'Name' => 'Gravatar',
  5. 'Description' => 'Implements Gravatar avatars for all users who have not uploaded their own custom profile picture & icon.',
  6. 'Version' => '1',
  7. 'Author' => "Mark O'Sullivan",
  8. 'AuthorEmail' => 'mark@vanillaforums.com',
  9. 'AuthorUrl' => 'http://vanillaforums.com'
  10. );
  11. class GravatarPlugin extends Gdn_Plugin {
  12. // Find all the places where UserBuilder is called, and make sure that there
  13. // is a related $UserPrefix.'Email' field pulled from the database.
  14. public function AddonCommentModel_BeforeGet_Handler(&$Sender) {
  15. $Sender->SQL->Select('iu.Email', '', 'InsertEmail');
  16. }
  17. public function ConversationModel_BeforeGet_Handler(&$Sender) {
  18. $Sender->SQL->Select('lmu.Email', '', 'LastMessageEmail');
  19. }
  20. public function ConversationMessageModel_BeforeGet_Handler(&$Sender) {
  21. $Sender->SQL->Select('iu.Email', '', 'InsertEmail');
  22. }
  23. public function ActivityModel_BeforeGet_Handler(&$Sender) {
  24. $Sender->SQL
  25. ->Select('au.Email', '', 'ActivityEmail')
  26. ->Select('ru.Email', '', 'RegardingEmail');
  27. }
  28. public function ActivityModel_BeforeGetNotifications_Handler(&$Sender) {
  29. $Sender->SQL
  30. ->Select('au.Email', '', 'ActivityEmail')
  31. ->Select('ru.Email', '', 'RegardingEmail');
  32. }
  33. public function ActivityModel_BeforeGetComments_Handler(&$Sender) {
  34. $Sender->SQL->Select('au.Email', '', 'ActivityEmail');
  35. }
  36. public function UserModel_BeforeGetActiveUsers_Handler(&$Sender) {
  37. $Sender->SQL->Select('u.Email');
  38. }
  39. public function DiscussionModel_BeforeGetID_Handler(&$Sender) {
  40. $Sender->SQL->Select('iu.Email', '', 'InsertEmail');
  41. }
  42. public function CommentModel_BeforeGet_Handler(&$Sender) {
  43. $Sender->SQL->Select('iu.Email', '', 'InsertEmail');
  44. }
  45. public function CommentModel_BeforeGetNew_Handler(&$Sender) {
  46. $Sender->SQL->Select('iu.Email', '', 'InsertEmail');
  47. }
  48. public function Setup() {
  49. // No setup required.
  50. }
  51. }
  52. if (!function_exists('UserBuilder')) {
  53. /**
  54. * Override the default UserBuilder function with one that switches the photo
  55. * out with a gravatar url if the photo is empty.
  56. */
  57. function UserBuilder($Object, $UserPrefix = '') {
  58. $User = new stdClass();
  59. $UserID = $UserPrefix.'UserID';
  60. $Name = $UserPrefix.'Name';
  61. $Photo = $UserPrefix.'Photo';
  62. $Email = $UserPrefix.'Email';
  63. $User->UserID = $Object->$UserID;
  64. $User->Name = $Object->$Name;
  65. $User->Photo = property_exists($Object, $Photo) ? $Object->$Photo : '';
  66. $Protocol = (strlen($_SERVER['HTTPS']) > 0 || $_SERVER['SERVER_PORT'] == 443) ? 'https://secure.' : 'http://www.';
  67. if ($User->Photo == '' && property_exists($Object, $Email)) {
  68. $User->Photo = $Protocol.'gravatar.com/avatar.php?'
  69. .'gravatar_id='.md5(strtolower($Object->$Email))
  70. .'&amp;default='.urlencode(Asset(Gdn::Config('Plugins.Gravatar.DefaultAvatar', 'plugins/Gravatar/default.gif'), TRUE))
  71. .'&amp;size='.Gdn::Config('Garden.Thumbnail.Width', 40);
  72. }
  73. return $User;
  74. }
  75. }