* * @author Matthias Adler * @license gpl v2 * * @see http://site.gravatar.com/site/implement */ define (GRAVATAR_SERVICEURL, 'http://www.gravatar.com/avatar.php?gravatar_id='); /** * implementation of hook_help * * @param (string) $path * path * @param (mixed) $arg * url components */ function gravatar_help($path, $arg) { switch ($path) { case 'admin/help#gravatar': $help .= '

' . t("Make sure !user_picture_support is enabled.", array('!user_picture_support' => l(t('user picture support'), 'admin/user/settings#edit-user-pictures-0-wrapper'))) . '

'; $help .= '

' . t("Global settings") . '

'; $help .= '

' . t("Go to the !gravatar_integration_settings and enable gravatar integration. ", array('!gravatar_integration_settings' => l(t("gravatar integration settings"), 'admin/user/gravatar'))); $help .= t("If enabled, all user images will be replaced with gravatars. If disabled, the site's default user picture settings are used.") . '

'; $help .= '

' . t("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.") . '

'; $help .= '

' . t("Per user settings") . '

'; $help .= '

' . t("In the user profile page, each authenticated user can choose to use his or her gravatar, or the uploaded user image. There the user may also enter an alternative email address for which to retrieve the registered gravatar.") . '

'; return $help; break; } } // gravatar_help /** * implementation of hook_perm * */ function gravatar_perm () { return array('administer gravatar'); } // gravatar_perm /** * implementation of hook_menu */ function gravatar_menu() { $items['admin/user/gravatar'] = array( 'title' => 'Gravatar', 'description' => t('Administer how gravatars are used.'), 'file' => 'gravatar.admin.inc', 'page callback' => 'drupal_get_form', 'page arguments' => array('gravatar_admin_settings'), 'access arguments' => array('administer gravatar'), 'type' => MENU_NORMAL_ITEM, ); return $items; } // gravatar_menu /** * override template_preprocess_user_picture * * replaces theme_user_picture function * * @param (array) $variables * template vars */ function gravatar_preprocess_user_picture (&$variables) { $variables['picture'] = ''; if (variable_get('user_pictures', 0)) { $account = $variables['account']; $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')))); // gravatar is enabled if (variable_get('gravatar_toggle', 0)) { // anon / or user uses his account email address if (empty($account->uid)) { $gravatar = gravatar_get_gravatar($account->mail); } else if ($account->gravatar_mode) { $gravatar = gravatar_get_gravatar( (empty($account->gravatar_email) ? $account->mail : $account->gravatar_email)); } // yeah yeah, redundancy else if (!empty($account->picture) && file_exists($account->picture)) { $picture = file_create_url($account->picture); } else if (variable_get('user_picture_default', '')) { $picture = variable_get('user_picture_default', ''); } } // old behavior from user.module else { if (!empty($account->picture) && file_exists($account->picture)) { $picture = file_create_url($account->picture); } // disable for anon user else if (variable_get('user_picture_default', '')) { $picture = variable_get('user_picture_default', ''); } } if (isset($gravatar)) { $size = variable_get('gravatar_displaysize', 48); $variables['picture'] = theme('image', $gravatar, $alt, $alt, array('width' => $size, 'height' => $size), false); } if (isset($picture)) { // enforce max dimensions for default image, too $real = getimagesize($picture); $dimension = explode('x', variable_get('user_picture_dimensions', '85x85')); $variables['picture'] = theme('image', $picture, $alt, $alt, array('width' => min($dimension[0], $real[0]), 'height' => min($dimension[0], $real[0])), FALSE); } // enable link to user profile whether gravatar is enabled or not if (!empty($account->uid) && user_access('access user profiles')) { $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE); $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes); } } } // gravatar_preprocess_user_picture /** * implementation of hook_user * * @param (string) $op * What kind of action is being performed. Possible values (in alphabetical order): * "after_update": The user object has been updated and changed. Use this if (probably along with 'insert') if you want to reuse some information from the user object. * "categories": A set of user information categories is requested. * "delete": The user account is being deleted. The module should remove its custom additions to the user object from the database. * "form": The user account edit form is about to be displayed. The module should present the form elements it wishes to inject into the form. * "insert": The user account is being added. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit. * "load": The user account is being loaded. The module may respond to this * "login": The user just logged in. * "logout": The user just logged out. and insert additional information into the user object. * "register": The user account registration form is about to be displayed. The module should present the form elements it wishes to inject into the form. * "submit": Modify the account before it gets saved. * "update": The user account is being changed. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit. * "validate": The user account is about to be modified. The module should validate its custom additions to the user object, registering errors as necessary. * "view": The user's account information is being displayed. The module should format its custom additions for display. * * @param (array) $edit * The array of form values submitted by the user. * @param (object) $account * The user object on which the operation is being performed. * @param (string) $category * The active category of user information being edited. */ function gravatar_user ($op, &$edit, &$account, $category = NULL) { if (variable_get('user_pictures', 0)) { if ( $op == 'form' && $category == 'account' && variable_get('gravatar_toggle', 0)) { // inject gravatar form $form['picture']['gravatar_mode'] = array( '#type' => 'checkbox', '#title' => t('Enable Gravatar'), '#description' => t('If enabled, replaces your user image with the avatar submitted to your gravatar.com account.'), '#default_value' => $edit['gravatar_mode'], '#options' => array( 0 => t('Disable'), 1 => t('Enable'), ), ); $form['picture']['gravatar_email'] = array( '#type' => 'textfield', '#title' => t('Email Address'), '#description' => t('Enter the email address under which you registered your avatar at gravatar.com. Leave blank to use your user account email setting.'), '#default_value' => $edit['gravatar_email'], '#element_validate' => array('gravatar_email_validate'), ); return $form; } } } // gravatar_user /** * custom validation hook for gravatar_email * * @param (array) $element * form api element * @param (array) $form_state * current state of form */ function gravatar_email_validate($element, &$form_state) { if ($form_state['values']['gravatar_mode']) { if (!empty($element['#value']) && !valid_email_address($element['#value'])) { form_error($element, t('Please enter a valid email address.')); } } } // gravatar_email_validate /** * generate a gravatar.com request url * * @param (string) $email * a valid email address * @return (string) * url encoded string */ function gravatar_get_gravatar ($email) { if ($email && valid_email_address($email)) { // base parameters $gravatar = GRAVATAR_SERVICEURL . md5($email); // optional parameters if ($size = variable_get('gravatar_displaysize', 48)) { $gravatar .= '&size=' . $size; } if ($rating = variable_get('gravatar_displayrating', 'PG')) { $gravatar .= '&rating=' . $rating; } switch (variable_get('gravatar_imagedefault', 0)) { case 1: $path = variable_get('user_picture_default', ''); break; case 2: global $base_url; $path = $base_url . '/' . drupal_get_path('module', 'gravatar') . '/avatar.png'; break; } if (isset($path)) { $gravatar .= '&default=' . urlencode($path); } return $gravatar; } return false; } // gravatar_get_gravatar