gravatar.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/gravatar/gravatar.module

Integrates gravatar service for user pictures.

@author Arnaud Ligny <http://drupal.org/user/141690> @author Matthias Adler <http://drupal.org/user/123779> @author Dave Reid <http://drupal.org/user/53892> @link http://site.gravatar.com/site/implement

Constants

NameDescription
GRAVATAR_DEFAULT_GLOBALGlobal default user picture (user.module)
GRAVATAR_DEFAULT_IDENTICONGenerated, unique gravatar.com identicon.
GRAVATAR_DEFAULT_LOGOGravatar.com logo.
GRAVATAR_DEFAULT_MODULEDefault image provided by the Gravatar module.
GRAVATAR_DEFAULT_MODULE_CLEARDefault transparent image provided by the Gravatar module.
GRAVATAR_DEFAULT_MONSTERIDGenerated, unique gravatar.com monster id.
GRAVATAR_DEFAULT_MYSTERY_MANGravatar.com Mystery Man.
GRAVATAR_DEFAULT_RETROGravatar.com retro 8-bit faces.
GRAVATAR_DEFAULT_WAVATARGenerated, unique gravatar.com wavatar.
GRAVATAR_SIZE_MAXMaximum Gravatar image size in pixels.
GRAVATAR_URLThe default URL for fetching Gravatars.
GRAVATAR_URL_SSLThe default URL for fetching Gravatars via SSL.

Functions & methods

NameDescription
gravatar_form_comment_form_alterImplements hook_form_FORM_ID_alter().
gravatar_form_user_profile_form_alterImplements hook_form_FORM_ID_alter().
gravatar_get_gravatarGenerate a gravatar URL.
gravatar_helpImplements hook_help().
gravatar_menuImplements hook_menu().
gravatar_permImplements hook_perm().
gravatar_preprocess_user_pictureOverride template_preprocess_user_picture() to display user pictures with Gravatar integration.
gravatar_varInternal implementation of variable_get().
gravatar_variablesInternal default variables for gravatar_var().
_gravatar_get_account_user_pictureDecide which user picture should be displayed for a user account.
_gravatar_get_default_imageGet the default gravatar image.
_gravatar_get_gravatar_imageFetch a gravatar image.
_gravatar_get_sizeGet the size in pixels of the gravatar.
_gravatar_load_account

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Integrates gravatar service for user pictures.
  5. *
  6. * @author Arnaud Ligny <http://drupal.org/user/141690>
  7. * @author Matthias Adler <http://drupal.org/user/123779>
  8. * @author Dave Reid <http://drupal.org/user/53892>
  9. * @link http://site.gravatar.com/site/implement
  10. */
  11. /**
  12. * Global default user picture (user.module)
  13. */
  14. define('GRAVATAR_DEFAULT_GLOBAL', 1);
  15. /**
  16. * Default image provided by the Gravatar module.
  17. */
  18. define('GRAVATAR_DEFAULT_MODULE', 2);
  19. /**
  20. * Default transparent image provided by the Gravatar module.
  21. */
  22. define('GRAVATAR_DEFAULT_MODULE_CLEAR', 7);
  23. /**
  24. * Generated, unique gravatar.com identicon.
  25. */
  26. define('GRAVATAR_DEFAULT_IDENTICON', 3);
  27. /**
  28. * Generated, unique gravatar.com wavatar.
  29. */
  30. define('GRAVATAR_DEFAULT_WAVATAR', 4);
  31. /**
  32. * Generated, unique gravatar.com monster id.
  33. */
  34. define('GRAVATAR_DEFAULT_MONSTERID', 5);
  35. /**
  36. * Gravatar.com logo.
  37. */
  38. define('GRAVATAR_DEFAULT_LOGO', 6);
  39. /**
  40. * Gravatar.com Mystery Man.
  41. */
  42. define('GRAVATAR_DEFAULT_MYSTERY_MAN', 'mm');
  43. /**
  44. * Gravatar.com retro 8-bit faces.
  45. */
  46. define('GRAVATAR_DEFAULT_RETRO', 'retro');
  47. /**
  48. * The default URL for fetching Gravatars.
  49. */
  50. define('GRAVATAR_URL', 'http://www.gravatar.com/avatar/');
  51. /**
  52. * The default URL for fetching Gravatars via SSL.
  53. */
  54. define('GRAVATAR_URL_SSL', 'https://secure.gravatar.com/avatar/');
  55. /**
  56. * Maximum Gravatar image size in pixels.
  57. */
  58. define('GRAVATAR_SIZE_MAX', 512);
  59. /**
  60. * Implements hook_perm().
  61. */
  62. function gravatar_perm() {
  63. return array(
  64. 'administer gravatar',
  65. 'use gravatar',
  66. 'disable own gravatar',
  67. );
  68. }
  69. /**
  70. * Implements hook_help().
  71. */
  72. function gravatar_help($path, $arg) {
  73. switch ($path) {
  74. //case 'admin/help#gravatar':
  75. // @todo Improve this documentation.
  76. //$output = '<h3>' . t("Global settings") . '</h3>';
  77. //$output .= '<p>' . t("Go to the !gravatar_integration_settings and enable gravatar integration. If enabled, all user images will be replaced with gravatars. If disabled, the site's default user picture settings are used. The site administrator may set a default image width, the maximum allowed maturity level, and select the site's default user image, the default user image provided by this module, or the image provided by gravatar.com, for the case no avatar could be retrieved.", array('!gravatar_integration_settings' => l(t("gravatar integration settings"), 'admin/user/gravatar'))) .'</p>';
  78. //$output .= '<h3>' . t("Per user settings") . '</h3>';
  79. //$output .= '<p>' . t("In the user profile page, each authenticated user can choose to use his or her gravatar, or the uploaded user image.") . '</p>';
  80. //return $output;
  81. case 'admin/user/gravatar':
  82. case 'admin/user/settings':
  83. module_load_install('gravatar');
  84. gravatar_check_requirements();
  85. break;
  86. }
  87. }
  88. /**
  89. * Implements hook_menu().
  90. */
  91. function gravatar_menu() {
  92. $items['admin/user/gravatar'] = array(
  93. 'title' => 'Gravatar',
  94. 'description' => 'Administer Gravatar integration.',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('gravatar_admin_settings'),
  97. 'access arguments' => array('administer gravatar'),
  98. 'file' => 'gravatar.admin.inc',
  99. );
  100. return $items;
  101. }
  102. /**
  103. * Override template_preprocess_user_picture() to display user pictures with
  104. * Gravatar integration.
  105. *
  106. * @see template_preprocess_user_picture()
  107. * @see _gravatar_load_account()
  108. * @see _gravatar_get_account_user_picture()
  109. */
  110. function gravatar_preprocess_user_picture(&$variables) {
  111. $variables['picture'] = '';
  112. if (variable_get('user_pictures', 0)) {
  113. // Load the full user object since it is not provided with nodes, comments,
  114. // or views displays.
  115. $account = _gravatar_load_account($variables['account']);
  116. $picture = _gravatar_get_account_user_picture($account);
  117. if (!empty($picture)) {
  118. $alt = t("@user's picture", array('@user' => $account->name));
  119. $variables['picture'] = theme('image', $picture, $alt, $alt, NULL, FALSE);
  120. if ($account->uid && user_access('access user profiles')) {
  121. // Create link to the user's profile.
  122. $attributes = array('title' => t('View user profile.'));
  123. $variables['picture'] = l($variables['picture'], 'user/' . $account->uid, array('attributes' => $attributes, 'html' => TRUE));
  124. }
  125. elseif (!empty($account->homepage)) {
  126. // If user is anonymous, create link to the commenter's homepage.
  127. $attributes = array(
  128. 'title' => t('View user website.'),
  129. 'rel' => 'external nofollow',
  130. );
  131. $variables['picture'] = l($variables['picture'], $account->homepage, array('attributes' => $attributes, 'html' => TRUE));
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Decide which user picture should be displayed for a user account.
  138. *
  139. * @param $account
  140. * A user object.
  141. * @return
  142. * A string with the path to the user's picture.
  143. */
  144. function _gravatar_get_account_user_picture($account) {
  145. if (!empty($account->picture) && file_exists($account->picture)) {
  146. // If the user has an uploaded picture, use it first.
  147. return file_create_url($account->picture);
  148. }
  149. elseif (!user_access('use gravatar', $account) || (user_access('disable own gravatar', $account) && isset($account->gravatar) && !$account->gravatar)) {
  150. // If the user does not have access to use gravatars or has gravatars
  151. // disabled for their account, use the global default image.
  152. return variable_get('user_picture_default', '');
  153. }
  154. else {
  155. // Otherwise, show a gravatar with the appropraite default picture.
  156. $mail = $account->mail;
  157. $options = array();
  158. if (empty($mail)) {
  159. $options['force default'] = TRUE;
  160. // Use various fallbacks to provide a unique default gravatar.
  161. if (!empty($account->hostname)) {
  162. $mail = $account->hostname;
  163. }
  164. elseif (!empty($account->homepage)) {
  165. $mail = $account->homepage;
  166. }
  167. else {
  168. $mail = serialize($account);
  169. }
  170. }
  171. return gravatar_get_gravatar($mail, $options);
  172. }
  173. }
  174. function _gravatar_load_account($account) {
  175. // If this is a node or comment object, load the user object.
  176. if (!empty($account->nid) || !empty($account->cid) || empty($account->roles)) {
  177. $original_values = $account;
  178. // If a comment is being edited and previewed, the $account->uid is NULL.
  179. // @todo Remove when http://drupal.org/node/334826 is fixed in 6.x.
  180. if (!isset($account->uid)) {
  181. $account->uid = 0;
  182. }
  183. $account = $account->uid ? user_load($account->uid) : drupal_anonymous_user();
  184. // Load mail/homepage variable from an anonymous comment.
  185. if (!$account->uid) {
  186. $values = array(
  187. 'name' => variable_get('anonymous', t('Anonymous')),
  188. 'mail' => '',
  189. 'homepage' => '',
  190. 'hostname' => '',
  191. );
  192. foreach ($values as $value => $default_value) {
  193. if (empty($account->$value)) {
  194. $account->$value = !empty($original_values->$value) ? $original_values->$value : $default_value;
  195. }
  196. }
  197. }
  198. }
  199. return $account;
  200. }
  201. /**
  202. * Implements hook_form_FORM_ID_alter().
  203. *
  204. * @todo Improve message shown to user.
  205. */
  206. function gravatar_form_comment_form_alter(&$form, $form_state) {
  207. if (isset($form['mail']) && user_access('use gravatar', drupal_anonymous_user())) {
  208. $form['mail']['#description'] .= ' ' . t('If you have a <a href="@gravatar-website">Gravatar</a> account associated with the e-mail address you provide, it will be used to display your avatar.', array('@gravatar-website' => url('http://www.gravatar.com/')));
  209. }
  210. }
  211. /**
  212. * Implements hook_form_FORM_ID_alter().
  213. *
  214. * @todo Improve message shown to user.
  215. */
  216. function gravatar_form_user_profile_form_alter(&$form, $form_state) {
  217. if ($form['_category']['#value'] == 'account' && isset($form['picture']) && variable_get('user_pictures', 0) && ($account = $form['_account']['#value']) && user_access('use gravatar', $account)) {
  218. // Add the default user picture preview.
  219. if (!isset($form['picture']['current_picture']) && ($picture = theme('user_picture', $account))) {
  220. $form['picture']['current_picture'] = array(
  221. '#value' => $picture,
  222. '#weight' => -10,
  223. );
  224. }
  225. if (user_access('disable own gravatar', $account)) {
  226. $form['picture']['gravatar'] = array(
  227. '#type' => 'checkbox',
  228. '#title' => t('If you have a <a href="@gravatar-check">valid Gravatar</a> associated with your e-mail address, use it for your user picture.', array('@gravatar-check' => 'http://en.gravatar.com/site/check/' . $account->mail)),
  229. '#description' => t('Your Gravatar will not be shown if you upload a user picture.'),
  230. '#default_value' => isset($account->gravatar) ? $account->gravatar : 1,
  231. '#disabled' => !empty($account->picture),
  232. );
  233. }
  234. else {
  235. $form['picture']['gravatar'] = array(
  236. '#type' => 'item',
  237. '#value' => t('If you have a <a href="@gravatar-check">valid gravatar</a> associated with your e-mail address, it will be used for your user picture.', array('@gravatar-check' => 'http://en.gravatar.com/site/check/' . $account->mail)),
  238. '#description' => t('Your Gravatar will not be shown if you upload a user picture.'),
  239. );
  240. }
  241. }
  242. }
  243. /**
  244. * Generate a gravatar URL.
  245. *
  246. * @param $mail
  247. * A string with an e-mail address.
  248. * @param $options
  249. * An associative array of additional options, with the following keys:
  250. * - 'default'
  251. * A string with the default gravatar image parameter. Defaults to the
  252. * result of _gravatar_get_default_image() with the current value of the
  253. * gravatar_default variable.
  254. * - 'size'
  255. * An integer of the desired size of the image. Defaults to smallest size
  256. * of the user_picture_dimensions variable.
  257. * - 'rating'
  258. * A string with a MPAA rating limit for the image. Can be 'G', 'PG', 'R',
  259. * or 'X'. Defaults to 'G'.
  260. * - 'cache'
  261. * A boolean if TRUE, the resulting image will be cached. Defaults to FALSE.
  262. * This feature is not yet implemented.
  263. * @return
  264. * An URL-encoded string with the gravatar image.
  265. */
  266. function gravatar_get_gravatar($mail, $options = array()) {
  267. static $is_https;
  268. if (!isset($is_https)) {
  269. $is_https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
  270. }
  271. // Merge default options.
  272. $options += array(
  273. 'default' => _gravatar_get_default_image(gravatar_var('default')),
  274. 'size' => _gravatar_get_size(),
  275. 'rating' => variable_get('gravatar_rating', 'G'),
  276. 'cache' => FALSE,
  277. 'force default' => FALSE,
  278. );
  279. $hash = md5(drupal_strtolower($mail));
  280. // @todo Implement cache fetching.
  281. //if ($options['cache'] && gravatar_var('cache') && valid_email_address($mail)) {
  282. // if ($cached = cache_get($hash, 'gravatar')) {
  283. // return $cached;
  284. // }
  285. // elseif ($data = _gravatar_get_gravatar_image($mail)) {
  286. // cache_set($hash, $data, 'gravatar');
  287. // return $data;
  288. // }
  289. //}
  290. $gravatar = $is_https ? variable_get('gravatar_url_ssl', GRAVATAR_URL_SSL) : variable_get('gravatar_url', GRAVATAR_URL);
  291. $gravatar .= $hash . '.jpg';
  292. $query = array(
  293. 'd' => $options['default'],
  294. 's' => $options['size'],
  295. 'r' => $options['rating'],
  296. 'f' => $options['force default'] ? 'y' : '',
  297. );
  298. $query = array_filter($query);
  299. return url($gravatar, array('query' => $query));
  300. }
  301. /**
  302. * Get the size in pixels of the gravatar.
  303. *
  304. * @return
  305. * An integer representing a square image size in pixels.
  306. */
  307. function _gravatar_get_size() {
  308. static $size = NULL;
  309. if (!isset($size)) {
  310. $size = min(explode('x', variable_get('user_picture_dimensions', '85x85') . 'x' . GRAVATAR_SIZE_MAX));
  311. }
  312. return $size;
  313. }
  314. /**
  315. * Get the default gravatar image.
  316. *
  317. * @param $index
  318. * An integer index for selection.
  319. * @return
  320. * The default image for use in a Gravatar avatar URL.
  321. */
  322. function _gravatar_get_default_image($index) {
  323. global $base_url;
  324. static $defaults = array();
  325. if (!isset($defaults[$index])) {
  326. switch ($index) {
  327. case GRAVATAR_DEFAULT_GLOBAL:
  328. $default = variable_get('user_picture_default', '');
  329. if ($default && !valid_url($default, TRUE)) {
  330. // Convert a relative global default picture URL to an absolute URL.
  331. $default = $base_url . '/' . ltrim($default, '/');
  332. }
  333. break;
  334. case GRAVATAR_DEFAULT_MODULE:
  335. $default = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/avatar.png';
  336. break;
  337. case GRAVATAR_DEFAULT_MODULE_CLEAR:
  338. $default = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/avatar-clear.png';
  339. break;
  340. case GRAVATAR_DEFAULT_IDENTICON:
  341. $default = 'identicon';
  342. break;
  343. case GRAVATAR_DEFAULT_WAVATAR:
  344. $default = 'wavatar';
  345. break;
  346. case GRAVATAR_DEFAULT_MONSTERID:
  347. $default = 'monsterid';
  348. break;
  349. case GRAVATAR_DEFAULT_LOGO:
  350. $default = '';
  351. //$default = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/gravatar.jpg';
  352. break;
  353. case GRAVATAR_DEFAULT_MYSTERY_MAN:
  354. case GRAVATAR_DEFAULT_RETRO:
  355. case 404:
  356. $default = $index;
  357. break;
  358. default:
  359. // @todo Remove when stable.
  360. $default = '';
  361. trigger_error('Unwanted condition ' . check_plain(var_export($index, TRUE)) . ' in _gravatar_get_default_image().');
  362. }
  363. $defaults[$index] = $default;
  364. }
  365. return $defaults[$index];
  366. }
  367. /**
  368. * Fetch a gravatar image.
  369. *
  370. * @param $mail
  371. * A string with an e-mail address.
  372. * @return
  373. * An image if the e-mail has a gravatar, FALSE otherwise.
  374. */
  375. function _gravatar_get_gravatar_image($mail) {
  376. $url = gravatar_get_gravatar(array('mail' => $mail, 'cache' => FALSE));
  377. $request = drupal_http_request($url, array(), 'GET', NULL, 0);
  378. return ($request->code == '200' ? $request->data : FALSE);
  379. }
  380. /**
  381. * Internal default variables for gravatar_var().
  382. */
  383. function gravatar_variables() {
  384. return array(
  385. 'gravatar_default' => GRAVATAR_DEFAULT_MODULE,
  386. 'gravatar_rating' => 'G',
  387. 'gravatar_url' => GRAVATAR_URL,
  388. 'gravatar_url_ssl' => GRAVATAR_URL_SSL,
  389. 'gravatar_cache' => 0,
  390. // Deleted variables set to NULL so they can be removed during uninstall.
  391. 'gravatar_default_type' => NULL,
  392. 'gravatar_imagerating' => NULL,
  393. 'gravatar_displaysize' => NULL,
  394. 'gravatar_imagedefault' => NULL,
  395. 'gravatar_toggle' => NULL,
  396. 'gravatar_disabled_by_users' => NULL,
  397. 'gravatar_size' => NULL,
  398. 'gravatar_prepend' => NULL,
  399. );
  400. }
  401. /**
  402. * Internal implementation of variable_get().
  403. */
  404. function gravatar_var($name, $default = NULL) {
  405. static $defaults = NULL;
  406. if (!isset($defaults)) {
  407. $defaults = gravatar_variables();
  408. }
  409. $name = 'gravatar_' . $name;
  410. // @todo Remove when stable.
  411. if (!isset($defaults[$name])) {
  412. trigger_error('Default variable for ' . theme_placeholder($name) . ' not found.');
  413. }
  414. return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
  415. }