* @copyright Narno.com
*/
define(GRAVATAR_URL, 'http://www.gravatar.com/avatar.php?gravatar_id=%s');
define(GRAVATAR_MAX_SIZE, 80);
/**
* Implementation of hook_perm().
* Defines a few access roles utilized in this module.
*/
function gravatar_perm() {
return array('use gravatar');
}
/**
* Implementation of hook_help().
* Displays helpful descriptions and hints on necessary pages.
*/
function gravatar_help($section) {
switch ($section) {
case 'admin/user/gravatar':
// picture enabled
$help_picture_disabled = '';
if (variable_get('user_pictures', 0) == FALSE) {
$help_picture_disabled = '
'. t("Make sure user picture support is enabled at users settings page.", array('@link' => '/admin/user/settings')) .'';
}
$output = '
'. t("The Gravatar module display the user's Gravatar in comments.") . $help_picture_disabled .'
'."\n";
$output .= '' . "\n";
$output .= "\t" . '- '. t("To enable Gravatar and authorize users (anonymous or registered) to dipslay their Gravatar, check the users access.", array('@link' => '/admin/user/access#module-gravatar')) .'
' . "\n";
$output .= "\t" . '- '. t("You can define avatars max size and the default picture on the users settings page.", array('@link' => '/admin/user/settings')) .'
' . "\n";
$output .= '
';
return $output;
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('Gravatar settings.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('gravatar_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM
);
$items[] = array(
'path' => 'admin/user/gravatar/general',
'title' => t('General settings'),
'type' => MENU_DEFAULT_LOCAL_TASK
);
$items[] = array(
'path' => 'admin/user/gravatar/advanced',
'title' => t('Advanced settings'),
'description' => t('Advanced settings.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('gravatar_admin_advanced_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_LOCAL_TASK
);
}
return $items;
}
/**
* admin settings for the Gravatar module
*/
function gravatar_admin_settings() {
$modules_path = drupal_get_path('module', 'gravatar');
// picture enabled
if (variable_get('user_pictures', 0)) {
$picture_disabled = FALSE;
} else {
$picture_disabled = TRUE;
}
$form = array();
// picture integration
$form['gravatar_prepend'] = array(
'#type' => 'fieldset',
'#title' => t('How to insert the Gravatar picture in comment ?'),
'#collapsible' => TRUE,
'#collapsed' => $picture_disabled
);
$form['gravatar_prepend']['gravatar_prepend'] = array(
'#type' => 'radios',
'#default_value' => variable_get('gravatar_prepend', TRUE),
'#options' => array(
TRUE => t('Prepend the Gravatar picture to the comment'),
FALSE => t('Leave it up to the theme to add') .' ('. t('only if the theme support Gravatar') .')'
),
'#description' => t('Preprend is default option.'),
'#disabled' => $picture_disabled
);
// picture settings
if (variable_get('user_pictures', 0)) {
$user_picture_default = variable_get('user_picture_default', '');
$user_picture_dimensions = variable_get('user_picture_dimensions', '85x85');
$gravatar_pictures = '';
if (!empty($user_picture_default)) {
// fix absolute URL
if (!preg_match('/^[a-z]+:\/\//', $user_picture_default)) {
global $base_url;
$user_picture_default = $base_url . '/' . $user_picture_default;
}
$gravatar_pictures .= ' - '. ' ' . t("as default picture") .'
';
}
if (!empty($user_picture_dimensions)) {
$gravatar_pictures .= ' - '. t("Picture maximum dimensions") .' : '. variable_get('user_picture_dimensions', '85x85') . ' ' . t("pixels") .'
';
}
$gravatar_pictures .= '
';
$gravatar_pictures .= ''. t("You can modify this settings on users settings page.", array('@link' => '/admin/user/settings')) .'
';
$form['gravatar_pictures'] = array(
'#type' => 'fieldset',
'#title' => t('Pictures settings'),
'#description' => $gravatar_pictures,
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
}
// rating
$form['gravatar_rating'] = array(
'#type' => 'fieldset',
'#title' => t('Authorized rating'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
$form['gravatar_rating']['gravatar_rating'] = array(
'#type' => 'radios',
'#default_value' => variable_get('gravatar_rating', ''),
'#options' => array(
'G' => theme('image', $modules_path . '/rating/g.gif', 'G', 'G', '', FALSE) . ' ' . t('rated Gravatars are suitable for display on all websites with any audience type'),
'PG' => theme('image', $modules_path . '/rating/pg.gif', 'PG', 'PG', '', FALSE) . ' ' . t('rated Gravatars contain may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence'),
'R' => theme('image', $modules_path . '/rating/r.gif', 'R', 'R', '', FALSE) . ' ' . t('rated Gravatars may contain such things as harsh profanity, intense violence, nudity, or hard drug use'),
'X' => theme('image', $modules_path . '/rating/x.gif', 'X', 'X', '', FALSE) . ' ' . t('rated Gravatars may contain hardcore sexual imagery or extremely disturbing violence'),
'' => t('None')
),
'#disabled' => $picture_disabled
);
return system_settings_form($form);
}
/**
* admin settings for the Gravatar module (advanced)
*/
function gravatar_admin_advanced_settings() {
$form = array();
$form['gravatar_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['gravatar_advanced']['gravatar_url'] = array(
'#type' => 'textfield',
'#title' => t('Gravatar URL'),
'#default_value' => variable_get('gravatar_url', GRAVATAR_URL),
'#description' => t("If you are not sure don't modify this !")
);
return system_settings_form($form);
}
/**
* Implementation of hook_comment().
*/
function gravatar_comment(&$comment, $op) {
$comment_account = user_load(array('uid' => $comment->uid));
// allowed to use gravatar (for ananymous and users) and enabled (for users)
if (($comment_account->uid == 0) && user_access('use gravatar', $comment_account)
|| (($comment_account->uid != 0) && (user_access('use gravatar', $comment_account) && $comment_account->gravatar_enabled))) {
switch ($op) {
case 'view':
// hack for authenticated users
if ($comment_account->uid != 0) {
$comment->mail = $comment_account->mail;
}
// check email address
if (!empty($comment->mail)) {
// Gravatar url
$grav_url = _gravatar_build_url($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 (variable_get('gravatar_prepend', TRUE)) {
$comment->comment = $comment->gravatar . $comment->comment;
}
}
break;
case 'form':
$comment['mail']['#description'] .= '
'. t('If you have a Gravatar account, used to display your avatar.', array('@gravatar-website' => url('http://www.gravatar.com'))) .'';
return $comment;
break;
}
}
}
/**
* Implementation of hook_user()
*/
function gravatar_user($op, &$edit, &$account, $category = NULL) {
if (variable_get('user_pictures', 0) && user_access('use gravatar')) {
switch ($op) {
case 'form':
if ($category == 'account') {
$grav_url = _gravatar_build_url($account->mail);
$form = array();
$form['picture']['gravatar_preview'] = array(
'#value' => theme('gravatar', $grav_url)
);
$form['picture']['gravatar_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Use my Gravatar in comments'),
'#default_value' => $edit['gravatar_enabled'],
'#description' => t('If checked, display your Gravatar (You can see it at the right).'),
'#suffix' => ''
);
return $form;
}
break;
}
}
}
/**
* 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" . $picture . "
\n";
return $output;
}
/**
* build the Gravatar URL from e-mail
*/
function _gravatar_build_url($email) {
$grav_url = '';
// Gravatar url
$grav_url = sprintf(variable_get('gravatar_url', GRAVATAR_URL), md5($email));
// default picture
$default = variable_get('user_picture_default', '');
if (!empty($default)) {
// fix absolute URL
if (!preg_match('/^[a-z]+:\/\//', $default)) {
global $base_url;
$default = $base_url . '/' . $default;
}
$grav_url .= "&default=" . urlencode(url($default));
}
// rating
$rating = variable_get('gravatar_rating', '');
if (!empty($rating)) {
$grav_url .= "&rating=" . $rating;
}
// set the maximum Gravatar sized
// get the maximum allowed user picture dimensions from user settings
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if (!empty($maxwidth) && (integer)$maxwidth <= GRAVATAR_MAX_SIZE) {
$grav_url .= "&size=" . $maxwidth;
}
return $grav_url;
}
?>