gravatar.module in Gravatar integration 5
Same filename and directory in other branches
Integrates gravatar service for comment user pictures.
@author Arnaud 'Narno' Ligny <arnaud.ligny@narno.com> @author Dave Reid <http://drupal.org/user/53892>
File
gravatar.moduleView source
<?php
/**
* @file
* Integrates gravatar service for comment user pictures.
*
* @author Arnaud 'Narno' Ligny <arnaud.ligny@narno.com>
* @author Dave Reid <http://drupal.org/user/53892>
*/
/**
* Global default user picture (user.module)
*/
define('GRAVATAR_DEFAULT_GLOBAL', 1);
/**
* Default image provided by the Gravatar module.
*/
define('GRAVATAR_DEFAULT_MODULE', 2);
/**
* Default transparent image provided by the Gravatar module.
*/
define('GRAVATAR_DEFAULT_MODULE_CLEAR', 7);
/**
* Generated, unique gravatar.com identicon.
*/
define('GRAVATAR_DEFAULT_IDENTICON', 3);
/**
* Generated, unique gravatar.com wavatar.
*/
define('GRAVATAR_DEFAULT_WAVATAR', 4);
/**
* Generated, unique gravatar.com monster id.
*/
define('GRAVATAR_DEFAULT_MONSTERID', 5);
/**
* Gravatar.com logo.
*/
define('GRAVATAR_DEFAULT_LOGO', 6);
/**
* Implementation of hook_perm().
*/
function gravatar_perm() {
return array(
'use gravatar',
'disable own gravatar',
);
}
/**
* Implementation of hook_help().
*/
function gravatar_help($section) {
switch ($section) {
case 'admin/user/gravatar':
case 'admin/user/settings':
module_load_install('gravatar');
$requirements = gravatar_requirements('runtime');
if (!empty($requirements['gravatar'])) {
drupal_set_message(t('Please check the following potential issues: !issues', array(
'!issues' => $requirements['gravatar']['description'],
)), 'warning');
}
break;
}
}
/**
* Implementation of hook_menu().
*/
function gravatar_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/gravatar',
'title' => t('Gravatar'),
'description' => t('Administer gravatar integration.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'gravatar_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implementation of hook_comment().
*/
function gravatar_comment(&$comment, $op) {
$account = user_load(array(
'uid' => $comment->uid,
));
// allowed to use gravatar (for ananymous and users) and enabled (for users)
if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
switch ($op) {
case 'view':
// hack for authenticated users
if ($account->uid) {
$comment->mail = $account->mail;
}
// check email address
if (!empty($comment->mail)) {
// Gravatar url
$grav_url = _gravatar_get_gravatar(array(
'mail' => $comment->mail,
));
// set Gravatar for template
$comment->gravatar = theme('gravatar', $grav_url, $comment->name, $comment->homepage);
// if theme dosen't support Gravatar, insert picture in comment text
if (gravatar_var('prepend')) {
$comment->comment = $comment->gravatar . $comment->comment;
}
}
break;
case 'form':
$comment['mail']['#description'] .= '<br />' . t('If you have a <a href="@gravatar-website">Gravatar</a> account, used to display your avatar.', array(
'@gravatar-website' => url('http://www.gravatar.com'),
));
return $comment;
break;
}
}
}
/**
* Implementation of hook_form_alter().
*/
function gravatar_form_alter($form_id, &$form) {
// User edit account.
if ($form_id == 'user_edit' && $form['_category']['#value'] == 'account' && isset($form['picture']) && variable_get('user_pictures', 0) && ($account = $form['_account']['#value']) && user_access('use gravatar', $account)) {
// Add the default user picture preview.
if (!isset($form['picture']['current_picture']) && ($picture = theme('user_picture', $account))) {
$form['picture']['current_picture'] = array(
'#value' => $picture,
'#weight' => -10,
);
}
$form['picture']['gravatar_description'] = array(
'#value' => t('If you have a <a href="@gravatar-check">valid gravatar</a> for your e-mail address, it will replace your current user picture in comments.', array(
'@gravatar-website' => url('http://www.gravatar.com/'),
'@gravatar-check' => url('http://en.gravatar.com/site/check/' . $account->mail),
)),
'#access' => !isset($account->gravatar) || $account->gravatar,
);
$form['picture']['gravatar'] = array(
'#type' => 'checkbox',
'#title' => t('Replace my user picture in comments with the gravatar for my e-mail address.'),
'#default_value' => isset($account->gravatar) ? $account->gravatar : 1,
'#access' => user_access('disable own gravatar', $account),
);
}
}
/*function phptemplate_user_picture($account) {
return "<div class=\"picture\"><img src=\"". _gravatar_get_gravatar() . "\" /></div>";
}*/
/**
* print Gravatar
*/
function theme_gravatar($grav_url, $name = '', $homepage = '') {
$alt = t("@user's picture", array(
'@user' => $name ? $name : variable_get('anonymous', t('Anonymous')),
));
$picture = theme('image', $grav_url, $alt, $alt, '', FALSE);
if (!empty($homepage)) {
$picture = l($picture, $homepage, array(
'title' => t('View user website.'),
), NULL, NULL, FALSE, TRUE);
}
$output = "\n<div class=\"picture\">" . $picture . "</div>\n";
return $output;
}
/**
* Generate a gravatar URL.
*
* @param $options
* An associative array of additional options, with the following keys:
* - 'mail'
* A string with an e-mail address. Defaults to a random number.
* - 'default'
* A string with the default gravatar image parameter. Defaults to the
* result of _gravatar_get_default_image() with the current value of the
* gravatar_default variable.
* - 'size'
* An integer of the desired size of the image. Defaults to smallest size
* of the user_picture_dimensions variable.
* - 'rating'
* A string with a MPAA rating limit for the image. Can be 'G', 'PG', 'R',
* or 'X'. Defaults to 'G'.
* @return
* An URL-encoded string with the gravatar image.
*/
function _gravatar_get_gravatar($options = array()) {
// Merge default options.
$options += array(
'mail' => mt_rand(),
'default' => _gravatar_get_default_image(gravatar_var('default')),
'size' => _gravatar_get_size(),
'rating' => gravatar_var('rating'),
);
$hash = md5(drupal_strtolower($options['mail']));
$gravatar = gravatar_var('url') . $hash;
$gravatar .= '?d=' . urlencode($options['default']);
$gravatar .= '&s=' . $options['size'];
$gravatar .= '&r=' . $options['rating'];
return $gravatar;
}
/**
* Get the size in pixels of the gravatar.
*
* @return
* An integer representing a square image size in pixels.
*/
function _gravatar_get_size() {
static $size = NULL;
if (!isset($size)) {
$size = min(explode('x', variable_get('user_picture_dimensions', '85x85') . 'x512'));
}
return $size;
}
/**
* Get the default gravatar image.
*
* @param $index
* An integer index for selection.
* @return
* The default image for use in a Gravatar avatar URL.
*/
function _gravatar_get_default_image($index) {
global $base_url;
static $defaults = array();
if (!isset($defaults[$index])) {
$default = NULL;
switch ($index) {
case GRAVATAR_DEFAULT_GLOBAL:
$default = variable_get('user_picture_default', '');
if ($default && !valid_url($default, TRUE)) {
// Convert a relative global default picture URL to an absolute URL.
$default = $base_url . '/' . $default;
}
break;
case GRAVATAR_DEFAULT_MODULE:
$default = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/avatar.png';
break;
case GRAVATAR_DEFAULT_MODULE_CLEAR:
$default = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/avatar-clear.png';
break;
case GRAVATAR_DEFAULT_IDENTICON:
$default = 'identicon';
break;
case GRAVATAR_DEFAULT_WAVATAR:
$default = 'wavatar';
break;
case GRAVATAR_DEFAULT_MONSTERID:
$default = 'monsterid';
break;
case GRAVATAR_DEFAULT_LOGO:
$default = 'default';
break;
}
$defaults[$index] = $default;
}
// @todo Remove when stable.
if (!isset($defaults[$index])) {
watchdog('gravatar', t('Hit unwanted condition in _gravatar_get_default_image.'));
return NULL;
}
return $defaults[$index];
}
/**
* Administration settings form.
*
* @see system_settings_form()
*/
function gravatar_admin_settings() {
$form = array();
// Display settings.
$form['display'] = array(
'#type' => 'fieldset',
'#title' => t('Display'),
);
$form['display']['gravatar_prepend'] = array(
'#type' => 'radios',
'#title' => t('Display method'),
'#default_value' => gravatar_var('prepend'),
'#options' => array(
1 => t('Prepend the Gravatar picture to the comment'),
0 => t('Leave it up to the theme to add (<a href="@readme">Only if supported by your theme</a>)', array(
'@readme' => url(drupal_get_path('module', 'gravatar') . '/README.txt'),
)),
),
);
$form['display']['gravatar_size'] = array(
'#type' => 'item',
'#title' => t('Image size'),
'#description' => t('The preferred image size (maximum 512 pixels). This setting can be adjusted in the <a href="@user-picture-link">user pictures settings</a>.', array(
'@user-picture-link' => url('admin/user/settings', NULL, 'edit-user-picture-default'),
)),
'#value' => t('@sizex@size pixels', array(
'@size' => _gravatar_get_size(),
)),
);
$form['display']['gravatar_rating'] = array(
'#type' => 'select',
'#title' => t('Image maturity filter'),
'#default_value' => gravatar_var('rating'),
'#options' => drupal_map_assoc(array(
'G',
'PG',
'R',
'X',
)),
'#description' => theme('item_list', array(
t('G: Suitable for display on all websites with any audience type.'),
t('PG: May contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.'),
t('R: May contain such things as harsh profanity, intense violence, nudity, or hard drug use.'),
t('X: May contain hardcore sexual imagery or extremely disturbing violence.'),
)),
);
$form['display']['gravatar_default'] = array(
'#type' => 'radios',
'#title' => t('Default image'),
'#description' => t('Specifies an image that should be returned if either the requested e-mail address has no associated gravatar, or that gravatar has a rating higher than is allowed by the maturity filter.'),
'#options' => array(
GRAVATAR_DEFAULT_GLOBAL => t('Global default user image'),
GRAVATAR_DEFAULT_MODULE => t('Module default image (white background)'),
GRAVATAR_DEFAULT_MODULE_CLEAR => t('Module default image (transparent background)'),
GRAVATAR_DEFAULT_IDENTICON => t('Gravatar.com identicon (generated)'),
GRAVATAR_DEFAULT_WAVATAR => t('Gravatar.com wavatar (generated)'),
GRAVATAR_DEFAULT_MONSTERID => t('Gravatar.com monsterid (generated)'),
GRAVATAR_DEFAULT_LOGO => t('Gravatar.com logo'),
),
'#default_value' => gravatar_var('default'),
'#prefix' => '<div class="picture js-show">' . theme('image', '', t('Default picture example'), t('Default picture example'), array(
'id' => 'gravatar-imagepreview',
), FALSE) . '</div>',
);
// Add JavaScript and CSS to swap the defalut image previews.
drupal_add_js(drupal_get_path('module', 'gravatar') . '/gravatar.js');
drupal_add_css(drupal_get_path('module', 'gravatar') . '/gravatar.css');
// Add default picture options manually so we can disable individual radios.
foreach ($form['display']['gravatar_default']['#options'] as $option_index => $option_title) {
$option_element =& $form['display']['gravatar_default'][$option_index];
$option_element = array(
'#type' => 'radio',
'#title' => $option_title,
'#return_value' => $option_index,
'#default_value' => gravatar_var('default'),
'#parents' => array(
'gravatar_default',
),
'#disabled' => FALSE,
);
switch ($option_index) {
case GRAVATAR_DEFAULT_GLOBAL:
$option_element['#description'] = t('This setting can be adjusted in the <a href="@user-picture-link">user pictures settings</a>.', array(
'@user-picture-link' => url('admin/user/settings', NULL, 'edit-user-picture-default'),
));
// If the global user picture is empty, disable the respective option.
if (!variable_get('user_picture_default', '')) {
$option_element['#disabled'] = TRUE;
$option_element['#description'] = t('There currently is not a global default user picture specified.') . ' ' . $option_element['#description'];
}
break;
}
// Add an image to preview this default image option.
$attributes = array(
'id' => 'gravatar-imagepreview-' . $option_index,
// Hide the image if the respective option is disabled.
'class' => 'js-hide' . ($option_element['#disabled'] ? ' hide' : ''),
);
$option_element['#suffix'] = theme('image', _gravatar_get_gravatar(array(
'default' => _gravatar_get_default_image($option_index),
)), $option_title, $option_title, $attributes, FALSE);
}
// Advanced settings.
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#description' => t('Do not modify these options unless you know what you are doing!'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['gravatar_url'] = array(
'#type' => 'textfield',
'#title' => t('Gravatar URL'),
'#default_value' => variable_get('gravatar_url', 'http://www.gravatar.com/avatar/'),
);
return system_settings_form($form);
}
/**
* Internal default variables for gravatar_var().
*/
function gravatar_variables() {
return array(
'gravatar_rating' => 'G',
'gravatar_default' => GRAVATAR_DEFAULT_MODULE,
'gravatar_url' => 'http://www.gravatar.com/avatar/',
'gravatar_prepend' => 1,
);
}
/**
* Internal implementation of variable_get().
*/
function gravatar_var($name) {
static $defaults = NULL;
if (!isset($defaults)) {
$defaults = gravatar_variables();
}
$name = 'gravatar_' . $name;
// @todo Remove when stable.
if (!isset($defaults[$name])) {
watchdog('gravatar', t('Default variable for %variable not found.', array(
'%variable' => $name,
)), WATCHDOG_WARNING);
}
return variable_get($name, isset($defaults[$name]) ? $defaults[$name] : NULL);
}
Functions
Name | Description |
---|---|
gravatar_admin_settings | Administration settings form. |
gravatar_comment | Implementation of hook_comment(). |
gravatar_form_alter | Implementation of hook_form_alter(). |
gravatar_help | Implementation of hook_help(). |
gravatar_menu | Implementation of hook_menu(). |
gravatar_perm | Implementation of hook_perm(). |
gravatar_var | Internal implementation of variable_get(). |
gravatar_variables | Internal default variables for gravatar_var(). |
theme_gravatar | print Gravatar |
_gravatar_get_default_image | Get the default gravatar image. |
_gravatar_get_gravatar | Generate a gravatar URL. |
_gravatar_get_size | Get the size in pixels of the gravatar. |
Constants
Name | Description |
---|---|
GRAVATAR_DEFAULT_GLOBAL | Global default user picture (user.module) |
GRAVATAR_DEFAULT_IDENTICON | Generated, unique gravatar.com identicon. |
GRAVATAR_DEFAULT_LOGO | Gravatar.com logo. |
GRAVATAR_DEFAULT_MODULE | Default image provided by the Gravatar module. |
GRAVATAR_DEFAULT_MODULE_CLEAR | Default transparent image provided by the Gravatar module. |
GRAVATAR_DEFAULT_MONSTERID | Generated, unique gravatar.com monster id. |
GRAVATAR_DEFAULT_WAVATAR | Generated, unique gravatar.com wavatar. |