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