api.gravatar.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Gravatar API
  4. */
  5. /**
  6. * Get gravatar URL by some email
  7. *
  8. * @param string $email user email
  9. * @param bool $secure use HTTPS for API interraction?
  10. * @param string $avatarService gravatar|libravatar|etc
  11. *
  12. * @return string
  13. */
  14. function gravatar_GetUrl($email, $secure = false, $avatarService = '') {
  15. $hash = strtolower($email);
  16. $hash = md5($hash);
  17. $proto = ($secure) ? 'https' : 'http';
  18. switch ($avatarService) {
  19. case 'gravatar':
  20. $baseUrl = 'gravatar.com/avatar/';
  21. break;
  22. case 'libravatar':
  23. $baseUrl = 'seccdn.libravatar.org/avatar/';
  24. break;
  25. default:
  26. $baseUrl = 'seccdn.libravatar.org/avatar/';
  27. break;
  28. }
  29. $result = $proto . '://' . $baseUrl . $hash;
  30. return ($result);
  31. }
  32. /**
  33. * Function that returns avatar code by user email
  34. *
  35. * @global object $ubillingConfig
  36. * @param string $email user email
  37. * @param int $size user avatar size
  38. * @param string $class custom image class
  39. * @param title $title custom image title
  40. *
  41. * @return string
  42. */
  43. function gravatar_GetAvatar($email, $size = '64', $class = '', $title = '') {
  44. global $ubillingConfig;
  45. $cachePath = DATA_PATH . 'avatars/';
  46. $gravatarOption = $ubillingConfig->getAlterParam('GRAVATAR_DEFAULT');
  47. $gravatarCacheTime = $ubillingConfig->getAlterParam('GRAVATAR_CACHETIME');
  48. $avatarService = $ubillingConfig->getAlterParam('GRAVATAR_SERVICE');
  49. $getsize = ($size) ? '&s=' . $size : '';
  50. //option not set
  51. if (!$gravatarOption) {
  52. $gravatarOption = 'monsterid';
  53. }
  54. $useSSL = ($gravatarCacheTime) ? false : true; //avoid mixed content issues on disabled caching cases
  55. $url = gravatar_GetUrl($email, $useSSL, $avatarService);
  56. $fullUrl = $url . '?d=' . $gravatarOption . $getsize;
  57. //avatar caching to local FS.
  58. if ($gravatarCacheTime) {
  59. $cacheTime = time() - ($gravatarCacheTime * 86400); //Expire time. Option in days.
  60. $avatarHash = md5($fullUrl) . '.jpg';
  61. $fullCachedPath = $cachePath . $avatarHash;
  62. $updateCache = true;
  63. if (file_exists($fullCachedPath)) {
  64. $updateCache = false;
  65. if ((filemtime($fullCachedPath) > $cacheTime)) {
  66. $updateCache = false;
  67. } else {
  68. $updateCache = true;
  69. }
  70. } else {
  71. $updateCache = true;
  72. }
  73. if ($updateCache) {
  74. $gravatarApi = new OmaeUrl($fullUrl);
  75. $remoteAvatar = $gravatarApi->response();
  76. if (!empty($remoteAvatar)) {
  77. file_put_contents($fullCachedPath, $remoteAvatar);
  78. }
  79. }
  80. $fullUrl = $fullCachedPath;
  81. }
  82. $result = wf_tag('img', false, $class, 'src="' . $fullUrl . '" alt="avatar" title="' . $title . '"');
  83. return ($result);
  84. }
  85. /**
  86. * Get framework user email
  87. *
  88. * @param string $username rcms user login
  89. *
  90. * @return string
  91. */
  92. function gravatar_GetUserEmail($username) {
  93. $storePath = DATA_PATH . "users/";
  94. if (file_exists($storePath . $username)) {
  95. $userContent = file_get_contents($storePath . $username);
  96. $userData = unserialize($userContent);
  97. $result = $userData['email'];
  98. } else {
  99. $result = '';
  100. }
  101. return ($result);
  102. }
  103. /**
  104. * Shows avatar for some framework user - use only this in production!
  105. *
  106. * @param string $username rcms user login
  107. * @param int $size - size of returning avatar
  108. * @param string $class - class of image body
  109. * @param string $title - title of avatar image
  110. *
  111. * @return string
  112. */
  113. function gravatar_ShowAdminAvatar($username, $size = '', $class = '', $title = '') {
  114. $adminEmail = gravatar_GetUserEmail($username);
  115. if ($adminEmail) {
  116. $result = gravatar_GetAvatar($adminEmail, $size, $class, $title);
  117. } else {
  118. $result = wf_img('skins/admava.png', $title);
  119. }
  120. return ($result);
  121. }