PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/user_avatar/api.php

https://github.com/mmakaay/Modules
PHP | 195 lines | 115 code | 33 blank | 47 comment | 28 complexity | aa463c81a0b941eb24f349202bb23b01 MD5 | raw file
  1. <?php
  2. // Add avatar images to Phorum messages.
  3. function mod_user_avatar_apply_avatar_to_messages($messages)
  4. {
  5. $PHORUM = $GLOBALS['PHORUM'];
  6. // If the user doesn't want to show avatars, we are done.
  7. if (mod_user_avatar_current_user_has_permission('disable') &&
  8. !empty($PHORUM["user"]["mod_user_avatar"]["disable_avatar_display"])) {
  9. return $messages;
  10. }
  11. // If the permission settings are set to only display moderator avatars
  12. // if a user is moderator for the active forum, then retrieve a list
  13. // of moderators.
  14. $moderators = NULL;
  15. if ($PHORUM['mod_user_avatar']['permission_create'] == AVATAR_PERM_MODERATOR &&
  16. !empty($PHORUM['mod_user_avatar']['moderator_only_in_mod_forums'])) {
  17. $moderators = phorum_api_user_list_moderators();
  18. }
  19. $file_url_template = phorum_get_url(PHORUM_FILE_URL, "file=%file_id%");
  20. $cache = array();
  21. // Retrieve the data for the involved users.
  22. // Sometimes, the user data will already be filled in the message
  23. // data. For those messages, we will not retrieve fresh data.
  24. $user_ids = array();
  25. foreach ($messages as $message) {
  26. if (empty($message['user']) && !empty($message['user_id'])) {
  27. $user_ids[$message['user_id']] = $message['user_id'];
  28. }
  29. }
  30. $users = phorum_api_user_get($user_ids);
  31. // Apply avatars to all messages in the array.
  32. foreach ($messages as $messageid => $message)
  33. {
  34. // We start out with no avatar.
  35. $messages[$messageid]["MOD_USER_AVATAR"] = FALSE;
  36. // Only registered users can have an avatar.
  37. if (empty($message["user_id"]))
  38. {
  39. // We do apply the default avatar for anonymous users though.
  40. $url = $PHORUM['mod_user_avatar']['default_avatar'];
  41. $messages[$messageid]["MOD_USER_AVATAR"] = $url;
  42. continue;
  43. }
  44. // Use the cached avatar if we have one.
  45. if (isset($cache[$message["user_id"]]))
  46. {
  47. if ($cache[$message["user_id"]]) {
  48. $url = $cache[$message["user_id"]];
  49. $messages[$messageid]["MOD_USER_AVATAR"] = $url;
  50. }
  51. continue;
  52. }
  53. // Handle special permission, where the avatar is only shown
  54. // for moderators that actually have moderator permission for
  55. // the current forum.
  56. if ($moderators!==NULL && !isset($moderators[$message['user_id']])){
  57. continue;
  58. }
  59. // Retrieve the author's user information.
  60. // The user information is already available in the message.
  61. if (isset($message['user'])) {
  62. $author = $message['user'];
  63. }
  64. // The user information was looked up earlier successfully.
  65. elseif (isset($users[$message['user_id']])) {
  66. $author = $users[$message['user_id']];
  67. }
  68. // No user data found.
  69. else {
  70. $author = NULL;
  71. }
  72. // In case only admins can have an avatar, check if the user
  73. // is an admin or not.
  74. if ($PHORUM['mod_user_avatar']['permission_create'] == AVATAR_PERM_ADMIN) {
  75. if (empty($author) || empty($author['admin'])) continue;
  76. }
  77. // From here on, use the default avatar image if the user
  78. // has no avatar set.
  79. $url = $PHORUM['mod_user_avatar']['default_avatar'];
  80. $messages[$messageid]["MOD_USER_AVATAR"] = $url;
  81. // Without author information at hand, nothing can be done below here.
  82. if (empty($author)) {
  83. continue;
  84. }
  85. // If the author has no avatar enabled, we're done.
  86. if (empty($author["mod_user_avatar"]["avatar"]) ||
  87. $author["mod_user_avatar"]["avatar"] == -1) {
  88. $cache[$message["user_id"]] = $url; // negative caching.
  89. continue;
  90. }
  91. // Handle Gravatar.
  92. if ($author["mod_user_avatar"]["avatar"] == -2) {
  93. $url = mod_user_avatar_get_gravatar_url($author);
  94. }
  95. // Handle standard avatar.
  96. else {
  97. $file_id = $author["mod_user_avatar"]["avatar"];
  98. $url = str_replace('%file_id%', $file_id, $file_url_template);
  99. }
  100. $messages[$messageid]["MOD_USER_AVATAR"] = $url;
  101. // Cache the info, in case we encounter this user more
  102. // often in the loop.
  103. $cache[$message["user_id"]] = $url; // positive caching
  104. }
  105. return $messages;
  106. }
  107. // Add avatar images to a user's data.
  108. function mod_user_avatar_apply_avatar_to_user($profile)
  109. {
  110. global $PHORUM;
  111. // Setup the default avatar image as a starting point.
  112. $url = $PHORUM['mod_user_avatar']['default_avatar'];
  113. $profile['MOD_USER_AVATAR'] = $url;
  114. // Check if we have an avatar for this user.
  115. if (empty($profile["mod_user_avatar"]["avatar"]) ||
  116. $profile["mod_user_avatar"]["avatar"] == -1) {
  117. return $profile;
  118. }
  119. // Add a Gravatar to the profile data.
  120. if ($profile["mod_user_avatar"]["avatar"] == -2) {
  121. $profile["MOD_USER_AVATAR"] =
  122. mod_user_avatar_get_gravatar_url($profile);
  123. return $profile;
  124. }
  125. // Add a standard avatar to the profile data.
  126. $file_id = $profile['mod_user_avatar']["avatar"];
  127. $profile["MOD_USER_AVATAR"] = phorum_get_url(
  128. PHORUM_FILE_URL,
  129. "file=$file_id"
  130. );
  131. return $profile;
  132. }
  133. /**
  134. * Format the Gravatar URL for a user.
  135. *
  136. * @param array $user
  137. * Phorum user data. This should at least contain the field "email".
  138. * @param string $alternative_url
  139. * An alternative image URL to use in case there is no Gravatar
  140. * available on the Gravatar server.
  141. *
  142. * @return string
  143. * The URL for the Gravatar image.
  144. */
  145. function mod_user_avatar_get_gravatar_url($user, $alternative_url = NULL)
  146. {
  147. global $PHORUM;
  148. if (empty($PHORUM['mod_user_avatar']['gravatar_enabled'])) {
  149. return NULL;
  150. }
  151. $w = $PHORUM["mod_user_avatar"]["max_width"];
  152. $h = $PHORUM["mod_user_avatar"]["max_height"];
  153. $size = $w > $h ? $h : $w;
  154. $url = 'http://www.gravatar.com/avatar/' .
  155. md5(strtolower(trim($user['email']))) .
  156. "?s=$size";
  157. if ($alternative_url !== NULL) {
  158. $url .= 'd=' . urlencode($alternative_url);
  159. }
  160. return $url;
  161. }
  162. ?>